mqtt specific components for the impact mbed endpoint library
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp
IOCEndpoint.cpp@11:9ae0fe4517c1, 2014-03-28 (annotated)
- Committer:
- ansond
- Date:
- Fri Mar 28 17:49:19 2014 +0000
- Revision:
- 11:9ae0fe4517c1
- Parent:
- 7:8a4a61202b36
- Child:
- 36:23a1b2dde4d9
updates
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:8a4a61202b36 | 24 | |
ansond | 6:2db2c7e75ad9 | 25 | // default constructor |
ansond | 11:9ae0fe4517c1 | 26 | IOCEndpoint::IOCEndpoint(ErrorHandler *error_handler,void *endpoint) : BaseClass(error_handler,endpoint) { |
ansond | 6:2db2c7e75ad9 | 27 | } |
ansond | 6:2db2c7e75ad9 | 28 | |
ansond | 6:2db2c7e75ad9 | 29 | // default destructor |
ansond | 6:2db2c7e75ad9 | 30 | IOCEndpoint::~IOCEndpoint() { |
ansond | 6:2db2c7e75ad9 | 31 | } |
ansond | 6:2db2c7e75ad9 | 32 | |
ansond | 6:2db2c7e75ad9 | 33 | // build out the IOC Payload |
ansond | 7:8a4a61202b36 | 34 | char *IOCEndpoint::buildLightPayload(char *data,int data_length,Light *light) { |
ansond | 6:2db2c7e75ad9 | 35 | char tmp[TEMP_BUFFER_LEN+1]; |
ansond | 6:2db2c7e75ad9 | 36 | |
ansond | 6:2db2c7e75ad9 | 37 | // MBED Endpoint |
ansond | 11:9ae0fe4517c1 | 38 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
ansond | 6:2db2c7e75ad9 | 39 | |
ansond | 6:2db2c7e75ad9 | 40 | // construct the payload for Load/Updates |
ansond | 6:2db2c7e75ad9 | 41 | ResourceFactory *factory = light->getResourceFactory(); |
ansond | 6:2db2c7e75ad9 | 42 | |
ansond | 6:2db2c7e75ad9 | 43 | // start the buffer |
ansond | 6:2db2c7e75ad9 | 44 | strcat(data,"{"); |
ansond | 6:2db2c7e75ad9 | 45 | |
ansond | 6:2db2c7e75ad9 | 46 | // loop through the resources and build a JSON representation for the payload |
ansond | 6:2db2c7e75ad9 | 47 | for(int i=0;i<factory->numResources();++i) { |
ansond | 6:2db2c7e75ad9 | 48 | // get the ith resource |
ansond | 6:2db2c7e75ad9 | 49 | Resource *resource = factory->getResource(i); |
ansond | 6:2db2c7e75ad9 | 50 | if (resource != NULL) { |
ansond | 6:2db2c7e75ad9 | 51 | // add to the JSON payload |
ansond | 6:2db2c7e75ad9 | 52 | char *name = endpoint->getMap()->endpointNameToIOCName(resource->getName()); |
ansond | 6:2db2c7e75ad9 | 53 | char *value = resource->getValue(); |
ansond | 6:2db2c7e75ad9 | 54 | |
ansond | 6:2db2c7e75ad9 | 55 | // make sure that we have a positive IOC resource match for the NSP resource |
ansond | 6:2db2c7e75ad9 | 56 | if (name != NULL && strlen(name) > 0) { |
ansond | 6:2db2c7e75ad9 | 57 | // Handle LOCATION a special way |
ansond | 6:2db2c7e75ad9 | 58 | if (strcmp(name,"LOCATION") != 0) { |
ansond | 6:2db2c7e75ad9 | 59 | // standard name,value for IOC |
ansond | 6:2db2c7e75ad9 | 60 | sprintf(tmp, "\"%s\":\"%s\",",name,value); |
ansond | 6:2db2c7e75ad9 | 61 | } |
ansond | 6:2db2c7e75ad9 | 62 | else { |
ansond | 6:2db2c7e75ad9 | 63 | // IOC expects "Point(X,Y)" for LOCATION |
ansond | 6:2db2c7e75ad9 | 64 | sprintf(tmp, "\"%s\":\"Point(%s)\",",name,value); |
ansond | 6:2db2c7e75ad9 | 65 | } |
ansond | 6:2db2c7e75ad9 | 66 | strcat(data,tmp); |
ansond | 6:2db2c7e75ad9 | 67 | |
ansond | 6:2db2c7e75ad9 | 68 | // Handle /dev/addldata |
ansond | 6:2db2c7e75ad9 | 69 | char *dev_addldata = endpoint->getMap()->endpointNameToIOCName("/dev/addldata"); |
ansond | 7:8a4a61202b36 | 70 | if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getExternalID() > 0) { |
ansond | 7:8a4a61202b36 | 71 | char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getExternalID()); |
ansond | 6:2db2c7e75ad9 | 72 | sprintf(tmp,"\"%s\":\"id:%s\",",name,buf); |
ansond | 6:2db2c7e75ad9 | 73 | strcat(data,tmp); |
ansond | 6:2db2c7e75ad9 | 74 | } |
ansond | 6:2db2c7e75ad9 | 75 | } |
ansond | 6:2db2c7e75ad9 | 76 | } |
ansond | 6:2db2c7e75ad9 | 77 | } |
ansond | 6:2db2c7e75ad9 | 78 | |
ansond | 6:2db2c7e75ad9 | 79 | // Special Case: STARTDATETIME |
ansond | 6:2db2c7e75ad9 | 80 | strcat(data,ENDPOINT_STARTTIME); |
ansond | 6:2db2c7e75ad9 | 81 | |
ansond | 6:2db2c7e75ad9 | 82 | // Special Case: ENDDATETIME |
ansond | 6:2db2c7e75ad9 | 83 | strcat(data,ENDPOINT_STOPTIME); |
ansond | 6:2db2c7e75ad9 | 84 | |
ansond | 6:2db2c7e75ad9 | 85 | // Special Case: NAME |
ansond | 6:2db2c7e75ad9 | 86 | sprintf(tmp,"\"NAME\":\"%s\",",light->getName()); |
ansond | 6:2db2c7e75ad9 | 87 | strcat(data,tmp); |
ansond | 6:2db2c7e75ad9 | 88 | |
ansond | 6:2db2c7e75ad9 | 89 | // Special Case: TIMEZONEOFFSET |
ansond | 6:2db2c7e75ad9 | 90 | strcat(data,ENDPOINT_TIMEZONE); |
ansond | 6:2db2c7e75ad9 | 91 | |
ansond | 6:2db2c7e75ad9 | 92 | // close |
ansond | 6:2db2c7e75ad9 | 93 | strcat(data,"}"); |
ansond | 6:2db2c7e75ad9 | 94 | |
ansond | 6:2db2c7e75ad9 | 95 | // DEBUG |
ansond | 6:2db2c7e75ad9 | 96 | //this->logger()->log("Loading Payload: %s",data); |
ansond | 6:2db2c7e75ad9 | 97 | |
ansond | 6:2db2c7e75ad9 | 98 | // return the payload |
ansond | 6:2db2c7e75ad9 | 99 | return data; |
ansond | 6:2db2c7e75ad9 | 100 | } |
ansond | 6:2db2c7e75ad9 | 101 | |
ansond | 7:8a4a61202b36 | 102 | // save the IOC ID for our Personality |
ansond | 7:8a4a61202b36 | 103 | void IOCEndpoint::saveExternalID(Personality *instance,char *json) { |
ansond | 6:2db2c7e75ad9 | 104 | if (json != NULL) { |
ansond | 6:2db2c7e75ad9 | 105 | //this->logger()->log("RESULT: %s",json); |
ansond | 6:2db2c7e75ad9 | 106 | |
ansond | 6:2db2c7e75ad9 | 107 | // look for "id": |
ansond | 6:2db2c7e75ad9 | 108 | char *check = "\"id\":"; |
ansond | 6:2db2c7e75ad9 | 109 | char *pos1 = strstr(json,check); |
ansond | 6:2db2c7e75ad9 | 110 | if (pos1 != NULL) { |
ansond | 6:2db2c7e75ad9 | 111 | char *pos2 = strstr(pos1,","); |
ansond | 6:2db2c7e75ad9 | 112 | if (pos1 != NULL && pos2 != NULL && pos2 > pos1) { |
ansond | 6:2db2c7e75ad9 | 113 | pos1 += strlen(check); |
ansond | 6:2db2c7e75ad9 | 114 | int length = pos2 - pos1; |
ansond | 6:2db2c7e75ad9 | 115 | char str_ioc_id[IOC_IOC_ID_LEN+1]; |
ansond | 6:2db2c7e75ad9 | 116 | memset(str_ioc_id,0,IOC_IOC_ID_LEN+1); |
ansond | 6:2db2c7e75ad9 | 117 | strncpy(str_ioc_id,pos1,length); |
ansond | 6:2db2c7e75ad9 | 118 | |
ansond | 6:2db2c7e75ad9 | 119 | // DEBUG |
ansond | 6:2db2c7e75ad9 | 120 | //this->logger()->log("IOC ID found: %s",str_ioc_id); |
ansond | 6:2db2c7e75ad9 | 121 | |
ansond | 6:2db2c7e75ad9 | 122 | // parse into int |
ansond | 6:2db2c7e75ad9 | 123 | int ioc_id = 0; |
ansond | 6:2db2c7e75ad9 | 124 | sscanf(str_ioc_id,"%d",&ioc_id); |
ansond | 6:2db2c7e75ad9 | 125 | |
ansond | 6:2db2c7e75ad9 | 126 | // save the IOC ID |
ansond | 7:8a4a61202b36 | 127 | if (ioc_id > 0) instance->setExternalID(ioc_id); |
ansond | 6:2db2c7e75ad9 | 128 | } |
ansond | 6:2db2c7e75ad9 | 129 | else { |
ansond | 6:2db2c7e75ad9 | 130 | // cannot find the ID tag in the result JSON |
ansond | 6:2db2c7e75ad9 | 131 | this->logger()->log("Cannot find the IOC ID in the JSON result"); |
ansond | 6:2db2c7e75ad9 | 132 | this->logger()->log("JSON: %s",json); |
ansond | 6:2db2c7e75ad9 | 133 | } |
ansond | 6:2db2c7e75ad9 | 134 | } |
ansond | 6:2db2c7e75ad9 | 135 | else { |
ansond | 6:2db2c7e75ad9 | 136 | // cannot find the ID tag in the result JSON |
ansond | 6:2db2c7e75ad9 | 137 | this->logger()->log("Cannot find the IOC ID in the JSON result"); |
ansond | 6:2db2c7e75ad9 | 138 | this->logger()->log("JSON: %s",json); |
ansond | 6:2db2c7e75ad9 | 139 | } |
ansond | 6:2db2c7e75ad9 | 140 | } |
ansond | 11:9ae0fe4517c1 | 141 | } |