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 Mar 14 21:25:46 2014 +0000
Revision:
97:d631edbc157b
Parent:
96:686ec39400dc
Child:
104:7a96a9a7cafb
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 97:d631edbc157b 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_error_handler = error_handler;
ansond 29:9a99f076129a 28 memset(this->m_url,0,PL_URL_BUFFER_LEN+1);
ansond 29:9a99f076129a 29 memset(this->m_payload,0,PL_PAYLOAD_BUFFER_LEN+1);
ansond 29:9a99f076129a 30 memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1);
ansond 97:d631edbc157b 31 this->m_transport = transport;
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 91:8732d54328ae 43
ansond 29:9a99f076129a 44 // dim the light
ansond 29:9a99f076129a 45 void PhilipsLight::dim(int dim) { this->m_dim = dim; this->update(); }
ansond 91:8732d54328ae 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 97:d631edbc157b 62 // initialize the respones
ansond 97:d631edbc157b 63 memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1);
ansond 97:d631edbc157b 64
ansond 29:9a99f076129a 65 // DEBUG
ansond 97:d631edbc157b 66 //this->logger()->log("Setting Philips light: %d payload: %s",this->m_id,this->m_payload);
ansond 97:d631edbc157b 67 //this->logger()->log("Setting Philips light: url: %s",this->m_url);
ansond 29:9a99f076129a 68
ansond 29:9a99f076129a 69 // issue the PUT
ansond 33:339eb862a4c4 70 bool success = false;
ansond 95:ee602f51c677 71 if (transport != NULL) success = transport->httpPut(this->m_url,this->m_payload,strlen(this->m_payload),this->m_response,PL_RESPONSE_BUFFER_LEN);
ansond 29:9a99f076129a 72
ansond 29:9a99f076129a 73 // DEBUG
ansond 97:d631edbc157b 74 //if (success) this->logger()->log("Philips Light: %d updated successfully",this->m_id);
ansond 97:d631edbc157b 75 //else this->logger()->log("Philips Light: %d update FAILED.",this->m_id);
ansond 97:d631edbc157b 76 //this->logger()->log("Philips Light: Response: %s",this->m_response);
ansond 29:9a99f076129a 77 }
ansond 29:9a99f076129a 78
ansond 29:9a99f076129a 79 // get the error handler
ansond 29:9a99f076129a 80 ErrorHandler *PhilipsLight::logger() { return this->m_error_handler; }