Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832_lcd EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed
MBEDEndpoint.cpp@17:84ab108ed670, 2014-02-27 (annotated)
- Committer:
- ansond
- Date:
- Thu Feb 27 06:28:42 2014 +0000
- Revision:
- 17:84ab108ed670
- Parent:
- 15:e44d75d95b38
- Child:
- 18:214698dbcdfa
updates
Who changed what in which revision?
| User | Revision | Line number | New 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 | 7:f570eb3f38cd | 31 | |
| ansond | 13:25448d92c205 | 32 | // JSON serialization support |
| ansond | 13:25448d92c205 | 33 | #include "MbedJSONValue.h" |
| ansond | 13:25448d92c205 | 34 | |
| ansond | 7:f570eb3f38cd | 35 | // shutdown endpoint reference |
| ansond | 7:f570eb3f38cd | 36 | extern void closedown(int code); |
| ansond | 0:ae2a45502448 | 37 | |
| ansond | 0:ae2a45502448 | 38 | // default constructor |
| ansond | 0:ae2a45502448 | 39 | MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) { |
| ansond | 10:748fc7052e61 | 40 | this->m_ioc_id = -1; |
| 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 | 15:e44d75d95b38 | 44 | this->m_map = new MBEDToIOCResourceMap(error_handler); |
| ansond | 0:ae2a45502448 | 45 | this->logger()->log(ENDPOINT_VERSION_ANNOUNCE); |
| ansond | 0:ae2a45502448 | 46 | if (success) this->logger()->turnLEDBlue(); |
| ansond | 0:ae2a45502448 | 47 | if (success) success = this->initializeEthernet(ethernet); |
| ansond | 0:ae2a45502448 | 48 | if (success) this->logger()->turnLEDYellow(); |
| ansond | 8:45f9a920e82c | 49 | if (success) success = this->initializeLights(); |
| ansond | 0:ae2a45502448 | 50 | if (success) success = this->initializeTransports(); |
| ansond | 0:ae2a45502448 | 51 | if (success) this->logger()->turnLEDGreen(); |
| ansond | 7:f570eb3f38cd | 52 | if (!success) closedown(2); |
| ansond | 0:ae2a45502448 | 53 | } |
| ansond | 0:ae2a45502448 | 54 | |
| ansond | 0:ae2a45502448 | 55 | // default destructor |
| ansond | 0:ae2a45502448 | 56 | MBEDEndpoint::~MBEDEndpoint() { |
| ansond | 0:ae2a45502448 | 57 | bool success = true; |
| ansond | 0:ae2a45502448 | 58 | if (success) this->logger()->turnLEDYellow(); |
| ansond | 0:ae2a45502448 | 59 | if (success) success = this->closeLights(); |
| ansond | 0:ae2a45502448 | 60 | if (success) success = this->closeTransports(); |
| ansond | 0:ae2a45502448 | 61 | if (success) success = this->closeEthernet(); |
| ansond | 0:ae2a45502448 | 62 | if (success) this->logger()->turnLEDBlue(); |
| ansond | 15:e44d75d95b38 | 63 | if (this->m_map != NULL) delete this->m_map; |
| ansond | 0:ae2a45502448 | 64 | } |
| ansond | 0:ae2a45502448 | 65 | |
| ansond | 10:748fc7052e61 | 66 | // set the IOC ID |
| ansond | 10:748fc7052e61 | 67 | void MBEDEndpoint::setIOCID(int ioc_id) { this->m_ioc_id = ioc_id; } |
| ansond | 10:748fc7052e61 | 68 | |
| ansond | 10:748fc7052e61 | 69 | // get the IOC ID |
| ansond | 10:748fc7052e61 | 70 | int MBEDEndpoint::getIOCID() { return this->m_ioc_id; } |
| ansond | 10:748fc7052e61 | 71 | |
| ansond | 15:e44d75d95b38 | 72 | // get the IOC <--> MBED resource map |
| ansond | 15:e44d75d95b38 | 73 | MBEDToIOCResourceMap *MBEDEndpoint::getMap() { return this->m_map; } |
| ansond | 15:e44d75d95b38 | 74 | |
| ansond | 0:ae2a45502448 | 75 | // initialize the Lights |
| ansond | 0:ae2a45502448 | 76 | bool MBEDEndpoint::initializeLights() { |
| ansond | 0:ae2a45502448 | 77 | this->logger()->log("Initializing Lights..."); |
| ansond | 0:ae2a45502448 | 78 | for(int i=0;i<NUM_LIGHTS;++i) { |
| ansond | 0:ae2a45502448 | 79 | this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this); |
| ansond | 0:ae2a45502448 | 80 | this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i])); |
| ansond | 0:ae2a45502448 | 81 | this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i])); |
| ansond | 0:ae2a45502448 | 82 | } |
| ansond | 0:ae2a45502448 | 83 | return true; |
| ansond | 0:ae2a45502448 | 84 | } |
| ansond | 0:ae2a45502448 | 85 | |
| ansond | 0:ae2a45502448 | 86 | // does the input name match any of our light resources? |
| ansond | 0:ae2a45502448 | 87 | int MBEDEndpoint::indexOfLight(char *name) { |
| ansond | 0:ae2a45502448 | 88 | bool found = false; |
| ansond | 0:ae2a45502448 | 89 | int index = -1; |
| ansond | 0:ae2a45502448 | 90 | |
| ansond | 0:ae2a45502448 | 91 | for(int i=0;i<NUM_LIGHTS && !found;++i) { |
| ansond | 0:ae2a45502448 | 92 | if (strcmp(this->m_lights[i]->getName(),name) == 0) { |
| ansond | 0:ae2a45502448 | 93 | found = true; |
| ansond | 0:ae2a45502448 | 94 | index = i; |
| ansond | 0:ae2a45502448 | 95 | } |
| ansond | 0:ae2a45502448 | 96 | } |
| ansond | 0:ae2a45502448 | 97 | |
| ansond | 0:ae2a45502448 | 98 | return index; |
| ansond | 0:ae2a45502448 | 99 | } |
| ansond | 0:ae2a45502448 | 100 | |
| ansond | 0:ae2a45502448 | 101 | // get a specific resources |
| ansond | 0:ae2a45502448 | 102 | ResourceFactory *MBEDEndpoint::getResources(int index) { |
| ansond | 0:ae2a45502448 | 103 | if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources(); |
| ansond | 0:ae2a45502448 | 104 | return NULL; |
| ansond | 0:ae2a45502448 | 105 | } |
| ansond | 0:ae2a45502448 | 106 | |
| ansond | 0:ae2a45502448 | 107 | // initialize our ResourceFactory |
| ansond | 2:90a84a216c58 | 108 | ResourceFactory *MBEDEndpoint::initResourceFactory() { return new EmulatedResourceFactory(this->logger()); } |
| ansond | 2:90a84a216c58 | 109 | |
| ansond | 2:90a84a216c58 | 110 | // get our endpoint name (first light name) |
| ansond | 6:34c07e145caa | 111 | char *MBEDEndpoint::getEndpointName() { return this->m_lights[0]->getName(); } |
| ansond | 0:ae2a45502448 | 112 | |
| ansond | 0:ae2a45502448 | 113 | // initialize a specific transport |
| ansond | 0:ae2a45502448 | 114 | bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) { |
| ansond | 0:ae2a45502448 | 115 | bool success = false; |
| ansond | 0:ae2a45502448 | 116 | if (this->m_transports[index] == NULL) { |
| ansond | 0:ae2a45502448 | 117 | this->logger()->log("Initializing %s Transport...", key); |
| ansond | 0:ae2a45502448 | 118 | this->m_transports[index] = transport; |
| ansond | 0:ae2a45502448 | 119 | if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect(); |
| ansond | 0:ae2a45502448 | 120 | } |
| ansond | 0:ae2a45502448 | 121 | else { |
| ansond | 0:ae2a45502448 | 122 | this->logger()->log("%s already connected (OK)...", key); |
| ansond | 0:ae2a45502448 | 123 | success = true; |
| ansond | 0:ae2a45502448 | 124 | } |
| ansond | 0:ae2a45502448 | 125 | return success; |
| ansond | 0:ae2a45502448 | 126 | } |
| ansond | 0:ae2a45502448 | 127 | |
| ansond | 0:ae2a45502448 | 128 | // initialize our transports |
| ansond | 0:ae2a45502448 | 129 | bool MBEDEndpoint::initializeTransports() { |
| ansond | 0:ae2a45502448 | 130 | bool success = true; |
| ansond | 0:ae2a45502448 | 131 | |
| ansond | 0:ae2a45502448 | 132 | if (success == true) { |
| ansond | 0:ae2a45502448 | 133 | // MQTT Initialization |
| ansond | 15:e44d75d95b38 | 134 | success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this,this->getMap())); |
| ansond | 0:ae2a45502448 | 135 | } |
| ansond | 0:ae2a45502448 | 136 | |
| ansond | 0:ae2a45502448 | 137 | if (success == true) { |
| ansond | 0:ae2a45502448 | 138 | // HTTP Initialization |
| ansond | 7:f570eb3f38cd | 139 | success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler,this)); |
| ansond | 0:ae2a45502448 | 140 | } |
| ansond | 0:ae2a45502448 | 141 | return success; |
| ansond | 0:ae2a45502448 | 142 | } |
| ansond | 0:ae2a45502448 | 143 | |
| ansond | 0:ae2a45502448 | 144 | // initialize our Ethernet |
| ansond | 0:ae2a45502448 | 145 | bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) { |
| ansond | 0:ae2a45502448 | 146 | bool success = false; |
| ansond | 0:ae2a45502448 | 147 | this->m_ethernet = ethernet; |
| ansond | 0:ae2a45502448 | 148 | if (this->m_ethernet != NULL) { |
| ansond | 0:ae2a45502448 | 149 | this->logger()->log("Initializing Ethernet..."); |
| ansond | 0:ae2a45502448 | 150 | |
| ansond | 0:ae2a45502448 | 151 | // connect up ethernet |
| ansond | 0:ae2a45502448 | 152 | this->m_ethernet->init(); |
| ansond | 0:ae2a45502448 | 153 | this->m_ethernet->connect(); |
| ansond | 0:ae2a45502448 | 154 | |
| ansond | 0:ae2a45502448 | 155 | // display our IP address |
| ansond | 0:ae2a45502448 | 156 | char *ipaddr = this->m_ethernet->getIPAddress(); |
| ansond | 0:ae2a45502448 | 157 | if (ipaddr != NULL && strlen(ipaddr) > 0) { |
| ansond | 0:ae2a45502448 | 158 | this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress()); |
| ansond | 0:ae2a45502448 | 159 | success = true; |
| ansond | 0:ae2a45502448 | 160 | } |
| ansond | 0:ae2a45502448 | 161 | else { |
| ansond | 0:ae2a45502448 | 162 | this->logger()->log("Ethernet Not Connected..."); |
| ansond | 0:ae2a45502448 | 163 | success = false; |
| ansond | 0:ae2a45502448 | 164 | } |
| ansond | 0:ae2a45502448 | 165 | } |
| ansond | 0:ae2a45502448 | 166 | else { |
| ansond | 0:ae2a45502448 | 167 | this->logger()->log("No Ethernet instance found"); |
| ansond | 0:ae2a45502448 | 168 | success = false; |
| ansond | 0:ae2a45502448 | 169 | } |
| ansond | 0:ae2a45502448 | 170 | return success; |
| ansond | 0:ae2a45502448 | 171 | } |
| ansond | 0:ae2a45502448 | 172 | |
| ansond | 13:25448d92c205 | 173 | // load up all endpoints into the IOC |
| ansond | 13:25448d92c205 | 174 | bool MBEDEndpoint::loadEndpoints() { |
| ansond | 13:25448d92c205 | 175 | bool success = true; |
| ansond | 15:e44d75d95b38 | 176 | this->logger()->log("Loading All Endpoints to IOC..."); |
| ansond | 13:25448d92c205 | 177 | for(int i=0;i<NUM_LIGHTS && success;++i) success = this->loadEndpoint(this->m_lights[i]); |
| ansond | 13:25448d92c205 | 178 | return success; |
| ansond | 13:25448d92c205 | 179 | } |
| ansond | 13:25448d92c205 | 180 | |
| ansond | 0:ae2a45502448 | 181 | // load up our endpoint to the IOC |
| ansond | 15:e44d75d95b38 | 182 | bool MBEDEndpoint::loadEndpoint(Light *light) { |
| ansond | 11:59ee476fda24 | 183 | bool success = false; |
| ansond | 11:59ee476fda24 | 184 | char result[IOC_RESULT_LEN+1]; |
| ansond | 11:59ee476fda24 | 185 | char payload[IOC_PAYLOAD_LEN+1]; |
| ansond | 11:59ee476fda24 | 186 | |
| ansond | 11:59ee476fda24 | 187 | // initialize |
| ansond | 17:84ab108ed670 | 188 | memset(result,0,IOC_RESULT_LEN+1); |
| ansond | 17:84ab108ed670 | 189 | memset(payload,0,IOC_PAYLOAD_LEN+1); |
| ansond | 15:e44d75d95b38 | 190 | |
| ansond | 15:e44d75d95b38 | 191 | // DEBUG |
| ansond | 15:e44d75d95b38 | 192 | this->logger()->log("Loading Light: %s...",light->getName()); |
| ansond | 12:952dce085876 | 193 | |
| ansond | 11:59ee476fda24 | 194 | // build the payload |
| ansond | 14:0a6497a380a4 | 195 | char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light); |
| ansond | 11:59ee476fda24 | 196 | |
| ansond | 15:e44d75d95b38 | 197 | // DEBUG |
| ansond | 15:e44d75d95b38 | 198 | this->logger()->log("Sending Light: %s to the IOC...",light->getName()); |
| ansond | 15:e44d75d95b38 | 199 | |
| ansond | 15:e44d75d95b38 | 200 | return true; |
| ansond | 15:e44d75d95b38 | 201 | |
| ansond | 11:59ee476fda24 | 202 | // issue the request |
| ansond | 12:952dce085876 | 203 | success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN); |
| ansond | 11:59ee476fda24 | 204 | |
| ansond | 15:e44d75d95b38 | 205 | // DEBUG |
| ansond | 15:e44d75d95b38 | 206 | this->logger()->log("Saving IOC ID for Light: %s...",light->getName()); |
| ansond | 15:e44d75d95b38 | 207 | |
| ansond | 11:59ee476fda24 | 208 | // update the IOC ID if found |
| ansond | 11:59ee476fda24 | 209 | if (success) this->saveIOCID(result); |
| ansond | 11:59ee476fda24 | 210 | |
| ansond | 15:e44d75d95b38 | 211 | // DEBUG |
| ansond | 15:e44d75d95b38 | 212 | this->logger()->log("Light: %s IOC ID=%d...",light->getName(),this->getIOCID()); |
| ansond | 15:e44d75d95b38 | 213 | |
| ansond | 11:59ee476fda24 | 214 | // return our status |
| ansond | 11:59ee476fda24 | 215 | return success; |
| ansond | 11:59ee476fda24 | 216 | } |
| ansond | 0:ae2a45502448 | 217 | |
| ansond | 13:25448d92c205 | 218 | // update all endpoints to the IOC |
| ansond | 13:25448d92c205 | 219 | bool MBEDEndpoint::updateEndpoints() { |
| ansond | 13:25448d92c205 | 220 | bool success = true; |
| ansond | 13:25448d92c205 | 221 | for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i); |
| ansond | 13:25448d92c205 | 222 | return success; |
| ansond | 13:25448d92c205 | 223 | } |
| ansond | 13:25448d92c205 | 224 | |
| ansond | 13:25448d92c205 | 225 | // update all endpoints to the IOC |
| ansond | 13:25448d92c205 | 226 | bool MBEDEndpoint::updateEndpoints(int index) { |
| ansond | 13:25448d92c205 | 227 | if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]); |
| ansond | 13:25448d92c205 | 228 | return false; |
| ansond | 13:25448d92c205 | 229 | } |
| ansond | 13:25448d92c205 | 230 | |
| ansond | 0:ae2a45502448 | 231 | // update our endpoint with the IOC |
| ansond | 13:25448d92c205 | 232 | bool MBEDEndpoint::updateEndpoint(Light *light) { |
| ansond | 11:59ee476fda24 | 233 | bool success = false; |
| ansond | 11:59ee476fda24 | 234 | char result[IOC_RESULT_LEN+1]; |
| ansond | 11:59ee476fda24 | 235 | char payload[IOC_PAYLOAD_LEN+1]; |
| ansond | 11:59ee476fda24 | 236 | |
| ansond | 11:59ee476fda24 | 237 | // initialize |
| ansond | 17:84ab108ed670 | 238 | memset(result,0,IOC_RESULT_LEN+1); |
| ansond | 17:84ab108ed670 | 239 | memset(payload,0,IOC_PAYLOAD_LEN+1); |
| ansond | 12:952dce085876 | 240 | |
| ansond | 11:59ee476fda24 | 241 | // build the payload |
| ansond | 14:0a6497a380a4 | 242 | char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light); |
| ansond | 11:59ee476fda24 | 243 | |
| ansond | 11:59ee476fda24 | 244 | // issue the request |
| ansond | 12:952dce085876 | 245 | success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(this->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN); |
| ansond | 11:59ee476fda24 | 246 | |
| ansond | 11:59ee476fda24 | 247 | // return our status |
| ansond | 11:59ee476fda24 | 248 | return success; |
| ansond | 11:59ee476fda24 | 249 | } |
| ansond | 11:59ee476fda24 | 250 | |
| ansond | 11:59ee476fda24 | 251 | // build out the Payload |
| ansond | 13:25448d92c205 | 252 | char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) { |
| ansond | 11:59ee476fda24 | 253 | // construct the payload for Load/Updates |
| ansond | 13:25448d92c205 | 254 | ResourceFactory *factory = light->getResourceFactory(); |
| ansond | 15:e44d75d95b38 | 255 | |
| ansond | 13:25448d92c205 | 256 | // JSON factory |
| ansond | 13:25448d92c205 | 257 | MbedJSONValue json; |
| ansond | 13:25448d92c205 | 258 | |
| ansond | 13:25448d92c205 | 259 | // loop through the resources and build a JSON representation for the payload |
| ansond | 13:25448d92c205 | 260 | for(int i=0;i<factory->numResources();++i) { |
| ansond | 13:25448d92c205 | 261 | // get the ith resource |
| ansond | 13:25448d92c205 | 262 | Resource *resource = factory->getResource(i); |
| ansond | 13:25448d92c205 | 263 | |
| ansond | 13:25448d92c205 | 264 | // add to the JSON payload |
| ansond | 17:84ab108ed670 | 265 | char *name = this->getMap()->endpointNameToIOCName(resource->getName()); |
| ansond | 17:84ab108ed670 | 266 | |
| ansond | 17:84ab108ed670 | 267 | this->logger()->log("Name: %s Value: %s",name,resource->getValue()); |
| ansond | 17:84ab108ed670 | 268 | |
| ansond | 17:84ab108ed670 | 269 | json[name] = resource->getValue(); |
| ansond | 13:25448d92c205 | 270 | } |
| ansond | 13:25448d92c205 | 271 | |
| ansond | 17:84ab108ed670 | 272 | // Special Case: STARTDATETIME |
| ansond | 17:84ab108ed670 | 273 | |
| ansond | 17:84ab108ed670 | 274 | // Special Case: ENDDATETIME |
| ansond | 17:84ab108ed670 | 275 | |
| ansond | 17:84ab108ed670 | 276 | // Speical Case: LOCATION |
| ansond | 17:84ab108ed670 | 277 | |
| ansond | 13:25448d92c205 | 278 | // now convert to JSON |
| ansond | 17:84ab108ed670 | 279 | //std::string str_json = json.serialize(); |
| ansond | 13:25448d92c205 | 280 | |
| ansond | 13:25448d92c205 | 281 | // copy over to the buffer |
| ansond | 15:e44d75d95b38 | 282 | //strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length)); |
| ansond | 15:e44d75d95b38 | 283 | |
| ansond | 13:25448d92c205 | 284 | // DEBUG |
| ansond | 17:84ab108ed670 | 285 | //this->logger()->log("Loading Payload: %s",data); |
| ansond | 11:59ee476fda24 | 286 | |
| ansond | 11:59ee476fda24 | 287 | // return the payload |
| ansond | 12:952dce085876 | 288 | return data; |
| ansond | 11:59ee476fda24 | 289 | } |
| ansond | 11:59ee476fda24 | 290 | |
| ansond | 11:59ee476fda24 | 291 | // save the IOC ID |
| ansond | 13:25448d92c205 | 292 | void MBEDEndpoint::saveIOCID(char *json) { |
| ansond | 13:25448d92c205 | 293 | // JSON factory |
| ansond | 13:25448d92c205 | 294 | MbedJSONValue parser; |
| ansond | 11:59ee476fda24 | 295 | |
| ansond | 13:25448d92c205 | 296 | // parse the result |
| ansond | 13:25448d92c205 | 297 | parse(parser,json); |
| ansond | 13:25448d92c205 | 298 | |
| ansond | 13:25448d92c205 | 299 | // look for the ID element specifically |
| ansond | 13:25448d92c205 | 300 | int ioc_id = parser[IOC_REPONSE_ID_KEY].get<int>(); |
| ansond | 11:59ee476fda24 | 301 | |
| ansond | 11:59ee476fda24 | 302 | // save the IOC ID |
| ansond | 11:59ee476fda24 | 303 | if (ioc_id > 0) this->setIOCID(ioc_id); |
| ansond | 11:59ee476fda24 | 304 | } |
| ansond | 0:ae2a45502448 | 305 | |
| ansond | 0:ae2a45502448 | 306 | // close down the Lights |
| ansond | 0:ae2a45502448 | 307 | bool MBEDEndpoint::closeLights() { |
| ansond | 0:ae2a45502448 | 308 | bool success = true; |
| ansond | 0:ae2a45502448 | 309 | this->logger()->log("Closing down Lights..."); |
| ansond | 15:e44d75d95b38 | 310 | for(int i=0;i<NUM_LIGHTS;++i) |
| ansond | 15:e44d75d95b38 | 311 | if (this->m_lights[i] != NULL) delete this->m_lights[i]; |
| ansond | 0:ae2a45502448 | 312 | return success; |
| ansond | 0:ae2a45502448 | 313 | } |
| ansond | 0:ae2a45502448 | 314 | |
| ansond | 0:ae2a45502448 | 315 | // close a given transport |
| ansond | 0:ae2a45502448 | 316 | bool MBEDEndpoint::closeTransport(int index,char *key) { |
| ansond | 0:ae2a45502448 | 317 | this->logger()->log("Closing down %s Transport...", key); |
| ansond | 0:ae2a45502448 | 318 | if (this->m_transports[index] != NULL) delete this->m_transports[index]; |
| ansond | 0:ae2a45502448 | 319 | return true; |
| ansond | 0:ae2a45502448 | 320 | } |
| ansond | 0:ae2a45502448 | 321 | |
| ansond | 0:ae2a45502448 | 322 | // close down our transports |
| ansond | 0:ae2a45502448 | 323 | bool MBEDEndpoint::closeTransports() { |
| ansond | 0:ae2a45502448 | 324 | bool success = true; |
| ansond | 0:ae2a45502448 | 325 | |
| ansond | 0:ae2a45502448 | 326 | if (success) { |
| ansond | 0:ae2a45502448 | 327 | // close MQTT |
| ansond | 0:ae2a45502448 | 328 | success = this->closeTransport(MQTT_TRANSPORT,"MQTT"); |
| ansond | 0:ae2a45502448 | 329 | } |
| ansond | 0:ae2a45502448 | 330 | |
| ansond | 0:ae2a45502448 | 331 | if (success) { |
| ansond | 0:ae2a45502448 | 332 | // close HTTP |
| ansond | 0:ae2a45502448 | 333 | success = this->closeTransport(HTTP_TRANSPORT,"HTTP"); |
| ansond | 0:ae2a45502448 | 334 | } |
| ansond | 0:ae2a45502448 | 335 | |
| ansond | 0:ae2a45502448 | 336 | return success; |
| ansond | 0:ae2a45502448 | 337 | } |
| ansond | 0:ae2a45502448 | 338 | |
| ansond | 0:ae2a45502448 | 339 | // close down our Ethernet |
| ansond | 0:ae2a45502448 | 340 | bool MBEDEndpoint::closeEthernet() { |
| ansond | 0:ae2a45502448 | 341 | this->logger()->log("Closing down Ethernet..."); |
| ansond | 0:ae2a45502448 | 342 | if (this->m_ethernet != NULL) this->m_ethernet->disconnect(); |
| ansond | 0:ae2a45502448 | 343 | return true; |
| ansond | 0:ae2a45502448 | 344 | } |
| ansond | 0:ae2a45502448 | 345 | |
| ansond | 13:25448d92c205 | 346 | // min function |
| ansond | 13:25448d92c205 | 347 | int MBEDEndpoint::min(int value1,int value2) { |
| ansond | 13:25448d92c205 | 348 | if (value1 < value2) return value1; |
| ansond | 13:25448d92c205 | 349 | return value2; |
| ansond | 13:25448d92c205 | 350 | } |
| ansond | 13:25448d92c205 | 351 | |
| ansond | 0:ae2a45502448 | 352 | // get our error handler |
| ansond | 0:ae2a45502448 | 353 | ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; } |
| ansond | 0:ae2a45502448 | 354 | |
| ansond | 0:ae2a45502448 | 355 | // main running loop |
| ansond | 0:ae2a45502448 | 356 | void MBEDEndpoint::run() { |
| ansond | 0:ae2a45502448 | 357 | this->logger()->log("Endpoint Main Loop"); |
| ansond | 0:ae2a45502448 | 358 | while(true) { |
| ansond | 0:ae2a45502448 | 359 | // sleep a bit |
| ansond | 0:ae2a45502448 | 360 | //this->logger()->log("Sleeping for a bit..."); |
| ansond | 0:ae2a45502448 | 361 | wait_ms(MAIN_LOOP_SLEEP); |
| ansond | 0:ae2a45502448 | 362 | |
| ansond | 0:ae2a45502448 | 363 | // check for events |
| ansond | 0:ae2a45502448 | 364 | //this->logger()->log("Processing Events..."); |
| ansond | 0:ae2a45502448 | 365 | for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess(); |
| ansond | 0:ae2a45502448 | 366 | |
| ansond | 0:ae2a45502448 | 367 | // check for exit |
| ansond | 0:ae2a45502448 | 368 | //this->logger()->log("Checking for exit..."); |
| ansond | 0:ae2a45502448 | 369 | this->logger()->checkForExit(); |
| ansond | 0:ae2a45502448 | 370 | } |
| ansond | 0:ae2a45502448 | 371 | } |
| ansond | 0:ae2a45502448 | 372 |