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 20:20:32 2014 +0000
Revision:
96:686ec39400dc
Parent:
95:ee602f51c677
Child:
97:d631edbc157b
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 96:686ec39400dc 22 // HTTP Client
ansond 96:686ec39400dc 23 HTTPClient _philips_http;
ansond 96:686ec39400dc 24
ansond 29:9a99f076129a 25 // default constructor
ansond 29:9a99f076129a 26 PhilipsLight::PhilipsLight(int id,void *transport,ErrorHandler *error_handler) {
ansond 29:9a99f076129a 27 this->m_id = id;
ansond 29:9a99f076129a 28 this->m_state = PL_DEFAULT_STATE;
ansond 29:9a99f076129a 29 this->m_dim = PL_DEFAULT_DIM;
ansond 29:9a99f076129a 30 this->m_error_handler = error_handler;
ansond 29:9a99f076129a 31 memset(this->m_url,0,PL_URL_BUFFER_LEN+1);
ansond 29:9a99f076129a 32 memset(this->m_payload,0,PL_PAYLOAD_BUFFER_LEN+1);
ansond 29:9a99f076129a 33 memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1);
ansond 96:686ec39400dc 34 this->initHttpTransport(transport);
ansond 29:9a99f076129a 35 }
ansond 29:9a99f076129a 36
ansond 29:9a99f076129a 37 // destructor
ansond 29:9a99f076129a 38 PhilipsLight::~PhilipsLight() {
ansond 96:686ec39400dc 39 HTTPTransport *transport = (HTTPTransport *)this->m_transport;
ansond 96:686ec39400dc 40 if (transport != NULL) delete transport;
ansond 96:686ec39400dc 41 }
ansond 96:686ec39400dc 42
ansond 96:686ec39400dc 43 // initialize the transport
ansond 96:686ec39400dc 44 void PhilipsLight::initHttpTransport(void *transport) {
ansond 96:686ec39400dc 45 if (transport != NULL && this->m_transport == NULL) {
ansond 96:686ec39400dc 46 HTTPTransport *http = (HTTPTransport *)transport;
ansond 96:686ec39400dc 47 this->m_transport = new HTTPTransport(&_philips_http,this->m_error_handler,http->getEndpoint());
ansond 96:686ec39400dc 48 }
ansond 29:9a99f076129a 49 }
ansond 29:9a99f076129a 50
ansond 29:9a99f076129a 51 // turn the light on
ansond 29:9a99f076129a 52 void PhilipsLight::on() { this->m_state = 1; this->update(); }
ansond 29:9a99f076129a 53
ansond 29:9a99f076129a 54 // turn the light off
ansond 29:9a99f076129a 55 void PhilipsLight::off() { this->m_state = 0; this->update(); }
ansond 91:8732d54328ae 56
ansond 29:9a99f076129a 57 // dim the light
ansond 29:9a99f076129a 58 void PhilipsLight::dim(int dim) { this->m_dim = dim; this->update(); }
ansond 91:8732d54328ae 59
ansond 29:9a99f076129a 60 // set the light state
ansond 29:9a99f076129a 61 void PhilipsLight::update() {
ansond 29:9a99f076129a 62 // create the state string
ansond 29:9a99f076129a 63 char *str_state = "false";
ansond 29:9a99f076129a 64 if (this->m_state == 1) str_state = "true";
ansond 29:9a99f076129a 65
ansond 29:9a99f076129a 66 // make the URL
ansond 29:9a99f076129a 67 sprintf(this->m_url,PL_URL_TEMPLATE,PL_GW_ADDRESS,this->m_id);
ansond 29:9a99f076129a 68
ansond 29:9a99f076129a 69 // make the payload
ansond 29:9a99f076129a 70 sprintf(this->m_payload,PL_ON_COMMAND_TEMPLATE,str_state,this->m_dim);
ansond 29:9a99f076129a 71
ansond 29:9a99f076129a 72 // get the transport
ansond 29:9a99f076129a 73 HTTPTransport *transport = (HTTPTransport *)this->m_transport;
ansond 29:9a99f076129a 74
ansond 29:9a99f076129a 75 // DEBUG
ansond 29:9a99f076129a 76 this->logger()->log("Setting Philips light: %d payload: %s",this->m_id,this->m_payload);
ansond 94:4939666f455d 77 this->logger()->log("Setting Philips light: url: %s",this->m_url);
ansond 29:9a99f076129a 78
ansond 29:9a99f076129a 79 // issue the PUT
ansond 33:339eb862a4c4 80 bool success = false;
ansond 95:ee602f51c677 81 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 82
ansond 29:9a99f076129a 83 // DEBUG
ansond 29:9a99f076129a 84 if (success) this->logger()->log("Philips Light: %d updated successfully",this->m_id);
ansond 95:ee602f51c677 85 else this->logger()->log("Philips Light: %d update FAILED.",this->m_id);
ansond 95:ee602f51c677 86 this->logger()->log("Philips Light: Response: %s",this->m_response);
ansond 29:9a99f076129a 87 }
ansond 29:9a99f076129a 88
ansond 29:9a99f076129a 89 // get the error handler
ansond 29:9a99f076129a 90 ErrorHandler *PhilipsLight::logger() { return this->m_error_handler; }