summaryrefslogtreecommitdiff
path: root/src/controller.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller.c')
-rw-r--r--src/controller.c104
1 files changed, 56 insertions, 48 deletions
diff --git a/src/controller.c b/src/controller.c
index 683294c..90a54d0 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -28,6 +28,10 @@
#define LED_ON "on"
#define LED_OFF "off"
+#define SENSOR_LED_INIT "off"
+#define SENSOR_MOTION_CH (46)
+#define SENSOR_LED_CH (130)
+#define SENSOR_GATHER_INTERVAL (50) //50ms
#define USE_ST_SDK
@@ -43,12 +47,8 @@
#define SENSOR_MOTION_URI "/capability/motionSensor/main/0"
#define SENSOR_MOTION_KEY "value"
-#define SENSOR_MOTION_CH 46
#define SENSOR_LED_URI "/capability/switch/main/0"
#define SENSOR_LED_KEY "power"
-#define SENSOR_LED_INIT "off"
-#define SENSOR_LED_CH 130
-#define SENSOR_GATHER_INTERVAL (50) //50ms
#endif /* USE_ST_SDK */
@@ -106,6 +106,26 @@ __resource_error_to_str(smartthings_resource_error_e error)
return err_str;
}
+static int __change_led_data(void *data, char *state) {
+ int ret = 0;
+ app_data *ad = data;
+
+ retv_if(!ad, -1);
+ retv_if(!ad->led_data, -1);
+
+ sensor_data_set_string(ad->led_data, state, strlen(state));
+
+ if (0 == strcmp(state, LED_ON)) {
+ ret = resource_write_led(SENSOR_LED_CH, 1);
+ } else {
+ ret = resource_write_led(SENSOR_LED_CH, 0);
+ }
+
+ retv_if(ret != 0, -1);
+
+ return 0;
+}
+
static bool
handle_get_motion(smartthings_payload_h resp_payload, void *user_data)
{
@@ -114,8 +134,10 @@ handle_get_motion(smartthings_payload_h resp_payload, void *user_data)
retv_if(!ad, false);
+ _D("Received a GET request for MOTION");
+
sensor_data_get_bool(ad->motion_data, &value);
- smartthings_payload_set_bool(resp_payload, SENSOR_MOTION_KEY, value);
+ smartthings_payload_set_bool(resp_payload, SENSOR_MOTION_KEY, ad->motion_data);
_D("Value : %d", value);
@@ -127,43 +149,52 @@ handle_get_led(smartthings_payload_h resp_payload, void *user_data)
{
app_data *ad = user_data;
const char *str = NULL;
+ int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
retv_if(!ad, false);
- sensor_data_get_string(ad->led_data, &str);
+ _D("Received a GET request for LED");
- if (!str) {
- str = SENSOR_LED_INIT;
- }
+ error = smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, ad->led_data);
+ if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
+ _E("smartthings_resource_notify() failed, [%s]",
+ __resource_error_to_str(error));
- smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, str);
_D("Power : %s", str);
-// free(str);
-
return true;
}
static bool
-handle_set_led(smartthings_payload_h resp_payload, void *user_data)
+handle_set_led(smartthings_payload_h payload, smartthings_payload_h resp_payload, void *user_data)
{
app_data *ad = user_data;
- const char *str = NULL;
+ char *str = NULL;
int ret = 0;
+ int error = SMARTTHINGS_RESOURCE_ERROR_NONE;
retv_if(!ad, false);
- _D("set [%s:%s] == %s", SENSOR_LED_URI, SENSOR_LED_KEY, str);
- smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, str);
+ _D("Received a SET request");
+
+ smartthings_payload_get_string(payload, SENSOR_LED_KEY, &str);
+ if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
+ _E("smartthings_resource_notify() failed, [%s]",
+ __resource_error_to_str(error));
-// ret = __change_led_data(ad, strdup(str));
+ ret = __change_led_data(ad, strdup(str));
retv_if(ret != 0, false);
-// free(str);
+ free(str);
- return true;
+ error = smartthings_payload_set_string(resp_payload, SENSOR_LED_KEY, ad->led_data);
+ if (error != SMARTTHINGS_RESOURCE_ERROR_NONE)
+ _E("smartthings_resource_notify() failed, [%s]",
+ __resource_error_to_str(error));
+
+ return true;
}
static void
@@ -192,7 +223,7 @@ _request_cb(smartthings_resource_h handle, int req_id,
_E("No matching Resource uri to get");
} else if (req_type == SMARTTHINGS_RESOURCE_REQUEST_SET) {
if (0 == g_strcmp0(uri, SENSOR_LED_URI))
- result = handle_set_led(resp_payload, user_data);
+ result = handle_set_led(payload, resp_payload, user_data);
else
_E("No matching Resource uri to get");
} else {
@@ -602,35 +633,10 @@ static gboolean __change_motion_sensor_data(gpointer user_data)
// Notify observers of the Motion sensor resource
st_thing_notify_resource(ad, SENSOR_MOTION_URI, SENSOR_MOTION_KEY, ad->motion_data);
#endif
-
- return TRUE;
+ return FALSE;
+// return TRUE;
}
-//static int __change_led_data(void *data, char *state) {
-// int ret = 0;
-// app_data *ad = data;
-//
-// retv_if(!ad, -1);
-// retv_if(!ad->led_data, -1);
-//
-// sensor_data_set_string(ad->led_data, state, strlen(state));
-//
-// if (0 == strcmp(state, LED_ON)) {
-// ret = resource_write_led(SENSOR_LED_CH, 1);
-// } else {
-// ret = resource_write_led(SENSOR_LED_CH, 0);
-// }
-//
-// retv_if(ret != 0, -1);
-//
-//#ifdef USE_ST_SDK
-// // Notify observers of the LED resource
-// st_thing_notify_resource(ad, SENSOR_LED_URI, SENSOR_LED_KEY, ad->motion_data);
-//#endif
-//
-// return 0;
-//}
-
static void gathering_stop(void *data)
{
app_data *ad = data;
@@ -646,7 +652,9 @@ static void gathering_start(void *data)
{
app_data *ad = data;
ret_if(!ad);
+
ad->getter_motion = g_timeout_add(SENSOR_GATHER_INTERVAL, __change_motion_sensor_data, ad);
+
if (!ad->getter_motion)
_E("Failed to add getter_motion");
}
@@ -698,7 +706,7 @@ static void service_app_terminate(void *user_data)
#endif
// Turn off LED light with __set_led()
-// __change_led_data(ad, LED_OFF);
+ __change_led_data(ad, LED_OFF);
// Free sensor Motion & LED data
sensor_data_free(ad->motion_data);