CaySimple - Homemade Android App Demo / Toolkit

New version of the simple Cayenne app – notably for the cayenne.java object.

Once you copy the cayenne.java app, the rest is easy.

First, create the object

myCayenne = new Cayenne(cayenneemail, cayennepassword, -7, ErrorStatusBox); // -7 for Mountain, -5 for Eastern, etc

Second, get authenticated.

myCayenne.getCayenneAuth();

Third (optional), get a list of all your devices and sensors

myCayenne.getThings();

This function fills some data structures

myCayenne.SensorArrayList is a list of the names of device and sensor, plus the deviceID and sensorID numbers, and hashmaps

myCayenne.NameIDhashmap

and

myCayenne.IDNamehashmap

to convert names to numbers and back.

Fourth, get some data the easy way.

You create the object for the data point with the deviceID and sensorID, and give it the name of the box to deposit the result when it arrives is 500ms or so. Your program can continue without suspending the user interface waiting for the data. When you want it refreshed, you call the update function.

myCayDataPoint = myCayenne.new CayDataPoint(deviceID, sensorID, (TextView) findViewById(R.id.textDP));

myCayDataPoint.update();

Fifth, get some data the fancy way.

The fancy way sets up a callback function, so your program will be alerted and you can do some post-processing to display, manipulate, and react to the data when it arrives in 500ms or so. This example changes some colors and stores the various bits of the data in boxes.

            myCayDataPoint.myResponse.addListener(new Cayenne.CayDataPointRecd() {
            public void CayDataPointRecdHandler_Callback() {

            myCayenne.AddtoBigBox("data handler");

            TextView tv = findViewById(R.id.textDPv);
            tv.setText(String.valueOf(myCayDataPoint.v));

            if (myCayDataPoint.v > 20) {
                tv.setBackgroundColor(Color.RED);
            } else {
                tv.setBackgroundColor(Color.WHITE);
            }

            tv = findViewById(R.id.textDPts);
            tv.setText(myCayDataPoint.ts);
            tv = findViewById(R.id.textDPunit);
            tv.setText(myCayDataPoint.unit);
            tv = findViewById(R.id.textDPdevice_type);
            tv.setText(myCayDataPoint.device_type);
        }
    });

    myCayDataPoint.update();

And Sixth, draw a graph of your data

You specify the box to draw the graph, 0 or 1 for left or right axis, color of the line, and lower/upper bounds, or just provide the same number if you don’t know the bounds.

myCayDataPoint.updategraph(gv, 0, Color.BLUE, 0.0, 0.0);

Okay, see the example code - easier than typing into this little box.

See the example code as the getCayenneAuth() and getThings() also have callback functions, to launch the next request when the previous one completes.

1 Like