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:
15:e44d75d95b38
Parent:
14:0a6497a380a4
Child:
17:84ab108ed670
--- a/MBEDEndpoint.cpp	Thu Feb 27 04:09:39 2014 +0000
+++ b/MBEDEndpoint.cpp	Thu Feb 27 06:05:01 2014 +0000
@@ -41,6 +41,7 @@
      bool success = true;
      for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
      this->m_error_handler = error_handler;
+     this->m_map = new MBEDToIOCResourceMap(error_handler);
      this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
      if (success) this->logger()->turnLEDBlue();
      if (success) success = this->initializeEthernet(ethernet);
@@ -59,6 +60,7 @@
      if (success) success = this->closeTransports();
      if (success) success = this->closeEthernet();
      if (success) this->logger()->turnLEDBlue();
+     if (this->m_map != NULL) delete this->m_map;
  }
  
  // set the IOC ID
@@ -67,6 +69,9 @@
  // get the IOC ID
  int MBEDEndpoint::getIOCID() { return this->m_ioc_id; }
  
+ // get the IOC <--> MBED resource map
+ MBEDToIOCResourceMap *MBEDEndpoint::getMap() { return this->m_map; }
+ 
  // initialize the Lights
  bool MBEDEndpoint::initializeLights() {
      this->logger()->log("Initializing Lights...");
@@ -126,7 +131,7 @@
       
       if (success == true) {
         // MQTT Initialization
-        success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this));
+        success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler,this,this->getMap()));
       }
       
       if (success == true) {
@@ -168,29 +173,46 @@
  // load up all endpoints into the IOC
  bool MBEDEndpoint::loadEndpoints() {
      bool success = true;
+     this->logger()->log("Loading All Endpoints to IOC...");
      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(Light *light) { 
+ bool MBEDEndpoint::loadEndpoint(Light *light) {      
      bool success = false;
      char result[IOC_RESULT_LEN+1];
      char payload[IOC_PAYLOAD_LEN+1];
      
      // initialize
-     memset(result,0,IOC_RESULT_LEN+1);
-     memset(payload,0,IOC_PAYLOAD_LEN+1);
+     //memset(result,0,IOC_RESULT_LEN+1);
+     //memset(payload,0,IOC_PAYLOAD_LEN+1);
+     
+     // DEBUG
+     this->logger()->log("Loading Light: %s...",light->getName());
+
+return true;
           
      // build the payload
      char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
      
+     // DEBUG
+     this->logger()->log("Sending Light: %s to the IOC...",light->getName());
+ 
+ return true;
+     
      // issue the request
      success = this->m_transports[LOAD_TRANSPORT]->loadEndpoint((char *)payload,strlen(payload),(char *)result,IOC_RESULT_LEN);
      
+     // DEBUG
+     this->logger()->log("Saving IOC ID for Light: %s...",light->getName());
+     
      // update the IOC ID if found
      if (success) this->saveIOCID(result);
      
+     // DEBUG
+     this->logger()->log("Light: %s IOC ID=%d...",light->getName(),this->getIOCID());
+     
      // return our status
      return success;
  }
@@ -215,8 +237,8 @@
      char payload[IOC_PAYLOAD_LEN+1];
      
      // initialize
-     memset(result,0,IOC_RESULT_LEN+1);
-     memset(payload,0,IOC_PAYLOAD_LEN+1);
+     //memset(result,0,IOC_RESULT_LEN+1);
+     //memset(payload,0,IOC_PAYLOAD_LEN+1);
           
      // build the payload
      char *data = this->buildIOCPayload(payload,IOC_PAYLOAD_LEN,light);
@@ -232,8 +254,7 @@
  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;
      
@@ -243,18 +264,15 @@
          Resource *resource = factory->getResource(i);
          
          // add to the JSON payload
-         json[map->endpointNameToIOCName(resource->getName())] = resource->getValue();
+         json[this->getMap()->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;
-     
+     //strncpy(data,str_json.c_str(),this->min(str_json.length(),data_length));
+          
      // DEBUG
      this->logger()->log("Loading Payload: %s",data);
      
@@ -281,6 +299,8 @@
  bool MBEDEndpoint::closeLights() {
     bool success = true;
     this->logger()->log("Closing down Lights...");
+    for(int i=0;i<NUM_LIGHTS;++i)
+        if (this->m_lights[i] != NULL) delete this->m_lights[i];
     return success;
  }