mqtt specific components for the impact mbed endpoint library
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp
EmulatedLightResourceFactory.cpp@14:717372430717, 2014-03-31 (annotated)
- Committer:
- ansond
- Date:
- Mon Mar 31 18:47:19 2014 +0000
- Revision:
- 14:717372430717
- Parent:
- 11:9ae0fe4517c1
- Child:
- 17:36a4ab911aaa
updates
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:1ba6e68bf50e | 68 | EmulatedLightResourceFactory::EmulatedLightResourceFactory(ErrorHandler *error_handler,void *endpoint) : EmulatedResourceFactory(error_handler,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 | 5:1ba6e68bf50e | 80 | endpoint->preferences()->getPreference("coords",this->m_gps_coords,PREFERENCE_VALUE_LEN,ENDPOINT_GPS_COORDS); |
ansond | 5:1ba6e68bf50e | 81 | } |
ansond | 5:1ba6e68bf50e | 82 | |
ansond | 5:1ba6e68bf50e | 83 | // get our GPS coords from preferences |
ansond | 5:1ba6e68bf50e | 84 | char *EmulatedLightResourceFactory::getGPSCoords() { return this->m_gps_coords; } |
ansond | 5:1ba6e68bf50e | 85 | |
ansond | 5:1ba6e68bf50e | 86 | // create resources for an Emulated Light Resource |
ansond | 5:1ba6e68bf50e | 87 | void EmulatedLightResourceFactory::createResources(char *endpoint_name) { |
ansond | 5:1ba6e68bf50e | 88 | // create all of the resources we expect for this endpoint |
ansond | 5:1ba6e68bf50e | 89 | this->createResource("/dev/addldata","id:0"); |
ansond | 5:1ba6e68bf50e | 90 | this->createResource("/dev/location",ENDPOINT_LOCATION); |
ansond | 5:1ba6e68bf50e | 91 | this->createResource("/fw/devtype","Light"); |
ansond | 5:1ba6e68bf50e | 92 | this->createResource("/dev/bat","5.0V",(void *)&init_battery,NULL); |
ansond | 5:1ba6e68bf50e | 93 | this->createResource("/sen/I","0.1A",(void *)&init_current,NULL); |
ansond | 5:1ba6e68bf50e | 94 | this->createResource("/dev/I","0.1A",(void *)&init_current,NULL); |
ansond | 14:717372430717 | 95 | #ifndef CELLULAR_NETWORK |
ansond | 5:1ba6e68bf50e | 96 | this->createResource("/nw/ipaddr",ethernet.getIPAddress()); |
ansond | 14:717372430717 | 97 | #else |
ansond | 14:717372430717 | 98 | this->createResource("/nw/ipaddr","0.0.0.0"); |
ansond | 14:717372430717 | 99 | #endif |
ansond | 5:1ba6e68bf50e | 100 | this->createResource(endpoint_name,"/lt/0/dim","25",(void *)&emulated_light_dimming_cb); // Action: dim/brighten light |
ansond | 5:1ba6e68bf50e | 101 | this->createResource("/nw/eripaddr","N/A"); |
ansond | 5:1ba6e68bf50e | 102 | this->createResource(endpoint_name,"/lt/0/on","1",(void *)&emulated_light_switch_cb); // Action: light on/off |
ansond | 5:1ba6e68bf50e | 103 | //this->createResource(endpoint_name,"/lt/0/ctr","1",(void *)&emulated_light_switch_cb); // Action: light on/off |
ansond | 5:1ba6e68bf50e | 104 | this->createResource("/dev/mdl","MQTT MBED Light"); |
ansond | 5:1ba6e68bf50e | 105 | this->createResource("/dev/mfg",PLATFORM_STRING); |
ansond | 5:1ba6e68bf50e | 106 | this->createResource("/gps/int","60"); |
ansond | 5:1ba6e68bf50e | 107 | this->createResource("/gps/fix","1"); |
ansond | 5:1ba6e68bf50e | 108 | this->createResource("/nw/pipaddr","N/A"); |
ansond | 5:1ba6e68bf50e | 109 | this->createResource("/dev/W","0.1W",(void *)&init_wattage,NULL); |
ansond | 5:1ba6e68bf50e | 110 | this->createResource("/nw/prssi","-75",(void *)&init_rssi,NULL); |
ansond | 5:1ba6e68bf50e | 111 | this->createResource("/sen/temp","10.0C",(void *)&init_temperature,NULL); |
ansond | 5:1ba6e68bf50e | 112 | this->createResource("/dev/t","10.0C",(void *)&init_temperature,NULL); |
ansond | 5:1ba6e68bf50e | 113 | this->createResource("/sen/V","5.0V",(void *)&init_voltage,NULL); |
ansond | 5:1ba6e68bf50e | 114 | this->createResource("/dev/V","5.0V",(void *)&init_voltage,NULL); |
ansond | 5:1ba6e68bf50e | 115 | this->createResource("/gps/loc",this->getGPSCoords(),(void *)&init_gps,NULL); |
ansond | 5:1ba6e68bf50e | 116 | this->createResource("/ns/nspaddr","(NA)"); |
ansond | 5:1ba6e68bf50e | 117 | this->createResource(endpoint_name,"/dev/panic","0",(void *)&emulated_light_switch_cb); // Action: light on/off |
ansond | 5:1ba6e68bf50e | 118 | |
ansond | 5:1ba6e68bf50e | 119 | #ifdef MAC_ADDRESS |
ansond | 5:1ba6e68bf50e | 120 | extern char fmt_mac[RESOURCE_VALUE_LEN+1]; |
ansond | 5:1ba6e68bf50e | 121 | this->createResource("/nw/macaddr",fmt_mac); |
ansond | 5:1ba6e68bf50e | 122 | #else |
ansond | 5:1ba6e68bf50e | 123 | this->createResource("/nw/macaddr","00:00:00:00:00"); |
ansond | 5:1ba6e68bf50e | 124 | #endif |
ansond | 5:1ba6e68bf50e | 125 | } |
ansond | 5:1ba6e68bf50e | 126 | |
ansond | 5:1ba6e68bf50e | 127 |