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:
Fri Feb 28 18:08:11 2014 +0000
Revision:
30:5c670ec5d203
Parent:
25:8e960728d50a
Child:
32:a7b3c285446c
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 22:f1002e5993c5 31
ansond 7:f570eb3f38cd 32 // shutdown endpoint reference
ansond 7:f570eb3f38cd 33 extern void closedown(int code);
ansond 0:ae2a45502448 34
ansond 0:ae2a45502448 35 // default constructor
ansond 0:ae2a45502448 36 MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) {
ansond 0:ae2a45502448 37 bool success = true;
ansond 0:ae2a45502448 38 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
ansond 0:ae2a45502448 39 this->m_error_handler = error_handler;
ansond 0:ae2a45502448 40 if (success) this->logger()->turnLEDBlue();
ansond 30:5c670ec5d203 41 if (success)this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
ansond 0:ae2a45502448 42 if (success) success = this->initializeEthernet(ethernet);
ansond 0:ae2a45502448 43 if (success) this->logger()->turnLEDYellow();
ansond 30:5c670ec5d203 44 if (success)this->m_map = new MBEDToIOCResourceMap(error_handler);
ansond 30:5c670ec5d203 45 if (success) success = this->initializeTransports();
ansond 8:45f9a920e82c 46 if (success) success = this->initializeLights();
ansond 0:ae2a45502448 47 if (success) this->logger()->turnLEDGreen();
ansond 7:f570eb3f38cd 48 if (!success) closedown(2);
ansond 0:ae2a45502448 49 }
ansond 0:ae2a45502448 50
ansond 0:ae2a45502448 51 // default destructor
ansond 0:ae2a45502448 52 MBEDEndpoint::~MBEDEndpoint() {
ansond 0:ae2a45502448 53 bool success = true;
ansond 0:ae2a45502448 54 if (success) this->logger()->turnLEDYellow();
ansond 0:ae2a45502448 55 if (success) success = this->closeLights();
ansond 0:ae2a45502448 56 if (success) success = this->closeTransports();
ansond 0:ae2a45502448 57 if (success) success = this->closeEthernet();
ansond 0:ae2a45502448 58 if (success) this->logger()->turnLEDBlue();
ansond 15:e44d75d95b38 59 if (this->m_map != NULL) delete this->m_map;
ansond 0:ae2a45502448 60 }
ansond 24:7b3a5c927a53 61
ansond 15:e44d75d95b38 62 // get the IOC <--> MBED resource map
ansond 15:e44d75d95b38 63 MBEDToIOCResourceMap *MBEDEndpoint::getMap() { return this->m_map; }
ansond 15:e44d75d95b38 64
ansond 0:ae2a45502448 65 // initialize the Lights
ansond 0:ae2a45502448 66 bool MBEDEndpoint::initializeLights() {
ansond 0:ae2a45502448 67 this->logger()->log("Initializing Lights...");
ansond 0:ae2a45502448 68 for(int i=0;i<NUM_LIGHTS;++i) {
ansond 0:ae2a45502448 69 this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this);
ansond 0:ae2a45502448 70 this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 71 this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i]));
ansond 0:ae2a45502448 72 }
ansond 0:ae2a45502448 73 return true;
ansond 0:ae2a45502448 74 }
ansond 0:ae2a45502448 75
ansond 0:ae2a45502448 76 // does the input name match any of our light resources?
ansond 0:ae2a45502448 77 int MBEDEndpoint::indexOfLight(char *name) {
ansond 0:ae2a45502448 78 bool found = false;
ansond 0:ae2a45502448 79 int index = -1;
ansond 0:ae2a45502448 80
ansond 0:ae2a45502448 81 for(int i=0;i<NUM_LIGHTS && !found;++i) {
ansond 0:ae2a45502448 82 if (strcmp(this->m_lights[i]->getName(),name) == 0) {
ansond 0:ae2a45502448 83 found = true;
ansond 0:ae2a45502448 84 index = i;
ansond 0:ae2a45502448 85 }
ansond 0:ae2a45502448 86 }
ansond 0:ae2a45502448 87
ansond 0:ae2a45502448 88 return index;
ansond 0:ae2a45502448 89 }
ansond 0:ae2a45502448 90
ansond 0:ae2a45502448 91 // get a specific resources
ansond 0:ae2a45502448 92 ResourceFactory *MBEDEndpoint::getResources(int index) {
ansond 0:ae2a45502448 93 if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources();
ansond 0:ae2a45502448 94 return NULL;
ansond 0:ae2a45502448 95 }
ansond 0:ae2a45502448 96
ansond 0:ae2a45502448 97 // initialize our ResourceFactory
ansond 2:90a84a216c58 98 ResourceFactory *MBEDEndpoint::initResourceFactory() { return new EmulatedResourceFactory(this->logger()); }
ansond 2:90a84a216c58 99
ansond 2:90a84a216c58 100 // get our endpoint name (first light name)
ansond 6:34c07e145caa 101 char *MBEDEndpoint::getEndpointName() { return this->m_lights[0]->getName(); }
ansond 0:ae2a45502448 102
ansond 0:ae2a45502448 103 // initialize a specific transport
ansond 0:ae2a45502448 104 bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
ansond 0:ae2a45502448 105 bool success = false;
ansond 0:ae2a45502448 106 if (this->m_transports[index] == NULL) {
ansond 0:ae2a45502448 107 this->logger()->log("Initializing %s Transport...", key);
ansond 0:ae2a45502448 108 this->m_transports[index] = transport;
ansond 0:ae2a45502448 109 if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
ansond 0:ae2a45502448 110 }
ansond 0:ae2a45502448 111 else {
ansond 0:ae2a45502448 112 this->logger()->log("%s already connected (OK)...", key);
ansond 0:ae2a45502448 113 success = true;
ansond 0:ae2a45502448 114 }
ansond 0:ae2a45502448 115 return success;
ansond 0:ae2a45502448 116 }
ansond 0:ae2a45502448 117
ansond 0:ae2a45502448 118 // initialize our transports
ansond 0:ae2a45502448 119 bool MBEDEndpoint::initializeTransports() {
ansond 0:ae2a45502448 120 bool success = true;
ansond 0:ae2a45502448 121
ansond 0:ae2a45502448 122 if (success == true) {
ansond 0:ae2a45502448 123 // MQTT Initialization
ansond 15:e44d75d95b38 124 success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this,this->getMap()));
ansond 0:ae2a45502448 125 }
ansond 0:ae2a45502448 126
ansond 0:ae2a45502448 127 if (success == true) {
ansond 0:ae2a45502448 128 // HTTP Initialization
ansond 7:f570eb3f38cd 129 success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler,this));
ansond 0:ae2a45502448 130 }
ansond 0:ae2a45502448 131 return success;
ansond 0:ae2a45502448 132 }
ansond 0:ae2a45502448 133
ansond 0:ae2a45502448 134 // initialize our Ethernet
ansond 0:ae2a45502448 135 bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
ansond 0:ae2a45502448 136 bool success = false;
ansond 0:ae2a45502448 137 this->m_ethernet = ethernet;
ansond 0:ae2a45502448 138 if (this->m_ethernet != NULL) {
ansond 0:ae2a45502448 139 this->logger()->log("Initializing Ethernet...");
ansond 0:ae2a45502448 140
ansond 0:ae2a45502448 141 // connect up ethernet
ansond 0:ae2a45502448 142 this->m_ethernet->init();
ansond 0:ae2a45502448 143 this->m_ethernet->connect();
ansond 0:ae2a45502448 144
ansond 0:ae2a45502448 145 // display our IP address
ansond 0:ae2a45502448 146 char *ipaddr = this->m_ethernet->getIPAddress();
ansond 0:ae2a45502448 147 if (ipaddr != NULL && strlen(ipaddr) > 0) {
ansond 0:ae2a45502448 148 this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
ansond 0:ae2a45502448 149 success = true;
ansond 0:ae2a45502448 150 }
ansond 0:ae2a45502448 151 else {
ansond 0:ae2a45502448 152 this->logger()->log("Ethernet Not Connected...");
ansond 0:ae2a45502448 153 success = false;
ansond 0:ae2a45502448 154 }
ansond 0:ae2a45502448 155 }
ansond 0:ae2a45502448 156 else {
ansond 0:ae2a45502448 157 this->logger()->log("No Ethernet instance found");
ansond 0:ae2a45502448 158 success = false;
ansond 0:ae2a45502448 159 }
ansond 0:ae2a45502448 160 return success;
ansond 0:ae2a45502448 161 }
ansond 0:ae2a45502448 162
ansond 13:25448d92c205 163 // load up all endpoints into the IOC
ansond 13:25448d92c205 164 bool MBEDEndpoint::loadEndpoints() {
ansond 13:25448d92c205 165 bool success = true;
ansond 15:e44d75d95b38 166 this->logger()->log("Loading All Endpoints to IOC...");
ansond 13:25448d92c205 167 for(int i=0;i<NUM_LIGHTS && success;++i) success = this->loadEndpoint(this->m_lights[i]);
ansond 13:25448d92c205 168 return success;
ansond 13:25448d92c205 169 }
ansond 13:25448d92c205 170
ansond 0:ae2a45502448 171 // load up our endpoint to the IOC
ansond 15:e44d75d95b38 172 bool MBEDEndpoint::loadEndpoint(Light *light) {
ansond 11:59ee476fda24 173 bool success = false;
ansond 11:59ee476fda24 174 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 175 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 176
ansond 11:59ee476fda24 177 // initialize
ansond 17:84ab108ed670 178 memset(result,0,IOC_RESULT_LEN+1);
ansond 17:84ab108ed670 179 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 15:e44d75d95b38 180
ansond 15:e44d75d95b38 181 // DEBUG
ansond 23:793b2898522e 182 //this->logger()->log("Loading Light: %s...",light->getName());
ansond 12:952dce085876 183
ansond 11:59ee476fda24 184 // build the payload
ansond 14:0a6497a380a4 185 char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
ansond 11:59ee476fda24 186
ansond 15:e44d75d95b38 187 // DEBUG
ansond 15:e44d75d95b38 188 this->logger()->log("Sending Light: %s to the IOC...",light->getName());
ansond 19:956b694f6542 189
ansond 11:59ee476fda24 190 // issue the request
ansond 12:952dce085876 191 success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 11:59ee476fda24 192
ansond 15:e44d75d95b38 193 // DEBUG
ansond 23:793b2898522e 194 //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
ansond 15:e44d75d95b38 195
ansond 11:59ee476fda24 196 // update the IOC ID if found
ansond 24:7b3a5c927a53 197 if (success) this->saveIOCID(light,result);
ansond 11:59ee476fda24 198
ansond 15:e44d75d95b38 199 // DEBUG
ansond 24:7b3a5c927a53 200 if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),light->getIOCID());
ansond 24:7b3a5c927a53 201 else this->logger()->log("Light: %s send FAILED",light->getName());
ansond 19:956b694f6542 202
ansond 19:956b694f6542 203 // DEBUG
ansond 19:956b694f6542 204 if (!success) this->logger()->log("Loading Light: %s FAILED",light->getName());
ansond 15:e44d75d95b38 205
ansond 11:59ee476fda24 206 // return our status
ansond 11:59ee476fda24 207 return success;
ansond 11:59ee476fda24 208 }
ansond 0:ae2a45502448 209
ansond 13:25448d92c205 210 // update all endpoints to the IOC
ansond 13:25448d92c205 211 bool MBEDEndpoint::updateEndpoints() {
ansond 13:25448d92c205 212 bool success = true;
ansond 13:25448d92c205 213 for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i);
ansond 13:25448d92c205 214 return success;
ansond 13:25448d92c205 215 }
ansond 13:25448d92c205 216
ansond 13:25448d92c205 217 // update all endpoints to the IOC
ansond 13:25448d92c205 218 bool MBEDEndpoint::updateEndpoints(int index) {
ansond 13:25448d92c205 219 if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]);
ansond 13:25448d92c205 220 return false;
ansond 13:25448d92c205 221 }
ansond 13:25448d92c205 222
ansond 0:ae2a45502448 223 // update our endpoint with the IOC
ansond 13:25448d92c205 224 bool MBEDEndpoint::updateEndpoint(Light *light) {
ansond 11:59ee476fda24 225 bool success = false;
ansond 11:59ee476fda24 226 char result[IOC_RESULT_LEN+1];
ansond 11:59ee476fda24 227 char payload[IOC_PAYLOAD_LEN+1];
ansond 11:59ee476fda24 228
ansond 11:59ee476fda24 229 // initialize
ansond 17:84ab108ed670 230 memset(result,0,IOC_RESULT_LEN+1);
ansond 17:84ab108ed670 231 memset(payload,0,IOC_PAYLOAD_LEN+1);
ansond 12:952dce085876 232
ansond 11:59ee476fda24 233 // build the payload
ansond 14:0a6497a380a4 234 char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
ansond 11:59ee476fda24 235
ansond 11:59ee476fda24 236 // issue the request
ansond 24:7b3a5c927a53 237 success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(light->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
ansond 23:793b2898522e 238
ansond 23:793b2898522e 239 // DEBUG
ansond 23:793b2898522e 240 if (success) this->logger()->log("Update of Endpoint to IOC successful");
ansond 23:793b2898522e 241 else this->logger()->log("Update of Endpoint to IOC FAILED");
ansond 11:59ee476fda24 242
ansond 11:59ee476fda24 243 // return our status
ansond 11:59ee476fda24 244 return success;
ansond 11:59ee476fda24 245 }
ansond 11:59ee476fda24 246
ansond 11:59ee476fda24 247 // build out the Payload
ansond 13:25448d92c205 248 char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) {
ansond 11:59ee476fda24 249 // construct the payload for Load/Updates
ansond 13:25448d92c205 250 ResourceFactory *factory = light->getResourceFactory();
ansond 21:d1ce325d1d32 251
ansond 19:956b694f6542 252 // accumulator string
ansond 19:956b694f6542 253 std::string str_json("{");
ansond 13:25448d92c205 254
ansond 13:25448d92c205 255 // loop through the resources and build a JSON representation for the payload
ansond 13:25448d92c205 256 for(int i=0;i<factory->numResources();++i) {
ansond 13:25448d92c205 257 // get the ith resource
ansond 13:25448d92c205 258 Resource *resource = factory->getResource(i);
ansond 13:25448d92c205 259
ansond 13:25448d92c205 260 // add to the JSON payload
ansond 17:84ab108ed670 261 char *name = this->getMap()->endpointNameToIOCName(resource->getName());
ansond 18:214698dbcdfa 262 char *value = resource->getValue();
ansond 17:84ab108ed670 263
ansond 21:d1ce325d1d32 264 // Handle LOCATION a special way
ansond 19:956b694f6542 265 if (strcmp(name,"LOCATION") != 0) {
ansond 21:d1ce325d1d32 266 str_json += "\"" + std::string(name) + "\":\"" + std::string(value) + "\",";
ansond 21:d1ce325d1d32 267 }
ansond 21:d1ce325d1d32 268 else {
ansond 22:f1002e5993c5 269 // IOC expects "Point(X,Y)" for LOCATION, but in reverse from MBED
ansond 23:793b2898522e 270 str_json += "\"" + std::string(name) + "\":\"Point(" + std::string(value) + ")\",";
ansond 21:d1ce325d1d32 271 }
ansond 24:7b3a5c927a53 272
ansond 24:7b3a5c927a53 273 // Handle /dev/addldata
ansond 25:8e960728d50a 274 char *dev_addldata = this->getMap()->endpointNameToIOCName("/dev/addldata");
ansond 25:8e960728d50a 275 if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getIOCID() > 0) {
ansond 25:8e960728d50a 276 char buf[10]; memset(buf,0,10); sprintf(buf,"%d",light->getIOCID());
ansond 25:8e960728d50a 277 str_json += "\"" + std::string(name) + "\":\"id:" + std::string(buf) + "\",";
ansond 24:7b3a5c927a53 278 }
ansond 13:25448d92c205 279 }
ansond 13:25448d92c205 280
ansond 17:84ab108ed670 281 // Special Case: STARTDATETIME
ansond 21:d1ce325d1d32 282 str_json += "\"STARTDATETIME\":\"2014-02-27T22:03:06.973Z\",";
ansond 17:84ab108ed670 283
ansond 17:84ab108ed670 284 // Special Case: ENDDATETIME
ansond 21:d1ce325d1d32 285 str_json += "\"ENDDATETIME\":\"2015-02-27T03:35:55.940Z\",";
ansond 21:d1ce325d1d32 286
ansond 21:d1ce325d1d32 287 // Special Case: NAME
ansond 21:d1ce325d1d32 288 str_json += "\"NAME\":\"" + std::string(light->getName()) + "\",";
ansond 19:956b694f6542 289
ansond 21:d1ce325d1d32 290 // Special Case: TIMEZONEOFFSET
ansond 21:d1ce325d1d32 291 str_json += "\"TIMEZONEOFFSET\":\"+2:00\"";
ansond 24:7b3a5c927a53 292
ansond 21:d1ce325d1d32 293 // close
ansond 21:d1ce325d1d32 294 str_json += "}";
ansond 21:d1ce325d1d32 295
ansond 13:25448d92c205 296 // copy over to the buffer
ansond 18:214698dbcdfa 297 strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length));
ansond 15:e44d75d95b38 298
ansond 13:25448d92c205 299 // DEBUG
ansond 23:793b2898522e 300 //this->logger()->log("Loading Payload: %s",data);
ansond 11:59ee476fda24 301
ansond 11:59ee476fda24 302 // return the payload
ansond 12:952dce085876 303 return data;
ansond 11:59ee476fda24 304 }
ansond 11:59ee476fda24 305
ansond 11:59ee476fda24 306 // save the IOC ID
ansond 24:7b3a5c927a53 307 void MBEDEndpoint::saveIOCID(Light *light,char *json) {
ansond 22:f1002e5993c5 308 if (json != NULL) {
ansond 22:f1002e5993c5 309 std::string str_json(json);
ansond 22:f1002e5993c5 310
ansond 22:f1002e5993c5 311 // look for "id":
ansond 23:793b2898522e 312 std::string check("\"id\":");
ansond 23:793b2898522e 313 int pos1 = str_json.find(check);
ansond 23:793b2898522e 314 int pos2 = str_json.find(",",pos1);
ansond 23:793b2898522e 315 if (pos1 >= 0 && pos2 >= 0 && pos2 > pos1) {
ansond 23:793b2898522e 316 pos1 += check.length();
ansond 23:793b2898522e 317 int length = pos2 - pos1;
ansond 23:793b2898522e 318 std::string str_ioc_id = str_json.substr(pos1,length);
ansond 23:793b2898522e 319
ansond 23:793b2898522e 320 // DEBUG
ansond 23:793b2898522e 321 //this->logger()->log("IOC ID found: %s",str_ioc_id.c_str());
ansond 23:793b2898522e 322
ansond 23:793b2898522e 323 // parse into int
ansond 23:793b2898522e 324 int ioc_id = 0;
ansond 23:793b2898522e 325 sscanf(str_ioc_id.c_str(),"%d",&ioc_id);
ansond 23:793b2898522e 326
ansond 23:793b2898522e 327 // save the IOC ID
ansond 24:7b3a5c927a53 328 if (ioc_id > 0) light->setIOCID(ioc_id);
ansond 23:793b2898522e 329 }
ansond 23:793b2898522e 330 else {
ansond 23:793b2898522e 331 // cannot find the ID tag in the result JSON
ansond 23:793b2898522e 332 this->logger()->log("Cannot find the IOC ID in the JSON result");
ansond 23:793b2898522e 333 this->logger()->log("JSON: %s",json);
ansond 23:793b2898522e 334 }
ansond 22:f1002e5993c5 335 }
ansond 22:f1002e5993c5 336 }
ansond 22:f1002e5993c5 337
ansond 0:ae2a45502448 338 // close down the Lights
ansond 0:ae2a45502448 339 bool MBEDEndpoint::closeLights() {
ansond 0:ae2a45502448 340 bool success = true;
ansond 0:ae2a45502448 341 this->logger()->log("Closing down Lights...");
ansond 15:e44d75d95b38 342 for(int i=0;i<NUM_LIGHTS;++i)
ansond 15:e44d75d95b38 343 if (this->m_lights[i] != NULL) delete this->m_lights[i];
ansond 0:ae2a45502448 344 return success;
ansond 0:ae2a45502448 345 }
ansond 0:ae2a45502448 346
ansond 0:ae2a45502448 347 // close a given transport
ansond 0:ae2a45502448 348 bool MBEDEndpoint::closeTransport(int index,char *key) {
ansond 0:ae2a45502448 349 this->logger()->log("Closing down %s Transport...", key);
ansond 0:ae2a45502448 350 if (this->m_transports[index] != NULL) delete this->m_transports[index];
ansond 0:ae2a45502448 351 return true;
ansond 0:ae2a45502448 352 }
ansond 0:ae2a45502448 353
ansond 0:ae2a45502448 354 // close down our transports
ansond 0:ae2a45502448 355 bool MBEDEndpoint::closeTransports() {
ansond 0:ae2a45502448 356 bool success = true;
ansond 0:ae2a45502448 357
ansond 0:ae2a45502448 358 if (success) {
ansond 0:ae2a45502448 359 // close MQTT
ansond 0:ae2a45502448 360 success = this->closeTransport(MQTT_TRANSPORT,"MQTT");
ansond 0:ae2a45502448 361 }
ansond 0:ae2a45502448 362
ansond 0:ae2a45502448 363 if (success) {
ansond 0:ae2a45502448 364 // close HTTP
ansond 0:ae2a45502448 365 success = this->closeTransport(HTTP_TRANSPORT,"HTTP");
ansond 0:ae2a45502448 366 }
ansond 0:ae2a45502448 367
ansond 0:ae2a45502448 368 return success;
ansond 0:ae2a45502448 369 }
ansond 0:ae2a45502448 370
ansond 0:ae2a45502448 371 // close down our Ethernet
ansond 0:ae2a45502448 372 bool MBEDEndpoint::closeEthernet() {
ansond 0:ae2a45502448 373 this->logger()->log("Closing down Ethernet...");
ansond 0:ae2a45502448 374 if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
ansond 0:ae2a45502448 375 return true;
ansond 0:ae2a45502448 376 }
ansond 0:ae2a45502448 377
ansond 13:25448d92c205 378 // min function
ansond 13:25448d92c205 379 int MBEDEndpoint::min(int value1,int value2) {
ansond 13:25448d92c205 380 if (value1 < value2) return value1;
ansond 13:25448d92c205 381 return value2;
ansond 13:25448d92c205 382 }
ansond 13:25448d92c205 383
ansond 0:ae2a45502448 384 // get our error handler
ansond 0:ae2a45502448 385 ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }
ansond 0:ae2a45502448 386
ansond 0:ae2a45502448 387 // main running loop
ansond 0:ae2a45502448 388 void MBEDEndpoint::run() {
ansond 0:ae2a45502448 389 this->logger()->log("Endpoint Main Loop");
ansond 0:ae2a45502448 390 while(true) {
ansond 0:ae2a45502448 391 // sleep a bit
ansond 0:ae2a45502448 392 //this->logger()->log("Sleeping for a bit...");
ansond 0:ae2a45502448 393 wait_ms(MAIN_LOOP_SLEEP);
ansond 0:ae2a45502448 394
ansond 0:ae2a45502448 395 // check for events
ansond 0:ae2a45502448 396 //this->logger()->log("Processing Events...");
ansond 0:ae2a45502448 397 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess();
ansond 0:ae2a45502448 398
ansond 0:ae2a45502448 399 // check for exit
ansond 0:ae2a45502448 400 //this->logger()->log("Checking for exit...");
ansond 0:ae2a45502448 401 this->logger()->checkForExit();
ansond 0:ae2a45502448 402 }
ansond 0:ae2a45502448 403 }
ansond 0:ae2a45502448 404