Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

Committer:
ansond
Date:
Fri Sep 26 05:16:07 2014 +0000
Revision:
192:54b758a8eaaa
Parent:
135:7f3f963cd159
updates to new logger name

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 1:5d332fa199ce 1 /* Copyright C2013 Doug Anson, MIT License
ansond 1:5d332fa199ce 2 *
ansond 1:5d332fa199ce 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 1:5d332fa199ce 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 1:5d332fa199ce 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 1:5d332fa199ce 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 1:5d332fa199ce 7 * furnished to do so, subject to the following conditions:
ansond 1:5d332fa199ce 8 *
ansond 1:5d332fa199ce 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 1:5d332fa199ce 10 * substantial portions of the Software.
ansond 1:5d332fa199ce 11 *
ansond 1:5d332fa199ce 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 1:5d332fa199ce 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 1:5d332fa199ce 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 1:5d332fa199ce 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 1:5d332fa199ce 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 1:5d332fa199ce 17 */
ansond 1:5d332fa199ce 18
ansond 1:5d332fa199ce 19 #include "ResourceFactory.h"
ansond 40:eae89a487d86 20 #include "MBEDEndpoint.h"
ansond 1:5d332fa199ce 21
ansond 1:5d332fa199ce 22 // default constructor
ansond 192:54b758a8eaaa 23 ResourceFactory::ResourceFactory(Logger *logger,void *endpoint) : BaseClass(logger,endpoint) {
ansond 26:791d22d43cb4 24 this->m_count = 0;
ansond 26:791d22d43cb4 25 for(int i=0;i<NUM_RESOURCES;++i) this->m_list[i] = NULL;
ansond 1:5d332fa199ce 26 }
ansond 1:5d332fa199ce 27
ansond 1:5d332fa199ce 28 // default destructor
ansond 1:5d332fa199ce 29 ResourceFactory::~ResourceFactory() {
ansond 26:791d22d43cb4 30 for(int i=0;i<NUM_RESOURCES;++i) if (this->m_list[i] != NULL) delete this->m_list[i];
ansond 1:5d332fa199ce 31 }
ansond 1:5d332fa199ce 32
ansond 25:2a001b4f7024 33 // get our number of resources
ansond 26:791d22d43cb4 34 int ResourceFactory::numResources() { return this->m_count; }
ansond 25:2a001b4f7024 35
ansond 25:2a001b4f7024 36 // get the ith resource
ansond 47:fa96ddc36f04 37 Resource *ResourceFactory::getResource(int index) { if (index >= 0 && index < NUM_RESOURCES) return this->m_list[index]; return NULL;};
ansond 135:7f3f963cd159 38
ansond 1:5d332fa199ce 39 // create resources (base class does nothing)
ansond 4:3176f927618d 40 void ResourceFactory::createResources(char *endpoint_name) { ; }
ansond 6:9a4085eeac52 41
ansond 6:9a4085eeac52 42 // create a resource value
ansond 40:eae89a487d86 43 void ResourceFactory::createResource(char *name,char *value) {
ansond 135:7f3f963cd159 44 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 42:ff05e71c4cb5 45 char *ep_name = NULL; if (endpoint != NULL) ep_name = endpoint->getEndpointName();
ansond 42:ff05e71c4cb5 46 this->createResource(ep_name,name,value,NULL);
ansond 40:eae89a487d86 47 }
ansond 40:eae89a487d86 48
ansond 40:eae89a487d86 49 void ResourceFactory::createResource(char *endpoint_name,char *name,char *value,void *cb) {
ansond 40:eae89a487d86 50 this->m_list[this->m_count] = new Resource(this->logger(),endpoint_name,name,value,cb);
ansond 26:791d22d43cb4 51 ++this->m_count;
ansond 6:9a4085eeac52 52 }
ansond 6:9a4085eeac52 53
ansond 6:9a4085eeac52 54 // get a resource value (null in base class)
ansond 6:9a4085eeac52 55 char *ResourceFactory::getResourceValue(char *name) {
ansond 6:9a4085eeac52 56 char *value = NULL;
ansond 6:9a4085eeac52 57 bool found = false;
ansond 6:9a4085eeac52 58
ansond 6:9a4085eeac52 59 // loop through the resource list
ansond 47:fa96ddc36f04 60 for(int i=0;i<this->m_count && !found && name != NULL;++i) {
ansond 26:791d22d43cb4 61 if (strcmp(this->m_list[i]->getName(),name) == 0) {
ansond 6:9a4085eeac52 62 found = true;
ansond 26:791d22d43cb4 63 value = this->m_list[i]->getValue();
ansond 114:bd38ad417d6a 64 //this->logger()->log("Getting Resource: %s Value: %s",name,value);
ansond 6:9a4085eeac52 65 }
ansond 6:9a4085eeac52 66 }
ansond 6:9a4085eeac52 67
ansond 6:9a4085eeac52 68 return value;
ansond 10:62107616fc6c 69 }
ansond 10:62107616fc6c 70
ansond 10:62107616fc6c 71 // set a resource value
ansond 21:cfdaee0a2b50 72 bool ResourceFactory::setResourceValue(char *name, char *value) {
ansond 10:62107616fc6c 73 bool found = false;
ansond 21:cfdaee0a2b50 74 bool set = false;
ansond 10:62107616fc6c 75
ansond 10:62107616fc6c 76 // loop through the resource list
ansond 47:fa96ddc36f04 77 for(int i=0;i<this->m_count && !found && name != NULL && value != NULL;++i) {
ansond 26:791d22d43cb4 78 if (strcmp(this->m_list[i]->getName(),name) == 0) {
ansond 10:62107616fc6c 79 found = true;
ansond 14:d4f8ab1e199a 80 this->logger()->log("Setting Resource: %s Value: %s",name,value);
ansond 26:791d22d43cb4 81 this->m_list[i]->setValue(value);
ansond 21:cfdaee0a2b50 82 set = true;
ansond 10:62107616fc6c 83 }
ansond 10:62107616fc6c 84 }
ansond 21:cfdaee0a2b50 85
ansond 21:cfdaee0a2b50 86 return set;
ansond 6:9a4085eeac52 87 }
ansond 10:62107616fc6c 88
ansond 21:cfdaee0a2b50 89 // get the callback pointer
ansond 21:cfdaee0a2b50 90 void *ResourceFactory::getCallbackPointer(char *name) {
ansond 21:cfdaee0a2b50 91 void *cb = NULL;
ansond 21:cfdaee0a2b50 92 bool found = false;
ansond 21:cfdaee0a2b50 93
ansond 21:cfdaee0a2b50 94 // loop through the resource list
ansond 47:fa96ddc36f04 95 for(int i=0;i<this->m_count && !found && name != NULL;++i) {
ansond 26:791d22d43cb4 96 if (strcmp(this->m_list[i]->getName(),name) == 0) {
ansond 21:cfdaee0a2b50 97 found = true;
ansond 26:791d22d43cb4 98 cb = this->m_list[i]->getCallbackPointer();
ansond 21:cfdaee0a2b50 99 }
ansond 21:cfdaee0a2b50 100 }
ansond 21:cfdaee0a2b50 101
ansond 21:cfdaee0a2b50 102 return cb;
ansond 21:cfdaee0a2b50 103 }
ansond 21:cfdaee0a2b50 104