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@15:e44d75d95b38, 2014-02-27 (annotated)
- Committer:
- ansond
- Date:
- Thu Feb 27 06:05:01 2014 +0000
- Revision:
- 15:e44d75d95b38
- Parent:
- 14:0a6497a380a4
- Child:
- 17:84ab108ed670
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 | 15:e44d75d95b38 | 188 | //memset(result,0,IOC_RESULT_LEN+1); | 
| ansond | 15:e44d75d95b38 | 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 | 15:e44d75d95b38 | 193 | |
| ansond | 15:e44d75d95b38 | 194 | return true; | 
| ansond | 12:952dce085876 | 195 | |
| ansond | 11:59ee476fda24 | 196 | // build the payload | 
| ansond | 14:0a6497a380a4 | 197 | char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light); | 
| ansond | 11:59ee476fda24 | 198 | |
| ansond | 15:e44d75d95b38 | 199 | // DEBUG | 
| ansond | 15:e44d75d95b38 | 200 | this->logger()->log("Sending Light: %s to the IOC...",light->getName()); | 
| ansond | 15:e44d75d95b38 | 201 | |
| ansond | 15:e44d75d95b38 | 202 | return true; | 
| ansond | 15:e44d75d95b38 | 203 | |
| ansond | 11:59ee476fda24 | 204 | // issue the request | 
| ansond | 12:952dce085876 | 205 | success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN); | 
| ansond | 11:59ee476fda24 | 206 | |
| ansond | 15:e44d75d95b38 | 207 | // DEBUG | 
| ansond | 15:e44d75d95b38 | 208 | 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 | 11:59ee476fda24 | 211 | if (success) this->saveIOCID(result); | 
| ansond | 11:59ee476fda24 | 212 | |
| ansond | 15:e44d75d95b38 | 213 | // DEBUG | 
| ansond | 15:e44d75d95b38 | 214 | this->logger()->log("Light: %s IOC ID=%d...",light->getName(),this->getIOCID()); | 
| ansond | 15:e44d75d95b38 | 215 | |
| ansond | 11:59ee476fda24 | 216 | // return our status | 
| ansond | 11:59ee476fda24 | 217 | return success; | 
| ansond | 11:59ee476fda24 | 218 | } | 
| ansond | 0:ae2a45502448 | 219 | |
| ansond | 13:25448d92c205 | 220 | // update all endpoints to the IOC | 
| ansond | 13:25448d92c205 | 221 | bool MBEDEndpoint::updateEndpoints() { | 
| ansond | 13:25448d92c205 | 222 | bool success = true; | 
| ansond | 13:25448d92c205 | 223 | for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i); | 
| ansond | 13:25448d92c205 | 224 | return success; | 
| ansond | 13:25448d92c205 | 225 | } | 
| ansond | 13:25448d92c205 | 226 | |
| ansond | 13:25448d92c205 | 227 | // update all endpoints to the IOC | 
| ansond | 13:25448d92c205 | 228 | bool MBEDEndpoint::updateEndpoints(int index) { | 
| ansond | 13:25448d92c205 | 229 | if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]); | 
| ansond | 13:25448d92c205 | 230 | return false; | 
| ansond | 13:25448d92c205 | 231 | } | 
| ansond | 13:25448d92c205 | 232 | |
| ansond | 0:ae2a45502448 | 233 | // update our endpoint with the IOC | 
| ansond | 13:25448d92c205 | 234 | bool MBEDEndpoint::updateEndpoint(Light *light) { | 
| ansond | 11:59ee476fda24 | 235 | bool success = false; | 
| ansond | 11:59ee476fda24 | 236 | char result[IOC_RESULT_LEN+1]; | 
| ansond | 11:59ee476fda24 | 237 | char payload[IOC_PAYLOAD_LEN+1]; | 
| ansond | 11:59ee476fda24 | 238 | |
| ansond | 11:59ee476fda24 | 239 | // initialize | 
| ansond | 15:e44d75d95b38 | 240 | //memset(result,0,IOC_RESULT_LEN+1); | 
| ansond | 15:e44d75d95b38 | 241 | //memset(payload,0,IOC_PAYLOAD_LEN+1); | 
| ansond | 12:952dce085876 | 242 | |
| ansond | 11:59ee476fda24 | 243 | // build the payload | 
| ansond | 14:0a6497a380a4 | 244 | char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light); | 
| ansond | 11:59ee476fda24 | 245 | |
| ansond | 11:59ee476fda24 | 246 | // issue the request | 
| ansond | 12:952dce085876 | 247 | success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(this->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN); | 
| ansond | 11:59ee476fda24 | 248 | |
| ansond | 11:59ee476fda24 | 249 | // return our status | 
| ansond | 11:59ee476fda24 | 250 | return success; | 
| ansond | 11:59ee476fda24 | 251 | } | 
| ansond | 11:59ee476fda24 | 252 | |
| ansond | 11:59ee476fda24 | 253 | // build out the Payload | 
| ansond | 13:25448d92c205 | 254 | char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) { | 
| ansond | 11:59ee476fda24 | 255 | // construct the payload for Load/Updates | 
| ansond | 13:25448d92c205 | 256 | ResourceFactory *factory = light->getResourceFactory(); | 
| ansond | 15:e44d75d95b38 | 257 | |
| ansond | 13:25448d92c205 | 258 | // JSON factory | 
| ansond | 13:25448d92c205 | 259 | MbedJSONValue json; | 
| ansond | 13:25448d92c205 | 260 | |
| ansond | 13:25448d92c205 | 261 | // loop through the resources and build a JSON representation for the payload | 
| ansond | 13:25448d92c205 | 262 | for(int i=0;i<factory->numResources();++i) { | 
| ansond | 13:25448d92c205 | 263 | // get the ith resource | 
| ansond | 13:25448d92c205 | 264 | Resource *resource = factory->getResource(i); | 
| ansond | 13:25448d92c205 | 265 | |
| ansond | 13:25448d92c205 | 266 | // add to the JSON payload | 
| ansond | 15:e44d75d95b38 | 267 | json[this->getMap()->endpointNameToIOCName(resource->getName())] = resource->getValue(); | 
| ansond | 13:25448d92c205 | 268 | } | 
| ansond | 13:25448d92c205 | 269 | |
| ansond | 13:25448d92c205 | 270 | // now convert to JSON | 
| ansond | 13:25448d92c205 | 271 | std::string str_json = json.serialize(); | 
| ansond | 13:25448d92c205 | 272 | |
| ansond | 13:25448d92c205 | 273 | // copy over to the buffer | 
| ansond | 15:e44d75d95b38 | 274 | //strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length)); | 
| ansond | 15:e44d75d95b38 | 275 | |
| ansond | 13:25448d92c205 | 276 | // DEBUG | 
| ansond | 13:25448d92c205 | 277 | this->logger()->log("Loading Payload: %s",data); | 
| ansond | 11:59ee476fda24 | 278 | |
| ansond | 11:59ee476fda24 | 279 | // return the payload | 
| ansond | 12:952dce085876 | 280 | return data; | 
| ansond | 11:59ee476fda24 | 281 | } | 
| ansond | 11:59ee476fda24 | 282 | |
| ansond | 11:59ee476fda24 | 283 | // save the IOC ID | 
| ansond | 13:25448d92c205 | 284 | void MBEDEndpoint::saveIOCID(char *json) { | 
| ansond | 13:25448d92c205 | 285 | // JSON factory | 
| ansond | 13:25448d92c205 | 286 | MbedJSONValue parser; | 
| ansond | 11:59ee476fda24 | 287 | |
| ansond | 13:25448d92c205 | 288 | // parse the result | 
| ansond | 13:25448d92c205 | 289 | parse(parser,json); | 
| ansond | 13:25448d92c205 | 290 | |
| ansond | 13:25448d92c205 | 291 | // look for the ID element specifically | 
| ansond | 13:25448d92c205 | 292 | int ioc_id = parser[IOC_REPONSE_ID_KEY].get<int>(); | 
| ansond | 11:59ee476fda24 | 293 | |
| ansond | 11:59ee476fda24 | 294 | // save the IOC ID | 
| ansond | 11:59ee476fda24 | 295 | if (ioc_id > 0) this->setIOCID(ioc_id); | 
| ansond | 11:59ee476fda24 | 296 | } | 
| ansond | 0:ae2a45502448 | 297 | |
| ansond | 0:ae2a45502448 | 298 | // close down the Lights | 
| ansond | 0:ae2a45502448 | 299 | bool MBEDEndpoint::closeLights() { | 
| ansond | 0:ae2a45502448 | 300 | bool success = true; | 
| ansond | 0:ae2a45502448 | 301 | this->logger()->log("Closing down Lights..."); | 
| ansond | 15:e44d75d95b38 | 302 | for(int i=0;i<NUM_LIGHTS;++i) | 
| ansond | 15:e44d75d95b38 | 303 | if (this->m_lights[i] != NULL) delete this->m_lights[i]; | 
| ansond | 0:ae2a45502448 | 304 | return success; | 
| ansond | 0:ae2a45502448 | 305 | } | 
| ansond | 0:ae2a45502448 | 306 | |
| ansond | 0:ae2a45502448 | 307 | // close a given transport | 
| ansond | 0:ae2a45502448 | 308 | bool MBEDEndpoint::closeTransport(int index,char *key) { | 
| ansond | 0:ae2a45502448 | 309 | this->logger()->log("Closing down %s Transport...", key); | 
| ansond | 0:ae2a45502448 | 310 | if (this->m_transports[index] != NULL) delete this->m_transports[index]; | 
| ansond | 0:ae2a45502448 | 311 | return true; | 
| ansond | 0:ae2a45502448 | 312 | } | 
| ansond | 0:ae2a45502448 | 313 | |
| ansond | 0:ae2a45502448 | 314 | // close down our transports | 
| ansond | 0:ae2a45502448 | 315 | bool MBEDEndpoint::closeTransports() { | 
| ansond | 0:ae2a45502448 | 316 | bool success = true; | 
| ansond | 0:ae2a45502448 | 317 | |
| ansond | 0:ae2a45502448 | 318 | if (success) { | 
| ansond | 0:ae2a45502448 | 319 | // close MQTT | 
| ansond | 0:ae2a45502448 | 320 | success = this->closeTransport(MQTT_TRANSPORT,"MQTT"); | 
| ansond | 0:ae2a45502448 | 321 | } | 
| ansond | 0:ae2a45502448 | 322 | |
| ansond | 0:ae2a45502448 | 323 | if (success) { | 
| ansond | 0:ae2a45502448 | 324 | // close HTTP | 
| ansond | 0:ae2a45502448 | 325 | success = this->closeTransport(HTTP_TRANSPORT,"HTTP"); | 
| ansond | 0:ae2a45502448 | 326 | } | 
| ansond | 0:ae2a45502448 | 327 | |
| ansond | 0:ae2a45502448 | 328 | return success; | 
| ansond | 0:ae2a45502448 | 329 | } | 
| ansond | 0:ae2a45502448 | 330 | |
| ansond | 0:ae2a45502448 | 331 | // close down our Ethernet | 
| ansond | 0:ae2a45502448 | 332 | bool MBEDEndpoint::closeEthernet() { | 
| ansond | 0:ae2a45502448 | 333 | this->logger()->log("Closing down Ethernet..."); | 
| ansond | 0:ae2a45502448 | 334 | if (this->m_ethernet != NULL) this->m_ethernet->disconnect(); | 
| ansond | 0:ae2a45502448 | 335 | return true; | 
| ansond | 0:ae2a45502448 | 336 | } | 
| ansond | 0:ae2a45502448 | 337 | |
| ansond | 13:25448d92c205 | 338 | // min function | 
| ansond | 13:25448d92c205 | 339 | int MBEDEndpoint::min(int value1,int value2) { | 
| ansond | 13:25448d92c205 | 340 | if (value1 < value2) return value1; | 
| ansond | 13:25448d92c205 | 341 | return value2; | 
| ansond | 13:25448d92c205 | 342 | } | 
| ansond | 13:25448d92c205 | 343 | |
| ansond | 0:ae2a45502448 | 344 | // get our error handler | 
| ansond | 0:ae2a45502448 | 345 | ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; } | 
| ansond | 0:ae2a45502448 | 346 | |
| ansond | 0:ae2a45502448 | 347 | // main running loop | 
| ansond | 0:ae2a45502448 | 348 | void MBEDEndpoint::run() { | 
| ansond | 0:ae2a45502448 | 349 | this->logger()->log("Endpoint Main Loop"); | 
| ansond | 0:ae2a45502448 | 350 | while(true) { | 
| ansond | 0:ae2a45502448 | 351 | // sleep a bit | 
| ansond | 0:ae2a45502448 | 352 | //this->logger()->log("Sleeping for a bit..."); | 
| ansond | 0:ae2a45502448 | 353 | wait_ms(MAIN_LOOP_SLEEP); | 
| ansond | 0:ae2a45502448 | 354 | |
| ansond | 0:ae2a45502448 | 355 | // check for events | 
| ansond | 0:ae2a45502448 | 356 | //this->logger()->log("Processing Events..."); | 
| ansond | 0:ae2a45502448 | 357 | for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess(); | 
| ansond | 0:ae2a45502448 | 358 | |
| ansond | 0:ae2a45502448 | 359 | // check for exit | 
| ansond | 0:ae2a45502448 | 360 | //this->logger()->log("Checking for exit..."); | 
| ansond | 0:ae2a45502448 | 361 | this->logger()->checkForExit(); | 
| ansond | 0:ae2a45502448 | 362 | } | 
| ansond | 0:ae2a45502448 | 363 | } | 
| ansond | 0:ae2a45502448 | 364 |