mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Committer:
ansond
Date:
Thu Mar 27 18:14:05 2014 +0000
Revision:
6:2db2c7e75ad9
Child:
7:8a4a61202b36
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 6:2db2c7e75ad9 1 /* Copyright C2013 Doug Anson, MIT License
ansond 6:2db2c7e75ad9 2 *
ansond 6:2db2c7e75ad9 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 6:2db2c7e75ad9 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 6:2db2c7e75ad9 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 6:2db2c7e75ad9 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 6:2db2c7e75ad9 7 * furnished to do so, subject to the following conditions:
ansond 6:2db2c7e75ad9 8 *
ansond 6:2db2c7e75ad9 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 6:2db2c7e75ad9 10 * substantial portions of the Software.
ansond 6:2db2c7e75ad9 11 *
ansond 6:2db2c7e75ad9 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 6:2db2c7e75ad9 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 6:2db2c7e75ad9 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 6:2db2c7e75ad9 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 6:2db2c7e75ad9 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 6:2db2c7e75ad9 17 */
ansond 6:2db2c7e75ad9 18
ansond 6:2db2c7e75ad9 19 // class support
ansond 6:2db2c7e75ad9 20 #include "IOCEndpoint.h"
ansond 6:2db2c7e75ad9 21
ansond 6:2db2c7e75ad9 22 // MBED instance support
ansond 6:2db2c7e75ad9 23 #include "MBEDEndpoint.h"
ansond 6:2db2c7e75ad9 24
ansond 6:2db2c7e75ad9 25 // default constructor
ansond 6:2db2c7e75ad9 26 IOCEndpoint::IOCEndpoint(ErrorHandler *error_handler,void *endpoint) {
ansond 6:2db2c7e75ad9 27 this->m_error_handler = error_handler;
ansond 6:2db2c7e75ad9 28 this->m_endpoint = endpoint;
ansond 6:2db2c7e75ad9 29 }
ansond 6:2db2c7e75ad9 30
ansond 6:2db2c7e75ad9 31 // default destructor
ansond 6:2db2c7e75ad9 32 IOCEndpoint::~IOCEndpoint() {
ansond 6:2db2c7e75ad9 33 }
ansond 6:2db2c7e75ad9 34
ansond 6:2db2c7e75ad9 35 // build out the IOC Payload
ansond 6:2db2c7e75ad9 36 char *IOCEndpoint::buildPayload(char *data,int data_length,Light *light) {
ansond 6:2db2c7e75ad9 37 char tmp[TEMP_BUFFER_LEN+1];
ansond 6:2db2c7e75ad9 38
ansond 6:2db2c7e75ad9 39 // MBED Endpoint
ansond 6:2db2c7e75ad9 40 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->m_endpoint;
ansond 6:2db2c7e75ad9 41
ansond 6:2db2c7e75ad9 42 // construct the payload for Load/Updates
ansond 6:2db2c7e75ad9 43 ResourceFactory *factory = light->getResourceFactory();
ansond 6:2db2c7e75ad9 44
ansond 6:2db2c7e75ad9 45 // start the buffer
ansond 6:2db2c7e75ad9 46 strcat(data,"{");
ansond 6:2db2c7e75ad9 47
ansond 6:2db2c7e75ad9 48 // loop through the resources and build a JSON representation for the payload
ansond 6:2db2c7e75ad9 49 for(int i=0;i<factory->numResources();++i) {
ansond 6:2db2c7e75ad9 50 // get the ith resource
ansond 6:2db2c7e75ad9 51 Resource *resource = factory->getResource(i);
ansond 6:2db2c7e75ad9 52 if (resource != NULL) {
ansond 6:2db2c7e75ad9 53 // add to the JSON payload
ansond 6:2db2c7e75ad9 54 char *name = endpoint->getMap()->endpointNameToIOCName(resource->getName());
ansond 6:2db2c7e75ad9 55 char *value = resource->getValue();
ansond 6:2db2c7e75ad9 56
ansond 6:2db2c7e75ad9 57 // make sure that we have a positive IOC resource match for the NSP resource
ansond 6:2db2c7e75ad9 58 if (name != NULL && strlen(name) > 0) {
ansond 6:2db2c7e75ad9 59 // Handle LOCATION a special way
ansond 6:2db2c7e75ad9 60 if (strcmp(name,"LOCATION") != 0) {
ansond 6:2db2c7e75ad9 61 // standard name,value for IOC
ansond 6:2db2c7e75ad9 62 sprintf(tmp, "\"%s\":\"%s\",",name,value);
ansond 6:2db2c7e75ad9 63 }
ansond 6:2db2c7e75ad9 64 else {
ansond 6:2db2c7e75ad9 65 // IOC expects "Point(X,Y)" for LOCATION
ansond 6:2db2c7e75ad9 66 sprintf(tmp, "\"%s\":\"Point(%s)\",",name,value);
ansond 6:2db2c7e75ad9 67 }
ansond 6:2db2c7e75ad9 68 strcat(data,tmp);
ansond 6:2db2c7e75ad9 69
ansond 6:2db2c7e75ad9 70 // Handle /dev/addldata
ansond 6:2db2c7e75ad9 71 char *dev_addldata = endpoint->getMap()->endpointNameToIOCName("/dev/addldata");
ansond 6:2db2c7e75ad9 72 if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getIOCID() > 0) {
ansond 6:2db2c7e75ad9 73 char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getIOCID());
ansond 6:2db2c7e75ad9 74 sprintf(tmp,"\"%s\":\"id:%s\",",name,buf);
ansond 6:2db2c7e75ad9 75 strcat(data,tmp);
ansond 6:2db2c7e75ad9 76 }
ansond 6:2db2c7e75ad9 77 }
ansond 6:2db2c7e75ad9 78 }
ansond 6:2db2c7e75ad9 79 }
ansond 6:2db2c7e75ad9 80
ansond 6:2db2c7e75ad9 81 // Special Case: STARTDATETIME
ansond 6:2db2c7e75ad9 82 strcat(data,ENDPOINT_STARTTIME);
ansond 6:2db2c7e75ad9 83
ansond 6:2db2c7e75ad9 84 // Special Case: ENDDATETIME
ansond 6:2db2c7e75ad9 85 strcat(data,ENDPOINT_STOPTIME);
ansond 6:2db2c7e75ad9 86
ansond 6:2db2c7e75ad9 87 // Special Case: NAME
ansond 6:2db2c7e75ad9 88 sprintf(tmp,"\"NAME\":\"%s\",",light->getName());
ansond 6:2db2c7e75ad9 89 strcat(data,tmp);
ansond 6:2db2c7e75ad9 90
ansond 6:2db2c7e75ad9 91 // Special Case: TIMEZONEOFFSET
ansond 6:2db2c7e75ad9 92 strcat(data,ENDPOINT_TIMEZONE);
ansond 6:2db2c7e75ad9 93
ansond 6:2db2c7e75ad9 94 // close
ansond 6:2db2c7e75ad9 95 strcat(data,"}");
ansond 6:2db2c7e75ad9 96
ansond 6:2db2c7e75ad9 97 // DEBUG
ansond 6:2db2c7e75ad9 98 //this->logger()->log("Loading Payload: %s",data);
ansond 6:2db2c7e75ad9 99
ansond 6:2db2c7e75ad9 100 // return the payload
ansond 6:2db2c7e75ad9 101 return data;
ansond 6:2db2c7e75ad9 102 }
ansond 6:2db2c7e75ad9 103
ansond 6:2db2c7e75ad9 104 // save the IOC ID for a Light node
ansond 6:2db2c7e75ad9 105 void IOCEndpoint::saveLightID(Light *light,char *json) {
ansond 6:2db2c7e75ad9 106 if (json != NULL) {
ansond 6:2db2c7e75ad9 107 //this->logger()->log("RESULT: %s",json);
ansond 6:2db2c7e75ad9 108
ansond 6:2db2c7e75ad9 109 // look for "id":
ansond 6:2db2c7e75ad9 110 char *check = "\"id\":";
ansond 6:2db2c7e75ad9 111 char *pos1 = strstr(json,check);
ansond 6:2db2c7e75ad9 112 if (pos1 != NULL) {
ansond 6:2db2c7e75ad9 113 char *pos2 = strstr(pos1,",");
ansond 6:2db2c7e75ad9 114 if (pos1 != NULL && pos2 != NULL && pos2 > pos1) {
ansond 6:2db2c7e75ad9 115 pos1 += strlen(check);
ansond 6:2db2c7e75ad9 116 int length = pos2 - pos1;
ansond 6:2db2c7e75ad9 117 char str_ioc_id[IOC_IOC_ID_LEN+1];
ansond 6:2db2c7e75ad9 118 memset(str_ioc_id,0,IOC_IOC_ID_LEN+1);
ansond 6:2db2c7e75ad9 119 strncpy(str_ioc_id,pos1,length);
ansond 6:2db2c7e75ad9 120
ansond 6:2db2c7e75ad9 121 // DEBUG
ansond 6:2db2c7e75ad9 122 //this->logger()->log("IOC ID found: %s",str_ioc_id);
ansond 6:2db2c7e75ad9 123
ansond 6:2db2c7e75ad9 124 // parse into int
ansond 6:2db2c7e75ad9 125 int ioc_id = 0;
ansond 6:2db2c7e75ad9 126 sscanf(str_ioc_id,"%d",&ioc_id);
ansond 6:2db2c7e75ad9 127
ansond 6:2db2c7e75ad9 128 // save the IOC ID
ansond 6:2db2c7e75ad9 129 if (ioc_id > 0) light->setIOCID(ioc_id);
ansond 6:2db2c7e75ad9 130 }
ansond 6:2db2c7e75ad9 131 else {
ansond 6:2db2c7e75ad9 132 // cannot find the ID tag in the result JSON
ansond 6:2db2c7e75ad9 133 this->logger()->log("Cannot find the IOC ID in the JSON result");
ansond 6:2db2c7e75ad9 134 this->logger()->log("JSON: %s",json);
ansond 6:2db2c7e75ad9 135 }
ansond 6:2db2c7e75ad9 136 }
ansond 6:2db2c7e75ad9 137 else {
ansond 6:2db2c7e75ad9 138 // cannot find the ID tag in the result JSON
ansond 6:2db2c7e75ad9 139 this->logger()->log("Cannot find the IOC ID in the JSON result");
ansond 6:2db2c7e75ad9 140 this->logger()->log("JSON: %s",json);
ansond 6:2db2c7e75ad9 141 }
ansond 6:2db2c7e75ad9 142 }
ansond 6:2db2c7e75ad9 143 }
ansond 6:2db2c7e75ad9 144
ansond 6:2db2c7e75ad9 145 // get our error handler
ansond 6:2db2c7e75ad9 146 ErrorHandler *IOCEndpoint::logger() { return this->m_error_handler; }