tizen 5.0 migration
[apps/native/co2-meter.git] / src / capability / capability_switch.c
index 3310ad7..e18e861 100644 (file)
  * limitations under the License.
  */
 
-#include "st_things.h"
+#include "smartthings_resource.h"
 #include "log.h"
 
 #define VALUE_STR_LEN_MAX 32
 
-static const charPROP_POWER = "power";
-static const charVALUE_SWITCH_ON = "on";
-static const charVALUE_SWITCH_OFF = "off";
+static const char *PROP_POWER = "power";
+static const char *VALUE_SWITCH_ON = "on";
+static const char *VALUE_SWITCH_OFF = "off";
 static char g_switch[VALUE_STR_LEN_MAX] = "off";
 bool g_switch_is_on = 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;
 
-       if (req_msg->has_property_key(req_msg, PROP_POWER)) {
-               resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
-       }
-       else
+       error = smartthings_payload_set_string(resp_payload, PROP_POWER, g_switch);
+       if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
+               _E("smartthings_payload_set_string() failed, [%d]", error);
                return false;
+       }
+       _D("get switch : %s", g_switch);
+
        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_value = NULL;
-       req_msg->rep->get_str_value(req_msg->rep, PROP_POWER, &str_value);
+       int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
+
+    error = smartthings_payload_get_string(payload, PROP_POWER, &str_value);
+       if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
+               _E("smartthings_payload_get_string() failed, [%d]", error);
+               return false;
+       }
+       _D("set switch : %s", str_value);
 
        /* 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!!");
+               _E("Not supported value!!");
                free(str_value);
                return false;
        }
+
        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))) {
                        g_switch_is_on = true;
-               }
-               else  {
+               } else {
                        g_switch_is_on = false;
                }
        }
-       resp_rep->set_str_value(resp_rep, PROP_POWER, g_switch);
-
-       st_things_notify_observers(req_msg->resource_uri);
-
        free(str_value);
 
+       error = smartthings_payload_set_string(resp_payload, PROP_POWER, g_switch);
+       if (error != SMARTTHINGS_RESOURCE_ERROR_NONE) {
+               _E("smartthings_payload_set_string() failed, [%d]", error);
+               return false;
+       }
+
        return true;
 }