Doug Anson / endpoint_mqtt

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EmulatedResourceFactory.cpp Source File

EmulatedResourceFactory.cpp

00001 /* Copyright C2013 Doug Anson, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files the "Software", to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018  
00019  // class definition
00020  #include "EmulatedResourceFactory.h"
00021  
00022  // MBEDEndpoint instance support
00023  #include "MBEDEndpoint.h"
00024   
00025  // default constructor
00026  EmulatedResourceFactory::EmulatedResourceFactory(Logger *logger,void *endpoint) : ResourceFactory(logger,endpoint) {  
00027  }
00028  
00029  // default destructor
00030  EmulatedResourceFactory::~EmulatedResourceFactory() {
00031  }
00032  
00033  // resource creators for the EmulatedResourceFactory...
00034  void EmulatedResourceFactory::createResource(char *name,char *value) { ResourceFactory::createResource(name,value); }
00035  void EmulatedResourceFactory::createResource(char *ep_name,char *name,char *value,void *cb) { ResourceFactory::createResource(ep_name,name,value,cb); }
00036  void EmulatedResourceFactory::createResource(char *name,char *value,void *io,void *notused) {
00037      MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
00038      char *ep_name = NULL; if (endpoint != NULL) ep_name = endpoint->getEndpointName();
00039      this->m_list[this->m_count] = new Resource(this->logger(),ep_name,name,value,NULL);
00040      if (io != NULL && this->m_list[this->m_count] != NULL) {
00041         resourceInitializer initializer = (resourceInitializer)io;
00042         (initializer)(this->m_list[this->m_count]);
00043      }
00044      if (this->m_list[this->m_count] != NULL) ++this->m_count;
00045  }
00046   
00047  // set a resource value (AND trigger the Emulated actions if registered)
00048  bool EmulatedResourceFactory::setResourceValue(char *name, char *value) {
00049      // set the resource value
00050      bool success = ResourceFactory::setResourceValue(name,value);
00051      if (success) {
00052          // invoke an action if registered
00053          EmulatedCallbackPointer cb = (EmulatedCallbackPointer)this->getCallbackPointer(name);
00054          if (cb != NULL) {
00055              // invoke the callback
00056              this->logger()->log("Invoking Action...");
00057              cb();
00058          }
00059          
00060          // additionally update the IOC with a REST call to update its record of us (assuming 1 personality/endpoint!!)
00061          MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
00062          endpoint->updatePersonality(0);
00063      }
00064      return success;
00065  }