MBED_DEMOS / Mbed 2 deprecated mbed_mqtt_endpoint_nxp

Dependencies:   C12832_lcd EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed

Committer:
ansond
Date:
Sat Mar 01 19:23:50 2014 +0000
Revision:
48:d3663434128d
Parent:
39:bd5b2bcd2dcc
Child:
79:67accc95965a
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:ae2a45502448 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:ae2a45502448 2 *
ansond 0:ae2a45502448 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:ae2a45502448 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:ae2a45502448 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:ae2a45502448 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:ae2a45502448 7 * furnished to do so, subject to the following conditions:
ansond 0:ae2a45502448 8 *
ansond 0:ae2a45502448 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:ae2a45502448 10 * substantial portions of the Software.
ansond 0:ae2a45502448 11 *
ansond 0:ae2a45502448 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:ae2a45502448 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:ae2a45502448 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:ae2a45502448 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:ae2a45502448 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:ae2a45502448 17 */
ansond 0:ae2a45502448 18
ansond 0:ae2a45502448 19 #include "MQTTTransport.h"
ansond 0:ae2a45502448 20 #include "MBEDEndpoint.h"
ansond 0:ae2a45502448 21
ansond 0:ae2a45502448 22 // MBED Light support
ansond 0:ae2a45502448 23 #include "MBEDLight.h"
ansond 0:ae2a45502448 24
ansond 2:90a84a216c58 25 // Emulated Resource Factory
ansond 2:90a84a216c58 26 #include "EmulatedResourceFactory.h"
ansond 2:90a84a216c58 27
ansond 0:ae2a45502448 28 // Emulated Actions we can act on
ansond 0:ae2a45502448 29 #include "EmulatedLightDimmerAction.h"
ansond 0:ae2a45502448 30 #include "EmulatedLightSwitchAction.h"
ansond 48:d3663434128d 31
ansond 48:d3663434128d 32 // string support
ansond 48:d3663434128d 33 #include <stdlib.h>
ansond 48:d3663434128d 34 #include <string.h>
ansond 22:f1002e5993c5 35
ansond 7:f570eb3f38cd 36 // shutdown endpoint reference
ansond 7:f570eb3f38cd 37 extern void closedown(int code);
ansond 0:ae2a45502448 38
ansond 0:ae2a45502448 39 // default constructor
ansond 0:ae2a45502448 40 MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) {
ansond 0:ae2a45502448 41 bool success = true;
ansond 0:ae2a45502448 42 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 0:ae2a45502448 43 this->m_error_handler = error_handler;
ansond 32:a7b3c285446c 44 if (success) this->initEndpointName();
ansond 0:ae2a45502448 45 if (success) this->logger()->turnLEDBlue();
ansond 30:5c670ec5d203 46 if (success)this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:ae2a45502448 47 if (success) success = this->initializeEthernet(ethernet);
ansond 0:ae2a45502448 48 if (success) this->logger()->turnLEDYellow();
ansond 33:8302b02413bd 49 if (success)this->m_map = new MBEDToIOCResourceMap(error_handler);
ansond 30:5c670ec5d203 50 if (success) success = this->initializeTransports();
ansond 33:8302b02413bd 51 if (success) success = this->initializeLights();
ansond 0:ae2a45502448 52 if (success) this->logger()->turnLEDGreen();
ansond 7:f570eb3f38cd 53 if (!success) closedown(2);
ansond 0:ae2a45502448 54 }
ansond 0:ae2a45502448 55
ansond 0:ae2a45502448 56 // default destructor
ansond 0:ae2a45502448 57 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:ae2a45502448 58 bool success = true;
ansond 0:ae2a45502448 59 if (success) this->logger()->turnLEDYellow();
ansond 0:ae2a45502448 60 if (success) success = this->closeLights();
ansond 0:ae2a45502448 61 if (success) success = this->closeTransports();
ansond 0:ae2a45502448 62 if (success) success = this->closeEthernet();
ansond 0:ae2a45502448 63 if (success) this->logger()->turnLEDBlue();
ansond 15:e44d75d95b38 64 if (this->m_map != NULL) delete this->m_map;
ansond 0:ae2a45502448 65 }
ansond 24:7b3a5c927a53 66
ansond 15:e44d75d95b38 67 // get the IOC <--> MBED resource map
ansond 15:e44d75d95b38 68 MBEDToIOCResourceMap *MBEDEndpoint::getMap() { return this->m_map; }
ansond 15:e44d75d95b38 69
ansond 0:ae2a45502448 70 // initialize the Lights
ansond 0:ae2a45502448 71 bool MBEDEndpoint::initializeLights() {
ansond 0:ae2a45502448 72 this->logger()->log("Initializing Lights...");
ansond 0:ae2a45502448 73 for(int i=0;i<NUM_LIGHTS;++i) {
ansond 0:ae2a45502448 74 this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this);
ansond 0:ae2a45502448 75 this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 76 this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 77 }
ansond 0:ae2a45502448 78 return true;
ansond 0:ae2a45502448 79 }
ansond 0:ae2a45502448 80
ansond 0:ae2a45502448 81 // does the input name match any of our light resources?
ansond 0:ae2a45502448 82 int MBEDEndpoint::indexOfLight(char *name) {
ansond 0:ae2a45502448 83 bool found = false;
ansond 0:ae2a45502448 84 int index = -1;
ansond 0:ae2a45502448 85
ansond 0:ae2a45502448 86 for(int i=0;i<NUM_LIGHTS && !found;++i) {
ansond 0:ae2a45502448 87 if (strcmp(this->m_lights[i]->getName(),name) == 0) {
ansond 0:ae2a45502448 88 found = true;
ansond 0:ae2a45502448 89 index = i;
ansond 0:ae2a45502448 90 }
ansond 0:ae2a45502448 91 }
ansond 0:ae2a45502448 92
ansond 0:ae2a45502448 93 return index;
ansond 0:ae2a45502448 94 }
ansond 0:ae2a45502448 95
ansond 0:ae2a45502448 96 // get a specific resources
ansond 0:ae2a45502448 97 ResourceFactory *MBEDEndpoint::getResources(int index) {
ansond 0:ae2a45502448 98 if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources();
ansond 0:ae2a45502448 99 return NULL;
ansond 0:ae2a45502448 100 }
ansond 0:ae2a45502448 101
ansond 0:ae2a45502448 102 // initialize our ResourceFactory
ansond 39:bd5b2bcd2dcc 103 ResourceFactory *MBEDEndpoint::initResourceFactory() { return new EmulatedResourceFactory(this->logger(),(void *)this); }
ansond 2:90a84a216c58 104
ansond 32:a7b3c285446c 105 // Initialize the Endpoint Name - will be the first Light resource name (and there must be one...)
ansond 32:a7b3c285446c 106 void MBEDEndpoint::initEndpointName() {
ansond 32:a7b3c285446c 107 memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
ansond 32:a7b3c285446c 108 sprintf(this->m_endpoint_name,LIGHT_NAME,1);
ansond 32:a7b3c285446c 109 }
ansond 32:a7b3c285446c 110
ansond 32:a7b3c285446c 111 // get our endpoint name
ansond 32:a7b3c285446c 112 char *MBEDEndpoint::getEndpointName() { return this->m_endpoint_name; }
ansond 0:ae2a45502448 113
ansond 0:ae2a45502448 114 // initialize a specific transport
ansond 0:ae2a45502448 115 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:ae2a45502448 116 bool success = false;
ansond 0:ae2a45502448 117 if (this->m_transports[index] == NULL) {
ansond 0:ae2a45502448 118 this->logger()->log("Initializing %s Transport...", key);
ansond 0:ae2a45502448 119 this->m_transports[index] = transport;
ansond 0:ae2a45502448 120 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:ae2a45502448 121 }
ansond 0:ae2a45502448 122 else {
ansond 0:ae2a45502448 123 this->logger()->log("%s already connected (OK)...", key);
ansond 0:ae2a45502448 124 success = true;
ansond 0:ae2a45502448 125 }
ansond 0:ae2a45502448 126 return success;
ansond 0:ae2a45502448 127 }
ansond 0:ae2a45502448 128
ansond 0:ae2a45502448 129 // initialize our transports
ansond 0:ae2a45502448 130 bool MBEDEndpoint::initializeTransports() {
ansond 0:ae2a45502448 131 bool success = true;
ansond 0:ae2a45502448 132
ansond 0:ae2a45502448 133 if (success == true) {
ansond 0:ae2a45502448 134 // MQTT Initialization
ansond 15:e44d75d95b38 135 success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this,this->getMap()));
ansond 0:ae2a45502448 136 }
ansond 0:ae2a45502448 137
ansond 0:ae2a45502448 138 if (success == true) {
ansond 0:ae2a45502448 139 // HTTP Initialization
ansond 36:210f3956038c 140 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new IOCHTTPTransport(this->m_error_handler,this));
ansond 0:ae2a45502448 141 }
ansond 0:ae2a45502448 142 return success;
ansond 0:ae2a45502448 143 }
ansond 0:ae2a45502448 144
ansond 0:ae2a45502448 145 // initialize our Ethernet
ansond 0:ae2a45502448 146 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:ae2a45502448 147 bool success = false;
ansond 0:ae2a45502448 148 this->m_ethernet = ethernet;
ansond 0:ae2a45502448 149 if (this->m_ethernet != NULL) {
ansond 0:ae2a45502448 150 this->logger()->log("Initializing Ethernet...");
ansond 0:ae2a45502448 151
ansond 0:ae2a45502448 152 // connect up ethernet
ansond 0:ae2a45502448 153 this->m_ethernet->init();
ansond 0:ae2a45502448 154 this->m_ethernet->connect();
ansond 0:ae2a45502448 155
ansond 0:ae2a45502448 156 // display our IP address
ansond 0:ae2a45502448 157 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:ae2a45502448 158 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:ae2a45502448 159 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:ae2a45502448 160 success = true;
ansond 0:ae2a45502448 161 }
ansond 0:ae2a45502448 162 else {
ansond 0:ae2a45502448 163 this->logger()->log("Ethernet Not Connected...");
ansond 0:ae2a45502448 164 success = false;
ansond 0:ae2a45502448 165 }
ansond 0:ae2a45502448 166 }
ansond 0:ae2a45502448 167 else {
ansond 0:ae2a45502448 168 this->logger()->log("No Ethernet instance found");
ansond 0:ae2a45502448 169 success = false;
ansond 0:ae2a45502448 170 }
ansond 0:ae2a45502448 171 return success;
ansond 0:ae2a45502448 172 }
ansond 0:ae2a45502448 173
ansond 13:25448d92c205 174 // load up all endpoints into the IOC
ansond 13:25448d92c205 175 bool MBEDEndpoint::loadEndpoints() {
ansond 13:25448d92c205 176 bool success = true;
ansond 15:e44d75d95b38 177 this->logger()->log("Loading All Endpoints to IOC...");
ansond 13:25448d92c205 178 for(int i=0;i<NUM_LIGHTS && success;++i) success = this->loadEndpoint(this->m_lights[i]);
ansond 13:25448d92c205 179 return success;
ansond 13:25448d92c205 180 }
ansond 13:25448d92c205 181
ansond 0:ae2a45502448 182 // load up our endpoint to the IOC
ansond 15:e44d75d95b38 183 bool MBEDEndpoint::loadEndpoint(Light *light) {
ansond 11:59ee476fda24 184 bool success = false;
ansond 11:59ee476fda24 185 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 186 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 187
ansond 11:59ee476fda24 188 // initialize
ansond 17:84ab108ed670 189 memset(result,0,IOC_RESULT_LEN+1);
ansond 17:84ab108ed670 190 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 15:e44d75d95b38 191
ansond 15:e44d75d95b38 192 // DEBUG
ansond 48:d3663434128d 193 this->logger()->log("Building Payload for Light: %s...",light->getName());
ansond 12:952dce085876 194
ansond 11:59ee476fda24 195 // build the payload
ansond 14:0a6497a380a4 196 char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
ansond 19:956b694f6542 197
ansond 11:59ee476fda24 198 // issue the request
ansond 48:d3663434128d 199 if (data != NULL && strlen(data) > 0) {
ansond 48:d3663434128d 200 // DEBUG
ansond 48:d3663434128d 201 this->logger()->log("Sending Light: %s to the IOC...",light->getName());
ansond 48:d3663434128d 202
ansond 48:d3663434128d 203 // load
ansond 48:d3663434128d 204 success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 48:d3663434128d 205 }
ansond 11:59ee476fda24 206
ansond 15:e44d75d95b38 207 // DEBUG
ansond 23:793b2898522e 208 //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
ansond 15:e44d75d95b38 209
ansond 11:59ee476fda24 210 // update the IOC ID if found
ansond 24:7b3a5c927a53 211 if (success) this->saveIOCID(light,result);
ansond 11:59ee476fda24 212
ansond 15:e44d75d95b38 213 // DEBUG
ansond 24:7b3a5c927a53 214 if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),light->getIOCID());
ansond 19:956b694f6542 215
ansond 19:956b694f6542 216 // DEBUG
ansond 48:d3663434128d 217 if (!success) {
ansond 48:d3663434128d 218 if (light != NULL) this->logger()->log("Light: %s send FAILED",light->getName());
ansond 48:d3663434128d 219 else this->logger()->log("Light: send FAILED (NULL LIGHT)");
ansond 48:d3663434128d 220 }
ansond 15:e44d75d95b38 221
ansond 11:59ee476fda24 222 // return our status
ansond 11:59ee476fda24 223 return success;
ansond 11:59ee476fda24 224 }
ansond 0:ae2a45502448 225
ansond 13:25448d92c205 226 // update all endpoints to the IOC
ansond 13:25448d92c205 227 bool MBEDEndpoint::updateEndpoints() {
ansond 13:25448d92c205 228 bool success = true;
ansond 13:25448d92c205 229 for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i);
ansond 13:25448d92c205 230 return success;
ansond 13:25448d92c205 231 }
ansond 13:25448d92c205 232
ansond 13:25448d92c205 233 // update all endpoints to the IOC
ansond 13:25448d92c205 234 bool MBEDEndpoint::updateEndpoints(int index) {
ansond 13:25448d92c205 235 if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]);
ansond 13:25448d92c205 236 return false;
ansond 13:25448d92c205 237 }
ansond 13:25448d92c205 238
ansond 0:ae2a45502448 239 // update our endpoint with the IOC
ansond 13:25448d92c205 240 bool MBEDEndpoint::updateEndpoint(Light *light) {
ansond 11:59ee476fda24 241 bool success = false;
ansond 11:59ee476fda24 242 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 243 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 244
ansond 11:59ee476fda24 245 // initialize
ansond 17:84ab108ed670 246 memset(result,0,IOC_RESULT_LEN+1);
ansond 17:84ab108ed670 247 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 12:952dce085876 248
ansond 11:59ee476fda24 249 // build the payload
ansond 14:0a6497a380a4 250 char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
ansond 11:59ee476fda24 251
ansond 48:d3663434128d 252 // issue the request
ansond 48:d3663434128d 253 if (data != NULL && strlen(data) > 0) {
ansond 48:d3663434128d 254 // DEBUG
ansond 48:d3663434128d 255 this->logger()->log("Updating Light: %s at the IOC...",light->getName());
ansond 48:d3663434128d 256
ansond 48:d3663434128d 257 // update
ansond 48:d3663434128d 258 success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(light->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 48:d3663434128d 259 }
ansond 23:793b2898522e 260
ansond 23:793b2898522e 261 // DEBUG
ansond 23:793b2898522e 262 if (success) this->logger()->log("Update of Endpoint to IOC successful");
ansond 23:793b2898522e 263 else this->logger()->log("Update of Endpoint to IOC FAILED");
ansond 11:59ee476fda24 264
ansond 11:59ee476fda24 265 // return our status
ansond 11:59ee476fda24 266 return success;
ansond 11:59ee476fda24 267 }
ansond 11:59ee476fda24 268
ansond 11:59ee476fda24 269 // build out the Payload
ansond 13:25448d92c205 270 char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) {
ansond 48:d3663434128d 271 char tmp[TEMP_BUFFER_LEN+1];
ansond 48:d3663434128d 272
ansond 11:59ee476fda24 273 // construct the payload for Load/Updates
ansond 13:25448d92c205 274 ResourceFactory *factory = light->getResourceFactory();
ansond 48:d3663434128d 275
ansond 48:d3663434128d 276 // start the buffer
ansond 48:d3663434128d 277 strcat(data,"{");
ansond 13:25448d92c205 278
ansond 13:25448d92c205 279 // loop through the resources and build a JSON representation for the payload
ansond 13:25448d92c205 280 for(int i=0;i<factory->numResources();++i) {
ansond 13:25448d92c205 281 // get the ith resource
ansond 13:25448d92c205 282 Resource *resource = factory->getResource(i);
ansond 48:d3663434128d 283 if (resource != NULL) {
ansond 48:d3663434128d 284 // add to the JSON payload
ansond 48:d3663434128d 285 char *name = this->getMap()->endpointNameToIOCName(resource->getName());
ansond 48:d3663434128d 286 char *value = resource->getValue();
ansond 48:d3663434128d 287
ansond 48:d3663434128d 288 // Handle LOCATION a special way
ansond 48:d3663434128d 289 if (strcmp(name,"LOCATION") != 0) {
ansond 48:d3663434128d 290 // standard name,value for IOC
ansond 48:d3663434128d 291 sprintf(tmp, "\"%s\":\"%s\",",name,value);
ansond 48:d3663434128d 292 }
ansond 48:d3663434128d 293 else {
ansond 48:d3663434128d 294 // IOC expects "Point(X,Y)" for LOCATION
ansond 48:d3663434128d 295 sprintf(tmp, "\"%s\":\"Point(%s)\",",name,value);
ansond 48:d3663434128d 296 }
ansond 48:d3663434128d 297 strcat(data,tmp);
ansond 48:d3663434128d 298
ansond 48:d3663434128d 299 // Handle /dev/addldata
ansond 48:d3663434128d 300 char *dev_addldata = this->getMap()->endpointNameToIOCName("/dev/addldata");
ansond 48:d3663434128d 301 if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getIOCID() > 0) {
ansond 48:d3663434128d 302 char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getIOCID());
ansond 48:d3663434128d 303 sprintf(tmp,"\"%s\":\"id:%s\",",name,buf);
ansond 48:d3663434128d 304 strcat(data,tmp);
ansond 48:d3663434128d 305 }
ansond 24:7b3a5c927a53 306 }
ansond 13:25448d92c205 307 }
ansond 13:25448d92c205 308
ansond 17:84ab108ed670 309 // Special Case: STARTDATETIME
ansond 48:d3663434128d 310 strcat(data,"\"STARTDATETIME\":\"2014-02-27T22:03:06.973Z\",");
ansond 17:84ab108ed670 311
ansond 17:84ab108ed670 312 // Special Case: ENDDATETIME
ansond 48:d3663434128d 313 strcat(data,"\"ENDDATETIME\":\"2015-02-27T03:35:55.940Z\",");
ansond 48:d3663434128d 314
ansond 21:d1ce325d1d32 315 // Special Case: NAME
ansond 48:d3663434128d 316 sprintf(tmp,"\"NAME\":\"%s\",",light->getName());
ansond 48:d3663434128d 317 strcat(data,tmp);
ansond 48:d3663434128d 318
ansond 21:d1ce325d1d32 319 // Special Case: TIMEZONEOFFSET
ansond 48:d3663434128d 320 strcat(data,"\"TIMEZONEOFFSET\":\"+2:00\"");
ansond 24:7b3a5c927a53 321
ansond 21:d1ce325d1d32 322 // close
ansond 48:d3663434128d 323 strcat(data,"}");
ansond 48:d3663434128d 324
ansond 13:25448d92c205 325 // DEBUG
ansond 23:793b2898522e 326 //this->logger()->log("Loading Payload: %s",data);
ansond 11:59ee476fda24 327
ansond 11:59ee476fda24 328 // return the payload
ansond 12:952dce085876 329 return data;
ansond 11:59ee476fda24 330 }
ansond 11:59ee476fda24 331
ansond 11:59ee476fda24 332 // save the IOC ID
ansond 24:7b3a5c927a53 333 void MBEDEndpoint::saveIOCID(Light *light,char *json) {
ansond 48:d3663434128d 334 if (json != NULL) {
ansond 48:d3663434128d 335 //this->logger()->log("RESULT: %s",json);
ansond 48:d3663434128d 336
ansond 22:f1002e5993c5 337 // look for "id":
ansond 48:d3663434128d 338 char *check = "\"id\":";
ansond 48:d3663434128d 339 char *pos1 = strstr(json,check);
ansond 48:d3663434128d 340 if (pos1 != NULL) {
ansond 48:d3663434128d 341 char *pos2 = strstr(pos1,",");
ansond 48:d3663434128d 342 if (pos1 != NULL && pos2 != NULL && pos2 > pos1) {
ansond 48:d3663434128d 343 pos1 += strlen(check);
ansond 48:d3663434128d 344 int length = pos2 - pos1;
ansond 48:d3663434128d 345 char str_ioc_id[IOC_IOC_ID_LEN+1];
ansond 48:d3663434128d 346 memset(str_ioc_id,0,IOC_IOC_ID_LEN+1);
ansond 48:d3663434128d 347 strncpy(str_ioc_id,pos1,length);
ansond 48:d3663434128d 348
ansond 48:d3663434128d 349 // DEBUG
ansond 48:d3663434128d 350 //this->logger()->log("IOC ID found: %s",str_ioc_id);
ansond 48:d3663434128d 351
ansond 48:d3663434128d 352 // parse into int
ansond 48:d3663434128d 353 int ioc_id = 0;
ansond 48:d3663434128d 354 sscanf(str_ioc_id,"%d",&ioc_id);
ansond 48:d3663434128d 355
ansond 48:d3663434128d 356 // save the IOC ID
ansond 48:d3663434128d 357 if (ioc_id > 0) light->setIOCID(ioc_id);
ansond 48:d3663434128d 358 }
ansond 48:d3663434128d 359 else {
ansond 48:d3663434128d 360 // cannot find the ID tag in the result JSON
ansond 48:d3663434128d 361 this->logger()->log("Cannot find the IOC ID in the JSON result");
ansond 48:d3663434128d 362 this->logger()->log("JSON: %s",json);
ansond 48:d3663434128d 363 }
ansond 23:793b2898522e 364 }
ansond 23:793b2898522e 365 else {
ansond 23:793b2898522e 366 // cannot find the ID tag in the result JSON
ansond 23:793b2898522e 367 this->logger()->log("Cannot find the IOC ID in the JSON result");
ansond 23:793b2898522e 368 this->logger()->log("JSON: %s",json);
ansond 48:d3663434128d 369 }
ansond 22:f1002e5993c5 370 }
ansond 22:f1002e5993c5 371 }
ansond 22:f1002e5993c5 372
ansond 0:ae2a45502448 373 // close down the Lights
ansond 0:ae2a45502448 374 bool MBEDEndpoint::closeLights() {
ansond 0:ae2a45502448 375 bool success = true;
ansond 0:ae2a45502448 376 this->logger()->log("Closing down Lights...");
ansond 15:e44d75d95b38 377 for(int i=0;i<NUM_LIGHTS;++i)
ansond 15:e44d75d95b38 378 if (this->m_lights[i] != NULL) delete this->m_lights[i];
ansond 0:ae2a45502448 379 return success;
ansond 0:ae2a45502448 380 }
ansond 0:ae2a45502448 381
ansond 0:ae2a45502448 382 // close a given transport
ansond 0:ae2a45502448 383 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:ae2a45502448 384 this->logger()->log("Closing down %s Transport...", key);
ansond 0:ae2a45502448 385 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:ae2a45502448 386 return true;
ansond 0:ae2a45502448 387 }
ansond 0:ae2a45502448 388
ansond 0:ae2a45502448 389 // close down our transports
ansond 0:ae2a45502448 390 bool MBEDEndpoint::closeTransports() {
ansond 0:ae2a45502448 391 bool success = true;
ansond 0:ae2a45502448 392
ansond 0:ae2a45502448 393 if (success) {
ansond 0:ae2a45502448 394 // close MQTT
ansond 0:ae2a45502448 395 success = this->closeTransport(MQTT_TRANSPORT,"MQTT");
ansond 0:ae2a45502448 396 }
ansond 0:ae2a45502448 397
ansond 0:ae2a45502448 398 if (success) {
ansond 0:ae2a45502448 399 // close HTTP
ansond 0:ae2a45502448 400 success = this->closeTransport(HTTP_TRANSPORT,"HTTP");
ansond 0:ae2a45502448 401 }
ansond 0:ae2a45502448 402
ansond 0:ae2a45502448 403 return success;
ansond 0:ae2a45502448 404 }
ansond 0:ae2a45502448 405
ansond 0:ae2a45502448 406 // close down our Ethernet
ansond 0:ae2a45502448 407 bool MBEDEndpoint::closeEthernet() {
ansond 0:ae2a45502448 408 this->logger()->log("Closing down Ethernet...");
ansond 0:ae2a45502448 409 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:ae2a45502448 410 return true;
ansond 0:ae2a45502448 411 }
ansond 0:ae2a45502448 412
ansond 13:25448d92c205 413 // min function
ansond 13:25448d92c205 414 int MBEDEndpoint::min(int value1,int value2) {
ansond 13:25448d92c205 415 if (value1 < value2) return value1;
ansond 13:25448d92c205 416 return value2;
ansond 13:25448d92c205 417 }
ansond 13:25448d92c205 418
ansond 0:ae2a45502448 419 // get our error handler
ansond 0:ae2a45502448 420 ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }
ansond 0:ae2a45502448 421
ansond 0:ae2a45502448 422 // main running loop
ansond 0:ae2a45502448 423 void MBEDEndpoint::run() {
ansond 0:ae2a45502448 424 this->logger()->log("Endpoint Main Loop");
ansond 0:ae2a45502448 425 while(true) {
ansond 0:ae2a45502448 426 // sleep a bit
ansond 0:ae2a45502448 427 //this->logger()->log("Sleeping for a bit...");
ansond 0:ae2a45502448 428 wait_ms(MAIN_LOOP_SLEEP);
ansond 0:ae2a45502448 429
ansond 0:ae2a45502448 430 // check for events
ansond 0:ae2a45502448 431 //this->logger()->log("Processing Events...");
ansond 0:ae2a45502448 432 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess();
ansond 0:ae2a45502448 433
ansond 0:ae2a45502448 434 // check for exit
ansond 0:ae2a45502448 435 //this->logger()->log("Checking for exit...");
ansond 0:ae2a45502448 436 this->logger()->checkForExit();
ansond 0:ae2a45502448 437 }
ansond 0:ae2a45502448 438 }
ansond 0:ae2a45502448 439