diff options
Diffstat (limited to 'src/capability/capability_switch.c')
-rw-r--r-- | src/capability/capability_switch.c | 78 |
1 files changed, 42 insertions, 36 deletions
diff --git a/src/capability/capability_switch.c b/src/capability/capability_switch.c index 46f8209..bc6eef9 100644 --- a/src/capability/capability_switch.c +++ b/src/capability/capability_switch.c @@ -14,58 +14,64 @@ * limitations under the License. */ -#include "st_things.h" +#include <smartthings_resource.h> #include "log.h" -static const char *PROP_POWER = "power"; +#define SWITCH_POWER_ON "on" +#define SWITCH_POWER_OFF "off" -#define VALUE_STR_LEN_MAX 32 -static const char *VALUE_SWITCH_ON = "on"; -static const char *VALUE_SWITCH_OFF = "off"; -static char g_switch[VALUE_STR_LEN_MAX] = "off"; +#define RES_CAPABILITY_SWITCH_MAIN_0 "/capability/switch/main/0" +#define PROP_POWER "power" -extern void set_switch_status(bool status); +static bool switch_status = false; -bool handle_get_request_on_resource_capability_switch(st_things_get_request_message_s* req_msg, st_things_representation_s* resp_rep) +bool handle_get_request_on_resource_capability_switch_main_0(smartthings_payload_h resp_payload, void *user_data) { - DBG("Received a GET request on %s\n", req_msg->resource_uri); + int error = SMARTTHINGS_RESOURCE_ERROR_NONE; + char *str = NULL; - if (req_msg->has_property_key(req_msg, PROP_POWER)) { - resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch); - } + _D("Received a GET request\n"); + + if (switch_status == true) + str = SWITCH_POWER_ON; + else + str = SWITCH_POWER_OFF; + + error = smartthings_payload_set_string(resp_payload, PROP_POWER, str); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) + _E("smartthings_payload_set_string() failed, [%d]", error); - return true; + _D("Power : %s", str); + + return true; } -bool handle_set_request_on_resource_capability_switch(st_things_set_request_message_s* req_msg, st_things_representation_s* resp_rep) +bool handle_set_request_on_resource_capability_switch_main_0(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data) { - DBG("Received a SET request on %s\n", req_msg->resource_uri); + char *str = NULL; + char *res_str = NULL; + int error = SMARTTHINGS_RESOURCE_ERROR_NONE; - char *str_value = NULL; - req_msg->rep->get_str_value(req_msg->rep, PROP_POWER, &str_value); + _D("Received a SET request"); - /* check validation */ - if ((0 != strncmp(str_value, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON))) - && (0 != strncmp(str_value, VALUE_SWITCH_OFF, strlen(VALUE_SWITCH_OFF)))) { - ERR("Not supported value!!"); - free(str_value); - return false; - } + error = smartthings_payload_get_string(payload, PROP_POWER, &str); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) + _E("smartthings_payload_get_string() failed, [%d]", error); - if (0 != strncmp(str_value, g_switch, strlen(g_switch))) { - strncpy(g_switch, str_value, VALUE_STR_LEN_MAX); - if (0 == strncmp(g_switch, VALUE_SWITCH_ON, strlen(VALUE_SWITCH_ON))) { - set_switch_status(true); - } - else { - set_switch_status(false); - } + if (strncmp(str, SWITCH_POWER_ON, strlen(SWITCH_POWER_ON))) { + switch_status = false; + res_str = SWITCH_POWER_OFF; + } else { + switch_status = true; + res_str = SWITCH_POWER_ON; } - resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch); - st_things_notify_observers(req_msg->resource_uri); + free(str); - free(str_value); + error = smartthings_payload_set_string(resp_payload, PROP_POWER, res_str); + if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) + _E("smartthings_payload_set_string() failed, [%d]", error); - return true; + return true; } + |