DreamForce 2013 Hands-On Demo

Dependencies:   C12832_lcd EthernetInterface-ansond-patched HTTPClient-thermostat-remotes LM75B MMA7660 SocketIO WebSocketClient-ThermostatDemo mbed-rtos mbed picojson

Fork of ThermostatHandsOn by Doug Anson

Committer:
ansond
Date:
Mon Nov 11 20:35:13 2013 +0000
Revision:
7:57681d46485d
Parent:
0:6b56785dd2b6
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:6b56785dd2b6 1 #include "ThermostatSocketIO.h"
ansond 0:6b56785dd2b6 2
ansond 0:6b56785dd2b6 3 // Default SocketIO URL
ansond 0:6b56785dd2b6 4 #define DEFAULT_URL "mc-control-1.herokuapp.com"
ansond 0:6b56785dd2b6 5
ansond 0:6b56785dd2b6 6 // message counter
ansond 0:6b56785dd2b6 7 int counter = 1;
ansond 0:6b56785dd2b6 8
ansond 0:6b56785dd2b6 9 // constructor
ansond 0:6b56785dd2b6 10 ThermostatSocketIO::ThermostatSocketIO(char *devname) : SocketIO(DEFAULT_URL) {
ansond 0:6b56785dd2b6 11 this->device_name = devname;
ansond 0:6b56785dd2b6 12 }
ansond 0:6b56785dd2b6 13
ansond 0:6b56785dd2b6 14 // constructor
ansond 0:6b56785dd2b6 15 ThermostatSocketIO::ThermostatSocketIO(char * myurl, char *devname) : SocketIO(myurl) {
ansond 0:6b56785dd2b6 16 this->device_name = devname;
ansond 0:6b56785dd2b6 17 }
ansond 0:6b56785dd2b6 18
ansond 0:6b56785dd2b6 19 // reset the message counter
ansond 0:6b56785dd2b6 20 void ThermostatSocketIO::resetMessageCounter() {
ansond 0:6b56785dd2b6 21 counter = 1;
ansond 0:6b56785dd2b6 22 }
ansond 0:6b56785dd2b6 23
ansond 0:6b56785dd2b6 24 // emit a message (broadcast) to the SocketIO server
ansond 0:6b56785dd2b6 25 int ThermostatSocketIO::emit(float temp, float latitude, float longitude, float bat, int errorState, char *t_status) {
ansond 0:6b56785dd2b6 26 // create json string with acc/tmp data
ansond 0:6b56785dd2b6 27 char send[256];
ansond 0:6b56785dd2b6 28 char *json = this->createJSON(send,temp,latitude,longitude,bat,errorState,t_status);
ansond 0:6b56785dd2b6 29
ansond 0:6b56785dd2b6 30 // handle the message name
ansond 0:6b56785dd2b6 31 char message_name[32];
ansond 0:6b56785dd2b6 32 if (counter == 1)
ansond 0:6b56785dd2b6 33 // we need to register the device
ansond 0:6b56785dd2b6 34 sprintf(message_name,"register-device");
ansond 0:6b56785dd2b6 35 else
ansond 0:6b56785dd2b6 36 // we are simply issuing readings...
ansond 0:6b56785dd2b6 37 sprintf(message_name,"readings");
ansond 0:6b56785dd2b6 38
ansond 0:6b56785dd2b6 39 // increment our counter
ansond 0:6b56785dd2b6 40 ++counter;
ansond 0:6b56785dd2b6 41
ansond 0:6b56785dd2b6 42 // emit this json
ansond 0:6b56785dd2b6 43 return ((SocketIO *)this)->emit(message_name,json);
ansond 0:6b56785dd2b6 44 }
ansond 0:6b56785dd2b6 45
ansond 0:6b56785dd2b6 46 // Close the thermostat socket.io
ansond 0:6b56785dd2b6 47 bool ThermostatSocketIO::close() {
ansond 0:6b56785dd2b6 48 char buf[256];
ansond 0:6b56785dd2b6 49 char message_name[32];
ansond 0:6b56785dd2b6 50
ansond 0:6b56785dd2b6 51 // we need to register the device
ansond 0:6b56785dd2b6 52 sprintf(message_name,"disconnect");
ansond 0:6b56785dd2b6 53
ansond 0:6b56785dd2b6 54 // create the deregister JSON message
ansond 0:6b56785dd2b6 55 char *json = this->createDeregisterJSON(buf);
ansond 0:6b56785dd2b6 56
ansond 0:6b56785dd2b6 57 // now send...
ansond 0:6b56785dd2b6 58 int sent = ((SocketIO *)this)->emit(message_name,json);
ansond 0:6b56785dd2b6 59
ansond 0:6b56785dd2b6 60 // now close
ansond 0:6b56785dd2b6 61 return ((SocketIO *)this)->close();
ansond 0:6b56785dd2b6 62 }
ansond 0:6b56785dd2b6 63
ansond 0:6b56785dd2b6 64 // create the JSON to deregister the device
ansond 0:6b56785dd2b6 65 char *ThermostatSocketIO::createDeregisterJSON(char *buffer) {
ansond 0:6b56785dd2b6 66 // create the JSON to just register the device
ansond 0:6b56785dd2b6 67 sprintf(buffer,"[{ \"device_id\": \"%s\" } }]",this->device_name);
ansond 0:6b56785dd2b6 68
ansond 0:6b56785dd2b6 69 // return the JSON
ansond 0:6b56785dd2b6 70 return buffer;
ansond 0:6b56785dd2b6 71 }
ansond 0:6b56785dd2b6 72
ansond 0:6b56785dd2b6 73 // Create the JSON that is consumable by the demo webservice
ansond 0:6b56785dd2b6 74 char *ThermostatSocketIO::createJSON(char *buffer, float temp, float latitude, float longitude, float bat, int errorState, char *t_status) {
ansond 0:6b56785dd2b6 75 // create the message name
ansond 0:6b56785dd2b6 76 if (counter == 1) {
ansond 0:6b56785dd2b6 77 // create the JSON to just register the device
ansond 0:6b56785dd2b6 78 sprintf(buffer,"[\"%s\",{\"battery\": %d, \"city\": \"Austin\", \"country\": \"US\", \"device_id\": \"%s\", \"lat\": %4.6f, \"long\": %4.6f, \"status\": \"%s\", \"temp\": %4.1f, \"switch\": %d}]",
ansond 0:6b56785dd2b6 79 device_name, (int)bat, this->device_name, latitude, longitude, t_status, temp, errorState);
ansond 0:6b56785dd2b6 80 }
ansond 0:6b56785dd2b6 81 else {
ansond 0:6b56785dd2b6 82 // create the JSON
ansond 0:6b56785dd2b6 83 sprintf(buffer,"[{\"battery\": %d, \"city\": \"Austin\", \"country\": \"US\", \"device_id\": \"%s\", \"lat\": %4.6f, \"long\": %4.6f, \"status\": \"%s\", \"temp\": %4.1f, \"switch\": %d}]",
ansond 0:6b56785dd2b6 84 (int)bat, this->device_name, latitude, longitude, t_status, temp, errorState);
ansond 0:6b56785dd2b6 85 }
ansond 0:6b56785dd2b6 86
ansond 0:6b56785dd2b6 87 // return the JSON
ansond 0:6b56785dd2b6 88 return buffer;
ansond 0:6b56785dd2b6 89 }