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 EmulatedLightResourceFactory.cpp Source File

EmulatedLightResourceFactory.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 "EmulatedLightResourceFactory.h"
00021  
00022  // MBEDEndpoint support
00023  #include "MBEDEndpoint.h"
00024  
00025  // MBED supports Battery Emulation
00026  #include "MBEDBattery.h"
00027  
00028  // MBED supports Current Emulation
00029  #include "MBEDCurrent.h"
00030  
00031  // MBED supports Temperature Sensing
00032  #include "MBEDTemperature.h"
00033  
00034  // MBED supports GPS Emulation
00035  #include "MBEDgps.h"
00036  
00037  // MBED supports RSSI Emulation
00038  #include "MBEDrssi.h"
00039  
00040  // MBED supports Voltage Emulation
00041  #include "MBEDVoltage.h"
00042  
00043  // MBED supports Wattage Emulation
00044  #include "MBEDWattage.h"
00045  
00046  // Initializers
00047  void init_battery(Resource *resource) { if (resource != NULL) new MBEDBattery(resource->logger(),resource); }
00048  void init_current(Resource *resource) { if (resource != NULL) new MBEDCurrent(resource->logger(),resource); }
00049  void init_gps(Resource *resource) { if (resource != NULL) new MBEDgps(resource->logger(),resource); }
00050  void init_rssi(Resource *resource) { if (resource != NULL) new MBEDrssi(resource->logger(),resource); }
00051  void init_temperature(Resource *resource) { if (resource != NULL) new MBEDTemperature(resource->logger(),resource); }
00052  void init_voltage(Resource *resource) { if (resource != NULL) new MBEDVoltage(resource->logger(),resource); }
00053  void init_wattage(Resource *resource) { if (resource != NULL) new MBEDWattage(resource->logger(),resource); }
00054  
00055   // NSP supports Light Dimming
00056  extern void emulated_light_dimming_cb();
00057  
00058  // NSP supports Light Switch 
00059  extern void emulated_light_switch_cb();
00060  
00061  // get the IP address if we are using Ethernet
00062  #ifndef CELLULAR_NETWORK
00063     #include "EthernetInterface.h"
00064     extern EthernetInterface ethernet;
00065  #endif
00066  
00067  // default constructor
00068  EmulatedLightResourceFactory::EmulatedLightResourceFactory(Logger *logger,void *endpoint) : EmulatedResourceFactory(logger,endpoint) { 
00069     this->initGPSCoords(); 
00070  }
00071  
00072  // default destructor
00073  EmulatedLightResourceFactory::~EmulatedLightResourceFactory() {
00074  }
00075  
00076  // initialize our GPS coords
00077  void EmulatedLightResourceFactory::initGPSCoords() {
00078      memset(this->m_gps_coords,0,PREFERENCE_VALUE_LEN+1);
00079      MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint();
00080      if (endpoint != NULL && endpoint->preferences() != NULL)
00081         endpoint->preferences()->getPreference("coords",this->m_gps_coords,PREFERENCE_VALUE_LEN,ENDPOINT_GPS_COORDS);
00082      else
00083         strcpy(this->m_gps_coords,ENDPOINT_GPS_COORDS);
00084      for(int i=0;i<strlen(this->m_gps_coords);++i) if (this->m_gps_coords[i] == ',') this->m_gps_coords[i] = ' ';
00085  }
00086  
00087  // get our GPS coords from preferences
00088  char *EmulatedLightResourceFactory::getGPSCoords() { return this->m_gps_coords; }
00089  
00090  // create resources for an Emulated Light Resource
00091  void EmulatedLightResourceFactory::createResources(char *endpoint_name) {
00092     // create all of the resources we expect for this endpoint
00093     this->createResource("/dev/addldata","id:0");
00094     this->createResource("/dev/location",ENDPOINT_LOCATION);
00095     //this->createResource("/fw/devtype","Light");
00096     this->createResource("/dev/bat","5.0V",(void *)&init_battery,NULL);
00097     this->createResource("/sen/I","0.1A",(void *)&init_current,NULL);
00098     //this->createResource("/dev/I","0.1A",(void *)&init_current,NULL);
00099 #ifndef CELLULAR_NETWORK
00100     this->createResource("/nw/ipaddr",ethernet.getIPAddress());
00101 #else
00102     this->createResource("/nw/ipaddr","0.0.0.0");
00103 #endif
00104     this->createResource(endpoint_name,"/lt/0/dim",LIGHT_DIM_STATE,(void *)&emulated_light_dimming_cb);          // Action: dim/brighten light
00105     //this->createResource("/nw/eripaddr","N/A");
00106     this->createResource(endpoint_name,"/lt/0/on",LIGHT_DEFAULT_STATE,(void *)&emulated_light_switch_cb);             // Action: light on/off
00107     //this->createResource(endpoint_name,"/lt/0/ctr",LIGHT_DEFAULT_STATE,(void *)&emulated_light_switch_cb);             // Action: light on/off
00108     this->createResource("/dev/mdl",MQTT_ENDPOINT_TYPE);
00109     this->createResource("/dev/mfg",PLATFORM_STRING);
00110     this->createResource("/gps/int","60");
00111     this->createResource("/gps/fix","1");
00112     //this->createResource("/nw/pipaddr","N/A");
00113     //this->createResource("/dev/W","0.1W",(void *)&init_wattage,NULL);
00114     //this->createResource("/nw/prssi","-75",(void *)&init_rssi,NULL);
00115     this->createResource("/sen/temp","10.0C",(void *)&init_temperature,NULL);
00116     //this->createResource("/dev/t","10.0C",(void *)&init_temperature,NULL);
00117     this->createResource("/sen/V","5.0V",(void *)&init_voltage,NULL);
00118     //this->createResource("/dev/V","5.0V",(void *)&init_voltage,NULL);
00119     this->createResource("/gps/loc",this->getGPSCoords(),(void *)&init_gps,NULL);
00120     this->createResource("/ns/nspaddr","(NA)");
00121     this->createResource(endpoint_name,"/dev/panic","0",(void *)&emulated_light_switch_cb);             // Action: light on/off
00122     
00123 #ifdef MAC_ADDRESS
00124     extern char fmt_mac[RESOURCE_VALUE_LEN+1];
00125     this->createResource("/nw/macaddr",fmt_mac);
00126 #else
00127     this->createResource("/nw/macaddr","00:00:00:00:00");
00128 #endif
00129  } 
00130  
00131