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