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:
22:f1002e5993c5
Parent:
21:d1ce325d1d32
Child:
23:793b2898522e
--- a/MBEDEndpoint.cpp	Thu Feb 27 22:07:45 2014 +0000
+++ b/MBEDEndpoint.cpp	Thu Feb 27 23:33:24 2014 +0000
@@ -28,11 +28,7 @@
  // Emulated Actions we can act on
  #include "EmulatedLightDimmerAction.h"
  #include "EmulatedLightSwitchAction.h"
- 
- // JSON serialization support
- #include "MbedJSONValue.h"
- #include <string>
- 
+   
  // shutdown endpoint reference
  extern void closedown(int code);
   
@@ -272,8 +268,11 @@
              str_json += "\"" + std::string(name) + "\":\"" + std::string(value) + "\","; 
          }
          else {
-             // IOC expects "Point(X,Y)" for LOCATION
-             str_json += "\"" + std::string(name) + "\":\"Point(" + std::string(value) + ")\",";
+             // 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 + ")\",";
          }           
      }
      
@@ -303,20 +302,42 @@
  }
   
  // save the IOC ID
- void MBEDEndpoint::saveIOCID(char *json) {     
-     // JSON factory
-     MbedJSONValue parser;
-     
-     this->logger()->log("Load Result: %s",json);
-     
-     // parse the result 
-     parse(parser,json);
-     
-     // look for the ID element specifically
-     int ioc_id = parser[IOC_REPONSE_ID_KEY].get<int>();
-     
-     // save the IOC ID
-     if (ioc_id > 0) this->setIOCID(ioc_id);
+ void MBEDEndpoint::saveIOCID(char *json) {
+     if (json != NULL) {
+         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);
+     }
+ }
+ 
+ // 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