Dreamforce 2013 MiniHack Thermostat Challenge - remotes

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

Fork of df-2013-minihack-thermostat-complete by MBED_DEMOS

Committer:
ansond
Date:
Mon Nov 11 22:46:25 2013 +0000
Revision:
6:33f71cf9a04f
Parent:
0:26c48388f725
updates

Who changed what in which revision?

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