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:
0:ae2a45502448
Child:
2:90a84a216c58
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MBEDEndpoint.cpp	Tue Feb 25 16:56:31 2014 +0000
@@ -0,0 +1,219 @@
+/* Copyright C2013 Doug Anson, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files the "Software", to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+ #include "MQTTTransport.h"
+ #include "MBEDEndpoint.h"
+ 
+ // MBED Light support
+ #include "MBEDLight.h"
+ 
+ // Emulated Actions we can act on
+ #include "EmulatedLightDimmerAction.h"
+ #include "EmulatedLightSwitchAction.h"
+  
+ // default constructor
+ MBEDEndpoint::MBEDEndpoint(ErrorHandler *error_handler,EthernetInterface *ethernet) {
+     bool success = true;
+     for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = NULL;
+     this->m_error_handler = error_handler;
+     this->logger()->log(ENDPOINT_VERSION_ANNOUNCE);
+     if (success) this->logger()->turnLEDBlue();
+     if (success) success = this->initializeEthernet(ethernet);
+     if (success) this->logger()->turnLEDYellow();
+     if (success) success = this->initializeTransports();
+     if (success) success = this->initializeLights();
+     if (success) this->logger()->turnLEDGreen();
+     if (!success) this->logger()->turnLEDRed();
+ }
+ 
+ // default destructor
+ MBEDEndpoint::~MBEDEndpoint() {
+     bool success = true;
+     if (success) this->logger()->turnLEDYellow();
+     if (success) success = this->closeLights();
+     if (success) success = this->closeTransports();
+     if (success) success = this->closeEthernet();
+     if (success) this->logger()->turnLEDBlue();
+ }
+ 
+ // initialize the Lights
+ bool MBEDEndpoint::initializeLights() {
+     this->logger()->log("Initializing Lights...");
+     for(int i=0;i<NUM_LIGHTS;++i) {
+         this->m_lights[i] = new MBEDLight(this->logger(),this->m_transports,i+1,this);
+         this->m_lights[i]->setDimmerAction(new EmulatedLightDimmerAction(this->logger(),this->m_lights[i]));
+         this->m_lights[i]->setSwitchAction(new EmulatedLightSwitchAction(this->logger(),this->m_lights[i]));
+     }
+     return true;
+ }
+ 
+ // does the input name match any of our light resources?
+ int MBEDEndpoint::indexOfLight(char *name) {
+     bool found = false;
+     int index = -1;
+     
+     for(int i=0;i<NUM_LIGHTS && !found;++i) {
+         if (strcmp(this->m_lights[i]->getName(),name) == 0) {
+             found = true;
+             index = i;
+         }
+     }
+     
+     return index;
+ }
+ 
+ // get a specific resources
+ ResourceFactory *MBEDEndpoint::getResources(int index) {
+     if (index >= 0 && index < NUM_LIGHTS) return this->m_lights[index]->resources();
+     return NULL;
+ }
+ 
+ // initialize our ResourceFactory
+ ResourceFactory *MBEDEndpoint::initResourceFactory() {
+     ResourceFactory *factory = new ResourceFactory(this->logger());
+     return factory;
+ }
+ 
+ // initialize a specific transport
+ bool MBEDEndpoint::initializeTransport(int index,char *key,Transport *transport) {
+     bool success = false;
+     if (this->m_transports[index] == NULL) {
+          this->logger()->log("Initializing %s Transport...", key);
+          this->m_transports[index] = transport;
+          if (this->m_transports[index] != NULL) success = this->m_transports[index]->connect();
+      }
+      else {
+          this->logger()->log("%s already connected (OK)...", key);
+          success = true;
+      }
+      return success;
+ }
+ 
+ // initialize our transports
+ bool MBEDEndpoint::initializeTransports() {
+      bool success = true;
+      
+      if (success == true) {
+        // MQTT Initialization
+        success = this->initializeTransport(MQTT_TRANSPORT,"MQTT",new MQTTTransport(this->m_error_handler));
+      }
+      
+      if (success == true) {
+          // HTTP Initialization
+          success = this->initializeTransport(HTTP_TRANSPORT,"HTTP",new HTTPTransport(this->m_error_handler));
+      }
+      return success;
+ }
+ 
+ // initialize our Ethernet 
+ bool MBEDEndpoint::initializeEthernet(EthernetInterface *ethernet) {
+     bool success = false;
+     this->m_ethernet = ethernet;
+     if (this->m_ethernet != NULL) {
+         this->logger()->log("Initializing Ethernet...");
+         
+         // connect up ethernet
+         this->m_ethernet->init();
+         this->m_ethernet->connect();
+         
+         // display our IP address
+         char *ipaddr = this->m_ethernet->getIPAddress();
+         if (ipaddr != NULL && strlen(ipaddr) > 0) {
+            this->logger()->log("IPAddress: %s",this->m_ethernet->getIPAddress());
+            success = true;
+         }
+         else {
+            this->logger()->log("Ethernet Not Connected...");
+            success = false;
+         }
+     }
+     else {
+         this->logger()->log("No Ethernet instance found");
+         success = false;
+     }
+     return success;
+ }
+ 
+ // load up our endpoint to the IOC
+ void MBEDEndpoint::loadEndpoint() {
+ }
+ 
+ // update our endpoint with the IOC
+ void MBEDEndpoint::updateEndpoint() {
+ }
+        
+ 
+ // close down the Lights
+ bool MBEDEndpoint::closeLights() {
+    bool success = true;
+    this->logger()->log("Closing down Lights...");
+    return success;
+ }
+ 
+ // close a given transport
+ bool MBEDEndpoint::closeTransport(int index,char *key) {
+    this->logger()->log("Closing down %s Transport...", key);
+    if (this->m_transports[index] != NULL) delete this->m_transports[index];
+    return true;
+ }
+ 
+ // close down our transports
+ bool MBEDEndpoint::closeTransports() {
+     bool success = true;
+     
+     if (success) {
+         // close MQTT
+         success = this->closeTransport(MQTT_TRANSPORT,"MQTT");
+     }
+     
+     if (success) {
+         // close HTTP
+         success = this->closeTransport(HTTP_TRANSPORT,"HTTP");
+     }
+     
+     return success;
+ }
+ 
+ // close down our Ethernet 
+ bool MBEDEndpoint::closeEthernet() {
+     this->logger()->log("Closing down Ethernet...");
+     if (this->m_ethernet != NULL) this->m_ethernet->disconnect();
+     return true;
+ }
+ 
+ // get our error handler
+ ErrorHandler *MBEDEndpoint::logger() { return this->m_error_handler; }
+ 
+ // main running loop
+ void MBEDEndpoint::run() {
+     this->logger()->log("Endpoint Main Loop");
+     while(true) {
+         // sleep a bit
+         //this->logger()->log("Sleeping for a bit...");
+         wait_ms(MAIN_LOOP_SLEEP);
+         
+         // check for events 
+         //this->logger()->log("Processing Events...");
+         for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i]->checkAndProcess();
+         
+         // check for exit
+         //this->logger()->log("Checking for exit...");
+         this->logger()->checkForExit();
+     }
+}
+ 
\ No newline at end of file