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
Diff: Thermostat.cpp
- Revision:
- 3:58f1cc293901
- Parent:
- 1:3faa003ad6e6
--- a/Thermostat.cpp Fri Nov 08 20:10:52 2013 +0000 +++ b/Thermostat.cpp Sun Nov 10 02:18:02 2013 +0000 @@ -44,16 +44,25 @@ // DreamForce 2013 Tunables START -// -// Our default Latitude and Longitude -// -#define DEFAULT_LATITUDE 37.7842 -#define DEFAULT_LONGITUDE -122.4016 +// set our location +int location_index = 0; -// -// Name our endpoint something unique -// -#define DEFAULT_ENDPOINT_NAME "DreamForce Thermostat" +// Remoted Thermostats +// Thermostat 1: Austin TX 30.2500 –97.7500 +// Thermostat 2: Phoenix AZ 33.4500 -112.0667 +// Thermostat 3: Dallas TX 32.7758 -96.7967 +// Thermostat 4: Miami FL 25.7877 -80.2241 +// Thermostat 5: Chicago IL 41.8819 –87.6278 +// Thermostat 6: Memphis TN 35.1174 -89.9711 +// Thermostat 7: Seattle WA 47.6097 –122.3331 +// Thermostat 8: New York NY 40.6700 –73.9400 +// Thermostat 9: Raleigh NC 35.8189 –78.6447 +// Thermostat 10: Moscone 37.7842 –122.4016 +float latitudes[10] = { 30.2500, 33.4500, 32.7758, 25.7877, 41.8819, 35.1174, 47.6097, 40.6700, 35.8189, 37.7842}; +float longitudes[10] = { -97.7500, -112.0667, -96.7967, -80.2241, -87.6278, -89.9711, -122.3331, -73.9400, -78.6447, -122.4016}; +char *locations[10] = { "DF Thermostat-Austin TX", "DF Thermostat-Phoenix AZ", "DF Thermostat-Dallas TX", "DF Thermostat-Miami FL", + "DF Thermostat-Chicago IL", "DF Thermostat-Memphis TN", "DF Thermostat-Seattle WA", + "DF Thermostat-New York NY", "DF Thermostat-Raleigh NC", "DF Thermostat-Moscone Center"}; // DreamForce 2013 Tunables END @@ -61,7 +70,7 @@ #define DEFAULT_MAIN_LOOP_WAIT 2.0 // 2 seconds // JSON parsing support -#include "picojson.h" +#include "MbedJSONValue.h" // // Accelerometer Support @@ -85,7 +94,7 @@ // Thermostat SocketIO Support // #include "ThermostatSocketIO.h" -ThermostatSocketIO socketio(DEFAULT_ENDPOINT_NAME); +ThermostatSocketIO socketio(locations[location_index]); // Include LED Utils #include "Thermostat-LEDUtils.h" @@ -180,27 +189,24 @@ // Control Message Format: 5:::{"name":"control-device","args":[{"hello":"world"}]} // void Thermostat::parseAndActOnControlMessage(char *json) { - picojson::value v; - + MbedJSONValue result; + if (json != NULL && strlen(json) > 0 && strstr(json,"{") != NULL) { // move past the socket.io header char *json_proper = strstr(json,"{"); // parse the packet - string err = picojson::parse(v, json_proper, json_proper + strlen(json_proper)); - - // the args value is an array of json values - picojson::array list = v.get("args").get<picojson::array>(); - + parse(result,json_proper); + // loop through the array and parse/process each element - for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter) { + for(int i=0;i<result["args"].size();++i) { // Toggle LEDs - if ((*iter).get("led1") != NULL) led1.write(this->translateLEDStatus((*iter).get("led1").get<string>().c_str(),(int)led1)); - if ((*iter).get("led2") != NULL) led2.write(this->translateLEDStatus((*iter).get("led2").get<string>().c_str(),(int)led2)); - if ((*iter).get("led3") != NULL) led3.write(this->translateLEDStatus((*iter).get("led3").get<string>().c_str(),(int)led3)); - if ((*iter).get("led4") != NULL) led4.write(this->translateLEDStatus((*iter).get("led4").get<string>().c_str(),(int)led4)); - if ((*iter).get("reset") != NULL) this->resetDeviceStatus(); - if ((*iter).get("blink") != NULL) this->blinkAllLEDs(); + if (result["args"][i]["led1"].get<string>().size() > 0) led1.write(this->translateLEDStatus(result["args"][i]["led1"].get<string>().c_str(),(int)led1)); + if (result["args"][i]["led2"].get<string>().size() > 0) led2.write(this->translateLEDStatus(result["args"][i]["led2"].get<string>().c_str(),(int)led2)); + if (result["args"][i]["led3"].get<string>().size() > 0) led3.write(this->translateLEDStatus(result["args"][i]["led3"].get<string>().c_str(),(int)led3)); + if (result["args"][i]["led4"].get<string>().size() > 0) led4.write(this->translateLEDStatus(result["args"][i]["led4"].get<string>().c_str(),(int)led4)); + if (result["args"][i]["reset"].get<string>().size() > 0) this->resetDeviceStatus(); + if (result["args"][i]["blink"].get<string>().size() > 0) this->blinkAllLEDs(); // // 2013 DreamForce MiniHack - add code to look for a "text" message - send its contents to the LCD panel @@ -208,7 +214,7 @@ // // Answer: // - if ((*iter).get("text") != NULL) this->displayTextMessage((*iter).get("text").get<string>().c_str()); + if (result["args"][i]["text"].get<string>().size() > 0) this->displayTextMessage(result["args"][i]["text"].get<string>().c_str()); } } } @@ -412,6 +418,29 @@ } } + // if not connected... reconnect + if (!socketio.is_connected()){ + // announce + this->display("Re-connecting..."); + this->display_lcd("Re-connecting..."); + + // re-connect + if (this->connectWebSocketService()) { + // announce + this->display("Reconnect success"); + this->display_lcd("Reconnect: SUCCESS"); + this->turnRGBLEDGreen(); + this->resetAllLEDs(); + } + else { + // announce + this->display("Reconnect failure"); + this->display_lcd("Reconnect: FAILED"); + this->gracefullyDisconnect(); + this->turnRGBLEDRed(); + } + } + // if we are connected, send our status if (socketio.is_connected()) { // send status