mqtt specific components for the impact mbed endpoint library

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp

Committer:
ansond
Date:
Fri Sep 26 05:16:26 2014 +0000
Revision:
56:789a1a8c5ebe
Parent:
55:7992e9582e2e
updates for new logger name

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 5:1ba6e68bf50e 1 /* Copyright C2013 Doug Anson, MIT License
ansond 5:1ba6e68bf50e 2 *
ansond 5:1ba6e68bf50e 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 5:1ba6e68bf50e 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 5:1ba6e68bf50e 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 5:1ba6e68bf50e 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 5:1ba6e68bf50e 7 * furnished to do so, subject to the following conditions:
ansond 5:1ba6e68bf50e 8 *
ansond 5:1ba6e68bf50e 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 5:1ba6e68bf50e 10 * substantial portions of the Software.
ansond 5:1ba6e68bf50e 11 *
ansond 5:1ba6e68bf50e 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 5:1ba6e68bf50e 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 5:1ba6e68bf50e 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 5:1ba6e68bf50e 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 5:1ba6e68bf50e 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 5:1ba6e68bf50e 17 */
ansond 5:1ba6e68bf50e 18
ansond 5:1ba6e68bf50e 19 // class definition
ansond 5:1ba6e68bf50e 20 #include "EmulatedLightResourceFactory.h"
ansond 5:1ba6e68bf50e 21
ansond 5:1ba6e68bf50e 22 // MBEDEndpoint support
ansond 5:1ba6e68bf50e 23 #include "MBEDEndpoint.h"
ansond 5:1ba6e68bf50e 24
ansond 5:1ba6e68bf50e 25 // MBED supports Battery Emulation
ansond 5:1ba6e68bf50e 26 #include "MBEDBattery.h"
ansond 5:1ba6e68bf50e 27
ansond 5:1ba6e68bf50e 28 // MBED supports Current Emulation
ansond 5:1ba6e68bf50e 29 #include "MBEDCurrent.h"
ansond 5:1ba6e68bf50e 30
ansond 5:1ba6e68bf50e 31 // MBED supports Temperature Sensing
ansond 5:1ba6e68bf50e 32 #include "MBEDTemperature.h"
ansond 5:1ba6e68bf50e 33
ansond 5:1ba6e68bf50e 34 // MBED supports GPS Emulation
ansond 5:1ba6e68bf50e 35 #include "MBEDgps.h"
ansond 5:1ba6e68bf50e 36
ansond 5:1ba6e68bf50e 37 // MBED supports RSSI Emulation
ansond 5:1ba6e68bf50e 38 #include "MBEDrssi.h"
ansond 5:1ba6e68bf50e 39
ansond 5:1ba6e68bf50e 40 // MBED supports Voltage Emulation
ansond 5:1ba6e68bf50e 41 #include "MBEDVoltage.h"
ansond 5:1ba6e68bf50e 42
ansond 5:1ba6e68bf50e 43 // MBED supports Wattage Emulation
ansond 5:1ba6e68bf50e 44 #include "MBEDWattage.h"
ansond 5:1ba6e68bf50e 45
ansond 5:1ba6e68bf50e 46 // Initializers
ansond 5:1ba6e68bf50e 47 void init_battery(Resource *resource) { if (resource != NULL) new MBEDBattery(resource->logger(),resource); }
ansond 5:1ba6e68bf50e 48 void init_current(Resource *resource) { if (resource != NULL) new MBEDCurrent(resource->logger(),resource); }
ansond 5:1ba6e68bf50e 49 void init_gps(Resource *resource) { if (resource != NULL) new MBEDgps(resource->logger(),resource); }
ansond 5:1ba6e68bf50e 50 void init_rssi(Resource *resource) { if (resource != NULL) new MBEDrssi(resource->logger(),resource); }
ansond 5:1ba6e68bf50e 51 void init_temperature(Resource *resource) { if (resource != NULL) new MBEDTemperature(resource->logger(),resource); }
ansond 5:1ba6e68bf50e 52 void init_voltage(Resource *resource) { if (resource != NULL) new MBEDVoltage(resource->logger(),resource); }
ansond 5:1ba6e68bf50e 53 void init_wattage(Resource *resource) { if (resource != NULL) new MBEDWattage(resource->logger(),resource); }
ansond 5:1ba6e68bf50e 54
ansond 5:1ba6e68bf50e 55 // NSP supports Light Dimming
ansond 5:1ba6e68bf50e 56 extern void emulated_light_dimming_cb();
ansond 5:1ba6e68bf50e 57
ansond 5:1ba6e68bf50e 58 // NSP supports Light Switch
ansond 5:1ba6e68bf50e 59 extern void emulated_light_switch_cb();
ansond 5:1ba6e68bf50e 60
ansond 14:717372430717 61 // get the IP address if we are using Ethernet
ansond 14:717372430717 62 #ifndef CELLULAR_NETWORK
ansond 14:717372430717 63 #include "EthernetInterface.h"
ansond 14:717372430717 64 extern EthernetInterface ethernet;
ansond 14:717372430717 65 #endif
ansond 5:1ba6e68bf50e 66
ansond 5:1ba6e68bf50e 67 // default constructor
ansond 56:789a1a8c5ebe 68 EmulatedLightResourceFactory::EmulatedLightResourceFactory(Logger *logger,void *endpoint) : EmulatedResourceFactory(logger,endpoint) {
ansond 5:1ba6e68bf50e 69 this->initGPSCoords();
ansond 5:1ba6e68bf50e 70 }
ansond 5:1ba6e68bf50e 71
ansond 5:1ba6e68bf50e 72 // default destructor
ansond 5:1ba6e68bf50e 73 EmulatedLightResourceFactory::~EmulatedLightResourceFactory() {
ansond 5:1ba6e68bf50e 74 }
ansond 5:1ba6e68bf50e 75
ansond 5:1ba6e68bf50e 76 // initialize our GPS coords
ansond 5:1ba6e68bf50e 77 void EmulatedLightResourceFactory::initGPSCoords() {
ansond 5:1ba6e68bf50e 78 memset(this->m_gps_coords,0,PREFERENCE_VALUE_LEN+1);
ansond 11:9ae0fe4517c1 79 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
ansond 29:ac6390032cec 80 if (endpoint != NULL && endpoint->preferences() != NULL)
ansond 29:ac6390032cec 81 endpoint->preferences()->getPreference("coords",this->m_gps_coords,PREFERENCE_VALUE_LEN,ENDPOINT_GPS_COORDS);
ansond 29:ac6390032cec 82 else
ansond 29:ac6390032cec 83 strcpy(this->m_gps_coords,ENDPOINT_GPS_COORDS);
ansond 29:ac6390032cec 84 for(int i=0;i<strlen(this->m_gps_coords);++i) if (this->m_gps_coords[i] == ',') this->m_gps_coords[i] = ' ';
ansond 5:1ba6e68bf50e 85 }
ansond 5:1ba6e68bf50e 86
ansond 5:1ba6e68bf50e 87 // get our GPS coords from preferences
ansond 5:1ba6e68bf50e 88 char *EmulatedLightResourceFactory::getGPSCoords() { return this->m_gps_coords; }
ansond 5:1ba6e68bf50e 89
ansond 5:1ba6e68bf50e 90 // create resources for an Emulated Light Resource
ansond 5:1ba6e68bf50e 91 void EmulatedLightResourceFactory::createResources(char *endpoint_name) {
ansond 5:1ba6e68bf50e 92 // create all of the resources we expect for this endpoint
ansond 5:1ba6e68bf50e 93 this->createResource("/dev/addldata","id:0");
ansond 5:1ba6e68bf50e 94 this->createResource("/dev/location",ENDPOINT_LOCATION);
ansond 55:7992e9582e2e 95 //this->createResource("/fw/devtype","Light");
ansond 5:1ba6e68bf50e 96 this->createResource("/dev/bat","5.0V",(void *)&init_battery,NULL);
ansond 5:1ba6e68bf50e 97 this->createResource("/sen/I","0.1A",(void *)&init_current,NULL);
ansond 55:7992e9582e2e 98 //this->createResource("/dev/I","0.1A",(void *)&init_current,NULL);
ansond 14:717372430717 99 #ifndef CELLULAR_NETWORK
ansond 5:1ba6e68bf50e 100 this->createResource("/nw/ipaddr",ethernet.getIPAddress());
ansond 14:717372430717 101 #else
ansond 14:717372430717 102 this->createResource("/nw/ipaddr","0.0.0.0");
ansond 14:717372430717 103 #endif
ansond 17:36a4ab911aaa 104 this->createResource(endpoint_name,"/lt/0/dim",LIGHT_DIM_STATE,(void *)&emulated_light_dimming_cb); // Action: dim/brighten light
ansond 55:7992e9582e2e 105 //this->createResource("/nw/eripaddr","N/A");
ansond 17:36a4ab911aaa 106 this->createResource(endpoint_name,"/lt/0/on",LIGHT_DEFAULT_STATE,(void *)&emulated_light_switch_cb); // Action: light on/off
ansond 17:36a4ab911aaa 107 //this->createResource(endpoint_name,"/lt/0/ctr",LIGHT_DEFAULT_STATE,(void *)&emulated_light_switch_cb); // Action: light on/off
ansond 22:afb208d1d79e 108 this->createResource("/dev/mdl",MQTT_ENDPOINT_TYPE);
ansond 5:1ba6e68bf50e 109 this->createResource("/dev/mfg",PLATFORM_STRING);
ansond 5:1ba6e68bf50e 110 this->createResource("/gps/int","60");
ansond 5:1ba6e68bf50e 111 this->createResource("/gps/fix","1");
ansond 55:7992e9582e2e 112 //this->createResource("/nw/pipaddr","N/A");
ansond 55:7992e9582e2e 113 //this->createResource("/dev/W","0.1W",(void *)&init_wattage,NULL);
ansond 55:7992e9582e2e 114 //this->createResource("/nw/prssi","-75",(void *)&init_rssi,NULL);
ansond 5:1ba6e68bf50e 115 this->createResource("/sen/temp","10.0C",(void *)&init_temperature,NULL);
ansond 55:7992e9582e2e 116 //this->createResource("/dev/t","10.0C",(void *)&init_temperature,NULL);
ansond 5:1ba6e68bf50e 117 this->createResource("/sen/V","5.0V",(void *)&init_voltage,NULL);
ansond 55:7992e9582e2e 118 //this->createResource("/dev/V","5.0V",(void *)&init_voltage,NULL);
ansond 5:1ba6e68bf50e 119 this->createResource("/gps/loc",this->getGPSCoords(),(void *)&init_gps,NULL);
ansond 5:1ba6e68bf50e 120 this->createResource("/ns/nspaddr","(NA)");
ansond 5:1ba6e68bf50e 121 this->createResource(endpoint_name,"/dev/panic","0",(void *)&emulated_light_switch_cb); // Action: light on/off
ansond 5:1ba6e68bf50e 122
ansond 5:1ba6e68bf50e 123 #ifdef MAC_ADDRESS
ansond 5:1ba6e68bf50e 124 extern char fmt_mac[RESOURCE_VALUE_LEN+1];
ansond 5:1ba6e68bf50e 125 this->createResource("/nw/macaddr",fmt_mac);
ansond 5:1ba6e68bf50e 126 #else
ansond 5:1ba6e68bf50e 127 this->createResource("/nw/macaddr","00:00:00:00:00");
ansond 5:1ba6e68bf50e 128 #endif
ansond 5:1ba6e68bf50e 129 }
ansond 5:1ba6e68bf50e 130
ansond 5:1ba6e68bf50e 131