MBED_DEMOS / Mbed 2 deprecated mbed_mqtt_endpoint_ublox_ethernet

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

Revision:
15:e44d75d95b38
Parent:
14:0a6497a380a4
Child:
22:f1002e5993c5
diff -r 0a6497a380a4 -r e44d75d95b38 MQTTTransport.cpp
--- a/MQTTTransport.cpp	Thu Feb 27 04:09:39 2014 +0000
+++ b/MQTTTransport.cpp	Thu Feb 27 06:05:01 2014 +0000
@@ -28,28 +28,27 @@
  #include "splitstring.h"
  
  // our transmitt instance 
- MQTTTransport *instance =  NULL;
+ MQTTTransport *_mqtt_instance =  NULL;
 
  // MQTT callback to handle received messages
  void _mqtt_message_handler(char *topic,char *payload,unsigned int length) {
-    if (instance != NULL) instance->processMessage(instance->getEndpointNameFromTopic(topic),payload,length);
+    if (_mqtt_instance != NULL) _mqtt_instance->processMessage(_mqtt_instance->getEndpointNameFromTopic(topic),payload,length);
  }
  
  // our MQTT client endpoint
  PubSubClient _mqtt(MQTT_HOSTNAME,MQTT_HOSTPORT,_mqtt_message_handler);
   
  // default constructor
- MQTTTransport::MQTTTransport(ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) {
+ MQTTTransport::MQTTTransport(ErrorHandler *error_handler,void *endpoint,MBEDToIOCResourceMap *map) : Transport(error_handler,endpoint) {
      this->m_mqtt = NULL;
-     instance = this;
-     this->m_map = new MBEDToIOCResourceMap(error_handler);
+     _mqtt_instance = this;
+     this->m_map = map;
      this->initTopic();
  }
  
  // default destructor
  MQTTTransport::~MQTTTransport() {
      this->disconnect();
-     if (this->m_mqtt != NULL) delete this->m_mqtt;
  }
  
  // init our topic
@@ -63,6 +62,9 @@
  // get our topic
  char *MQTTTransport::getTopic() { return this->m_topic; }
  
+ // get the IOC <--> MBED resource map
+ MBEDToIOCResourceMap *MQTTTransport::getMap() { return this->m_map; }
+ 
  // pull the endpoint name from the MQTT topic
  char *MQTTTransport::getEndpointNameFromTopic(char *topic) {
      memset(this->m_endpoint_name,0,LIGHT_NAME_LEN+1);
@@ -101,29 +103,36 @@
          //if (data.size() > 3) message_opt   = (char *)data[3].c_str();
                
          // DEBUG
+         //this->logger()->log("Raw Payload: %s",payload);
          //this->logger()->log("Type: %s Name: %s Verb: %s Value: %s",message_type,message_name,message_verb,message_value);
                           
          // load endpoints
-         if (message_type != NULL && strcmp(message_type,IOC_ENDPOINT_VERB) == 0) {
-             if (message_name != NULL && strcmp(message_name,IOC_ENDPOINT_ALL_VERB) == 0) {
-                 if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_LOAD_ALL_VERB) == 0) {
-                     // load up our endpoints
-                     endpoint->loadEndpoints();
-                 }
-                 if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {
-                     // update our endpoints
-                     endpoint->updateEndpoints();
+         if (message_type != NULL && strcmp(message_type,IOC_ENDPOINT_VERB) == 0) {                 // Endpoint
+             if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_LOAD_ALL_VERB) == 0) {     // load
+                 if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) {   // all
+                    // load up our endpoints
+                    this->logger()->log("would load all endpoints...");
+                    endpoint->loadEndpoints();
                  }
              }
-             else {
-                 // destined for our lights?
-                 int index = -1;
-                 if (message_name != NULL) {
-                     index = endpoint->indexOfLight((char *)message_name);
-                     if (index >= 0) {
-                         if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {
-                            // update our endpoint
-                            endpoint->updateEndpoints(index);
+             
+             else if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {  // update
+                 if (message_value != NULL && strcmp(message_value,IOC_ENDPOINT_ALL_VERB) == 0) {       // all
+                     // update our endpoints
+                     this->logger()->log("would update all endpoints...");
+                     //endpoint->updateEndpoints();
+                 }
+                 else {
+                     // update just our endpoint
+                     int index = -1;
+                     if (message_name != NULL) {
+                         index = endpoint->indexOfLight((char *)message_name);
+                         if (index >= 0) {
+                             if (message_verb != NULL && strcmp(message_verb,IOC_REQUEST_UPDATE_ALL_VERB) == 0) {
+                                // update our endpoint
+                                this->logger()->log("would update just our endpoint...");
+                                //endpoint->updateEndpoints(index);
+                             }
                          }
                      }
                  }
@@ -211,7 +220,7 @@
      }
  }
  
- char *MQTTTransport::mapIOCResourceToEndpointResource(char *ioc_name) { return this->m_map->iocNameToEndpointName(ioc_name); }
+ char *MQTTTransport::mapIOCResourceToEndpointResource(char *ioc_name) { return this->getMap()->iocNameToEndpointName(ioc_name); }
  
  char *MQTTTransport::makeID(char *id_template,char *buffer) {
      srand(time(0));