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

Revision:
29:9a99f076129a
Child:
33:339eb862a4c4
diff -r 52b7ca6aacd6 -r 9a99f076129a PhilipsLight.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PhilipsLight.cpp	Fri Feb 28 04:56:16 2014 +0000
@@ -0,0 +1,74 @@
+/* Copyright C2013 Doug Anson, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files the "Software", to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+ 
+ #include "PhilipsLight.h"
+ #include "HTTPTransport.h"
+ 
+ // default constructor
+ PhilipsLight::PhilipsLight(int id,void *transport,ErrorHandler *error_handler) {
+     this->m_id = id;
+     this->m_state = PL_DEFAULT_STATE;
+     this->m_dim = PL_DEFAULT_DIM;
+     this->m_transport = transport;
+     this->m_error_handler = error_handler;
+     memset(this->m_url,0,PL_URL_BUFFER_LEN+1);
+     memset(this->m_payload,0,PL_PAYLOAD_BUFFER_LEN+1);
+     memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1);
+ }
+ 
+ // destructor
+ PhilipsLight::~PhilipsLight() {
+ }
+ 
+ // turn the light on
+ void PhilipsLight::on() { this->m_state = 1; this->update(); }
+ 
+ // turn the light off
+ void PhilipsLight::off() { this->m_state = 0; this->update(); }
+ 
+ // dim the light
+ void PhilipsLight::dim(int dim) { this->m_dim = dim; this->update(); }
+ 
+ // set the light state
+ void PhilipsLight::update() {
+     // create the state string
+     char *str_state = "false";
+     if (this->m_state == 1) str_state = "true";
+     
+     // make the URL
+     sprintf(this->m_url,PL_URL_TEMPLATE,PL_GW_ADDRESS,this->m_id);
+     
+     // make the payload
+     sprintf(this->m_payload,PL_ON_COMMAND_TEMPLATE,str_state,this->m_dim);
+     
+     // get the transport
+     HTTPTransport *transport = (HTTPTransport *)this->m_transport;
+     
+     // DEBUG
+     this->logger()->log("Setting Philips light: %d payload: %s",this->m_id,this->m_payload);
+     
+     // issue the PUT
+     bool success = transport->httpPut(this->m_url,this->m_payload,PL_PAYLOAD_BUFFER_LEN,this->m_response,PL_RESPONSE_BUFFER_LEN);
+     
+     // DEBUG
+     if (success) this->logger()->log("Philips Light: %d updated successfully",this->m_id);
+     else this->logger()->log("Philips Light: %d update FAILED",this->m_id);
+ }
+ 
+ // get the error handler
+ ErrorHandler *PhilipsLight::logger() { return this->m_error_handler; }