Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
PhilipsLight.cpp@192:54b758a8eaaa, 2014-09-26 (annotated)
- Committer:
- ansond
- Date:
- Fri Sep 26 05:16:07 2014 +0000
- Revision:
- 192:54b758a8eaaa
- Parent:
- 142:b7a75144b0f1
updates to new logger name
Who changed what in which revision?
User | Revision | Line number | New 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 | 192:54b758a8eaaa | 23 | PhilipsLight::PhilipsLight(int id,void *transport,Logger *logger) : BaseClass(logger,NULL) { |
ansond | 29:9a99f076129a | 24 | this->m_id = id; |
ansond | 142:b7a75144b0f1 | 25 | sscanf(PL_DEFAULT_STATE,"%d",&this->m_state); |
ansond | 29:9a99f076129a | 26 | this->m_dim = PL_DEFAULT_DIM; |
ansond | 29:9a99f076129a | 27 | memset(this->m_url,0,PL_URL_BUFFER_LEN+1); |
ansond | 29:9a99f076129a | 28 | memset(this->m_payload,0,PL_PAYLOAD_BUFFER_LEN+1); |
ansond | 29:9a99f076129a | 29 | memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1); |
ansond | 97:d631edbc157b | 30 | this->m_transport = transport; |
ansond | 29:9a99f076129a | 31 | } |
ansond | 29:9a99f076129a | 32 | |
ansond | 29:9a99f076129a | 33 | // destructor |
ansond | 29:9a99f076129a | 34 | PhilipsLight::~PhilipsLight() { |
ansond | 29:9a99f076129a | 35 | } |
ansond | 29:9a99f076129a | 36 | |
ansond | 29:9a99f076129a | 37 | // turn the light on |
ansond | 29:9a99f076129a | 38 | void PhilipsLight::on() { this->m_state = 1; this->update(); } |
ansond | 29:9a99f076129a | 39 | |
ansond | 29:9a99f076129a | 40 | // turn the light off |
ansond | 29:9a99f076129a | 41 | void PhilipsLight::off() { this->m_state = 0; this->update(); } |
ansond | 91:8732d54328ae | 42 | |
ansond | 29:9a99f076129a | 43 | // dim the light |
ansond | 29:9a99f076129a | 44 | void PhilipsLight::dim(int dim) { this->m_dim = dim; this->update(); } |
ansond | 91:8732d54328ae | 45 | |
ansond | 29:9a99f076129a | 46 | // set the light state |
ansond | 29:9a99f076129a | 47 | void PhilipsLight::update() { |
ansond | 29:9a99f076129a | 48 | // create the state string |
ansond | 29:9a99f076129a | 49 | char *str_state = "false"; |
ansond | 29:9a99f076129a | 50 | if (this->m_state == 1) str_state = "true"; |
ansond | 29:9a99f076129a | 51 | |
ansond | 29:9a99f076129a | 52 | // make the URL |
ansond | 29:9a99f076129a | 53 | sprintf(this->m_url,PL_URL_TEMPLATE,PL_GW_ADDRESS,this->m_id); |
ansond | 29:9a99f076129a | 54 | |
ansond | 29:9a99f076129a | 55 | // make the payload |
ansond | 29:9a99f076129a | 56 | sprintf(this->m_payload,PL_ON_COMMAND_TEMPLATE,str_state,this->m_dim); |
ansond | 29:9a99f076129a | 57 | |
ansond | 29:9a99f076129a | 58 | // get the transport |
ansond | 29:9a99f076129a | 59 | HTTPTransport *transport = (HTTPTransport *)this->m_transport; |
ansond | 29:9a99f076129a | 60 | |
ansond | 97:d631edbc157b | 61 | // initialize the respones |
ansond | 97:d631edbc157b | 62 | memset(this->m_response,0,PL_RESPONSE_BUFFER_LEN+1); |
ansond | 97:d631edbc157b | 63 | |
ansond | 29:9a99f076129a | 64 | // DEBUG |
ansond | 97:d631edbc157b | 65 | //this->logger()->log("Setting Philips light: %d payload: %s",this->m_id,this->m_payload); |
ansond | 97:d631edbc157b | 66 | //this->logger()->log("Setting Philips light: url: %s",this->m_url); |
ansond | 29:9a99f076129a | 67 | |
ansond | 29:9a99f076129a | 68 | // issue the PUT |
ansond | 33:339eb862a4c4 | 69 | bool success = false; |
ansond | 95:ee602f51c677 | 70 | 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 | 71 | |
ansond | 29:9a99f076129a | 72 | // DEBUG |
ansond | 104:7a96a9a7cafb | 73 | if (success) this->logger()->log("Philips Light: %d updated successfully",this->m_id); |
ansond | 97:d631edbc157b | 74 | //else this->logger()->log("Philips Light: %d update FAILED.",this->m_id); |
ansond | 97:d631edbc157b | 75 | //this->logger()->log("Philips Light: Response: %s",this->m_response); |
ansond | 135:7f3f963cd159 | 76 | } |