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:
23:793b2898522e
Parent:
22:f1002e5993c5
Child:
24:7b3a5c927a53
--- a/MBEDEndpoint.cpp	Thu Feb 27 23:33:24 2014 +0000
+++ b/MBEDEndpoint.cpp	Fri Feb 28 02:52:04 2014 +0000
@@ -186,7 +186,7 @@
      memset(payload,0,IOC_PAYLOAD_LEN+1);
      
      // DEBUG
-     this->logger()->log("Loading Light: %s...",light->getName());
+     //this->logger()->log("Loading Light: %s...",light->getName());
           
      // build the payload
      char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
@@ -198,13 +198,14 @@
      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());
+     //if (success) this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
      
      // update the IOC ID if found
      if (success) this->saveIOCID(result);
      
      // DEBUG
-     if (success) this->logger()->log("Light: %s IOC ID=%d...",light->getName(),this->getIOCID());
+     if (success) this->logger()->log("Light: %s IOC ID=%d sent successfully",light->getName(),this->getIOCID());
+     else this->logger()->log("Light: %s IOC ID=%d send FAILED",light->getName(),this->getIOCID());
      
      // DEBUG
      if (!success) this->logger()->log("Loading Light: %s FAILED",light->getName());
@@ -241,6 +242,10 @@
      
      // issue the request   
      success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(this->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
+     
+     // DEBUG
+     if (success) this->logger()->log("Update of Endpoint to IOC successful");
+     else this->logger()->log("Update of Endpoint to IOC FAILED");
           
      // return our status
      return success; 
@@ -269,10 +274,7 @@
          }
          else {
              // IOC expects "Point(X,Y)" for LOCATION, but in reverse from MBED
-             std::string coords(value);
-             std::vector<std::string> x = this->split(coords, ',');
-             std::string flipped_coords = x[1] + " " + x[0];
-             str_json += "\"" + std::string(name) + "\":\"Point(" + flipped_coords + ")\",";
+             str_json += "\"" + std::string(name) + "\":\"Point(" + std::string(value) + ")\",";
          }           
      }
      
@@ -295,7 +297,7 @@
      strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length));
           
      // DEBUG
-     this->logger()->log("Loading Payload: %s",data);
+     //this->logger()->log("Loading Payload: %s",data);
      
      // return the payload
      return data;
@@ -307,39 +309,32 @@
          std::string str_json(json);
           
          // look for "id":
-         unsigned pos1 = str_json.find("\"id\":");
-         unsigned pos2 = str_json.find(",",pos1);    
-         std::string str_ioc_id = str_json.substr(pos1+5,pos2); 
-         
-         // 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) this->setIOCID(ioc_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) this->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);
+         }
      }
  }
  
- // string splitting
- std::vector<std::string> &MBEDEndpoint::split(const std::string &s, char delim, std::vector<std::string> &elems) {
-    std::stringstream ss(s);
-    std::string item;
-    while (std::getline(ss, item, delim)) {
-        elems.push_back(item);
-    }
-    return elems;
- }
-
- // string splitting
- std::vector<std::string> MBEDEndpoint::split(const std::string &s, char delim) {
-    std::vector<std::string> elems;
-    this->split(s, delim, elems);
-    return elems;
- }
- 
  // close down the Lights
  bool MBEDEndpoint::closeLights() {
     bool success = true;