Doug Anson / endpoint_mqtt

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IOCEndpoint.cpp Source File

IOCEndpoint.cpp

00001 /* Copyright C2013 Doug Anson, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files the "Software", to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019  // class support
00020  #include "IOCEndpoint.h"
00021  
00022  // MBED instance support
00023  #include "MBEDEndpoint.h"
00024   
00025  // default constructor
00026  IOCEndpoint::IOCEndpoint(Logger *logger,void *endpoint) : BaseClass(logger,endpoint) {
00027  }
00028  
00029  // default destructor
00030  IOCEndpoint::~IOCEndpoint() {
00031  }
00032  
00033  // build out the IOC Payload
00034  char *IOCEndpoint::buildLightPayload(char *data,int data_length,Light *light) {
00035      char tmp[TEMP_BUFFER_LEN+1];
00036      
00037      // MBED Endpoint
00038      MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
00039       
00040      // construct the payload for Load/Updates
00041      ResourceFactory *factory = light->getResourceFactory();
00042      
00043      // start the buffer
00044      strcat(data,"{");
00045      
00046      // loop through the resources and build a JSON representation for the payload
00047      for(int i=0;i<factory->numResources();++i) {
00048          // get the ith resource
00049          Resource *resource = factory->getResource(i);
00050          if (resource != NULL) {
00051              // add to the JSON payload
00052              char *name = endpoint->getMap()->endpointNameToIOCName(resource->getName());
00053              char *value = resource->getValue();
00054              
00055              // make sure that we have a positive IOC resource match for the NSP resource 
00056              if (name != NULL && strlen(name) > 0) {
00057                  // Handle LOCATION a special way
00058                  if (strcmp(name,"LOCATION") != 0) {
00059                      // standard name,value for IOC
00060                      sprintf(tmp, "\"%s\":\"%s\",",name,value); 
00061                   }
00062                  else {
00063                      // IOC expects "Point(X,Y)" for LOCATION
00064                      sprintf(tmp, "\"%s\":\"Point(%s)\",",name,value); 
00065                  }   
00066                  strcat(data,tmp);
00067             
00068                  // Handle /dev/addldata 
00069                  char *dev_addldata = endpoint->getMap()->endpointNameToIOCName("/dev/addldata");
00070                  if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getExternalID() > 0) {
00071                      char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getExternalID());
00072                      sprintf(tmp,"\"%s\":\"id:%s\",",name,buf);
00073                      strcat(data,tmp);
00074                  }
00075              }
00076          }
00077      }
00078      
00079      // Special Case: STARTDATETIME
00080      strcat(data,ENDPOINT_STARTTIME);
00081      
00082      // Special Case: ENDDATETIME
00083      strcat(data,ENDPOINT_STOPTIME);
00084                 
00085      // Special Case: NAME
00086      sprintf(tmp,"\"NAME\":\"%s\",",light->getName());
00087      strcat(data,tmp);
00088           
00089      // Special Case: TIMEZONEOFFSET
00090      strcat(data,ENDPOINT_TIMEZONE);
00091           
00092      // close
00093      strcat(data,"}");
00094      
00095      // DEBUG
00096      //this->logger()->log("Loading Payload: %s",data);
00097      
00098      // return the payload
00099      return data;
00100  }
00101   
00102  // save the IOC ID for our Personality
00103  void IOCEndpoint::saveExternalID(Personality *instance,char *json) {
00104      if (json != NULL) {          
00105          //this->logger()->log("RESULT: %s",json);
00106          
00107          // look for "id":
00108          char *check = "\"id\":";
00109          char *pos1 = strstr(json,check);
00110          if (pos1 != NULL) {
00111              char *pos2 = strstr(pos1,","); 
00112              if (pos1 != NULL && pos2 != NULL && pos2 > pos1) {
00113                  pos1 += strlen(check);
00114                  int length = pos2 - pos1;  
00115                  char str_ioc_id[IOC_IOC_ID_LEN+1];
00116                  memset(str_ioc_id,0,IOC_IOC_ID_LEN+1);
00117                  strncpy(str_ioc_id,pos1,length);
00118                  
00119                  // DEBUG
00120                  //this->logger()->log("IOC ID found: %s",str_ioc_id);
00121                  
00122                  // parse into int  
00123                  int ioc_id = 0;             
00124                  sscanf(str_ioc_id,"%d",&ioc_id);
00125                           
00126                  // save the IOC ID
00127                  if (ioc_id > 0) instance->setExternalID(ioc_id);
00128              }
00129              else {
00130                  // cannot find the ID tag in the result JSON
00131                  this->logger()->log("Cannot find the IOC ID in the JSON result");
00132                  this->logger()->log("JSON: %s",json);
00133              }
00134          }
00135          else {
00136              // cannot find the ID tag in the result JSON
00137              this->logger()->log("Cannot find the IOC ID in the JSON result");
00138              this->logger()->log("JSON: %s",json);
00139         }
00140      }
00141   }