mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Revision:
6:2db2c7e75ad9
Parent:
5:1ba6e68bf50e
Child:
7:8a4a61202b36
--- a/MBEDEndpoint.cpp	Thu Mar 27 17:48:38 2014 +0000
+++ b/MBEDEndpoint.cpp	Thu Mar 27 18:14:05 2014 +0000
@@ -45,6 +45,7 @@
      memset(this->m_gw_address,0,PREFERENCE_VALUE_LEN+1);
      for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
      this->m_error_handler = error_handler;
+     this->m_ioc_endpoint = new IOCEndpoint(error_handler,(void *)this);
      this->m_error_handler->setEndpoint((void *)this);
      if (success) this->initPreferences();
      if (success) this->initEndpointName();
@@ -62,7 +63,7 @@
      if (success) this->logger()->turnLEDYellow();
      if (success)this->m_map = new MBEDToIOCResourceMap(error_handler); 
      if (success) success = this->initializeTransports();
-     if (success) success = this->initializePersonality();   
+     if (success) success = this->initializeEndpointPersonality();   
      if (success) this->logger()->turnLEDOrange();
      this->logger()->lcdStatusOnly(true);
      if (!success) closedown(2);
@@ -72,7 +73,7 @@
  MBEDEndpoint::~MBEDEndpoint() {
      bool success = true;
      if (success) this->logger()->turnLEDYellow();
-     if (success) success = this->closePersonality();
+     if (success) success = this->closeEndpointPersonality();
      if (success) success = this->closeTransports();
      if (success) success = this->closeEthernet();
      if (success) this->logger()->turnLEDBlue();
@@ -263,7 +264,7 @@
      this->logger()->log("Building Payload for Light: %s...",light->getName());
           
      // build the payload
-     char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
+     char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light);
       
      // issue the request
      if (data != NULL && strlen(data) > 0) {
@@ -278,7 +279,7 @@
      //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
      
      // update the IOC ID if found
-     if (success) this->saveIOCID(light,result);
+     if (success) this->iocEndpoint()->saveLightID(light,result);
      
      // DEBUG
      if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),light->getIOCID());
@@ -317,7 +318,7 @@
      memset(payload,0,IOC_PAYLOAD_LEN+1);
           
      // build the payload
-     char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
+     char *data = this->iocEndpoint()->buildPayload(payload,IOC_PAYLOAD_LEN,light);
      
      // issue the request  
      if (data != NULL && strlen(data) > 0) {
@@ -336,113 +337,6 @@
      return success; 
  } 
  
- // build out the Payload
- char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) {
-     char tmp[TEMP_BUFFER_LEN+1];
-      
-     // construct the payload for Load/Updates
-     ResourceFactory *factory = light->getResourceFactory();
-     
-     // start the buffer
-     strcat(data,"{");
-     
-     // loop through the resources and build a JSON representation for the payload
-     for(int i=0;i<factory->numResources();++i) {
-         // get the ith resource
-         Resource *resource = factory->getResource(i);
-         if (resource != NULL) {
-             // add to the JSON payload
-             char *name = this->getMap()->endpointNameToIOCName(resource->getName());
-             char *value = resource->getValue();
-             
-             // make sure that we have a positive IOC resource match for the NSP resource 
-             if (name != NULL && strlen(name) > 0) {
-                 // Handle LOCATION a special way
-                 if (strcmp(name,"LOCATION") != 0) {
-                     // standard name,value for IOC
-                     sprintf(tmp, "\"%s\":\"%s\",",name,value); 
-                  }
-                 else {
-                     // IOC expects "Point(X,Y)" for LOCATION
-                     sprintf(tmp, "\"%s\":\"Point(%s)\",",name,value); 
-                 }   
-                 strcat(data,tmp);
-            
-                 // Handle /dev/addldata 
-                 char *dev_addldata = this->getMap()->endpointNameToIOCName("/dev/addldata");
-                 if (dev_addldata != NULL && strcmp(name,dev_addldata) == 0 && light != NULL && light->getIOCID() > 0) {
-                     char buf[IOC_IOC_ID_LEN+1]; memset(buf,0,IOC_IOC_ID_LEN+1); sprintf(buf,"%d",light->getIOCID());
-                     sprintf(tmp,"\"%s\":\"id:%s\",",name,buf);
-                     strcat(data,tmp);
-                 }
-             }
-         }
-     }
-     
-     // Special Case: STARTDATETIME
-     strcat(data,ENDPOINT_STARTTIME);
-     
-     // Special Case: ENDDATETIME
-     strcat(data,ENDPOINT_STOPTIME);
-                
-     // Special Case: NAME
-     sprintf(tmp,"\"NAME\":\"%s\",",light->getName());
-     strcat(data,tmp);
-          
-     // Special Case: TIMEZONEOFFSET
-     strcat(data,ENDPOINT_TIMEZONE);
-          
-     // close
-     strcat(data,"}");
-     
-     // DEBUG
-     //this->logger()->log("Loading Payload: %s",data);
-     
-     // return the payload
-     return data;
- }
-  
- // save the IOC ID
- void MBEDEndpoint::saveIOCID(Light *light,char *json) {
-     if (json != NULL) {          
-         //this->logger()->log("RESULT: %s",json);
-         
-         // look for "id":
-         char *check = "\"id\":";
-         char *pos1 = strstr(json,check);
-         if (pos1 != NULL) {
-             char *pos2 = strstr(pos1,","); 
-             if (pos1 != NULL && pos2 != NULL && pos2 > pos1) {
-                 pos1 += strlen(check);
-                 int length = pos2 - pos1;  
-                 char str_ioc_id[IOC_IOC_ID_LEN+1];
-                 memset(str_ioc_id,0,IOC_IOC_ID_LEN+1);
-                 strncpy(str_ioc_id,pos1,length);
-                 
-                 // DEBUG
-                 //this->logger()->log("IOC ID found: %s",str_ioc_id);
-                 
-                 // parse into int  
-                 int ioc_id = 0;             
-                 sscanf(str_ioc_id,"%d",&ioc_id);
-                          
-                 // save the IOC ID
-                 if (ioc_id > 0) light->setIOCID(ioc_id);
-             }
-             else {
-                 // cannot find the ID tag in the result JSON
-                 this->logger()->log("Cannot find the IOC ID in the JSON result");
-                 this->logger()->log("JSON: %s",json);
-             }
-         }
-         else {
-             // cannot find the ID tag in the result JSON
-             this->logger()->log("Cannot find the IOC ID in the JSON result");
-             this->logger()->log("JSON: %s",json);
-        }
-     }
- }
- 
  // close down our endpoint personality
  bool MBEDEndpoint::closeEndpointPersonality() {
  #ifdef LIGHT_PERSONALITY
@@ -501,6 +395,9 @@
  // get our error handler
  ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }
  
+ // get our IOC endpoint
+ IOCEndpoint *MBEDEndpoint::iocEndpoint() { return this->m_ioc_endpoint; }
+ 
  // main running loop
  void MBEDEndpoint::run() {
      this->logger()->log("Endpoint Main Loop");