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