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