From 633b7aa758e3de47bb3c84d5f3594b1ba9b8e490 Mon Sep 17 00:00:00 2001 From: Jeonghoon Park Date: Wed, 27 Jun 2018 19:45:41 +0900 Subject: [PATCH] [things-lib] storing timebased data to json object and add reset API to reuse timebased data Change-Id: I1cbac84da62f9c15f7ab75d152b20b5c8b80345f --- lib/things-service/include/things-service.h | 6 +- lib/things-service/src/things-service.c | 277 ++++++++++++---------------- 2 files changed, 126 insertions(+), 157 deletions(-) diff --git a/lib/things-service/include/things-service.h b/lib/things-service/include/things-service.h index 792ec5e..5a5ff11 100644 --- a/lib/things-service/include/things-service.h +++ b/lib/things-service/include/things-service.h @@ -31,8 +31,12 @@ int things_service_init(ts_handle *handle, const char *project); int things_service_fini(ts_handle handle); timebased_data *things_service_timebased_data_new(ts_value_type_e type); +int things_service_timebased_data_reset( + timebased_data *tb_data, ts_value_type_e type); void things_service_timebased_data_free(timebased_data *tb_data); + int things_service_timebased_data_get_length(timebased_data *tb_data); + int things_service_timebased_data_append_int( timebased_data *tb_data, int ivalue); int things_service_timebased_data_append_double( @@ -40,6 +44,6 @@ int things_service_timebased_data_append_double( int things_service_send_data(ts_handle handle, const char *json_data); int things_service_send_timebased_data( - ts_handle handle, timebased_data *tb_data); + ts_handle handle, timebased_data *tb_data, const char *project_key); #endif /* __LIB_THINGS_SERVICE_H__ */ diff --git a/lib/things-service/src/things-service.c b/lib/things-service/src/things-service.c index ab45b7c..517b499 100644 --- a/lib/things-service/src/things-service.c +++ b/lib/things-service/src/things-service.c @@ -27,6 +27,8 @@ #include "common-app-inf.h" #include "common-util.h" +#define TEST_DEVICE_ID "test-page-device" + struct _ts_data { char *app_id; char *token; @@ -40,140 +42,15 @@ struct _ts_data { int accept_thread_running; }; -struct __timebased_int { - gint64 i_time; - gint value; -}; - -struct __timebased_double { - gint64 i_time; - gdouble value; -}; - struct __timebased_data { ts_value_type_e type; unsigned int length; - GList *v_list; + struct json_object *arr_obj; }; static void __quit_n_join_accept_thread(ts_handle handle); static int _create_accept_thread(ts_handle handle); -static void __timebase_value_free(gpointer data) -{ - g_free(data); -} - -static void -__add_timebased_int_to_json_object(gpointer data, gpointer user_data) -{ - struct __timebased_int *tb_int = data; - struct json_object *a_obj = user_data; - struct json_object *obj = NULL; - struct json_object *value_o = NULL; - struct json_object *time_o = NULL; - - ret_if(!tb_int); - ret_if(!a_obj); - - obj = json_object_new_object(); - ret_if(!obj); - - time_o = json_object_new_int64(tb_int->i_time); - goto_if(!time_o, ERROR); - json_object_object_add(obj, "x", time_o); - - value_o = json_object_new_int64(tb_int->value); - goto_if(!value_o, ERROR); - json_object_object_add(obj, "y", value_o); - - json_object_array_add(a_obj, obj); - return; - -ERROR: - json_object_put(obj); - return; -} - -static void -__add_timebased_double_to_json_object(gpointer data, gpointer user_data) -{ - struct __timebased_double *tb_double = data; - struct json_object *a_obj = user_data; - struct json_object *obj = NULL; - struct json_object *value_o = NULL; - struct json_object *time_o = NULL; - - ret_if(!tb_double); - ret_if(!a_obj); - - obj = json_object_new_object(); - ret_if(!obj); - - time_o = json_object_new_int64(tb_double->i_time); - goto_if(!time_o, ERROR); - json_object_object_add(obj, "x", time_o); - - value_o = json_object_new_double(tb_double->value); - goto_if(!value_o, ERROR); - json_object_object_add(obj, "y", value_o); - - json_object_array_add(a_obj, obj); - - return; - -ERROR: - json_object_put(obj); - return; -} - -static char *__timebased_data_to_json(timebased_data *tb_data) -{ - struct json_object *obj = NULL; - struct json_object *a_obj = NULL; - char *json_str = NULL; - - retv_if(!tb_data, NULL); - retvm_if(!((tb_data->type == TS_VALUE_TYPE_INT) || - (tb_data->type == TS_VALUE_TYPE_DOUBLE)), - NULL, "invalid type[%d]", tb_data->type); - - obj = json_object_new_object(); - retv_if(!obj, NULL); - - a_obj = json_object_new_array(); - goto_if(!a_obj, ERROR); - json_object_object_add(obj, "data", a_obj); - - switch (tb_data->type) { - case TS_VALUE_TYPE_INT: - g_list_foreach(tb_data->v_list, - __add_timebased_int_to_json_object, a_obj); - break; - case TS_VALUE_TYPE_DOUBLE: - g_list_foreach(tb_data->v_list, - __add_timebased_double_to_json_object, a_obj); - break; - default: - _E("invalid type [%d]", tb_data->type); - goto ERROR; - break; - } - - json_str = g_strdup(json_object_to_json_string(obj)); - - if (obj) - json_object_put(obj); - - return json_str; - -ERROR: - if (obj) - json_object_put(obj); - - return NULL; -} - static char *_get_socket_addr_name(const char *appID, const char *token) { return common_make_socket_addr_name(token, appID, (guint)getpid()); @@ -519,27 +396,46 @@ int things_service_send_data(ts_handle handle, const char *json_data) timebased_data *things_service_timebased_data_new(ts_value_type_e type) { - timebased_data *t_data = NULL; + timebased_data *tb_data = NULL; retvm_if(!((type == TS_VALUE_TYPE_INT) || (type == TS_VALUE_TYPE_DOUBLE)), NULL, "invalid type[%d]", type); - t_data = g_try_malloc0(sizeof(timebased_data)); - if (!t_data) - return NULL; + tb_data = g_try_malloc(sizeof(timebased_data)); + retv_if(!tb_data, NULL); + + tb_data->type = type; + tb_data->length = 0; + tb_data->arr_obj = NULL; + + return tb_data; +} + +int things_service_timebased_data_reset( + timebased_data *tb_data, ts_value_type_e type) +{ + retv_if(!tb_data, -1); + + retvm_if(!((type == TS_VALUE_TYPE_INT) || (type == TS_VALUE_TYPE_DOUBLE)), + -1, "invalid type[%d]", type); - t_data->type = type; - t_data->length = 0; + tb_data->type = type; + tb_data->length = 0; - return t_data; + if (tb_data->arr_obj) { + json_object_put(tb_data->arr_obj); + tb_data->arr_obj = NULL; + } + return 0; } + void things_service_timebased_data_free(timebased_data *tb_data) { ret_if(!tb_data); - if (tb_data->v_list) - g_list_free_full(tb_data->v_list, __timebase_value_free); + if (tb_data->arr_obj) + json_object_put(tb_data->arr_obj); g_free(tb_data); } @@ -554,61 +450,130 @@ int things_service_timebased_data_get_length(timebased_data *tb_data) int things_service_timebased_data_append_int(timebased_data *tb_data, int ivalue) { - struct __timebased_int *tb_int = NULL; + struct json_object *tmp_o = NULL; + struct json_object *time_o = NULL; + struct json_object *value_o = NULL; retv_if(!tb_data, -1); retv_if(tb_data->type != TS_VALUE_TYPE_INT, -1); - tb_int = g_try_malloc0(sizeof(struct __timebased_int)); - retv_if(!tb_int, -1); + if (!tb_data->arr_obj) { + struct json_object *obj = NULL; + obj = json_object_new_array(); + retvm_if(!obj, -1, "failed to create json arry object"); + tb_data->arr_obj = obj; + } + + tmp_o = json_object_new_object(); + retvm_if (!tmp_o, -1, "failed to create json object"); + + time_o = json_object_new_int64(common_get_epoch_coarse_time()); + goto_if(!time_o, ERROR); + value_o = json_object_new_int64(ivalue); + goto_if(!value_o, ERROR); - tb_int->i_time = common_get_epoch_coarse_time(); - tb_int->value = ivalue; + json_object_object_add(tmp_o, "x", time_o); + json_object_object_add(tmp_o, "y", value_o); + json_object_array_add(tb_data->arr_obj, tmp_o); - tb_data->v_list = g_list_append(tb_data->v_list, tb_int); tb_data->length++; return 0; + +ERROR: + if (tmp_o) + json_object_put(tmp_o); + + if (time_o) + json_object_put(time_o); + + return -1; } int things_service_timebased_data_append_double( timebased_data *tb_data, double dvalue) { - struct __timebased_double *tb_double = NULL; + struct json_object *tmp_o = NULL; + struct json_object *time_o = NULL; + struct json_object *value_o = NULL; retv_if(!tb_data, -1); retv_if(tb_data->type != TS_VALUE_TYPE_DOUBLE, -1); - tb_double = g_try_malloc0(sizeof(struct __timebased_double)); - retv_if(!tb_double, -1); + if (!tb_data->arr_obj) { + struct json_object *obj = NULL; + obj = json_object_new_array(); + retvm_if(!obj, -1, "failed to create json arry object"); + tb_data->arr_obj = obj; + } + + tmp_o = json_object_new_object(); + retvm_if (!tmp_o, -1, "failed to create json object"); + + time_o = json_object_new_int64(common_get_epoch_coarse_time()); + goto_if(!time_o, ERROR); + value_o = json_object_new_double(dvalue); + goto_if(!value_o, ERROR); - tb_double->i_time = common_get_epoch_coarse_time(); - tb_double->value = dvalue; + json_object_object_add(tmp_o, "x", time_o); + json_object_object_add(tmp_o, "y", value_o); + json_object_array_add(tb_data->arr_obj, tmp_o); - tb_data->v_list = g_list_append(tb_data->v_list, tb_double); tb_data->length++; return 0; + +ERROR: + if (tmp_o) + json_object_put(tmp_o); + + if (time_o) + json_object_put(time_o); + + return -1; } -int -things_service_send_timebased_data(ts_handle handle, timebased_data *tb_data) +int things_service_send_timebased_data( + ts_handle handle, timebased_data *tb_data, const char *project_key) { int ret = 0; - char *json_data = NULL; + struct json_object *msg_o = NULL; + struct json_object *key_o = NULL; retv_if(!handle, -1); retv_if(!tb_data, -1); + retv_if(!tb_data->arr_obj, -1); + retv_if(!project_key, -1); - json_data = __timebased_data_to_json(tb_data); - ret = __ts_send_data(handle, json_data); - g_free(json_data); + msg_o = json_object_new_object(); + retvm_if(!msg_o, -1, "failed to create json object for message"); - if (!ret) { /* if success to send data, remove all items in tb_data */ - g_list_free_full(tb_data->v_list, __timebase_value_free); - tb_data->v_list = NULL; + key_o = json_object_new_string(project_key); + if (!key_o) { + _E("failed to create json object for project_key"); + json_object_put(msg_o); + return -1; + } + + json_object_object_add(msg_o, "key", key_o); + json_object_object_add(msg_o, "data", tb_data->arr_obj); +#ifdef TEST_DEVICE_ID +/* only for test, SHOULD BE REMOVED after device auth method is applied on cloud */ + struct json_object *did_o = json_object_new_string(TEST_DEVICE_ID); + json_object_object_add(msg_o, "deviceId", did_o); +#endif /* TEST_DEVICE_ID */ + + ret = __ts_send_data(handle, json_object_to_json_string(msg_o)); + if (ret) { + _E("failed to send data"); + /* keep appended data */ + json_object_get(tb_data->arr_obj); + } else { /* if success to send data, remove all items in tb_data */ + tb_data->arr_obj = NULL; tb_data->length = 0; } + json_object_put(msg_o); + return ret; } -- 2.7.4