MBED_DEMOS / Mbed 2 deprecated mbed_mqtt_endpoint_nxp

Dependencies:   C12832_lcd EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed

Revision:
48:d3663434128d
Parent:
39:bd5b2bcd2dcc
Child:
79:67accc95965a
--- a/MBEDEndpoint.cpp	Sat Mar 01 17:16:17 2014 +0000
+++ b/MBEDEndpoint.cpp	Sat Mar 01 19:23:50 2014 +0000
@@ -28,6 +28,10 @@
  // Emulated Actions we can act on
  #include "EmulatedLightDimmerAction.h"
  #include "EmulatedLightSwitchAction.h"
+ 
+ // string support
+ #include <stdlib.h>
+ #include <string.h>
    
  // shutdown endpoint reference
  extern void closedown(int code);
@@ -186,16 +190,19 @@
      memset(payload,0,IOC_PAYLOAD_LEN+1);
      
      // DEBUG
-     //this->logger()->log("Loading Light: %s...",light->getName());
+     this->logger()->log("Building Payload for Light: %s...",light->getName());
           
      // build the payload
      char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
-     
-     // DEBUG
-     this->logger()->log("Sending Light: %s to the IOC...",light->getName());
       
      // issue the request
-     success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
+     if (data != NULL && strlen(data) > 0) {
+        // DEBUG
+        this->logger()->log("Sending Light: %s to the IOC...",light->getName());
+     
+        // load
+        success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
+     }
      
      // DEBUG
      //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
@@ -205,10 +212,12 @@
      
      // DEBUG
      if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),light->getIOCID());
-     else this->logger()->log("Light: %s send FAILED",light->getName());
      
      // DEBUG
-     if (!success) this->logger()->log("Loading Light: %s FAILED",light->getName());
+     if (!success) {
+         if (light != NULL) this->logger()->log("Light: %s send FAILED",light->getName());
+         else this->logger()->log("Light: send FAILED (NULL LIGHT)");
+     }
      
      // return our status
      return success;
@@ -240,8 +249,14 @@
      // build the payload
      char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
      
-     // issue the request   
-     success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(light->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
+     // issue the request  
+     if (data != NULL && strlen(data) > 0) {
+        // DEBUG
+        this->logger()->log("Updating Light: %s at the IOC...",light->getName()); 
+        
+        // update
+        success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(light->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
+     }
      
      // DEBUG
      if (success) this->logger()->log("Update of Endpoint to IOC successful");
@@ -253,56 +268,60 @@
  
  // 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();
-           
-     // accumulator string
-     std::string str_json("{");
+     
+     // 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);
-         
-         // add to the JSON payload
-         char *name = this->getMap()->endpointNameToIOCName(resource->getName());
-         char *value = resource->getValue();
-         
-         // Handle LOCATION a special way
-         if (strcmp(name,"LOCATION") != 0) {
-             str_json += "\"" + std::string(name) + "\":\"" + std::string(value) + "\","; 
-         }
-         else {
-             // IOC expects "Point(X,Y)" for LOCATION, but in reverse from MBED
-             str_json += "\"" + std::string(name) + "\":\"Point(" + std::string(value) + ")\",";
-         }           
-         
-         // 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[10]; memset(buf,0,10); sprintf(buf,"%d",light->getIOCID());
-             str_json += "\"" + std::string(name) + "\":\"id:" + std::string(buf) + "\",";
+         if (resource != NULL) {
+             // add to the JSON payload
+             char *name = this->getMap()->endpointNameToIOCName(resource->getName());
+             char *value = resource->getValue();
+             
+             // 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
-     str_json += "\"STARTDATETIME\":\"2014-02-27T22:03:06.973Z\",";
+     strcat(data,"\"STARTDATETIME\":\"2014-02-27T22:03:06.973Z\",");
      
      // Special Case: ENDDATETIME
-      str_json += "\"ENDDATETIME\":\"2015-02-27T03:35:55.940Z\",";
-          
+     strcat(data,"\"ENDDATETIME\":\"2015-02-27T03:35:55.940Z\",");
+                
      // Special Case: NAME
-     str_json += "\"NAME\":\"" + std::string(light->getName()) + "\",";
-     
+     sprintf(tmp,"\"NAME\":\"%s\",",light->getName());
+     strcat(data,tmp);
+          
      // Special Case: TIMEZONEOFFSET
-     str_json += "\"TIMEZONEOFFSET\":\"+2:00\"";
+     strcat(data,"\"TIMEZONEOFFSET\":\"+2:00\"");
           
      // close
-     str_json += "}";
-               
-     // copy over to the buffer
-     strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length));
-          
+     strcat(data,"}");
+     
      // DEBUG
      //this->logger()->log("Loading Payload: %s",data);
      
@@ -312,33 +331,42 @@
   
  // save the IOC ID
  void MBEDEndpoint::saveIOCID(Light *light,char *json) {
-     if (json != NULL) {
-         std::string str_json(json);
-          
+     if (json != NULL) {          
+         //this->logger()->log("RESULT: %s",json);
+         
          // look for "id":
-         std::string check("\"id\":");
-         int pos1 = str_json.find(check);
-         int pos2 = str_json.find(",",pos1); 
-         if (pos1 >= 0 && pos2 >= 0 && pos2 > pos1) { 
-             pos1 += check.length();
-             int length = pos2 - pos1;  
-             std::string str_ioc_id = str_json.substr(pos1,length); 
-             
-             // DEBUG
-             //this->logger()->log("IOC ID found: %s",str_ioc_id.c_str());
-             
-             // parse into int  
-             int ioc_id = 0;             
-             sscanf(str_ioc_id.c_str(),"%d",&ioc_id);
-                      
-             // save the IOC ID
-             if (ioc_id > 0) light->setIOCID(ioc_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);
-         }
+        }
      }
  }