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

PhilipsLight.cpp

Committer:
ansond
Date:
2014-03-13
Revision:
91:8732d54328ae
Parent:
87:e9d77e9f9eae
Child:
94:4939666f455d

File content as of revision 91:8732d54328ae:

/* 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 = false;
     if (transport != NULL) transport->httpPut(this->m_url,this->m_payload,strlen(this->m_payload),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; }