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

Committer:
ansond
Date:
Fri Feb 28 04:56:16 2014 +0000
Revision:
29:9a99f076129a
Child:
33:339eb862a4c4
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 29:9a99f076129a 1 /* Copyright C2013 Doug Anson, MIT License
ansond 29:9a99f076129a 2 *
ansond 29:9a99f076129a 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 29:9a99f076129a 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 29:9a99f076129a 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 29:9a99f076129a 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 29:9a99f076129a 7 * furnished to do so, subject to the following conditions:
ansond 29:9a99f076129a 8 *
ansond 29:9a99f076129a 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 29:9a99f076129a 10 * substantial portions of the Software.
ansond 29:9a99f076129a 11 *
ansond 29:9a99f076129a 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 29:9a99f076129a 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 29:9a99f076129a 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 29:9a99f076129a 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 29:9a99f076129a 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 29:9a99f076129a 17 */
ansond 29:9a99f076129a 18
ansond 29:9a99f076129a 19 #include "PhilipsLight.h"
ansond 29:9a99f076129a 20 #include "HTTPTransport.h"
ansond 29:9a99f076129a 21
ansond 29:9a99f076129a 22 // default constructor
ansond 29:9a99f076129a 23 PhilipsLight::PhilipsLight(int id,void *transport,ErrorHandler *error_handler) {
ansond 29:9a99f076129a 24 this->m_id = id;
ansond 29:9a99f076129a 25 this->m_state = PL_DEFAULT_STATE;
ansond 29:9a99f076129a 26 this->m_dim = PL_DEFAULT_DIM;
ansond 29:9a99f076129a 27 this->m_transport = transport;
ansond 29:9a99f076129a 28 this->m_error_handler = error_handler;
ansond 29:9a99f076129a 29 memset(this->m_url,0,PL_URL_BUFFER_LEN+1);
ansond 29:9a99f076129a 30 memset(this->m_payload,0,PL_PAYLOAD_BUFFER_LEN+1);
ansond 29:9a99f076129a 31 memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1);
ansond 29:9a99f076129a 32 }
ansond 29:9a99f076129a 33
ansond 29:9a99f076129a 34 // destructor
ansond 29:9a99f076129a 35 PhilipsLight::~PhilipsLight() {
ansond 29:9a99f076129a 36 }
ansond 29:9a99f076129a 37
ansond 29:9a99f076129a 38 // turn the light on
ansond 29:9a99f076129a 39 void PhilipsLight::on() { this->m_state = 1; this->update(); }
ansond 29:9a99f076129a 40
ansond 29:9a99f076129a 41 // turn the light off
ansond 29:9a99f076129a 42 void PhilipsLight::off() { this->m_state = 0; this->update(); }
ansond 29:9a99f076129a 43
ansond 29:9a99f076129a 44 // dim the light
ansond 29:9a99f076129a 45 void PhilipsLight::dim(int dim) { this->m_dim = dim; this->update(); }
ansond 29:9a99f076129a 46
ansond 29:9a99f076129a 47 // set the light state
ansond 29:9a99f076129a 48 void PhilipsLight::update() {
ansond 29:9a99f076129a 49 // create the state string
ansond 29:9a99f076129a 50 char *str_state = "false";
ansond 29:9a99f076129a 51 if (this->m_state == 1) str_state = "true";
ansond 29:9a99f076129a 52
ansond 29:9a99f076129a 53 // make the URL
ansond 29:9a99f076129a 54 sprintf(this->m_url,PL_URL_TEMPLATE,PL_GW_ADDRESS,this->m_id);
ansond 29:9a99f076129a 55
ansond 29:9a99f076129a 56 // make the payload
ansond 29:9a99f076129a 57 sprintf(this->m_payload,PL_ON_COMMAND_TEMPLATE,str_state,this->m_dim);
ansond 29:9a99f076129a 58
ansond 29:9a99f076129a 59 // get the transport
ansond 29:9a99f076129a 60 HTTPTransport *transport = (HTTPTransport *)this->m_transport;
ansond 29:9a99f076129a 61
ansond 29:9a99f076129a 62 // DEBUG
ansond 29:9a99f076129a 63 this->logger()->log("Setting Philips light: %d payload: %s",this->m_id,this->m_payload);
ansond 29:9a99f076129a 64
ansond 29:9a99f076129a 65 // issue the PUT
ansond 29:9a99f076129a 66 bool success = transport->httpPut(this->m_url,this->m_payload,PL_PAYLOAD_BUFFER_LEN,this->m_response,PL_RESPONSE_BUFFER_LEN);
ansond 29:9a99f076129a 67
ansond 29:9a99f076129a 68 // DEBUG
ansond 29:9a99f076129a 69 if (success) this->logger()->log("Philips Light: %d updated successfully",this->m_id);
ansond 29:9a99f076129a 70 else this->logger()->log("Philips Light: %d update FAILED",this->m_id);
ansond 29:9a99f076129a 71 }
ansond 29:9a99f076129a 72
ansond 29:9a99f076129a 73 // get the error handler
ansond 29:9a99f076129a 74 ErrorHandler *PhilipsLight::logger() { return this->m_error_handler; }