summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/webutil.h6
-rw-r--r--src/webutil.c133
2 files changed, 131 insertions, 8 deletions
diff --git a/inc/webutil.h b/inc/webutil.h
index d8c3ae2..2d3e437 100644
--- a/inc/webutil.h
+++ b/inc/webutil.h
@@ -70,8 +70,14 @@ int web_util_noti_post(const char *resource, const char *json_data);
int web_util_json_init(void);
int web_util_json_fini(void);
+int web_util_json_begin(void);
+int web_util_json_end(void);
int web_util_json_data_array_begin(void);
int web_util_json_data_array_end(void);
+int web_util_json_add_int(const char* key, long long int value);
+int web_util_json_add_double(const char* key, double value);
+int web_util_json_add_boolean(const char* key, bool value);
+int web_util_json_add_string(const char* key, const char *value);
int web_util_json_add_sensor_data(const char* sensorpi_id, web_util_sensor_data_s *sensor_data);
char *web_util_get_json_string(void);
diff --git a/src/webutil.c b/src/webutil.c
index d1fed79..ea5ae9b 100644
--- a/src/webutil.c
+++ b/src/webutil.c
@@ -141,7 +141,7 @@ int web_util_json_fini(void)
return 0;
}
-int web_util_json_data_array_begin(void)
+int web_util_json_begin(void)
{
retv_if(Json_h.builder == NULL, -1);
retv_if(Json_h.is_begin == true, -1);
@@ -149,13 +149,131 @@ int web_util_json_data_array_begin(void)
Json_h.is_begin = true;
- /*
- {
- SensorsDataList : [ SensorData ]
+ json_builder_begin_object(Json_h.builder);
+
+ return 0;
+}
+
+int web_util_json_end(void)
+{
+ retv_if(Json_h.builder == NULL, -1);
+ retv_if(Json_h.is_begin == false, -1);
+ retv_if(Json_h.is_end == true, -1);
+
+ json_builder_end_object(Json_h.builder);
+ Json_h.is_end = true;
+
+ return 0;
+}
+
+int web_util_json_add_int(const char* key, long long int value)
+{
+ retv_if(!key, -1);
+
+ if (Json_h.builder == NULL) {
+ _E("Handle for json is not initialized, call web_util_json_init() first");
+ return -1;
}
- */
- json_builder_begin_object(Json_h.builder);
+ if (Json_h.is_begin == false) {
+ _E("json object has not begun, call web_util_json_begin() first");
+ return -1;
+ }
+
+ if (Json_h.is_end == true) {
+ _E("json object has already ended, call web_util_json_begin() first");
+ return -1;
+ }
+
+ json_builder_set_member_name(Json_h.builder, key);
+ json_builder_add_int_value(Json_h.builder, value);
+
+ return 0;
+}
+
+int web_util_json_add_double(const char* key, double value)
+{
+ retv_if(!key, -1);
+
+ if (Json_h.builder == NULL) {
+ _E("Handle for json is not initialized, call web_util_json_init() first");
+ return -1;
+ }
+
+ if (Json_h.is_begin == false) {
+ _E("json object has not begun, call web_util_json_begin() first");
+ return -1;
+ }
+
+ if (Json_h.is_end == true) {
+ _E("json object has already ended, call web_util_json_begin() first");
+ return -1;
+ }
+
+ json_builder_set_member_name(Json_h.builder, key);
+ json_builder_add_double_value(Json_h.builder, value);
+
+ return 0;
+}
+
+int web_util_json_add_boolean(const char* key, bool value)
+{
+ retv_if(!key, -1);
+
+ if (Json_h.builder == NULL) {
+ _E("Handle for json is not initialized, call web_util_json_init() first");
+ return -1;
+ }
+
+ if (Json_h.is_begin == false) {
+ _E("json object has not begun, call web_util_json_begin() first");
+ return -1;
+ }
+
+ if (Json_h.is_end == true) {
+ _E("json object has already ended, call web_util_json_begin() first");
+ return -1;
+ }
+
+ json_builder_set_member_name(Json_h.builder, key);
+ json_builder_add_boolean_value(Json_h.builder, value);
+
+ return 0;
+}
+
+int web_util_json_add_string(const char* key, const char *value)
+{
+ retv_if(!key, -1);
+
+ if (Json_h.builder == NULL) {
+ _E("Handle for json is not initialized, call web_util_json_init() first");
+ return -1;
+ }
+
+ if (Json_h.is_begin == false) {
+ _E("json object has not begun, call web_util_json_begin() first");
+ return -1;
+ }
+
+ if (Json_h.is_end == true) {
+ _E("json object has already ended, call web_util_json_begin() first");
+ return -1;
+ }
+
+ json_builder_set_member_name(Json_h.builder, key);
+ json_builder_add_string_value(Json_h.builder, value);
+
+ return 0;
+}
+
+int web_util_json_data_array_begin(void)
+{
+ int ret = 0;
+ retv_if(Json_h.builder == NULL, -1);
+
+ ret = web_util_json_begin();
+ retv_if(ret, -1);
+
json_builder_set_member_name(Json_h.builder, "SensorDataList");
json_builder_begin_array(Json_h.builder);
@@ -169,8 +287,7 @@ int web_util_json_data_array_end(void)
retv_if(Json_h.is_end == true, -1);
json_builder_end_array(Json_h.builder);
- json_builder_end_object(Json_h.builder);
- Json_h.is_end = true;
+ web_util_json_end();
return 0;
}