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:
3:3db076b9d380
Parent:
2:90a84a216c58
Child:
4:11f00b499106
diff -r 90a84a216c58 -r 3db076b9d380 MQTTTransport.cpp
--- a/MQTTTransport.cpp	Tue Feb 25 18:09:29 2014 +0000
+++ b/MQTTTransport.cpp	Tue Feb 25 22:11:11 2014 +0000
@@ -49,94 +49,124 @@
  
  // process a MQTT Message
  void MQTTTransport::processMessage(char *payload, unsigned int len) {
-     string message_type;
-     string message_name;
-     string message_verb;
-     string message_value;
-     string message_opt;
+     char *message_type = NULL;
+     char *message_name = NULL;
+     char *message_verb = NULL;
+     char *message_value = NULL;
+     char *message_opt = NULL;
+     char *endpoint_name = NULL;
+     splitstring *str_payload = NULL;
      MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
 
      // DEBUG
      this->logger()->log("MQTT Message: %s length=%d",payload,len);
      
      // split the string by the delimiter
-     splitstring str_payload(payload);
-     vector<string> data = str_payload.split(':');
+     str_payload = new splitstring(payload);
+     vector<string> data = str_payload->split(':');
      
      // format of the MQTT message:   message_type:name:verb|Parameter_X:value|keyword:optional_data
      /*
-     if (data.size() > 0) message_type = data[0];
-     if (data.size() > 1) message_name = data[1];
-     if (data.size() > 2) message_verb = data[2];
-     if (data.size() > 3) message_value = data[3];
-     if (data.size() > 4) message_opt = data[4];
+     if (data.size() > 0) message_type = data[0].c_str();
+     if (data.size() > 1) message_name = data[1].c_str();
+     if (data.size() > 2) message_verb = data[2].c_str();
+     if (data.size() > 3) message_value = data[3].c_str();
+     if (data.size() > 4) message_opt = data[4].c_str();
      */
      
+     // get our endpoint name
+     endpoint_name = endpoint->getEndpointName();
+     
      // XXX FIXUP until MQTT messages have endpoint_names in them
-     string tmp_name(endpoint->getEndpointName());
-     if (data.size() > 0) message_type = data[0];
-     if (data.size() > 1) message_name = tmp_name;
-     if (data.size() > 2) message_verb = data[2];
-     if (data.size() > 3) message_value = data[3];
-     if (data.size() > 4) message_opt = data[4];
-     
+     if (data.size() > 0) message_type  = (char *)data[0].c_str();
+     message_name = endpoint->getEndpointName();               // if (data.size() > 1) message_name = (char *)data[1].c_str();
+     if (data.size() > 1) message_verb  = (char *)data[1].c_str();
+     if (data.size() > 2) message_value = (char *)data[2].c_str();
+     if (data.size() > 3) message_opt   = (char *)data[3].c_str();
+               
      // DEBUG
-     this->logger()->log("Type: %s Name: %s Verb: %s Value: %s Opt: %s",message_type.c_str(),message_name.c_str(),message_verb.c_str(),message_value.c_str(),message_opt.c_str());
+     this->logger()->log("Type: %s Name: %s Verb: %s Value: %s",message_type,message_name,message_verb,message_value);
      
-     // load endpoints
-     if (strcmp(message_type.c_str(),"Endpoint") == 0) {
-         if (strcmp(message_name.c_str(),"all") == 0) {
-             if (strcmp(message_verb.c_str(),"load") == 0) {
-                 // load up our endpoint
-                 endpoint->loadEndpoint();
-             }
-             if (strcmp(message_verb.c_str(),"Update") == 0) {
-                 // update our endpoint
-                 endpoint->updateEndpoint();
-             }
-         }
-         else {
-             // destined for our lights?
-             int index = endpoint->indexOfLight((char *)message_name.c_str());
-             if (index >= 0) {
-                 if (strcmp(message_verb.c_str(),"Update") == 0) {
+     // only respond if its for our node
+     if (false && strcmp(endpoint_name,message_name) == 0) {
+         // DEBUG
+         this->logger()->log("Message: %s bound for %s... processing...",payload,endpoint_name);
+         
+         // load endpoints
+         if (message_type != NULL && strcmp(message_type,"Endpoint") == 0) {
+             if (message_name != NULL && strcmp(message_name,"all") == 0) {
+                 if (message_verb != NULL && strcmp(message_verb,"load") == 0) {
+                     // load up our endpoint
+                     endpoint->loadEndpoint();
+                 }
+                 if (message_verb != NULL && strcmp(message_verb,"Update") == 0) {
                      // update our endpoint
                      endpoint->updateEndpoint();
                  }
              }
+             else {
+                 // destined for our lights?
+                 int index = -1;
+                 if (message_name != NULL) {
+                     endpoint->indexOfLight((char *)message_name);
+                     if (index >= 0) {
+                         if (message_verb != NULL && strcmp(message_verb,"Update") == 0) {
+                            // update our endpoint
+                            endpoint->updateEndpoint();
+                         }
+                     }
+                 }
+             }
          }
-     }
-     
-     // change a resource value
-     if (strcmp(message_type.c_str(),"Change") == 0) {
-         // destined for our lights?
-         int index = endpoint->indexOfLight((char *)message_name.c_str());
-         if (index >= 0) {
-             // map the parameter to one of ours
-             char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb.c_str());
-             if (mapped_resource != NULL) {
-                 ResourceFactory *factory = endpoint->getResources(index);
-                 factory->setResourceValue((char *)message_name.c_str(),(char *)message_value.c_str());
+         
+         // change a resource value
+         if (message_type != NULL && strcmp(message_type,"Change") == 0) {
+             if (message_name != NULL) {
+                 // destined for our lights?
+                 int index = endpoint->indexOfLight((char *)message_name);
+                 if (index >= 0) {
+                     if (message_verb != NULL) {
+                         // map the parameter to one of ours
+                         char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb);
+                         if (mapped_resource != NULL) {
+                             if (message_value != NULL) {
+                                 ResourceFactory *factory = endpoint->getResources(index);
+                                 factory->setResourceValue(message_name,message_value);
+                             }
+                         }
+                     }
+                 }
+             }
+         }
+         
+         // get a resource value
+         if (message_type != NULL && strcmp(message_type,"Get") == 0) {
+             if (message_name != NULL) {
+                 // destined for our lights?
+                 int index = endpoint->indexOfLight((char *)message_name);
+                 if (index >= 0) {
+                     if (message_verb != NULL) {
+                         // map the parameter to one of ours
+                         char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb);
+                         if (mapped_resource != NULL) {
+                             ResourceFactory *factory = endpoint->getResources(index);
+                             char *resource_value = factory->getResourceValue((char *)message_name);
+                             
+                             // end the resource value back over MQTT
+                             this->sendResourceValue(message_name,message_verb,resource_value);
+                         }
+                     }
+                 }
              }
          }
      }
+     else {
+         // message not bound for our node
+         this->logger()->log("MQTT Message: %s not for us: %s... ignoring...",payload,endpoint_name);
+     }
      
-     // get a resource value
-     if (strcmp(message_type.c_str(),"Get") == 0) {
-         // destined for our lights?
-         int index = endpoint->indexOfLight((char *)message_name.c_str());
-         if (index >= 0) {
-             // map the parameter to one of ours
-             char *mapped_resource = this->mapEndpointResourceToIOCResource((char *)message_verb.c_str());
-             if (mapped_resource != NULL) {
-                 ResourceFactory *factory = endpoint->getResources(index);
-                 char *resource_value = factory->getResourceValue((char *)message_name.c_str());
-                 
-                 // end the resource value back over MQTT
-                 this->sendResourceValue((char *)message_name.c_str(),(char *)message_verb.c_str(),resource_value);
-             }
-         }
-     }
+     // clean up
+     if (str_payload != NULL) delete str_payload;
  }
  
  void MQTTTransport::sendResourceValue(char *endpoint_name,char *parameter_name,char *value) {