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:
13:25448d92c205
Parent:
12:952dce085876
Child:
14:0a6497a380a4
--- a/MBEDEndpoint.cpp	Thu Feb 27 00:23:30 2014 +0000
+++ b/MBEDEndpoint.cpp	Thu Feb 27 04:05:41 2014 +0000
@@ -29,6 +29,9 @@
  #include "EmulatedLightDimmerAction.h"
  #include "EmulatedLightSwitchAction.h"
  
+ // JSON serialization support
+ #include "MbedJSONValue.h"
+ 
  // shutdown endpoint reference
  extern void closedown(int code);
   
@@ -162,8 +165,15 @@
      return success;
  }
  
+ // load up all endpoints into the IOC
+ bool MBEDEndpoint::loadEndpoints() {
+     bool success = true;
+     for(int i=0;i<NUM_LIGHTS && success;++i) success = this->loadEndpoint(this->m_lights[i]);
+     return success;
+ }
+ 
  // load up our endpoint to the IOC
- bool MBEDEndpoint::loadEndpoint() { 
+ bool MBEDEndpoint::loadEndpoint(Light *light) { 
      bool success = false;
      char result[IOC_RESULT_LEN+1];
      char payload[IOC_PAYLOAD_LEN+1];
@@ -173,7 +183,7 @@
      memset(payload,0,IOC_PAYLOAD_LEN+1);
           
      // build the payload
-     char *data = this->buildIOCPayload(data,IOC_PAYLOAD_LEN);
+     char *data = this->buildIOCPayload(data,IOC_PAYLOAD_LEN,light);
      
      // issue the request
      success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
@@ -185,8 +195,21 @@
      return success;
  }
  
+ // update all endpoints to the IOC
+ bool MBEDEndpoint::updateEndpoints() {
+     bool success = true;
+     for(int i=0;i<NUM_LIGHTS && success;++i) success = this->updateEndpoints(i);
+     return success;
+ }
+ 
+ // update all endpoints to the IOC
+ bool MBEDEndpoint::updateEndpoints(int index) {
+     if (index >= 0 && index < NUM_LIGHTS) return this->updateEndpoint(this->m_lights[index]);
+     return false; 
+ }
+ 
  // update our endpoint with the IOC
- bool MBEDEndpoint::updateEndpoint() { 
+ bool MBEDEndpoint::updateEndpoint(Light *light) { 
      bool success = false;
      char result[IOC_RESULT_LEN+1];
      char payload[IOC_PAYLOAD_LEN+1];
@@ -196,7 +219,7 @@
      memset(payload,0,IOC_PAYLOAD_LEN+1);
           
      // build the payload
-     char *data = this->buildIOCPayload(data,IOC_PAYLOAD_LEN);
+     char *data = this->buildIOCPayload(data,IOC_PAYLOAD_LEN,light);
      
      // issue the request   
      success = this->m_transports[LOAD_TRANSPORT]->updateEndpoint(this->getIOCID(),(char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
@@ -206,18 +229,49 @@
  } 
  
  // build out the Payload
- char *MBEDEndpoint::buildIOCPayload(char *data,int data_length) {
+ char *MBEDEndpoint::buildIOCPayload(char *data,int data_length,Light *light) {
      // construct the payload for Load/Updates
+     ResourceFactory *factory = light->getResourceFactory();
+     MBEDToIOCResourceMap *map = new MBEDToIOCResourceMap(this->logger());
+     
+     // JSON factory
+     MbedJSONValue json;
+     
+     // 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
+         json[map->endpointNameToIOCName(resource->getName())] = resource->getValue();
+     }
+     
+     // now convert to JSON
+     std::string str_json = json.serialize();
+     
+     // copy over to the buffer
+     strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length));
+     
+     // clean up
+     if (map != NULL) delete map;
+     
+     // DEBUG
+     this->logger()->log("Loading Payload: %s",data);
      
      // return the payload
      return data;
  }
   
  // save the IOC ID
- void MBEDEndpoint::saveIOCID(char *data) {
-     int ioc_id = -1;
+ void MBEDEndpoint::saveIOCID(char *json) {     
+     // JSON factory
+     MbedJSONValue parser;
      
-     // parse the IOC ID from the data result
+     // 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);
@@ -261,6 +315,12 @@
      return true;
  }
  
+ // min function
+ int MBEDEndpoint::min(int value1,int value2) {
+    if (value1 < value2) return value1;
+    return value2;
+ }
+ 
  // get our error handler
  ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }