diff --git a/burn_id.sh b/burn_id.sh new file mode 100755 index 000000000..c9af810ee --- /dev/null +++ b/burn_id.sh @@ -0,0 +1,14 @@ +#!/bin/bash +baud_rate=921600 +controller="esp32s3" +# controller="esp32" + +# usb_port="/dev/ttyACM0" +usb_port="/dev/ttyUSB0" +fimrware_loc="firmware/id" + +id_bin="100002119.bin" +# id_bin="100005802.bin" + + +esptool.py -p $usb_port -b $baud_rate --before default_reset --after hard_reset --chip $controller write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x3b0000 $fimrware_loc/$id_bin diff --git a/components/trace/trace.h b/components/trace/trace.h index 728c18ec4..711ab0ed7 100644 --- a/components/trace/trace.h +++ b/components/trace/trace.h @@ -10,7 +10,7 @@ extern "C" { #endif -#define ENABLE_TRACE 0 +#define ENABLE_TRACE 1 void __dump(const char *file_name, uint32_t line, char *buffer_name, void *_buff, uint32_t ofs, uint32_t cnt); diff --git a/devices/device_0001_digitalOut_generic/device_0001_digitalOut_generic.c b/devices/device_0001_digitalOut_generic/device_0001_digitalOut_generic.c index 3d63f8a03..defccba05 100644 --- a/devices/device_0001_digitalOut_generic/device_0001_digitalOut_generic.c +++ b/devices/device_0001_digitalOut_generic/device_0001_digitalOut_generic.c @@ -5,26 +5,26 @@ #include "trace.h" #include "items.h" -#include "gpio_isr_service.h" #include "ezlopi_gpio.h" #include "ezlopi_cloud.h" #include "ezlopi_timer.h" #include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" -#include "gpio_isr_service.h" +#include "ezlopi_nvs.h" #include "ezlopi_gpio.h" #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" -#include "ezlopi_device_value_updated.h" -#include "ezlopi_cloud_constants.h" #include "ezlopi_valueformatter.h" -#include "ezlopi_nvs.h" +#include "ezlopi_cloud_constants.h" +#include "ezlopi_device_value_updated.h" +#include "gpio_isr_service.h" #include "device_0001_digitalOut_generic.h" // #define DEV_TEST_SETTINGS_EN @@ -34,9 +34,9 @@ static int __init(l_ezlopi_item_t *item); static int __set_value(l_ezlopi_item_t *item, void *arg); static int __get_value_cjson(l_ezlopi_item_t *item, void *arg); +static void __interrupt_upcall(void *arg); static void __toggle_gpio(l_ezlopi_item_t *item); static void __write_gpio_value(l_ezlopi_item_t *item); -static void __interrupt_upcall(l_ezlopi_item_t *item); static void __set_gpio_value(l_ezlopi_item_t *item, int value); #ifdef DEV_TEST_SETTINGS_EN @@ -142,16 +142,16 @@ static int __settings_get(void *arg, l_ezlopi_device_settings_v3_t *setting) cJSON_AddStringToObject(description, "text", "User defined operation mode, where user can set several distance parameters for setting a custom operation mode"); cJSON_AddStringToObject(description, "lang_tag", "ezlopi_presence_user_defined_mode_description"); - cJSON_AddItemToObject(cjson_propertise, "label", label); + cJSON_AddItemToObject(cjson_propertise, ezlopi_label_str, label); cJSON_AddItemToObject(cjson_propertise, "description", description); - cJSON_AddStringToObject(cjson_propertise, "valueType", "presence_operation_mode"); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueType_str, "presence_operation_mode"); cJSON_AddNumberToObject(value, "min_move_distance", 0.75); cJSON_AddNumberToObject(value, "max_move_distance", 1.5); cJSON_AddNumberToObject(value, "min_still_distance", 0.75); cJSON_AddNumberToObject(value, "max_still_distance", 1.5); cJSON_AddTrueToObject(value, "is_active"); - cJSON_AddItemToObject(cjson_propertise, "value", value); + cJSON_AddItemToObject(cjson_propertise, ezlopi_value_str, value); cJSON_AddNumberToObject(value_default, "min_move_distance", 0.75); cJSON_AddNumberToObject(value_default, "max_move_distance", 6.0); @@ -171,13 +171,13 @@ static int __settings_get(void *arg, l_ezlopi_device_settings_v3_t *setting) cJSON_AddStringToObject(description, "text", "This is PWM setting value for setting the backlight brightness"); cJSON_AddStringToObject(description, "lang_tag", "ezlopi_digitalio_pwm_setting_description"); - cJSON_AddItemToObject(cjson_propertise, "label", label); + cJSON_AddItemToObject(cjson_propertise, ezlopi_label_str, label); cJSON_AddItemToObject(cjson_propertise, "description", description); - cJSON_AddStringToObject(cjson_propertise, "valueType", "int"); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueType_str, "int"); s_digio_settings_t *settings_data = (s_digio_settings_t *)setting->user_arg; - cJSON_AddNumberToObject(cjson_propertise, "value", settings_data->settings_int_data); + cJSON_AddNumberToObject(cjson_propertise, ezlopi_value_str, settings_data->settings_int_data); cJSON_AddNumberToObject(cjson_propertise, "valueMin", 0); cJSON_AddNumberToObject(cjson_propertise, "valueMax", 100); cJSON_AddNumberToObject(cjson_propertise, "valueDefault", 50); @@ -200,7 +200,7 @@ static int __settings_set(void *arg, l_ezlopi_device_settings_v3_t *setting) else if (setting->cloud_properties.setting_id == settings_ids[1]) { int32_t value = 0; - CJSON_GET_VALUE_INT(cjson_propertise, "value", value); + CJSON_GET_VALUE_INT(cjson_propertise, ezlopi_value_str, value); TRACE_D("Setting Value : %d", value); s_digio_settings_t *settings_data = (s_digio_settings_t *)setting->user_arg; @@ -247,7 +247,7 @@ static int __settings_update(void *arg, l_ezlopi_device_settings_v3_t *setting) else if (setting->cloud_properties.setting_id == settings_ids[1]) { s_digio_settings_t *settings_data = (s_digio_settings_t *)setting->user_arg; - cJSON_AddNumberToObject(cjson_propertise, "value", settings_data->settings_int_data); + cJSON_AddNumberToObject(cjson_propertise, ezlopi_value_str, settings_data->settings_int_data); } } return ret; @@ -257,16 +257,18 @@ static int __settings_update(void *arg, l_ezlopi_device_settings_v3_t *setting) static void __setup_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cjson_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cjson_device, "dev_name", device_name); + // uint32_t device_id = 0; + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cjson_device, ezlopi_dev_name_str, device_name); + // CJSON_GET_ID(device_id, cJSON_GetObjectItem(cjson_device, ezlopi__id_str)); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + // ASSIGN_DEVICE_NAME_V2(device, device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_switch; device->cloud_properties.subcategory = subcategory_in_wall; device->cloud_properties.device_type = dev_type_switch_inwall; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) @@ -280,21 +282,21 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cjson_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_dev_type_str, item->interface_type); - CJSON_GET_VALUE_INT(cjson_device, "is_ip", item->interface.gpio.gpio_in.enable); - CJSON_GET_VALUE_INT(cjson_device, "gpio_in", item->interface.gpio.gpio_in.gpio_num); - CJSON_GET_VALUE_INT(cjson_device, "ip_inv", item->interface.gpio.gpio_in.invert); - CJSON_GET_VALUE_INT(cjson_device, "val_ip", item->interface.gpio.gpio_in.value); - CJSON_GET_VALUE_INT(cjson_device, "pullup_ip", tmp_var); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_is_ip_str, item->interface.gpio.gpio_in.enable); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_gpio_in_str, item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_ip_inv_str, item->interface.gpio.gpio_in.invert); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_val_ip_str, item->interface.gpio.gpio_in.value); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_pullup_ip_str, tmp_var); item->interface.gpio.gpio_in.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; item->interface.gpio.gpio_in.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_out.enable = true; - CJSON_GET_VALUE_INT(cjson_device, "gpio_out", item->interface.gpio.gpio_out.gpio_num); - CJSON_GET_VALUE_INT(cjson_device, "op_inv", item->interface.gpio.gpio_out.invert); - CJSON_GET_VALUE_INT(cjson_device, "val_op", item->interface.gpio.gpio_out.value); - CJSON_GET_VALUE_INT(cjson_device, "pullup_op", tmp_var); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_gpio_out_str, item->interface.gpio.gpio_out.gpio_num); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_op_inv_str, item->interface.gpio.gpio_out.invert); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_val_op_str, item->interface.gpio.gpio_out.value); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_pullup_op_str, tmp_var); item->interface.gpio.gpio_out.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_out.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; } @@ -314,7 +316,7 @@ static int __prepare(void *arg) cJSON *cjson_device = prep_arg->cjson_device; if (cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(cjson_device); if (device) { __setup_device_cloud_properties(device, cjson_device); @@ -398,6 +400,7 @@ static int __init(l_ezlopi_item_t *item) gpio_config(&io_conf); // digital_io_write_gpio_value(item); __write_gpio_value(item); + ret = 1; } } @@ -423,7 +426,19 @@ static int __init(l_ezlopi_item_t *item) gpio_config(&io_conf); gpio_isr_service_register_v3(item, __interrupt_upcall, 1000); + ret = 1; } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } + } + return ret; } @@ -433,8 +448,8 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) cJSON *cjson_propertise = (cJSON *)arg; if (cjson_propertise) { - cJSON_AddBoolToObject(cjson_propertise, "value", item->interface.gpio.gpio_out.value); - cJSON_AddStringToObject(cjson_propertise, "valueFormatted", ezlopi_valueformatter_bool(item->interface.gpio.gpio_out.value ? true : false)); + cJSON_AddBoolToObject(cjson_propertise, ezlopi_value_str, item->interface.gpio.gpio_out.value); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueFormatted_str, ezlopi_valueformatter_bool(item->interface.gpio.gpio_out.value ? true : false)); ret = 1; } @@ -443,7 +458,6 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) static void __set_gpio_value(l_ezlopi_item_t *item, int value) { - int temp_value = (0 == item->interface.gpio.gpio_out.invert) ? value : !(value); gpio_set_level(item->interface.gpio.gpio_out.gpio_num, value); item->interface.gpio.gpio_out.value = value; } @@ -455,15 +469,10 @@ static int __set_value(l_ezlopi_item_t *item, void *arg) if (NULL != cjson_params) { - char *cjson_params_str = cJSON_Print(cjson_params); - if (cjson_params) - { - TRACE_D("cjson_params: %s", cjson_params_str); - free(cjson_params_str); - } + CJSON_TRACE("cjson_params", cjson_params); int value = 0; - cJSON *cj_value = cJSON_GetObjectItem(cjson_params, "value"); + cJSON *cj_value = cJSON_GetObjectItem(cjson_params, ezlopi_value_str); if (cj_value) { switch (cj_value->type) @@ -531,10 +540,14 @@ static void __write_gpio_value(l_ezlopi_item_t *item) gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); } -static void __interrupt_upcall(l_ezlopi_item_t *item) +static void __interrupt_upcall(void *arg) { - __toggle_gpio(item); - ezlopi_device_value_updated_from_device_v3(item); + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; + if (item) + { + __toggle_gpio(item); + ezlopi_device_value_updated_from_device_v3(item); + } } static void __toggle_gpio(l_ezlopi_item_t *item) diff --git a/devices/device_0002_digitalOut_relay/device_0002_digitalOut_relay.c b/devices/device_0002_digitalOut_relay/device_0002_digitalOut_relay.c index d80983bbd..97ba07f41 100644 --- a/devices/device_0002_digitalOut_relay/device_0002_digitalOut_relay.c +++ b/devices/device_0002_digitalOut_relay/device_0002_digitalOut_relay.c @@ -10,6 +10,7 @@ #include "ezlopi_cloud.h" #include "ezlopi_timer.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -20,9 +21,9 @@ static int __init(l_ezlopi_item_t *item); static int __set_value(l_ezlopi_item_t *item, void *arg); static int __get_value_cjson(l_ezlopi_item_t *item, void *arg); +static void __interrupt_upcall(void *arg); static void __toggle_gpio(l_ezlopi_item_t *item); static void __write_gpio_value(l_ezlopi_item_t *item); -static void __interrupt_upcall(l_ezlopi_item_t *item); static void __set_gpio_value(l_ezlopi_item_t *item, int value); int device_0002_digitalOut_relay(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) @@ -64,16 +65,16 @@ int device_0002_digitalOut_relay(e_ezlopi_actions_t action, l_ezlopi_item_t *ite static void __setup_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cjson_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cjson_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cjson_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_switch; device->cloud_properties.subcategory = subcategory_relay; device->cloud_properties.device_type = dev_type_switch_inwall; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) @@ -87,21 +88,21 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cjson_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_dev_type_str, item->interface_type); - CJSON_GET_VALUE_INT(cjson_device, "is_ip", item->interface.gpio.gpio_in.enable); - CJSON_GET_VALUE_INT(cjson_device, "gpio_in", item->interface.gpio.gpio_in.gpio_num); - CJSON_GET_VALUE_INT(cjson_device, "ip_inv", item->interface.gpio.gpio_in.invert); - CJSON_GET_VALUE_INT(cjson_device, "val_ip", item->interface.gpio.gpio_in.value); - CJSON_GET_VALUE_INT(cjson_device, "pullup_ip", tmp_var); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_is_ip_str, item->interface.gpio.gpio_in.enable); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_gpio_in_str, item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_ip_inv_str, item->interface.gpio.gpio_in.invert); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_val_ip_str, item->interface.gpio.gpio_in.value); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_pullup_ip_str, tmp_var); item->interface.gpio.gpio_in.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; item->interface.gpio.gpio_in.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_out.enable = true; - CJSON_GET_VALUE_INT(cjson_device, "gpio_out", item->interface.gpio.gpio_out.gpio_num); - CJSON_GET_VALUE_INT(cjson_device, "op_inv", item->interface.gpio.gpio_out.invert); - CJSON_GET_VALUE_INT(cjson_device, "val_op", item->interface.gpio.gpio_out.value); - CJSON_GET_VALUE_INT(cjson_device, "pullup_op", tmp_var); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_gpio_out_str, item->interface.gpio.gpio_out.gpio_num); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_op_inv_str, item->interface.gpio.gpio_out.invert); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_val_op_str, item->interface.gpio.gpio_out.value); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_pullup_op_str, tmp_var); item->interface.gpio.gpio_out.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_out.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; } @@ -115,7 +116,7 @@ static int __prepare(void *arg) cJSON *cjson_device = prep_arg->cjson_device; if (cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(cjson_device); if (device) { __setup_device_cloud_properties(device, cjson_device); @@ -161,6 +162,7 @@ static int __init(l_ezlopi_item_t *item) gpio_config(&io_conf); // digital_io_write_gpio_value(item); __write_gpio_value(item); + ret = 1; } } @@ -186,7 +188,19 @@ static int __init(l_ezlopi_item_t *item) gpio_config(&io_conf); gpio_isr_service_register_v3(item, __interrupt_upcall, 1000); + ret = 1; + } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } } + return ret; } @@ -196,8 +210,8 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) cJSON *cjson_propertise = (cJSON *)arg; if (cjson_propertise) { - cJSON_AddBoolToObject(cjson_propertise, "value", item->interface.gpio.gpio_out.value); - cJSON_AddStringToObject(cjson_propertise, "valueFormatted", ezlopi_valueformatter_bool(item->interface.gpio.gpio_out.value ? true : false)); + cJSON_AddBoolToObject(cjson_propertise, ezlopi_value_str, item->interface.gpio.gpio_out.value); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueFormatted_str, ezlopi_valueformatter_bool(item->interface.gpio.gpio_out.value ? true : false)); ret = 1; } @@ -206,7 +220,6 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) static void __set_gpio_value(l_ezlopi_item_t *item, int value) { - int temp_value = (0 == item->interface.gpio.gpio_out.invert) ? value : !(value); gpio_set_level(item->interface.gpio.gpio_out.gpio_num, value); item->interface.gpio.gpio_out.value = value; } @@ -218,15 +231,10 @@ static int __set_value(l_ezlopi_item_t *item, void *arg) if (NULL != cjson_params) { - char *cjson_params_str = cJSON_Print(cjson_params); - if (cjson_params) - { - TRACE_D("cjson_params: %s", cjson_params_str); - free(cjson_params_str); - } + CJSON_TRACE("cjson_params", cjson_params); int value = 0; - cJSON *cj_value = cJSON_GetObjectItem(cjson_params, "value"); + cJSON *cj_value = cJSON_GetObjectItem(cjson_params, ezlopi_value_str); if (cj_value) { switch (cj_value->type) @@ -292,18 +300,22 @@ static int __set_value(l_ezlopi_item_t *item, void *arg) static void __write_gpio_value(l_ezlopi_item_t *item) { uint32_t write_value = (0 == item->interface.gpio.gpio_out.invert) ? item->interface.gpio.gpio_out.value : (item->interface.gpio.gpio_out.value ? 0 : 1); - esp_err_t error = gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); + gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); } -static void __interrupt_upcall(l_ezlopi_item_t *item) +static void __interrupt_upcall(void *arg) { - __toggle_gpio(item); - ezlopi_device_value_updated_from_device_v3(item); + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; + if (item) + { + __toggle_gpio(item); + ezlopi_device_value_updated_from_device_v3(item); + } } static void __toggle_gpio(l_ezlopi_item_t *item) { uint32_t write_value = !(item->interface.gpio.gpio_out.value); - esp_err_t error = gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); + gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); item->interface.gpio.gpio_out.value = write_value; } diff --git a/devices/device_0003_digitalOut_plug/device_0003_digitalOut_plug.c b/devices/device_0003_digitalOut_plug/device_0003_digitalOut_plug.c index cb15d972c..a9e66545a 100644 --- a/devices/device_0003_digitalOut_plug/device_0003_digitalOut_plug.c +++ b/devices/device_0003_digitalOut_plug/device_0003_digitalOut_plug.c @@ -11,6 +11,7 @@ #include "ezlopi_timer.h" #include "ezlopi_cloud.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -21,12 +22,11 @@ static int __init(l_ezlopi_item_t *item); static int __set_value(l_ezlopi_item_t *item, void *arg); static int __get_value_cjson(l_ezlopi_item_t *item, void *arg); +static void __interrupt_upcall(void *arg); static void __toggle_gpio(l_ezlopi_item_t *item); static void __write_gpio_value(l_ezlopi_item_t *item); -static void __interrupt_upcall(l_ezlopi_item_t *item); static void __set_gpio_value(l_ezlopi_item_t *item, int value); - int device_0003_digitalOut_plug(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) { int ret = 0; @@ -66,16 +66,16 @@ int device_0003_digitalOut_plug(e_ezlopi_actions_t action, l_ezlopi_item_t *item static void __setup_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cjson_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cjson_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cjson_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_switch; device->cloud_properties.subcategory = subcategory_relay; device->cloud_properties.device_type = dev_type_switch_inwall; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) @@ -89,21 +89,21 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cjson_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_dev_type_str, item->interface_type); - CJSON_GET_VALUE_INT(cjson_device, "is_ip", item->interface.gpio.gpio_in.enable); - CJSON_GET_VALUE_INT(cjson_device, "gpio_in", item->interface.gpio.gpio_in.gpio_num); - CJSON_GET_VALUE_INT(cjson_device, "ip_inv", item->interface.gpio.gpio_in.invert); - CJSON_GET_VALUE_INT(cjson_device, "val_ip", item->interface.gpio.gpio_in.value); - CJSON_GET_VALUE_INT(cjson_device, "pullup_ip", tmp_var); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_is_ip_str, item->interface.gpio.gpio_in.enable); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_gpio_in_str, item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_ip_inv_str, item->interface.gpio.gpio_in.invert); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_val_ip_str, item->interface.gpio.gpio_in.value); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_pullup_ip_str, tmp_var); item->interface.gpio.gpio_in.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; item->interface.gpio.gpio_in.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_out.enable = true; - CJSON_GET_VALUE_INT(cjson_device, "gpio_out", item->interface.gpio.gpio_out.gpio_num); - CJSON_GET_VALUE_INT(cjson_device, "op_inv", item->interface.gpio.gpio_out.invert); - CJSON_GET_VALUE_INT(cjson_device, "val_op", item->interface.gpio.gpio_out.value); - CJSON_GET_VALUE_INT(cjson_device, "pullup_op", tmp_var); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_gpio_out_str, item->interface.gpio.gpio_out.gpio_num); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_op_inv_str, item->interface.gpio.gpio_out.invert); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_val_op_str, item->interface.gpio.gpio_out.value); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_pullup_op_str, tmp_var); item->interface.gpio.gpio_out.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_out.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; } @@ -117,7 +117,7 @@ static int __prepare(void *arg) cJSON *cjson_device = prep_arg->cjson_device; if (cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(cjson_device); if (device) { __setup_device_cloud_properties(device, cjson_device); @@ -163,6 +163,7 @@ static int __init(l_ezlopi_item_t *item) gpio_config(&io_conf); // digital_io_write_gpio_value(item); __write_gpio_value(item); + ret = 1; } } @@ -188,7 +189,19 @@ static int __init(l_ezlopi_item_t *item) gpio_config(&io_conf); gpio_isr_service_register_v3(item, __interrupt_upcall, 1000); + ret = 1; + } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } } + return ret; } @@ -198,8 +211,8 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) cJSON *cjson_propertise = (cJSON *)arg; if (cjson_propertise) { - cJSON_AddBoolToObject(cjson_propertise, "value", item->interface.gpio.gpio_out.value); - cJSON_AddStringToObject(cjson_propertise, "valueFormatted", ezlopi_valueformatter_bool(item->interface.gpio.gpio_out.value ? true : false)); + cJSON_AddBoolToObject(cjson_propertise, ezlopi_value_str, item->interface.gpio.gpio_out.value); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueFormatted_str, ezlopi_valueformatter_bool(item->interface.gpio.gpio_out.value ? true : false)); ret = 1; } @@ -208,7 +221,6 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) static void __set_gpio_value(l_ezlopi_item_t *item, int value) { - int temp_value = (0 == item->interface.gpio.gpio_out.invert) ? value : !(value); gpio_set_level(item->interface.gpio.gpio_out.gpio_num, value); item->interface.gpio.gpio_out.value = value; } @@ -220,15 +232,10 @@ static int __set_value(l_ezlopi_item_t *item, void *arg) if (NULL != cjson_params) { - char *cjson_params_str = cJSON_Print(cjson_params); - if (cjson_params) - { - TRACE_D("cjson_params: %s", cjson_params_str); - free(cjson_params_str); - } + CJSON_TRACE("cjson_params", cjson_params); int value = 0; - cJSON *cj_value = cJSON_GetObjectItem(cjson_params, "value"); + cJSON *cj_value = cJSON_GetObjectItem(cjson_params, ezlopi_value_str); if (cj_value) { switch (cj_value->type) @@ -293,18 +300,22 @@ static int __set_value(l_ezlopi_item_t *item, void *arg) static void __write_gpio_value(l_ezlopi_item_t *item) { uint32_t write_value = (0 == item->interface.gpio.gpio_out.invert) ? item->interface.gpio.gpio_out.value : (item->interface.gpio.gpio_out.value ? 0 : 1); - esp_err_t error = gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); + gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); } -static void __interrupt_upcall(l_ezlopi_item_t *item) +static void __interrupt_upcall(void *arg) { - __toggle_gpio(item); - ezlopi_device_value_updated_from_device_v3(item); + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; + if (item) + { + __toggle_gpio(item); + ezlopi_device_value_updated_from_device_v3(item); + } } static void __toggle_gpio(l_ezlopi_item_t *item) { uint32_t write_value = !(item->interface.gpio.gpio_out.value); - esp_err_t error = gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); + gpio_set_level(item->interface.gpio.gpio_out.gpio_num, write_value); item->interface.gpio.gpio_out.value = write_value; } diff --git a/devices/device_0004_digitalIn_generic/device_0004_digitalIn_generic.c b/devices/device_0004_digitalIn_generic/device_0004_digitalIn_generic.c index ebb670f0b..314454e5a 100644 --- a/devices/device_0004_digitalIn_generic/device_0004_digitalIn_generic.c +++ b/devices/device_0004_digitalIn_generic/device_0004_digitalIn_generic.c @@ -11,6 +11,7 @@ #include "ezlopi_cloud.h" #include "ezlopi_actions.h" #include "gpio_isr_service.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -18,13 +19,9 @@ static int __prepare(void *arg); static int __init(l_ezlopi_item_t *item); -static int __set_value(l_ezlopi_item_t *item, void *arg); +static void __interrupt_upcall(void *arg); static int __get_value_cjson(l_ezlopi_item_t *item, void *arg); -static void __write_gpio_value(l_ezlopi_item_t *item); -static void __interrupt_upcall(l_ezlopi_item_t *item); -static void __set_gpio_value(l_ezlopi_item_t *item, int value); - int device_0004_digitalIn_generic(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) { int ret = 0; @@ -64,16 +61,16 @@ int device_0004_digitalIn_generic(e_ezlopi_actions_t action, l_ezlopi_item_t *it static void __setup_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cjson_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cjson_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cjson_device, ezlopi_dev_name_str, device_name); + // // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_switch; device->cloud_properties.subcategory = subcategory_in_wall; device->cloud_properties.device_type = dev_type_switch_inwall; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) @@ -88,8 +85,8 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cjson_device) item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->interface.gpio.gpio_in.enable = true; - CJSON_GET_VALUE_INT(cjson_device, "dev_type", item->interface_type); - CJSON_GET_VALUE_INT(cjson_device, "gpio", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_dev_type_str, item->interface_type); + CJSON_GET_VALUE_INT(cjson_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); CJSON_GET_VALUE_INT(cjson_device, "logic_inv", item->interface.gpio.gpio_in.invert); CJSON_GET_VALUE_INT(cjson_device, "pull_up", tmp_var); item->interface.gpio.gpio_in.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; @@ -105,7 +102,7 @@ static int __prepare(void *arg) cJSON *cjson_device = prep_arg->cjson_device; if (cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(cjson_device); if (device) { __setup_device_cloud_properties(device, cjson_device); @@ -153,7 +150,20 @@ static int __init(l_ezlopi_item_t *item) gpio_config(&io_conf); gpio_isr_service_register_v3(item, __interrupt_upcall, 1000); + + ret = 1; } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } + } + return ret; } @@ -163,16 +173,20 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) cJSON *cjson_propertise = (cJSON *)arg; if (cjson_propertise) { - cJSON_AddBoolToObject(cjson_propertise, "value", item->interface.gpio.gpio_in.value); - cJSON_AddStringToObject(cjson_propertise, "valueFormatted", ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? true : false)); + cJSON_AddBoolToObject(cjson_propertise, ezlopi_value_str, item->interface.gpio.gpio_in.value); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueFormatted_str, ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? true : false)); ret = 1; } return ret; } -static void __interrupt_upcall(l_ezlopi_item_t *item) +static void __interrupt_upcall(void *arg) { - item->interface.gpio.gpio_in.value = !item->interface.gpio.gpio_in.value; - ezlopi_device_value_updated_from_device_v3(item); + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; + if (item) + { + item->interface.gpio.gpio_in.value = !item->interface.gpio.gpio_in.value; + ezlopi_device_value_updated_from_device_v3(item); + } } diff --git a/devices/device_0009_other_RMT_SK6812/device_0009_other_RMT_SK6812.c b/devices/device_0009_other_RMT_SK6812/device_0009_other_RMT_SK6812.c index 350116cb6..63122daad 100644 --- a/devices/device_0009_other_RMT_SK6812/device_0009_other_RMT_SK6812.c +++ b/devices/device_0009_other_RMT_SK6812/device_0009_other_RMT_SK6812.c @@ -13,6 +13,7 @@ #include "ezlopi_i2c_master.h" #include "ezlopi_spi_master.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -95,7 +96,7 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_rgbcolor == item->cloud_properties.item_name) { - cJSON *color_json = cJSON_AddObjectToObject(cjson_properties, "value"); + cJSON *color_json = cJSON_AddObjectToObject(cjson_properties, ezlopi_value_str); if (color_json) { int green = sk6812_strip->buf[0]; @@ -106,24 +107,30 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) cJSON_AddNumberToObject(color_json, "blue", blue); cJSON_AddNumberToObject(color_json, "cwhite", ((red << 16) | (green << 8) | (blue))); char *formatted_val = ezlopi_valueformatter_rgb(red, green, blue); - cJSON_AddStringToObject(cjson_properties, "valueFormatted", formatted_val); - free(formatted_val); + if (formatted_val) + { + cJSON_AddStringToObject(cjson_properties, ezlopi_valueFormatted_str, formatted_val); + free(formatted_val); + } } } else if (ezlopi_item_name_dimmer == item->cloud_properties.item_name) { item->interface.pwm.duty_cycle = (int)ceil(((sk6812_strip->brightness * 100.0) / 255.0)); - cJSON_AddNumberToObject(cjson_properties, "value", item->interface.pwm.duty_cycle); + cJSON_AddNumberToObject(cjson_properties, ezlopi_value_str, item->interface.pwm.duty_cycle); char *formatted_val = ezlopi_valueformatter_int32(item->interface.pwm.duty_cycle); - cJSON_AddStringToObject(cjson_properties, "valueFormatted", formatted_val); - free(formatted_val); + if (formatted_val) + { + cJSON_AddStringToObject(cjson_properties, ezlopi_valueFormatted_str, formatted_val); + free(formatted_val); + } } else if (ezlopi_item_name_switch == item->cloud_properties.item_name) { item->interface.gpio.gpio_in.value = (0 == sk6812_strip->brightness) ? 0 : 1; - cJSON_AddBoolToObject(cjson_properties, "value", item->interface.gpio.gpio_in.value); + cJSON_AddBoolToObject(cjson_properties, ezlopi_value_str, item->interface.gpio.gpio_in.value); char *formatted_val = ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? true : false); - cJSON_AddStringToObject(cjson_properties, "valueFormatted", formatted_val); + cJSON_AddStringToObject(cjson_properties, ezlopi_valueFormatted_str, formatted_val); } } return ret; @@ -142,7 +149,7 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_rgbcolor == item->cloud_properties.item_name) { - cJSON *cjson_params_color_values = cJSON_GetObjectItem(cjson_params, "value"); + cJSON *cjson_params_color_values = cJSON_GetObjectItem(cjson_params, ezlopi_value_str); rgb_t color = { .red = 0, @@ -162,7 +169,7 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) else if (ezlopi_item_name_dimmer == item->cloud_properties.item_name) { int dimmable_value_percentage = 0; - CJSON_GET_VALUE_INT(cjson_params, "value", dimmable_value_percentage); + CJSON_GET_VALUE_INT(cjson_params, ezlopi_value_str, dimmable_value_percentage); int dimmable_value = (int)((dimmable_value_percentage * 255) / 100); led_strip_set_brightness(&dimmer_args->sk6812_strip, dimmable_value); led_strip_flush(&dimmer_args->sk6812_strip); @@ -172,7 +179,7 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) else if (ezlopi_item_name_switch == item->cloud_properties.item_name) { int led_state = 0; - CJSON_GET_VALUE_INT(cjson_params, "value", led_state); + CJSON_GET_VALUE_INT(cjson_params, ezlopi_value_str, led_state); dimmer_args->previous_brightness = (false == led_state) ? dimmer_args->sk6812_strip.brightness : dimmer_args->previous_brightness; int brightness_value = (false == led_state) ? 0 : ((0 == dimmer_args->previous_brightness) ? 255 : dimmer_args->previous_brightness); led_strip_set_brightness(&dimmer_args->sk6812_strip, brightness_value); @@ -216,37 +223,50 @@ static int __init(l_ezlopi_item_t *item) .blue = 255, }; - err |= led_strip_fill(&dimmer_args->sk6812_strip, 0, &dimmer_args->sk6812_strip.length, color); - err |= led_strip_set_brightness(&dimmer_args->sk6812_strip, 255); - err |= led_strip_flush(&dimmer_args->sk6812_strip); - - if (ESP_OK == err) + err |= led_strip_fill(&dimmer_args->sk6812_strip, 0, dimmer_args->sk6812_strip.length, color); + if (ESP_OK == (err = led_strip_set_brightness(&dimmer_args->sk6812_strip, 255))) { - dimmer_args->sk6812_led_strip_initialized = true; + if (ESP_OK == (err = led_strip_flush(&dimmer_args->sk6812_strip))) + { + ret = 1; + dimmer_args->sk6812_led_strip_initialized = true; + } } } if (ESP_OK != err) { - ezlopi_device_free_device_by_item(item); + TRACE_E("Couldn't initiate device!, error: %d", err); } } } + if (0 == ret) + { + TRACE_E("Here"); + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } + } + return ret; } static void __prepare_device_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_dimmable_light; device->cloud_properties.subcategory = subcategory_dimmable_colored; device->cloud_properties.device_type = dev_type_dimmer_outlet; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_SK6812_RGB_color_item(l_ezlopi_item_t *item, cJSON *cj_device) @@ -261,7 +281,7 @@ static void __prepare_SK6812_RGB_color_item(l_ezlopi_item_t *item, cJSON *cj_dev item->interface_type = EZLOPI_DEVICE_INTERFACE_PWM; item->interface.pwm.channel = 0; item->interface.pwm.value = 0; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -279,7 +299,7 @@ static void __prepare_SK6812_RGB_dimmer_item(l_ezlopi_item_t *item, cJSON *cj_de item->interface_type = EZLOPI_DEVICE_INTERFACE_PWM; item->interface.pwm.channel = 0; item->interface.pwm.value = 0; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -287,7 +307,7 @@ static void __prepare_SK6812_RGB_dimmer_item(l_ezlopi_item_t *item, cJSON *cj_de static void __prepare_SK6812_RGB_dimmer_up_item(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = true; item->cloud_properties.show = true; @@ -296,7 +316,7 @@ static void __prepare_SK6812_RGB_dimmer_up_item(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.value_type = value_type_int; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -304,7 +324,7 @@ static void __prepare_SK6812_RGB_dimmer_up_item(l_ezlopi_item_t *item, cJSON *cj static void __prepare_SK6812_RGB_dimmer_down_item(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = true; item->cloud_properties.show = true; @@ -313,7 +333,7 @@ static void __prepare_SK6812_RGB_dimmer_down_item(l_ezlopi_item_t *item, cJSON * item->cloud_properties.value_type = value_type_int; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -321,7 +341,7 @@ static void __prepare_SK6812_RGB_dimmer_down_item(l_ezlopi_item_t *item, cJSON * static void __prepare_SK6812_RGB_dimmer_stop_item(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = true; item->cloud_properties.show = true; @@ -330,7 +350,7 @@ static void __prepare_SK6812_RGB_dimmer_stop_item(l_ezlopi_item_t *item, cJSON * item->cloud_properties.value_type = value_type_int; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -363,7 +383,7 @@ static int __prepare(void *arg) s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __prepare_device_properties(device, prep_arg->cjson_device); @@ -405,6 +425,8 @@ static int __prepare(void *arg) } else { + TRACE_E("Here"); + free(dimmer_args); ezlopi_device_free_device(device); } diff --git a/devices/device_0022_PWM_dimmable_lamp/device_0022_PWM_dimmable_lamp.c b/devices/device_0022_PWM_dimmable_lamp/device_0022_PWM_dimmable_lamp.c index c4899b1d4..0b93b7684 100644 --- a/devices/device_0022_PWM_dimmable_lamp/device_0022_PWM_dimmable_lamp.c +++ b/devices/device_0022_PWM_dimmable_lamp/device_0022_PWM_dimmable_lamp.c @@ -8,6 +8,7 @@ #include "ezlopi_timer.h" #include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -84,7 +85,7 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) if (ezlopi_item_name_dimmer == item->cloud_properties.item_name) { int value = 0; - CJSON_GET_VALUE_INT(device_details, "value", value); + CJSON_GET_VALUE_INT(device_details, ezlopi_value_str, value); int target_value = (int)ceil(((value * 4095.0) / 100.0)); ezlopi_pwm_change_duty(item->interface.pwm.channel, item->interface.pwm.speed_mode, target_value); dimmable_bulb_arg->previous_brightness_value = dimmable_bulb_arg->current_brightness_value; @@ -95,7 +96,7 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) else if (ezlopi_item_name_switch == item->cloud_properties.item_name) { bool switch_state = false; - CJSON_GET_VALUE_INT(device_details, "value", switch_state); + CJSON_GET_VALUE_INT(device_details, ezlopi_value_str, switch_state); dimmable_bulb_arg->previous_brightness_value = (false == switch_state) ? dimmable_bulb_arg->current_brightness_value : dimmable_bulb_arg->previous_brightness_value; dimmable_bulb_arg->current_brightness_value = (false == switch_state) ? 0 : (0 == dimmable_bulb_arg->previous_brightness_value ? 4095 : dimmable_bulb_arg->previous_brightness_value); ezlopi_pwm_change_duty(dimmable_bulb_arg->item_dimmer->interface.pwm.channel, dimmable_bulb_arg->item_dimmer->interface.pwm.speed_mode, dimmable_bulb_arg->current_brightness_value); @@ -181,9 +182,9 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) if (ezlopi_item_name_dimmer == item->cloud_properties.item_name) { int dimmable_value_percentage = (int)floor(((dimmable_bulb_arg->current_brightness_value * 100.0) / 4095.0)); - cJSON_AddNumberToObject(param, "value", dimmable_value_percentage); + cJSON_AddNumberToObject(param, ezlopi_value_str, dimmable_value_percentage); char *formatted_val = ezlopi_valueformatter_int(dimmable_value_percentage); - cJSON_AddStringToObject(param, "valueFormatted", formatted_val); + cJSON_AddStringToObject(param, ezlopi_valueFormatted_str, formatted_val); free(formatted_val); } else if (ezlopi_item_name_dimmer_up == item->cloud_properties.item_name) @@ -219,8 +220,8 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) else if (ezlopi_item_name_switch == item->cloud_properties.item_name) { bool switch_state = (0 == dimmable_bulb_arg->current_brightness_value) ? false : true; - cJSON_AddBoolToObject(param, "value", switch_state); - cJSON_AddStringToObject(param, "valueFormatted", ezlopi_valueformatter_bool(switch_state)); + cJSON_AddBoolToObject(param, ezlopi_value_str, switch_state); + cJSON_AddStringToObject(param, ezlopi_valueFormatted_str, ezlopi_valueformatter_bool(switch_state)); } } @@ -243,27 +244,40 @@ static int __init(l_ezlopi_item_t *item) dimmable_bulb_arg->previous_brightness_value = item->interface.pwm.duty_cycle; dimmable_bulb_arg->dimmable_bulb_initialized = true; ezlopi_pwm_change_duty(item->interface.pwm.channel, item->interface.pwm.speed_mode, item->interface.pwm.duty_cycle); + ret = 1; + } + } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; } } + return ret; } static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_dimmable_light; device->cloud_properties.subcategory = subcategory_dimmable_bulb; device->cloud_properties.device_type = dev_type_dimmer_outlet; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_dimmer_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = true; item->cloud_properties.show = true; @@ -272,7 +286,7 @@ static void __prepare_dimmer_item_properties(l_ezlopi_item_t *item, cJSON *cj_de item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->cloud_properties.scale = NULL; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -280,7 +294,7 @@ static void __prepare_dimmer_item_properties(l_ezlopi_item_t *item, cJSON *cj_de static void __prepare_dimmer_up_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = true; item->cloud_properties.show = true; @@ -289,7 +303,7 @@ static void __prepare_dimmer_up_item_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->cloud_properties.scale = NULL; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -297,7 +311,7 @@ static void __prepare_dimmer_up_item_properties(l_ezlopi_item_t *item, cJSON *cj static void __prepare_dimmer_down_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = true; item->cloud_properties.show = true; @@ -306,7 +320,7 @@ static void __prepare_dimmer_down_item_properties(l_ezlopi_item_t *item, cJSON * item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->cloud_properties.scale = NULL; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -314,7 +328,7 @@ static void __prepare_dimmer_down_item_properties(l_ezlopi_item_t *item, cJSON * static void __prepare_dimmer_stop_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = true; item->cloud_properties.show = true; @@ -323,7 +337,7 @@ static void __prepare_dimmer_stop_item_properties(l_ezlopi_item_t *item, cJSON * item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->cloud_properties.scale = NULL; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); CJSON_GET_VALUE_INT(cj_device, "duty_cycle", item->interface.pwm.duty_cycle); CJSON_GET_VALUE_INT(cj_device, "freq_hz", item->interface.pwm.freq_hz); item->interface.pwm.pwm_resln = 12; @@ -340,7 +354,7 @@ static void __prepare_dimmer_switch_item_properties(l_ezlopi_item_t *item, cJSON item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->cloud_properties.scale = NULL; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_out.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_out.gpio_num); item->interface.gpio.gpio_in.enable = false; item->interface.gpio.gpio_out.enable = true; item->interface.gpio.gpio_out.interrupt = false; @@ -357,7 +371,7 @@ static int __prepare(void *arg) s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __prepare_device_cloud_properties(device, prep_arg->cjson_device); @@ -411,7 +425,7 @@ static int __prepare(void *arg) if ((NULL == dimmable_bulb_arg->item_dimmer) || (NULL == dimmable_bulb_arg->item_dimmer_up) || (NULL == dimmable_bulb_arg->item_dimmer_down) || (NULL == dimmable_bulb_arg->item_dimmer_stop) || (NULL == dimmable_bulb_arg->item_dimmer_switch)) { - ezlopi_device_free_device(device); + ret = -1; free(dimmable_bulb_arg); } } diff --git a/devices/device_0036_PWM_servo_MG996R/device_0036_PWM_servo_MG996R.c b/devices/device_0036_PWM_servo_MG996R/device_0036_PWM_servo_MG996R.c index 8656d3e23..8f576b4f9 100644 --- a/devices/device_0036_PWM_servo_MG996R/device_0036_PWM_servo_MG996R.c +++ b/devices/device_0036_PWM_servo_MG996R/device_0036_PWM_servo_MG996R.c @@ -95,7 +95,7 @@ static int __prepare(void *arg) { cJSON *cj_device = dev_prep_arg->cjson_device; - l_ezlopi_device_t *servo_device = ezlopi_device_add_device(); + l_ezlopi_device_t *servo_device = ezlopi_device_add_device(dev_prep_arg->cjson_device); if (servo_device) { __prepare_device_cloud_properties(servo_device, cj_device); @@ -204,7 +204,6 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" -#warning "Implementation required" #include "device_0036_PWM_servo_MG996R.h" static int ezlopi_servo_motor_MG_996R_prepare_and_add(void *args); @@ -281,9 +280,11 @@ static s_ezlopi_device_properties_t *ezlopi_servo_motor_MG_996R_prepare(cJSON *c memset(ezlopi_servo_motor_MG_996R_properties, 0, sizeof(s_ezlopi_device_properties_t)); ezlopi_servo_motor_MG_996R_properties->interface_type = EZLOPI_DEVICE_INTERFACE_PWM; - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cjson_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME(ezlopi_servo_motor_MG_996R_properties, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cjson_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME(ezlopi_servo_motor_MG_996R_properties, device_name); + // ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.device_id = ezlopi_cloud_generate_device_id(); + ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.category = category_dimmable_light; ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.subcategory = subcategory_dimmable_bulb; ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.item_name = ezlopi_item_name_dimmer; @@ -295,11 +296,10 @@ static s_ezlopi_device_properties_t *ezlopi_servo_motor_MG_996R_prepare(cJSON *c ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.battery_powered = false; ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.show = true; ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.room_name[0] = '\0'; - ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.device_id = ezlopi_cloud_generate_device_id(); ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.room_id = ezlopi_cloud_generate_room_id(); ezlopi_servo_motor_MG_996R_properties->ezlopi_cloud.item_id = ezlopi_cloud_generate_item_id(); - // CJSON_GET_VALUE_INT(cjson_device, "gpio", ezlopi_servo_motor_MG_996R_properties->interface.pwm.gpio_num); + // CJSON_GET_VALUE_INT(cjson_device, ezlopi_dev_name_str, ezlopi_servo_motor_MG_996R_properties->interface.pwm.gpio_num); // CJSON_GET_VALUE_INT(cjson_device, "duty_cycle", ezlopi_servo_motor_MG_996R_properties->interface.pwm.duty_cycle); // CJSON_GET_VALUE_INT(cjson_device, "freq_hz", ezlopi_servo_motor_MG_996R_properties->interface.pwm.freq_hz); // CJSON_GET_VALUE_INT(cjson_device, "pwm_resln", ezlopi_servo_motor_MG_996R_properties->interface.pwm.pwm_resln); @@ -339,7 +339,7 @@ static int ezlopi_servo_motor_MG_996R_set_value(s_ezlopi_device_properties_t *pr if (NULL != cjson_params) { int value = 0; - CJSON_GET_VALUE_INT(cjson_params, "value", value); + CJSON_GET_VALUE_INT(cjson_params, ezlopi_value_str, value); TRACE_I("item_name: %s", properties->ezlopi_cloud.item_name); TRACE_I("gpio_num: %d", properties->interface.pwm.gpio_num); @@ -363,7 +363,7 @@ static int ezlopi_servo_motor_MG_996R_get_value_cjson(s_ezlopi_device_properties { uint32_t duty = ezlopi_pwm_get_duty(properties->interface.pwm.channel, properties->interface.pwm.speed_mode); int target_duty = (int)(((duty - 13) * 100) / 17); - cJSON_AddNumberToObject(cjson_propertise, "value", target_duty); + cJSON_AddNumberToObject(cjson_propertise, ezlopi_value_str, target_duty); ret = 1; } return ret; diff --git a/devices/device_0038_other_RGB/device_0038_other_RGB.c b/devices/device_0038_other_RGB/device_0038_other_RGB.c index 1a2b2ce64..36f9f8c56 100644 --- a/devices/device_0038_other_RGB/device_0038_other_RGB.c +++ b/devices/device_0038_other_RGB/device_0038_other_RGB.c @@ -6,6 +6,7 @@ #include "ezlopi_timer.h" #include "ezlopi_cloud.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -95,29 +96,29 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_rgbcolor == item->cloud_properties.item_name) { - cJSON *color_values = cJSON_AddObjectToObject(cjson_params, "value"); + cJSON *color_values = cJSON_AddObjectToObject(cjson_params, ezlopi_value_str); if (color_values) { cJSON_AddNumberToObject(color_values, "red", rgb_args->red_struct.value); cJSON_AddNumberToObject(color_values, "green", rgb_args->green_struct.value); cJSON_AddNumberToObject(color_values, "blue", rgb_args->blue_struct.value); char *formatted_val = ezlopi_valueformatter_rgb(rgb_args->red_struct.value, rgb_args->green_struct.value, rgb_args->blue_struct.value); - cJSON_AddStringToObject(cjson_params, "valueFormatted", formatted_val); + cJSON_AddStringToObject(cjson_params, ezlopi_valueFormatted_str, formatted_val); free(formatted_val); } } if (ezlopi_item_name_switch == item->cloud_properties.item_name) { int state = ((0 == rgb_args->brightness) ? 0 : 1); - cJSON_AddBoolToObject(cjson_params, "value", state); - cJSON_AddStringToObject(cjson_params, "valueFormatted", ezlopi_valueformatter_bool(state ? true : false)); + cJSON_AddBoolToObject(cjson_params, ezlopi_value_str, state); + cJSON_AddStringToObject(cjson_params, ezlopi_valueFormatted_str, ezlopi_valueformatter_bool(state ? true : false)); } if (ezlopi_item_name_dimmer == item->cloud_properties.item_name) { int dim_percentage = (int)(rgb_args->brightness * 100); - cJSON_AddNumberToObject(cjson_params, "value", dim_percentage); + cJSON_AddNumberToObject(cjson_params, ezlopi_value_str, dim_percentage); char *formatted_val = ezlopi_valueformatter_int(dim_percentage); - cJSON_AddStringToObject(cjson_params, "valueFormatted", formatted_val); + cJSON_AddStringToObject(cjson_params, ezlopi_valueFormatted_str, formatted_val); free(formatted_val); } } @@ -134,8 +135,9 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_rgbcolor == item->cloud_properties.item_name) { - cJSON *cjson_params_rgb_values = cJSON_GetObjectItem(cjson_params, "value"); - TRACE_B("%s", cJSON_Print(cjson_params_rgb_values)); + cJSON *cjson_params_rgb_values = cJSON_GetObjectItem(cjson_params, ezlopi_value_str); + + CJSON_TRACE("cjson_params_rgb_values", cjson_params_rgb_values); CJSON_GET_VALUE_INT(cjson_params_rgb_values, "red", rgb_args->red_struct.value); CJSON_GET_VALUE_INT(cjson_params_rgb_values, "green", rgb_args->green_struct.value); @@ -146,7 +148,7 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) if (ezlopi_item_name_switch == item->cloud_properties.item_name) { int led_state = 0; - CJSON_GET_VALUE_INT(cjson_params, "value", led_state); + CJSON_GET_VALUE_INT(cjson_params, ezlopi_value_str, led_state); rgb_args->previous_dim_factor = ((0 == led_state) ? rgb_args->brightness : rgb_args->previous_dim_factor); rgb_args->brightness = ((0 == led_state) ? 0.0 : ((0 == rgb_args->previous_dim_factor) ? 1.0 : rgb_args->previous_dim_factor)); TRACE_B("Brightness value is %d, %d, %d", (uint8_t)(rgb_args->red_struct.value * rgb_args->brightness), (uint8_t)(rgb_args->green_struct.value * rgb_args->brightness), @@ -157,7 +159,7 @@ static int __set_cjson_value(l_ezlopi_item_t *item, void *arg) if (ezlopi_item_name_dimmer == item->cloud_properties.item_name) { int dim_percent = 0; - CJSON_GET_VALUE_INT(cjson_params, "value", dim_percent); + CJSON_GET_VALUE_INT(cjson_params, ezlopi_value_str, dim_percent); float dim_brightness_factor = dim_percent / 100.0; TRACE_B("dim_percent %d, dim_brightness_factor is %f", dim_percent, dim_brightness_factor); rgb_args->brightness = dim_brightness_factor; @@ -202,6 +204,17 @@ static int __init(l_ezlopi_item_t *item) rgb_args->RGB_LED_initialized = true; } + + ret = 1; + } + else + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } } return ret; @@ -209,22 +222,23 @@ static int __init(l_ezlopi_item_t *item) static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_dimmable_light; device->cloud_properties.subcategory = subcategory_dimmable_colored; device->cloud_properties.device_type = dev_type_dimmer_outlet; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_RGB_LED_user_args(s_rgb_args_t *rgb_args, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "gpio1", rgb_args->red_struct.gpio_num); - CJSON_GET_VALUE_INT(cj_device, "gpio2", rgb_args->green_struct.gpio_num); - CJSON_GET_VALUE_INT(cj_device, "gpio3", rgb_args->blue_struct.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, rgb_args->red_struct.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, rgb_args->green_struct.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio3_str, rgb_args->blue_struct.gpio_num); rgb_args->red_struct.duty_cycle = 0; rgb_args->green_struct.duty_cycle = 0; @@ -310,7 +324,7 @@ static int __prepare(void *arg) s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *RGB_device = ezlopi_device_add_device(); + l_ezlopi_device_t *RGB_device = ezlopi_device_add_device(prep_arg->cjson_device); if (RGB_device) { s_rgb_args_t *rgb_args = malloc(sizeof(s_rgb_args_t)); @@ -345,6 +359,7 @@ static int __prepare(void *arg) if (!rgb_args->RGB_LED_item && !rgb_args->RGB_LED_onoff_switch_item && !rgb_args->RGB_LED_dimmer_item) { + free(rgb_args); ezlopi_device_free_device(RGB_device); } } diff --git a/devices/sensor_0005_I2C_MPU6050/sensor_0005_I2C_MPU6050.c b/devices/sensor_0005_I2C_MPU6050/sensor_0005_I2C_MPU6050.c index b42d9971f..3813ca324 100644 --- a/devices/sensor_0005_I2C_MPU6050/sensor_0005_I2C_MPU6050.c +++ b/devices/sensor_0005_I2C_MPU6050/sensor_0005_I2C_MPU6050.c @@ -113,7 +113,7 @@ static int __prepare(void *arg) if (NULL != user_data) { memset(user_data, 0, sizeof(s_mpu6050_data_t)); - l_ezlopi_device_t *mpu6050_device = ezlopi_device_add_device(); + l_ezlopi_device_t *mpu6050_device = ezlopi_device_add_device(cj_device); if (mpu6050_device) { __prepare_device_cloud_properties(mpu6050_device, cj_device); diff --git a/devices/sensor_0006_I2C_ADXL345/sensor_0006_I2C_ADXL345.c b/devices/sensor_0006_I2C_ADXL345/sensor_0006_I2C_ADXL345.c index 28e7f473a..b3b122afc 100644 --- a/devices/sensor_0006_I2C_ADXL345/sensor_0006_I2C_ADXL345.c +++ b/devices/sensor_0006_I2C_ADXL345/sensor_0006_I2C_ADXL345.c @@ -111,7 +111,7 @@ static int __prepare(void *arg) if (NULL != user_data) { memset(user_data, 0, sizeof(s_adxl345_data_t)); - l_ezlopi_device_t *adxl345_device = ezlopi_device_add_device(); + l_ezlopi_device_t *adxl345_device = ezlopi_device_add_device(cj_device); if (adxl345_device) { __prepare_device_cloud_properties(adxl345_device, cj_device); diff --git a/devices/sensor_0007_I2C_GY271/sensor_0007_I2C_GY271.c b/devices/sensor_0007_I2C_GY271/sensor_0007_I2C_GY271.c index 8c79c9aea..0b2294647 100644 --- a/devices/sensor_0007_I2C_GY271/sensor_0007_I2C_GY271.c +++ b/devices/sensor_0007_I2C_GY271/sensor_0007_I2C_GY271.c @@ -175,7 +175,7 @@ static int __prepare(void *arg) if (NULL != user_data) { memset(user_data, 0, sizeof(s_gy271_data_t)); - l_ezlopi_device_t *gy271_device_parent_x_item = ezlopi_device_add_device(); + l_ezlopi_device_t *gy271_device_parent_x_item = ezlopi_device_add_device(cj_device); if (gy271_device_parent_x_item) { __prepare_device_cloud_properties_parent_x(gy271_device_parent_x_item, cj_device); @@ -195,7 +195,7 @@ static int __prepare(void *arg) } } - l_ezlopi_device_t *gy271_device_child_y_item = ezlopi_device_add_device(); + l_ezlopi_device_t *gy271_device_child_y_item = ezlopi_device_add_device(cj_device); if (gy271_device_parent_x_item && gy271_device_child_y_item) { __prepare_device_cloud_properties_child_y(gy271_device_child_y_item, cj_device); @@ -216,7 +216,7 @@ static int __prepare(void *arg) } } - l_ezlopi_device_t *gy271_device_child_z_item = ezlopi_device_add_device(); + l_ezlopi_device_t *gy271_device_child_z_item = ezlopi_device_add_device(cj_device); if (gy271_device_parent_x_item && gy271_device_child_z_item) { __prepare_device_cloud_properties_child_z(gy271_device_child_z_item, cj_device); @@ -236,7 +236,7 @@ static int __prepare(void *arg) ezlopi_device_free_device(gy271_device_child_z_item); } } - l_ezlopi_device_t *gy271_device_child_azi_item = ezlopi_device_add_device(); + l_ezlopi_device_t *gy271_device_child_azi_item = ezlopi_device_add_device(cj_device); if (gy271_device_parent_x_item && gy271_device_child_azi_item) { __prepare_device_cloud_properties_child_azi(gy271_device_child_azi_item, cj_device); @@ -256,7 +256,7 @@ static int __prepare(void *arg) ezlopi_device_free_device(gy271_device_child_azi_item); } } - l_ezlopi_device_t *gy271_device_child_temp_item = ezlopi_device_add_device(); + l_ezlopi_device_t *gy271_device_child_temp_item = ezlopi_device_add_device(cj_device); if (gy271_device_parent_x_item && gy271_device_child_temp_item) { __prepare_device_cloud_properties_child_temp(gy271_device_child_temp_item, cj_device); diff --git a/devices/sensor_0008_I2C_LTR303ALS/sensor_0008_I2C_LTR303ALS.c b/devices/sensor_0008_I2C_LTR303ALS/sensor_0008_I2C_LTR303ALS.c index 5acea0509..d8424970e 100644 --- a/devices/sensor_0008_I2C_LTR303ALS/sensor_0008_I2C_LTR303ALS.c +++ b/devices/sensor_0008_I2C_LTR303ALS/sensor_0008_I2C_LTR303ALS.c @@ -13,6 +13,7 @@ #include "ezlopi_i2c_master.h" #include "ezlopi_spi_master.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -68,9 +69,9 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) ltr303_data_t *als_ltr303_data = (ltr303_data_t *)item->user_arg; if (cj_param && als_ltr303_data) { - cJSON_AddNumberToObject(cj_param, "value", als_ltr303_data->lux); + cJSON_AddNumberToObject(cj_param, ezlopi_value_str, als_ltr303_data->lux); char *valueFormatted = ezlopi_valueformatter_double(als_ltr303_data->lux); - cJSON_AddStringToObject(cj_param, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_param, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); } @@ -108,6 +109,16 @@ static int __init(l_ezlopi_item_t *item) { ltr303_setup(item->interface.i2c_master.sda, item->interface.i2c_master.scl, true); ltr303_get_val(als_ltr303_data); + ret = 1; + } + else + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } } return ret; @@ -115,21 +126,21 @@ static int __init(l_ezlopi_item_t *item) static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_params) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_params, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_params, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); device->cloud_properties.category = category_light_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_param) { - CJSON_GET_VALUE_INT(cj_param, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_param, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); @@ -138,8 +149,8 @@ static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_param) item->cloud_properties.show = true; item->cloud_properties.scale = scales_lux; - CJSON_GET_VALUE_INT(cj_param, "gpio_sda", item->interface.i2c_master.sda); - CJSON_GET_VALUE_INT(cj_param, "gpio_scl", item->interface.i2c_master.scl); + CJSON_GET_VALUE_INT(cj_param, ezlopi_gpio_sda_str, item->interface.i2c_master.sda); + CJSON_GET_VALUE_INT(cj_param, ezlopi_gpio_scl_str, item->interface.i2c_master.scl); item->interface.i2c_master.enable = true; item->interface.i2c_master.clock_speed = 100000; @@ -159,7 +170,7 @@ static int __prepare(void *arg) s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *als_ltr303_device = ezlopi_device_add_device(); + l_ezlopi_device_t *als_ltr303_device = ezlopi_device_add_device(prep_arg->cjson_device); if (als_ltr303_device) { __prepare_device_cloud_properties(als_ltr303_device, prep_arg->cjson_device); @@ -174,10 +185,6 @@ static int __prepare(void *arg) ezlopi_device_free_device(als_ltr303_device); } } - else - { - ezlopi_device_free_device(als_ltr303_device); - } } return ret; diff --git a/devices/sensor_0010_I2C_BME680/sensor_0010_I2C_BME680.c b/devices/sensor_0010_I2C_BME680/sensor_0010_I2C_BME680.c index f75f08e07..176f86b7b 100644 --- a/devices/sensor_0010_I2C_BME680/sensor_0010_I2C_BME680.c +++ b/devices/sensor_0010_I2C_BME680/sensor_0010_I2C_BME680.c @@ -100,7 +100,7 @@ static int __prepare(void *arg) if (user_data) { memset(user_data, 0, sizeof(bme680_data_t)); - l_ezlopi_device_t *temp_humid_device = ezlopi_device_add_device(); + l_ezlopi_device_t *temp_humid_device = ezlopi_device_add_device(cj_device); if (temp_humid_device) { temp_humid_device->cloud_properties.category = category_temperature; @@ -130,7 +130,7 @@ static int __prepare(void *arg) { ezlopi_device_free_device(temp_humid_device); } - l_ezlopi_device_t *pressure_device = ezlopi_device_add_device(); + l_ezlopi_device_t *pressure_device = ezlopi_device_add_device(cj_device); if (pressure_device) { pressure_device->cloud_properties.category = category_level_sensor; @@ -152,7 +152,7 @@ static int __prepare(void *arg) { ezlopi_device_free_device(pressure_device); } - l_ezlopi_device_t *aqi_device = ezlopi_device_add_device(); + l_ezlopi_device_t *aqi_device = ezlopi_device_add_device(cj_device); if (aqi_device) { aqi_device->cloud_properties.category = category_level_sensor; @@ -174,7 +174,7 @@ static int __prepare(void *arg) { ezlopi_device_free_device(aqi_device); } - l_ezlopi_device_t *altitude_device = ezlopi_device_add_device(); + l_ezlopi_device_t *altitude_device = ezlopi_device_add_device(cj_device); if (altitude_device) { altitude_device->cloud_properties.category = category_level_sensor; @@ -196,7 +196,7 @@ static int __prepare(void *arg) { ezlopi_device_free_device(altitude_device); } - l_ezlopi_device_t *co2_device = ezlopi_device_add_device(); + l_ezlopi_device_t *co2_device = ezlopi_device_add_device(cj_device); if (co2_device) { co2_device->cloud_properties.category = category_level_sensor; diff --git a/devices/sensor_0012_I2C_BME280/sensor_0012_I2C_BME280.c b/devices/sensor_0012_I2C_BME280/sensor_0012_I2C_BME280.c index 52e87ffd2..724ae49db 100644 --- a/devices/sensor_0012_I2C_BME280/sensor_0012_I2C_BME280.c +++ b/devices/sensor_0012_I2C_BME280/sensor_0012_I2C_BME280.c @@ -8,6 +8,7 @@ #include "ezlopi_cloud.h" #include "ezlopi_timer.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -136,23 +137,25 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) if (ezlopi_item_name_temp == item->cloud_properties.item_name) { snprintf(valueFormatted, 20, "%0.3f", bmp280_sensor_params->temperature); - cJSON_AddStringToObject(cj_device, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_device, "value", bmp280_sensor_params->temperature); - cJSON_AddStringToObject(cj_device, "scale", "celsius"); + cJSON_AddStringToObject(cj_device, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_device, ezlopi_value_str, bmp280_sensor_params->temperature); + cJSON_AddStringToObject(cj_device, ezlopi_scale_str, scales_celsius); } + if (ezlopi_item_name_humidity == item->cloud_properties.item_name) { snprintf(valueFormatted, 20, "%0.3f", bmp280_sensor_params->humidity); - cJSON_AddStringToObject(cj_device, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_device, "value", bmp280_sensor_params->humidity); - cJSON_AddStringToObject(cj_device, "scale", "percent"); + cJSON_AddStringToObject(cj_device, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_device, ezlopi_value_str, bmp280_sensor_params->humidity); + cJSON_AddStringToObject(cj_device, ezlopi_scale_str, scales_percent); } + if (ezlopi_item_name_atmospheric_pressure == item->cloud_properties.item_name) { snprintf(valueFormatted, 20, "%0.3f", (bmp280_sensor_params->pressure / 1000.0)); - cJSON_AddStringToObject(cj_device, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_device, "value", (bmp280_sensor_params->pressure / 1000.0)); - cJSON_AddStringToObject(cj_device, "scale", "kilo_pascal"); + cJSON_AddStringToObject(cj_device, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_device, ezlopi_value_str, (bmp280_sensor_params->pressure / 1000.0)); + cJSON_AddStringToObject(cj_device, ezlopi_scale_str, scales_kilo_pascal); } } @@ -172,6 +175,17 @@ static int __init(l_ezlopi_item_t *item) bmp280_init_default_params(&bmp280_sensor_params->bmp280_params); bmp280_init(&bmp280_sensor_params->bmp280_dev, &bmp280_sensor_params->bmp280_params, &item->interface.i2c_master); bmp280_read_float(&item->interface.i2c_master, &bmp280_sensor_params->bmp280_dev, &bmp280_sensor_params->temperature, &bmp280_sensor_params->pressure, &bmp280_sensor_params->humidity); + ret = 1; + } + } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; } } @@ -185,9 +199,9 @@ static int __prepare(void *arg) s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; if (prep_arg && prep_arg->cjson_device) { - int error_settingup_device = 0; - l_ezlopi_device_t *pressure_deivce = ezlopi_device_add_device(); - l_ezlopi_device_t *temp_humid_device = ezlopi_device_add_device(); + l_ezlopi_device_t *pressure_deivce = ezlopi_device_add_device(prep_arg->cjson_device); + l_ezlopi_device_t *temp_humid_device = ezlopi_device_add_device(prep_arg->cjson_device); + s_ezlopi_bmp280_t *bme280_sensor_params = (s_ezlopi_bmp280_t *)malloc(sizeof(s_ezlopi_bmp280_t)); if (temp_humid_device && pressure_deivce && bme280_sensor_params) @@ -204,7 +218,7 @@ static int __prepare(void *arg) } else { - error_settingup_device = -1; + ret = -1; } l_ezlopi_item_t *humidity_item = ezlopi_device_add_item_to_device(temp_humid_device, sensor_0012_I2C_BME280); @@ -215,7 +229,7 @@ static int __prepare(void *arg) } else { - error_settingup_device = -1; + ret = -1; } __prepare_pressure_device_cloud_properties(pressure_deivce, prep_arg->cjson_device); @@ -228,18 +242,28 @@ static int __prepare(void *arg) } else { - error_settingup_device = -1; + ret = -1; } } - if (error_settingup_device) + if (-1 == ret) { if (temp_humid_device) + { ezlopi_device_free_device(temp_humid_device); + } if (pressure_deivce) + { ezlopi_device_free_device(pressure_deivce); + } if (bme280_sensor_params) + { free(bme280_sensor_params); + } + } + else + { + ret = 1; } } @@ -248,35 +272,35 @@ static int __prepare(void *arg) static void __prepare_temp_humid_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_pressure_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_generic_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_temperature_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_arg) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.scale = scales_celsius; @@ -286,8 +310,8 @@ static void __prepare_temperature_properties(l_ezlopi_item_t *item, cJSON *cj_de item->interface.i2c_master.enable = true; item->interface.i2c_master.clock_speed = 100000; - CJSON_GET_VALUE_INT(cj_device, "gpio_scl", item->interface.i2c_master.scl); - CJSON_GET_VALUE_INT(cj_device, "gpio_sda", item->interface.i2c_master.sda); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_scl_str, item->interface.i2c_master.scl); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_sda_str, item->interface.i2c_master.sda); CJSON_GET_VALUE_INT(cj_device, "slave_addr", item->interface.i2c_master.address); item->user_arg = user_arg; @@ -295,7 +319,7 @@ static void __prepare_temperature_properties(l_ezlopi_item_t *item, cJSON *cj_de static void __prepare_humidity_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_arg) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.scale = scales_percent; @@ -305,8 +329,8 @@ static void __prepare_humidity_properties(l_ezlopi_item_t *item, cJSON *cj_devic item->interface.i2c_master.enable = false; item->interface.i2c_master.clock_speed = 100000; - CJSON_GET_VALUE_INT(cj_device, "gpio_scl", item->interface.i2c_master.scl); - CJSON_GET_VALUE_INT(cj_device, "gpio_sda", item->interface.i2c_master.sda); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_scl_str, item->interface.i2c_master.scl); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_sda_str, item->interface.i2c_master.sda); CJSON_GET_VALUE_INT(cj_device, "slave_addr", item->interface.i2c_master.address); item->user_arg = user_arg; @@ -314,7 +338,7 @@ static void __prepare_humidity_properties(l_ezlopi_item_t *item, cJSON *cj_devic static void __prepare_pressure_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_arg) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.scale = scales_kilo_pascal; @@ -324,8 +348,8 @@ static void __prepare_pressure_properties(l_ezlopi_item_t *item, cJSON *cj_devic item->interface.i2c_master.enable = false; item->interface.i2c_master.clock_speed = 100000; - CJSON_GET_VALUE_INT(cj_device, "gpio_scl", item->interface.i2c_master.scl); - CJSON_GET_VALUE_INT(cj_device, "gpio_sda", item->interface.i2c_master.sda); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_scl_str, item->interface.i2c_master.scl); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_sda_str, item->interface.i2c_master.sda); CJSON_GET_VALUE_INT(cj_device, "slave_addr", item->interface.i2c_master.address); item->user_arg = user_arg; diff --git a/devices/sensor_0015_oneWire_DHT11/sensor_0015_oneWire_DHT11.c b/devices/sensor_0015_oneWire_DHT11/sensor_0015_oneWire_DHT11.c index b005547ec..ed9badbd7 100644 --- a/devices/sensor_0015_oneWire_DHT11/sensor_0015_oneWire_DHT11.c +++ b/devices/sensor_0015_oneWire_DHT11/sensor_0015_oneWire_DHT11.c @@ -74,7 +74,7 @@ static int __0015_prepare(void *arg) s_ezlopi_dht11_data_t *dht11_sensor_data = (s_ezlopi_dht11_data_t *)malloc(sizeof(s_ezlopi_dht11_data_t)); if (dht11_sensor_data) { - l_ezlopi_device_t *device_temperature = ezlopi_device_add_device(); + l_ezlopi_device_t *device_temperature = ezlopi_device_add_device(cjson_device); if (device_temperature) { __dht11_setup_device_cloud_properties_temperature(device_temperature, cjson_device); @@ -85,7 +85,7 @@ static int __0015_prepare(void *arg) __dht11_setup_item_properties_temperature(item_temperature, cjson_device, dht11_sensor_data); } } - l_ezlopi_device_t *device_humidity = ezlopi_device_add_device(); + l_ezlopi_device_t *device_humidity = ezlopi_device_add_device(cjson_device); if (device_humidity) { __dht11_setup_device_cloud_properties_humidity(device_humidity, cjson_device); diff --git a/devices/sensor_0016_oneWire_DHT22/sensor_0016_oneWire_DHT22.c b/devices/sensor_0016_oneWire_DHT22/sensor_0016_oneWire_DHT22.c index 7c7263b97..b865de6c8 100644 --- a/devices/sensor_0016_oneWire_DHT22/sensor_0016_oneWire_DHT22.c +++ b/devices/sensor_0016_oneWire_DHT22/sensor_0016_oneWire_DHT22.c @@ -5,6 +5,7 @@ #include "ezlopi_adc.h" #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_cloud_item_name_str.h" @@ -120,20 +121,20 @@ static int dht22_sensor_get_sensor_value_v3(l_ezlopi_item_t *item, void *args) if (ezlopi_item_name_temp == item->cloud_properties.item_name) { - cJSON_AddNumberToObject(cj_properties, "value", dht22_data->temperature); + cJSON_AddNumberToObject(cj_properties, ezlopi_value_str, dht22_data->temperature); char *valueFormatted = ezlopi_valueformatter_float(dht22_data->temperature); - cJSON_AddStringToObject(cj_properties, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_properties, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); - cJSON_AddStringToObject(cj_properties, "scale", item->cloud_properties.scale); + cJSON_AddStringToObject(cj_properties, ezlopi_scale_str, item->cloud_properties.scale); } if (ezlopi_item_name_humidity == item->cloud_properties.item_name) { - cJSON_AddNumberToObject(cj_properties, "value", dht22_data->humidity); + cJSON_AddNumberToObject(cj_properties, ezlopi_value_str, dht22_data->humidity); char *valueFormatted = ezlopi_valueformatter_float(dht22_data->humidity); - cJSON_AddStringToObject(cj_properties, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_properties, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); - cJSON_AddStringToObject(cj_properties, "scale", item->cloud_properties.scale); + cJSON_AddStringToObject(cj_properties, ezlopi_scale_str, item->cloud_properties.scale); } } return ret; @@ -148,8 +149,8 @@ static int dht22_sensor_prepare_v3(void *arg) cJSON *cjson_device = prep_arg->cjson_device; if (cjson_device) { - l_ezlopi_device_t *device_temperature = ezlopi_device_add_device(); - l_ezlopi_device_t *device_humidity = ezlopi_device_add_device(); + l_ezlopi_device_t *device_temperature = ezlopi_device_add_device(prep_arg->cjson_device); + l_ezlopi_device_t *device_humidity = ezlopi_device_add_device(prep_arg->cjson_device); if (device_temperature) { @@ -198,16 +199,16 @@ static int dht22_sensor_setup_device_cloud_properties_temperature(l_ezlopi_devic int ret = 0; if (device && cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_temperature; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } return ret; } @@ -217,16 +218,16 @@ static int dht22_sensor_setup_device_cloud_properties_humidity(l_ezlopi_device_t int ret = 0; if (device && cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_humidity; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } return ret; } @@ -246,10 +247,10 @@ static int dht22_sensor_setup_item_properties_temperature(l_ezlopi_item_t *item, item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->user_arg = user_arg; - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->interface.onewire_master.enable = true; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.onewire_master.onewire_pin); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.onewire_master.onewire_pin); } return ret; @@ -270,10 +271,10 @@ static int dht22_sensor_setup_item_properties_humidity(l_ezlopi_item_t *item, cJ item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->user_arg = user_arg; - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->interface.onewire_master.enable = true; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.onewire_master.onewire_pin); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.onewire_master.onewire_pin); } return ret; diff --git a/devices/sensor_0017_ADC_potentiometer/sensor_0017_ADC_potentiometer.c b/devices/sensor_0017_ADC_potentiometer/sensor_0017_ADC_potentiometer.c index 265e896e2..671c4aa65 100644 --- a/devices/sensor_0017_ADC_potentiometer/sensor_0017_ADC_potentiometer.c +++ b/devices/sensor_0017_ADC_potentiometer/sensor_0017_ADC_potentiometer.c @@ -91,7 +91,7 @@ static int __0017_prepare(void *arg) if (NULL != user_data) { memset(user_data, 0, sizeof(s_potentiometer_t)); - l_ezlopi_device_t *potentiometer_device = ezlopi_device_add_device(); + l_ezlopi_device_t *potentiometer_device = ezlopi_device_add_device(cj_device); if (potentiometer_device) { __prepare_device_cloud_properties(potentiometer_device, cj_device); diff --git a/devices/sensor_0018_other_internal_hall_effect/sensor_0018_other_internal_hall_effect.c b/devices/sensor_0018_other_internal_hall_effect/sensor_0018_other_internal_hall_effect.c index 6ec2511b0..e55334c21 100644 --- a/devices/sensor_0018_other_internal_hall_effect/sensor_0018_other_internal_hall_effect.c +++ b/devices/sensor_0018_other_internal_hall_effect/sensor_0018_other_internal_hall_effect.c @@ -119,7 +119,7 @@ static int __prepare(void *arg) if (user_data) { memset(user_data, 0, sizeof(s_hall_data_t)); - l_ezlopi_device_t *hall_device = ezlopi_device_add_device(); + l_ezlopi_device_t *hall_device = ezlopi_device_add_device(cj_device); if (hall_device) { __setup_device_cloud_properties(hall_device, cj_device); @@ -156,13 +156,13 @@ static int __init(l_ezlopi_item_t *item) } else { + ret = 1; TRACE_I("Width configuration was successfully done!"); TRACE_W("Calibrating....."); s_hall_data_t *user_data = (s_hall_data_t *)item->user_arg; user_data->hall_state = "dw_is_closed"; xTaskCreate(__hall_calibration_task, "Hall_Calibration_Task", 2048, item, 1, NULL); } - ret = 1; } return ret; } diff --git a/devices/sensor_0019_digitalIn_PIR/sensor_0019_digitalIn_PIR.c b/devices/sensor_0019_digitalIn_PIR/sensor_0019_digitalIn_PIR.c index 972a70a83..23fe49077 100644 --- a/devices/sensor_0019_digitalIn_PIR/sensor_0019_digitalIn_PIR.c +++ b/devices/sensor_0019_digitalIn_PIR/sensor_0019_digitalIn_PIR.c @@ -1,15 +1,17 @@ #include "ezlopi_cloud.h" -#include "gpio_isr_service.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" +#include "ezlopi_cloud_constants.h" +#include "gpio_isr_service.h" #include "sensor_0019_digitalIn_PIR.h" static int sensor_pir_prepare_v3(void *arg); static int sensor_pir_init_v3(l_ezlopi_item_t *item); +static void sensor_pir_value_updated_from_device_v3(void *arg); static int sensor_pir_get_value_cjson_v3(l_ezlopi_item_t *item, void *arg); -static void sensor_pir_value_updated_from_device_v3(l_ezlopi_item_t *item); static void sensor_pir_setup_item_properties_v3(l_ezlopi_item_t *item, cJSON *cj_device); static void sensor_pir_setup_device_cloud_properties_v3(l_ezlopi_device_t *device, cJSON *cj_device); @@ -52,18 +54,22 @@ static int sensor_pir_get_value_cjson_v3(l_ezlopi_item_t *item, void *args) if (cj_result) { item->interface.gpio.gpio_out.value = gpio_get_level(item->interface.gpio.gpio_in.gpio_num); - cJSON_AddBoolToObject(cj_result, "value", item->interface.gpio.gpio_out.value); + cJSON_AddBoolToObject(cj_result, ezlopi_value_str, item->interface.gpio.gpio_out.value); char *valueFormatted = ezlopi_valueformatter_bool(item->interface.gpio.gpio_out.value ? true : false); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); ret = 1; } return ret; } -static void sensor_pir_value_updated_from_device_v3(l_ezlopi_item_t *item) +static void sensor_pir_value_updated_from_device_v3(void *arg) { - ezlopi_device_value_updated_from_device_v3(item); + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; + if (item) + { + ezlopi_device_value_updated_from_device_v3(item); + } } static int sensor_pir_init_v3(l_ezlopi_item_t *item) @@ -106,7 +112,7 @@ static int sensor_pir_prepare_v3(void *arg) cJSON *cj_device = prep_arg->cjson_device; if (cj_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { sensor_pir_setup_device_cloud_properties_v3(device, cj_device); @@ -121,7 +127,7 @@ static int sensor_pir_prepare_v3(void *arg) } else { - ezlopi_device_free_device(device); + ret = -1; } } } @@ -134,16 +140,16 @@ static void sensor_pir_setup_device_cloud_properties_v3(l_ezlopi_device_t *devic { if (device && cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); device->cloud_properties.category = category_generic_sensor; device->cloud_properties.subcategory = subcategory_motion; device->cloud_properties.device_type = dev_type_sensor_motion; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } } @@ -157,11 +163,11 @@ static void sensor_pir_setup_item_properties_v3(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.show = true; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->interface.gpio.gpio_in.enable = true; item->interface.gpio.gpio_in.mode = GPIO_MODE_INPUT; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); CJSON_GET_VALUE_INT(cj_device, "logic_inv", item->interface.gpio.gpio_in.invert); CJSON_GET_VALUE_INT(cj_device, "pull_up", tmp_var); item->interface.gpio.gpio_in.pull = tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; diff --git a/devices/sensor_0020_ADC_2axis_joystick/sensor_0020_ADC_2axis_joystick.c b/devices/sensor_0020_ADC_2axis_joystick/sensor_0020_ADC_2axis_joystick.c new file mode 100644 index 000000000..b9f14eb5a --- /dev/null +++ b/devices/sensor_0020_ADC_2axis_joystick/sensor_0020_ADC_2axis_joystick.c @@ -0,0 +1,162 @@ + +#warning "Implementation not done" +#if 0 + +#include "cJSON.h" +#include "ezlopi_cloud.h" +#include "ezlopi_devices_list.h" +#include "ezlopi_device_value_updated.h" +#include "ezlopi_cloud_category_str.h" +#include "ezlopi_cloud_subcategory_str.h" +#include "ezlopi_cloud_item_name_str.h" +#include "ezlopi_cloud_device_types_str.h" +#include "ezlopi_cloud_value_type_str.h" +#include "ezlopi_valueformatter.h" + +#include "trace.h" +#include "ezlopi_adc.h" + +#include "sensor_0020_ADC_2axis_joystick.h" + +static int __prepare(void *arg); +static int __init(l_ezlopi_item_t *item); +static int __notify(l_ezlopi_item_t *item); +static int __get_value_cjson(l_ezlopi_item_t *item, void *arg); + +int sensor_0020_ADC_2axis_joystick(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) +{ + int ret = 0; + + switch (action) + { + case EZLOPI_ACTION_PREPARE: + { + ret = __prepare(arg); + break; + } + case EZLOPI_ACTION_INITIALIZE: + { + ret = __init(item); + break; + } + case EZLOPI_ACTION_HUB_GET_ITEM: + case EZLOPI_ACTION_GET_EZLOPI_VALUE: + { + ret = __get_value_cjson(item, arg); + break; + } + case EZLOPI_ACTION_NOTIFY_1000_MS: + { + ret = __notify(item); + break; + } + + default: + { + break; + } + } + + return ret; +} + +static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) +{ + int ret = 0; + if (arg) + { + cJSON *cj_result = (cJSON *)arg; + s_ezlopi_analog_data_t ezlopi_analog_data; + memset(&ezlopi_analog_data, 0, sizeof(s_ezlopi_analog_data_t)); + ezlopi_adc_get_adc_data(item->interface.adc.gpio_num, &ezlopi_analog_data); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, ezlopi_analog_data.value); + ret = 1; + } + return ret; +} + +static int __init(l_ezlopi_item_t *item) +{ + int ret = 0; + if (GPIO_IS_VALID_GPIO(item->interface.adc.gpio_num)) + { + ezlopi_adc_init(item->interface.adc.gpio_num, item->interface.adc.resln_bit); + ret = 1; + } else { + ret = -1; + if (item->user_args) + { + free(item->user_args); + item->user_args = NULL; + } + } + return ret; +} + +static void __setup_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) +{ + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + + device->cloud_properties.category = category_not_defined; + device->cloud_properties.subcategory = subcategory_not_defined; + device->cloud_properties.device_type = dev_type_device; +} + +static void __setup_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) +{ + item->cloud_properties.has_getter = true; + item->cloud_properties.has_setter = true; + item->cloud_properties.item_name = ezlopi_item_name_undefined; + item->cloud_properties.value_type = value_type_int; + item->cloud_properties.show = true; + item->cloud_properties.scale = NULL; + item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); +} + +static void __setup_item_interface_properties(l_ezlopi_item_t *item, cJSON *cj_device) +{ + item->interface_type = EZLOPI_DEVICE_INTERFACE_ANALOG_INPUT; + item->interface.adc.resln_bit = 3; + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.adc.gpio_num); +} + +static int __prepare(void *arg) +{ + int ret = 0; + s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; + if (arg) + { + cJSON *cjson_device = prep_arg->cjson_device; + if (cjson_device) + { + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); + if (device) + { + __setup_device_cloud_properties(device, cjson_device); + l_ezlopi_item_t *item = ezlopi_device_add_item_to_device(device, sensor_0020_ADC_2axis_joystick); + if (item) + { + __setup_item_cloud_properties(item, cjson_device); + __setup_item_interface_properties(item, cjson_device); + ret = 1; + } + else + { + ret = -1; + } + } + } + } + + return ret; +} + +static int __notify(l_ezlopi_item_t *item) +{ + return ezlopi_device_value_updated_from_device_v3(item); +} + +#endif \ No newline at end of file diff --git a/devices/sensor_0020_other_2axis_joystick/sensor_0020_other_2axis_joystick.c b/devices/sensor_0020_other_2axis_joystick/sensor_0020_other_2axis_joystick.c index 1dd2511f4..13553dcbf 100644 --- a/devices/sensor_0020_other_2axis_joystick/sensor_0020_other_2axis_joystick.c +++ b/devices/sensor_0020_other_2axis_joystick/sensor_0020_other_2axis_joystick.c @@ -153,7 +153,7 @@ static int __prepare(void *arg) user_data->sensor_0020_joystick_item_ids[i] = ezlopi_cloud_generate_item_id(); } - l_ezlopi_device_t *joystick_x_device = ezlopi_device_add_device(); + l_ezlopi_device_t *joystick_x_device = ezlopi_device_add_device(cj_device); if (joystick_x_device) { joystick_x_device->cloud_properties.category = category_level_sensor; @@ -172,7 +172,7 @@ static int __prepare(void *arg) ezlopi_device_free_device(joystick_x_device); } } - l_ezlopi_device_t *joystick_y_device = ezlopi_device_add_device(); + l_ezlopi_device_t *joystick_y_device = ezlopi_device_add_device(cj_device); if (joystick_y_device) { joystick_y_device->cloud_properties.category = category_level_sensor; @@ -191,7 +191,7 @@ static int __prepare(void *arg) ezlopi_device_free_device(joystick_y_device); } } - l_ezlopi_device_t *joystick_sw_device = ezlopi_device_add_device(); + l_ezlopi_device_t *joystick_sw_device = ezlopi_device_add_device(cj_device); if (joystick_sw_device) { joystick_sw_device->cloud_properties.category = category_switch; diff --git a/devices/sensor_0021_UART_MB1013/sensor_0021_UART_MB1013.c b/devices/sensor_0021_UART_MB1013/sensor_0021_UART_MB1013.c index 084008902..bb7e31f29 100644 --- a/devices/sensor_0021_UART_MB1013/sensor_0021_UART_MB1013.c +++ b/devices/sensor_0021_UART_MB1013/sensor_0021_UART_MB1013.c @@ -9,6 +9,7 @@ #include "ezlopi_cloud.h" #include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -72,9 +73,9 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) s_mb1013_args_t *mb1013_args = item->user_arg; cJSON *cj_result = (cJSON *)arg; - cJSON_AddNumberToObject(cj_result, "value", mb1013_args->current_value); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, mb1013_args->current_value); snprintf(valueFormatted, 20, "%.2f", mb1013_args->current_value); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); ret = 1; } return ret; @@ -122,21 +123,32 @@ static int __init(l_ezlopi_item_t *item) ret = 1; } } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } + } + return ret; } static void __setup_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __setup_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) @@ -153,9 +165,9 @@ static void __setup_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_devic static void __setup_item_interface_properties(l_ezlopi_item_t *item, cJSON *cj_device) { item->interface_type = EZLOPI_DEVICE_INTERFACE_UART; - CJSON_GET_VALUE_INT(cj_device, "baud_rate", item->interface.uart.baudrate); - CJSON_GET_VALUE_INT(cj_device, "gpio_tx", item->interface.uart.tx); - CJSON_GET_VALUE_INT(cj_device, "gpio_rx", item->interface.uart.rx); + CJSON_GET_VALUE_INT(cj_device, ezlopi_baud_rate_str, item->interface.uart.baudrate); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_tx_str, item->interface.uart.tx); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_rx_str, item->interface.uart.rx); } static int __prepare(void *arg) @@ -167,7 +179,7 @@ static int __prepare(void *arg) cJSON *cjson_device = prep_arg->cjson_device; if (cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __setup_device_cloud_properties(device, cjson_device); @@ -189,7 +201,7 @@ static int __prepare(void *arg) } else { - ezlopi_device_free_device(device); + ret = -1; } } } diff --git a/devices/sensor_0023_digitalIn_touch_switch_TTP223B/sensor_0023_digitalIn_touch_switch_TTP223B.c b/devices/sensor_0023_digitalIn_touch_switch_TTP223B/sensor_0023_digitalIn_touch_switch_TTP223B.c index b87311387..bf695141a 100644 --- a/devices/sensor_0023_digitalIn_touch_switch_TTP223B/sensor_0023_digitalIn_touch_switch_TTP223B.c +++ b/devices/sensor_0023_digitalIn_touch_switch_TTP223B/sensor_0023_digitalIn_touch_switch_TTP223B.c @@ -4,6 +4,7 @@ #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -14,11 +15,6 @@ #include "sensor_0023_digitalIn_touch_switch_TTP223B.h" -#include "sensor_0023_digitalIn_touch_switch_TTP223B.h" - -#warning "Use of static variable" -static bool sensor_bme280_initialized = false; - static int __prepare(void *arg); static void __prepare_touch_switch_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device); static void __prepare_touch_switch_properties(l_ezlopi_item_t *item, cJSON *cj_device); @@ -68,9 +64,9 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) cJSON *param = (cJSON *)arg; if (param) { - cJSON_AddBoolToObject(param, "value", item->interface.gpio.gpio_in.value); + cJSON_AddBoolToObject(param, ezlopi_value_str, item->interface.gpio.gpio_in.value); char *valueFormatted = ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? true : false); - cJSON_AddStringToObject(param, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(param, ezlopi_valueFormatted_str, valueFormatted); } return ret; @@ -90,6 +86,7 @@ static int __init(l_ezlopi_item_t *item) ESP_ERROR_CHECK(gpio_config(&touch_switch_config)); gpio_isr_service_register_v3(item, touch_switch_callback, 200); + ret = 1; return ret; } @@ -104,11 +101,11 @@ static void touch_switch_callback(void *arg) static int __prepare(void *arg) { int ret = 0; + s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; - s_ezlopi_prep_arg_t *prep_arg = (cJSON *)arg; if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *touch_device = ezlopi_device_add_device(); + l_ezlopi_device_t *touch_device = ezlopi_device_add_device(prep_arg->cjson_device); if (touch_device) { __prepare_touch_switch_device_cloud_properties(touch_device, prep_arg->cjson_device); @@ -123,10 +120,6 @@ static int __prepare(void *arg) ezlopi_device_free_device(touch_device); } } - else - { - ezlopi_device_free_device(touch_device); - } } return ret; @@ -134,188 +127,35 @@ static int __prepare(void *arg) static void __prepare_touch_switch_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); device->cloud_properties.category = category_switch; device->cloud_properties.subcategory = subcategory_in_wall; device->cloud_properties.device_type = dev_type_switch_outlet; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_touch_switch_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; - item->cloud_properties.scale = true; item->cloud_properties.item_name = ezlopi_item_name_switch; item->cloud_properties.value_type = value_type_bool; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->cloud_properties.scale = NULL; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); - CJSON_GET_VALUE_INT(cj_device, "ip_inv", item->interface.gpio.gpio_in.invert); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_ip_inv_str, item->interface.gpio.gpio_in.invert); int val_ip = 0; - CJSON_GET_VALUE_INT(cj_device, "val_ip", val_ip); + CJSON_GET_VALUE_INT(cj_device, ezlopi_val_ip_str, val_ip); item->interface.gpio.gpio_in.value = (true == item->interface.gpio.gpio_in.invert) ? !val_ip : val_ip; item->interface.gpio.gpio_in.enable = true; item->interface.gpio.gpio_in.interrupt = GPIO_INTR_POSEDGE; item->interface.gpio.gpio_in.pull = GPIO_PULLDOWN_ONLY; } - -#if 0 -static int sensor_touch_tpp_223b_prepare_and_add(void *args); -static s_ezlopi_device_properties_t *sensor_touch_tpp_223b_prepare(cJSON *cjson_device); -static int sensor_touch_tpp_223b_init(s_ezlopi_device_properties_t *properties); -static void sensor_touch_tpp_223b_value_updated_from_device(s_ezlopi_device_properties_t *properties); -static int sensor_touch_tpp_223b_get_value_cjson(s_ezlopi_device_properties_t *properties, void *args); - -int sensor_touch_ttp_223b(e_ezlopi_actions_t action, s_ezlopi_device_properties_t *ezlo_device, void *arg, void *user_arg) -{ - int ret = 0; - - switch (action) - { - case EZLOPI_ACTION_PREPARE: - { - ret = sensor_touch_tpp_223b_prepare_and_add(arg); - break; - } - case EZLOPI_ACTION_INITIALIZE: - { - ret = sensor_touch_tpp_223b_init(ezlo_device); - break; - } - case EZLOPI_ACTION_GET_EZLOPI_VALUE: - { - ret = sensor_touch_tpp_223b_get_value_cjson(ezlo_device, arg); - break; - } - - default: - { - break; - } - } - - return ret; -} - -static int sensor_touch_tpp_223b_prepare_and_add(void *args) -{ - int ret = 0; - s_ezlopi_prep_arg_t *device_prep_arg = (s_ezlopi_prep_arg_t *)args; - - if ((NULL != device_prep_arg) && (NULL != device_prep_arg->cjson_device)) - { - s_ezlopi_device_properties_t *sensor_touch_tpp_223b_properties = sensor_touch_tpp_223b_prepare(device_prep_arg->cjson_device); - if (sensor_touch_tpp_223b_properties) - { - if (0 == ezlopi_devices_list_add(device_prep_arg->device, sensor_touch_tpp_223b_properties, NULL)) - { - free(sensor_touch_tpp_223b_properties); - } - else - { - ret = 1; - } - } - } - - return ret; -} - -static s_ezlopi_device_properties_t *sensor_touch_tpp_223b_prepare(cJSON *cjson_device) -{ - s_ezlopi_device_properties_t *sensor_touch_tpp_223b_properties = malloc(sizeof(s_ezlopi_device_properties_t)); - - if (sensor_touch_tpp_223b_properties) - { - memset(sensor_touch_tpp_223b_properties, 0, sizeof(s_ezlopi_device_properties_t)); - sensor_touch_tpp_223b_properties->interface_type = EZLOPI_DEVICE_INTERFACE_DIGITAL_INPUT; - - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cjson_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME(sensor_touch_tpp_223b_properties, device_name); - sensor_touch_tpp_223b_properties->ezlopi_cloud.category = category_switch; - sensor_touch_tpp_223b_properties->ezlopi_cloud.subcategory = subcategory_in_wall; - sensor_touch_tpp_223b_properties->ezlopi_cloud.item_name = ezlopi_item_name_switch; - sensor_touch_tpp_223b_properties->ezlopi_cloud.device_type = dev_type_switch_outlet; - sensor_touch_tpp_223b_properties->ezlopi_cloud.value_type = value_type_bool; - sensor_touch_tpp_223b_properties->ezlopi_cloud.has_getter = true; - sensor_touch_tpp_223b_properties->ezlopi_cloud.has_setter = false; - sensor_touch_tpp_223b_properties->ezlopi_cloud.reachable = true; - sensor_touch_tpp_223b_properties->ezlopi_cloud.battery_powered = false; - sensor_touch_tpp_223b_properties->ezlopi_cloud.show = true; - sensor_touch_tpp_223b_properties->ezlopi_cloud.room_name[0] = '\0'; - sensor_touch_tpp_223b_properties->ezlopi_cloud.device_id = ezlopi_cloud_generate_device_id(); - sensor_touch_tpp_223b_properties->ezlopi_cloud.room_id = ezlopi_cloud_generate_room_id(); - sensor_touch_tpp_223b_properties->ezlopi_cloud.item_id = ezlopi_cloud_generate_item_id(); - - CJSON_GET_VALUE_INT(cjson_device, "gpio", sensor_touch_tpp_223b_properties->interface.gpio.gpio_in.gpio_num); - CJSON_GET_VALUE_INT(cjson_device, "ip_inv", sensor_touch_tpp_223b_properties->interface.gpio.gpio_in.invert); - CJSON_GET_VALUE_INT(cjson_device, "val_ip", sensor_touch_tpp_223b_properties->interface.gpio.gpio_in.value); - - sensor_touch_tpp_223b_properties->interface.gpio.gpio_in.enable = true; - sensor_touch_tpp_223b_properties->interface.gpio.gpio_in.interrupt = GPIO_INTR_POSEDGE; - sensor_touch_tpp_223b_properties->interface.gpio.gpio_in.pull = GPIO_PULLDOWN_ONLY; - } - - return sensor_touch_tpp_223b_properties; -} - -static int sensor_touch_tpp_223b_init(s_ezlopi_device_properties_t *properties) -{ - int ret = 0; - if (GPIO_IS_VALID_GPIO(properties->interface.gpio.gpio_in.gpio_num)) - { - const gpio_config_t io_conf = { - .pin_bit_mask = (1ULL << properties->interface.gpio.gpio_in.gpio_num), - .mode = GPIO_MODE_INPUT, - .pull_up_en = GPIO_PULLUP_DISABLE, - .pull_down_en = (properties->interface.gpio.gpio_in.pull == GPIO_PULLDOWN_ONLY) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE, - .intr_type = GPIO_INTR_POSEDGE, - }; - - ret = gpio_config(&io_conf); - if (ret) - { - TRACE_E("Error initializing PIR sensor"); - } - else - { - TRACE_I("PIR sensor initialize successfully."); - properties->interface.gpio.gpio_in.value = gpio_get_level(properties->interface.gpio.gpio_in.gpio_num); - } - - gpio_isr_service_register(properties, sensor_touch_tpp_223b_value_updated_from_device, 200); - } - - return ret; -} - -static void sensor_touch_tpp_223b_value_updated_from_device(s_ezlopi_device_properties_t *properties) -{ - ezlopi_device_value_updated_from_device(properties); -} - -static int sensor_touch_tpp_223b_get_value_cjson(s_ezlopi_device_properties_t *properties, void *args) -{ - int ret = 0; - cJSON *cjson_propertise = (cJSON *)args; - if (cjson_propertise) - { - int gpio_level = gpio_get_level(properties->interface.gpio.gpio_in.gpio_num); - properties->interface.gpio.gpio_in.value = 0 == properties->interface.gpio.gpio_in.invert ? gpio_level : !gpio_level; - cJSON_AddBoolToObject(cjson_propertise, "value", properties->interface.gpio.gpio_in.value); - ret = 1; - } - - return ret; -} -#endif // if 0 diff --git a/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.c b/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.c index 741e6977d..ddd277a74 100644 --- a/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.c +++ b/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.c @@ -12,22 +12,32 @@ #include "ezlopi_timer.h" #include "ezlopi_cloud.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" #include "sensor_0024_other_HCSR04.h" -#warning "use of static variable" static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; +/** + * Device descriptor + */ +typedef struct +{ + uint32_t trigger_pin; //!< GPIO output pin for trigger + uint32_t echo_pin; //!< GPIO input pin for echo + uint32_t distance; // distance in cm +} s_ultrasonic_sensor_t; + static int __prepare(void *arg); static int __init(l_ezlopi_item_t *item); static int __notify(l_ezlopi_item_t *item); static int __get_value_cjson(l_ezlopi_item_t *item, void *arg); static bool ezlopi_sensor_0024_other_HCSR04_get_from_sensor(l_ezlopi_item_t *item); -static esp_err_t ultrasonic_measure(const ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance); -static esp_err_t ultrasonic_measure_raw(const ultrasonic_sensor_t *dev, uint32_t max_time_us, uint32_t *time_us); +static esp_err_t ultrasonic_measure(const s_ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance); +static esp_err_t ultrasonic_measure_raw(const s_ultrasonic_sensor_t *dev, uint32_t max_time_us, uint32_t *time_us); int sensor_0024_other_HCSR04_v3(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) { @@ -69,14 +79,14 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) { int ret = 0; - ultrasonic_sensor_t *ultrasonic_sensor = (ultrasonic_sensor_t *)item->user_arg; + s_ultrasonic_sensor_t *ultrasonic_sensor = (s_ultrasonic_sensor_t *)item->user_arg; cJSON *cj_param = (cJSON *)arg; char valueFormatted[20]; if (cj_param && ultrasonic_sensor) { snprintf(valueFormatted, 20, "%d cm", ultrasonic_sensor->distance); - cJSON_AddStringToObject(cj_param, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_param, "value", ultrasonic_sensor->distance); + cJSON_AddStringToObject(cj_param, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_param, ezlopi_value_str, ultrasonic_sensor->distance); } return ret; @@ -115,6 +125,7 @@ static int __init(l_ezlopi_item_t *item) }; gpio_config(&io_conf); + ret = 1; } if (GPIO_IS_VALID_GPIO(item->interface.gpio.gpio_in.gpio_num)) @@ -136,22 +147,34 @@ static int __init(l_ezlopi_item_t *item) }; gpio_config(&io_conf); + ret = 1; + } + + if (0 == ret) + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } } + return ret; } static void __setup_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) @@ -164,8 +187,8 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) item->cloud_properties.scale = scales_centi_meter; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_out.gpio_num); - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_out.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.gpio.gpio_in.gpio_num); item->interface.gpio.gpio_out.enable = true; item->interface.gpio.gpio_out.interrupt = GPIO_INTR_DISABLE; @@ -191,7 +214,7 @@ static int __prepare(void *arg) cJSON *cj_device = prep_arg->cjson_device; if (cj_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __setup_device_cloud_properties(device, cj_device); @@ -200,10 +223,10 @@ static int __prepare(void *arg) { item->cloud_properties.device_id = device->cloud_properties.device_id; __setup_item_properties(item, cj_device); - ultrasonic_sensor_t *ultrasonic_sensor = (ultrasonic_sensor_t *)malloc(sizeof(ultrasonic_sensor_t)); + s_ultrasonic_sensor_t *ultrasonic_sensor = (s_ultrasonic_sensor_t *)malloc(sizeof(s_ultrasonic_sensor_t)); if (ultrasonic_sensor) { - memset(ultrasonic_sensor, 0, sizeof(ultrasonic_sensor_t)); + memset(ultrasonic_sensor, 0, sizeof(s_ultrasonic_sensor_t)); ultrasonic_sensor->distance = 0; ultrasonic_sensor->trigger_pin = item->interface.gpio.gpio_out.gpio_num; ultrasonic_sensor->echo_pin = item->interface.gpio.gpio_in.gpio_num; @@ -212,7 +235,7 @@ static int __prepare(void *arg) } else { - ezlopi_device_free_device(device); + ret = -1; } } } @@ -222,9 +245,7 @@ static int __prepare(void *arg) static bool ezlopi_sensor_0024_other_HCSR04_get_from_sensor(l_ezlopi_item_t *item) { - uint32_t distance; - - ultrasonic_sensor_t *ultrasonic_HCSR04_sensor = (ultrasonic_sensor_t *)item->user_arg; + s_ultrasonic_sensor_t *ultrasonic_HCSR04_sensor = (s_ultrasonic_sensor_t *)item->user_arg; if (ultrasonic_HCSR04_sensor) { esp_err_t res = ultrasonic_measure(ultrasonic_HCSR04_sensor, MAX_DISTANCE_CM, &ultrasonic_HCSR04_sensor->distance); @@ -254,10 +275,11 @@ static bool ezlopi_sensor_0024_other_HCSR04_get_from_sensor(l_ezlopi_item_t *ite vTaskDelay(500 / portTICK_PERIOD_MS); } } + return true; } -static esp_err_t ultrasonic_measure(const ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance) +static esp_err_t ultrasonic_measure(const s_ultrasonic_sensor_t *dev, uint32_t max_distance, uint32_t *distance) { CHECK_ARG(dev && distance); @@ -269,7 +291,7 @@ static esp_err_t ultrasonic_measure(const ultrasonic_sensor_t *dev, uint32_t max return ESP_OK; } -static esp_err_t ultrasonic_measure_raw(const ultrasonic_sensor_t *dev, uint32_t max_time_us, uint32_t *time_us) +static esp_err_t ultrasonic_measure_raw(const s_ultrasonic_sensor_t *dev, uint32_t max_time_us, uint32_t *time_us) { CHECK_ARG(dev && time_us); diff --git a/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.h b/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.h index 7af18f0eb..eb797cd5e 100644 --- a/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.h +++ b/devices/sensor_0024_other_HCSR04/sensor_0024_other_HCSR04.h @@ -3,7 +3,6 @@ #ifndef _ULTRASONIC_HC_SR04_H_ #define _ULTRASONIC_HC_SR04_H_ - #include "ezlopi_actions.h" #include "ezlopi_devices.h" #include "ezlopi_uart.h" @@ -11,8 +10,7 @@ #include #include - -#define ESP_ERR_ULTRASONIC_PING 0x200 +#define ESP_ERR_ULTRASONIC_PING 0x200 #define ESP_ERR_ULTRASONIC_PING_TIMEOUT 0x201 #define ESP_ERR_ULTRASONIC_ECHO_TIMEOUT 0x202 @@ -28,22 +26,26 @@ #define timeout_expired(start, len) ((esp_timer_get_time() - (start)) >= (len)) -#define CHECK_ARG(VAL) do { if (!(VAL)) return ESP_ERR_INVALID_ARG; } while (0) -#define CHECK(x) do { esp_err_t __; if ((__ = x) != ESP_OK) return __; } while (0) -#define RETURN_CRITICAL(RES) do { PORT_EXIT_CRITICAL; return RES; } while(0) - -/** - * Device descriptor - */ -typedef struct -{ - uint32_t trigger_pin; //!< GPIO output pin for trigger - uint32_t echo_pin; //!< GPIO input pin for echo - uint32_t distance; // distance in cm -} ultrasonic_sensor_t; - +#define CHECK_ARG(VAL) \ + do \ + { \ + if (!(VAL)) \ + return ESP_ERR_INVALID_ARG; \ + } while (0) +#define CHECK(x) \ + do \ + { \ + esp_err_t __; \ + if ((__ = x) != ESP_OK) \ + return __; \ + } while (0) +#define RETURN_CRITICAL(RES) \ + do \ + { \ + PORT_EXIT_CRITICAL; \ + return RES; \ + } while (0) int sensor_0024_other_HCSR04_v3(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg); - #endif //_ULTRASONIC_HC_SR04_H_ \ No newline at end of file diff --git a/devices/sensor_0025_digitalIn_LDR/sensor_0025_digitalIn_LDR.c b/devices/sensor_0025_digitalIn_LDR/sensor_0025_digitalIn_LDR.c index ce1e673ba..a8ed61fc9 100644 --- a/devices/sensor_0025_digitalIn_LDR/sensor_0025_digitalIn_LDR.c +++ b/devices/sensor_0025_digitalIn_LDR/sensor_0025_digitalIn_LDR.c @@ -4,6 +4,7 @@ #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -19,7 +20,7 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cj_device); static void __setup_device_properties(l_ezlopi_device_t *device, cJSON *cj_device); static void __value_updated_from_interrupt(void *arg); -int sensor_0025_digitalIn_LDR(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void **user_arg) +int sensor_0025_digitalIn_LDR(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) { int ret = 0; @@ -59,9 +60,9 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) cJSON *cj_value_obj = (cJSON *)arg; int gpio_level = gpio_get_level(item->interface.gpio.gpio_in.gpio_num); item->interface.gpio.gpio_in.value = (0 == item->interface.gpio.gpio_in.invert) ? gpio_level : !gpio_level; - cJSON_AddBoolToObject(cj_value_obj, "value", item->interface.gpio.gpio_in.value); + cJSON_AddBoolToObject(cj_value_obj, ezlopi_value_str, item->interface.gpio.gpio_in.value); char *valueFormatted = ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? true : false); - cJSON_AddStringToObject(cj_value_obj, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_value_obj, ezlopi_valueFormatted_str, valueFormatted); ret = 1; } return ret; @@ -88,6 +89,16 @@ static int __init(l_ezlopi_item_t *item) } gpio_isr_service_register_v3(item, __value_updated_from_interrupt, 200); + ret = 1; + } + else + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } } } return ret; @@ -110,7 +121,7 @@ static int __prepare(void *arg) cJSON *cj_device = prep_arg->cjson_device; if (cj_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __setup_device_properties(device, cj_device); @@ -137,10 +148,10 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); CJSON_GET_VALUE_INT(cj_device, "logic_inv", item->interface.gpio.gpio_in.invert); - CJSON_GET_VALUE_INT(cj_device, "val_ip", item->interface.gpio.gpio_in.value); + CJSON_GET_VALUE_INT(cj_device, ezlopi_val_ip_str, item->interface.gpio.gpio_in.value); item->interface.gpio.gpio_in.enable = true; item->interface.gpio.gpio_in.interrupt = GPIO_INTR_ANYEDGE; item->interface.gpio.gpio_in.pull = GPIO_PULLDOWN_ONLY; @@ -148,12 +159,14 @@ static void __setup_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) static void __setup_device_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_switch; device->cloud_properties.subcategory = subcategory_in_wall; device->cloud_properties.device_type = dev_type_switch_outlet; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); -} \ No newline at end of file + device->cloud_properties.info = NULL; + device->cloud_properties.device_type_id = NULL; +} diff --git a/devices/sensor_0026_ADC_LDR/sensor_0026_ADC_LDR.c b/devices/sensor_0026_ADC_LDR/sensor_0026_ADC_LDR.c index a432aae33..0ba48116a 100644 --- a/devices/sensor_0026_ADC_LDR/sensor_0026_ADC_LDR.c +++ b/devices/sensor_0026_ADC_LDR/sensor_0026_ADC_LDR.c @@ -1,6 +1,7 @@ #include "ezlopi_adc.h" #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -105,18 +106,21 @@ static int __prepare(void *arg) if (prep_arg && prep_arg->cjson_device) { cJSON *cj_device = prep_arg->cjson_device; - l_ezlopi_device_t *device = ezlopi_device_add_device(); - if (device) + if (cj_device) { - __setup_device_cloud_params(device, cj_device); - l_ezlopi_item_t *item = ezlopi_device_add_item_to_device(device, sensor_0026_ADC_LDR); - if (item) + l_ezlopi_device_t *device = ezlopi_device_add_device(cj_device); + if (device) { - __setup_item_cloud_properties(item, cj_device); - } - else - { - ezlopi_device_free_device(device); + __setup_device_cloud_params(device, cj_device); + l_ezlopi_item_t *item = ezlopi_device_add_item_to_device(device, sensor_0026_ADC_LDR); + if (item) + { + __setup_item_cloud_properties(item, cj_device); + } + else + { + ezlopi_device_free_device(device); + } } } } @@ -154,10 +158,10 @@ static int _get_item_list(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } - cJSON_AddStringToObject(cj_result, "value", ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); - cJSON_AddStringToObject(cj_result, "valueFormatted", ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); ret = 1; } @@ -170,8 +174,8 @@ static int __get_value_cjson(l_ezlopi_item_t *item, void *arg) cJSON *cj_result = (cJSON *)arg; if (cj_result && item) { - cJSON_AddStringToObject(cj_result, "value", ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); - cJSON_AddStringToObject(cj_result, "valueFormatted", ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, ((char *)item->user_arg) ? item->user_arg : light_alarm_states[0]); ret = 1; } return ret; diff --git a/devices/sensor_0027_ADC_waterLeak/sensor_0027_ADC_waterLeak.c b/devices/sensor_0027_ADC_waterLeak/sensor_0027_ADC_waterLeak.c index 575497069..652cf4a4b 100644 --- a/devices/sensor_0027_ADC_waterLeak/sensor_0027_ADC_waterLeak.c +++ b/devices/sensor_0027_ADC_waterLeak/sensor_0027_ADC_waterLeak.c @@ -1,5 +1,6 @@ #include "ezlopi_adc.h" #include "ezlopi_cloud.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -14,11 +15,11 @@ const static char *_no_water_leak = "no_water_leak"; const static char *_water_leak_detected = "water_leak_detected"; -const char *water_leak_alarm_states[] = - { - "no_water_leak", - "water_leak_detected", - "unknown"}; +const char *water_leak_alarm_states[] = { + "no_water_leak", + "water_leak_detected", + "unknown", +}; static int __prepare(void *arg); static int __init(l_ezlopi_item_t *item); @@ -68,16 +69,16 @@ int sensor_0027_ADC_waterLeak(e_ezlopi_actions_t action, l_ezlopi_item_t *item, static void prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_leak; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) @@ -94,7 +95,7 @@ static void prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_devic static void prepare_item_interface_properties(l_ezlopi_item_t *item, cJSON *cj_device) { item->interface_type = EZLOPI_DEVICE_INTERFACE_ANALOG_INPUT; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; } @@ -108,7 +109,7 @@ static int __prepare(void *arg) cJSON *cj_device = prep_arg->cjson_device; if (cj_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { prepare_device_cloud_properties(device, cj_device); @@ -143,16 +144,17 @@ static int __get_item_list(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cjson_propertise, "enum", json_array_enum); + cJSON_AddItemToObject(cjson_propertise, ezlopi_enum_str, json_array_enum); } - cJSON_AddStringToObject(cjson_propertise, "value", (char *)item->user_arg ? item->user_arg : _no_water_leak); - cJSON_AddStringToObject(cjson_propertise, "valueFormatted", (char *)item->user_arg ? item->user_arg : _no_water_leak); + cJSON_AddStringToObject(cjson_propertise, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : _no_water_leak); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : _no_water_leak); ret = 1; } return ret; } + static int __get_ezlopi_value(l_ezlopi_item_t *item, void *arg) { int ret = 0; @@ -161,8 +163,8 @@ static int __get_ezlopi_value(l_ezlopi_item_t *item, void *arg) cJSON *cj_result = (cJSON *)arg; if (cj_result) { - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : _no_water_leak); - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : _no_water_leak); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : _no_water_leak); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : _no_water_leak); ret = 1; } } @@ -175,7 +177,7 @@ static int __notify(l_ezlopi_item_t *item) if (item) { - char *curret_value = NULL; + const char *curret_value = NULL; s_ezlopi_analog_data_t ezlopi_analog_data = {.value = 0, .voltage = 0}; ezlopi_adc_get_adc_data(item->interface.adc.gpio_num, &ezlopi_analog_data); @@ -205,13 +207,21 @@ static int __init(l_ezlopi_item_t *item) int ret = 0; if (item) { - int ret = 0; if (GPIO_IS_VALID_GPIO(item->interface.adc.gpio_num)) { ezlopi_adc_init(item->interface.adc.gpio_num, item->interface.adc.resln_bit); ret = 1; } - return ret; + else + { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } + } } + return ret; } diff --git a/devices/sensor_0028_other_GY61/sensor_0028_other_GY61.c b/devices/sensor_0028_other_GY61/sensor_0028_other_GY61.c index 9034fa6ab..d4f0e08b5 100644 --- a/devices/sensor_0028_other_GY61/sensor_0028_other_GY61.c +++ b/devices/sensor_0028_other_GY61/sensor_0028_other_GY61.c @@ -125,7 +125,7 @@ static int __0028_prepare(void *arg) if (NULL != gy61_value) { memset(gy61_value, 0, sizeof(s_gy61_data_t)); - l_ezlopi_device_t *gy61_device = ezlopi_device_add_device(); + l_ezlopi_device_t *gy61_device = ezlopi_device_add_device(cj_device); if (gy61_device) { __prepare_device_cloud_properties(gy61_device, cj_device); diff --git a/devices/sensor_0029_I2C_GXHTC3/sensor_0029_I2C_GXHTC3.c b/devices/sensor_0029_I2C_GXHTC3/sensor_0029_I2C_GXHTC3.c index 319d912ef..284a1b312 100644 --- a/devices/sensor_0029_I2C_GXHTC3/sensor_0029_I2C_GXHTC3.c +++ b/devices/sensor_0029_I2C_GXHTC3/sensor_0029_I2C_GXHTC3.c @@ -2,6 +2,7 @@ #include "math.h" #include "ezlopi_cloud.h" #include "ezlopi_i2c_master.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -234,7 +235,7 @@ static void __prepare_device_cloud_properties_hum(l_ezlopi_device_t *device, cJS static void __prepare_temperature_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.item_name = ezlopi_item_name_temp; @@ -247,14 +248,14 @@ static void __prepare_temperature_item_properties(l_ezlopi_item_t *item, cJSON * item->interface.i2c_master.enable = true; item->interface.i2c_master.channel = 0; item->interface.i2c_master.clock_speed = 400000; - CJSON_GET_VALUE_INT(cj_device, "gpio_scl", item->interface.i2c_master.scl); - CJSON_GET_VALUE_INT(cj_device, "gpio_sda", item->interface.i2c_master.sda); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_scl_str, item->interface.i2c_master.scl); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_sda_str, item->interface.i2c_master.sda); CJSON_GET_VALUE_INT(cj_device, "slave_addr", item->interface.i2c_master.address); } static void __prepare_humidity_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.item_name = ezlopi_item_name_humidity; @@ -267,8 +268,8 @@ static void __prepare_humidity_item_properties(l_ezlopi_item_t *item, cJSON *cj_ item->interface.i2c_master.enable = false; item->interface.i2c_master.channel = 0; item->interface.i2c_master.clock_speed = 400000; - CJSON_GET_VALUE_INT(cj_device, "gpio_scl", item->interface.i2c_master.scl); - CJSON_GET_VALUE_INT(cj_device, "gpio_sda", item->interface.i2c_master.sda); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_scl_str, item->interface.i2c_master.scl); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_sda_str, item->interface.i2c_master.sda); CJSON_GET_VALUE_INT(cj_device, "slave_addr", item->interface.i2c_master.address); } @@ -284,7 +285,7 @@ static int __prepare(void *arg) if (value_ptr) { memset(value_ptr, 0, sizeof(s_gxhtc3_value_t)); - l_ezlopi_device_t *device_temp = ezlopi_device_add_device(); + l_ezlopi_device_t *device_temp = ezlopi_device_add_device(prep_arg->cjson_device); if (device_temp) { __prepare_device_cloud_properties_temp(device_temp, prep_arg->cjson_device); @@ -302,7 +303,7 @@ static int __prepare(void *arg) free(value_ptr); } } - l_ezlopi_device_t *device_hum = ezlopi_device_add_device(); + l_ezlopi_device_t *device_hum = ezlopi_device_add_device(prep_arg->cjson_device); if (device_hum) { __prepare_device_cloud_properties_hum(device_hum, prep_arg->cjson_device); diff --git a/devices/sensor_0030_oneWire_DS18B20/sensor_0030_oneWire_DS18B20.c b/devices/sensor_0030_oneWire_DS18B20/sensor_0030_oneWire_DS18B20.c index bfa5d4aee..743217a88 100644 --- a/devices/sensor_0030_oneWire_DS18B20/sensor_0030_oneWire_DS18B20.c +++ b/devices/sensor_0030_oneWire_DS18B20/sensor_0030_oneWire_DS18B20.c @@ -3,6 +3,7 @@ #include "ezlopi_adc.h" #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -91,18 +92,18 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) { cJSON *cj_result = (cJSON *)arg; double *temperatue_value = (double *)item->user_arg; - cJSON_AddNumberToObject(cj_result, "value", *temperatue_value); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, *temperatue_value); char *valueFormatted = ezlopi_valueformatter_double(*temperatue_value); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); - cJSON_AddStringToObject(cj_result, "scale", "celsius"); + cJSON_AddStringToObject(cj_result, ezlopi_scale_str, scales_celsius); } return ret; } static int __init(l_ezlopi_item_t *item) { - int ret = 0; + int ret = -1; if (item->interface.onewire_master.enable) { @@ -116,6 +117,23 @@ static int __init(l_ezlopi_item_t *item) ds18b20_get_temperature_data(temperature_prev_value, item->interface.onewire_master.onewire_pin); ret = 1; } + else + { + ret = -1; + } + } + else + { + ret = -1; + } + + if (-1 == ret) + { + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } } } return ret; @@ -123,21 +141,21 @@ static int __init(l_ezlopi_item_t *item) static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_temperature; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.show = true; item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; @@ -148,7 +166,7 @@ static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device) item->interface_type = EZLOPI_DEVICE_INTERFACE_ONEWIRE_MASTER; item->interface.onewire_master.enable = true; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.onewire_master.onewire_pin); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.onewire_master.onewire_pin); } static int __prepare(void *arg) @@ -158,7 +176,7 @@ static int __prepare(void *arg) if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __prepare_device_cloud_properties(device, prep_arg->cjson_device); @@ -170,7 +188,7 @@ static int __prepare(void *arg) } if (NULL == item_temperature) { - ezlopi_device_free_device(device); + ret = -1; } else { @@ -192,144 +210,6 @@ static int __prepare(void *arg) return ret; } -#if 0 -static int ds18b20_sensor_prepare_and_add(void *args); -static s_ezlopi_device_properties_t *ds18b20_sensor_prepare(cJSON *cjson_device); -static int ds18b20_sensor_init(s_ezlopi_device_properties_t *properties); -static int get_ds18b20_sensor_value_to_cloud(s_ezlopi_device_properties_t *properties, void *args); - -int ds18b20_sensor(e_ezlopi_actions_t action, s_ezlopi_device_properties_t *ezlo_device, void *arg, void *user_arg) -{ - int ret = 0; - - switch (action) - { - case EZLOPI_ACTION_PREPARE: - { - ret = ds18b20_sensor_prepare_and_add(arg); - break; - } - case EZLOPI_ACTION_INITIALIZE: - { - ret = ds18b20_sensor_init(ezlo_device); - break; - } - case EZLOPI_ACTION_GET_EZLOPI_VALUE: - { - ret = get_ds18b20_sensor_value_to_cloud(ezlo_device, arg); - break; - } - case EZLOPI_ACTION_NOTIFY_1000_MS: - { - ezlopi_device_value_updated_from_device(ezlo_device); - break; - } - default: - { - break; - } - } - return ret; -} - -static int ds18b20_sensor_prepare_and_add(void *args) -{ - int ret = 0; - s_ezlopi_prep_arg_t *device_prep_arg = (s_ezlopi_prep_arg_t *)args; - - if ((NULL != device_prep_arg) && (NULL != device_prep_arg->cjson_device)) - { - s_ezlopi_device_properties_t *ds18b20_sensor_properties = ds18b20_sensor_prepare(device_prep_arg->cjson_device); - if (ds18b20_sensor_properties) - { - if (0 == ezlopi_devices_list_add(device_prep_arg->device, ds18b20_sensor_properties, NULL)) - { - free(ds18b20_sensor_properties); - } - else - { - ret = 1; - } - } - } - - return ret; -} - -static s_ezlopi_device_properties_t *ds18b20_sensor_prepare(cJSON *cjson_device) -{ - s_ezlopi_device_properties_t *ds18b20_sensor_properties = malloc(sizeof(s_ezlopi_device_properties_t)); - - if (ds18b20_sensor_properties) - { - memset(ds18b20_sensor_properties, 0, sizeof(s_ezlopi_device_properties_t)); - ds18b20_sensor_properties->interface_type = EZLOPI_DEVICE_INTERFACE_ANALOG_INPUT; - - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cjson_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME(ds18b20_sensor_properties, device_name); - ds18b20_sensor_properties->ezlopi_cloud.category = category_temperature; - ds18b20_sensor_properties->ezlopi_cloud.subcategory = subcategory_not_defined; - ds18b20_sensor_properties->ezlopi_cloud.item_name = ezlopi_item_name_temp; - ds18b20_sensor_properties->ezlopi_cloud.device_type = dev_type_sensor; - ds18b20_sensor_properties->ezlopi_cloud.value_type = value_type_temperature; - ds18b20_sensor_properties->ezlopi_cloud.has_getter = true; - ds18b20_sensor_properties->ezlopi_cloud.has_setter = false; - ds18b20_sensor_properties->ezlopi_cloud.reachable = true; - ds18b20_sensor_properties->ezlopi_cloud.battery_powered = false; - ds18b20_sensor_properties->ezlopi_cloud.show = true; - ds18b20_sensor_properties->ezlopi_cloud.room_name[0] = '\0'; - ds18b20_sensor_properties->ezlopi_cloud.device_id = ezlopi_cloud_generate_device_id(); - ds18b20_sensor_properties->ezlopi_cloud.room_id = ezlopi_cloud_generate_room_id(); - ds18b20_sensor_properties->ezlopi_cloud.item_id = ezlopi_cloud_generate_item_id(); - - CJSON_GET_VALUE_INT(cjson_device, "gpio", ds18b20_sensor_properties->interface.gpio.gpio_in.gpio_num); - ds18b20_sensor_properties->interface.gpio.gpio_out.enable = false; - ds18b20_sensor_properties->interface.gpio.gpio_in.enable = true; - // ds18b20_sensor_properties->interface.gpio.gpio_in.gpio_num = 2; - ds18b20_sensor_properties->interface.gpio.gpio_in.interrupt = GPIO_INTR_DISABLE; - ds18b20_sensor_properties->interface.gpio.gpio_in.invert = false; - ds18b20_sensor_properties->interface.gpio.gpio_in.mode = GPIO_MODE_DISABLE; - ds18b20_sensor_properties->interface.gpio.gpio_in.pull = GPIO_FLOATING; - ds18b20_sensor_properties->interface.gpio.gpio_in.value = 0; - } - return ds18b20_sensor_properties; -} - -static int ds18b20_sensor_init(s_ezlopi_device_properties_t *properties) -{ - int ret = 0; - if (ds18b20_reset_line(properties->interface.gpio.gpio_in.gpio_num)) - { - if (ds18b20_recognize_device(properties->interface.gpio.gpio_in.gpio_num)) - { - TRACE_B("Providing initial settings to DS18B20."); - ds18b20_write_to_scratchpad(DS18B20_TH_HIGHER_THRESHOLD, DS18B20_TL_LOWER_THRESHOLD, 12, properties->interface.gpio.gpio_in.gpio_num); - ret = 1; - } - } - return ret; -} - -static int get_ds18b20_sensor_value_to_cloud(s_ezlopi_device_properties_t *properties, void *args) -{ - int ret = 0; - double temperature = 0; - cJSON *cjson_properties = (cJSON *)args; - - if (cjson_properties) - { - ds18b20_get_temperature_data(&temperature, properties->interface.gpio.gpio_in.gpio_num); - TRACE_B("Temperature is: %f degree censius", temperature); - cJSON_AddNumberToObject(cjson_properties, "value", temperature); - cJSON_AddStringToObject(cjson_properties, "scale", "celsius"); - } - - return ret; -} - -#endif - static esp_err_t ds18b20_write_data(uint8_t *data, uint32_t gpio_pin) { esp_err_t error = ESP_OK; diff --git a/devices/sensor_0031_other_JSNSR04T/sensor_0031_other_JSNSR04T.c b/devices/sensor_0031_other_JSNSR04T/sensor_0031_other_JSNSR04T.c index 4a40464d8..657fe236a 100644 --- a/devices/sensor_0031_other_JSNSR04T/sensor_0031_other_JSNSR04T.c +++ b/devices/sensor_0031_other_JSNSR04T/sensor_0031_other_JSNSR04T.c @@ -1,14 +1,11 @@ -#include "jsn_sr04t.h" - #include "trace.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" -#include "ezlopi_valueformatter.h" - -#include "sensor_0031_other_JSNSR04T.h" +#include "jsn_sr04t.h" #include "sensor_0031_other_JSNSR04T.h" static int __prepare(void *arg); @@ -72,31 +69,13 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) // jsn_sr04t_print_data(jsn_sr04t_data); float distance = (jsn_sr04t_data.distance_cm / 100.0f); - cJSON_AddNumberToObject(cj_result, "value", distance); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, distance); char *valueFormatted = ezlopi_valueformatter_float(distance); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); - cJSON_AddStringToObject(cj_result, "scale", "meter"); + cJSON_AddStringToObject(cj_result, ezlopi_scale_str, scales_meter); ret = 1; -#if 0 - if (jsn_sr04t_data.distance_cm >= 50 && jsn_sr04t_data.distance_cm < 100) - { - cJSON_AddStringToObject(cj_result, "value", "water_level_ok"); - } - else if (jsn_sr04t_data.distance_cm >= 100) - { - cJSON_AddStringToObject(cj_result, "value", "water_level_below_low_threshold"); - } - else if (jsn_sr04t_data.distance_cm < 50) - { - cJSON_AddStringToObject(cj_result, "value", "water_level_above_high_threshold"); - } - else - { - cJSON_AddStringToObject(cj_result, "value", "unknown"); - } -#endif } else { @@ -114,7 +93,6 @@ static int __init(l_ezlopi_item_t *item) if (item) { - int ret = 0; jsn_sr04t_config_t *jsn_sr04t_config = malloc(sizeof(jsn_sr04t_config_t)); if (jsn_sr04t_config) { @@ -133,6 +111,9 @@ static int __init(l_ezlopi_item_t *item) } else { + ret = -1; + item->user_arg = NULL; + free(jsn_sr04t_config); TRACE_E("JSN_SR04T not initializeed"); } } @@ -143,19 +124,21 @@ static int __init(l_ezlopi_item_t *item) static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.info = NULL; + device->cloud_properties.device_type_id = NULL; } static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.item_name = ezlopi_item_name_distance; @@ -169,7 +152,7 @@ static void __prepare_item_interface_properties(l_ezlopi_item_t *item, cJSON *cj { item->interface_type = EZLOPI_DEVICE_INTERFACE_DIGITAL_OUTPUT; - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_out.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_out_str, item->interface.gpio.gpio_out.gpio_num); item->interface.gpio.gpio_out.enable = true; item->interface.gpio.gpio_out.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_out.invert = EZLOPI_GPIO_LOGIC_NONINVERTED; @@ -177,7 +160,7 @@ static void __prepare_item_interface_properties(l_ezlopi_item_t *item, cJSON *cj item->interface.gpio.gpio_out.pull = GPIO_PULLDOWN_ONLY; item->interface.gpio.gpio_out.value = 0; - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_in_str, item->interface.gpio.gpio_in.gpio_num); item->interface.gpio.gpio_in.enable = true; item->interface.gpio.gpio_in.interrupt = GPIO_INTR_DISABLE; item->interface.gpio.gpio_in.invert = EZLOPI_GPIO_LOGIC_NONINVERTED; @@ -193,7 +176,7 @@ static int __prepare(void *arg) if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __prepare_device_cloud_properties(device, prep_arg->cjson_device); diff --git a/devices/sensor_0032_ADC_soilMoisture/sensor_0032_ADC_soilMoisture.c b/devices/sensor_0032_ADC_soilMoisture/sensor_0032_ADC_soilMoisture.c index 2ee486d38..70b50bb85 100644 --- a/devices/sensor_0032_ADC_soilMoisture/sensor_0032_ADC_soilMoisture.c +++ b/devices/sensor_0032_ADC_soilMoisture/sensor_0032_ADC_soilMoisture.c @@ -4,6 +4,7 @@ #include "ezlopi_adc.h" #include "ezlopi_cloud.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -77,10 +78,9 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) cJSON *cj_result = (cJSON *)arg; s_ezlopi_analog_data_t *soil_moisture_data = (s_ezlopi_analog_data_t *)item->user_arg; double percent_data = ((4095 - soil_moisture_data->value) / 4095.0) * 100; - - cJSON_AddNumberToObject(cj_result, "value", percent_data); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, percent_data); char *valueFormatted = ezlopi_valueformatter_double(percent_data); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); ret = 1; } @@ -100,27 +100,27 @@ static int __init(l_ezlopi_item_t *item) static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_generic_sensor; device->cloud_properties.subcategory = subcategory_moisture; device->cloud_properties.device_type = dev_type_sensor; // cJSON *cj_info = cJSON_CreateObject(); - // cJSON_AddStringToObject(cj_info, "manufacturer", "EzloPi"); - // cJSON_AddStringToObject(cj_info, "model", "EzloPi Generic"); + // cJSON_AddStringToObject(cj_info, ezlopi_manufacturer_str, "EzloPi"); + // cJSON_AddStringToObject(cj_info, ezlopi_model_str, "EzloPi Generic"); // cJSON_AddStringToObject(cj_info, "protocol", "WiFi"); // cJSON_AddStringToObject(cj_info, "firmware.stack", "3.0.4"); // cJSON_AddStringToObject(cj_info, "hardware", "ESP32"); device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_arg) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.item_name = ezlopi_item_name_soil_humidity; @@ -131,7 +131,7 @@ static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device, v item->interface_type = EZLOPI_DEVICE_INTERFACE_ANALOG_INPUT; item->interface.adc.resln_bit = 3; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.adc.gpio_num); item->user_arg = user_arg; } @@ -142,7 +142,7 @@ static int __prepare(void *arg) if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __prepare_device_cloud_properties(device, prep_arg->cjson_device); diff --git a/devices/sensor_0033_ADC_turbidity/sensor_0033_ADC_turbidity.c b/devices/sensor_0033_ADC_turbidity/sensor_0033_ADC_turbidity.c index 5b4f126cb..f9fab0e67 100644 --- a/devices/sensor_0033_ADC_turbidity/sensor_0033_ADC_turbidity.c +++ b/devices/sensor_0033_ADC_turbidity/sensor_0033_ADC_turbidity.c @@ -5,6 +5,7 @@ #include "ezlopi_adc.h" #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -16,14 +17,15 @@ static int __notify(l_ezlopi_item_t *item); static int __get_cjson_value(l_ezlopi_item_t *item, void *arg); static int __get_item_list(l_ezlopi_item_t *item, void *arg); -const char *water_filter_replacement_alarm_states[] = { +#warning "Use of static variable, static variable can't be used!" +static const char *ezlopi_water_present_turbidity_state = NULL; + +static const char *water_filter_replacement_alarm_states[] = { "water_filter_ok", "replace_water_filter", "unknown", }; -static char *ezlopi_water_present_turbidity_state = NULL; - int sensor_0033_ADC_turbidity(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) { int ret = 0; @@ -79,7 +81,7 @@ static int __get_item_list(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cjson_propertise, "enum", json_array_enum); + cJSON_AddItemToObject(cjson_propertise, ezlopi_enum_str, json_array_enum); } s_ezlopi_analog_data_t ezlopi_analog_data = {.value = 0, .voltage = 0}; @@ -98,13 +100,13 @@ static int __get_item_list(l_ezlopi_item_t *item, void *arg) if (ezlopi_water_present_turbidity_state) { - cJSON_AddStringToObject(cjson_propertise, "value", ezlopi_water_present_turbidity_state); - cJSON_AddStringToObject(cjson_propertise, "valueFormatted", ezlopi_water_present_turbidity_state); + cJSON_AddStringToObject(cjson_propertise, ezlopi_value_str, ezlopi_water_present_turbidity_state); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueFormatted_str, ezlopi_water_present_turbidity_state); } else { - cJSON_AddStringToObject(cjson_propertise, "value", water_filter_replacement_alarm_states[TURBIDITY__UNKNOWN]); - cJSON_AddStringToObject(cjson_propertise, "valueFormatted", water_filter_replacement_alarm_states[TURBIDITY__UNKNOWN]); + cJSON_AddStringToObject(cjson_propertise, ezlopi_value_str, water_filter_replacement_alarm_states[TURBIDITY__UNKNOWN]); + cJSON_AddStringToObject(cjson_propertise, ezlopi_valueFormatted_str, water_filter_replacement_alarm_states[TURBIDITY__UNKNOWN]); } ret = 1; } @@ -114,9 +116,9 @@ static int __get_item_list(l_ezlopi_item_t *item, void *arg) static int __notify(l_ezlopi_item_t *item) { int ret = 0; - - char *tmp_sensor_state = NULL; + const char *tmp_sensor_state = NULL; char *turbidity_sensor_state = (char *)item->user_arg; + if (turbidity_sensor_state) { s_ezlopi_analog_data_t tmp_analog_data = {.value = 0, .voltage = 0}; @@ -135,6 +137,7 @@ static int __notify(l_ezlopi_item_t *item) ezlopi_device_value_updated_from_device_v3(item); } } + return ret; } @@ -147,8 +150,8 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) char *turbidity_sensor_state = (char *)item->user_arg; if (turbidity_sensor_state) { - cJSON_AddStringToObject(cj_result, "value", turbidity_sensor_state); - cJSON_AddStringToObject(cj_result, "valueFormatted", turbidity_sensor_state); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, turbidity_sensor_state); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, turbidity_sensor_state); } ret = 1; } @@ -168,16 +171,16 @@ static int __init(l_ezlopi_item_t *item) static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); - ASSIGN_DEVICE_NAME_V2(device, device_name); device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_water; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_arg) @@ -192,7 +195,7 @@ static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device, v item->interface_type = EZLOPI_DEVICE_INTERFACE_ANALOG_INPUT; item->interface.adc.resln_bit = 3; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.adc.gpio_num); item->user_arg = user_arg; } @@ -203,7 +206,7 @@ static int __prepare(void *arg) if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(prep_arg->cjson_device); if (device) { __prepare_device_cloud_properties(device, prep_arg->cjson_device); diff --git a/devices/sensor_0034_digitalIn_proximity/sensor_0034_digitalIn_proximity.c b/devices/sensor_0034_digitalIn_proximity/sensor_0034_digitalIn_proximity.c index c47488b09..143a6e934 100644 --- a/devices/sensor_0034_digitalIn_proximity/sensor_0034_digitalIn_proximity.c +++ b/devices/sensor_0034_digitalIn_proximity/sensor_0034_digitalIn_proximity.c @@ -1,13 +1,14 @@ #include "ezlopi_cloud.h" -#include "gpio_isr_service.h" -#include "ezlopi_cloud_value_type_str.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" +#include "ezlopi_cloud_constants.h" +#include "gpio_isr_service.h" #include "sensor_0034_digitalIn_proximity.h" static int proximity_sensor_prepare(void *args); static int proximity_sensor_init(l_ezlopi_item_t *item); -static void proximity_sensor_value_updated_from_device(l_ezlopi_item_t *item); +static void proximity_sensor_value_updated_from_device(void *arg); static int proximity_sensor_get_value_cjson(l_ezlopi_item_t *item, void *args); int sensor_0034_digitalIn_proximity(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *args, void *user_arg) @@ -46,16 +47,16 @@ static void proximity_sensor_setup_device_cloud_properties(l_ezlopi_device_t *de { if (device && cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); device->cloud_properties.category = category_generic_sensor; device->cloud_properties.subcategory = subcategory_motion; device->cloud_properties.device_type = dev_type_sensor_motion; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } } @@ -63,7 +64,6 @@ static void proximity_sensor_setup_item_properties(l_ezlopi_item_t *item, cJSON { if (item && cj_device) { - int tmp_var = 0; item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.item_name = ezlopi_item_name_motion; @@ -72,11 +72,11 @@ static void proximity_sensor_setup_item_properties(l_ezlopi_item_t *item, cJSON item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->interface.gpio.gpio_in.enable = true; item->interface.gpio.gpio_in.mode = GPIO_MODE_INPUT; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); CJSON_GET_VALUE_INT(cj_device, "logic_inv", item->interface.gpio.gpio_in.invert); // CJSON_GET_VALUE_INT(cj_device, "pull_up", tmp_var); item->interface.gpio.gpio_in.pull = GPIO_PULLUP_ONLY; // tmp_var ? GPIO_PULLUP_ONLY : GPIO_PULLDOWN_ONLY; @@ -91,7 +91,7 @@ static int proximity_sensor_prepare(void *args) if ((NULL != device_prep_arg) && (NULL != device_prep_arg->cjson_device)) { - l_ezlopi_device_t *device = ezlopi_device_add_device(); + l_ezlopi_device_t *device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (device) { proximity_sensor_setup_device_cloud_properties(device, device_prep_arg->cjson_device); @@ -104,7 +104,7 @@ static int proximity_sensor_prepare(void *args) } else { - ezlopi_device_free_device(device); + ret = -1; } } } @@ -141,9 +141,13 @@ static int proximity_sensor_init(l_ezlopi_item_t *item) return ret; } -static void proximity_sensor_value_updated_from_device(l_ezlopi_item_t *item) +static void proximity_sensor_value_updated_from_device(void *arg) { - ezlopi_device_value_updated_from_device_v3(item); + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; + if (item) + { + ezlopi_device_value_updated_from_device_v3(item); + } } static int proximity_sensor_get_value_cjson(l_ezlopi_item_t *item, void *args) @@ -157,10 +161,9 @@ static int proximity_sensor_get_value_cjson(l_ezlopi_item_t *item, void *args) { item->interface.gpio.gpio_in.value = item->interface.gpio.gpio_in.value ? false : true; } - item->interface.gpio.gpio_in.value = !item->interface.gpio.gpio_in.value; - cJSON_AddBoolToObject(cj_result, "value", item->interface.gpio.gpio_in.value); - char *valueFormatted = ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? true : false); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddBoolToObject(cj_result, ezlopi_value_str, item->interface.gpio.gpio_in.value); + char *valueFormatted = ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? false : true); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); ret = 1; // TRACE_D("value: %d", item->interface.gpio.gpio_in.value); } diff --git a/devices/sensor_0035_digitalIn_touch_sensor_TPP223B/sensor_0035_digitalIn_touch_sensor_TPP223B.c b/devices/sensor_0035_digitalIn_touch_sensor_TPP223B/sensor_0035_digitalIn_touch_sensor_TPP223B.c index 9f56b78af..6c5e2134f 100644 --- a/devices/sensor_0035_digitalIn_touch_sensor_TPP223B/sensor_0035_digitalIn_touch_sensor_TPP223B.c +++ b/devices/sensor_0035_digitalIn_touch_sensor_TPP223B/sensor_0035_digitalIn_touch_sensor_TPP223B.c @@ -8,14 +8,13 @@ #include "ezlopi_cloud.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" #include "sensor_0035_digitalIn_touch_sensor_TPP223B.h" -static bool sensor_bme280_initialized = false; - static int __prepare(void *arg); static void __prepare_touch_sensor_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device); static void __prepare_touch_sensor_properties(l_ezlopi_item_t *item, cJSON *cj_device); @@ -58,9 +57,9 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) cJSON *param = (cJSON *)arg; if (param) { - cJSON_AddBoolToObject(param, "value", item->interface.gpio.gpio_in.value); + cJSON_AddBoolToObject(param, ezlopi_value_str, item->interface.gpio.gpio_in.value); char *valueFormatted = ezlopi_valueformatter_bool(item->interface.gpio.gpio_in.value ? true : false); - cJSON_AddStringToObject(param, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(param, ezlopi_valueFormatted_str, valueFormatted); } return ret; @@ -83,6 +82,7 @@ static int __init(l_ezlopi_item_t *item) int gpio_level = gpio_get_level(item->interface.gpio.gpio_in.gpio_num); item->interface.gpio.gpio_in.value = (false == item->interface.gpio.gpio_in.invert) ? gpio_level : !gpio_level; gpio_isr_service_register_v3(item, __touch_switch_callback, 200); + ret = 1; return ret; } @@ -99,10 +99,10 @@ static int __prepare(void *arg) { int ret = 0; - s_ezlopi_prep_arg_t *prep_arg = (cJSON *)arg; + s_ezlopi_prep_arg_t *prep_arg = (s_ezlopi_prep_arg_t *)arg; if (prep_arg && prep_arg->cjson_device) { - l_ezlopi_device_t *touch_device = ezlopi_device_add_device(); + l_ezlopi_device_t *touch_device = ezlopi_device_add_device(prep_arg->cjson_device); if (touch_device) { __prepare_touch_sensor_device_cloud_properties(touch_device, prep_arg->cjson_device); @@ -117,10 +117,6 @@ static int __prepare(void *arg) ezlopi_device_free_device(touch_device); } } - else - { - ezlopi_device_free_device(touch_device); - } } return ret; @@ -128,32 +124,32 @@ static int __prepare(void *arg) static void __prepare_touch_sensor_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); device->cloud_properties.category = category_switch; device->cloud_properties.subcategory = subcategory_in_wall; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_touch_sensor_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; - item->cloud_properties.scale = true; + item->cloud_properties.scale = NULL; item->cloud_properties.item_name = ezlopi_item_name_switch; item->cloud_properties.value_type = value_type_bool; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); item->cloud_properties.scale = NULL; - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); - CJSON_GET_VALUE_INT(cj_device, "ip_inv", item->interface.gpio.gpio_in.invert); - CJSON_GET_VALUE_INT(cj_device, "val_ip", item->interface.gpio.gpio_in.value); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_ip_inv_str, item->interface.gpio.gpio_in.invert); + CJSON_GET_VALUE_INT(cj_device, ezlopi_val_ip_str, item->interface.gpio.gpio_in.value); item->interface.gpio.gpio_in.enable = true; item->interface.gpio.gpio_in.interrupt = GPIO_INTR_ANYEDGE; diff --git a/devices/sensor_0040_other_TCS230/sensor_0040_other_TCS230.c b/devices/sensor_0040_other_TCS230/sensor_0040_other_TCS230.c index 980013e96..e27354d0d 100644 --- a/devices/sensor_0040_other_TCS230/sensor_0040_other_TCS230.c +++ b/devices/sensor_0040_other_TCS230/sensor_0040_other_TCS230.c @@ -135,7 +135,7 @@ static int __0040_prepare(void *arg) s_TCS230_data_t *user_data = (s_TCS230_data_t *)malloc(sizeof(s_TCS230_data_t)); if (user_data) { - l_ezlopi_device_t *tcs230_device = ezlopi_device_add_device(); + l_ezlopi_device_t *tcs230_device = ezlopi_device_add_device(cj_device); if (tcs230_device) { __prepare_device_cloud_properties(tcs230_device, cj_device); diff --git a/devices/sensor_0041_ADC_FC28_soilMoisture/sensor_0041_ADC_FC28_soilMoisture.c b/devices/sensor_0041_ADC_FC28_soilMoisture/sensor_0041_ADC_FC28_soilMoisture.c index 89a5ceb6c..eab572a33 100644 --- a/devices/sensor_0041_ADC_FC28_soilMoisture/sensor_0041_ADC_FC28_soilMoisture.c +++ b/devices/sensor_0041_ADC_FC28_soilMoisture/sensor_0041_ADC_FC28_soilMoisture.c @@ -102,7 +102,7 @@ static int __0041_prepare(void *arg) if (NULL != user_data) { memset(user_data, 0, sizeof(s_fc28_data_t)); - l_ezlopi_device_t *fc28_device = ezlopi_device_add_device(); + l_ezlopi_device_t *fc28_device = ezlopi_device_add_device(cj_device); if (fc28_device) { __prepare_device_cloud_properties(fc28_device, cj_device); diff --git a/devices/sensor_0042_ADC_shunt_voltmeter/sensor_0042_ADC_shunt_voltmeter.c b/devices/sensor_0042_ADC_shunt_voltmeter/sensor_0042_ADC_shunt_voltmeter.c index 10c2955c8..7f8808b1e 100644 --- a/devices/sensor_0042_ADC_shunt_voltmeter/sensor_0042_ADC_shunt_voltmeter.c +++ b/devices/sensor_0042_ADC_shunt_voltmeter/sensor_0042_ADC_shunt_voltmeter.c @@ -99,7 +99,7 @@ static int __0042_prepare(void *arg) { memset(user_data, 0, sizeof(s_voltmeter_t)); - l_ezlopi_device_t *voltmeter_device = ezlopi_device_add_device(); + l_ezlopi_device_t *voltmeter_device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (voltmeter_device) { __prepare_device_cloud_properties(voltmeter_device, device_prep_arg->cjson_device); diff --git a/devices/sensor_0043_ADC_GYML8511_UV_intensity/sensor_0043_ADC_GYML8511_UV_intensity.c b/devices/sensor_0043_ADC_GYML8511_UV_intensity/sensor_0043_ADC_GYML8511_UV_intensity.c index 4591400e7..162716d52 100644 --- a/devices/sensor_0043_ADC_GYML8511_UV_intensity/sensor_0043_ADC_GYML8511_UV_intensity.c +++ b/devices/sensor_0043_ADC_GYML8511_UV_intensity/sensor_0043_ADC_GYML8511_UV_intensity.c @@ -103,7 +103,7 @@ static int __0043_prepare(void *arg) if (NULL != gyml8511_value) { memset(gyml8511_value, 0, sizeof(s_gyml8511_data_t)); - l_ezlopi_device_t *gyml8511_device = ezlopi_device_add_device(); + l_ezlopi_device_t *gyml8511_device = ezlopi_device_add_device(cj_device); if (gyml8511_device) { __prepare_device_cloud_properties(gyml8511_device, cj_device); diff --git a/devices/sensor_0044_I2C_TSL256_luminosity/TSL256_interface.c b/devices/sensor_0044_I2C_TSL256_luminosity/TSL256_interface.c index 275548052..733fe28db 100644 --- a/devices/sensor_0044_I2C_TSL256_luminosity/TSL256_interface.c +++ b/devices/sensor_0044_I2C_TSL256_luminosity/TSL256_interface.c @@ -2,7 +2,6 @@ #include "sensor_0044_I2C_TSL256_luminosity.h" #include "trace.h" - static uint32_t TSL2561_CalculateLux(uint16_t ch0, uint16_t ch1, integration_t conv_time, gain_t gain) { unsigned long chScale; @@ -47,6 +46,8 @@ static uint32_t TSL2561_CalculateLux(uint16_t ch0, uint16_t ch1, integration_t c unsigned long ratio = (ratio1 + 1) >> 1; // is ratio <= eachBreak ? unsigned int b, m; + +#warning "Nabin needs to check this!, Note: Unsigned value type is always true for (ratio >= 0)." if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) { b = TSL2561_LUX_B1T; @@ -93,6 +94,7 @@ static uint32_t TSL2561_CalculateLux(uint16_t ch0, uint16_t ch1, integration_t c temp = ((channel0 * b) - (channel1 * m)); // do not allow negative lux value +#warning "Nabin needs to check this!, Note: Unsigned value type is always false for (temp < 0)." if (temp < 0) { temp = 0; @@ -232,20 +234,20 @@ uint32_t tsl2561_get_intensity_value(s_ezlopi_i2c_master_t *i2c_master) // extract the CH1-bits first target_address = (SELECT_CMD_REGISTER | DO_WORD_TRANSACTION | TSL2561_REGISTER_CHAN1_LOW); ezlopi_i2c_master_write_to_device(i2c_master, &target_address, 1); - ezlopi_i2c_master_read_from_device(i2c_master, &IR, 2); // extracts CH1-data + ezlopi_i2c_master_read_from_device(i2c_master, (uint8_t *)&IR, 2); // extracts CH1-data // readRegister8(i2c_master, &target_address, 1, &IR, 2); // extract the CH0-bits first target_address = (SELECT_CMD_REGISTER | DO_WORD_TRANSACTION | TSL2561_REGISTER_CHAN0_LOW); ezlopi_i2c_master_write_to_device(i2c_master, &target_address, 1); - ezlopi_i2c_master_read_from_device(i2c_master, &Visible_Ir, 2); // extracts CH2-data + ezlopi_i2c_master_read_from_device(i2c_master, (uint8_t *)&Visible_Ir, 2); // extracts CH2-data // readRegister8(i2c_master, &target_address, 1, &Visible_Ir, 2); // Calculate the lux value illuminance_value = TSL2561_CalculateLux(Visible_Ir, // CH0 - IR, // CH1 - TSL2561_INTEGRATIONTIME_101MS, // conv_time - TSL2561_GAIN_x1); // adc_gain + IR, // CH1 + TSL2561_INTEGRATIONTIME_101MS, // conv_time + TSL2561_GAIN_x1); // adc_gain // TRACE_B("IR : %d", IR); // TRACE_B("Visible : %d", Visible_Ir - IR); // TRACE_B("Lux : %d", Lux_intensity); @@ -257,4 +259,3 @@ uint32_t tsl2561_get_intensity_value(s_ezlopi_i2c_master_t *i2c_master) } return illuminance_value; } - diff --git a/devices/sensor_0044_I2C_TSL256_luminosity/sensor_0044_I2C_TSL256_luminosity.c b/devices/sensor_0044_I2C_TSL256_luminosity/sensor_0044_I2C_TSL256_luminosity.c index a6376099e..6f2f6a84b 100644 --- a/devices/sensor_0044_I2C_TSL256_luminosity/sensor_0044_I2C_TSL256_luminosity.c +++ b/devices/sensor_0044_I2C_TSL256_luminosity/sensor_0044_I2C_TSL256_luminosity.c @@ -9,6 +9,7 @@ #include "ezlopi_actions.h" #include "ezlopi_i2c_master.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -89,7 +90,7 @@ static int __get_cjson_value(l_ezlopi_item_t *item, void *arg) char *valueFormatted = ezlopi_valueformatter_int((int)(illuminance_value->lux_val)); if (valueFormatted) { - cJSON_AddStringToObject(cjson_properties, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cjson_properties, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); } cJSON_AddNumberToObject(cjson_properties, "values", (int)illuminance_value->lux_val); @@ -111,9 +112,16 @@ static int __init(l_ezlopi_item_t *item) { TRACE_B("TSL561 initialization finished........."); sensor_0044_tsl2561_configure_device(&item->interface.i2c_master); + ret = 1; } else { + ret = -1; + if (item->user_arg) + { + free(item->user_arg); + item->user_arg = NULL; + } TRACE_E("TSL561 not found!....... Please Restart!! or Check your I2C connection..."); } } @@ -122,20 +130,21 @@ static int __init(l_ezlopi_item_t *item) static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_light_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); item->cloud_properties.has_getter = true; item->cloud_properties.has_setter = false; item->cloud_properties.item_name = ezlopi_item_name_lux; @@ -144,13 +153,13 @@ static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_dev item->cloud_properties.scale = scales_lux; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "gpio_sda", item->interface.i2c_master.sda); - CJSON_GET_VALUE_INT(cj_device, "gpio_scl", item->interface.i2c_master.scl); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_sda_str, item->interface.i2c_master.sda); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_scl_str, item->interface.i2c_master.scl); CJSON_GET_VALUE_INT(cj_device, "slave_addr", item->interface.i2c_master.address); item->interface.i2c_master.enable = true; item->interface.i2c_master.clock_speed = 100000; - if (NULL == item->interface.i2c_master.address) + if (0x00 == item->interface.i2c_master.address) { item->interface.i2c_master.address = TSL2561_ADDRESS; } @@ -169,7 +178,7 @@ static int __prepare(void *arg) if (NULL != TSL2561_lux_data) { memset(TSL2561_lux_data, 0, sizeof(TSL256_lum_t)); - l_ezlopi_device_t *tsl256_device = ezlopi_device_add_device(); + l_ezlopi_device_t *tsl256_device = ezlopi_device_add_device(prep_arg->cjson_device); if (tsl256_device) { __prepare_device_cloud_properties(tsl256_device, prep_arg->cjson_device); @@ -188,7 +197,6 @@ static int __prepare(void *arg) } else { - ezlopi_device_free_device(tsl256_device); free(TSL2561_lux_data); } ret = 1; diff --git a/devices/sensor_0046_ADC_ACS712_05B_currentmeter/sensor_0046_ADC_ACS712_05B_currentmeter.c b/devices/sensor_0046_ADC_ACS712_05B_currentmeter/sensor_0046_ADC_ACS712_05B_currentmeter.c index f705d08a0..dfa594cbe 100644 --- a/devices/sensor_0046_ADC_ACS712_05B_currentmeter/sensor_0046_ADC_ACS712_05B_currentmeter.c +++ b/devices/sensor_0046_ADC_ACS712_05B_currentmeter/sensor_0046_ADC_ACS712_05B_currentmeter.c @@ -100,7 +100,7 @@ static int __0046_prepare(void *arg) if (NULL != user_data) { memset(user_data, 0, sizeof(s_currentmeter_t)); - l_ezlopi_device_t *currentmeter_device = ezlopi_device_add_device(); + l_ezlopi_device_t *currentmeter_device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (currentmeter_device) { __prepare_device_cloud_properties(currentmeter_device, device_prep_arg->cjson_device); diff --git a/devices/sensor_0047_other_HX711_loadcell/sensor_0047_other_HX711_loadcell.c b/devices/sensor_0047_other_HX711_loadcell/sensor_0047_other_HX711_loadcell.c index 55ec3eeea..216d62f4d 100644 --- a/devices/sensor_0047_other_HX711_loadcell/sensor_0047_other_HX711_loadcell.c +++ b/devices/sensor_0047_other_HX711_loadcell/sensor_0047_other_HX711_loadcell.c @@ -126,7 +126,7 @@ static int __0047_prepare(void *arg) { memset(hx711_data, 0, sizeof(s_hx711_data_t)); //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *hx711_device = ezlopi_device_add_device(); + l_ezlopi_device_t *hx711_device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (hx711_device) { __prepare_device_cloud_properties(hx711_device, device_prep_arg->cjson_device); diff --git a/devices/sensor_0048_other_MQ4_CH4_detector/sensor_0048_other_MQ4_CH4_detector.c b/devices/sensor_0048_other_MQ4_CH4_detector/sensor_0048_other_MQ4_CH4_detector.c index 4b27378fa..bfe971e3b 100644 --- a/devices/sensor_0048_other_MQ4_CH4_detector/sensor_0048_other_MQ4_CH4_detector.c +++ b/devices/sensor_0048_other_MQ4_CH4_detector/sensor_0048_other_MQ4_CH4_detector.c @@ -9,6 +9,7 @@ #include "ezlopi_adc.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -91,7 +92,7 @@ static int __0048_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ4_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ4_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ4_device_digi) { __prepare_device_digi_cloud_properties(MQ4_device_digi, device_prep_arg->cjson_device); @@ -106,10 +107,6 @@ static int __0048_prepare(void *arg) ezlopi_device_free_device(MQ4_device_digi); } } - else - { - ezlopi_device_free_device(MQ4_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- @@ -117,7 +114,7 @@ static int __0048_prepare(void *arg) if (NULL != MQ4_value) { memset(MQ4_value, 0, sizeof(mq4_value_t)); - l_ezlopi_device_t *MQ4_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ4_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ4_device_adc) { __prepare_device_adc_cloud_properties(MQ4_device_adc, device_prep_arg->cjson_device); @@ -135,7 +132,6 @@ static int __0048_prepare(void *arg) } else { - ezlopi_device_free_device(MQ4_device_adc); free(MQ4_value); } ret = 1; @@ -179,15 +175,16 @@ static int __0048_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -199,22 +196,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ4-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -226,8 +224,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ4-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -258,18 +256,18 @@ static int __0048_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq4_value_t *MQ4_value = ((mq4_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ4_value->_CH4_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ4_value->_CH4_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ4_value->_CH4_ppm); free(valueFormatted); } ret = 1; @@ -288,15 +286,15 @@ static int __0048_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq4_value_t *MQ4_value = ((mq4_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ4_value->_CH4_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", (MQ4_value->_CH4_ppm)); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, (MQ4_value->_CH4_ppm)); free(valueFormatted); } ret = 1; @@ -312,7 +310,7 @@ static int __0048_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0049_other_MQ2_LPG_detector/sensor_0049_other_MQ2_LPG_detector.c b/devices/sensor_0049_other_MQ2_LPG_detector/sensor_0049_other_MQ2_LPG_detector.c index 6cd36a970..186267172 100644 --- a/devices/sensor_0049_other_MQ2_LPG_detector/sensor_0049_other_MQ2_LPG_detector.c +++ b/devices/sensor_0049_other_MQ2_LPG_detector/sensor_0049_other_MQ2_LPG_detector.c @@ -9,6 +9,7 @@ #include "ezlopi_adc.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -90,7 +91,7 @@ static int __0049_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ2_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ2_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ2_device_digi) { __prepare_device_digi_cloud_properties(MQ2_device_digi, device_prep_arg->cjson_device); @@ -105,17 +106,13 @@ static int __0049_prepare(void *arg) ezlopi_device_free_device(MQ2_device_digi); } } - else - { - ezlopi_device_free_device(MQ2_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- mq2_value_t *MQ2_value = (mq2_value_t *)malloc(sizeof(mq2_value_t)); if (NULL != MQ2_value) { memset(MQ2_value, 0, sizeof(mq2_value_t)); - l_ezlopi_device_t *MQ2_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ2_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ2_device_adc) { __prepare_device_adc_cloud_properties(MQ2_device_adc, device_prep_arg->cjson_device); @@ -133,7 +130,6 @@ static int __0049_prepare(void *arg) } else { - ezlopi_device_free_device(MQ2_device_adc); free(MQ2_value); } ret = 1; @@ -177,15 +173,16 @@ static int __0049_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -197,22 +194,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ2-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -224,8 +222,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ2-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -256,18 +254,18 @@ static int __0049_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq2_value_t *MQ2_value = ((mq2_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ2_value->_LPG_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ2_value->_LPG_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ2_value->_LPG_ppm); free(valueFormatted); } ret = 1; @@ -286,15 +284,15 @@ static int __0049_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq2_value_t *MQ2_value = ((mq2_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ2_value->_LPG_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ2_value->_LPG_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ2_value->_LPG_ppm); free(valueFormatted); } ret = 1; @@ -310,7 +308,7 @@ static int __0049_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0050_other_MQ3_alcohol_detector/sensor_0050_other_MQ3_alcohol_detector.c b/devices/sensor_0050_other_MQ3_alcohol_detector/sensor_0050_other_MQ3_alcohol_detector.c index 338226b32..a2f4a6cc6 100644 --- a/devices/sensor_0050_other_MQ3_alcohol_detector/sensor_0050_other_MQ3_alcohol_detector.c +++ b/devices/sensor_0050_other_MQ3_alcohol_detector/sensor_0050_other_MQ3_alcohol_detector.c @@ -9,6 +9,7 @@ #include "ezlopi_adc.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -91,7 +92,7 @@ static int __0050_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ3_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ3_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ3_device_digi) { __prepare_device_digi_cloud_properties(MQ3_device_digi, device_prep_arg->cjson_device); @@ -106,17 +107,13 @@ static int __0050_prepare(void *arg) ezlopi_device_free_device(MQ3_device_digi); } } - else - { - ezlopi_device_free_device(MQ3_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- mq3_value_t *MQ3_value = (mq3_value_t *)malloc(sizeof(mq3_value_t)); if (NULL != MQ3_value) { memset(MQ3_value, 0, sizeof(mq3_value_t)); - l_ezlopi_device_t *MQ3_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ3_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ3_device_adc) { __prepare_device_adc_cloud_properties(MQ3_device_adc, device_prep_arg->cjson_device); @@ -134,7 +131,6 @@ static int __0050_prepare(void *arg) } else { - ezlopi_device_free_device(MQ3_device_adc); free(MQ3_value); } ret = 1; @@ -178,15 +174,16 @@ static int __0050_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -198,22 +195,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ3-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -225,8 +223,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ3-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -257,18 +255,18 @@ static int __0050_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq3_value_t *MQ3_value = ((mq3_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ3_value->_alcohol_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ3_value->_alcohol_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ3_value->_alcohol_ppm); free(valueFormatted); } ret = 1; @@ -287,15 +285,15 @@ static int __0050_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq3_value_t *MQ3_value = ((mq3_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ3_value->_alcohol_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ3_value->_alcohol_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ3_value->_alcohol_ppm); free(valueFormatted); } ret = 1; @@ -311,7 +309,7 @@ static int __0050_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0051_other_MQ8_H2_detector/sensor_0051_other_MQ8_H2_detector.c b/devices/sensor_0051_other_MQ8_H2_detector/sensor_0051_other_MQ8_H2_detector.c index 2481074e6..099df94e6 100644 --- a/devices/sensor_0051_other_MQ8_H2_detector/sensor_0051_other_MQ8_H2_detector.c +++ b/devices/sensor_0051_other_MQ8_H2_detector/sensor_0051_other_MQ8_H2_detector.c @@ -9,6 +9,7 @@ #include "ezlopi_adc.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -89,7 +90,7 @@ static int __0051_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ8_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ8_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ8_device_digi) { __prepare_device_digi_cloud_properties(MQ8_device_digi, device_prep_arg->cjson_device); @@ -104,17 +105,13 @@ static int __0051_prepare(void *arg) ezlopi_device_free_device(MQ8_device_digi); } } - else - { - ezlopi_device_free_device(MQ8_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- mq8_value_t *MQ8_value = (mq8_value_t *)malloc(sizeof(mq8_value_t)); if (NULL != MQ8_value) { memset(MQ8_value, 0, sizeof(mq8_value_t)); - l_ezlopi_device_t *MQ8_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ8_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ8_device_adc) { __prepare_device_adc_cloud_properties(MQ8_device_adc, device_prep_arg->cjson_device); @@ -132,7 +129,6 @@ static int __0051_prepare(void *arg) } else { - ezlopi_device_free_device(MQ8_device_adc); free(MQ8_value); } ret = 1; @@ -176,15 +172,16 @@ static int __0051_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -196,22 +193,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ8-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -223,8 +221,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ8-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -255,18 +253,18 @@ static int __0051_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq8_value_t *MQ8_value = ((mq8_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ8_value->_H2_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ8_value->_H2_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ8_value->_H2_ppm); free(valueFormatted); } ret = 1; @@ -285,15 +283,15 @@ static int __0051_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq8_value_t *MQ8_value = ((mq8_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ8_value->_H2_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ8_value->_H2_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ8_value->_H2_ppm); free(valueFormatted); } ret = 1; @@ -309,7 +307,7 @@ static int __0051_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0052_other_MQ135_NH3_detector/sensor_0052_other_MQ135_NH3_detector.c b/devices/sensor_0052_other_MQ135_NH3_detector/sensor_0052_other_MQ135_NH3_detector.c index fba8d582a..798ffd542 100644 --- a/devices/sensor_0052_other_MQ135_NH3_detector/sensor_0052_other_MQ135_NH3_detector.c +++ b/devices/sensor_0052_other_MQ135_NH3_detector/sensor_0052_other_MQ135_NH3_detector.c @@ -9,6 +9,7 @@ #include "ezlopi_adc.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -90,7 +91,7 @@ static int __0052_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ135_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ135_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ135_device_digi) { __prepare_device_digi_cloud_properties(MQ135_device_digi, device_prep_arg->cjson_device); @@ -105,17 +106,13 @@ static int __0052_prepare(void *arg) ezlopi_device_free_device(MQ135_device_digi); } } - else - { - ezlopi_device_free_device(MQ135_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- mq135_value_t *MQ135_value = (mq135_value_t *)malloc(sizeof(mq135_value_t)); if (NULL != MQ135_value) { memset(MQ135_value, 0, sizeof(mq135_value_t)); - l_ezlopi_device_t *MQ135_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ135_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ135_device_adc) { __prepare_device_adc_cloud_properties(MQ135_device_adc, device_prep_arg->cjson_device); @@ -133,7 +130,6 @@ static int __0052_prepare(void *arg) } else { - ezlopi_device_free_device(MQ135_device_adc); free(MQ135_value); } ret = 1; @@ -177,15 +173,16 @@ static int __0052_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -197,22 +194,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ135-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -224,8 +222,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ135-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -256,11 +254,11 @@ static int __0052_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { @@ -268,10 +266,10 @@ static int __0052_get_item(l_ezlopi_item_t *item, void *arg) char *valueFormatted = ezlopi_valueformatter_float(MQ135_value->_NH3_ppm); if (valueFormatted) { - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); } - cJSON_AddNumberToObject(cj_result, "value", MQ135_value->_NH3_ppm); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ135_value->_NH3_ppm); } ret = 1; } @@ -289,8 +287,8 @@ static int __0052_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { @@ -298,10 +296,10 @@ static int __0052_get_cjson_value(l_ezlopi_item_t *item, void *arg) char *valueFormatted = ezlopi_valueformatter_float(MQ135_value->_NH3_ppm); if (valueFormatted) { - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); } - cJSON_AddNumberToObject(cj_result, "value", MQ135_value->_NH3_ppm); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ135_value->_NH3_ppm); } ret = 1; } @@ -316,7 +314,7 @@ static int __0052_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0053_UART_GYGPS6MV2/sensor_0053_UART_GYGPS6MV2.c b/devices/sensor_0053_UART_GYGPS6MV2/sensor_0053_UART_GYGPS6MV2.c index 4bd46d7b4..00918cd32 100644 --- a/devices/sensor_0053_UART_GYGPS6MV2/sensor_0053_UART_GYGPS6MV2.c +++ b/devices/sensor_0053_UART_GYGPS6MV2/sensor_0053_UART_GYGPS6MV2.c @@ -4,17 +4,19 @@ #include "stdint.h" #include "string.h" -#include "ezlopi_actions.h" -#include "ezlopi_timer.h" #include "ezlopi_uart.h" +#include "ezlopi_timer.h" #include "ezlopi_cloud.h" +#include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" #include "gyGPS6MV2.h" #include "sensor_0053_UART_GYGPS6MV2.h" + //------------------------------------------------------------------------ static int __0053_prepare(void *arg); static int __0053_init(l_ezlopi_item_t *item); @@ -23,7 +25,7 @@ static int __0053_notify(l_ezlopi_item_t *item); static int __sensor_uart_gps6mv2_update_values(l_ezlopi_item_t *item); static void __retrieve_GPGGA_sentence(l_ezlopi_item_t *item); -static void __uart_gps6mv2_upcall(uint8_t *buffer, s_ezlopi_uart_object_handle_t uart_object_handle); +static void __uart_gps6mv2_upcall(uint8_t *buffer, uint32_t output_len, s_ezlopi_uart_object_handle_t uart_object_handle); static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device); static void __prepare_lat_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, GPS6MV2_t *gps_arg); @@ -32,6 +34,7 @@ static void __prepare_fix_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj static void __prepare_sea_level_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, GPS6MV2_t *gps_arg); static void __prepare_geiod_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, GPS6MV2_t *gps_arg); static void __prepare_item_interface_properties(l_ezlopi_item_t *item, cJSON *cj_device); + //------------------------------------------------------------------------ int sensor_0053_UART_GYGPS6MV2(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) { @@ -70,15 +73,11 @@ int sensor_0053_UART_GYGPS6MV2(e_ezlopi_actions_t action, l_ezlopi_item_t *item, //--------------------------------------------------------------------------------------------------------- static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *dev_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", dev_name); - ASSIGN_DEVICE_NAME_V2(device, dev_name); device->cloud_properties.category = category_generic_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_lat_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, GPS6MV2_t *gps_arg) { @@ -148,10 +147,10 @@ static void __prepare_geiod_item_cloud_properties(l_ezlopi_item_t *item, cJSON * static void __prepare_item_interface_properties(l_ezlopi_item_t *item, cJSON *cj_device) { - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); - CJSON_GET_VALUE_INT(cj_device, "gpio_tx", item->interface.uart.tx); - CJSON_GET_VALUE_INT(cj_device, "gpio_rx", item->interface.uart.rx); - CJSON_GET_VALUE_INT(cj_device, "baud", item->interface.uart.baudrate); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_tx_str, item->interface.uart.tx); + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio_rx_str, item->interface.uart.rx); + CJSON_GET_VALUE_INT(cj_device, ezlopi_baud_str, item->interface.uart.baudrate); /*Here we decide, when uart is allowed to initialize*/ GPS6MV2_t *sensor_0053_UART_gps6mv2_data = (GPS6MV2_t *)item->user_arg; @@ -179,7 +178,7 @@ static int __0053_prepare(void *arg) if (NULL != sensor_0053_UART_gps6mv2_data) { memset(sensor_0053_UART_gps6mv2_data, 0, sizeof(GPS6MV2_t)); - l_ezlopi_device_t *gps_device = ezlopi_device_add_device(); + l_ezlopi_device_t *gps_device = ezlopi_device_add_device(cjson_device); if (gps_device) { __prepare_device_cloud_properties(gps_device, device_prep_arg->cjson_device); @@ -227,14 +226,14 @@ static int __0053_prepare(void *arg) } else { - ezlopi_device_free_device(gps_device); free(sensor_0053_UART_gps6mv2_data); } } ret = 1; } } - return 0; + + return ret; } //---------------------------------------------------------------------------------------------------------------------- static int __0053_init(l_ezlopi_item_t *item) @@ -266,38 +265,38 @@ static int __0053_get_value_cjson(l_ezlopi_item_t *item, void *arg) if ((sensor_0053_UART_gps6mv2_data->Latitude_item_id) == item->cloud_properties.item_id) { char *valueFormatted = ezlopi_valueformatter_float(sensor_0053_UART_gps6mv2_data->prev_lat_angle_val); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", sensor_0053_UART_gps6mv2_data->prev_lat_angle_val); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, sensor_0053_UART_gps6mv2_data->prev_lat_angle_val); free(valueFormatted); } if ((sensor_0053_UART_gps6mv2_data->Longitude_item_id) == item->cloud_properties.item_id) { char *valueFormatted = ezlopi_valueformatter_float(sensor_0053_UART_gps6mv2_data->prev_long_angle_val); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", sensor_0053_UART_gps6mv2_data->prev_long_angle_val); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, sensor_0053_UART_gps6mv2_data->prev_long_angle_val); free(valueFormatted); } if ((sensor_0053_UART_gps6mv2_data->Fix_item_id) == item->cloud_properties.item_id) { char *valueFormatted = ezlopi_valueformatter_bool(sensor_0053_UART_gps6mv2_data->prev_GPS_FIX); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddBoolToObject(cj_result, "value", sensor_0053_UART_gps6mv2_data->prev_GPS_FIX); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddBoolToObject(cj_result, ezlopi_value_str, sensor_0053_UART_gps6mv2_data->prev_GPS_FIX); } if ((sensor_0053_UART_gps6mv2_data->Sea_level_item_id) == item->cloud_properties.item_id) { char *valueFormatted = ezlopi_valueformatter_float(sensor_0053_UART_gps6mv2_data->prev_antenna_alti); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", sensor_0053_UART_gps6mv2_data->prev_antenna_alti); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, sensor_0053_UART_gps6mv2_data->prev_antenna_alti); free(valueFormatted); } if ((sensor_0053_UART_gps6mv2_data->Geoid_item_id) == item->cloud_properties.item_id) { char *valueFormatted = ezlopi_valueformatter_float(sensor_0053_UART_gps6mv2_data->prev_geoid); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", sensor_0053_UART_gps6mv2_data->prev_geoid); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, sensor_0053_UART_gps6mv2_data->prev_geoid); free(valueFormatted); } ret = 1; @@ -411,8 +410,7 @@ static int __sensor_uart_gps6mv2_update_values(l_ezlopi_item_t *item) } //------------------------------------------------------------------------- - -static void __uart_gps6mv2_upcall(uint8_t *buffer, s_ezlopi_uart_object_handle_t uart_object_handle) +static void __uart_gps6mv2_upcall(uint8_t *buffer, uint32_t output_len, s_ezlopi_uart_object_handle_t uart_object_handle) { // TRACE_E("UART_Buffer => \n%s\n", buffer); char *another_buffer = (char *)malloc(256); diff --git a/devices/sensor_0054_PWM_YFS201_flowmeter/sensor_0054_PWM_YFS201_flowmeter.c b/devices/sensor_0054_PWM_YFS201_flowmeter/sensor_0054_PWM_YFS201_flowmeter.c index c261d9732..5435f90b9 100644 --- a/devices/sensor_0054_PWM_YFS201_flowmeter/sensor_0054_PWM_YFS201_flowmeter.c +++ b/devices/sensor_0054_PWM_YFS201_flowmeter/sensor_0054_PWM_YFS201_flowmeter.c @@ -6,6 +6,7 @@ #include "ezlopi_pwm.h" #include "ezlopi_timer.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -73,15 +74,16 @@ int sensor_0054_PWM_YFS201_flowmeter(e_ezlopi_actions_t action, l_ezlopi_item_t //------------------------------------------------------------------------------------------------------ static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_flow_meter; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -93,8 +95,8 @@ static void __prepare_item_properties(l_ezlopi_item_t *item, cJSON *cj_device, v item->cloud_properties.scale = scales_liter_per_hour; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.pwm.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.pwm.gpio_num); // passing the custom data_structure item->user_arg = user_data; @@ -112,7 +114,7 @@ static int __0054_prepare(void *arg) if (NULL != yfs201_data) { memset(yfs201_data, 0, sizeof(yfs201_t)); - l_ezlopi_device_t *flowmeter_device = ezlopi_device_add_device(); + l_ezlopi_device_t *flowmeter_device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (flowmeter_device) { __prepare_device_cloud_properties(flowmeter_device, device_prep_arg->cjson_device); @@ -130,7 +132,6 @@ static int __0054_prepare(void *arg) } else { - ezlopi_device_free_device(flowmeter_device); free(yfs201_data); } @@ -180,10 +181,10 @@ static int __0054_get_cjson_value(l_ezlopi_item_t *item, void *arg) char *valueFormatted = ezlopi_valueformatter_float(Lt_per_hr); if (valueFormatted) { - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); free(valueFormatted); } - cJSON_AddNumberToObject(cj_result, "value", Lt_per_hr); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, Lt_per_hr); ret = 1; } } @@ -238,7 +239,7 @@ static void __extract_YFS201_Pulse_Count_func(l_ezlopi_item_t *item) } // check queue_full => 1 - if (xQueueSend(yfs201_queue, &(yfs201_data->_pulses_yfs201), NULL)) + if (xQueueSend(yfs201_queue, &(yfs201_data->_pulses_yfs201), 0)) { (yfs201_data->yfs201_QueueFlag) = YFS201_QUEUE_AVAILABLE; // TRACE_E("Pulse_count : %d", (yfs201_data->_pulses_yfs201)); diff --git a/devices/sensor_0055_ADC_FlexResistor/sensor_0055_ADC_FlexResistor.c b/devices/sensor_0055_ADC_FlexResistor/sensor_0055_ADC_FlexResistor.c index 1ff37b880..a3e5186df 100644 --- a/devices/sensor_0055_ADC_FlexResistor/sensor_0055_ADC_FlexResistor.c +++ b/devices/sensor_0055_ADC_FlexResistor/sensor_0055_ADC_FlexResistor.c @@ -7,6 +7,7 @@ #include "ezlopi_adc.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -59,15 +60,16 @@ int sensor_0055_ADC_FlexResistor(e_ezlopi_actions_t action, l_ezlopi_item_t *ite //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -80,8 +82,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_ohm_meter; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit // passing the custom data_structure @@ -101,7 +103,7 @@ static int __0055_prepare(void *arg) { memset(FLEX_value, 0, sizeof(flex_t)); - l_ezlopi_device_t *device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (device_adc) { __prepare_device_adc_cloud_properties(device_adc, device_prep_arg->cjson_device); @@ -119,7 +121,6 @@ static int __0055_prepare(void *arg) } else { - ezlopi_device_free_device(device_adc); free(FLEX_value); } ret = 1; @@ -152,8 +153,8 @@ static int __0055_get_cjson_value(l_ezlopi_item_t *item, void *arg) { flex_t *FLEX_value = (flex_t *)item->user_arg; char *valueFormatted = ezlopi_valueformatter_int(FLEX_value->RS_0055); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", FLEX_value->RS_0055); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, FLEX_value->RS_0055); free(valueFormatted); ret = 1; } diff --git a/devices/sensor_0056_ADC_Force_Sensitive_Resistor/sensor_0056_ADC_Force_Sensitive_Resistor.c b/devices/sensor_0056_ADC_Force_Sensitive_Resistor/sensor_0056_ADC_Force_Sensitive_Resistor.c index 718d55af6..545492eca 100644 --- a/devices/sensor_0056_ADC_Force_Sensitive_Resistor/sensor_0056_ADC_Force_Sensitive_Resistor.c +++ b/devices/sensor_0056_ADC_Force_Sensitive_Resistor/sensor_0056_ADC_Force_Sensitive_Resistor.c @@ -6,6 +6,8 @@ #include "ezlopi_adc.h" #include "ezlopi_timer.h" +#include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -59,15 +61,16 @@ int sensor_0056_ADC_Force_Sensitive_Resistor(e_ezlopi_actions_t action, l_ezlopi //------------------------------------------------------------------------------------------------------ static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -79,8 +82,8 @@ static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_dev item->cloud_properties.scale = scales_newton; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit // passing the custom data_structure @@ -99,7 +102,7 @@ static int __0056_prepare(void *arg) { memset(FSR_struct, 0, sizeof(fsr_t)); - l_ezlopi_device_t *FSR_device = ezlopi_device_add_device(); + l_ezlopi_device_t *FSR_device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (FSR_device) { __prepare_device_cloud_properties(FSR_device, device_prep_arg->cjson_device); @@ -117,7 +120,6 @@ static int __0056_prepare(void *arg) } else { - ezlopi_device_free_device(FSR_device); free(FSR_struct); } ret = 1; @@ -151,8 +153,8 @@ static int __0056_get_cjson_value(l_ezlopi_item_t *item, void *arg) { fsr_t *FSR_struct = (fsr_t *)item->user_arg; char *valueFormatted = ezlopi_valueformatter_float(FSR_struct->FSR_value); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", FSR_struct->FSR_value); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, FSR_struct->FSR_value); free(valueFormatted); ret = 1; } diff --git a/devices/sensor_0057_other_KY026_FlameDetector/sensor_0057_other_KY026_FlameDetector.c b/devices/sensor_0057_other_KY026_FlameDetector/sensor_0057_other_KY026_FlameDetector.c index 0e55a6721..0ab23fe1d 100644 --- a/devices/sensor_0057_other_KY026_FlameDetector/sensor_0057_other_KY026_FlameDetector.c +++ b/devices/sensor_0057_other_KY026_FlameDetector/sensor_0057_other_KY026_FlameDetector.c @@ -3,8 +3,9 @@ #include "items.h" #include "ezlopi_adc.h" -#include "ezlopi_actions.h" #include "ezlopi_timer.h" +#include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -79,7 +80,7 @@ static int __0057_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *flame_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *flame_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (flame_device_digi) { __prepare_device_digi_cloud_properties(flame_device_digi, device_prep_arg->cjson_device); @@ -94,17 +95,13 @@ static int __0057_prepare(void *arg) ezlopi_device_free_device(flame_device_digi); } } - else - { - ezlopi_device_free_device(flame_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- flame_t *FLAME_struct = (flame_t *)malloc(sizeof(flame_t)); if (NULL != FLAME_struct) { memset(FLAME_struct, 0, sizeof(flame_t)); - l_ezlopi_device_t *flame_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *flame_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (flame_device_adc) { __prepare_device_adc_cloud_properties(flame_device_adc, device_prep_arg->cjson_device); @@ -122,7 +119,6 @@ static int __0057_prepare(void *arg) } else { - ezlopi_device_free_device(flame_device_adc); free(FLAME_struct); } ret = 1; @@ -161,13 +157,14 @@ static int __0057_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_heat; device->cloud_properties.device_type = dev_type_sensor; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -179,22 +176,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("flame_> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -206,8 +204,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_percent; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("flame_> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -238,19 +236,19 @@ static int __0057_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "heat_ok"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "heat_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "heat_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "heat_ok"); } if (ezlopi_item_name_temperature_changes == item->cloud_properties.item_name) { flame_t *FLAME_struct = (flame_t *)item->user_arg; char *valueFormatted = ezlopi_valueformatter_float(FLAME_struct->_absorbed_percent); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", FLAME_struct->_absorbed_percent); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, FLAME_struct->_absorbed_percent); free(valueFormatted); } ret = 1; @@ -269,15 +267,15 @@ static int __0057_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_heat_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "heat_ok"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "heat_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "heat_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "heat_ok"); } if (ezlopi_item_name_temperature_changes == item->cloud_properties.item_name) { flame_t *FLAME_struct = (flame_t *)item->user_arg; char *valueFormatted = ezlopi_valueformatter_float(FLAME_struct->_absorbed_percent); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", FLAME_struct->_absorbed_percent); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, FLAME_struct->_absorbed_percent); free(valueFormatted); } ret = 1; @@ -293,7 +291,7 @@ static int __0057_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_heat_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { curret_value = ky206_sensor_heat_alarm_token[0]; // heat_ok diff --git a/devices/sensor_0059_other_MQ6_LPG_detector/sensor_0059_other_MQ6_LPG_detector.c b/devices/sensor_0059_other_MQ6_LPG_detector/sensor_0059_other_MQ6_LPG_detector.c index b989086e5..f9f243bab 100644 --- a/devices/sensor_0059_other_MQ6_LPG_detector/sensor_0059_other_MQ6_LPG_detector.c +++ b/devices/sensor_0059_other_MQ6_LPG_detector/sensor_0059_other_MQ6_LPG_detector.c @@ -1,17 +1,18 @@ +#include "math.h" #include "trace.h" #include "cJSON.h" -#include "ezlopi_actions.h" -#include "ezlopi_timer.h" #include "items.h" -#include "math.h" -#include "stdbool.h" #include "string.h" +#include "stdbool.h" #include "ezlopi_adc.h" +#include "ezlopi_timer.h" +#include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" -#include "ezlopi_valueformatter.h" #include "sensor_0059_other_MQ6_LPG_detector.h" @@ -90,7 +91,7 @@ static int __0059_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ6_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ6_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ6_device_digi) { __prepare_device_digi_cloud_properties(MQ6_device_digi, device_prep_arg->cjson_device); @@ -105,17 +106,13 @@ static int __0059_prepare(void *arg) ezlopi_device_free_device(MQ6_device_digi); } } - else - { - ezlopi_device_free_device(MQ6_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- mq6_value_t *MQ6_value = (mq6_value_t *)malloc(sizeof(mq6_value_t)); if (NULL != MQ6_value) { memset(MQ6_value, 0, sizeof(mq6_value_t)); - l_ezlopi_device_t *MQ6_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ6_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ6_device_adc) { __prepare_device_adc_cloud_properties(MQ6_device_adc, device_prep_arg->cjson_device); @@ -133,7 +130,6 @@ static int __0059_prepare(void *arg) } else { - ezlopi_device_free_device(MQ6_device_adc); free(MQ6_value); } ret = 1; @@ -177,15 +173,16 @@ static int __0059_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -197,22 +194,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ6-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -224,8 +222,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ6-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -256,18 +254,18 @@ static int __0059_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq6_value_t *MQ6_value = ((mq6_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ6_value->_LPG_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ6_value->_LPG_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ6_value->_LPG_ppm); free(valueFormatted); } ret = 1; @@ -286,15 +284,15 @@ static int __0059_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq6_value_t *MQ6_value = ((mq6_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ6_value->_LPG_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ6_value->_LPG_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ6_value->_LPG_ppm); free(valueFormatted); } ret = 1; @@ -310,7 +308,7 @@ static int __0059_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0060_digitalIn_vibration_detector/sensor_0060_digitalIn_vibration_detector.c b/devices/sensor_0060_digitalIn_vibration_detector/sensor_0060_digitalIn_vibration_detector.c index ab7e6b5f7..962565caf 100644 --- a/devices/sensor_0060_digitalIn_vibration_detector/sensor_0060_digitalIn_vibration_detector.c +++ b/devices/sensor_0060_digitalIn_vibration_detector/sensor_0060_digitalIn_vibration_detector.c @@ -2,17 +2,19 @@ #include "trace.h" #include "items.h" -#include "gpio_isr_service.h" #include "ezlopi_gpio.h" #include "ezlopi_timer.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" +#include "gpio_isr_service.h" #include "sensor_0060_digitalIn_vibration_detector.h" + //--------------------------------------------------------------------------------------------------------- const char *Sw420_vibration_activity_state_token[] = { "no_activity", @@ -71,15 +73,16 @@ int sensor_0060_digitalIn_vibration_detector(e_ezlopi_actions_t action, l_ezlopi static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *dev_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", dev_name); - ASSIGN_DEVICE_NAME_V2(device, dev_name); + // char *dev_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, dev_name); + // ASSIGN_DEVICE_NAME_V2(device, dev_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_motion; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -91,8 +94,8 @@ static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_dev item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); CJSON_GET_VALUE_INT(cj_device, "logic_inv", item->interface.gpio.gpio_in.invert); item->interface.gpio.gpio_in.enable = true; @@ -109,7 +112,7 @@ static int __0060_prepare(void *arg) s_ezlopi_prep_arg_t *dev_prep_arg = (s_ezlopi_prep_arg_t *)arg; if (dev_prep_arg && (NULL != dev_prep_arg->cjson_device)) { - l_ezlopi_device_t *vibration_device = ezlopi_device_add_device(); + l_ezlopi_device_t *vibration_device = ezlopi_device_add_device(dev_prep_arg->cjson_device); if (vibration_device) { __prepare_device_cloud_properties(vibration_device, dev_prep_arg->cjson_device); @@ -124,10 +127,6 @@ static int __0060_prepare(void *arg) ezlopi_device_free_device(vibration_device); } } - else - { - ezlopi_device_free_device(vibration_device); - } } ret = 1; } @@ -187,12 +186,12 @@ static int __0060_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_activity"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_activity"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_activity"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_activity"); ret = 1; } } @@ -206,8 +205,8 @@ static int __0060_get_cjson_value(l_ezlopi_item_t *item, void *arg) cJSON *cj_result = (cJSON *)arg; if (cj_result) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_activity"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_activity"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_activity"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_activity"); ret = 1; } } @@ -217,10 +216,10 @@ static int __0060_get_cjson_value(l_ezlopi_item_t *item, void *arg) static int __0060_notify(l_ezlopi_item_t *item) { int ret = 0; - - char *curret_value = NULL; + const char *curret_value = NULL; item->interface.gpio.gpio_in.value = gpio_get_level(item->interface.gpio.gpio_in.gpio_num); item->interface.gpio.gpio_in.value = (false == item->interface.gpio.gpio_in.invert) ? (item->interface.gpio.gpio_in.value) : (!item->interface.gpio.gpio_in.value); + if (0 == (item->interface.gpio.gpio_in.value)) // when D0 -> 0V, { curret_value = Sw420_vibration_activity_state_token[0]; //"no_activity"; diff --git a/devices/sensor_0061_digitalIn_reed_switch/sensor_0061_digitalIn_reed_switch.c b/devices/sensor_0061_digitalIn_reed_switch/sensor_0061_digitalIn_reed_switch.c index 7040ab90b..eff7fd7d4 100644 --- a/devices/sensor_0061_digitalIn_reed_switch/sensor_0061_digitalIn_reed_switch.c +++ b/devices/sensor_0061_digitalIn_reed_switch/sensor_0061_digitalIn_reed_switch.c @@ -8,6 +8,7 @@ #include "ezlopi_timer.h" #include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -25,7 +26,7 @@ static int __0061_get_item(l_ezlopi_item_t *item, void *arg); static int __0061_get_cjson_value(l_ezlopi_item_t *item, void *arg); static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device); static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device); -static void _0061_update_from_device(l_ezlopi_item_t *item); +static void _0061_update_from_device(void *arg); //----------------------------------------------------------------------- int sensor_0061_digitalIn_reed_switch(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) @@ -64,15 +65,16 @@ int sensor_0061_digitalIn_reed_switch(e_ezlopi_actions_t action, l_ezlopi_item_t //---------------------------------------------------------------------------------------- static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_door; device->cloud_properties.device_type = dev_type_doorlock; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -84,8 +86,8 @@ static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_dev item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); CJSON_GET_VALUE_INT(cj_device, "logic_inv", item->interface.gpio.gpio_in.invert); item->interface.gpio.gpio_in.enable = true; @@ -102,7 +104,7 @@ static int __0061_prepare(void *arg) s_ezlopi_prep_arg_t *device_prep_arg = (s_ezlopi_prep_arg_t *)arg; if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { - l_ezlopi_device_t *reed_device = ezlopi_device_add_device(); + l_ezlopi_device_t *reed_device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (reed_device) { __prepare_device_cloud_properties(reed_device, device_prep_arg->cjson_device); @@ -118,10 +120,6 @@ static int __0061_prepare(void *arg) ezlopi_device_free_device(reed_device); } } - else - { - ezlopi_device_free_device(reed_device); - } } ret = 1; } @@ -184,12 +182,12 @@ static int __0061_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "dw_is_closed"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "dw_is_closed"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "dw_is_closed"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "dw_is_closed"); ret = 1; } } @@ -203,8 +201,8 @@ static int __0061_get_cjson_value(l_ezlopi_item_t *item, void *arg) cJSON *cj_result = (cJSON *)arg; if (cj_result) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "dw_is_closed"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "dw_is_closed"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "dw_is_closed"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "dw_is_closed"); ret = 1; } } @@ -212,13 +210,18 @@ static int __0061_get_cjson_value(l_ezlopi_item_t *item, void *arg) } //------------------------------------------------------------------------------------------------------------ -static void _0061_update_from_device(l_ezlopi_item_t *item) +static void _0061_update_from_device(void *arg) { + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; if (item) { - char *curret_value = NULL; + const char *curret_value = NULL; item->interface.gpio.gpio_in.value = gpio_get_level(item->interface.gpio.gpio_in.gpio_num); - item->interface.gpio.gpio_in.value = (false == item->interface.gpio.gpio_in.invert) ? (item->interface.gpio.gpio_in.value) : (!item->interface.gpio.gpio_in.value); + + item->interface.gpio.gpio_in.value = (false == item->interface.gpio.gpio_in.invert) + ? (item->interface.gpio.gpio_in.value) + : (!item->interface.gpio.gpio_in.value); + if (0 == (item->interface.gpio.gpio_in.value)) // when D0 -> 0V, { curret_value = reed_door_window_states[0]; //"dw_is_opened"; @@ -227,6 +230,7 @@ static void _0061_update_from_device(l_ezlopi_item_t *item) { curret_value = reed_door_window_states[1]; //"dw_is_closed"; } + if (curret_value != (char *)item->user_arg) // calls update only if there is change in state { item->user_arg = (void *)curret_value; diff --git a/devices/sensor_0062_other_MQ7_CO_detector/sensor_0062_other_MQ7_CO_detector.c b/devices/sensor_0062_other_MQ7_CO_detector/sensor_0062_other_MQ7_CO_detector.c index 8f07161cd..378a4f8a0 100644 --- a/devices/sensor_0062_other_MQ7_CO_detector/sensor_0062_other_MQ7_CO_detector.c +++ b/devices/sensor_0062_other_MQ7_CO_detector/sensor_0062_other_MQ7_CO_detector.c @@ -10,6 +10,7 @@ #include "ezlopi_timer.h" #include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -94,7 +95,7 @@ static int __0062_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ7_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ7_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ7_device_digi) { __prepare_device_digi_cloud_properties(MQ7_device_digi, device_prep_arg->cjson_device); @@ -109,17 +110,13 @@ static int __0062_prepare(void *arg) ezlopi_device_free_device(MQ7_device_digi); } } - else - { - ezlopi_device_free_device(MQ7_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- mq7_value_t *MQ7_value = (mq7_value_t *)malloc(sizeof(mq7_value_t)); if (NULL != MQ7_value) { memset(MQ7_value, 0, sizeof(mq7_value_t)); - l_ezlopi_device_t *MQ7_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ7_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ7_device_adc) { __prepare_device_adc_cloud_properties(MQ7_device_adc, device_prep_arg->cjson_device); @@ -137,7 +134,6 @@ static int __0062_prepare(void *arg) } else { - ezlopi_device_free_device(MQ7_device_adc); free(MQ7_value); } ret = 1; @@ -181,15 +177,16 @@ static int __0062_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -201,22 +198,23 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ7-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); } //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -228,8 +226,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ7-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -260,18 +258,18 @@ static int __0062_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq7_value_t *MQ7_value = ((mq7_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ7_value->_CO_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ7_value->_CO_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ7_value->_CO_ppm); free(valueFormatted); } ret = 1; @@ -290,15 +288,15 @@ static int __0062_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq7_value_t *MQ7_value = ((mq7_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ7_value->_CO_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ7_value->_CO_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ7_value->_CO_ppm); free(valueFormatted); } ret = 1; @@ -314,7 +312,7 @@ static int __0062_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0063_other_MQ9_LPG_flameable_detector/sensor_0063_other_MQ9_LPG_flameable_detector.c b/devices/sensor_0063_other_MQ9_LPG_flameable_detector/sensor_0063_other_MQ9_LPG_flameable_detector.c index 7a4cbf6f8..0e7750435 100644 --- a/devices/sensor_0063_other_MQ9_LPG_flameable_detector/sensor_0063_other_MQ9_LPG_flameable_detector.c +++ b/devices/sensor_0063_other_MQ9_LPG_flameable_detector/sensor_0063_other_MQ9_LPG_flameable_detector.c @@ -9,6 +9,7 @@ #include "ezlopi_adc.h" #include "ezlopi_timer.h" #include "ezlopi_actions.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" @@ -93,7 +94,7 @@ static int __0063_prepare(void *arg) if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { //--------------------------- DIGI - DEVICE 1 -------------------------------------------- - l_ezlopi_device_t *MQ9_device_digi = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ9_device_digi = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ9_device_digi) { __prepare_device_digi_cloud_properties(MQ9_device_digi, device_prep_arg->cjson_device); @@ -108,17 +109,13 @@ static int __0063_prepare(void *arg) ezlopi_device_free_device(MQ9_device_digi); } } - else - { - ezlopi_device_free_device(MQ9_device_digi); - } //---------------------------- ADC - DEVICE 2 ------------------------------------------- mq9_value_t *MQ9_value = (mq9_value_t *)malloc(sizeof(mq9_value_t)); if (NULL != MQ9_value) { memset(MQ9_value, 0, sizeof(mq9_value_t)); - l_ezlopi_device_t *MQ9_device_adc = ezlopi_device_add_device(); + l_ezlopi_device_t *MQ9_device_adc = ezlopi_device_add_device(device_prep_arg->cjson_device); if (MQ9_device_adc) { __prepare_device_adc_cloud_properties(MQ9_device_adc, device_prep_arg->cjson_device); @@ -136,7 +133,6 @@ static int __0063_prepare(void *arg) } else { - ezlopi_device_free_device(MQ9_device_adc); free(MQ9_value); } ret = 1; @@ -180,15 +176,16 @@ static int __0063_init(l_ezlopi_item_t *item) //------------------------------------------------------------------------------------------------------ static void __prepare_device_digi_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_security_sensor; device->cloud_properties.subcategory = subcategory_gas; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -200,8 +197,8 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio1", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio1_str, item->interface.gpio.gpio_in.gpio_num); TRACE_I("MQ9-> DIGITAL_PIN: %d ", item->interface.gpio.gpio_in.gpio_num); char *user_arg = (char *)malloc(40); if (user_arg) @@ -213,15 +210,16 @@ static void __prepare_item_digi_cloud_properties(l_ezlopi_item_t *item, cJSON *c //------------------------------------------------------------------------------------------------------ static void __prepare_device_adc_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_not_defined; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device, void *user_data) { @@ -233,8 +231,8 @@ static void __prepare_item_adc_cloud_properties(l_ezlopi_item_t *item, cJSON *cj item->cloud_properties.scale = scales_parts_per_million; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio2", item->interface.adc.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_gpio2_str, item->interface.adc.gpio_num); TRACE_I("MQ9-> ADC_PIN: %d ", item->interface.adc.gpio_num); item->interface.adc.resln_bit = 3; // ADC 12_bit @@ -265,18 +263,18 @@ static int __0063_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq9_value_t *MQ9_value = ((mq9_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ9_value->_LPG_flameable_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ9_value->_LPG_flameable_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ9_value->_LPG_flameable_ppm); free(valueFormatted); } ret = 1; @@ -295,15 +293,15 @@ static int __0063_get_cjson_value(l_ezlopi_item_t *item, void *arg) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "no_gas"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "no_gas"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "no_gas"); } if (ezlopi_item_name_smoke_density == item->cloud_properties.item_name) { mq9_value_t *MQ9_value = ((mq9_value_t *)item->user_arg); char *valueFormatted = ezlopi_valueformatter_float(MQ9_value->_LPG_flameable_ppm); - cJSON_AddStringToObject(cj_result, "valueFormatted", valueFormatted); - cJSON_AddNumberToObject(cj_result, "value", MQ9_value->_LPG_flameable_ppm); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, valueFormatted); + cJSON_AddNumberToObject(cj_result, ezlopi_value_str, MQ9_value->_LPG_flameable_ppm); free(valueFormatted); } ret = 1; @@ -319,7 +317,7 @@ static int __0063_notify(l_ezlopi_item_t *item) { if (ezlopi_item_name_gas_alarm == item->cloud_properties.item_name) { - char *curret_value = NULL; + const char *curret_value = NULL; if (0 == gpio_get_level(item->interface.gpio.gpio_in.gpio_num)) // when D0 -> 0V, { // curret_value = "combustible_gas_detected"; diff --git a/devices/sensor_0065_digitalIn_float_switch/sensor_0065_digitalIn_float_switch.c b/devices/sensor_0065_digitalIn_float_switch/sensor_0065_digitalIn_float_switch.c index 29c832980..89bc69b57 100644 --- a/devices/sensor_0065_digitalIn_float_switch/sensor_0065_digitalIn_float_switch.c +++ b/devices/sensor_0065_digitalIn_float_switch/sensor_0065_digitalIn_float_switch.c @@ -8,6 +8,7 @@ #include "ezlopi_timer.h" #include "ezlopi_actions.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_valueformatter.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_device_value_updated.h" @@ -26,9 +27,9 @@ static int __0065_init(l_ezlopi_item_t *item); static int __0065_get_item(l_ezlopi_item_t *item, void *arg); static int __0065_get_cjson_value(l_ezlopi_item_t *item, void *arg); -static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device); +static void __0065_update_from_device(void *arg); static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device); -static void __0065_update_from_device(l_ezlopi_item_t *item); +static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device); //----------------------------------------------------------------------- int sensor_0065_digitalIn_float_switch(e_ezlopi_actions_t action, l_ezlopi_item_t *item, void *arg, void *user_arg) @@ -67,15 +68,16 @@ int sensor_0065_digitalIn_float_switch(e_ezlopi_actions_t action, l_ezlopi_item_ //---------------------------------------------------------------------------------------- static void __prepare_device_cloud_properties(l_ezlopi_device_t *device, cJSON *cj_device) { - char *device_name = NULL; - CJSON_GET_VALUE_STRING(cj_device, "dev_name", device_name); - ASSIGN_DEVICE_NAME_V2(device, device_name); + // char *device_name = NULL; + // CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + // ASSIGN_DEVICE_NAME_V2(device, device_name); + // device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + device->cloud_properties.category = category_level_sensor; device->cloud_properties.subcategory = subcategory_water; device->cloud_properties.device_type = dev_type_sensor; device->cloud_properties.info = NULL; device->cloud_properties.device_type_id = NULL; - device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); } static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_device) { @@ -87,8 +89,8 @@ static void __prepare_item_cloud_properties(l_ezlopi_item_t *item, cJSON *cj_dev item->cloud_properties.scale = NULL; item->cloud_properties.item_id = ezlopi_cloud_generate_item_id(); - CJSON_GET_VALUE_INT(cj_device, "dev_type", item->interface_type); // _max = 10 - CJSON_GET_VALUE_INT(cj_device, "gpio", item->interface.gpio.gpio_in.gpio_num); + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_type_str, item->interface_type); // _max = 10 + CJSON_GET_VALUE_INT(cj_device, ezlopi_dev_name_str, item->interface.gpio.gpio_in.gpio_num); CJSON_GET_VALUE_INT(cj_device, "logic_inv", item->interface.gpio.gpio_in.invert); item->interface.gpio.gpio_in.enable = true; @@ -105,7 +107,7 @@ static int __0065_prepare(void *arg) s_ezlopi_prep_arg_t *device_prep_arg = (s_ezlopi_prep_arg_t *)arg; if (device_prep_arg && (NULL != device_prep_arg->cjson_device)) { - l_ezlopi_device_t *float_device = ezlopi_device_add_device(); + l_ezlopi_device_t *float_device = ezlopi_device_add_device(device_prep_arg->cjson_device); if (float_device) { __prepare_device_cloud_properties(float_device, device_prep_arg->cjson_device); @@ -121,10 +123,6 @@ static int __0065_prepare(void *arg) ezlopi_device_free_device(float_device); } } - else - { - ezlopi_device_free_device(float_device); - } } ret = 1; } @@ -186,12 +184,12 @@ static int __0065_get_item(l_ezlopi_item_t *item, void *arg) cJSON_AddItemToArray(json_array_enum, json_value); } } - cJSON_AddItemToObject(cj_result, "enum", json_array_enum); + cJSON_AddItemToObject(cj_result, ezlopi_enum_str, json_array_enum); } //-------------------------------------------------------------------------------------- - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "water_level_ok"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "water_level_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "water_level_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "water_level_ok"); ret = 1; } } @@ -206,8 +204,8 @@ static int __0065_get_cjson_value(l_ezlopi_item_t *item, void *arg) cJSON *cj_result = (cJSON *)arg; if (cj_result) { - cJSON_AddStringToObject(cj_result, "valueFormatted", (char *)item->user_arg ? item->user_arg : "water_level_ok"); - cJSON_AddStringToObject(cj_result, "value", (char *)item->user_arg ? item->user_arg : "water_level_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_valueFormatted_str, (char *)item->user_arg ? item->user_arg : "water_level_ok"); + cJSON_AddStringToObject(cj_result, ezlopi_value_str, (char *)item->user_arg ? item->user_arg : "water_level_ok"); ret = 1; } } @@ -215,11 +213,12 @@ static int __0065_get_cjson_value(l_ezlopi_item_t *item, void *arg) } //------------------------------------------------------------------------------------------------------------ -static void __0065_update_from_device(l_ezlopi_item_t *item) +static void __0065_update_from_device(void *arg) { + l_ezlopi_item_t *item = (l_ezlopi_item_t *)arg; if (item) { - char *curret_value = NULL; + const char *curret_value = NULL; item->interface.gpio.gpio_in.value = gpio_get_level(item->interface.gpio.gpio_in.gpio_num); item->interface.gpio.gpio_in.value = (false == item->interface.gpio.gpio_in.invert) ? (item->interface.gpio.gpio_in.value) : (!item->interface.gpio.gpio_in.value); if (0 == (item->interface.gpio.gpio_in.value)) // when D0 -> 0V, diff --git a/devices/sensor_0066_other_R307_FingerPrint/sensor_0066_other_R307_FingerPrint.c b/devices/sensor_0066_other_R307_FingerPrint/sensor_0066_other_R307_FingerPrint.c index d82b4fac0..28f82d05f 100644 --- a/devices/sensor_0066_other_R307_FingerPrint/sensor_0066_other_R307_FingerPrint.c +++ b/devices/sensor_0066_other_R307_FingerPrint/sensor_0066_other_R307_FingerPrint.c @@ -213,7 +213,7 @@ static int __0066_prepare(void *arg) { cJSON *cj_device = (dev_prep_arg->cjson_device); /* device-1 */ - l_ezlopi_device_t *fingerprint_device = ezlopi_device_add_device(); + l_ezlopi_device_t *fingerprint_device = ezlopi_device_add_device(cj_device); if (fingerprint_device) { server_packet_t *user_data = (server_packet_t *)malloc(sizeof(server_packet_t)); diff --git a/ezlopi-cloud/data/data.c b/ezlopi-cloud/data/data.c index 8825d8cf0..83bdedca2 100644 --- a/ezlopi-cloud/data/data.c +++ b/ezlopi-cloud/data/data.c @@ -8,106 +8,6 @@ #include "ezlopi_cloud_keywords.h" #include "ezlopi_cloud_methods_str.h" -static const char *data_list_start = "{\"method\":\"hub.data.list\",\"msg_id\":%d,\"api\":\"1.0\",\"error\":null,\"id\":\"%.*s\",\"result\":{\"settings\":{"; -static const char *data_list_cont = "\"first_start\":{\"value\": 0}"; -static const char *data_list_end = "}},\"sender\":%.*s}"; - -#if 0 -char *data_list_v2(const char *payload, uint32_t len, struct json_token *method, uint32_t msg_count) -{ - char *string_response = NULL; - cJSON *cjson_request = cJSON_ParseWithLength(payload, len); - - if (cjson_request) - { - cJSON *id = cJSON_GetObjectItem(cjson_request, ezlopi_id_str); - cJSON *sender = cJSON_GetObjectItem(cjson_request, ezlopi_sender_str); - - cJSON *cjson_response = cJSON_CreateObject(); - if (cjson_response) - { - cJSON_AddStringToObject(cjson_response, ezlopi_key_method_str, method_hub_data_value_list_request); - cJSON_AddNumberToObject(cjson_response, ezlopi_msg_id_str, msg_count); - cJSON_AddStringToObject(cjson_response, ezlopi_id_str, id ? (id->valuestring ? id->valuestring : "") : ""); - cJSON_AddStringToObject(cjson_response, ezlopi_sender_str, sender ? (sender->valuestring ? sender->valuestring : "{}") : "{}"); - cJSON_AddNullToObject(cjson_response, "error"); - - cJSON *cjson_result = cJSON_CreateObject(); - if (cjson_result) - { - cJSON *cjson_devices_array = cJSON_CreateArray(); - if (cjson_devices_array) - { - - l_ezlopi_configured_devices_t *registered_devices = ezlopi_devices_list_get_configured_items(); - int dev_idx = 0; - while (NULL != registered_devices) - { - if (NULL != registered_devices->properties) - { - cJSON *cjson_properties = cJSON_CreateObject(); - if (cjson_properties) - { - char tmp_string[64]; - snprintf(tmp_string, sizeof(tmp_string), "%08x", registered_devices->properties->ezlopi_cloud.device_id); - cJSON_AddStringToObject(cjson_properties, "_id", tmp_string); - cJSON_AddStringToObject(cjson_properties, "deviceTypeId", ezlopi_ezlopi_str); - cJSON_AddStringToObject(cjson_properties, "parentDeviceId", ""); - cJSON_AddStringToObject(cjson_properties, "category", registered_devices->properties->ezlopi_cloud.category); - cJSON_AddStringToObject(cjson_properties, "subcategory", registered_devices->properties->ezlopi_cloud.subcategory); - cJSON_AddBoolToObject(cjson_properties, "batteryPowered", registered_devices->properties->ezlopi_cloud.battery_powered); - cJSON_AddStringToObject(cjson_properties, "name", registered_devices->properties->ezlopi_cloud.device_name); - cJSON_AddStringToObject(cjson_properties, "type", registered_devices->properties->ezlopi_cloud.device_type); - cJSON_AddBoolToObject(cjson_properties, "reachable", registered_devices->properties->ezlopi_cloud.reachable); - cJSON_AddBoolToObject(cjson_properties, "persistent", true); - cJSON_AddBoolToObject(cjson_properties, "serviceNotification", false); - cJSON_AddBoolToObject(cjson_properties, "armed", false); - snprintf(tmp_string, sizeof(tmp_string), "%08x", registered_devices->properties->ezlopi_cloud.room_id); - cJSON_AddStringToObject(cjson_properties, "roomId", tmp_string); - cJSON_AddStringToObject(cjson_properties, "security", ""); - cJSON_AddBoolToObject(cjson_properties, "ready", true); - cJSON_AddStringToObject(cjson_properties, "status", "synced"); - cJSON_AddStringToObject(cjson_properties, "info", "{}"); - - if (!cJSON_AddItemToArray(cjson_devices_array, cjson_properties)) - { - cJSON_Delete(cjson_properties); - } - } - } - - registered_devices = registered_devices->next; - } - - if (!cJSON_AddItemToObjectCS(cjson_result, "devices", cjson_devices_array)) - { - cJSON_Delete(cjson_devices_array); - } - } - - if (!cJSON_AddItemToObjectCS(cjson_response, ezlopi_result_str, cjson_result)) - { - cJSON_Delete(cjson_result); - } - } - - string_response = cJSON_Print(cjson_response); - if (string_response) - { - printf("'hub.items.list' response:\r\n%s\r\n", string_response); - cJSON_Minify(string_response); - } - - cJSON_Delete(cjson_response); - } - - cJSON_Delete(cjson_request); - } - - return string_response; -} -#endif - void data_list(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); @@ -115,13 +15,13 @@ void data_list(cJSON *cj_request, cJSON *cj_response) cJSON *cjson_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); if (cjson_result) { - cJSON *cj_settings = cJSON_AddObjectToObject(cjson_result, "settings"); + cJSON *cj_settings = cJSON_AddObjectToObject(cjson_result, ezlopi_settings_str); if (cj_settings) { - cJSON *cj_first_start = cJSON_AddObjectToObject(cj_settings, "first_start"); + cJSON *cj_first_start = cJSON_AddObjectToObject(cj_settings, ezlopi_first_start_str); if (cj_first_start) { - cJSON_AddNumberToObject(cj_first_start, "value", 0); + cJSON_AddNumberToObject(cj_first_start, ezlopi_value_str, 0); } #warning "WARNING: work required here!" @@ -176,24 +76,24 @@ static cJSON *ezlopi_cloud_data_list_settings(l_ezlopi_configured_devices_t *ezl { char tmp_string[64]; snprintf(tmp_string, sizeof(tmp_string), "%08x", ezlopi_device->properties->ezlopi_cloud.device_id); - cJSON_AddStringToObject(cjson_settings, "_id", tmp_string); - cJSON_AddStringToObject(cjson_settings, "deviceTypeId", ezlopi_ezlopi_str); - cJSON_AddStringToObject(cjson_settings, "parentDeviceId", ""); - cJSON_AddStringToObject(cjson_settings, "category", ezlopi_device->properties->ezlopi_cloud.category); - cJSON_AddStringToObject(cjson_settings, "subcategory", ezlopi_device->properties->ezlopi_cloud.subcategory); - cJSON_AddBoolToObject(cjson_settings, "batteryPowered", ezlopi_device->properties->ezlopi_cloud.battery_powered); + cJSON_AddStringToObject(cjson_settings, ezlopi__id_str, tmp_string); + cJSON_AddStringToObject(cjson_settings, ezlopi_deviceTypeId_str, ezlopi_ezlopi_str); + cJSON_AddStringToObject(cjson_settings, ezlopi_parentDeviceId_str, ezlopi__str); + cJSON_AddStringToObject(cjson_settings, ezlopi_category_str, ezlopi_device->properties->ezlopi_cloud.category); + cJSON_AddStringToObject(cjson_settings, ezlopi_subcategory_str, ezlopi_device->properties->ezlopi_cloud.subcategory); + cJSON_AddBoolToObject(cjson_settings, ezlopi_batteryPowered_str, ezlopi_device->properties->ezlopi_cloud.battery_powered); cJSON_AddStringToObject(cjson_settings, "name", ezlopi_device->properties->ezlopi_cloud.device_name); - cJSON_AddStringToObject(cjson_settings, "type", ezlopi_device->properties->ezlopi_cloud.device_type); - cJSON_AddBoolToObject(cjson_settings, "reachable", ezlopi_device->properties->ezlopi_cloud.reachable); - cJSON_AddBoolToObject(cjson_settings, "persistent", true); - cJSON_AddBoolToObject(cjson_settings, "serviceNotification", false); + cJSON_AddStringToObject(cjson_settings, ezlopi_type_str, ezlopi_device->properties->ezlopi_cloud.device_type); + cJSON_AddBoolToObject(cjson_settings, ezlopi_reachable_str, ezlopi_device->properties->ezlopi_cloud.reachable); + cJSON_AddBoolToObject(cjson_settings, ezlopi_persistent_str, true); + cJSON_AddBoolToObject(cjson_settings, ezlopi_serviceNotification_str, false); cJSON_AddBoolToObject(cjson_settings, "armed", false); snprintf(tmp_string, sizeof(tmp_string), "%08x", ezlopi_device->properties->ezlopi_cloud.room_id); - cJSON_AddStringToObject(cjson_settings, "roomId", tmp_string); - cJSON_AddStringToObject(cjson_settings, "security", ""); - cJSON_AddBoolToObject(cjson_settings, "ready", true); - cJSON_AddStringToObject(cjson_settings, "status", "synced"); - cJSON_AddStringToObject(cjson_settings, "info", "{}"); + cJSON_AddStringToObject(cjson_settings, ezlopi_roomId_str, tmp_string); + cJSON_AddStringToObject(cjson_settings, ezlopi_security_str, ezlopi__str); + cJSON_AddBoolToObject(cjson_settings, ezlopi_ready_str, true); + cJSON_AddStringToObject(cjson_settings, ezlopi_status_str, ezlopi_synced_str); + cJSON_AddStringToObject(cjson_settings, ezlopi_info_str, "{}"); } } diff --git a/ezlopi-cloud/devices/devices.c b/ezlopi-cloud/devices/devices.c index db3d95222..10bf2e81a 100644 --- a/ezlopi-cloud/devices/devices.c +++ b/ezlopi-cloud/devices/devices.c @@ -46,3 +46,78 @@ void devices_list_v3(cJSON *cj_request, cJSON *cj_response) TRACE_E("Failed to create 'result'"); } } + +void device_name_set(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + if (cj_result) + { + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + cJSON *cj_device_id = cJSON_GetObjectItem(cj_params, ezlopi__id_str); + if (cj_device_id) + { + uint32_t device_id = strtoul(cj_device_id->valuestring, NULL, 16); + if (device_id) + { + ezlopi_device_name_set_by_device_id(device_id, cJSON_GetObjectItem(cj_params, ezlopi_name_str)); + } + } + } + } +} + +void device_updated(cJSON *cj_request, cJSON *cj_response) +{ + if (cj_request) + { + cJSON_DeleteItemFromObject(cj_response, ezlopi_sender_str); + cJSON_DeleteItemFromObject(cj_response, ezlopi_error_str); + + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, "hub.device.updated"); + + cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + if (cj_result) + { + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + cJSON *cj_device_id = cJSON_GetObjectItem(cj_params, ezlopi__id_str); + if (cj_device_id && cj_device_id->valuestring) + { + uint32_t device_id = strtoul(cj_device_id->valuestring, NULL, 16); + l_ezlopi_device_t *device_node = ezlopi_device_get_head(); + + while (device_node) + { + if (device_id == device_node->cloud_properties.device_id) + { + char tmp_str[32]; + snprintf(tmp_str, sizeof(tmp_str), "%08x", device_id); + cJSON_AddStringToObject(cj_result, ezlopi__id_str, tmp_str); + cJSON_AddStringToObject(cj_result, ezlopi_name_str, device_node->cloud_properties.device_name); + cJSON_AddTrueToObject(cj_result, ezlopi_syncNotification_str); + + s_ezlopi_cloud_controller_t *controller_info = ezlopi_device_get_controller_information(); + + if (controller_info) + { + cJSON_AddBoolToObject(cj_result, ezlopi_armed_str, controller_info->armed ? ezlopi_true_str : ezlopi_false_str); + cJSON_AddBoolToObject(cj_result, ezlopi_serviceNotification_str, controller_info->service_notification ? ezlopi_true_str : ezlopi_false_str); + } + + break; + } + + device_node = device_node->next; + } + } + } + } + } +} diff --git a/ezlopi-cloud/devices/devices.h b/ezlopi-cloud/devices/devices.h index 383959e9e..66939e537 100644 --- a/ezlopi-cloud/devices/devices.h +++ b/ezlopi-cloud/devices/devices.h @@ -11,6 +11,9 @@ extern "C" #endif void devices_list_v3(cJSON *cj_request, cJSON *cj_response); + void device_name_set(cJSON *cj_request, cJSON *cj_response); + void device_updated(cJSON *cj_request, cJSON *cj_response); + // char *devices_settings_list(const char *payload, uint32_t len, struct json_token *method, uint32_t msg_count); // char *devices_name_set(const char *payload, uint32_t len, struct json_token *method, uint32_t msg_count); diff --git a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.c b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.c index 6417cbf40..3ebef8214 100644 --- a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.c +++ b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.c @@ -2,12 +2,12 @@ #define __EZLOPI_CLOUD_KEYWORDS_H__ #include "stdio.h" -// const char *ezlopi_ezlopi_str = "ezlopi"; - +const char *ezlopi_ezlopi_str = "ezlopi"; +const char *ezlopi__str = ""; const char *ezlopi_id_str = "id"; const char *ezlopi_ids_str = "ids"; const char *ezlopi__id_str = "_id"; -const char *ezlopi_device_id_str = "deviceId"; +const char *ezlopi_deviceId_str = "deviceId"; const char *ezlopi_device_ids_str = "deviceIds"; const char *ezlopi_sender_str = "sender"; const char *ezlopi_name_str = "name"; @@ -24,20 +24,205 @@ const char *ezlopi_scene_name_str = "scene_name"; const char *ezlopi_status_str = "status"; const char *ezlopi_userNotification_str = "userNotification"; const char *ezlopi_notifications_str = "notifications"; +const char *ezlopi_syncNotification_str = "syncNotification"; const char *ezlopi_room_id_str = "room_id"; const char *ezlopi_room_name_str = "room_name"; const char *ezlopi_error_str = "error"; - -const char *ezlopi_scale_str = "scale"; +const char *ezlopi_expressions_str = "expressions"; +const char *ezlopi_is_group_str = "is_group"; +const char *ezlopi_user_notifications_str = "user_notifications"; +const char *ezlopi_house_modes_str = "house_modes"; +const char *ezlopi_blockOptions_str = "blockOptions"; +const char *ezlopi_version_str = "version"; +const char *ezlopi_args_str = "args"; +const char *ezlopi_delay_str = "delay"; +const char *ezlopi_fields_str = "fields"; +const char *ezlopi_blockType_str = "blockType"; +const char *ezlopi_when_str = "when"; +const char *ezlopi_enabled_str = "enabled"; +const char *ezlopi_group_id_str = "group_id"; +const char *ezlopi_parent_id_str = "parent_id"; +const char *ezlopi_then_str = "then"; +const char *ezlopi_else_str = "else"; +const char *ezlopi_hasGetter_str = "hasGetter"; +const char *ezlopi_hasSetter_str = "hasSetter"; +const char *ezlopi_show_str = "show"; +const char *ezlopi_hwaddr_str = "hwaddr"; +const char *ezlopi_internetAvailable_str = "internetAvailable"; +const char *ezlopi_network_str = "network"; +const char *ezlopi_wan_str = "wan"; +const char *ezlopi_wifi_str = "wifi"; +const char *ezlopi_up_str = "up"; +const char *ezlopi_interfaces_str = "interfaces"; +const char *ezlopi_auto_str = "auto"; +const char *ezlopi_type_str = "type"; +const char *ezlopi_ip_str = "ip"; +const char *ezlopi_mask_str = "mask"; +const char *ezlopi_gateway_str = "gateway"; +const char *ezlopi_mode_str = "mode"; +const char *ezlopi_dhcp_str = "dhcp"; +const char *ezlopi_ipv4_str = "ipv4"; +const char *ezlopi_gateways_str = "gateways"; +const char *ezlopi_label_str = "label"; +const char *ezlopi_yes_str = "yes"; +const char *ezlopi_no_str = "no"; +const char *ezlopi_ready_str = "ready"; +const char *ezlopi_not_ready_str = "not ready"; +const char *ezlopi_settings_str = "settings"; +const char *ezlopi_valueType_str = "valueType"; +const char *ezlopi_metadata_str = "metadata"; +const char *ezlopi_code_str = "code"; +const char *ezlopi_data_str = "data"; +const char *ezlopi_message_str = "message"; +const char *ezlopi_Unknown_method_str = "Unknown method"; +const char *ezlopi_firmware_str = "firmware"; +const char *ezlopi_null_str = "null"; +const char *ezlopi_firmware_version_str = "firmware_version"; +const char *ezlopi_uptime_str = "uptime"; +const char *ezlopi_mac_str = "mac"; +const char *ezlopi_uuid_str = "uuid"; +const char *ezlopi_firmware_type_str = "firmware_type"; +const char *ezlopi_deviceTypeId_str = "deviceTypeId"; +const char *ezlopi_parentDeviceId_str = "parentDeviceId"; +const char *ezlopi_category_str = "category"; +const char *ezlopi_subcategory_str = "subcategory"; +const char *ezlopi_gatewayId_str = "gatewayId"; +const char *ezlopi_batteryPowered_str = "batteryPowered"; +const char *ezlopi_reachable_str = "reachable"; +const char *ezlopi_persistent_str = "persistent"; +const char *ezlopi_serviceNotification_str = "serviceNotification"; +const char *ezlopi_roomId_str = "roomId"; +const char *ezlopi_roomsId_str = "roomsId"; +const char *ezlopi_security_str = "security"; +const char *ezlopi_synced_str = "synced"; +const char *ezlopi_info_str = "info"; +const char *ezlopi_generic_str = "generic"; +const char *ezlopi_firmware_hardware_str = "firmware_hardware"; +const char *ezlopi_plugin_id_str = "plugin_id"; +const char *ezlopi_reason_str = "reason"; +const char *ezlopi_unreachable_reasons_str = "unreachable_reasons"; +const char *ezlopi_unreachable_actions_str = "unreachable_actions"; +const char *ezlopi_manual_device_adding_str = "manual_device_adding"; const char *ezlopi_enum_str = "enum"; +const char *ezlopi_has_getter_str = "has_getter"; +const char *ezlopi_has_setter_str = "has_setter"; +const char *ezlopi_scale_str = "scale"; +const char *ezlopi_value_type_str = "value_type"; +const char *ezlopi_device_id_str = "device_id"; +const char *ezlopi_None_str = "None"; +const char *ezlopi_device_type_id_str = "device_type_id"; +const char *ezlopi_parent_device_id_str = "parent_device_id"; +const char *ezlopi_manufacturer_str = "manufacturer"; +const char *ezlopi_ezlopi_device_type_str = "ezlopi_device_type"; +const char *ezlopi_model_str = "model"; +const char *ezlopi_device_name_str = "device_name"; +const char *ezlopi_brand_str = "brand"; +const char *ezlopi_build__date_str = "build-date"; +const char *ezlopi_hash_str = "hash"; +const char *ezlopi_branch_str = "branch"; +const char *ezlopi_developer_str = "developer"; +const char *ezlopi_battery_powered_str = "battery_powered"; +const char *ezlopi_house_modes_options_str = "house_modes_options"; +const char *ezlopi_parent_room_str = "parent_room"; +const char *ezlopi_valueFormatted_str = "valueFormatted"; +const char *ezlopi_gpio_scl_str = "gpio_scl"; +const char *ezlopi_gpio_sda_str = "gpio_sda"; +const char *ezlopi_dev_type_str = "dev_type"; +const char *ezlopi_dev_name_str = "dev_name"; +const char *ezlopi_gpio_str = "gpio"; +const char *ezlopi_false_str = "false"; +const char *ezlopi_true_str = "true"; +const char *ezlopi_gpio_out_str = "gpio_out"; +const char *ezlopi_gpio_in_str = "gpio_in"; +const char *ezlopi_dev_detail_str = "dev_detail"; +const char *ezlopi_id_item_str = "id_item"; +const char *ezlopi_showCode_str = "showCode"; +const char *ezlopi_deviceName_str = "deviceName"; +const char *ezlopi_items_str = "items"; +const char *ezlopi_itemName_str = "itemName"; +const char *ezlopi_device_item_names_str = "device_item_names"; +const char *ezlopi_variable_str = "variable"; +const char *ezlopi_filterTypes_str = "filterTypes"; +const char *ezlopi_deviceCategory_str = "deviceCategory"; +const char *ezlopi_deviceSubcategory_str = "deviceSubcategory"; +const char *ezlopi_roomName_str = "roomName"; +const char *ezlopi_is_ip_str = "is_ip"; +const char *ezlopi_ip_inv_str = "ip_inv"; +const char *ezlopi_val_ip_str = "val_ip"; +const char *ezlopi_pullup_ip_str = "pullup_ip"; +const char *ezlopi_op_inv_str = "op_inv"; +const char *ezlopi_val_op_str = "val_op"; +const char *ezlopi_pullup_op_str = "pullup_op"; +const char *ezlopi_wifi_ssid_str = "wifi_ssid"; +const char *ezlopi_wifi_connection_status_str = "wifi-connection_status"; +const char *ezlopi_internet_status_str = "internet_status"; +const char *ezlopi_build_date_str = "build_date"; +const char *ezlopi_unknown_str = "unknown"; +const char *ezlopi_manufacturer_name_str = "manufacturer_name"; +const char *ezlopi_firmware_build_str = "firmware_build"; +const char *ezlopi_chip_str = "chip"; +const char *ezlopi_provisioned_status_str = "provisioned_status"; +const char *ezlopi_model_number_str = "model_number"; +const char *ezlopi_serial_str = "serial"; +const char *ezlopi_uuid_provisioning_str = "uuid_provisioning"; +const char *ezlopi_provision_server_str = "provision_server"; +const char *ezlopi_cloud_server_str = "cloud_server"; +const char *ezlopi_provision_token_str = "provision_token"; +const char *ezlopi_device_type_ezlopi_str = "device_type_ezlopi"; +const char *ezlopi_ssl_private_key_str = "ssl_private_key"; +const char *ezlopi_ssl_shared_key_str = "ssl_shared_key"; +const char *ezlopi_signing_ca_certificate_str = "signing_ca_certificate"; +const char *ezlopi_len_str = "len"; +const char *ezlopi_total_len_str = "total_len"; +const char *ezlopi_sequence_str = "sequence"; +const char *ezlopi_user_id_str = "user_id"; +const char *ezlopi_userId_str = "userId"; +const char *ezlopi_coinfig_time_str = "coinfig_time"; +const char *ezlopi_wifi_password_str = "wifi_password"; +const char *ezlopi_ca_cert_str = "ca_cert"; +const char *ezlopi_config_id_str = "config_id"; +const char *ezlopi_config_time_str = "config_time"; +const char *ezlopi_ssid_str = "ssid"; +const char *ezlopi_dev_type_ezlopi_str = "dev_type_ezlopi"; +const char *ezlopi_dev_flash_str = "dev_flash"; +const char *ezlopi_dev_free_flash_str = "dev_free_flash"; +const char *ezlopi_manf_name_str = "manf_name"; +const char *ezlopi_model_num_str = "model_num"; +const char *ezlopi_sta_connection_str = "sta_connection"; +const char *ezlopi_ip_sta_str = "ip_sta"; +const char *ezlopi_ip_nmask_str = "ip_nmask"; +const char *ezlopi_ip_gw_str = "ip_gw"; +const char *ezlopi_pass_str = "pass"; +const char *ezlopi_cmd_str = "cmd"; +const char *ezlopi_uuid_prov_str = "uuid_prov"; +const char *ezlopi_status_write_str = "status_write"; +const char *ezlopi_status_connect_str = "status_connect"; +const char *ezlopi_first_start_str = "first_start"; +const char *ezlopi_gpio1_str = "gpio1"; +const char *ezlopi_gpio2_str = "gpio2"; +const char *ezlopi_gpio3_str = "gpio3"; +const char *ezlopi_gpio_tx_str = "gpio_tx"; +const char *ezlopi_gpio_rx_str = "gpio_rx"; +const char *ezlopi_baud_str = "baud"; +const char *ezlopi_baud_rate_str = "baud_rate"; +const char *ezlopi_sceneId_str = "sceneId"; +const char *ezlopi_changed_by_str = "changed_by"; + +const char *scene_status_started_str = "started"; +const char *scene_status_finished_str = "finished"; +const char *scene_status_partially_finished_str = "partially_finished"; +const char *scene_status_failed_str = "failed"; +const char *scene_status_stopped_str = "stopped"; const char *ezlopi_valueformatted_str = "valueFormatted"; const char *ezlopi_value_default_str = "valueDefault"; const char *ezlopi_text_str = "text"; -const char *ezlopi_label_str = "label"; const char *ezlopi_lang_tag_str = "lang_tag"; const char *ezlopi_description_str = "description"; -const char *ezlopi_value_type_str = "valueType"; const char *ezlopi_value_min_str = "valueMin"; const char *ezlopi_value_max_str = "valueMax"; +const char *ezlopi_idle_str = "idle"; + +const char *ezlopi_armed_str = "armed"; +const char *ezlopi_subtype_str = "subtype"; #endif // __EZLOPI_CLOUD_KEYWORDS_H__ diff --git a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.h b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.h index 35b68633f..8a1859008 100644 --- a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.h +++ b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_keywords.h @@ -2,12 +2,12 @@ #define __EZLOPI_CLOUD_KEYWORDS_H__ #include "stdio.h" -// extern const char *ezlopi_ezlopi_str; - +extern const char *ezlopi_ezlopi_str; +extern const char *ezlopi__str; extern const char *ezlopi_id_str; extern const char *ezlopi_ids_str; extern const char *ezlopi__id_str; -extern const char *ezlopi_device_id_str; +extern const char *ezlopi_deviceId_str; extern const char *ezlopi_device_ids_str; extern const char *ezlopi_sender_str; extern const char *ezlopi_name_str; @@ -24,9 +24,198 @@ extern const char *ezlopi_scene_name_str; extern const char *ezlopi_status_str; extern const char *ezlopi_userNotification_str; extern const char *ezlopi_notifications_str; +extern const char *ezlopi_syncNotification_str; extern const char *ezlopi_room_id_str; extern const char *ezlopi_room_name_str; extern const char *ezlopi_error_str; +extern const char *ezlopi_expressions_str; +extern const char *ezlopi_is_group_str; +extern const char *ezlopi_user_notifications_str; +extern const char *ezlopi_house_modes_str; +extern const char *ezlopi_blockOptions_str; +extern const char *ezlopi_version_str; +extern const char *ezlopi_args_str; +extern const char *ezlopi_delay_str; +extern const char *ezlopi_fields_str; +extern const char *ezlopi_blockType_str; +extern const char *ezlopi_when_str; +extern const char *ezlopi_enabled_str; +extern const char *ezlopi_group_id_str; +extern const char *ezlopi_parent_id_str; +extern const char *ezlopi_then_str; +extern const char *ezlopi_else_str; +extern const char *ezlopi_hasGetter_str; +extern const char *ezlopi_hasSetter_str; +extern const char *ezlopi_show_str; +extern const char *ezlopi_hwaddr_str; +extern const char *ezlopi_internetAvailable_str; +extern const char *ezlopi_network_str; +extern const char *ezlopi_wan_str; +extern const char *ezlopi_wifi_str; +extern const char *ezlopi_up_str; +extern const char *ezlopi_interfaces_str; +extern const char *ezlopi_auto_str; +extern const char *ezlopi_type_str; +extern const char *ezlopi_ip_str; +extern const char *ezlopi_mask_str; +extern const char *ezlopi_gateway_str; +extern const char *ezlopi_mode_str; +extern const char *ezlopi_dhcp_str; +extern const char *ezlopi_ipv4_str; +extern const char *ezlopi_gateways_str; +extern const char *ezlopi_label_str; +extern const char *ezlopi_yes_str; +extern const char *ezlopi_no_str; +extern const char *ezlopi_ready_str; +extern const char *ezlopi_not_ready_str; +extern const char *ezlopi_settings_str; +extern const char *ezlopi_valueType_str; +extern const char *ezlopi_metadata_str; +extern const char *ezlopi_code_str; +extern const char *ezlopi_data_str; +extern const char *ezlopi_message_str; +extern const char *ezlopi_Unknown_method_str; +extern const char *ezlopi_firmware_str; +extern const char *ezlopi_null_str; +extern const char *ezlopi_firmware_version_str; +extern const char *ezlopi_uptime_str; +extern const char *ezlopi_mac_str; +extern const char *ezlopi_uuid_str; +extern const char *ezlopi_firmware_type_str; +extern const char *ezlopi_deviceTypeId_str; +extern const char *ezlopi_parentDeviceId_str; +extern const char *ezlopi_category_str; +extern const char *ezlopi_subcategory_str; +extern const char *ezlopi_gatewayId_str; +extern const char *ezlopi_batteryPowered_str; +extern const char *ezlopi_reachable_str; +extern const char *ezlopi_persistent_str; +extern const char *ezlopi_serviceNotification_str; +extern const char *ezlopi_roomId_str; +extern const char *ezlopi_roomsId_str; +extern const char *ezlopi_security_str; +extern const char *ezlopi_synced_str; +extern const char *ezlopi_info_str; +extern const char *ezlopi_generic_str; +extern const char *ezlopi_firmware_hardware_str; +extern const char *ezlopi_plugin_id_str; +extern const char *ezlopi_reason_str; +extern const char *ezlopi_unreachable_reasons_str; +extern const char *ezlopi_unreachable_actions_str; +extern const char *ezlopi_manual_device_adding_str; +extern const char *ezlopi_enum_str; +extern const char *ezlopi_has_getter_str; +extern const char *ezlopi_has_setter_str; +extern const char *ezlopi_scale_str; +extern const char *ezlopi_value_type_str; +extern const char *ezlopi_device_id_str; +extern const char *ezlopi_None_str; +extern const char *ezlopi_device_type_id_str; +extern const char *ezlopi_parent_device_id_str; +extern const char *ezlopi_manufacturer_str; +extern const char *ezlopi_ezlopi_device_type_str; +extern const char *ezlopi_model_str; +extern const char *ezlopi_device_name_str; +extern const char *ezlopi_brand_str; +extern const char *ezlopi_build__date_str; +extern const char *ezlopi_hash_str; +extern const char *ezlopi_branch_str; +extern const char *ezlopi_developer_str; +extern const char *ezlopi_battery_powered_str; +extern const char *ezlopi_house_modes_options_str; +extern const char *ezlopi_parent_room_str; +extern const char *ezlopi_valueFormatted_str; +extern const char *ezlopi_gpio_scl_str; +extern const char *ezlopi_gpio_sda_str; +extern const char *ezlopi_dev_type_str; +extern const char *ezlopi_dev_name_str; +extern const char *ezlopi_gpio_str; +extern const char *ezlopi_false_str; +extern const char *ezlopi_true_str; +extern const char *ezlopi_gpio_out_str; +extern const char *ezlopi_gpio_in_str; +extern const char *ezlopi_dev_detail_str; +extern const char *ezlopi_id_item_str; +extern const char *ezlopi_showCode_str; +extern const char *ezlopi_deviceName_str; +extern const char *ezlopi_items_str; +extern const char *ezlopi_itemName_str; +extern const char *ezlopi_device_item_names_str; +extern const char *ezlopi_variable_str; +extern const char *ezlopi_filterTypes_str; +extern const char *ezlopi_deviceCategory_str; +extern const char *ezlopi_deviceSubcategory_str; +extern const char *ezlopi_roomName_str; +extern const char *ezlopi_is_ip_str; +extern const char *ezlopi_ip_inv_str; +extern const char *ezlopi_val_ip_str; +extern const char *ezlopi_pullup_ip_str; +extern const char *ezlopi_op_inv_str; +extern const char *ezlopi_val_op_str; +extern const char *ezlopi_pullup_op_str; +extern const char *ezlopi_wifi_ssid_str; +extern const char *ezlopi_wifi_connection_status_str; +extern const char *ezlopi_internet_status_str; +extern const char *ezlopi_build_date_str; +extern const char *ezlopi_unknown_str; +extern const char *ezlopi_manufacturer_name_str; +extern const char *ezlopi_firmware_build_str; +extern const char *ezlopi_chip_str; +extern const char *ezlopi_provisioned_status_str; +extern const char *ezlopi_model_number_str; +extern const char *ezlopi_serial_str; +extern const char *ezlopi_uuid_provisioning_str; +extern const char *ezlopi_provision_server_str; +extern const char *ezlopi_cloud_server_str; +extern const char *ezlopi_provision_token_str; +extern const char *ezlopi_device_type_ezlopi_str; +extern const char *ezlopi_ssl_private_key_str; +extern const char *ezlopi_ssl_shared_key_str; +extern const char *ezlopi_signing_ca_certificate_str; +extern const char *ezlopi_len_str; +extern const char *ezlopi_total_len_str; +extern const char *ezlopi_sequence_str; +extern const char *ezlopi_user_id_str; +extern const char *ezlopi_userId_str; +extern const char *ezlopi_coinfig_time_str; +extern const char *ezlopi_wifi_password_str; +extern const char *ezlopi_ca_cert_str; +extern const char *ezlopi_config_id_str; +extern const char *ezlopi_config_time_str; +extern const char *ezlopi_ssid_str; +extern const char *ezlopi_dev_type_ezlopi_str; +extern const char *ezlopi_dev_flash_str; +extern const char *ezlopi_dev_free_flash_str; +extern const char *ezlopi_manf_name_str; +extern const char *ezlopi_model_num_str; +extern const char *ezlopi_sta_connection_str; +extern const char *ezlopi_ip_sta_str; +extern const char *ezlopi_ip_nmask_str; +extern const char *ezlopi_ip_gw_str; +extern const char *ezlopi_pass_str; +extern const char *ezlopi_cmd_str; +extern const char *ezlopi_uuid_prov_str; +extern const char *ezlopi_status_write_str; +extern const char *ezlopi_status_connect_str; +extern const char *ezlopi_first_start_str; +extern const char *ezlopi_gpio1_str; +extern const char *ezlopi_gpio2_str; +extern const char *ezlopi_gpio3_str; +extern const char *ezlopi_gpio_tx_str; +extern const char *ezlopi_gpio_rx_str; +extern const char *ezlopi_baud_str; +extern const char *ezlopi_baud_rate_str; +extern const char *ezlopi_sceneId_str; +extern const char *ezlopi_changed_by_str; + +extern const char *scene_status_started_str; +extern const char *scene_status_finished_str; +extern const char *scene_status_partially_finished_str; +extern const char *scene_status_failed_str; +extern const char *scene_status_stopped_str; + +extern const char *ezlopi_armed_str; +extern const char *ezlopi_subtype_str; extern const char *ezlopi_scale_str; extern const char *ezlopi_enum_str; @@ -39,5 +228,7 @@ extern const char *ezlopi_description_str; extern const char *ezlopi_value_type_str; extern const char *ezlopi_value_min_str; extern const char *ezlopi_value_max_str; +extern const char *ezlopi_no_str; +extern const char *ezlopi_idle_str; #endif // __EZLOPI_CLOUD_KEYWORDS_H__ diff --git a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.c b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.c index 842df01f3..d6e6bbd71 100644 --- a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.c +++ b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.c @@ -4,7 +4,6 @@ /***************************************************************************************** * Methods * *****************************************************************************************/ -const char *msg_sub_class_method_hub_item_updated = "method_hub_item_updated"; const char *method_hub_item_updated = "hub.item.updated"; const char *method_add_device = "addDevice"; const char *method_add_all_devices = "addAllDevices"; @@ -117,5 +116,14 @@ const char *method_hub_reboot = "hub.reboot"; // const char *method_hub_detection_devices_scan_start = "hub.detection.devices.scan.start"; // const char *method_hub_detection_devices_scans_top = "hub.detection.devices.scan.stop"; const char *method_hub_scene_run_progress = "hub.scene.run.progress"; +const char *ezlopi_rpc_method_notfound_str = "rpc.method.notfound"; +const char *ezlopi_hub_scene_changed_str = "hub.scene.changed"; +const char *ezlopi_hub_scene_added_str = "hub.scene.added"; +const char *ezlopi_hub_scene_deleted_str = "hub.scene.deleted"; + +const char *ezlopi_hub_room_created_str = "hub.room.created"; +const char *ezlopi_hub_room_edited_str = "hub.room.edited"; +const char *ezlopi_hub_room_deleted_str = "hub.room.deleted"; +const char *ezlopi_hub_room_reordered_str = "hub.room.reordered"; #endif // __EZLOPI_METHODS_STR_H__ \ No newline at end of file diff --git a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.h b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.h index 36a2524df..119dac6a7 100644 --- a/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.h +++ b/ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_methods_str.h @@ -4,7 +4,6 @@ /***************************************************************************************** * Methods * *****************************************************************************************/ -extern const char *msg_sub_class_method_hub_item_updated; extern const char *method_hub_item_updated; extern const char *method_add_device; extern const char *method_add_all_devices; @@ -117,5 +116,14 @@ extern const char *method_hub_reboot; // extern const char *method_hub_detection_devices_scan_start; // extern const char *method_hub_detection_devices_scans_top; extern const char *method_hub_scene_run_progress; +extern const char *ezlopi_rpc_method_notfound_str; +extern const char *ezlopi_hub_scene_changed_str; +extern const char *ezlopi_hub_scene_added_str; +extern const char *ezlopi_hub_scene_deleted_str; + +extern const char *ezlopi_hub_room_created_str; +extern const char *ezlopi_hub_room_edited_str; +extern const char *ezlopi_hub_room_deleted_str; +extern const char *ezlopi_hub_room_reordered_str; #endif // __EZLOPI_METHODS_STR_H__ \ No newline at end of file diff --git a/ezlopi-cloud/favorite/favorite.c b/ezlopi-cloud/favorite/favorite.c index 9c7b0cf17..dc5b7049d 100644 --- a/ezlopi-cloud/favorite/favorite.c +++ b/ezlopi-cloud/favorite/favorite.c @@ -3,7 +3,6 @@ #include "favorite.h" #include "trace.h" - #include "cJSON.h" #include "ezlopi_cloud_methods_str.h" #include "ezlopi_cloud_keywords.h" @@ -36,7 +35,7 @@ void favorite_list_v3(cJSON *cj_request, cJSON *cj_response) { cj_devices_req = elem; } - else if (strstr("items", elem->valuestring)) + else if (strstr(ezlopi_items_str, elem->valuestring)) { cj_items_req = elem; } @@ -55,7 +54,7 @@ void favorite_list_v3(cJSON *cj_request, cJSON *cj_response) if (cj_favorites) { cJSON *cj_device_list = cj_devices_req ? cJSON_AddArrayToObject(cj_favorites, "devices") : NULL; - cJSON *cj_items_list = cj_items_req ? cJSON_AddArrayToObject(cj_favorites, "items") : NULL; + cJSON *cj_items_list = cj_items_req ? cJSON_AddArrayToObject(cj_favorites,ezlopi_items_str) : NULL; cJSON *cj_rules_list = cj_rules_req ? cJSON_AddArrayToObject(cj_favorites, "rules") : NULL; l_ezlopi_device_t *curr_device = ezlopi_device_get_head(); @@ -68,7 +67,7 @@ void favorite_list_v3(cJSON *cj_request, cJSON *cj_response) cJSON *cj_device = cJSON_CreateObject(); if (cj_device) { - cJSON_AddStringToObject(cj_device, "_id", ""); + cJSON_AddStringToObject(cj_device, ezlopi__id_str, ezlopi__str); if (!cJSON_AddItemToArray(cj_device_list, cj_device)) { @@ -83,43 +82,3 @@ void favorite_list_v3(cJSON *cj_request, cJSON *cj_response) } } } - -#if 0 // v2.x -void favorite_list(cJSON *cj_request, cJSON *cj_response) -{ - cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); - cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - if (cj_result) - { - cJSON *cj_favorites = cJSON_AddObjectToObject(cj_result, "favorites"); - if (cj_favorites) - { - cJSON *cj_device_list = cJSON_AddArrayToObject(cj_favorites, "devices"); - if (cj_device_list) - { - l_ezlopi_configured_devices_t *registered_devices = ezlopi_devices_list_get_configured_items(); - while (NULL != registered_devices) - { - if (NULL != registered_devices->properties) - { - cJSON *cj_device = cJSON_CreateObject(); - if (cj_device) - { - cJSON_AddStringToObject(cj_device, "_id", ""); - cJSON_AddStringToObject(cj_device, "name", ""); - - if (!cJSON_AddItemToArray(cj_device_list, cj_device)) - { - cJSON_Delete(cj_device); - } - } - } - - registered_devices = registered_devices->next; - } - } - } - } -} -#endif diff --git a/ezlopi-cloud/firmware/firmware.c b/ezlopi-cloud/firmware/firmware.c index 717129f34..797afd802 100644 --- a/ezlopi-cloud/firmware/firmware.c +++ b/ezlopi-cloud/firmware/firmware.c @@ -5,20 +5,20 @@ #include "data.h" #include "trace.h" +#include "ezlopi_ota.h" +#include "ezlopi_event_group.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_devices_list.h" -#include "ezlopi_cloud_keywords.h" -#include "ezlopi_cloud_methods_str.h" +#include "ezlopi_factory_info.h" +#include "ezlopi_cloud_constants.h" #include "version.h" -#include "ezlopi_ota.h" -#include "ezlopi_factory_info.h" -#include "ezlopi_event_group.h" void firmware_update_start(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON_AddNullToObject(cj_response, "error"); + cJSON_AddNullToObject(cj_response, ezlopi_error_str); cJSON_AddObjectToObject(cj_response, ezlopi_result_str); cJSON *version = NULL; @@ -26,13 +26,13 @@ void firmware_update_start(cJSON *cj_request, cJSON *cj_response) cJSON *params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (params) { - version = cJSON_GetObjectItem(params, "version"); - TRACE_D("OTA - version: %s", (version && version->valuestring) ? version->valuestring : "null"); + version = cJSON_GetObjectItem(params, ezlopi_version_str); + TRACE_D("OTA - version: %s", (version && version->valuestring) ? version->valuestring : ezlopi_null_str); source_urls = cJSON_GetObjectItem(params, "urls"); if (source_urls) { - cJSON *firmware_url = cJSON_GetObjectItem(source_urls, "firmware"); + cJSON *firmware_url = cJSON_GetObjectItem(source_urls, ezlopi_firmware_str); TRACE_D("OTA - source: %s", (firmware_url && firmware_url->valuestring) ? firmware_url->valuestring : "null"); if (firmware_url) @@ -56,24 +56,24 @@ void firmware_info_get(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON_AddNullToObject(cj_response, "error"); + cJSON_AddNullToObject(cj_response, ezlopi_error_str); cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - cJSON *result = cJSON_GetObjectItem(cj_request, "result"); + cJSON *result = cJSON_GetObjectItem(cj_request, ezlopi_result_str); if (result) { cJSON *version = NULL; - version = cJSON_GetObjectItem(result, "version"); + version = cJSON_GetObjectItem(result, ezlopi_version_str); if (version != NULL) TRACE_I("version: %s", version->valuestring); - TRACE_D("Upgrading to version: %s", (version && version->valuestring) ? version->valuestring : "null"); + TRACE_D("Upgrading to version: %s", (version && version->valuestring) ? version->valuestring : ezlopi_null_str); cJSON *source_urls = NULL; source_urls = cJSON_GetObjectItem(result, "urls"); if (source_urls) { - cJSON *firmware_url = cJSON_GetObjectItem(source_urls, "firmware"); - TRACE_D("OTA - source: %s", (source_urls && source_urls->valuestring) ? source_urls->valuestring : "null"); + cJSON *firmware_url = cJSON_GetObjectItem(source_urls, ezlopi_firmware_str); + TRACE_D("OTA - source: %s", (source_urls && source_urls->valuestring) ? source_urls->valuestring : ezlopi_null_str); if (firmware_url) { @@ -96,33 +96,32 @@ cJSON *firmware_send_firmware_query_to_nma_server(uint32_t message_count) cJSON *cj_request = cJSON_CreateObject(); if (NULL != cj_request) { - cJSON_AddStringToObject(cj_request, "method", "cloud.firmware.info.get"); + cJSON_AddStringToObject(cj_request, ezlopi_key_method_str, "cloud.firmware.info.get"); cJSON_AddNumberToObject(cj_request, "id", message_count); - // cJSON_AddObjectToObject(cj_request, "sender"); cJSON *cj_params = cJSON_AddObjectToObject(cj_request, ezlopi_params_str); if (cj_params) { char firmware_version_str[20]; snprintf(firmware_version_str, sizeof(firmware_version_str), "%s.%d", VERSION_STR, BUILD); - cJSON_AddStringToObject(cj_params, "firmware_version", firmware_version_str); + cJSON_AddStringToObject(cj_params, ezlopi_firmware_version_str, firmware_version_str); char *device_type = ezlopi_factory_info_v3_get_device_type(); if (device_type) { if (isalpha(device_type[0])) { - cJSON_AddStringToObject(cj_params, "firmware_type", device_type); // + cJSON_AddStringToObject(cj_params, ezlopi_firmware_type_str, device_type); // } else { - cJSON_AddStringToObject(cj_params, "firmware_type", "unknown"); + cJSON_AddStringToObject(cj_params, ezlopi_firmware_type_str, ezlopi_generic_str); } } else { - cJSON_AddStringToObject(cj_params, "firmware_type", "unknown"); + cJSON_AddStringToObject(cj_params, ezlopi_firmware_type_str, ezlopi_generic_str); } - cJSON_AddStringToObject(cj_params, "firmware_hardware", CONFIG_IDF_TARGET); + cJSON_AddStringToObject(cj_params, ezlopi_firmware_hardware_str, CONFIG_IDF_TARGET); } char *str_request = cJSON_Print(cj_request); diff --git a/ezlopi-cloud/gateways/CMakeLists.txt b/ezlopi-cloud/gateways/CMakeLists.txt index aead66f5c..6ab7eeba9 100644 --- a/ezlopi-cloud/gateways/CMakeLists.txt +++ b/ezlopi-cloud/gateways/CMakeLists.txt @@ -1,5 +1,5 @@ file(GLOB_RECURSE src_files "*.c*") -idf_component_register(SRCS "gateways.c" "${src_files}" +idf_component_register(SRCS "gateways_commands_str.c" "gateways.c" "${src_files}" INCLUDE_DIRS "." REQUIRES trace json ezlopi_factory_info diff --git a/ezlopi-cloud/gateways/gateways.c b/ezlopi-cloud/gateways/gateways.c index f70bd67f8..5d7316443 100644 --- a/ezlopi-cloud/gateways/gateways.c +++ b/ezlopi-cloud/gateways/gateways.c @@ -1,11 +1,13 @@ #include -#include "gateways.h" #include "trace.h" #include "ezlopi_cloud.h" #include "ezlopi_gateway.h" #include "ezlopi_cloud_constants.h" +#include "gateways.h" +#include "gateways_commands_str.h" + void gateways_list(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); @@ -13,7 +15,7 @@ void gateways_list(cJSON *cj_request, cJSON *cj_response) cJSON *cjson_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); if (cjson_result) { - cJSON *cjson_gateways_array = cJSON_AddArrayToObject(cjson_result, "gateways"); + cJSON *cjson_gateways_array = cJSON_AddArrayToObject(cjson_result, ezlopi_gateways_str); if (cjson_gateways_array) { @@ -23,30 +25,29 @@ void gateways_list(cJSON *cj_request, cJSON *cj_response) s_ezlopi_gateway_t *gateway_prop = ezlopi_gateway_get(); if (gateway_prop) { - char *tmp_buffer[32]; + char tmp_buffer[32]; snprintf(tmp_buffer, sizeof(tmp_buffer), "%08x", gateway_prop->_id); - // cJSON_AddNumberToObject(cjson_gateway, "_id", ezlopi_cloud_get_gateway_id()); - cJSON_AddStringToObject(cjson_gateway, "_id", tmp_buffer); - cJSON_AddStringToObject(cjson_gateway, "addItemDictionaryValueCommand", gateway_prop->add_item_dictionary_value_command ? gateway_prop->add_item_dictionary_value_command : ""); - cJSON_AddStringToObject(cjson_gateway, "checkDeviceCommand", gateway_prop->check_device_command ? gateway_prop->check_device_command : ""); - cJSON_AddStringToObject(cjson_gateway, "clearItemDictionaryCommand", gateway_prop->clear_item_dictionary_command ? gateway_prop->clear_item_dictionary_command : ""); - cJSON_AddStringToObject(cjson_gateway, "forceRemoveDeviceCommand", gateway_prop->force_remove_device_command ? gateway_prop->force_remove_device_command : ""); - cJSON_AddStringToObject(cjson_gateway, "label", gateway_prop->label ? gateway_prop->label : ""); - cJSON_AddStringToObject(cjson_gateway, "manualDeviceAdding", gateway_prop->manual_device_adding ? "yes" : "no"); - cJSON_AddStringToObject(cjson_gateway, "name", gateway_prop->name ? gateway_prop->name : ""); - cJSON_AddStringToObject(cjson_gateway, "pluginId", gateway_prop->pluginid ? gateway_prop->pluginid : ""); - cJSON_AddBoolToObject(cjson_gateway, "ready", gateway_prop->ready ? true : false); - cJSON_AddStringToObject(cjson_gateway, "removeItemDictionaryValueCommand", gateway_prop->remove_item_dictionary_value_command ? gateway_prop->remove_item_dictionary_value_command : ""); - cJSON_AddStringToObject(cjson_gateway, "resetSettingCommand", gateway_prop->reset_setting_command ? gateway_prop->reset_setting_command : ""); - cJSON_AddStringToObject(cjson_gateway, "setGatewaySettingValueCommand", gateway_prop->set_gateway_setting_vlaue_command ? gateway_prop->set_gateway_setting_vlaue_command : ""); - cJSON_AddStringToObject(cjson_gateway, "setItemDictionaryValueCommand", gateway_prop->set_item_dictionary_value_command ? gateway_prop->set_item_dictionary_value_command : ""); - cJSON_AddStringToObject(cjson_gateway, "setItemValueCommand", gateway_prop->set_item_value_command ? gateway_prop->set_item_value_command : ""); - cJSON_AddStringToObject(cjson_gateway, "setSettingDictionaryValueCommand", gateway_prop->set_setting_dictionary_value_command ? gateway_prop->set_setting_dictionary_value_command : ""); - cJSON_AddStringToObject(cjson_gateway, "setSettingValueCommand", gateway_prop->set_setting_value_command ? gateway_prop->set_setting_value_command : ""); - cJSON_AddNumberToObject(cjson_gateway, "settings", gateway_prop->settings); - cJSON_AddStringToObject(cjson_gateway, "status", gateway_prop->ready ? "ready" : "not ready"); - cJSON_AddStringToObject(cjson_gateway, "updateDeviceFirmwareCommand", gateway_prop->update_device_firmware_command ? gateway_prop->update_device_firmware_command : ""); + cJSON_AddStringToObject(cjson_gateway, ezlopi__id_str, tmp_buffer); + cJSON_AddStringToObject(cjson_gateway, gw_addItemDictionaryValueCommand_str, gateway_prop->add_item_dictionary_value_command ? gateway_prop->add_item_dictionary_value_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_checkDeviceCommand_str, gateway_prop->check_device_command ? gateway_prop->check_device_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_clearItemDictionaryCommand_str, gateway_prop->clear_item_dictionary_command ? gateway_prop->clear_item_dictionary_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_forceRemoveDeviceCommand_str, gateway_prop->force_remove_device_command ? gateway_prop->force_remove_device_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, ezlopi_label_str, gateway_prop->label ? gateway_prop->label : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_manualDeviceAdding_str, gateway_prop->manual_device_adding ? ezlopi_yes_str : ezlopi_no_str); + cJSON_AddStringToObject(cjson_gateway, ezlopi_name_str, gateway_prop->name ? gateway_prop->name : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_pluginId_str, gateway_prop->pluginid ? gateway_prop->pluginid : ezlopi__str); + cJSON_AddBoolToObject(cjson_gateway, ezlopi_ready_str, gateway_prop->ready ? true : false); + cJSON_AddStringToObject(cjson_gateway, gw_removeItemDictionaryValueCommand_str, gateway_prop->remove_item_dictionary_value_command ? gateway_prop->remove_item_dictionary_value_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_resetSettingCommand_str, gateway_prop->reset_setting_command ? gateway_prop->reset_setting_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_setGatewaySettingValueCommand_str, gateway_prop->set_gateway_setting_vlaue_command ? gateway_prop->set_gateway_setting_vlaue_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_setItemDictionaryValueCommand_str, gateway_prop->set_item_dictionary_value_command ? gateway_prop->set_item_dictionary_value_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_setItemValueCommand_str, gateway_prop->set_item_value_command ? gateway_prop->set_item_value_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_setSettingDictionaryValueCommand_str, gateway_prop->set_setting_dictionary_value_command ? gateway_prop->set_setting_dictionary_value_command : ezlopi__str); + cJSON_AddStringToObject(cjson_gateway, gw_setSettingValueCommand_str, gateway_prop->set_setting_value_command ? gateway_prop->set_setting_value_command : ezlopi__str); + cJSON_AddNumberToObject(cjson_gateway, ezlopi_settings_str, gateway_prop->settings); + cJSON_AddStringToObject(cjson_gateway, ezlopi_status_str, gateway_prop->ready ? ezlopi_ready_str : ezlopi_not_ready_str); + cJSON_AddStringToObject(cjson_gateway, gw_updateDeviceFirmwareCommand_str, gateway_prop->update_device_firmware_command ? gateway_prop->update_device_firmware_command : ezlopi__str); } if (!cJSON_AddItemToArray(cjson_gateways_array, cjson_gateway)) diff --git a/ezlopi-cloud/gateways/gateways_commands_str.c b/ezlopi-cloud/gateways/gateways_commands_str.c new file mode 100644 index 000000000..d966e999f --- /dev/null +++ b/ezlopi-cloud/gateways/gateways_commands_str.c @@ -0,0 +1,16 @@ +#include "gateways_commands_str.h" + +const char *gw_addItemDictionaryValueCommand_str = "addItemDictionaryValueCommand"; +const char *gw_checkDeviceCommand_str = "checkDeviceCommand"; +const char *gw_clearItemDictionaryCommand_str = "clearItemDictionaryCommand"; +const char *gw_forceRemoveDeviceCommand_str = "forceRemoveDeviceCommand"; +const char *gw_manualDeviceAdding_str = "manualDeviceAdding"; +const char *gw_pluginId_str = "pluginId"; +const char *gw_removeItemDictionaryValueCommand_str = "removeItemDictionaryValueCommand"; +const char *gw_resetSettingCommand_str = "resetSettingCommand"; +const char *gw_setGatewaySettingValueCommand_str = "setGatewaySettingValueCommand"; +const char *gw_setItemDictionaryValueCommand_str = "setItemDictionaryValueCommand"; +const char *gw_setItemValueCommand_str = "setItemValueCommand"; +const char *gw_setSettingDictionaryValueCommand_str = "setSettingDictionaryValueCommand"; +const char *gw_setSettingValueCommand_str = "setSettingValueCommand"; +const char *gw_updateDeviceFirmwareCommand_str = "updateDeviceFirmwareCommand"; diff --git a/ezlopi-cloud/gateways/gateways_commands_str.h b/ezlopi-cloud/gateways/gateways_commands_str.h new file mode 100644 index 000000000..e8597edb4 --- /dev/null +++ b/ezlopi-cloud/gateways/gateways_commands_str.h @@ -0,0 +1,20 @@ +#ifndef __GATEWAYS_COMMANDS_STR_H__ +#define __GATEWAYS_COMMANDS_STR_H__ + +extern const char *gw_addItemDictionaryValueCommand_str; +extern const char *gw_checkDeviceCommand_str; +extern const char *gw_clearItemDictionaryCommand_str; +extern const char *gw_forceRemoveDeviceCommand_str; +extern const char *gw_manualDeviceAdding_str; +extern const char *gw_pluginId_str; +extern const char *gw_not_ready_str; +extern const char *gw_removeItemDictionaryValueCommand_str; +extern const char *gw_resetSettingCommand_str; +extern const char *gw_setGatewaySettingValueCommand_str; +extern const char *gw_setItemDictionaryValueCommand_str; +extern const char *gw_setItemValueCommand_str; +extern const char *gw_setSettingDictionaryValueCommand_str; +extern const char *gw_setSettingValueCommand_str; +extern const char *gw_updateDeviceFirmwareCommand_str; + +#endif // __GATEWAYS_COMMANDS_STR_H__ diff --git a/ezlopi-cloud/info/info.c b/ezlopi-cloud/info/info.c index eb91cbefa..a3bd17ee1 100644 --- a/ezlopi-cloud/info/info.c +++ b/ezlopi-cloud/info/info.c @@ -50,14 +50,14 @@ void info_get(cJSON *cj_request, cJSON *cj_response) { char *device_uuid = ezlopi_factory_info_v3_get_device_uuid(); // #include "esp_app_format.h" - cJSON_AddStringToObject(cjson_result, "model", ezlopi_factory_info_v3_get_device_type()); + cJSON_AddStringToObject(cjson_result, ezlopi_model_str, ezlopi_factory_info_v3_get_device_type()); cJSON_AddStringToObject(cjson_result, "architecture", CONFIG_SDK_TOOLPREFIX); - cJSON_AddStringToObject(cjson_result, "firmware", VERSION_STR); + cJSON_AddStringToObject(cjson_result, ezlopi_firmware_str, VERSION_STR); cJSON_AddStringToObject(cjson_result, "kernel", "FreeRTOS"); cJSON_AddStringToObject(cjson_result, "hardware", CONFIG_IDF_TARGET); - cJSON_AddNumberToObject(cjson_result, "serial", ezlopi_factory_info_v3_get_id()); + cJSON_AddNumberToObject(cjson_result, ezlopi_serial_str, ezlopi_factory_info_v3_get_id()); - cJSON_AddStringToObject(cjson_result, "uuid", device_uuid ? device_uuid : ""); + cJSON_AddStringToObject(cjson_result, ezlopi_uuid_str, device_uuid ? device_uuid : ezlopi__str); cJSON_AddBoolToObject(cjson_result, "offlineAnonymousAccess", true); cJSON_AddBoolToObject(cjson_result, "offlineInsecureAccess", true); @@ -66,8 +66,8 @@ void info_get(cJSON *cj_request, cJSON *cj_response) { cJSON_AddNumberToObject(cjson_location, "latitude", 0); cJSON_AddNumberToObject(cjson_location, "longitude", 0); - cJSON_AddStringToObject(cjson_location, "timezone", ""); - cJSON_AddStringToObject(cjson_location, "state", ""); + cJSON_AddStringToObject(cjson_location, "timezone", ezlopi__str); + cJSON_AddStringToObject(cjson_location, "state", ezlopi__str); } cJSON *cjson_build = cJSON_AddObjectToObject(cjson_result, "build"); @@ -85,7 +85,7 @@ void info_get(cJSON *cj_request, cJSON *cj_response) cJSON_AddNumberToObject(cjson_battery, "stateOfCharge", 0); cJSON_AddNumberToObject(cjson_battery, "remainingTime", 0); cJSON_AddNumberToObject(cjson_battery, "health", 0); - cJSON_AddStringToObject(cjson_battery, "status", ""); + cJSON_AddStringToObject(cjson_battery, ezlopi_status_str, ezlopi__str); } // time_t now; @@ -98,7 +98,7 @@ void info_get(cJSON *cj_request, cJSON *cj_response) // localtime_r(&now, &timeinfo); // strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); - cJSON_AddStringToObject(cjson_result, "localtime", ""); + cJSON_AddStringToObject(cjson_result, "localtime", ezlopi__str); // now = sntp_core_get_up_time(); // localtime_r(&now, &timeinfo); @@ -106,12 +106,12 @@ void info_get(cJSON *cj_request, cJSON *cj_response) char *time_string = ezlopi_tick_to_time((uint32_t)(xTaskGetTickCount() / portTICK_PERIOD_MS)); if (time_string) { - cJSON_AddStringToObject(cjson_result, "uptime", time_string); + cJSON_AddStringToObject(cjson_result, ezlopi_uptime_str, time_string); free(time_string); } else { - cJSON_AddStringToObject(cjson_result, "uptime", ""); + cJSON_AddStringToObject(cjson_result, ezlopi_uptime_str, ezlopi__str); } ezlopi_factory_info_v3_free(device_uuid); } diff --git a/ezlopi-cloud/items/items.c b/ezlopi-cloud/items/items.c index d15b4d92a..cbfc5ce39 100644 --- a/ezlopi-cloud/items/items.c +++ b/ezlopi-cloud/items/items.c @@ -2,16 +2,18 @@ #include #include -#include "sdkconfig.h" #include "items.h" #include "trace.h" +#include "sdkconfig.h" // #include "web_provisioning.h" #include "cJSON.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_devices_list.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_cloud_methods_str.h" -#include "ezlopi_devices_list.h" + #include "web_provisioning.h" static cJSON *ezlopi_device_create_item_table_from_prop(l_ezlopi_item_t *item_properties) @@ -21,25 +23,25 @@ static cJSON *ezlopi_device_create_item_table_from_prop(l_ezlopi_item_t *item_pr { char tmp_string[64]; snprintf(tmp_string, sizeof(tmp_string), "%08x", item_properties->cloud_properties.item_id); - cJSON_AddStringToObject(cj_item_properties, "_id", tmp_string); + cJSON_AddStringToObject(cj_item_properties, ezlopi__id_str, tmp_string); snprintf(tmp_string, sizeof(tmp_string), "%08x", item_properties->cloud_properties.device_id); - cJSON_AddStringToObject(cj_item_properties, "deviceId", tmp_string); - // cJSON_AddStringToObject(cj_item_properties, "deviceName", curr_device->cloud_properties.device_name); + cJSON_AddStringToObject(cj_item_properties, ezlopi_deviceId_str, tmp_string); + // cJSON_AddStringToObject(cj_item_properties, ezlopi_deviceName_str, curr_device->cloud_properties.device_name); // cJSON_AddTrueToObject(cj_item_properties, "deviceArmed"); - cJSON_AddBoolToObject(cj_item_properties, "hasGetter", item_properties->cloud_properties.has_getter); - cJSON_AddBoolToObject(cj_item_properties, "hasSetter", item_properties->cloud_properties.has_setter); - cJSON_AddStringToObject(cj_item_properties, "name", item_properties->cloud_properties.item_name); - cJSON_AddTrueToObject(cj_item_properties, "show"); - cJSON_AddStringToObject(cj_item_properties, "valueType", item_properties->cloud_properties.value_type); + cJSON_AddBoolToObject(cj_item_properties, ezlopi_hasGetter_str, item_properties->cloud_properties.has_getter); + cJSON_AddBoolToObject(cj_item_properties, ezlopi_hasSetter_str, item_properties->cloud_properties.has_setter); + cJSON_AddStringToObject(cj_item_properties, ezlopi_name_str, item_properties->cloud_properties.item_name); + cJSON_AddTrueToObject(cj_item_properties, ezlopi_show_str); + cJSON_AddStringToObject(cj_item_properties, ezlopi_valueType_str, item_properties->cloud_properties.value_type); if (item_properties->cloud_properties.scale) { - cJSON_AddStringToObject(cj_item_properties, "scale", item_properties->cloud_properties.scale); + cJSON_AddStringToObject(cj_item_properties, ezlopi_scale_str, item_properties->cloud_properties.scale); } item_properties->func(EZLOPI_ACTION_HUB_GET_ITEM, item_properties, cj_item_properties, item_properties->user_arg); if (item_properties->cloud_properties.scale) { - cJSON_AddStringToObject(cj_item_properties, "scale", item_properties->cloud_properties.scale); + cJSON_AddStringToObject(cj_item_properties, ezlopi_scale_str, item_properties->cloud_properties.scale); } cJSON_AddStringToObject(cj_item_properties, "status", "idle"); } @@ -55,12 +57,12 @@ void items_list_v3(cJSON *cj_request, cJSON *cj_response) cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); if (cj_result) { - cJSON *cj_items_array = cJSON_AddArrayToObject(cj_result, "items"); + cJSON *cj_items_array = cJSON_AddArrayToObject(cj_result, ezlopi_items_str); if (cj_items_array) { - cJSON *params = cJSON_GetObjectItem(cj_request, "params"); + cJSON *params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (params != NULL) { cJSON *device_ids_array = cJSON_GetObjectItem(params, ezlopi_device_ids_str); @@ -197,23 +199,23 @@ void items_update_v3(cJSON *cj_request, cJSON *cj_response) if (cj_result) { char tmp_string[64]; - cJSON_AddStringToObject(cj_result, "_id", item_id_str); + cJSON_AddStringToObject(cj_result, ezlopi__id_str, item_id_str); snprintf(tmp_string, sizeof(tmp_string), "%08x", curr_device->cloud_properties.device_id); - cJSON_AddStringToObject(cj_result, "deviceId", tmp_string); - cJSON_AddStringToObject(cj_result, "deviceName", curr_device->cloud_properties.device_name); - cJSON_AddStringToObject(cj_result, "deviceCategory", curr_device->cloud_properties.category); - cJSON_AddStringToObject(cj_result, "deviceSubcategory", curr_device->cloud_properties.subcategory); - cJSON_AddStringToObject(cj_result, "roomName", ""); - cJSON_AddFalseToObject(cj_result, "serviceNotification"); - cJSON_AddTrueToObject(cj_result, "userNotification"); - cJSON_AddNullToObject(cj_result, "notifications"); - cJSON_AddStringToObject(cj_result, "name", curr_item->cloud_properties.item_name); + cJSON_AddStringToObject(cj_result, ezlopi_deviceId_str, tmp_string); + cJSON_AddStringToObject(cj_result, ezlopi_deviceName_str, curr_device->cloud_properties.device_name); + cJSON_AddStringToObject(cj_result, ezlopi_deviceCategory_str, curr_device->cloud_properties.category); + cJSON_AddStringToObject(cj_result, ezlopi_deviceSubcategory_str, curr_device->cloud_properties.subcategory); + cJSON_AddStringToObject(cj_result, ezlopi_roomName_str, ezlopi__str); + cJSON_AddFalseToObject(cj_result, ezlopi_serviceNotification_str); + cJSON_AddTrueToObject(cj_result, ezlopi_userNotification_str); + cJSON_AddNullToObject(cj_result, ezlopi_notifications_str); + cJSON_AddStringToObject(cj_result, ezlopi_name_str, curr_item->cloud_properties.item_name); if (curr_item->cloud_properties.scale) { - cJSON_AddStringToObject(cj_result, "scale", curr_item->cloud_properties.scale); + cJSON_AddStringToObject(cj_result, ezlopi_scale_str, curr_item->cloud_properties.scale); } curr_item->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, curr_item, cj_result, curr_item->user_arg); - cJSON_AddStringToObject(cj_result, "valueType", curr_item->cloud_properties.value_type); + cJSON_AddStringToObject(cj_result, ezlopi_valueType_str, curr_item->cloud_properties.value_type); } break; @@ -243,25 +245,25 @@ cJSON *ezlopi_cloud_items_updated_from_devices_v3(l_ezlopi_device_t *device, l_e { char tmp_string[64]; snprintf(tmp_string, sizeof(tmp_string), "%08x", item->cloud_properties.item_id); - cJSON_AddStringToObject(cj_result, "_id", tmp_string); + cJSON_AddStringToObject(cj_result, ezlopi__id_str, tmp_string); snprintf(tmp_string, sizeof(tmp_string), "%08x", device->cloud_properties.device_id); - cJSON_AddStringToObject(cj_result, "deviceId", tmp_string); - cJSON_AddStringToObject(cj_result, "deviceName", device->cloud_properties.device_name); - cJSON_AddStringToObject(cj_result, "deviceCategory", device->cloud_properties.category); - cJSON_AddStringToObject(cj_result, "deviceSubcategory", device->cloud_properties.subcategory); - cJSON_AddStringToObject(cj_result, "roomName", ""); // roomName -> logic needs to be understood first - cJSON_AddFalseToObject(cj_result, "serviceNotification"); - cJSON_AddFalseToObject(cj_result, "userNotification"); - cJSON_AddNullToObject(cj_result, "notifications"); - cJSON_AddFalseToObject(cj_result, "syncNotification"); - cJSON_AddStringToObject(cj_result, "name", item->cloud_properties.item_name); + cJSON_AddStringToObject(cj_result, ezlopi_deviceId_str, tmp_string); + cJSON_AddStringToObject(cj_result, ezlopi_deviceName_str, device->cloud_properties.device_name); + cJSON_AddStringToObject(cj_result, ezlopi_deviceCategory_str, device->cloud_properties.category); + cJSON_AddStringToObject(cj_result, ezlopi_deviceSubcategory_str, device->cloud_properties.subcategory); + cJSON_AddStringToObject(cj_result, ezlopi_roomName_str, ezlopi__str); // roomName -> logic needs to be understood first + cJSON_AddFalseToObject(cj_result, ezlopi_serviceNotification_str); + cJSON_AddFalseToObject(cj_result, ezlopi_userNotification_str); + cJSON_AddNullToObject(cj_result, ezlopi_notifications_str); + cJSON_AddFalseToObject(cj_result, ezlopi_syncNotification_str); + cJSON_AddStringToObject(cj_result, ezlopi_name_str, item->cloud_properties.item_name); if (item->cloud_properties.scale) { - cJSON_AddStringToObject(cj_result, "scale", item->cloud_properties.scale); + cJSON_AddStringToObject(cj_result, ezlopi_scale_str, item->cloud_properties.scale); } item->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, item, cj_result, item->user_arg); // registered_device->device->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, registered_device->properties, cj_result, registered_device->user_arg); - cJSON_AddStringToObject(cj_result, "valueType", item->cloud_properties.value_type); + cJSON_AddStringToObject(cj_result, ezlopi_valueType_str, item->cloud_properties.value_type); } } else @@ -273,168 +275,3 @@ cJSON *ezlopi_cloud_items_updated_from_devices_v3(l_ezlopi_device_t *device, l_e return cjson_response; } - -#if 0 // v2.x -void items_list(cJSON *cj_request, cJSON *cj_response) -{ - cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); - cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - - cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - if (cj_result) - { - cJSON *cj_items_array = cJSON_AddArrayToObject(cj_result, "items"); - if (cj_items_array) - { - l_ezlopi_configured_devices_t *registered_device = ezlopi_devices_list_get_configured_items(); - while (NULL != registered_device) - { - if (NULL != registered_device->properties) - { - cJSON *cj_properties = cJSON_CreateObject(); - if (cj_properties) - { - char tmp_string[64]; - snprintf(tmp_string, sizeof(tmp_string), "%08x", registered_device->properties->ezlopi_cloud.item_id); - cJSON_AddStringToObject(cj_properties, "_id", tmp_string); - snprintf(tmp_string, sizeof(tmp_string), "%08x", registered_device->properties->ezlopi_cloud.device_id); - cJSON_AddStringToObject(cj_properties, "deviceId", tmp_string); - cJSON_AddStringToObject(cj_properties, "deviceName", registered_device->properties->ezlopi_cloud.device_name); - cJSON_AddTrueToObject(cj_properties, "deviceArmed"); - cJSON_AddBoolToObject(cj_properties, "hasGetter", registered_device->properties->ezlopi_cloud.has_getter); - cJSON_AddBoolToObject(cj_properties, "hasSetter", registered_device->properties->ezlopi_cloud.has_setter); - cJSON_AddStringToObject(cj_properties, "name", registered_device->properties->ezlopi_cloud.item_name); - cJSON_AddTrueToObject(cj_properties, "show"); - cJSON_AddStringToObject(cj_properties, "valueType", registered_device->properties->ezlopi_cloud.value_type); - - registered_device->device->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, registered_device->properties, cj_properties, registered_device->user_arg); - cJSON_AddStringToObject(cj_properties, "status", "synced"); - - if (!cJSON_AddItemToArray(cj_items_array, cj_properties)) - { - cJSON_Delete(cj_properties); - } - } - } - - registered_device = registered_device->next; - } - } - } -} - -void items_set_value(cJSON *cj_request, cJSON *cj_response) -{ - cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); - cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - - cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); - if (cj_params) - { - char *item_id_str = 0; - CJSON_GET_VALUE_STRING(cj_params, ezlopi__id_str, item_id_str); - int item_id = strtol(item_id_str, NULL, 16); - TRACE_I("item_id: %X", item_id); - - l_ezlopi_configured_devices_t *registered_device = ezlopi_devices_list_get_configured_items(); - while (NULL != registered_device) - { - if (registered_device->properties) - { - if (item_id == registered_device->properties->ezlopi_cloud.item_id) - { - registered_device->device->func(EZLOPI_ACTION_SET_VALUE, registered_device->properties, cj_params, registered_device->user_arg); - } - } - - registered_device = registered_device->next; - } - } -} - -void items_update(cJSON *cj_request, cJSON *cj_response) -{ - cJSON *cjson_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); - if (cjson_params) - { - char *item_id_str = NULL; - CJSON_GET_VALUE_STRING(cjson_params, ezlopi__id_str, item_id_str); - int item_id = strtol(item_id_str, NULL, 16); - - l_ezlopi_configured_devices_t *registered_device = ezlopi_devices_list_get_configured_items(); - while (NULL != registered_device) - { - if (NULL != registered_device->properties) - { - if (item_id == registered_device->properties->ezlopi_cloud.item_id) - { - cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); - cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, method_hub_item_updated); - cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - if (cj_result) - { - char tmp_string[64]; - cJSON_AddStringToObject(cj_result, "_id", item_id_str); - snprintf(tmp_string, sizeof(tmp_string), "%08x", registered_device->properties->ezlopi_cloud.device_id); - cJSON_AddStringToObject(cj_result, "deviceId", tmp_string); - cJSON_AddStringToObject(cj_result, "deviceName", registered_device->properties->ezlopi_cloud.device_name); - cJSON_AddStringToObject(cj_result, "deviceCategory", registered_device->properties->ezlopi_cloud.category); - cJSON_AddStringToObject(cj_result, "deviceSubcategory", registered_device->properties->ezlopi_cloud.subcategory); - cJSON_AddStringToObject(cj_result, "roomName", registered_device->properties->ezlopi_cloud.room_name); - cJSON_AddFalseToObject(cj_result, "serviceNotification"); - cJSON_AddFalseToObject(cj_result, "userNotification"); - cJSON_AddNullToObject(cj_result, "notifications"); - cJSON_AddStringToObject(cj_result, "name", registered_device->properties->ezlopi_cloud.item_name); - registered_device->device->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, registered_device->properties, cj_result, registered_device->user_arg); - cJSON_AddStringToObject(cj_result, "valueType", registered_device->properties->ezlopi_cloud.value_type); - } - - break; - } - } - - registered_device = registered_device->next; - } - } -} - -cJSON *ezlopi_cloud_items_updated_from_devices(l_ezlopi_configured_devices_t *registered_device) -{ - cJSON *cjson_response = cJSON_CreateObject(); - - if (NULL != registered_device) - { - if (NULL != registered_device->properties) - { - if (cjson_response) - { - cJSON_AddStringToObject(cjson_response, ezlopi_msg_subclass_str, method_hub_item_updated); - cJSON_AddNumberToObject(cjson_response, ezlopi_msg_id_str, web_provisioning_get_message_count()); - cJSON_AddStringToObject(cjson_response, ezlopi_id_str, ezlopi_ui_broadcast_str); - cJSON *cj_result = cJSON_AddObjectToObject(cjson_response, ezlopi_result_str); - if (cj_result) - { - char tmp_string[64]; - snprintf(tmp_string, sizeof(tmp_string), "%08x", registered_device->properties->ezlopi_cloud.item_id); - cJSON_AddStringToObject(cj_result, "_id", tmp_string); - snprintf(tmp_string, sizeof(tmp_string), "%08x", registered_device->properties->ezlopi_cloud.device_id); - cJSON_AddStringToObject(cj_result, "deviceId", tmp_string); - cJSON_AddStringToObject(cj_result, "deviceName", registered_device->properties->ezlopi_cloud.device_name); - cJSON_AddStringToObject(cj_result, "deviceCategory", registered_device->properties->ezlopi_cloud.category); - cJSON_AddStringToObject(cj_result, "deviceSubcategory", registered_device->properties->ezlopi_cloud.subcategory); - cJSON_AddStringToObject(cj_result, "roomName", registered_device->properties->ezlopi_cloud.room_name); - cJSON_AddFalseToObject(cj_result, "serviceNotification"); - cJSON_AddFalseToObject(cj_result, "userNotification"); - cJSON_AddNullToObject(cj_result, "notifications"); - cJSON_AddStringToObject(cj_result, "name", registered_device->properties->ezlopi_cloud.item_name); - registered_device->device->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, registered_device->properties, cj_result, registered_device->user_arg); - cJSON_AddStringToObject(cj_result, "valueType", registered_device->properties->ezlopi_cloud.value_type); - } - } - } - } - - return cjson_response; -} -#endif \ No newline at end of file diff --git a/ezlopi-cloud/modes/modes.c b/ezlopi-cloud/modes/modes.c index bd5d73800..9d0e3ebf8 100644 --- a/ezlopi-cloud/modes/modes.c +++ b/ezlopi-cloud/modes/modes.c @@ -24,11 +24,11 @@ void modes_get(cJSON *cj_request, cJSON *cj_response) cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); if (cj_result) { - cJSON_AddStringToObject(cj_result, "current", ""); - cJSON_AddStringToObject(cj_result, "switchTo", ""); - cJSON_AddStringToObject(cj_result, "timeIsLeftToSwitch", ""); - cJSON_AddStringToObject(cj_result, "switchToDelay", ""); - cJSON_AddStringToObject(cj_result, "alarmDelay", ""); + cJSON_AddStringToObject(cj_result, "current", ezlopi__str); + cJSON_AddStringToObject(cj_result, "switchTo", ezlopi__str); + cJSON_AddStringToObject(cj_result, "timeIsLeftToSwitch", ezlopi__str); + cJSON_AddStringToObject(cj_result, "switchToDelay", ezlopi__str); + cJSON_AddStringToObject(cj_result, "alarmDelay", ezlopi__str); cJSON_AddArrayToObject(cj_result, "modes"); } } diff --git a/ezlopi-cloud/network/network.c b/ezlopi-cloud/network/network.c index fbf76386a..78da07758 100644 --- a/ezlopi-cloud/network/network.c +++ b/ezlopi-cloud/network/network.c @@ -5,7 +5,6 @@ #include "ezlopi_wifi.h" #include "trace.h" - #include "cJSON.h" #include "ezlopi_cloud_methods_str.h" #include "ezlopi_cloud_keywords.h" @@ -19,38 +18,38 @@ void network_get(cJSON *cj_request, cJSON *cj_response) cJSON *cjson_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); if (cjson_result) { - cJSON *interfaces_array = cJSON_AddArrayToObject(cjson_result, "interfaces"); + cJSON *interfaces_array = cJSON_AddArrayToObject(cjson_result, ezlopi_interfaces_str); if (interfaces_array) { cJSON *wifi_properties = cJSON_CreateObject(); if (wifi_properties) { char tmp_string[64]; - cJSON_AddStringToObject(wifi_properties, "_id", "wifi"); - cJSON_AddStringToObject(wifi_properties, "enabled", "auto"); + cJSON_AddStringToObject(wifi_properties, ezlopi__id_str, ezlopi_wifi_str); + cJSON_AddStringToObject(wifi_properties, ezlopi_enabled_str, ezlopi_auto_str); uint8_t mac_addr[6]; esp_read_mac(mac_addr, ESP_MAC_WIFI_STA); snprintf(tmp_string, sizeof(tmp_string), "%02x:%02x:%02x:%02x:%02x:%02x", MAC_ADDR_EXPANDED(mac_addr)); - cJSON_AddStringToObject(wifi_properties, "hwaddr", tmp_string); - cJSON_AddBoolToObject(wifi_properties, "internetAvailable", true); - cJSON_AddStringToObject(wifi_properties, "network", "wan"); - cJSON_AddStringToObject(wifi_properties, "status", "up"); - cJSON_AddStringToObject(wifi_properties, "type", "wifi"); + cJSON_AddStringToObject(wifi_properties, ezlopi_hwaddr_str, tmp_string); + cJSON_AddBoolToObject(wifi_properties, ezlopi_internetAvailable_str, true); + cJSON_AddStringToObject(wifi_properties, ezlopi_network_str, ezlopi_wan_str); + cJSON_AddStringToObject(wifi_properties, ezlopi_status_str, ezlopi_up_str); + cJSON_AddStringToObject(wifi_properties, ezlopi_type_str, ezlopi_wifi_str); cJSON *wifi_ipv4 = cJSON_CreateObject(); if (wifi_ipv4) { esp_netif_ip_info_t *ip_info = ezlopi_wifi_get_ip_infos(); snprintf(tmp_string, sizeof(tmp_string), IPSTR, IP2STR(&ip_info->gw)); - cJSON_AddStringToObject(wifi_ipv4, "ip", tmp_string); + cJSON_AddStringToObject(wifi_ipv4, ezlopi_ip_str, tmp_string); snprintf(tmp_string, sizeof(tmp_string), IPSTR, IP2STR(&ip_info->ip)); - cJSON_AddStringToObject(wifi_ipv4, "mask", tmp_string); + cJSON_AddStringToObject(wifi_ipv4, ezlopi_mask_str, tmp_string); snprintf(tmp_string, sizeof(tmp_string), IPSTR, IP2STR(&ip_info->netmask)); - cJSON_AddStringToObject(wifi_ipv4, "gateway", tmp_string); - cJSON_AddStringToObject(wifi_ipv4, "mode", "dhcp"); + cJSON_AddStringToObject(wifi_ipv4, ezlopi_gateway_str, tmp_string); + cJSON_AddStringToObject(wifi_ipv4, ezlopi_mode_str, ezlopi_dhcp_str); - if (!cJSON_AddItemToObjectCS(wifi_properties, "ipv4", wifi_ipv4)) + if (!cJSON_AddItemToObjectCS(wifi_properties, ezlopi_ipv4_str, wifi_ipv4)) { cJSON_Delete(wifi_ipv4); wifi_ipv4 = NULL; diff --git a/ezlopi-cloud/registration/registration.c b/ezlopi-cloud/registration/registration.c index d447c5d8c..7b16b2f6f 100644 --- a/ezlopi-cloud/registration/registration.c +++ b/ezlopi-cloud/registration/registration.c @@ -45,16 +45,16 @@ static void registration_process(void *pv) mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); cJSON_AddStringToObject(cj_register, "id", "__ID__"); - cJSON_AddStringToObject(cj_register, "method", "register"); - cJSON *cj_params = cJSON_AddObjectToObject(cj_register, "params"); + cJSON_AddStringToObject(cj_register, ezlopi_key_method_str, "register"); + cJSON *cj_params = cJSON_AddObjectToObject(cj_register, ezlopi_params_str); if (cj_params) { - cJSON_AddStringToObject(cj_params, "firmware", VERSION_STR); + cJSON_AddStringToObject(cj_params, ezlopi_firmware_str, VERSION_STR); cJSON_AddNumberToObject(cj_params, "timeOffset", 18000); cJSON_AddStringToObject(cj_params, "media", "radio"); cJSON_AddStringToObject(cj_params, "hubType", "32.1"); cJSON_AddStringToObject(cj_params, "mac_address", mac_str); - cJSON_AddNumberToObject(cj_params, "maxFrameSize", 4096); + cJSON_AddNumberToObject(cj_params, "maxFrameSize", (20 * 1024)); } while (false == ezlopi_websocket_client_is_connected()) @@ -62,7 +62,7 @@ static void registration_process(void *pv) vTaskDelay(200 / portTICK_RATE_MS); } - while (0 >= ezlopi_event_group_wait_for_event(EZLOPI_EVENT_NMA_REG, 2000 / portTICK_RATE_MS, true)) + while (0 >= ezlopi_event_group_wait_for_event(EZLOPI_EVENT_NMA_REG, 2000, true)) { web_provisioning_send_to_nma_websocket(cj_register, TRACE_TYPE_B); // vTaskDelay(2000 / portTICK_RATE_MS); @@ -70,6 +70,5 @@ static void registration_process(void *pv) cJSON_Delete(cj_register); } - vTaskDelete(NULL); } diff --git a/ezlopi-cloud/room/CMakeLists.txt b/ezlopi-cloud/room/CMakeLists.txt index 6629021ff..f4d6d0cc4 100644 --- a/ezlopi-cloud/room/CMakeLists.txt +++ b/ezlopi-cloud/room/CMakeLists.txt @@ -1,5 +1,5 @@ file(GLOB_RECURSE src_files "*.c*") idf_component_register(SRCS "room.c" "${src_files}" INCLUDE_DIRS "." - REQUIRES ezlopi_factory_info trace freertos ezlopi_websocket_client json ezlopi_cloud_constants ezlopi_devices -) \ No newline at end of file + REQUIRES json trace freertos ezlopi_cloud_constants ezlopi_room ezlopi_nvs ) + \ No newline at end of file diff --git a/ezlopi-cloud/room/room.c b/ezlopi-cloud/room/room.c index 5a98d8a52..8e8fb4140 100644 --- a/ezlopi-cloud/room/room.c +++ b/ezlopi-cloud/room/room.c @@ -3,65 +3,184 @@ #include "room.h" #include "cJSON.h" -#include "ezlopi_cloud_methods_str.h" -#include "ezlopi_cloud_keywords.h" +#include "ezlopi_nvs.h" +#include "ezlopi_room.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cloud_keywords.h" +#include "ezlopi_cloud_methods_str.h" void room_list(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON *cjson_result_array = cJSON_AddArrayToObject(cj_response, ezlopi_result_str); - if (cjson_result_array) + + char *rooms_str = ezlopi_nvs_read_rooms(); + if (rooms_str) { - l_ezlopi_device_t *devices = ezlopi_device_get_head(); - while (NULL != devices) + cJSON_AddRawToObject(cj_response, ezlopi_result_str, rooms_str); + free(rooms_str); + } + else + { + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + } +} + +void room_get(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + cJSON *cj_result = cJSON_AddArrayToObject(cj_response, ezlopi_result_str); + if (cj_result) { - cJSON *cjson_room_info = cJSON_CreateObject(); - if (cjson_room_info) - { - cJSON_AddStringToObject(cjson_room_info, "_id", ""); - cJSON_AddStringToObject(cjson_room_info, "name", ""); + int idx = 0; + cJSON *cj_room_id = NULL; - if (!cJSON_AddItemToArray(cjson_result_array, cjson_room_info)) + while (NULL != (cj_room_id = cJSON_GetArrayItem(cj_params, idx++))) + { + uint32_t room_id = strtoul(cj_room_id->valuestring, NULL, 16); + s_ezlopi_room_t *l_room_node = ezlopi_room_get_room_head(); + while (l_room_node) { - cJSON_Delete(cjson_room_info); + if (l_room_node->_id == room_id) + { + cJSON *cj_room_obj = cJSON_CreateObject(); + if (cj_room_obj) + { + cJSON_AddItemToObject(cj_room_obj, ezlopi__id_str, cj_room_id); + cJSON_AddStringToObject(cj_room_obj, ezlopi_name_str, l_room_node->name); + + if (!cJSON_AddItemToArray(cj_result, cj_room_obj)) + { + cJSON_Delete(cj_room_obj); + } + } + + break; + } + + l_room_node = l_room_node->next; } } - - devices = devices->next; } } } -#if 0 // v2.x -void room_list(cJSON *cj_request, cJSON *cj_response) +void room_create(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON *cjson_result_array = cJSON_AddArrayToObject(cj_response, ezlopi_result_str); - if (cjson_result_array) + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + ezlopi_room_add_to_list(cj_params); + ezlopi_room_add_to_nvs(cj_params); + + cJSON_AddItemReferenceToObject(cj_response, ezlopi_result_str, cj_params); + } +} + +void room_name_set(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + + uint32_t room_id = 0; + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + ezlopi_room_name_set(cj_params); + } +} + +void room_delete(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) { - l_ezlopi_configured_devices_t *registered_device = ezlopi_devices_list_get_configured_items(); - while (NULL != registered_device) + ezlopi_room_delete(cj_params); + } +} + +void room_all_delete(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + + ezlopi_room_delete_all(); +} + +void room_order_set(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + cJSON *cj_rooms_ids = cJSON_GetObjectItem(cj_params, ezlopi_roomsId_str); + if (cj_rooms_ids) { - if (NULL != registered_device->properties) - { - cJSON *cjson_room_info = cJSON_CreateObject(); - if (cjson_room_info) - { - cJSON_AddStringToObject(cjson_room_info, "_id", ""); - cJSON_AddStringToObject(cjson_room_info, "name", registered_device->properties->ezlopi_cloud.room_name); + ezlopi_room_reorder(cj_rooms_ids); + } + } +} - if (!cJSON_AddItemToArray(cjson_result_array, cjson_room_info)) - { - cJSON_Delete(cjson_room_info); - } - } - } +////////////////// +void room_created(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_DeleteItemFromObject(cj_response, ezlopi_sender_str); + cJSON_DeleteItemFromObject(cj_response, ezlopi_error_str); + + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_room_created_str); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_result_str, cJSON_GetObjectItem(cj_request, ezlopi_params_str)); +} + +void room_edited(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_DeleteItemFromObject(cj_response, ezlopi_id_str); + + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_room_edited_str); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); +} - registered_device = registered_device->next; +void room_deleted(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_DeleteItemFromObject(cj_response, ezlopi_id_str); + + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_room_deleted_str); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + if (cj_result) + { + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + cJSON_AddItemReferenceToObject(cj_result, ezlopi__id_str, cJSON_GetObjectItem(cj_params, ezlopi__id_str)); } } } -#endif + +void room_reordered(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_DeleteItemFromObject(cj_response, ezlopi_id_str); + + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_room_reordered_str); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); +} \ No newline at end of file diff --git a/ezlopi-cloud/room/room.h b/ezlopi-cloud/room/room.h index 7e624947f..428117254 100644 --- a/ezlopi-cloud/room/room.h +++ b/ezlopi-cloud/room/room.h @@ -8,7 +8,18 @@ extern "C" { #endif + void room_get(cJSON *cj_request, cJSON *cj_response); void room_list(cJSON *cj_request, cJSON *cj_response); + void room_create(cJSON *cj_request, cJSON *cj_response); + void room_name_set(cJSON *cj_request, cJSON *cj_response); + void room_order_set(cJSON *cj_request, cJSON *cj_response); + void room_delete(cJSON *cj_request, cJSON *cj_response); + void room_all_delete(cJSON *cj_request, cJSON *cj_response); + + void room_created(cJSON *cj_request, cJSON *cj_response); + void room_edited(cJSON *cj_request, cJSON *cj_response); + void room_deleted(cJSON *cj_request, cJSON *cj_response); + void room_reordered(cJSON *cj_request, cJSON *cj_response); #ifdef __cplusplus } diff --git a/ezlopi-cloud/scenes/CMakeLists.txt b/ezlopi-cloud/scenes/CMakeLists.txt index fd476b376..3a160bbf1 100644 --- a/ezlopi-cloud/scenes/CMakeLists.txt +++ b/ezlopi-cloud/scenes/CMakeLists.txt @@ -1,4 +1,4 @@ file(GLOB_RECURSE src_files "*.c*") -idf_component_register(SRCS "scenes_scripts.c" "scenes.c" "${src_files}" +idf_component_register(SRCS "scenes_block_data_list.c" "scenes_block_list.c" "scenes.c" "${src_files}" INCLUDE_DIRS "." - REQUIRES ezlopi_factory_info trace ezlopi_factory_info trace json ezlopi_cloud_constants ezlopi_scenes) + REQUIRES ezlopi_factory_info trace json ezlopi_cloud_constants ezlopi_scenes) diff --git a/ezlopi-cloud/scenes/scenes.c b/ezlopi-cloud/scenes/scenes.c index 0093be615..8a9d98a36 100755 --- a/ezlopi-cloud/scenes/scenes.c +++ b/ezlopi-cloud/scenes/scenes.c @@ -1,24 +1,18 @@ #include #include -#include "scenes.h" #include "trace.h" #include "cJSON.h" +#include "scenes.h" + #include "ezlopi_nvs.h" -#include "ezlopi_scenes_v2.h" #include "ezlopi_devices.h" +#include "ezlopi_scenes_v2.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_meshbot_service.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_scenes_operators.h" -#include "ezlopi_meshbot_service.h" - -static void __value_types_list(char *list_name, cJSON *cj_result); -static void __scalable_value_types_list(char *list_name, cJSON *cj_result); -static void __value_scales_list(char *list_name, cJSON *cj_result); -static void __scenes_value_types_list(char *list_name, cJSON *cj_result); -static void __value_types_families_list(char *list_name, cJSON *cj_result); -static void __comparison_operators_list(char *list_name, cJSON *cj_result); -static void __comparison_methods_list(char *list_name, cJSON *cj_result); -static void __advanced_scenes_version_list(char *list_name, cJSON *cj_result); +#include "ezlopi_scenes_notifications.h" void scenes_list(cJSON *cj_request, cJSON *cj_response) { @@ -36,13 +30,19 @@ void scenes_create(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { uint32_t new_scene_id = ezlopi_store_new_scene_v2(cj_params); + TRACE_D("new-scene-id: %08x", new_scene_id); + if (new_scene_id) { + char tmp_buff[32]; + snprintf(tmp_buff, sizeof(tmp_buff), "%08x", new_scene_id); + cJSON_AddStringToObject(cj_request, ezlopi__id_str, tmp_buff); ezlopi_scenes_new_scene_populate(cj_params, new_scene_id); } } @@ -56,22 +56,13 @@ void scenes_get(cJSON *cj_request, cJSON *cj_response) cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { - cJSON *cj_ids = cJSON_GetObjectItem(cj_params, "_id"); + cJSON *cj_ids = cJSON_GetObjectItem(cj_params, ezlopi__id_str); if (cj_ids && cj_ids->valuestring) { char *scene_str = ezlopi_nvs_read_str(cj_ids->valuestring); - if (scene_str) { - cJSON *cj_scene = cJSON_Parse(scene_str); - if (cj_scene) - { - if (!cJSON_AddItemToObject(cj_response, "result", cj_scene)) - { - cJSON_Delete(cj_scene); - } - } - + cJSON_AddRawToObject(cj_response, ezlopi_result_str, scene_str); free(scene_str); } } @@ -80,46 +71,31 @@ void scenes_get(cJSON *cj_request, cJSON *cj_response) void scenes_edit(cJSON *cj_request, cJSON *cj_response) { + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { - cJSON *cj_id = cJSON_GetObjectItem(cj_params, "_id"); - if (cj_id && cj_id->valuestring) + cJSON *cj_eo = cJSON_GetObjectItem(cj_params, "eo"); + if (cj_eo) { - uint32_t u_id = strtoul(cj_id->valuestring, NULL, 16); - cJSON *cj_eo = cJSON_GetObjectItem(cj_params, "eo"); - if (cj_eo) + CJSON_TRACE("scene-edit eo", cj_eo); + + cJSON *cj_id = cJSON_GetObjectItem(cj_eo, ezlopi__id_str); + if (cj_id && cj_id->valuestring) { - // ezlopi_scenes_update_by_id(u_id, cj_eo); + if (cj_id && cj_id->valuestring) + { + uint32_t u_id = strtoul(cj_id->valuestring, NULL, 16); + // ezlopi_scenes_edit_by_id(u_id, cj_eo); + } } } } } -// void scenes_edit(cJSON *cj_request, cJSON *cj_response) -// { -// cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); -// cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); -// -// cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); -// if (cj_params) -// { -// cJSON *cj_id = cJSON_GetObjectItem(cj_params, "_id"); -// if (cj_id && cj_id->valuestring) -// { -// uint32_t u_id = strtoul(cj_id->valuestring, NULL, 16); -// cJSON *cj_eo = cJSON_GetObjectItem(cj_params, "eo"); -// if (cj_eo) -// { -// ezlopi_scenes_update_by_id(u_id, cj_eo); -// } -// } -// } -// } - void scenes_delete(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); @@ -129,11 +105,11 @@ void scenes_delete(cJSON *cj_request, cJSON *cj_response) cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { - cJSON *cj_id = cJSON_GetObjectItem(cj_params, "_id"); + cJSON *cj_id = cJSON_GetObjectItem(cj_params, ezlopi__id_str); if (cj_id && cj_id->valuestring) { uint32_t u_id = strtoul(cj_id->valuestring, NULL, 16); - ezlopi_nvs_delete_stored_script(u_id); + ezlopi_nvs_delete_stored_data_by_id(u_id); ezlopi_scenes_depopulate_by_id_v2(u_id); ezlopi_scenes_remove_id_from_list_v2(u_id); } @@ -144,19 +120,18 @@ void scenes_status_get(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON_AddObjectToObject(cj_response, ezlopi_result_str); cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { - cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, "sceneId"); + cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, ezlopi_sceneId_str); if (cj_scene_id && cj_scene_id->valuestring) { - uint32_t u_id = strtoul(cj_scene_id->valuestring, NULL, 16); - l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_by_id_v2(u_id); - if (scene_node) + char *scene_str = ezlopi_nvs_read_str(cj_scene_id->valuestring); + if (scene_str) { - // scene_node->status; + cJSON_AddRawToObject(cj_response, ezlopi_result_str, scene_str); + free(scene_str); } } } @@ -166,12 +141,12 @@ void scenes_run(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { - cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, "sceneId"); + cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, ezlopi_sceneId_str); if (cj_scene_id && cj_scene_id->valuestring) { uint32_t u32_scene_id = strtoul(cj_scene_id->valuestring, NULL, 16); @@ -180,399 +155,276 @@ void scenes_run(cJSON *cj_request, cJSON *cj_response) } } -void scenes_blocks_list(cJSON *cj_request, cJSON *cj_response) +void scenes_enable_set(cJSON *cj_request, cJSON *cj_response) { - cJSON_AddItemReferenceToObject(cj_response, ezlopi__id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - if (cj_result) + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) { - cJSON *cj_paramas = cJSON_GetObjectItem(cj_request, ezlopi_params_str); - if (cj_paramas) + cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, ezlopi__id_str); + if (cj_scene_id && cj_scene_id->valuestring) { - cJSON *cj_block_type = cJSON_GetObjectItem(cj_paramas, "blockType"); - if (cj_block_type && cj_block_type->valuestring) + uint32_t scene_id = strtoul(cj_scene_id->valuestring, NULL, 16); + if (scene_id) { - cJSON *cj_block_array = NULL; - e_scenes_block_type_v2_t block_type = SCENE_BLOCK_TYPE_NONE; - if (0 == strncmp("when", cj_block_type->valuestring, 4)) - { - cj_block_array = cJSON_AddArrayToObject(cj_result, "when"); - block_type = SCENE_BLOCK_TYPE_WHEN; - } - else if (0 == strncmp("then", cj_block_type->valuestring, 4)) - { - cj_block_array = cJSON_AddArrayToObject(cj_result, "then"); - block_type = SCENE_BLOCK_TYPE_THEN; - } + bool enabled_flag = false; + CJSON_GET_VALUE_BOOL(cj_params, ezlopi_enabled_str, enabled_flag); + char *scene_str = ezlopi_nvs_read_str(cj_scene_id->valuestring); - if (cj_block_array && block_type) + if (scene_str) { - cJSON *cj_devices_array = cJSON_GetObjectItem(cj_paramas, "devices"); - if (cj_devices_array && (cJSON_Array == cj_devices_array->type)) + cJSON *cj_scene = cJSON_Parse(scene_str); + free(scene_str); + + if (cj_scene) { - int device_id_idx = 0; - cJSON *cj_device_id = NULL; - while (NULL != (cj_device_id = cJSON_GetArrayItem(cj_devices_array, device_id_idx++))) - { - uint32_t device_id = strtoul(cj_device_id->valuestring, NULL, 16); - l_scenes_list_v2_t *req_scene = ezlopi_scenes_get_by_id_v2(device_id); + cJSON_DeleteItemFromObject(cj_scene, ezlopi_enabled_str); + cJSON_AddBoolToObject(cj_scene, ezlopi_enabled_str, enabled_flag); - if (req_scene) - { - switch (block_type) - { - case SCENE_BLOCK_TYPE_WHEN: - { - l_when_block_v2_t *curr_when_block = req_scene->when_block; - while (curr_when_block) - { - cJSON *cj_when_block = NULL; - // ezlopi_scenes_cjson_create_when_block(req_scene->when_block); - if (cj_when_block) - { - if (!cJSON_AddItemToArray(cj_block_array, cj_when_block)) - { - cJSON_Delete(cj_when_block); - } - } - curr_when_block = curr_when_block->next; - } + char *updated_scene_str = cJSON_Print(cj_scene); + cJSON_Delete(cj_scene); - break; - } -#warning "Check if else block is required or not" - case SCENE_BLOCK_TYPE_THEN: - { - l_action_block_v2_t *curr_then_block = req_scene->then_block; - while (curr_then_block) - { - cJSON *cj_then_block = NULL; - // ezlopi_scenes_cjson_create_then_block(req_scene->then_block); - if (cj_then_block) - { - if (!cJSON_AddItemToArray(cj_block_array, cj_then_block)) - { - cJSON_Delete(cj_then_block); - } - } - curr_then_block = curr_then_block->next; - } - break; - } - default: - { - break; - } - } - } + if (updated_scene_str) + { + TRACE_D("updated-scene: %s", updated_scene_str); + cJSON_Minify(updated_scene_str); + ezlopi_nvs_write_str(updated_scene_str, strlen(updated_scene_str), cj_scene_id->valuestring); + + free(updated_scene_str); } } } + + l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_by_id_v2(scene_id); + if (scene_node) + { + scene_node->enabled = enabled_flag; + if (false == scene_node->enabled) + { + ezlopi_meshobot_service_stop_scene(scene_node); + } + else if (true == scene_node->enabled) + { + ezlopi_meshbot_service_start_scene(scene_node); + } + } } } } + + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); } -void scenes_block_data_list(cJSON *cj_request, cJSON *cj_response) +void scenes_notification_add(cJSON *cj_request, cJSON *cj_response) { - typedef struct s_block_data_list_collector - { - char *key_string; - void (*func)(char *list_name, cJSON *result); - } s_block_data_list_collector_t; - - static const s_block_data_list_collector_t block_data_list_collector[] = { - {.key_string = "valueTypes", .func = __value_types_list}, - {.key_string = "scalableValueTypes", .func = __scalable_value_types_list}, - {.key_string = "valueScales", .func = __value_scales_list}, - {.key_string = "scenesValueTypes", .func = __scenes_value_types_list}, - {.key_string = "valueTypeFamilies", .func = __value_types_families_list}, - {.key_string = "comparisonOperators", .func = __comparison_operators_list}, - {.key_string = "comparisonMethods", .func = __comparison_methods_list}, - {.key_string = "advancedScenesVersion", .func = __advanced_scenes_version_list}, - {.key_string = NULL, .func = NULL}, - }; - - if (cj_request && cj_response) + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) { - cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); - cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON *cj_user_id = cJSON_GetObjectItem(cj_params, ezlopi_userId_str); + cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, ezlopi_notifications_str); - cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); - if (cj_params) + if (cj_scene_id && cj_scene_id->valuestring && cj_user_id && cj_user_id->valuestring) { - cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - if (cj_result) + char *scene_str = ezlopi_nvs_read_str(cj_scene_id->valuestring); + if (scene_str) { - cJSON *temp = cj_params->child; + cJSON *cj_scene = cJSON_Parse(scene_str); + free(scene_str); - while (temp != NULL) + if (cj_scene) { - uint32_t idx = 0; - while (block_data_list_collector[idx].func) + cJSON *cj_user_notifications = cJSON_GetObjectItem(cj_scene, ezlopi_user_notifications_str); + if (cj_user_notifications) { - if (0 == strcmp(block_data_list_collector[idx].key_string, temp->string)) - { - block_data_list_collector[idx].func(block_data_list_collector[idx].key_string, cj_result); - } - idx++; + cJSON_AddItemReferenceToArray(cj_user_notifications, cj_user_id); } - temp = temp->next; + CJSON_TRACE("updated-scene", cj_scene); + char *updated_scene_str = cJSON_Print(cj_scene); + cJSON_Delete(cj_scene); + + if (updated_scene_str) + { + ezlopi_nvs_write_str(updated_scene_str, strlen(updated_scene_str), cj_scene_id->valuestring); + free(updated_scene_str); + } } } - } - } -} -static void __value_types_list(char *list_name, cJSON *cj_result) -{ - if (cj_result && list_name) - { - cJSON *cj_value_types = cJSON_AddObjectToObject(cj_result, list_name); - if (cj_value_types) - { - cJSON *cj_value_array = cJSON_AddArrayToObject(cj_value_types, "list"); - if (cj_value_array) + uint32_t scene_id = strtoul(cj_scene_id->valuestring, NULL, 16); + + if (scene_id) { - l_ezlopi_device_t *devices = ezlopi_device_get_head(); - while (devices) + l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_scenes_head_v2(); + while (scene_node) { - l_ezlopi_item_t *items = devices->items; - while (items) + if (scene_id == scene_node->_id) { - if (NULL == items->cloud_properties.scale) - { - cJSON *cj_item_value_type = cJSON_CreateString(items->cloud_properties.value_type); - if (cj_item_value_type) - { - if (!cJSON_AddItemToArray(cj_value_array, cj_item_value_type)) - { - cJSON_Delete(cj_item_value_type); - } - } - } - items = items->next; + ezlopi_scene_add_users_in_notifications(scene_node, cj_user_id); + break; } - devices = devices->next; + scene_node = scene_node->next; } } } } } -static void __scalable_value_types_list(char *list_name, cJSON *cj_result) +void scenes_notification_remove(cJSON *cj_request, cJSON *cj_response) { - if (cj_result && list_name) + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) { - cJSON *cj_value_types = cJSON_AddObjectToObject(cj_result, list_name); - if (cj_value_types) + cJSON *cj_user_id_del = cJSON_GetObjectItem(cj_params, ezlopi_userId_str); + cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, ezlopi_notifications_str); + + if (cj_scene_id && cj_scene_id->valuestring && cj_user_id_del && cj_user_id_del->valuestring) { - cJSON *cj_value_array = cJSON_AddArrayToObject(cj_value_types, "list"); - if (cj_value_array) + char *scene_str = ezlopi_nvs_read_str(cj_scene_id->valuestring); + if (scene_str) { - l_ezlopi_device_t *devices = ezlopi_device_get_head(); - while (devices) + cJSON *cj_scene = cJSON_Parse(scene_str); + free(scene_str); + + if (cj_scene) { - l_ezlopi_item_t *items = devices->items; - while (items) + cJSON *cj_user_notifications = cJSON_GetObjectItem(cj_scene, ezlopi_user_notifications_str); + if (cj_user_notifications && cj_user_id_del) { - if (items->cloud_properties.scale) + uint32_t idx = 0; + cJSON *cj_user_id = NULL; + while (NULL != (cj_user_id = cJSON_GetArrayItem(cj_user_notifications, idx))) { - cJSON *cj_item_value_type = cJSON_CreateString(items->cloud_properties.value_type); - if (cj_item_value_type) + if (0 == strcmp(cj_user_id->valuestring, cj_user_id_del->valuestring)) { - if (!cJSON_AddItemToArray(cj_value_array, cj_item_value_type)) - { - cJSON_Delete(cj_item_value_type); - } + cJSON_DeleteItemFromArray(cj_user_notifications, idx); } + idx++; } - items = items->next; } - devices = devices->next; + + CJSON_TRACE("updated-scene", cj_scene); + char *updated_scene_str = cJSON_Print(cj_scene); + cJSON_Delete(cj_scene); + + if (updated_scene_str) + { + ezlopi_nvs_write_str(updated_scene_str, strlen(updated_scene_str), cj_scene_id->valuestring); + free(updated_scene_str); + } } } - } - } -} -static void __value_scales_list(char *list_name, cJSON *cj_result) -{ - if (cj_result && list_name) - { - cJSON *cj_value_scales = cJSON_AddObjectToObject(cj_result, list_name); - if (cj_value_scales) - { - l_ezlopi_device_t *devices = ezlopi_device_get_head(); - while (devices) + uint32_t scene_id = strtoul(cj_scene_id->valuestring, NULL, 16); + if (scene_id) { - l_ezlopi_item_t *items = devices->items; - while (items) + l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_scenes_head_v2(); + while (scene_node) { - if (items->cloud_properties.scale) + if (scene_node->_id == scene_id) { - cJSON *cj_scale_array = NULL; - cJSON *cj_value_type = cJSON_GetObjectItem(cj_value_scales, items->cloud_properties.value_type); - if (NULL == cj_value_type) + if (0 == strcmp(scene_node->user_notifications->user_id, cj_user_id_del->valuestring)) { - cj_value_type = cJSON_AddObjectToObject(cj_value_scales, items->cloud_properties.value_type); - if (cj_value_type) - { - cj_scale_array = cJSON_AddArrayToObject(cj_value_type, "scales"); - cJSON_AddFalseToObject(cj_value_type, "converter"); - } + l_user_notification_v2_t *user_id_del = scene_node->user_notifications; + scene_node->user_notifications = scene_node->user_notifications->next; + user_id_del->next = NULL; + ezlopi_scenes_delete_user_notifications(user_id_del); } else { - cj_scale_array = cJSON_GetObjectItem(cj_value_type, "scales"); - } - - if (cj_scale_array) - { - cJSON *cj_scale = cJSON_CreateString(items->cloud_properties.scale); - if (cj_scale) + l_user_notification_v2_t *user_node = scene_node->user_notifications; + while (user_node->next) { - if (!cJSON_AddItemToArray(cj_scale_array, cj_scale)) + if (0 == strcmp(user_node->user_id, cj_user_id_del->valuestring)) { - cJSON_Delete(cj_scale); + l_user_notification_v2_t *user_id_del = user_node; + user_node = user_node->next; + user_id_del->next = NULL; + ezlopi_scenes_delete_user_notifications(user_id_del); + break; } + + user_node = user_node->next; } } + + break; } - items = items->next; + scene_node = scene_node->next; } - devices = devices->next; } } } } -static void __scenes_value_types_list(char *list_name, cJSON *cj_result) +/////////// updater for scene +////// useful for 'hub.scenes.enabled.set' +void scene_changed(cJSON *cj_request, cJSON *cj_response) { - if (cj_result && list_name) + cJSON_DeleteItemFromObject(cj_response, ezlopi_sender_str); + cJSON_DeleteItemFromObject(cj_response, ezlopi_error_str); + + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_scene_changed_str); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_changed_by_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) { - cJSON *cj_scenes_value_types = cJSON_AddObjectToObject(cj_result, list_name); - if (cj_scenes_value_types) + cJSON *cj_scene_id = cJSON_GetObjectItem(cj_params, ezlopi__id_str); + if (cj_scene_id && cj_scene_id->valuestring) { - cJSON *cj_value_array = cJSON_AddArrayToObject(cj_scenes_value_types, "list"); - if (cj_value_array) + char *scene_str = ezlopi_nvs_read_str(cj_scene_id->valuestring); + if (scene_str) { - uint32_t idx = EZLOPI_VALUE_TYPE_NONE + 1; - while (ezlopi_scene_get_scene_value_type_name_v2(idx)) - { - cJSON *cj_string_val = cJSON_CreateString(ezlopi_scene_get_scene_value_type_name_v2(idx)); - if (cj_string_val) - { - if (!cJSON_AddItemToArray(cj_value_array, cj_string_val)) - { - cJSON_Delete(cj_string_val); - } - } - idx++; - } + cJSON_AddRawToObject(cj_response, ezlopi_result_str, scene_str); } } } } -static void __value_types_families_list(char *list_name, cJSON *cj_result) +void scene_added(cJSON *cj_request, cJSON *cj_response) { - if (cj_result && list_name) - { - cJSON *cj_value_type_famiies = cJSON_AddObjectToObject(cj_result, list_name); - if (cj_value_type_famiies) - { - const static char *numeric = "[\"int\",\"float\",\"scalableValueTypes\"]"; - const static char *strings = "[\"string\",\"token\"]"; - cJSON_AddRawToObject(cj_value_type_famiies, "numeric", numeric); - cJSON_AddRawToObject(cj_value_type_famiies, "strings", strings); - } - } -} + cJSON_DeleteItemFromObject(cj_response, ezlopi_sender_str); + cJSON_DeleteItemFromObject(cj_response, ezlopi_error_str); -static void __comparison_operators_list(char *list_name, cJSON *cj_result) -{ - if (cj_result) + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_scene_added_str); + + cJSON *new_scene_id = cJSON_GetObjectItem(cj_request, ezlopi__id_str); + if (new_scene_id && new_scene_id->valuestring) { - cJSON *cj_value_types = cJSON_AddObjectToObject(cj_result, list_name); - if (cj_value_types) + char *new_scene = ezlopi_nvs_read_str(new_scene_id->valuestring); + if (new_scene) { - cJSON *cj_families_array = cJSON_AddArrayToObject(cj_value_types, "families"); - if (cj_families_array) - { - cJSON *cj_family = cJSON_CreateObject(); - if (cj_family) - { - cJSON_AddStringToObject(cj_family, "family", "numeric"); - cJSON *cj_methods_array = cJSON_AddArrayToObject(cj_family, "methods"); - if (cj_methods_array) - { - e_scene_cmp_operators_t op_idx = SCENES_OPERATORS_LESS; - while (ezlopi_scenes_operators_get_op(op_idx)) - { - cJSON *cj_method = cJSON_CreateObject(); - if (cj_method) - { - cJSON_AddStringToObject(cj_method, "op", ezlopi_scenes_operators_get_op(op_idx)); - cJSON_AddStringToObject(cj_method, "name", ezlopi_scenes_operators_get_name(op_idx)); - cJSON_AddStringToObject(cj_method, "method", ezlopi_scenes_operators_get_method(op_idx)); - - if (!cJSON_AddItemToArray(cj_methods_array, cj_method)) - { - cJSON_Delete(cj_method); - } - } - - op_idx++; - } - } - } - - if (!cJSON_AddItemToArray(cj_families_array, cj_family)) - { - cJSON_Delete(cj_family); - } - } + cJSON_AddRawToObject(cj_response, ezlopi_result_str, new_scene); + free(new_scene); } } } -static void __comparison_methods_list(char *list_name, cJSON *cj_result) +void scene_deleted(cJSON *cj_request, cJSON *cj_response) { - if (cj_result) - { - cJSON *cj_comparision_methods = cJSON_AddObjectToObject(cj_result, list_name); - if (cj_comparision_methods) - { - const static char *info = "{\"version\":\"1.0.0\"}"; - cJSON_AddRawToObject(cj_comparision_methods, "info", info); - cJSON *cj_compare_numbers = cJSON_AddObjectToObject(cj_comparision_methods, "compareNumbers"); - if (cj_compare_numbers) - { - const static char *comparator = "{\"family\":\"numeric\",\"field\":\"comparator\",\"options\":[\"<\",\">\",\"<=\",\">=\",\"==\",\"!=\"],\"type\":\"enum\"}"; - const static char *data_source = "[{\"index\":0,\"types\":[\"item\",\"expression\"]},{\"index\":1,\"types\":[\"constant\",\"expression\"]}]"; - cJSON_AddRawToObject(cj_compare_numbers, "comparator", comparator); - cJSON_AddRawToObject(cj_compare_numbers, "dataSource", data_source); - } + cJSON_DeleteItemFromObject(cj_response, ezlopi_sender_str); + cJSON_DeleteItemFromObject(cj_response, ezlopi_error_str); - cJSON *cj_compare_strings = cJSON_AddObjectToObject(cj_comparision_methods, "compareStrings"); - if (cj_compare_strings) - { - const static char *comparator = "{\"family\":\"strings\",\"field\":\"comparator\",\"type\":\"enum\",\"options\":[\"<\",\">\",\"<=\",\">=\",\"==\",\"!=\"]}"; - const static char *data_source = "[{\"index\":0,\"types\":[\"item\",\"expression\"]},{\"index\":1,\"types\":[\"constant\",\"expression\"]}]"; - cJSON_AddRawToObject(cj_compare_strings, "dataSource", data_source); - cJSON_AddRawToObject(cj_compare_strings, "comparator", comparator); - } - } - } -} + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_scene_deleted_str); -static void __advanced_scenes_version_list(char *list_name, cJSON *cj_result) -{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi_result_str, cJSON_GetObjectItem(cj_request, ezlopi_params_str)); + cJSON *cj_result = cJSON_GetObjectItem(cj_response, ezlopi_result_str); if (cj_result) { + cJSON_AddBoolToObject(cj_result, ezlopi_syncNotification_str, true); } } diff --git a/ezlopi-cloud/scenes/scenes.h b/ezlopi-cloud/scenes/scenes.h index dc4197b47..9fe8f9d58 100755 --- a/ezlopi-cloud/scenes/scenes.h +++ b/ezlopi-cloud/scenes/scenes.h @@ -12,12 +12,20 @@ extern "C" void scenes_list(cJSON *cj_request, cJSON *cj_response); void scenes_create(cJSON *cj_request, cJSON *cj_response); void scenes_get(cJSON *cj_request, cJSON *cj_response); - // void scenes_edit(cJSON *cj_request, cJSON *cj_response); + void scenes_edit(cJSON *cj_request, cJSON *cj_response); void scenes_delete(cJSON *cj_request, cJSON *cj_response); // void scenes_status_get(cJSON *cj_request, cJSON *cj_response); void scenes_blocks_list(cJSON *cj_request, cJSON *cj_response); void scenes_block_data_list(cJSON *cj_request, cJSON *cj_response); void scenes_run(cJSON *cj_request, cJSON *cj_response); + void scenes_enable_set(cJSON *cj_request, cJSON *cj_response); + void scenes_notification_add(cJSON *cj_request, cJSON *cj_response); + void scenes_notification_remove(cJSON *cj_request, cJSON *cj_response); + + ///////////// updaters + void scene_changed(cJSON *cj_request, cJSON *cj_response); + void scene_added(cJSON *cj_request, cJSON *cj_response); + void scene_deleted(cJSON *cj_request, cJSON *cj_response); #ifdef __cplusplus } diff --git a/ezlopi-cloud/scenes/scenes_block_data_list.c b/ezlopi-cloud/scenes/scenes_block_data_list.c new file mode 100644 index 000000000..299beb4c2 --- /dev/null +++ b/ezlopi-cloud/scenes/scenes_block_data_list.c @@ -0,0 +1,1407 @@ +#include +#include + +#include "scenes.h" +#include "trace.h" +#include "cJSON.h" +#include "ezlopi_nvs.h" +#include "ezlopi_scenes_v2.h" +#include "ezlopi_devices.h" +#include "ezlopi_cloud_constants.h" +#include "ezlopi_scenes_operators.h" +#include "ezlopi_meshbot_service.h" + +typedef struct s_data_source_n_target_object +{ + char *types; + char *field; +} s_data_source_n_target_object_t; + +static void __value_types_list(char *list_name, cJSON *cj_result); +static void __scalable_value_types_list(char *list_name, cJSON *cj_result); +static void __value_scales_list(char *list_name, cJSON *cj_result); +static void __scenes_value_types_list(char *list_name, cJSON *cj_result); +static void __value_types_families_list(char *list_name, cJSON *cj_result); +static void __comparison_operators_list(char *list_name, cJSON *cj_result); +static void __comparison_methods_list(char *list_name, cJSON *cj_result); +static void __action_methods_list(char *list_name, cJSON *cj_result); +static void __advanced_scenes_version_list(char *list_name, cJSON *cj_result); + +static void __add_data_src_dest_array_to_object(cJSON *cj_method, char *array_name, const s_data_source_n_target_object_t *data_list); + +void scenes_block_data_list(cJSON *cj_request, cJSON *cj_response) +{ + typedef struct s_block_data_list_collector + { + char *key_string; + void (*func)(char *list_name, cJSON *result); + } s_block_data_list_collector_t; + + static const s_block_data_list_collector_t block_data_list_collector[] = { + {.key_string = "valueTypes", .func = __value_types_list}, + {.key_string = "scalableValueTypes", .func = __scalable_value_types_list}, + {.key_string = "valueScales", .func = __value_scales_list}, + {.key_string = "scenesValueTypes", .func = __scenes_value_types_list}, + {.key_string = "valueTypeFamilies", .func = __value_types_families_list}, + {.key_string = "comparisonOperators", .func = __comparison_operators_list}, + {.key_string = "comparisonMethods", .func = __comparison_methods_list}, + {.key_string = "actionMethods", .func = __action_methods_list}, + {.key_string = "advancedScenesVersion", .func = __advanced_scenes_version_list}, + {.key_string = NULL, .func = NULL}, + }; + + if (cj_request && cj_response) + { + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + if (cj_result) + { + uint32_t idx = 0; + while (block_data_list_collector[idx].func) + { + cJSON *cj_value = cJSON_GetObjectItem(cj_params, block_data_list_collector[idx].key_string); + if (cj_value && (cj_value->type == cJSON_True)) + { + block_data_list_collector[idx].func(block_data_list_collector[idx].key_string, cj_result); + } + + idx++; + } + } + } + } +} + +static void __value_types_list(char *list_name, cJSON *cj_result) +{ + if (cj_result && list_name) + { + cJSON *cj_value_types = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_value_types) + { + cJSON *cj_value_array = cJSON_AddArrayToObject(cj_value_types, "list"); + if (cj_value_array) + { + l_ezlopi_device_t *devices = ezlopi_device_get_head(); + while (devices) + { + l_ezlopi_item_t *items = devices->items; + while (items) + { + if (NULL == items->cloud_properties.scale) + { + cJSON *cj_item_value_type = cJSON_CreateString(items->cloud_properties.value_type); + if (cj_item_value_type) + { + if (!cJSON_AddItemToArray(cj_value_array, cj_item_value_type)) + { + cJSON_Delete(cj_item_value_type); + } + } + } + items = items->next; + } + devices = devices->next; + } + } + } + } +} + +static void __scalable_value_types_list(char *list_name, cJSON *cj_result) +{ + if (cj_result && list_name) + { + cJSON *cj_value_types = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_value_types) + { + cJSON *cj_value_array = cJSON_AddArrayToObject(cj_value_types, "list"); + if (cj_value_array) + { + l_ezlopi_device_t *devices = ezlopi_device_get_head(); + while (devices) + { + l_ezlopi_item_t *items = devices->items; + while (items) + { + if (items->cloud_properties.scale) + { + cJSON *cj_item_value_type = cJSON_CreateString(items->cloud_properties.value_type); + if (cj_item_value_type) + { + if (!cJSON_AddItemToArray(cj_value_array, cj_item_value_type)) + { + cJSON_Delete(cj_item_value_type); + } + } + } + items = items->next; + } + devices = devices->next; + } + } + } + } +} + +static void __value_scales_list(char *list_name, cJSON *cj_result) +{ + if (cj_result && list_name) + { + cJSON *cj_value_scales = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_value_scales) + { + l_ezlopi_device_t *devices = ezlopi_device_get_head(); + while (devices) + { + l_ezlopi_item_t *items = devices->items; + while (items) + { + if (items->cloud_properties.scale) + { + cJSON *cj_scale_array = NULL; + cJSON *cj_value_type = cJSON_GetObjectItem(cj_value_scales, items->cloud_properties.value_type); + if (NULL == cj_value_type) + { + cj_value_type = cJSON_AddObjectToObject(cj_value_scales, items->cloud_properties.value_type); + if (cj_value_type) + { + cj_scale_array = cJSON_AddArrayToObject(cj_value_type, "scales"); + cJSON_AddFalseToObject(cj_value_type, "converter"); + } + } + else + { + cj_scale_array = cJSON_GetObjectItem(cj_value_type, "scales"); + } + + if (cj_scale_array) + { + cJSON *cj_scale = cJSON_CreateString(items->cloud_properties.scale); + if (cj_scale) + { + if (!cJSON_AddItemToArray(cj_scale_array, cj_scale)) + { + cJSON_Delete(cj_scale); + } + } + } + } + items = items->next; + } + devices = devices->next; + } + } + } +} + +static void __scenes_value_types_list(char *list_name, cJSON *cj_result) +{ + if (cj_result && list_name) + { + cJSON *cj_scenes_value_types = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_scenes_value_types) + { + cJSON *cj_value_array = cJSON_AddArrayToObject(cj_scenes_value_types, "list"); + if (cj_value_array) + { + uint32_t idx = EZLOPI_VALUE_TYPE_NONE + 1; + while (idx < EZLOPI_VALUE_TYPE_MAX) + { + const char *type_name_str = ezlopi_scene_get_scene_value_type_name_v2(idx); + if (type_name_str) + { + cJSON *cj_string_val = cJSON_CreateString(type_name_str); + if (cj_string_val) + { + if (!cJSON_AddItemToArray(cj_value_array, cj_string_val)) + { + cJSON_Delete(cj_string_val); + } + } + } + + idx++; + } + } + } + } +} + +static void __value_types_families_list(char *list_name, cJSON *cj_result) +{ + if (cj_result && list_name) + { + cJSON *cj_value_type_famiies = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_value_type_famiies) + { + const static char *numeric = "[\"int\",\"float\",\"scalableValueTypes\"]"; + const static char *strings = "[\"string\",\"token\"]"; + const static char *value_with_less = "[\"int\",\"float\",\"scalableValueTypes\",\"string\"]"; + const static char *value_without_less = "[]"; // remained to fill + + cJSON_AddRawToObject(cj_value_type_famiies, "numeric", numeric); + cJSON_AddRawToObject(cj_value_type_famiies, "strings", strings); + cJSON_AddRawToObject(cj_value_type_famiies, "valuesWithLess", value_with_less); + cJSON_AddRawToObject(cj_value_type_famiies, "valuesWithoutLess", value_without_less); + } + } +} + +static cJSON *__comparision_operators_numeric(void) +{ + cJSON *cj_family = cJSON_CreateObject(); + if (cj_family) + { + cJSON_AddStringToObject(cj_family, "family", "numeric"); + cJSON *cj_methods_array = cJSON_AddArrayToObject(cj_family, "methods"); + if (cj_methods_array) + { + e_scene_num_cmp_operators_t op_idx = SCENES_NUM_COMP_OPERATORS_NONE + 1; + while (ezlopi_scenes_numeric_comparator_operators_get_op(op_idx)) + { + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) + { + cJSON_AddStringToObject(cj_method, "op", ezlopi_scenes_numeric_comparator_operators_get_op(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_name_str, ezlopi_scenes_numeric_comparator_operators_get_name(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_key_method_str, ezlopi_scenes_numeric_comparator_operators_get_method(op_idx)); + + if (!cJSON_AddItemToArray(cj_methods_array, cj_method)) + { + cJSON_Delete(cj_method); + cj_method = NULL; + } + } + + op_idx++; + } + } + } + + return cj_family; +} + +static cJSON *__comparision_operators_strings(void) +{ + cJSON *cj_family = cJSON_CreateObject(); + if (cj_family) + { + cJSON_AddStringToObject(cj_family, "family", "strings"); + cJSON *cj_methods_array = cJSON_AddArrayToObject(cj_family, "methods"); + if (cj_methods_array) + { + e_scene_str_cmp_operators_t op_idx = SCENES_STRINGS_OPERATORS_NONE + 1; + while (ezlopi_scenes_strings_comparator_operators_get_op(op_idx)) + { + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) + { + cJSON_AddStringToObject(cj_method, "op", ezlopi_scenes_strings_comparator_operators_get_op(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_name_str, ezlopi_scenes_strings_comparator_operators_get_name(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_key_method_str, ezlopi_scenes_strings_comparator_operators_get_method(op_idx)); + + if (!cJSON_AddItemToArray(cj_methods_array, cj_method)) + { + cJSON_Delete(cj_method); + cj_method = NULL; + } + } + + op_idx++; + } + } + } + + return cj_family; +} + +static cJSON *__comparision_operators_values_with_less(void) +{ + cJSON *cj_family = cJSON_CreateObject(); + if (cj_family) + { + cJSON_AddStringToObject(cj_family, "family", "valuesWithLess"); + cJSON *cj_methods_array = cJSON_AddArrayToObject(cj_family, "methods"); + if (cj_methods_array) + { + e_scene_value_with_less_cmp_operators_t op_idx = SCENES_VALUES_WITH_LESS_OPERATORS_NONE + 1; + while (ezlopi_scenes_value_with_less_comparator_operators_get_op(op_idx)) + { + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) + { + cJSON_AddStringToObject(cj_method, "op", ezlopi_scenes_value_with_less_comparator_operators_get_op(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_name_str, ezlopi_scenes_value_with_less_comparator_operators_get_name(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_key_method_str, ezlopi_scenes_value_with_less_comparator_operators_get_method(op_idx)); + + if (!cJSON_AddItemToArray(cj_methods_array, cj_method)) + { + cJSON_Delete(cj_method); + cj_method = NULL; + } + } + + op_idx++; + } + } + } + + return cj_family; +} + +static cJSON *__comparision_operators_values_without_less(void) +{ + cJSON *cj_family = cJSON_CreateObject(); + if (cj_family) + { + cJSON_AddStringToObject(cj_family, "family", "valuesWithoutLess"); + cJSON *cj_methods_array = cJSON_AddArrayToObject(cj_family, "methods"); + if (cj_methods_array) + { + e_scene_value_without_less_cmp_operators_t op_idx = SCENES_VALUES_WITHOUT_LESS_OPERATORS_NONE + 1; + while (ezlopi_scenes_value_without_less_comparator_operators_get_op(op_idx)) + { + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) + { + cJSON_AddStringToObject(cj_method, "op", ezlopi_scenes_value_without_less_comparator_operators_get_op(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_name_str, ezlopi_scenes_value_without_less_comparator_operators_get_name(op_idx)); + cJSON_AddStringToObject(cj_method, ezlopi_key_method_str, ezlopi_scenes_value_without_less_comparator_operators_get_method(op_idx)); + + if (!cJSON_AddItemToArray(cj_methods_array, cj_method)) + { + cJSON_Delete(cj_method); + cj_method = NULL; + } + } + + op_idx++; + } + } + } + + return cj_family; +} + +static void __comparison_operators_list(char *list_name, cJSON *cj_result) +{ + if (cj_result) + { + cJSON *cj_value_types = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_value_types) + { + cJSON *cj_families_array = cJSON_AddArrayToObject(cj_value_types, "families"); + if (cj_families_array) + { + static cJSON *(*com_operators_funcs[])(void) = { + __comparision_operators_numeric, + __comparision_operators_strings, + __comparision_operators_values_with_less, + __comparision_operators_values_without_less, + NULL, + }; + + uint32_t family_idx = 0; + while (com_operators_funcs[family_idx]) + { + cJSON *cj_family = com_operators_funcs[family_idx](); + if (cj_family) + { + if (!cJSON_AddItemToArray(cj_families_array, cj_family)) + { + cJSON_Delete(cj_family); + } + } + + family_idx++; + } + } + } + } +} + +static cJSON *__comparision_method_info(void) +{ + cJSON *cj_info = cJSON_CreateObject(); + if (cj_info) + { + cJSON_AddStringToObject(cj_info, ezlopi_version_str, "1.0.0"); + } + + return cj_info; +} + +static cJSON *__comparision_method_compare_number_range(void) +{ + cJSON *cj_compare_number_range = cJSON_CreateObject(); + if (cj_compare_number_range) + { + cJSON *cj_comparator = cJSON_AddObjectToObject(cj_compare_number_range, "comparator"); + if (cj_comparator) + { + cJSON_AddStringToObject(cj_comparator, "family", "numeric"); + cJSON_AddStringToObject(cj_comparator, "field", "comparator"); + cJSON_AddStringToObject(cj_comparator, ezlopi_type_str, ezlopi_enum_str); + + const char *options_str = "[\"between\", \"not_between\"]"; + cJSON_AddRawToObject(cj_comparator, "options", options_str); + } + + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"item\",\"expression\",\"device_group\",\"item_group\"]", .field = NULL}, + {.types = "[\"constant\"]", .field = "startValue"}, + {.types = "[\"constant\"]", .field = "endValue"}, + {.types = NULL, .field = NULL}, + }; + + __add_data_src_dest_array_to_object(cj_compare_number_range, "dataSource", data_src_obj); + } + + return cj_compare_number_range; +} + +static cJSON *__comparision_method_compare_numbers(void) +{ + cJSON *cj_compare_numbers = cJSON_CreateObject(); + if (cj_compare_numbers) + { + + cJSON *cj_comparator = cJSON_AddObjectToObject(cj_compare_numbers, "comparator"); + if (cj_comparator) + { + cJSON_AddStringToObject(cj_comparator, "family", "numeric"); + cJSON_AddStringToObject(cj_comparator, "field", "comparator"); + cJSON_AddStringToObject(cj_comparator, ezlopi_type_str, ezlopi_enum_str); + + const static char *options_str = "[\"<\",\">\",\"<=\",\">=\",\"==\",\"!=\"]"; + cJSON_AddRawToObject(cj_comparator, "options", options_str); + } + + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"item\",\"expression\",\"device_group\",\"item_group\"]", .field = NULL}, + {.types = "[\"constant\",\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_compare_numbers, "dataSource", data_src_obj); + } + + return cj_compare_numbers; +} + +static cJSON *__comparision_method_compare_strings(void) +{ + cJSON *cj_compare_strings = cJSON_CreateObject(); + if (cj_compare_strings) + { + cJSON *cj_comparator = cJSON_AddObjectToObject(cj_compare_strings, "comparator"); + if (cj_comparator) + { + cJSON_AddStringToObject(cj_comparator, "family", "strings"); + cJSON_AddStringToObject(cj_comparator, "field", "comparator"); + cJSON_AddStringToObject(cj_comparator, ezlopi_type_str, ezlopi_enum_str); + + const static char *options_str = "[\"<\",\">\",\"<=\",\">=\",\"==\",\"!=\"]"; + cJSON_AddRawToObject(cj_comparator, "options", options_str); + } + + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"item\",\"expression\",\"device_group\",\"item_group\"]", .field = NULL}, + {.types = "[\"constant\",\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_compare_strings, "dataSource", data_src_obj); + } + + return cj_compare_strings; +} + +static cJSON *__comparision_method_compare_values(void) +{ + cJSON *cj_compare_values = cJSON_CreateObject(); + if (cj_compare_values) + { + cJSON *cj_comparator = cJSON_AddObjectToObject(cj_compare_values, "comparator"); + if (cj_comparator) + { + cJSON_AddStringToObject(cj_comparator, "family", "valuesWithoutLess"); + cJSON_AddStringToObject(cj_comparator, "field", "comparator"); + cJSON_AddStringToObject(cj_comparator, ezlopi_type_str, ezlopi_enum_str); + + const static char *options_str = "[\"==\",\"!=\"]"; + cJSON_AddRawToObject(cj_comparator, "options", options_str); + } + + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"item\",\"expression\",\"device_group\",\"item_group\"]", .field = NULL}, + {.types = "[\"constant\",\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_compare_values, "dataSource", data_src_obj); + } + + return cj_compare_values; +} + +static cJSON *__comparision_method_in_array(void) +{ + cJSON *cj_in_array = cJSON_CreateObject(); + if (cj_in_array) + { + cJSON *cj_comparator = cJSON_AddObjectToObject(cj_in_array, "comparator"); + if (cj_comparator) + { + cJSON_AddStringToObject(cj_comparator, "family", "array"); + cJSON_AddStringToObject(cj_comparator, "field", "comparator"); + cJSON_AddStringToObject(cj_comparator, ezlopi_type_str, ezlopi_enum_str); + + const static char *options_str = "[\"in\",\"not_in\"]"; + cJSON_AddRawToObject(cj_comparator, "options", options_str); + } + + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"item\",\"expression\",\"device_group\",\"item_group\"]", .field = NULL}, + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_in_array, "dataSource", data_src_obj); + } + + return cj_in_array; +} + +static cJSON *__comparision_method_is_device_item_group(void) +{ + cJSON *cj_is_device_item_grp = cJSON_CreateObject(); + if (cj_is_device_item_grp) + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"device_group\"]", .field = NULL}, + {.types = "[\"item_group\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + + __add_data_src_dest_array_to_object(cj_is_device_item_grp, "dataSource", data_src_obj); + } + + return cj_is_device_item_grp; +} + +static cJSON *__comparision_method_is_device_state(void) +{ + cJSON *cj_is_device_state = cJSON_CreateObject(); + if (cj_is_device_state) + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"device\",\"device_group\"]", .field = NULL}, + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + + __add_data_src_dest_array_to_object(cj_is_device_state, "dataSource", data_src_obj); + } + + return cj_is_device_state; +} + +static cJSON *__comparision_method_is_item_state(void) +{ + cJSON *cj_is_item_state = cJSON_CreateObject(); + if (cj_is_item_state) + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"device_item\",\"item\",\"item_group\",\"device_group\"]", .field = NULL}, + {.types = "[\"constant\",\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + + __add_data_src_dest_array_to_object(cj_is_item_state, "dataSource", data_src_obj); + } + + return cj_is_item_state; +} + +static cJSON *__comparision_method_is_item_state_changed(void) +{ + cJSON *cj_is_item_state_changed = cJSON_CreateObject(); + if (cj_is_item_state_changed) + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"item\",\"expression\"]", .field = NULL}, + {.types = "[\"constant\",\"expression\"]", .field = "start"}, + {.types = "[\"constant\",\"expression\"]", .field = "finish"}, + {.types = NULL, .field = NULL}, + }; + + __add_data_src_dest_array_to_object(cj_is_item_state_changed, "dataSource", data_src_obj); + } + + return cj_is_item_state_changed; +} + +static cJSON *__comparision_method_string_operation(void) +{ + cJSON *cj_string_operation = cJSON_CreateObject(); + if (cj_string_operation) + { + cJSON *cj_comparator = cJSON_AddObjectToObject(cj_string_operation, "comparator"); + if (cj_comparator) + { + cJSON_AddStringToObject(cj_comparator, "family", "strings"); + cJSON_AddStringToObject(cj_comparator, "field", "comparator"); + cJSON_AddStringToObject(cj_comparator, ezlopi_type_str, ezlopi_enum_str); + + const static char *options_str = "[\"begin\",\"end\",\"contain\",\"length\",\"not_begin\",\"not_end\",\"not_contain\",\"not_length\"]"; + cJSON_AddRawToObject(cj_comparator, "options", options_str); + } + + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"item\",\"expression\",\"device_group\",\"item_group\"]", .field = NULL}, + {.types = "[\"constant\",\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + + __add_data_src_dest_array_to_object(cj_string_operation, "dataSource", data_src_obj); + } + + return cj_string_operation; +} + +static void __comparison_methods_list(char *list_name, cJSON *cj_result) +{ + if (cj_result) + { + cJSON *cj_comparision_methods = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_comparision_methods) + { + cJSON_AddItemToObject(cj_comparision_methods, ezlopi_info_str, __comparision_method_info()); + cJSON_AddItemToObject(cj_comparision_methods, "compareNumberRange", __comparision_method_compare_number_range()); + cJSON_AddItemToObject(cj_comparision_methods, "compareNumbers", __comparision_method_compare_numbers()); + cJSON_AddItemToObject(cj_comparision_methods, "compareStrings", __comparision_method_compare_strings()); + cJSON_AddItemToObject(cj_comparision_methods, "compareValues", __comparision_method_compare_values()); + cJSON_AddItemToObject(cj_comparision_methods, "inArray", __comparision_method_in_array()); + cJSON_AddItemToObject(cj_comparision_methods, "isDeviceItemGroup", __comparision_method_is_device_item_group()); + cJSON_AddItemToObject(cj_comparision_methods, "isDeviceState", __comparision_method_is_device_state()); + cJSON_AddItemToObject(cj_comparision_methods, "isItemState", __comparision_method_is_item_state()); + cJSON_AddItemToObject(cj_comparision_methods, "isItemStateChanged", __comparision_method_is_item_state_changed()); + cJSON_AddItemToObject(cj_comparision_methods, "stringOperation", __comparision_method_string_operation()); + } + } +} + +static cJSON *__action_methods_info(void) +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // Info + { + cJSON_AddStringToObject(cj_method, "schema_version", "0.0.1"); + } + return cj_method; +} +static cJSON *__action_methods_set_item_value() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // setItemValue + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"item\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + cJSON_AddStringToObject(cj_method, "typeSystem", "itemValueTypes"); + //------------------------------------------------------------------------- + + static const char *scope_raw_str = "[\"local\",\"global\"]"; + cJSON_AddRawToObject(cj_method, "scope", scope_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_set_device_armed() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // setDeviceArmed + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"device\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + static const char *scope_raw_str = "[\"local\",\"global\"]"; + cJSON_AddRawToObject(cj_method, "scope", scope_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_send_cloud_abstract_command() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // sendCloudAbstractCommand + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"object\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"cloud\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"sync\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_switch_house_mode() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // switchHouseMode + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"houseMode\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_send_http_request() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // sendHttpRequest + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + cJSON_AddNullToObject(cj_method, "dataTarget"); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t dataTarget_sideEffects_obj[] = { + {.types = "[\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + cJSON *cj_side_effects = cJSON_AddArrayToObject(cj_method, "sideEffects"); + if (cj_side_effects) + { + cJSON *cj_side_eff_elem = cJSON_CreateObject(); + if (cj_side_eff_elem) + { + cJSON_AddStringToObject(cj_side_eff_elem, "action", "saveResult"); + __add_data_src_dest_array_to_object(cj_side_eff_elem, "dataTarget", dataTarget_sideEffects_obj); + + if (!cJSON_AddItemToArray(cj_side_effects, cj_side_eff_elem)) + { + cJSON_Delete(cj_side_eff_elem); + } + } + } + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_run_custom_script() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // runCustomScript + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_run_plugin_script() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // runPluginScript + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"script\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"sync\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_run_scene() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // runScene + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"scene\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_stop_scene() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // stopScene + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"scene\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_set_scene_state() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // setSceneState + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"scene\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_reboot_hub() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // rebootHub + { + cJSON_AddNullToObject(cj_method, "dataSource"); + //------------------------------------------------------------------------- + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_cloud_api() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // cloudAPI + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t dataTarget_sideEffects_obj[] = { + {.types = "[\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + cJSON *cj_side_effects = cJSON_AddArrayToObject(cj_method, "sideEffects"); + if (cj_side_effects) + { + cJSON *cj_side_eff_elem = cJSON_CreateObject(); + if (cj_side_eff_elem) + { + cJSON_AddStringToObject(cj_side_eff_elem, "action", "saveResult"); + __add_data_src_dest_array_to_object(cj_side_eff_elem, "dataTarget", dataTarget_sideEffects_obj); + + if (!cJSON_AddItemToArray(cj_side_effects, cj_side_eff_elem)) + { + cJSON_Delete(cj_side_eff_elem); + } + } + } + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_reset_hub() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // resetHub + { + cJSON_AddNullToObject(cj_method, "dataSource"); + //------------------------------------------------------------------------- + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_reset_latch() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // resetLatch + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_set_variable() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // setVariable + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"sync\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + static const char *type_system_raw_str = "[\"itemValueTypes\",\"free\"]"; + cJSON_AddRawToObject(cj_method, "typeSystem", type_system_raw_str); + //------------------------------------------------------------------------- + + cJSON *cj_side_effects = cJSON_AddArrayToObject(cj_method, "sideEffects"); + if (cj_side_effects) + { + cJSON *cj_side_eff_elem = cJSON_CreateObject(); + if (cj_side_eff_elem) + { + cJSON_AddStringToObject(cj_side_eff_elem, "action", "saveResult"); + __add_data_src_dest_array_to_object(cj_side_eff_elem, "dataTarget", data_target_obj); + + if (!cJSON_AddItemToArray(cj_side_effects, cj_side_eff_elem)) + { + cJSON_Delete(cj_side_eff_elem); + } + } + } + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_reset_scene_latches() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // resetSceneLatches + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_set_expression() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // setExpression + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"sync\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + static const char *type_system_raw_str = "[\"itemValueTypes\",\"free\"]"; + cJSON_AddRawToObject(cj_method, "typeSystem", type_system_raw_str); + //------------------------------------------------------------------------- + + cJSON *cj_side_effects = cJSON_AddArrayToObject(cj_method, "sideEffects"); + if (cj_side_effects) + { + cJSON *cj_side_eff_elem = cJSON_CreateObject(); + if (cj_side_eff_elem) + { + cJSON_AddStringToObject(cj_side_eff_elem, "action", "saveResult"); + __add_data_src_dest_array_to_object(cj_side_eff_elem, "dataTarget", data_target_obj); + + if (!cJSON_AddItemToArray(cj_side_effects, cj_side_eff_elem)) + { + cJSON_Delete(cj_side_eff_elem); + } + } + } + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_toggle_value() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // toggleValue + { + cJSON_AddNullToObject(cj_method, "dataSource"); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"item\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + cJSON_AddStringToObject(cj_method, "typeSystem", "itemValueTypes"); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_group_set_item_value() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // groupSetItemValue + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\",\"expression\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"device_group\"]", .field = NULL}, + {.types = "[\"item_group\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + cJSON_AddStringToObject(cj_method, "typeSystem", "itemValueTypes"); + //------------------------------------------------------------------------- + + static const char *scope_raw_str = "[\"local\"]"; + cJSON_AddRawToObject(cj_method, "scope", scope_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_group_toggle_value() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // groupToggleValue + { + cJSON_AddNullToObject(cj_method, "dataSource"); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"device_group\"]", .field = NULL}, + {.types = "[\"item_group\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + cJSON_AddStringToObject(cj_method, "typeSystem", "itemValueTypes"); + //------------------------------------------------------------------------- + + static const char *scope_raw_str = "[\"local\"]"; + cJSON_AddRawToObject(cj_method, "scope", scope_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} +static cJSON *__action_methods_group_set_device_armed() +{ + cJSON *cj_method = cJSON_CreateObject(); + if (cj_method) // groupSetDeviceArmed + { + static const s_data_source_n_target_object_t data_src_obj[] = { + {.types = "[\"constant\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataSource", data_src_obj); + //------------------------------------------------------------------------- + + static const s_data_source_n_target_object_t data_target_obj[] = { + {.types = "[\"device_group\"]", .field = NULL}, + {.types = NULL, .field = NULL}, + }; + __add_data_src_dest_array_to_object(cj_method, "dataTarget", data_target_obj); + //------------------------------------------------------------------------- + + static const char *execution_raw_str = "[\"async\"]"; + cJSON_AddRawToObject(cj_method, "execution", execution_raw_str); + //------------------------------------------------------------------------- + + cJSON_AddStringToObject(cj_method, "typeSystem", "itemValueTypes"); + //------------------------------------------------------------------------- + + static const char *scope_raw_str = "[\"local\"]"; + cJSON_AddRawToObject(cj_method, "scope", scope_raw_str); + //------------------------------------------------------------------------- + } + return cj_method; +} + +static void __action_methods_list(char *list_name, cJSON *cj_result) +{ + if (cj_result) + { + cJSON *cj_action_methods = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_action_methods) + { + cJSON_AddItemToObject(cj_action_methods, "Info", __action_methods_info()); + cJSON_AddItemToObject(cj_action_methods, "setItemValue", __action_methods_set_item_value()); + cJSON_AddItemToObject(cj_action_methods, "setDeviceArmed", __action_methods_set_device_armed()); + cJSON_AddItemToObject(cj_action_methods, "sendCloudAbstractCommand", __action_methods_send_cloud_abstract_command()); + cJSON_AddItemToObject(cj_action_methods, "switchHouseMode", __action_methods_switch_house_mode()); + cJSON_AddItemToObject(cj_action_methods, "sendHttpRequest", __action_methods_send_http_request()); + cJSON_AddItemToObject(cj_action_methods, "runCustomScript", __action_methods_run_custom_script()); + cJSON_AddItemToObject(cj_action_methods, "runPluginScript", __action_methods_run_plugin_script()); + cJSON_AddItemToObject(cj_action_methods, "runScene", __action_methods_run_scene()); + cJSON_AddItemToObject(cj_action_methods, "stopScene", __action_methods_stop_scene()); + cJSON_AddItemToObject(cj_action_methods, "setSceneState", __action_methods_set_scene_state()); + cJSON_AddItemToObject(cj_action_methods, "rebootHub", __action_methods_reboot_hub()); + cJSON_AddItemToObject(cj_action_methods, "cloudAPI", __action_methods_cloud_api()); + cJSON_AddItemToObject(cj_action_methods, "resetHub", __action_methods_reset_hub()); + cJSON_AddItemToObject(cj_action_methods, "resetLatch", __action_methods_reset_latch()); + cJSON_AddItemToObject(cj_action_methods, "setVariable", __action_methods_set_variable()); + cJSON_AddItemToObject(cj_action_methods, "resetSceneLatches", __action_methods_reset_scene_latches()); + cJSON_AddItemToObject(cj_action_methods, "setExpression", __action_methods_set_expression()); + cJSON_AddItemToObject(cj_action_methods, "toggleValue", __action_methods_toggle_value()); + cJSON_AddItemToObject(cj_action_methods, "groupSetItemValue", __action_methods_group_set_item_value()); + cJSON_AddItemToObject(cj_action_methods, "groupToggleValue", __action_methods_group_toggle_value()); + cJSON_AddItemToObject(cj_action_methods, "groupSetDeviceArmed", __action_methods_group_set_device_armed()); + } + } +} + +static void __advanced_scenes_version_list(char *list_name, cJSON *cj_result) +{ + if (cj_result) + { + cJSON *cj_avance_scenes_version = cJSON_AddObjectToObject(cj_result, list_name); + if (cj_avance_scenes_version) + { + cJSON_AddStringToObject(cj_avance_scenes_version, ezlopi_version_str, "Major.Minor"); + } + } +} + +// helper functions + +static void __add_data_src_dest_array_to_object(cJSON *cj_method, char *array_name, const s_data_source_n_target_object_t *data_list) +{ + cJSON *cj_data_source_n_target_list = cJSON_AddArrayToObject(cj_method, array_name); + if (cj_data_source_n_target_list) + { + uint32_t idx = 0; + while (data_list[idx].types || data_list[idx].field) + { + cJSON *cj_arr_object = cJSON_CreateObject(); + if (cj_arr_object) + { + cJSON_AddNumberToObject(cj_arr_object, "index", idx); + if (data_list[idx].types) + { + cJSON_AddRawToObject(cj_arr_object, "types", data_list[idx].types); + } + + if (data_list[idx].field) + { + cJSON_AddStringToObject(cj_arr_object, "field", data_list[idx].field); + } + + if (!cJSON_AddItemToArray(cj_data_source_n_target_list, cj_arr_object)) + { + cJSON_Delete(cj_arr_object); + } + } + + idx++; + } + } +} \ No newline at end of file diff --git a/ezlopi-cloud/scenes/scenes_block_list.c b/ezlopi-cloud/scenes/scenes_block_list.c new file mode 100644 index 000000000..fb4093560 --- /dev/null +++ b/ezlopi-cloud/scenes/scenes_block_list.c @@ -0,0 +1,296 @@ +#include +#include + +#include "trace.h" +#include "cJSON.h" +#include "scenes.h" + +#include "ezlopi_nvs.h" +#include "ezlopi_devices.h" +#include "ezlopi_scenes_cjson.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_meshbot_service.h" +#include "ezlopi_cloud_constants.h" +#include "ezlopi_scenes_operators.h" + +static cJSON *__create_when_block_cjson(l_when_block_v2_t *when_block); + +static bool __found_item_in_field(l_fields_v2_t *field_node, uint32_t item_id); +static cJSON *__add_scenes_blocks_by_device_ids(e_scenes_block_type_v2_t block_type, cJSON *cj_devices_array); +static cJSON *__add_scenes_blocks_by_item_ids(e_scenes_block_type_v2_t block_type, l_ezlopi_item_t *item_list); +static e_scenes_block_type_v2_t __get_block_type_and_create_block_array(cJSON *cj_result, char const **block_type_name, cJSON *cj_block_type); + +void scenes_blocks_list(cJSON *cj_request, cJSON *cj_response) +{ + cJSON_AddItemReferenceToObject(cj_response, ezlopi__id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + if (cj_result) + { + cJSON *cj_paramas = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_paramas) + { + cJSON *cj_block_type = cJSON_GetObjectItem(cj_paramas, ezlopi_blockType_str); + if (cj_block_type && cj_block_type->valuestring) + { + char *block_type_name = NULL; + e_scenes_block_type_v2_t block_type = __get_block_type_and_create_block_array(cj_result, &block_type_name, cj_block_type); + + if (block_type) + { + cJSON *cj_devices_array = cJSON_GetObjectItem(cj_paramas, "devices"); + if (cj_devices_array && (cJSON_Array == cj_devices_array->type)) + { + cJSON *cj_block_array = __add_scenes_blocks_by_device_ids(block_type, cj_devices_array); + if (cj_block_array) + { + if (!cJSON_AddItemToObject(cj_result, block_type_name, cj_block_array)) + { + cJSON_Delete(cj_block_array); + } + } + } + } + } + } + } +} + +static void __add_block_options_and_fields_cjson(cJSON *cj_block, s_block_options_v2_t *block_options, l_fields_v2_t *fields_node) +{ + cJSON *cj_block_opt = cJSON_AddObjectToObject(cj_block, ezlopi_blockOptions_str); + cJSON *cj_fields = cJSON_AddArrayToObject(cj_block, ezlopi_fields_str); + if (cj_block_opt && cj_fields) + { + cJSON *cj_method = cJSON_AddObjectToObject(cj_block_opt, ezlopi_key_method_str); + if (cj_method) + { + cJSON_AddStringToObject(cj_method, ezlopi_name_str, block_options->method.name); + cJSON *cj_args = cJSON_AddObjectToObject(cj_method, ezlopi_args_str); + if (cj_args) + { + while (fields_node) + { + cJSON_AddStringToObject(cj_args, fields_node->name, fields_node->name); + + cJSON *cj_field_obj = ezlopi_scene_cjson_get_field(fields_node); + if (!cJSON_AddItemToArray(cj_fields, cj_field_obj)) + { + cJSON_Delete(cj_field_obj); + } + + fields_node = fields_node->next; + } + } + } + } +} + +static cJSON *__create_when_block_cjson(l_when_block_v2_t *when_block) +{ + cJSON *cj_when_block = NULL; + + if (when_block) + { + cj_when_block = cJSON_CreateObject(); + if (cj_when_block) + { + __add_block_options_and_fields_cjson(cj_when_block, &when_block->block_options, when_block->fields); + cJSON_AddStringToObject(cj_when_block, ezlopi_blockType_str, ezlopi_when_str); + } + } + + return cj_when_block; +} + +static cJSON *__create_then_block_cjson(l_action_block_v2_t *then_block) +{ + cJSON *cj_then_block = NULL; + + if (then_block) + { + cj_then_block = cJSON_CreateObject(); + if (cj_then_block) + { + __add_block_options_and_fields_cjson(cj_then_block, &then_block->block_options, then_block->fields); + cJSON_AddStringToObject(cj_then_block, ezlopi_blockType_str, ezlopi_then_str); + } + } + + return cj_then_block; +} + +static cJSON *__create_else_block_cjson(l_action_block_v2_t *else_block) +{ + cJSON *cj_else_block = NULL; + + if (else_block) + { + cj_else_block = cJSON_CreateObject(); + if (cj_else_block) + { + __add_block_options_and_fields_cjson(cj_else_block, &else_block->block_options, else_block->fields); + cJSON_AddStringToObject(cj_else_block, ezlopi_blockType_str, ezlopi_else_str); + } + } + + return cj_else_block; +} + +static bool __found_item_in_field(l_fields_v2_t *field_node, uint32_t item_id) +{ + bool ret = false; + while (field_node) + { + if (EZLOPI_VALUE_TYPE_ITEM == field_node->value_type) + { + uint32_t item_id_check = strtoul(field_node->value.value_string, NULL, 16); + TRACE_D("item-id: %s | %08x", field_node->value.value_string, item_id); + if (item_id_check == item_id) + { + ret = 1; + break; + } + } + + field_node = field_node->next; + } + + return ret; +} + +static cJSON *__add_scenes_blocks_by_item_ids(e_scenes_block_type_v2_t block_type, l_ezlopi_item_t *item_list) +{ + cJSON *cj_block = NULL; + while (item_list) + { + l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_scenes_head_v2(); + while (scene_node) + { + TRACE_D("Here"); + + switch (block_type) + { + case SCENE_BLOCK_TYPE_WHEN: + { + TRACE_D("Here"); + if (scene_node->when_block) + { + TRACE_D("Here"); + if (__found_item_in_field(scene_node->when_block->fields, item_list->cloud_properties.item_id)) + { + TRACE_D("Here"); + cj_block = __create_when_block_cjson(scene_node->when_block); + } + else + { + TRACE_W("item id not found"); + } + } + break; + } + case SCENE_BLOCK_TYPE_THEN: + { + if (scene_node->then_block) + { + if (__found_item_in_field(scene_node->then_block->fields, item_list->cloud_properties.item_id)) + { + cj_block = __create_then_block_cjson(scene_node->then_block); + } + } + break; + } + case SCENE_BLOCK_TYPE_ELSE: + { + if (scene_node->else_block) + { + if (__found_item_in_field(scene_node->else_block->fields, item_list->cloud_properties.item_id)) + { + cj_block = __create_else_block_cjson(scene_node->else_block); + } + } + break; + } + default: + { + break; + } + } + + scene_node = scene_node->next; + } + + item_list = item_list->next; + } + + return cj_block; +} + +static cJSON *__add_scenes_blocks_by_device_ids(e_scenes_block_type_v2_t block_type, cJSON *cj_devices_array) +{ + int device_id_idx = 0; + cJSON *cj_device_id = NULL; + cJSON *cj_block_array = NULL; + + CJSON_TRACE("device array", cj_devices_array); + + while (NULL != (cj_device_id = cJSON_GetArrayItem(cj_devices_array, device_id_idx++))) + { + TRACE_D("device-id: %s", cj_device_id->valuestring ? cj_device_id->valuestring : ezlopi__str); + if (cj_device_id->valuestring) + { + uint32_t device_id = strtoul(cj_device_id->valuestring, NULL, 16); + l_ezlopi_device_t *device_node = ezlopi_device_get_by_id(device_id); + if (device_node) + { + TRACE_D("Here"); + cJSON *cj_block = __add_scenes_blocks_by_item_ids(block_type, device_node->items); + if (cj_block) + { + if (NULL == cj_block_array) + { + cj_block_array = cJSON_CreateArray(); + } + + if (cj_block_array) + { + if (!cJSON_AddItemToArray(cj_block_array, cj_block)) + { + cJSON_Delete(cj_block); + } + } + } + } + else + { + TRACE_E("Device-id not found in list"); + } + } + } + + return cj_block_array; +} + +static e_scenes_block_type_v2_t __get_block_type_and_create_block_array(cJSON *cj_result, char const **block_type_name, cJSON *cj_block_type) +{ + e_scenes_block_type_v2_t block_type = SCENE_BLOCK_TYPE_NONE; + + if (0 == strncmp(ezlopi_when_str, cj_block_type->valuestring, 5)) + { + *block_type_name = ezlopi_when_str; + block_type = SCENE_BLOCK_TYPE_WHEN; + } + else if (0 == strncmp(ezlopi_then_str, cj_block_type->valuestring, 5)) + { + *block_type_name = ezlopi_then_str; + block_type = SCENE_BLOCK_TYPE_THEN; + } + else if (0 == strncmp(ezlopi_else_str, cj_block_type->valuestring, 5)) + { + *block_type_name = ezlopi_else_str; + block_type = SCENE_BLOCK_TYPE_ELSE; + } + + return block_type; +} diff --git a/ezlopi-cloud/scenes/scenes_expressions.c b/ezlopi-cloud/scenes/scenes_expressions.c new file mode 100644 index 000000000..2d10fef00 --- /dev/null +++ b/ezlopi-cloud/scenes/scenes_expressions.c @@ -0,0 +1,74 @@ +#include "trace.h" + +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" +#include "ezlopi_scenes_expressions.h" + +#include "scenes_expressions.h" + +void scenes_expressions_added(cJSON *cj_request, cJSON *cj_response) +{ + // https://api.ezlo.com/hub/broadcasts/index.html#hubexpressionadded +} + +void scenes_expressions_delete(cJSON *cj_request, cJSON *cj_response) +{ + if (cj_request && cj_response) + { + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + cJSON *cj_expression_name = cJSON_GetObjectItem(cj_params, ezlopi_name_str); + if (cj_expression_name && cj_expression_name->valuestring) + { + ezlopi_scenes_expressions_delete_by_name(cj_expression_name->valuestring); + } + } + } +} + +void scenes_expressions_list(cJSON *cj_request, cJSON *cj_response) +{ + if (cj_request && cj_response) + { + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + CJSON_TRACE("exp-params", cj_params); + + cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + if (cj_result) + { + cJSON *cj_expressions_array = cJSON_AddArrayToObject(cj_result, ezlopi_expressions_str); + if (cj_expressions_array) + { + ezlopi_scenes_expressions_list_cjson(cj_expressions_array, cj_params); + } + } + } + } +} + +void scenes_expressions_set(cJSON *cj_request, cJSON *cj_response) +{ + if (cj_request && cj_response) + { + cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); + cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); + cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); + if (cj_params) + { + CJSON_TRACE("expressions params", cj_params); + ezlopi_scenes_expressions_add_to_head(0, cj_params); + } + } +} \ No newline at end of file diff --git a/ezlopi-cloud/scenes/scenes_expressions.h b/ezlopi-cloud/scenes/scenes_expressions.h new file mode 100644 index 000000000..9ffaf4adf --- /dev/null +++ b/ezlopi-cloud/scenes/scenes_expressions.h @@ -0,0 +1,11 @@ +#ifndef __SCENES_EXPRESSIONS_H__ +#define __SCENES_EXPRESSIONS_H__ +#include + +#include "ezlopi_scenes_expressions.h" + +void scenes_expressions_list(cJSON *cj_request, cJSON *cj_response); +void scenes_expressions_set(cJSON *cj_request, cJSON *cj_response); +void scenes_expressions_delete(cJSON *cj_request, cJSON *cj_response); + +#endif // __SCENES_EXPRESSIONS_H__ \ No newline at end of file diff --git a/ezlopi-cloud/scenes/scenes_scripts.c b/ezlopi-cloud/scenes/scenes_scripts.c index 4983f28b6..061fb297d 100755 --- a/ezlopi-cloud/scenes/scenes_scripts.c +++ b/ezlopi-cloud/scenes/scenes_scripts.c @@ -15,8 +15,7 @@ void scenes_scripts_list(cJSON *cj_request, cJSON *cj_response) cJSON *cj_result = cJSON_AddArrayToObject(cj_response, ezlopi_result_str); if (cj_result) { - // l_ezlopi_scenes_script_t *script_nodes = ezlopi_scenes_scripts_get_head(); - l_ezlopi_scenes_script_t *script_nodes = NULL; + l_ezlopi_scenes_script_t *script_nodes = ezlopi_scenes_scripts_get_head(); while (script_nodes) { cJSON *cj_script = cJSON_CreateObject(); @@ -25,7 +24,7 @@ void scenes_scripts_list(cJSON *cj_request, cJSON *cj_response) char script_id_str[32]; snprintf(script_id_str, sizeof(script_id_str), "%08x", script_nodes->id); cJSON_AddStringToObject(cj_script, ezlopi__id_str, script_id_str); - cJSON_AddStringToObject(cj_script, "name", script_nodes->name); + cJSON_AddStringToObject(cj_script, ezlopi_name_str, script_nodes->name); if (!cJSON_AddItemToArray(cj_result, cj_script)) { @@ -63,15 +62,14 @@ void scenes_scripts_get(cJSON *cj_request, cJSON *cj_response) if (script_id_num) { - // l_ezlopi_scenes_script_t *script_nodes = ezlopi_scenes_scripts_get_head(); - l_ezlopi_scenes_script_t *script_nodes = NULL; + l_ezlopi_scenes_script_t *script_nodes = ezlopi_scenes_scripts_get_head(); while (script_nodes) { if (script_id_num == script_nodes->id) { cJSON_AddItemReferenceToObject(cj_result, ezlopi__id_str, cj_script_id); - cJSON_AddStringToObject(cj_result, "name", script_nodes->name); - cJSON_AddStringToObject(cj_result, "code", script_nodes->code); + cJSON_AddStringToObject(cj_result, ezlopi_name_str, script_nodes->name); + cJSON_AddStringToObject(cj_result, ezlopi_code_str, script_nodes->code); break; } @@ -91,8 +89,7 @@ void scenes_scripts_add(cJSON *cj_request, cJSON *cj_response) cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { - // uint32_t script_id = ezlopi_scenes_scripts_add_to_head(0, cj_params); - uint32_t script_id = NULL; + uint32_t script_id = ezlopi_scenes_scripts_add_to_head(0, cj_params); cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); if (cj_result) @@ -125,8 +122,8 @@ void scenes_scripts_delete(cJSON *cj_request, cJSON *cj_response) if (cj_script_id && cj_script_id->valuestring) { script_id_num = strtoul(cj_script_id->valuestring, NULL, 16); - // ezlopi_scenes_scripts_stop_by_id(script_id_num); - // ezlopi_scenes_scripts_delete_by_id(script_id_num); + ezlopi_scenes_scripts_stop_by_id(script_id_num); + ezlopi_scenes_scripts_delete_by_id(script_id_num); } } } @@ -141,7 +138,7 @@ void scenes_scripts_set(cJSON *cj_request, cJSON *cj_response) cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { - // ezlopi_scenes_scripts_update(cj_params); + ezlopi_scenes_scripts_update(cj_params); } } @@ -160,7 +157,7 @@ void scenes_scripts_run(cJSON *cj_request, cJSON *cj_response) uint32_t script_id = strtoul(cj_script_id->valuestring, NULL, 16); if (script_id) { - // ezlopi_scenes_scripts_run_by_id(script_id); + ezlopi_scenes_scripts_run_by_id(script_id); } } } diff --git a/ezlopi-cloud/settings/settings.c b/ezlopi-cloud/settings/settings.c index 7e21ebc08..565e3ad0d 100644 --- a/ezlopi-cloud/settings/settings.c +++ b/ezlopi-cloud/settings/settings.c @@ -4,9 +4,11 @@ #include "trace.h" #include "cJSON.h" +#include "ezlopi_devices_list.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_cloud_constants.h" #include "ezlopi_cloud_methods_str.h" -#include "ezlopi_devices_list.h" + #include "web_provisioning.h" void ezlopi_device_settings_list_v3(cJSON *cj_request, cJSON *cj_response) @@ -17,7 +19,7 @@ void ezlopi_device_settings_list_v3(cJSON *cj_request, cJSON *cj_response) cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); if (cj_result) { - cJSON *cj_settings_array = cJSON_AddArrayToObject(cj_result, "settings"); + cJSON *cj_settings_array = cJSON_AddArrayToObject(cj_result, ezlopi_settings_str); if (cj_settings_array) { l_ezlopi_device_t *curr_device = ezlopi_device_get_head(); @@ -31,10 +33,10 @@ void ezlopi_device_settings_list_v3(cJSON *cj_request, cJSON *cj_response) { char tmp_string[64]; snprintf(tmp_string, sizeof(tmp_string), "%08x", curr_setting->cloud_properties.setting_id); - cJSON_AddStringToObject(cj_properties, "_id", tmp_string); + cJSON_AddStringToObject(cj_properties, ezlopi__id_str, tmp_string); snprintf(tmp_string, sizeof(tmp_string), "%08x", curr_device->cloud_properties.device_id); - cJSON_AddStringToObject(cj_properties, "deviceId", tmp_string); - cJSON_AddStringToObject(cj_properties, "status", "synced"); + cJSON_AddStringToObject(cj_properties, ezlopi_deviceId_str, tmp_string); + cJSON_AddStringToObject(cj_properties, ezlopi_status_str, ezlopi_synced_str); curr_setting->func(EZLOPI_SETTINGS_ACTION_GET_SETTING, curr_setting, cj_properties, curr_setting->user_arg); if (!cJSON_AddItemToArray(cj_settings_array, cj_properties)) { @@ -54,7 +56,7 @@ void ezlopi_device_settings_value_set_v3(cJSON *cj_request, cJSON *cj_response) cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - cJSON *cj_params = cJSON_GetObjectItem(cj_request, "params"); + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { char *setting_id_str = 0; @@ -90,17 +92,17 @@ void ezlopi_device_settings_reset_v3(cJSON *cj_request, cJSON *cj_response) cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); cJSON_AddObjectToObject(cj_response, ezlopi_result_str); - cJSON *cj_params = cJSON_GetObjectItem(cj_request, "params"); + cJSON *cj_params = cJSON_GetObjectItem(cj_request, ezlopi_params_str); if (cj_params) { l_ezlopi_device_t *curr_device = ezlopi_device_get_head(); - uint32_t found_setting = 0; + while (curr_device) { - if (cJSON_HasObjectItem(cj_params, ezlopi_device_id_str)) + if (cJSON_HasObjectItem(cj_params, ezlopi_deviceId_str)) { char *device_id_str = 0; - CJSON_GET_VALUE_STRING(cj_params, ezlopi_device_id_str, device_id_str); + CJSON_GET_VALUE_STRING(cj_params, ezlopi_deviceId_str, device_id_str); int device_id = strtol(device_id_str, NULL, 16); TRACE_E("device_id: %X", device_id); if (device_id == curr_device->cloud_properties.device_id) @@ -155,7 +157,7 @@ cJSON *ezlopi_cloud_settings_updated_from_devices_v3(l_ezlopi_device_t *device, { char tmp_string[64]; snprintf(tmp_string, sizeof(tmp_string), "%08x", setting->cloud_properties.setting_id); - cJSON_AddStringToObject(cj_result, "_id", tmp_string); + cJSON_AddStringToObject(cj_result, ezlopi__id_str, tmp_string); setting->func(EZLOPI_SETTINGS_ACTION_UPDATE_SETTING, setting, cj_result, setting->user_arg); } } diff --git a/ezlopi-core/ezlopi/CMakeLists.txt b/ezlopi-core/ezlopi/CMakeLists.txt index 7aabf1d8c..d9e1b24fc 100644 --- a/ezlopi-core/ezlopi/CMakeLists.txt +++ b/ezlopi-core/ezlopi/CMakeLists.txt @@ -5,10 +5,11 @@ file(GLOB_RECURSE config_src "*.c") idf_component_register(SRCS "ezlopi.c" "${config_src}" INCLUDE_DIRS "." REQUIRES trace + core_sntp ezlopi_devices ezlopi_factory_info ezlopi_nvs ezlopi_wifi ezlopi_ethernet ezlopi_event_queue ezlopi_ping - core_sntp) + ezlopi_room ) diff --git a/ezlopi-core/ezlopi/ezlopi.c b/ezlopi-core/ezlopi/ezlopi.c index 8cd8a8ee8..be3fff68b 100644 --- a/ezlopi-core/ezlopi/ezlopi.c +++ b/ezlopi-core/ezlopi/ezlopi.c @@ -18,6 +18,8 @@ #include "ezlopi_ethernet.h" #include "ezlopi_scenes_v2.h" #include "ezlopi_scenes_scripts.h" +#include "ezlopi_scenes_expressions.h" +#include "ezlopi_room.h" static void ezlopi_initialize_devices_v3(void); @@ -34,18 +36,21 @@ void ezlopi_init(void) ezlopi_factory_info_v3_init(); print_factory_info_v3(); - // Init devices ezlopi_event_group_create(); - ezlopi_device_prepare(); - vTaskDelay(10); ezlopi_wifi_initialize(); vTaskDelay(10); + +#if 1 + // Init devices + ezlopi_device_prepare(); + vTaskDelay(10); ezlopi_initialize_devices_v3(); vTaskDelay(10); + ezlopi_room_init(); ezlopi_scenes_scripts_init(); + ezlopi_scenes_expressions_init(); ezlopi_scenes_init_v2(); - // ezlopi_ethernet_init(); uint32_t boot_count = ezlopi_system_info_get_boot_count(); @@ -57,10 +62,12 @@ void ezlopi_init(void) ezlopi_timer_start_1000ms(); ezlopi_ping_init(); // core_sntp_init(); +#endif } static void ezlopi_initialize_devices_v3(void) { + int device_init_ret = 0; l_ezlopi_device_t *curr_device = ezlopi_device_get_head(); while (curr_device) { @@ -69,14 +76,29 @@ static void ezlopi_initialize_devices_v3(void) { if (curr_item->func) { - curr_item->func(EZLOPI_ACTION_INITIALIZE, curr_item, NULL, NULL); + if ((device_init_ret = curr_item->func(EZLOPI_ACTION_INITIALIZE, curr_item, NULL, NULL)) < 0) + { + break; + } } else { TRACE_E("Function is not defined!"); } + curr_item = curr_item->next; } - curr_device = curr_device->next; + + if (device_init_ret < 0) + { + l_ezlopi_device_t *device_to_free = curr_device; + curr_device = curr_device->next; + device_to_free->next = NULL; + ezlopi_device_free_device(device_to_free); + } + else + { + curr_device = curr_device->next; + } } } diff --git a/ezlopi-core/ezlopi_ble/ezlopi_ble_gap.c b/ezlopi-core/ezlopi_ble/ezlopi_ble_gap.c index 29b136f66..8052bc273 100644 --- a/ezlopi-core/ezlopi_ble/ezlopi_ble_gap.c +++ b/ezlopi-core/ezlopi_ble/ezlopi_ble_gap.c @@ -9,12 +9,13 @@ #include "trace.h" #include "ezlopi_ble_gap.h" #include "ezlopi_ble_profile.h" +#include "ezlopi_cloud_constants.h" static uint8_t adv_config_done = 0; #define ADV_CONFIG_FLAG (1 << 0) #define SCAN_RSP_CONFIG_FLAG (1 << 1) -static uint8_t manufacturer[] = {'e', 'z', 'l', 'o', 'p', 'i'}; +// static uint8_t manufacturer[] = {'e', 'z', 'l', 'o', 'p', 'i'}; static uint8_t *all_service_uuid = NULL; static uint32_t all_service_uuid_len = 0; @@ -95,14 +96,15 @@ void ezlopi_ble_gap_config_adv_data(void) // adv_data.service_uuid_len = all_service_uuid_len; esp_err_t ret = esp_ble_gap_config_adv_data(&adv_data); - if (ret) + if (ESP_OK == ret) { - TRACE_E("config adv data failed, error code = %x", ret); + TRACE_I("Adv-data set complete"); } else { - TRACE_I("Adv-data set complete"); + TRACE_E("config adv data failed, error code = %x", ret); } + adv_config_done |= ADV_CONFIG_FLAG; } @@ -277,7 +279,7 @@ void ezlopi_ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_p TRACE_I("ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT status = %d", param->remove_bond_dev_cmpl.status); TRACE_I("ESP_GAP_BLE_REMOVE_BOND_DEV"); TRACE_I("-----ESP_GAP_BLE_REMOVE_BOND_DEV----"); - dump("", param->remove_bond_dev_cmpl.bd_addr, 0, sizeof(esp_bd_addr_t)); + dump("bd_adrr", param->remove_bond_dev_cmpl.bd_addr, 0, sizeof(esp_bd_addr_t)); TRACE_I("------------------------------------"); break; } @@ -692,30 +694,29 @@ static char *ezlopi_ble_gap_event_to_str(esp_gap_ble_cb_event_t event) } #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE) - // case ESP_GAP_BLE_SC_OOB_REQ_EVT: - // { - // ret = "ESP_GAP_BLE_SC_OOB_REQ_EVT"; - // break; - // } - // case ESP_GAP_BLE_EVT_MAX: - // { - // ret = "ESP_GAP_BLE_EVT_MAX"; - // break; - // } - // case ESP_GAP_BLE_SC_CR_LOC_OOB_EVT: - // { - // ret = "ESP_GAP_BLE_SC_CR_LOC_OOB_EVT"; - // break; - // } - // case ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT: - // { - // ret = "ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT"; - // break; - // } + // case ESP_GAP_BLE_SC_OOB_REQ_EVT: + // { + // ret = "ESP_GAP_BLE_SC_OOB_REQ_EVT"; + // break; + // } + // case ESP_GAP_BLE_EVT_MAX: + // { + // ret = "ESP_GAP_BLE_EVT_MAX"; + // break; + // } + // case ESP_GAP_BLE_SC_CR_LOC_OOB_EVT: + // { + // ret = "ESP_GAP_BLE_SC_CR_LOC_OOB_EVT"; + // break; + // } + // case ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT: + // { + // ret = "ESP_GAP_BLE_GET_DEV_NAME_COMPLETE_EVT"; + // break; + // } default: break; - } return ret; @@ -843,5 +844,5 @@ static void ezlopi_ble_setup_service_uuid(void) } } - dump("complete-uuid", (all_service_uuid ? all_service_uuid : ""), 0, all_service_uuid_len); + dump("complete-uuid", (all_service_uuid ? all_service_uuid : ezlopi__str), 0, all_service_uuid_len); } diff --git a/ezlopi-core/ezlopi_ble/ezlopi_ble_profile.c b/ezlopi-core/ezlopi_ble/ezlopi_ble_profile.c index 3597ffee9..f297b305c 100644 --- a/ezlopi-core/ezlopi_ble/ezlopi_ble_profile.c +++ b/ezlopi-core/ezlopi_ble/ezlopi_ble_profile.c @@ -3,6 +3,7 @@ #include "ezlopi_ble_gatt.h" #include "ezlopi_ble_config.h" #include "ezlopi_ble_profile.h" +#include "ezlopi_cloud_constants.h" static s_gatt_service_t *gatt_head_service = NULL; @@ -10,7 +11,6 @@ static void ezlopi_ble_gatt_append_descriptor_to_characteristic(s_gatt_char_t *c static void ezlopi_ble_gatt_append_characterstic_to_service(s_gatt_service_t *service_obj, s_gatt_char_t *character_object); static void ezlopi_ble_gatt_service_append_to_head(s_gatt_service_t *service_obj); static s_gatt_service_t *ezlopi_ble_gatt_search_service_by_characteristic(s_gatt_char_t *characteristic); -static char *ezlopi_ble_gatt_event_to_string(esp_gatts_cb_event_t event); s_gatt_service_t *ezlopi_ble_profile_get_head(void) { return gatt_head_service; @@ -394,7 +394,7 @@ void ezlopi_ble_gatt_print_service(s_gatt_service_t *service) void ezlopi_ble_gatt_print_uuid(esp_bt_uuid_t *uuid, char *msg) { - msg = msg ? msg : ""; + msg = msg ? msg : ezlopi__str; if (uuid) { diff --git a/ezlopi-core/ezlopi_ble/ezlopi_ble_v2.c.depricated b/ezlopi-core/ezlopi_ble/ezlopi_ble_v2.c.depricated index f3b5eff6b..c5b88a110 100644 --- a/ezlopi-core/ezlopi_ble/ezlopi_ble_v2.c.depricated +++ b/ezlopi-core/ezlopi_ble/ezlopi_ble_v2.c.depricated @@ -196,7 +196,7 @@ // cJSON_AddStringToObject(cjson_wifi_info, "PSD", "********"); // esp_netif_ip_info_t *wifi_ip_info = ezlopi_wifi_get_ip_infos(); -// cJSON_AddStringToObject(cjson_wifi_info, "ip", ip4addr_ntoa((const ip4_addr_t *)&wifi_ip_info->ip)); +// cJSON_AddStringToObject(cjson_wifi_info, ezlopi_ip_str, ip4addr_ntoa((const ip4_addr_t *)&wifi_ip_info->ip)); // cJSON_AddStringToObject(cjson_wifi_info, "gw", ip4addr_ntoa((const ip4_addr_t *)&wifi_ip_info->gw)); // cJSON_AddStringToObject(cjson_wifi_info, "netmask", ip4addr_ntoa((const ip4_addr_t *)&wifi_ip_info->netmask)); diff --git a/ezlopi-core/ezlopi_cloud/ezlopi_cloud.c b/ezlopi-core/ezlopi_cloud/ezlopi_cloud.c index 159e869b0..61808cccc 100644 --- a/ezlopi-core/ezlopi_cloud/ezlopi_cloud.c +++ b/ezlopi-core/ezlopi_cloud/ezlopi_cloud.c @@ -9,14 +9,16 @@ const uint32_t ROOM_ID_START = 0x30000000; const uint32_t SETTINGS_ID_START = 0x50000000; const uint32_t SCENE_ID_START = 0x60000000; const uint32_t SCRIPT_ID_START = 0x70000000; +const uint32_t EXPRESSION_ID_START = 0x80000000; -static uint64_t device_id = 0; -static uint64_t item_id = 0; -static uint64_t room_id = 0; -static uint64_t gateway_id = 0; -static uint64_t settings_id = 0; -static uint32_t scene_id = 0; -static uint32_t script_id = 0; +static uint32_t g_device_id = 0; +static uint32_t g_item_id = 0; +static uint32_t g_room_id = 0; +static uint32_t g_gateway_id = 0; +static uint32_t g_settings_id = 0; +static uint32_t g_scene_id = 0; +static uint32_t g_script_id = 0; +static uint32_t g_expression_id = 0; static uint32_t ezlopi_get_mac_crc(void) { @@ -32,98 +34,112 @@ static uint32_t ezlopi_get_mac_crc(void) for (int j = 0; j < 8; j++) { if (crc & 0x8000) + { crc = (crc << 1) ^ CRC16_POLY; + } else + { crc <<= 1; + } } } return (crc << 8) & 0x00FFFF00; } +void ezlopi_cloud_update_device_id(uint32_t device_id) +{ + g_device_id = (device_id > g_device_id) ? device_id : g_device_id; +} uint32_t ezlopi_cloud_generate_device_id(void) { - if (0 == device_id) + if (0 == g_device_id) { - device_id = DEVICE_ID_START + ezlopi_get_mac_crc(); + g_device_id = DEVICE_ID_START + ezlopi_get_mac_crc(); } else { - device_id++; + g_device_id++; } - // TRACE_D("device_id: %u\r\n", device_id); - return device_id; + // TRACE_D("g_device_id: %u\r\n", g_device_id); + return g_device_id; } uint32_t ezlopi_cloud_generate_item_id(void) { - if (0 == item_id) + if (0 == g_item_id) { - item_id = ITEM_ID_START + ezlopi_get_mac_crc(); + g_item_id = ITEM_ID_START + ezlopi_get_mac_crc(); } else { - item_id++; + g_item_id++; } - // TRACE_D("item_id: %u\r\n", item_id); - - return item_id; -} + // TRACE_D("g_item_id: %u\r\n", g_item_id); -uint32_t ezlopi_cloud_generate_room_id(void) -{ - if (0 == room_id) - { - room_id = ROOM_ID_START + ezlopi_get_mac_crc(); - } - else - { - room_id++; - } - // TRACE_D("room_id: %u\r\n", room_id); - return room_id; + return g_item_id; } uint32_t ezlopi_cloud_generate_gateway_id(void) { - if (0 == gateway_id) + if (0 == g_gateway_id) { - gateway_id = 0x457a5069; + g_gateway_id = 0x457a5069; } - // TRACE_D("gateway_id: %u\r\n", gateway_id); - return gateway_id; + // TRACE_D("g_gateway_id: %u\r\n", g_gateway_id); + return g_gateway_id; } uint32_t ezlopi_cloud_generate_settings_id(void) { - if (0 == settings_id) + if (0 == g_settings_id) { - settings_id = SETTINGS_ID_START + ezlopi_get_mac_crc(); + g_settings_id = SETTINGS_ID_START + ezlopi_get_mac_crc(); } else { - settings_id++; + g_settings_id++; } - // TRACE_D("settings_id: %u\r\n", gateway_id); - return settings_id; + // TRACE_D("g_settings_id: %u\r\n", g_gateway_id); + return g_settings_id; } void ezlopi_cloud_update_scene_id(uint32_t a_scene_id) { - scene_id = (a_scene_id > scene_id) ? a_scene_id : scene_id; + g_scene_id = (a_scene_id > g_scene_id) ? a_scene_id : g_scene_id; } uint32_t ezlopi_cloud_generate_scene_id(void) { - scene_id = (0 == scene_id) ? (SCENE_ID_START + ezlopi_get_mac_crc()) : (scene_id + 1); - return scene_id; + g_scene_id = (0 == g_scene_id) ? (SCENE_ID_START + ezlopi_get_mac_crc()) : (g_scene_id + 1); + return g_scene_id; } void ezlopi_cloud_update_script_id(uint32_t a_script_id) { - script_id = (a_script_id > script_id) ? a_script_id : script_id; + g_script_id = (a_script_id > g_script_id) ? a_script_id : g_script_id; } uint32_t ezlopi_cloud_generate_script_id(void) { - script_id = (0 == script_id) ? (SCRIPT_ID_START + ezlopi_get_mac_crc()) : (script_id + 1); - return script_id; + g_script_id = (0 == g_script_id) ? (SCRIPT_ID_START + ezlopi_get_mac_crc()) : (g_script_id + 1); + return g_script_id; +} + +void ezlopi_cloud_update_expression_id(uint32_t a_expression_id) +{ + g_expression_id = (a_expression_id > g_expression_id) ? a_expression_id : g_expression_id; +} +uint32_t ezlopi_cloud_generate_expression_id(void) +{ + g_expression_id = (0 == g_expression_id) ? (EXPRESSION_ID_START + ezlopi_get_mac_crc()) : (g_expression_id + 1); + return g_expression_id; +} + +void ezlopi_cloud_update_room_id(uint32_t a_room_id) +{ + g_room_id = (a_room_id > g_room_id) ? a_room_id : g_room_id; +} +uint32_t ezlopi_cloud_generate_room_id(void) +{ + g_room_id = (0 == g_room_id) ? (ROOM_ID_START + ezlopi_get_mac_crc()) : (g_room_id + 1); + return g_room_id; } diff --git a/ezlopi-core/ezlopi_cloud/ezlopi_cloud.h b/ezlopi-core/ezlopi_cloud/ezlopi_cloud.h index 22839c8d2..7154d8b19 100644 --- a/ezlopi-core/ezlopi_cloud/ezlopi_cloud.h +++ b/ezlopi-core/ezlopi_cloud/ezlopi_cloud.h @@ -16,9 +16,9 @@ typedef struct s_ezlopi_cloud_controller char room_id[40]; // 'roomId' -> https://api.ezlo.com/hub/devices_api/index.html bool persistent; // 'persistent' -> https://api.ezlo.com/hub/devices_api/index.html bool service_notification; // 'serviceNotification' -> https://api.ezlo.com/hub/devices_api/index.html - char *security; // 'security' -> https://api.ezlo.com/hub/devices_api/index.html, ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_security_str.h + const char *security; // 'security' -> https://api.ezlo.com/hub/devices_api/index.html, ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_security_str.h bool ready; // 'ready' -> https://api.ezlo.com/hub/devices_api/index.html - char *status; // 'status' -> https://api.ezlo.com/hub/devices_api/index.html, ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_status_str.h + const char *status; // 'status' -> https://api.ezlo.com/hub/devices_api/index.html, ezlopi-cloud/ezlopi_cloud_constants/ezlopi_cloud_status_str.h } s_ezlopi_cloud_controller_t; @@ -83,12 +83,18 @@ extern const uint32_t ROOM_ID_START; extern const uint32_t SETTINGS_ID_START; extern const uint32_t SCENE_ID_START; extern const uint32_t SCRIPT_ID_START; +extern const uint32_t EXPRESSION_ID_START; uint32_t ezlopi_cloud_generate_device_id(void); +void ezlopi_cloud_update_device_id(uint32_t device_id); + uint32_t ezlopi_cloud_generate_item_id(void); + uint32_t ezlopi_cloud_generate_room_id(void); -uint32_t ezlopi_cloud_generate_gateway_id(void); + uint32_t ezlopi_cloud_get_gateway_id(void); +uint32_t ezlopi_cloud_generate_gateway_id(void); + uint32_t ezlopi_cloud_generate_settings_id(void); uint32_t ezlopi_cloud_generate_scene_id(void); @@ -97,4 +103,10 @@ void ezlopi_cloud_update_scene_id(uint32_t a_scene_id); uint32_t ezlopi_cloud_generate_script_id(void); void ezlopi_cloud_update_script_id(uint32_t a_script_id); +uint32_t ezlopi_cloud_generate_expression_id(void); +void ezlopi_cloud_update_expression_id(uint32_t a_expression_id); + +uint32_t ezlopi_cloud_generate_room_id(void); +void ezlopi_cloud_update_room_id(uint32_t a_room_id); + #endif // __EZLOPI_CLOUD_H__ diff --git a/ezlopi-core/ezlopi_devices/ezlopi_cjson_macros.h b/ezlopi-core/ezlopi_devices/ezlopi_cjson_macros.h new file mode 100644 index 000000000..19be3e318 --- /dev/null +++ b/ezlopi-core/ezlopi_devices/ezlopi_cjson_macros.h @@ -0,0 +1,153 @@ +#ifndef __EZLOPI_CJSON_MACROS_H__ +#define __EZLOPI_CJSON_MACROS_H__ + +#include "trace.h" +#include +#include + +#define CJSON_GET_VALUE_DOUBLE(root, item_name, item_val) \ + { \ + cJSON *o_item = cJSON_GetObjectItem(root, item_name); \ + if (o_item) \ + { \ + item_val = o_item->valuedouble; \ + } \ + else \ + { \ + item_val = 0; \ + TRACE_E("%s not found!", item_name); \ + } \ + } + +// TRACE_B("%s: %f", item_name, (double)item_val); + +#define CJSON_GET_VALUE_BOOL(root, item_name, item_val) \ + { \ + cJSON *o_item = cJSON_GetObjectItem(root, item_name); \ + if (o_item) \ + { \ + if (o_item->type == cJSON_False) \ + { \ + item_val = false; \ + } \ + else \ + { \ + item_val = true; \ + } \ + item_val = o_item->valueint; \ + } \ + else \ + { \ + item_val = false; \ + TRACE_E("%s not found!", item_name); \ + } \ + } + +#define CJSON_GET_VALUE_INT(root, item_name, item_val) \ + { \ + cJSON *o_item = cJSON_GetObjectItem(root, item_name); \ + if (o_item) \ + { \ + item_val = o_item->valueint; \ + } \ + else \ + { \ + item_val = 0; \ + TRACE_E("%s not found!", item_name); \ + } \ + } +// TRACE_B("%s: %d", item_name, item_val); + +#define CJSON_GET_VALUE_STRING(root, item_name, item_val) \ + { \ + cJSON *o_item = cJSON_GetObjectItem(root, item_name); \ + if (o_item && o_item->valuestring) \ + { \ + item_val = o_item->valuestring; \ + } \ + else \ + { \ + item_val = NULL; \ + TRACE_E("%s: NULL", item_name); \ + } \ + } +// TRACE_B("%s: %s", item_name, item_val ? item_val : ezlopi__str); + +#define CJSON_TRACE(name, object) \ + { \ + if (object) \ + { \ + char *obj_str = cJSON_Print(object); \ + if (obj_str) \ + { \ + TRACE_D("%s:\r\n%s", name ? name : ezlopi__str, obj_str); \ + free(obj_str); \ + } \ + } \ + } + +#define CJSON_GET_VALUE_STRING_BY_COPY(root, item_name, item_val) \ + { \ + char *tmp_item_val = NULL; \ + CJSON_GET_VALUE_STRING(root, item_name, tmp_item_val); \ + if (tmp_item_val) \ + { \ + snprintf(item_val, sizeof(item_val), "%s", tmp_item_val); \ + } \ + } + +#define ASSIGN_DEVICE_NAME(digital_io_device_properties, dev_name) \ + { \ + if ((NULL != dev_name) && ('\0' != dev_name[0])) \ + { \ + snprintf(digital_io_device_properties->ezlopi_cloud.device_name, \ + sizeof(digital_io_device_properties->ezlopi_cloud.device_name), \ + "%s", dev_name); \ + } \ + else \ + { \ + snprintf(digital_io_device_properties->ezlopi_cloud.device_name, \ + sizeof(digital_io_device_properties->ezlopi_cloud.device_name), \ + "dev-%d:digital_out", digital_io_device_properties->ezlopi_cloud.device_id); \ + } \ + } + +#define ASSIGN_DEVICE_NAME_V2(device, dev_name) \ + { \ + if ((NULL != dev_name) && ('\0' != dev_name[0])) \ + { \ + snprintf(device->cloud_properties.device_name, \ + sizeof(device->cloud_properties.device_name), \ + "%s", dev_name); \ + } \ + else \ + { \ + snprintf(device->cloud_properties.device_name, \ + sizeof(device->cloud_properties.device_name), \ + "device-%d:digital_io", device->cloud_properties.device_id); \ + } \ + } + +#define CJSON_GET_ID(id, cj_id) \ + { \ + if (cj_id && cj_id->valuestring) \ + { \ + id = strtoul(cj_id->valuestring, NULL, 16); \ + } \ + else \ + { \ + id = 0; \ + } \ + } + +#define CJSON_ASSIGN_ID(cj_object, id, id_str) \ + { \ + if (id && cj_object && id_str) \ + { \ + char tmp_str[32]; \ + snprintf(tmp_str, sizeof(tmp_str), "%08x", id); \ + cJSON_AddStringToObject(cj_object, id_str, tmp_str); \ + } \ + } + +#endif // __EZLOPI_CJSON_MACROS_H__ \ No newline at end of file diff --git a/ezlopi-core/ezlopi_devices/ezlopi_device_value_updated.c b/ezlopi-core/ezlopi_devices/ezlopi_device_value_updated.c index eb1d583e8..0c99974e5 100644 --- a/ezlopi-core/ezlopi_devices/ezlopi_device_value_updated.c +++ b/ezlopi-core/ezlopi_devices/ezlopi_device_value_updated.c @@ -22,8 +22,15 @@ int ezlopi_device_value_updated_from_device_v3(l_ezlopi_item_t *item) cJSON *cj_response = ezlopi_cloud_items_updated_from_devices_v3(curr_device, item); if (cj_response) { - ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_D); + char *data_to_send = cJSON_Print(cj_response); cJSON_Delete(cj_response); + if (data_to_send) + { + cJSON_Minify(data_to_send); + ret = web_provisioning_send_str_data_to_nma_websocket(data_to_send, TRACE_TYPE_D); + // ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_D); + free(data_to_send); + } } break; } @@ -53,8 +60,15 @@ int ezlopi_device_value_updated_from_device_item_id_v3(uint32_t item_id) cJSON *cj_response = ezlopi_cloud_items_updated_from_devices_v3(curr_device, curr_item); if (cj_response) { - ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_B); + char *data_to_send = cJSON_Print(cj_response); cJSON_Delete(cj_response); + if (data_to_send) + { + cJSON_Minify(data_to_send); + ret = web_provisioning_send_str_data_to_nma_websocket(data_to_send, TRACE_TYPE_D); + // ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_D); + free(data_to_send); + } } break; } @@ -84,8 +98,15 @@ int ezlopi_setting_value_updated_from_device_v3(l_ezlopi_device_settings_v3_t *s cJSON *cj_response = ezlopi_cloud_settings_updated_from_devices_v3(curr_device, setting); if (cj_response) { - ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_B); + char *data_to_send = cJSON_Print(cj_response); cJSON_Delete(cj_response); + if (data_to_send) + { + cJSON_Minify(data_to_send); + ret = web_provisioning_send_str_data_to_nma_websocket(data_to_send, TRACE_TYPE_D); + free(data_to_send); + // ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_B); + } } break; } @@ -115,8 +136,15 @@ int ezlopi_setting_value_updated_from_device_settings_id_v3(uint32_t setting_id) cJSON *cj_response = ezlopi_cloud_settings_updated_from_devices_v3(curr_device, curr_setting); if (cj_response) { - ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_B); + char *data_to_send = cJSON_Print(cj_response); cJSON_Delete(cj_response); + if (data_to_send) + { + cJSON_Minify(data_to_send); + ret = web_provisioning_send_str_data_to_nma_websocket(data_to_send, TRACE_TYPE_D); + free(data_to_send); + // ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_B); + } } break; } @@ -128,34 +156,3 @@ int ezlopi_setting_value_updated_from_device_settings_id_v3(uint32_t setting_id) return ret; } - -#if 0 // v2.x -int ezlopi_device_value_updated_from_device(s_ezlopi_device_properties_t *device_properties) -{ - int ret = 0; - - if (device_properties) - { - l_ezlopi_configured_devices_t *registered_devices = ezlopi_devices_list_get_configured_items(); - while (registered_devices) - { - if (NULL != registered_devices->properties) - { - if (registered_devices->properties == device_properties) - { - cJSON *cj_response = ezlopi_cloud_items_updated_from_devices(registered_devices); - if (cj_response) - { - ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_D); - cJSON_Delete(cj_response); - } - } - } - - registered_devices = registered_devices->next; - } - } - - return ret; -} -#endif diff --git a/ezlopi-core/ezlopi_devices/ezlopi_devices.c b/ezlopi-core/ezlopi_devices/ezlopi_devices.c index 6af93d4c1..2a9ce06d1 100644 --- a/ezlopi-core/ezlopi_devices/ezlopi_devices.c +++ b/ezlopi-core/ezlopi_devices/ezlopi_devices.c @@ -1,21 +1,85 @@ #include "ctype.h" - #include "cJSON.h" #include "items.h" #include "trace.h" + #include "ezlopi_nvs.h" #include "ezlopi_devices.h" -#include "web_provisioning.h" #include "ezlopi_factory_info.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" + +#include "web_provisioning.h" static l_ezlopi_device_t *l_device_head = NULL; +static volatile uint32_t g_store_dev_config_with_id = 0; static s_ezlopi_cloud_controller_t s_controller_information; -static void ezlopi_device_free(l_ezlopi_device_t *device); -static void ezlopi_device_parse_json_v3(char *config_string); +static void ezlopi_device_parse_json_v3(cJSON *cj_config); +static void ezlopi_device_free_single(l_ezlopi_device_t *device); static void ezlopi_device_print_controller_cloud_information_v3(void); +void ezlopi_device_name_set_by_device_id(uint32_t a_device_id, cJSON *cj_new_name) +{ + if (a_device_id && cj_new_name && cj_new_name->valuestring) + { + l_ezlopi_device_t *l_device_node = l_device_head; + while (l_device_node) + { + if (a_device_id == l_device_node->cloud_properties.device_id) + { + snprintf(l_device_node->cloud_properties.device_name, sizeof(l_device_node->cloud_properties.device_name), "%s", cj_new_name->valuestring); + break; + } + + l_device_node = l_device_node->next; + } + + char *device_config_str = ezlopi_factory_info_v3_get_ezlopi_config(); + if (device_config_str) + { + cJSON *cj_device_config = cJSON_Parse(device_config_str); + ezlopi_factory_info_v3_free(device_config_str); + + if (cj_device_config) + { + cJSON *cj_devices = cJSON_GetObjectItem(cj_device_config, ezlopi_dev_detail_str); + if (cj_devices) + { + uint32_t idx = 0; + cJSON *cj_device = NULL; + while (NULL != (cj_device = cJSON_GetArrayItem(cj_devices, idx))) + { + cJSON *cj_device_id = cJSON_GetObjectItem(cj_device, ezlopi_device_id_str); + if (cj_device_id && cj_device_id->valuestring) + { + uint32_t device_id = strtoul(cj_device_id->valuestring, NULL, 16); + if (device_id == a_device_id) + { + cJSON_DeleteItemFromObject(cj_device, ezlopi_dev_name_str); + cJSON_AddItemReferenceToObject(cj_device, ezlopi_dev_name_str, cj_new_name); + break; + } + } + + idx++; + } + } + + char *update_device_config = cJSON_Print(cj_device_config); + cJSON_Delete(cj_device_config); + if (update_device_config) + { + cJSON_Minify(update_device_config); + ezlopi_factory_info_v3_set_ezlopi_config(update_device_config); + free(update_device_config); + } + } + } + } +} + s_ezlopi_cloud_controller_t *ezlopi_device_get_controller_information(void) { return &s_controller_information; @@ -42,12 +106,48 @@ l_ezlopi_device_t *ezlopi_device_get_by_id(uint32_t device_id) return device_node; } -l_ezlopi_device_t *ezlopi_device_add_device(void) +l_ezlopi_device_t *ezlopi_device_add_device(cJSON *cj_device) { l_ezlopi_device_t *new_device = malloc(sizeof(l_ezlopi_device_t)); if (new_device) { memset(new_device, 0, sizeof(l_ezlopi_device_t)); + + char *device_name = NULL; + + CJSON_GET_VALUE_STRING(cj_device, ezlopi_dev_name_str, device_name); + ASSIGN_DEVICE_NAME_V2(new_device, device_name); + CJSON_GET_ID(new_device->cloud_properties.device_id, cJSON_GetObjectItem(cj_device, ezlopi_device_id_str)); + + TRACE_D("Device name: %s", device_name); + TRACE_D("Device Id (before): %08x", new_device->cloud_properties.device_id); + + if (new_device->cloud_properties.device_id) + { + l_ezlopi_device_t *curr_dev_node = l_device_head; + while (curr_dev_node) + { + if (curr_dev_node->cloud_properties.device_id == new_device->cloud_properties.device_id) + { + g_store_dev_config_with_id = 1; + new_device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + break; + } + + curr_dev_node = curr_dev_node->next; + } + + ezlopi_cloud_update_device_id(new_device->cloud_properties.device_id); + } + else + { + new_device->cloud_properties.device_id = ezlopi_cloud_generate_device_id(); + CJSON_ASSIGN_ID(cj_device, new_device->cloud_properties.device_id, ezlopi_device_id_str); + g_store_dev_config_with_id = 1; + } + + TRACE_D("Device Id (after): %08x", new_device->cloud_properties.device_id); + if (NULL == l_device_head) { l_device_head = new_device; @@ -69,34 +169,34 @@ l_ezlopi_device_t *ezlopi_device_add_device(void) void ezlopi_device_free_device(l_ezlopi_device_t *device) { - if (device) + if (device && l_device_head) { - if (l_device_head) + if (l_device_head == device) { - if (l_device_head == device) - { - ezlopi_device_free(l_device_head); - l_device_head = NULL; - } - else + l_device_head = l_device_head->next; + device->next = NULL; + + TRACE_D("Device-ID: %08x", device->cloud_properties.device_id); + ezlopi_device_free_single(device); + } + else + { + l_ezlopi_device_t *curr_device = l_device_head; + while (curr_device->next) { - l_ezlopi_device_t *curr_device = l_device_head; - while (curr_device->next) + TRACE_D("Device-ID: %08x", curr_device->next->cloud_properties.device_id); + if (curr_device->next == device) { - if (curr_device->next == device) - { - break; - } - - curr_device = curr_device->next; - } + TRACE_E("To free Device-ID: %08x", curr_device->next->cloud_properties.device_id); - if (curr_device->next) - { l_ezlopi_device_t *free_device = curr_device->next; curr_device->next = curr_device->next->next; - ezlopi_device_free(free_device); + free_device->next = NULL; + ezlopi_device_free_single(free_device); + break; } + + curr_device = curr_device->next; } } } @@ -117,6 +217,8 @@ void ezlopi_device_free_device_by_item(l_ezlopi_item_t *item) ezlopi_device_free_device(device_node); return; } + + item_node = item_node->next; } device_node = device_node->next; @@ -188,6 +290,7 @@ l_ezlopi_item_t *ezlopi_device_add_item_to_device(l_ezlopi_device_t *device, int void ezlopi_device_prepare(void) { s_controller_information.battery_powered = false; + snprintf(s_controller_information.device_type_id, sizeof(s_controller_information.device_type_id), ezlopi_ezlopi_str); s_controller_information.gateway_id[0] = '\0'; s_controller_information.parent_device_id[0] = '\0'; s_controller_information.persistent = true; @@ -208,10 +311,32 @@ void ezlopi_device_prepare(void) if (config_string) { - ezlopi_device_parse_json_v3(config_string); + TRACE_D("Initial config:\r\n%s", config_string); + cJSON *cj_config = cJSON_Parse(config_string); + ezlopi_factory_info_v3_free(config_string); + + if (cj_config) + { + ezlopi_device_parse_json_v3(cj_config); + + if (g_store_dev_config_with_id) + { + char *updated_config = cJSON_Print(cj_config); + if (updated_config) + { + TRACE_D("Updated config:\r\n%s", config_string); + cJSON_Minify(updated_config); + ezlopi_factory_info_v3_set_ezlopi_config(updated_config); + free(updated_config); + } + } + + cJSON_Delete(cj_config); + } } } +///////// Print functions start here //////////// static void ezlopi_device_print_controller_cloud_information_v3(void) { TRACE_B("Armed: %d", s_controller_information.armed); @@ -222,24 +347,24 @@ static void ezlopi_device_print_controller_cloud_information_v3(void) TRACE_B("Persistent: %d", s_controller_information.persistent); TRACE_B("Reachable: %d", s_controller_information.reachable); TRACE_B("Room Id: %.*s", sizeof(s_controller_information.room_id), s_controller_information.room_id); - TRACE_B("Security: %s", s_controller_information.security ? s_controller_information.security : "null"); + TRACE_B("Security: %s", s_controller_information.security ? s_controller_information.security : ezlopi_null_str); TRACE_B("Service Notification: %d", s_controller_information.service_notification); TRACE_B("Ready: %d", s_controller_information.ready); - TRACE_B("Status: %s", s_controller_information.status ? s_controller_information.status : "null"); + TRACE_B("Status: %s", s_controller_information.status ? s_controller_information.status : ezlopi_null_str); } static void ezlopi_device_print_interface_digital_io(l_ezlopi_item_t *item) { - TRACE_D(" |~~~|- item->interface.gpio.gpio_in.enable: %s", item->interface.gpio.gpio_in.enable ? "true" : "false"); + TRACE_D(" |~~~|- item->interface.gpio.gpio_in.enable: %s", item->interface.gpio.gpio_in.enable ? ezlopi_true_str : ezlopi_false_str); TRACE_D(" |~~~|- item->interface.gpio.gpio_in.gpio_num: %d", item->interface.gpio.gpio_in.gpio_num); - TRACE_D(" |~~~|- item->interface.gpio.gpio_in.invert: %s", item->interface.gpio.gpio_in.invert ? "true" : "false"); + TRACE_D(" |~~~|- item->interface.gpio.gpio_in.invert: %s", item->interface.gpio.gpio_in.invert ? ezlopi_true_str : ezlopi_false_str); TRACE_D(" |~~~|- item->interface.gpio.gpio_in.value: %d", item->interface.gpio.gpio_in.value); TRACE_D(" |~~~|- item->interface.gpio.gpio_in.pull: %d", item->interface.gpio.gpio_in.pull); TRACE_D(" |~~~|- item->interface.gpio.gpio_in.interrupt: %d", item->interface.gpio.gpio_in.interrupt); - TRACE_D(" |~~~|- item->interface.gpio.gpio_out.enable: %s", item->interface.gpio.gpio_out.enable ? "true" : "false"); + TRACE_D(" |~~~|- item->interface.gpio.gpio_out.enable: %s", item->interface.gpio.gpio_out.enable ? ezlopi_true_str : ezlopi_false_str); TRACE_D(" |~~~|- item->interface.gpio.gpio_out.gpio_num: %d", item->interface.gpio.gpio_out.gpio_num); - TRACE_D(" |~~~|- item->interface.gpio.gpio_out.invert: %s", item->interface.gpio.gpio_out.invert ? "true" : "false"); + TRACE_D(" |~~~|- item->interface.gpio.gpio_out.invert: %s", item->interface.gpio.gpio_out.invert ? ezlopi_true_str : ezlopi_false_str); TRACE_D(" |~~~|- item->interface.gpio.gpio_out.value: %d", item->interface.gpio.gpio_out.value); TRACE_D(" |~~~|- item->interface.gpio.gpio_out.pull: %d", item->interface.gpio.gpio_out.pull); TRACE_D(" |~~~|- item->interface.gpio.gpio_in.interrupt: %d", item->interface.gpio.gpio_in.interrupt); @@ -274,7 +399,7 @@ static void ezlopi_device_print_interface_uart(l_ezlopi_item_t *item) static void ezlopi_device_print_interface_i2c_master(l_ezlopi_item_t *item) { - TRACE_D("|~~~|- item->interface.i2c_master.enable: %s", item->interface.i2c_master.enable ? "true" : "false"); + TRACE_D("|~~~|- item->interface.i2c_master.enable: %s", item->interface.i2c_master.enable ? ezlopi_true_str : ezlopi_false_str); TRACE_D("|~~~|- item->interface.i2c_master.channel: %d", item->interface.i2c_master.channel); TRACE_D("|~~~|- item->interface.i2c_master.clock_speed: %d", item->interface.i2c_master.clock_speed); TRACE_D("|~~~|- item->interface.i2c_master.scl: %d", item->interface.i2c_master.scl); @@ -355,56 +480,47 @@ static void ezlopi_device_print_interface_type(l_ezlopi_item_t *item) } } } +//////////////////// Print functions end here ///////////////////////// +/////////////////////////////////////////////////////////////////////// -static void ezlopi_device_parse_json_v3(char *config_string) +static void ezlopi_device_parse_json_v3(cJSON *cjson_config) { - TRACE_I("PARSING - config_string: \n%s", config_string); - cJSON *cjson_config = cJSON_Parse(config_string); + cJSON *cjson_device_list = cJSON_GetObjectItem(cjson_config, ezlopi_dev_detail_str); - if (cjson_config) + if (cjson_device_list) { - cJSON *cjson_device_list = cJSON_GetObjectItem(cjson_config, "dev_detail"); - if (cjson_device_list) + int config_dev_idx = 0; + cJSON *cjson_device = NULL; + + TRACE_B("---------------------------------------------"); + while (NULL != (cjson_device = cJSON_GetArrayItem(cjson_device_list, config_dev_idx))) { - int config_dev_idx = 0; - cJSON *cjson_device = NULL; + TRACE_B("Device-%d:", config_dev_idx); - TRACE_B("---------------------------------------------"); - while (NULL != (cjson_device = cJSON_GetArrayItem(cjson_device_list, config_dev_idx))) + int id_item = 0; + CJSON_GET_VALUE_INT(cjson_device, ezlopi_id_item_str, id_item); + + if (0 != id_item) { - TRACE_B("Device-%d:", config_dev_idx); - int id_item = 0; - CJSON_GET_VALUE_INT(cjson_device, "id_item", id_item); + s_ezlopi_device_v3_t *v3_sensor_list = ezlopi_devices_list_get_list_v3(); + int dev_idx = 0; - if (0 != id_item) + while (NULL != v3_sensor_list[dev_idx].func) { - - s_ezlopi_device_v3_t *v3_sensor_list = ezlopi_devices_list_get_list_v3(); - int dev_idx = 0; - - while (NULL != v3_sensor_list[dev_idx].func) + if (id_item == v3_sensor_list[dev_idx].id) { - if (id_item == v3_sensor_list[dev_idx].id) - { - s_ezlopi_prep_arg_t device_prep_arg = {.device = &v3_sensor_list[dev_idx], .cjson_device = cjson_device}; - v3_sensor_list[dev_idx].func(EZLOPI_ACTION_PREPARE, NULL, (void *)&device_prep_arg, NULL); - } - - dev_idx++; + s_ezlopi_prep_arg_t device_prep_arg = {.device = &v3_sensor_list[dev_idx], .cjson_device = cjson_device}; + v3_sensor_list[dev_idx].func(EZLOPI_ACTION_PREPARE, NULL, (void *)&device_prep_arg, NULL); } - } - config_dev_idx++; - TRACE_B("---------------------------------------------"); + dev_idx++; + } } - } - cJSON_Delete(cjson_config); - } - else - { - TRACE_E("EZLOPI-CONFIG parse- failed!"); + config_dev_idx++; + TRACE_B("---------------------------------------------"); + } } // ezlopi_device_print_controller_cloud_information_v3(); @@ -415,12 +531,12 @@ static void ezlopi_device_parse_json_v3(char *config_string) { TRACE_D("|~~~~~~~~~~~~~~~~ Device - %d ~~~~~~~~~~~~~~~~|", device_count + 1); // TRACE_D("|- Device Pointer: %p", tm_device_l_list); - TRACE_D("|- Name: %.*s", 32, isprint(tm_device_l_list->cloud_properties.device_name[0]) ? tm_device_l_list->cloud_properties.device_name : "null"); + TRACE_D("|- Name: %.*s", 32, isprint(tm_device_l_list->cloud_properties.device_name[0]) ? tm_device_l_list->cloud_properties.device_name : ezlopi_null_str); TRACE_D("|- Id: %08X", tm_device_l_list->cloud_properties.device_id); TRACE_D("|- Parent Device ID: %08X", tm_device_l_list->cloud_properties.parent_device_id); - TRACE_D("|- Category: %s", tm_device_l_list->cloud_properties.category ? tm_device_l_list->cloud_properties.category : "null"); - TRACE_D("|- Sub-category: %s", tm_device_l_list->cloud_properties.subcategory ? tm_device_l_list->cloud_properties.subcategory : "null"); - TRACE_D("|- Device-type: %s", tm_device_l_list->cloud_properties.device_type ? tm_device_l_list->cloud_properties.device_type : "null"); + TRACE_D("|- Category: %s", tm_device_l_list->cloud_properties.category ? tm_device_l_list->cloud_properties.category : ezlopi_null_str); + TRACE_D("|- Sub-category: %s", tm_device_l_list->cloud_properties.subcategory ? tm_device_l_list->cloud_properties.subcategory : ezlopi_null_str); + TRACE_D("|- Device-type: %s", tm_device_l_list->cloud_properties.device_type ? tm_device_l_list->cloud_properties.device_type : ezlopi_null_str); int item_count = 0; l_ezlopi_item_t *tm_itme_l_list = tm_device_l_list->items; @@ -428,9 +544,9 @@ static void ezlopi_device_parse_json_v3(char *config_string) { TRACE_D("|~~~|--------------- Item - %d ---------------|", item_count + 1); TRACE_D("|~~~|- Id: %08X", tm_itme_l_list->cloud_properties.item_id); - TRACE_D("|~~~|- Category: %s", tm_itme_l_list->cloud_properties.item_name ? tm_itme_l_list->cloud_properties.item_name : "null"); - TRACE_D("|~~~|- Value: %s", tm_itme_l_list->cloud_properties.value_type ? tm_itme_l_list->cloud_properties.value_type : "null"); - TRACE_D("|~~~|- Device-type: %.*s", 32, tm_device_l_list->cloud_properties.device_type ? tm_device_l_list->cloud_properties.device_type : "null"); + TRACE_D("|~~~|- Category: %s", tm_itme_l_list->cloud_properties.item_name ? tm_itme_l_list->cloud_properties.item_name : ezlopi_null_str); + // TRACE_D("|~~~|- Value: %s", tm_itme_l_list->cloud_properties.value_type ? tm_itme_l_list->cloud_properties.value_type : "null"); + // TRACE_D("|~~~|- Device-type: %.*s", 32, tm_device_l_list->cloud_properties.device_type ? tm_device_l_list->cloud_properties.device_type : "null"); TRACE_D("|~~~|- Interface-type: %d", tm_itme_l_list->interface_type); ezlopi_device_print_interface_type(tm_itme_l_list); @@ -472,16 +588,18 @@ static void ezlopi_device_free_item(l_ezlopi_item_t *items) // { // ezlopi_device_free_item(settings->next); // } - +// // free(settings); // } -static void ezlopi_device_free(l_ezlopi_device_t *device) +static void ezlopi_device_free_single(l_ezlopi_device_t *device) { if (device->items) { ezlopi_device_free_item(device->items); + device->items = NULL; } + free(device); } @@ -527,14 +645,14 @@ cJSON *ezlopi_device_create_device_table_from_prop(l_ezlopi_device_t *device_pro { char tmp_string[64]; snprintf(tmp_string, sizeof(tmp_string), "%08x", device_prop->cloud_properties.device_id); - cJSON_AddStringToObject(cj_device, "_id", tmp_string); + cJSON_AddStringToObject(cj_device, ezlopi__id_str, tmp_string); if (device_prop->cloud_properties.device_type_id) { - cJSON_AddStringToObject(cj_device, "deviceTypeId", device_prop->cloud_properties.device_type_id); + cJSON_AddStringToObject(cj_device, ezlopi_deviceTypeId_str, device_prop->cloud_properties.device_type_id); } else { - cJSON_AddStringToObject(cj_device, "deviceTypeId", "ezlopi"); + cJSON_AddStringToObject(cj_device, ezlopi_deviceTypeId_str, ezlopi_ezlopi_str); } if (device_prop->cloud_properties.parent_device_id >= DEVICE_ID_START) @@ -545,25 +663,25 @@ cJSON *ezlopi_device_create_device_table_from_prop(l_ezlopi_device_t *device_pro { tmp_string[0] = '\0'; } - cJSON_AddStringToObject(cj_device, "parentDeviceId", tmp_string); - cJSON_AddStringToObject(cj_device, "category", device_prop->cloud_properties.category); - cJSON_AddStringToObject(cj_device, "subcategory", device_prop->cloud_properties.subcategory); + cJSON_AddStringToObject(cj_device, ezlopi_parentDeviceId_str, tmp_string); + cJSON_AddStringToObject(cj_device, ezlopi_category_str, device_prop->cloud_properties.category); + cJSON_AddStringToObject(cj_device, ezlopi_subcategory_str, device_prop->cloud_properties.subcategory); snprintf(tmp_string, sizeof(tmp_string), "%08x", ezlopi_cloud_generate_gateway_id()); - cJSON_AddStringToObject(cj_device, "gatewayId", tmp_string); - cJSON_AddBoolToObject(cj_device, "batteryPowered", false); - cJSON_AddStringToObject(cj_device, "name", device_prop->cloud_properties.device_name); - cJSON_AddStringToObject(cj_device, "type", device_prop->cloud_properties.device_type); - cJSON_AddBoolToObject(cj_device, "reachable", true); - cJSON_AddBoolToObject(cj_device, "persistent", true); - cJSON_AddBoolToObject(cj_device, "serviceNotification", false); + cJSON_AddStringToObject(cj_device, ezlopi_gatewayId_str, tmp_string); + cJSON_AddBoolToObject(cj_device, ezlopi_batteryPowered_str, false); + cJSON_AddStringToObject(cj_device, ezlopi_name_str, device_prop->cloud_properties.device_name); + cJSON_AddStringToObject(cj_device, ezlopi_type_str, device_prop->cloud_properties.device_type); + cJSON_AddBoolToObject(cj_device, ezlopi_reachable_str, true); + cJSON_AddBoolToObject(cj_device, ezlopi_persistent_str, true); + cJSON_AddBoolToObject(cj_device, ezlopi_serviceNotification_str, false); // cJSON_AddBoolToObject(cj_device, "armed", false); - cJSON_AddStringToObject(cj_device, "roomId", ""); - cJSON_AddStringToObject(cj_device, "security", "no"); - cJSON_AddBoolToObject(cj_device, "ready", true); - cJSON_AddStringToObject(cj_device, "status", "idle"); + cJSON_AddStringToObject(cj_device, ezlopi_roomId_str, ezlopi__str); + cJSON_AddStringToObject(cj_device, ezlopi_security_str, ezlopi_no_str); + cJSON_AddBoolToObject(cj_device, ezlopi_ready_str, true); + cJSON_AddStringToObject(cj_device, ezlopi_status_str, ezlopi_idle_str); if (NULL != device_prop->cloud_properties.info) { - cJSON_AddItemReferenceToObject(cj_device, "info", device_prop->cloud_properties.info); + cJSON_AddItemReferenceToObject(cj_device, ezlopi_info_str, device_prop->cloud_properties.info); } } } diff --git a/ezlopi-core/ezlopi_devices/ezlopi_devices.h b/ezlopi-core/ezlopi_devices/ezlopi_devices.h index 77ed83bf4..e8ab84972 100644 --- a/ezlopi-core/ezlopi_devices/ezlopi_devices.h +++ b/ezlopi-core/ezlopi_devices/ezlopi_devices.h @@ -172,12 +172,13 @@ typedef struct l_ezlopi_item typedef struct l_ezlopi_device_settings_v3 { - s_ezlopi_cloud_device_settings_t cloud_properties; void *user_arg; + s_ezlopi_cloud_device_settings_t cloud_properties; int (*func)(e_ezlopi_settings_action_t action, struct l_ezlopi_device_settings_v3 *setting, void *arg, void *user_arg); struct l_ezlopi_device_settings_v3 *next; } l_ezlopi_device_settings_v3_t; + typedef struct l_ezlopi_device { l_ezlopi_item_t *items; @@ -189,7 +190,7 @@ typedef struct l_ezlopi_device void ezlopi_device_prepare(void); l_ezlopi_device_t *ezlopi_device_get_head(void); -l_ezlopi_device_t *ezlopi_device_add_device(void); +l_ezlopi_device_t *ezlopi_device_add_device(cJSON *cj_device); l_ezlopi_device_t *ezlopi_device_get_by_id(uint32_t device_id); l_ezlopi_item_t *ezlopi_device_get_item_by_id(uint32_t item_id); @@ -206,5 +207,6 @@ void ezlopi_device_free_device(l_ezlopi_device_t *device); void ezlopi_device_free_device_by_item(l_ezlopi_item_t *item); cJSON *ezlopi_device_create_device_table_from_prop(l_ezlopi_device_t *device_prop); s_ezlopi_cloud_controller_t *ezlopi_device_get_controller_information(void); +void ezlopi_device_name_set_by_device_id(uint32_t device_id, cJSON *cj_new_name); #endif // __EZLOPI_DEVICE_H__ diff --git a/ezlopi-core/ezlopi_event_group/ezlopi_event_group.c b/ezlopi-core/ezlopi_event_group/ezlopi_event_group.c index 027466938..fa62e1122 100644 --- a/ezlopi-core/ezlopi_event_group/ezlopi_event_group.c +++ b/ezlopi-core/ezlopi_event_group/ezlopi_event_group.c @@ -38,8 +38,8 @@ int ezlopi_event_group_wait_for_event(e_ezlopi_event_t event, uint32_t wait_time int ret = 0; if (ezlopi_event_group_handle) { - EventBits_t event_bit = xEventGroupWaitBits(ezlopi_event_group_handle, event, clear_on_exit ? pdTRUE : pdFALSE, pdFALSE, ((wait_time_ms == portMAX_DELAY) ? portMAX_DELAY : (wait_time_ms / portTICK_RATE_MS))); - if (event_bit & event) + EventBits_t event_bits = xEventGroupWaitBits(ezlopi_event_group_handle, event, clear_on_exit ? pdTRUE : pdFALSE, pdFALSE, ((wait_time_ms == portMAX_DELAY) ? portMAX_DELAY : (wait_time_ms / portTICK_RATE_MS))); + if (event_bits & event) { ret = 1; } diff --git a/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.c b/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.c index c75061c31..2f475dd6f 100644 --- a/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.c +++ b/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.c @@ -8,8 +8,10 @@ #include "freertos/task.h" #include "trace.h" -#include "ezlopi_factory_info.h" + #include "ezlopi_nvs.h" +#include "ezlopi_factory_info.h" +#include "ezlopi_cloud_constants.h" #if (ID_BIN_VERSION_2 == ID_BIN_VERSION) static char *g_ca_certificate = NULL; @@ -126,6 +128,7 @@ void ezlopi_factory_info_v3_free(void *arg) if (arg) { free(arg); + arg = NULL; } } @@ -948,6 +951,14 @@ int ezlopi_factory_info_v3_factory_reset(void) static int ezlopi_factory_info_v2_set_4kb(char *data, uint32_t offset); static char *ezlopi_factory_info_v2_read_string(e_ezlopi_factory_info_v2_offset_t offset, e_ezlopi_factory_info_v2_length_t length); +#define UPDATE_STRING_VALUE(buffer, data, offset, length) \ + { \ + if (data) \ + { \ + snprintf(buffer + offset, length, "%s", data); \ + } \ + } + const esp_partition_t *ezlopi_factory_info_v2_init(void) { if (NULL == partition_ctx_v2) @@ -988,24 +999,21 @@ void print_factory_info_v2(void) char *ca_certificate = ezlopi_factory_info_v2_get_ca_certificate(); char *ssl_private_key = ezlopi_factory_info_v2_get_ssl_private_key(); char *ssl_shared_key = ezlopi_factory_info_v2_get_ssl_shared_key(); -#if (ID_BIN_VERSION_2 == ID_BIN_VERSION) + char *ezlopi_config = ezlopi_factory_info_v2_get_ezlopi_config(); -#elif (ID_BIN_VERSION_1 == ID_BIN_VERSION) - char *ezlopi_config = ezlopi_nvs_read_config_data_str(); -#endif TRACE_D("----------------- Factory Info -----------------"); // TRACE_W("VERSION[off: 0x%04X, size: 0x%04X]: %d", VERSION_OFFSET, VERSION_LENGTH, version); TRACE_W("SERIAL-ID [off: 0x%04X, size: 0x%04X]: %llu", ID_OFFSET, ID_LENGTH, id); // TRACE_W("MAC [off: 0x%04X, size: 0x%04X]: %02X:%02X:%02X:%02X:%02X:%02X", DEVICE_MAC_OFFSET, DEVICE_MAC_LENGTH, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - TRACE_W("NAME [off: 0x%04X, size: 0x%04X]: %s", NAME_OFFSET, NAME_LENGTH, name ? name : "null"); - // TRACE_W("MANUFACTURER [off: 0x%04X, size: 0x%04X]: %s", MANUFACTURER_OFFSET, MANUFACTURER_LENGTH, manufacturer ? manufacturer : "null"); - // TRACE_W("BRAND [off: 0x%04X, size: 0x%04X]: %s", BRAND_OFFSET, BRAND_LENGTH, brand ? brand : "null"); - // TRACE_W("MODEL [off: 0x%04X, size: 0x%04X]: %s", MODEL_OFFSET, MODEL_LENGTH, model ? model : "null"); - TRACE_W("DEVICE_UUID [off: 0x%04X, size: 0x%04X]: %s", DEVICE_UUID_OFFSET, DEVICE_UUID_LENGTH, device_uuid ? device_uuid : "null"); - // TRACE_W("PROVISIONING_UUID [off: 0x%04X, size: 0x%04X]: %s", PROVISIONING_UUID_OFFSET, PROVISIONING_UUID_LENGTH, provisioning_uuid ? provisioning_uuid : "null"); - TRACE_W("WIFI-SSID [off: 0x%04X, size: 0x%04X]: %s", SSID_OFFSET, SSID_LENGTH, wifi_ssid ? wifi_ssid : "null"); - // TRACE_W("WIFI-PASSWORD [off: 0x%04X, size: 0x%04X]: %s", PASSWORD_OFFSET, PASSWORD_LENGTH, wifi_password ? wifi_password : "null"); + TRACE_W("NAME [off: 0x%04X, size: 0x%04X]: %s", NAME_OFFSET, NAME_LENGTH, name ? name : ezlopi_null_str); + // TRACE_W("MANUFACTURER [off: 0x%04X, size: 0x%04X]: %s", MANUFACTURER_OFFSET, MANUFACTURER_LENGTH, manufacturer ? manufacturer : ezlopi_null_str); + // TRACE_W("BRAND [off: 0x%04X, size: 0x%04X]: %s", BRAND_OFFSET, BRAND_LENGTH, brand ? brand : ezlopi_null_str); + // TRACE_W("MODEL [off: 0x%04X, size: 0x%04X]: %s", MODEL_OFFSET, MODEL_LENGTH, model ? model : ezlopi_null_str); + TRACE_W("DEVICE_UUID [off: 0x%04X, size: 0x%04X]: %s", DEVICE_UUID_OFFSET, DEVICE_UUID_LENGTH, device_uuid ? device_uuid : ezlopi_null_str); + // TRACE_W("PROVISIONING_UUID [off: 0x%04X, size: 0x%04X]: %s", PROVISIONING_UUID_OFFSET, PROVISIONING_UUID_LENGTH, provisioning_uuid ? provisioning_uuid : ezlopi_null_str); + TRACE_W("WIFI-SSID [off: 0x%04X, size: 0x%04X]: %s", SSID_OFFSET, SSID_LENGTH, wifi_ssid ? wifi_ssid : ezlopi_null_str); + // TRACE_W("WIFI-PASSWORD [off: 0x%04X, size: 0x%04X]: %s", PASSWORD_OFFSET, PASSWORD_LENGTH, wifi_password ? wifi_password : ezlopi_null_str); // TRACE_W("CLOUD_SERVER [off: 0x%04X, size: 0x%04X]: %s", CLOUD_SERVER_OFFSET, CLOUD_SERVER_LENGTH, cloud_server); // TRACE_W("DEVICE_TYPE [off: 0x%04X, size: 0x%04X]: %s", DEVICE_TYPE_OFFSET, DEVICE_TYPE_LENGTH, device_type); // TRACE_W("CA_CERTIFICATE [off: 0x%04X, size: 0x%04X]: %s", CA_CERTIFICATE_OFFSET, CA_CERTIFICATE_LENGTH, ca_certificate); @@ -1014,7 +1022,7 @@ void print_factory_info_v2(void) #if (ID_BIN_VERSION_2 == ID_BIN_VERSION) TRACE_W("EZLOPI_CONFIG [off: 0x%04X, size: 0x%04X]: %s", EZLOPI_CONFIG_OFFSET, EZLOPI_CONFIG_LENGTH, ezlopi_config); #elif (ID_BIN_VERSION_1 == ID_BIN_VERSION) - // TRACE_W("EZLOPI_CONFIG: %s", ezlopi_config ? ezlopi_config : ""); + // TRACE_W("EZLOPI_CONFIG: %s", ezlopi_config ? ezlopi_config : ezlopi__str); #endif TRACE_D("-------------------------------------------------"); @@ -1027,7 +1035,7 @@ void print_factory_info_v2(void) ezlopi_factory_info_v2_free(wifi_ssid); ezlopi_factory_info_v2_free(wifi_password); ezlopi_factory_info_v2_free(cloud_server); - // ezlopi_factory_info_v2_free(device_type); + ezlopi_factory_info_v2_free_ezlopi_config(); } /** Getter */ @@ -1263,131 +1271,6 @@ char *ezlopi_factory_info_v2_get_ezlopi_config(void) return NULL; } -#if 0 -/** Setter */ -uint16_t ezlopi_factory_info_v2_set_version(void) -{ - uint16_t _version = 0ULL; - - if (ezlopi_factory_info_v2_init()) - { - uint8_t tmp_version_arr[2]; - memset(tmp_version_arr, 0, 2); - esp_partition_read(partition_ctx_v2, VERSION_OFFSET, &tmp_version_arr, VERSION_LENGTH); - - for (int i = 0; i < 8; i++) - { - _version |= tmp_version_arr[i] << (i * 8); - } - } - - return _version; -} - -char *ezlopi_factory_info_v2_set_name(void) -{ - return ezlopi_factory_info_v2_read_string(NAME_OFFSET, NAME_LENGTH); -} - -char *ezlopi_factory_info_v2_set_manufacturer(void) -{ - return ezlopi_factory_info_v2_read_string(MANUFACTURER_OFFSET, MANUFACTURER_LENGTH); -} - -char *ezlopi_factory_info_v2_set_brand(void) -{ - return ezlopi_factory_info_v2_read_string(BRAND_OFFSET, BRAND_LENGTH); -} - -char *ezlopi_factory_info_v2_set_model(void) -{ - return ezlopi_factory_info_v2_read_string(MODEL_OFFSET, MODEL_LENGTH); -} - -unsigned long long ezlopi_factory_info_v2_set_id(void) -{ - unsigned long long _id = 0ULL; - - if (ezlopi_factory_info_v2_init()) - { - uint8_t tmp_id_arr[8]; - memset(tmp_id_arr, 0, 8); - esp_partition_read(partition_ctx_v2, ID_OFFSET, &tmp_id_arr, ID_LENGTH); - - for (int i = 0; i < 8; i++) - { - _id |= tmp_id_arr[i] << (i * 8); - } - } - - return _id; -} - -char *ezlopi_factory_info_v2_set_device_uuid(void) -{ - return ezlopi_factory_info_v2_read_string(DEVICE_UUID_OFFSET, DEVICE_UUID_LENGTH); -} - -char *ezlopi_factory_info_v2_set_provisioning_uuid(void) -{ - return ezlopi_factory_info_v2_read_string(PROVISIONING_UUID_OFFSET, PROVISIONING_UUID_LENGTH); -} - -char *ezlopi_factory_info_v2_set_ssid(void) -{ - return ezlopi_factory_info_v2_read_string(SSID_OFFSET, SSID_LENGTH); -} - -char *ezlopi_factory_info_v2_set_password(void) -{ - return ezlopi_factory_info_v2_read_string(PASSWORD_OFFSET, PASSWORD_LENGTH); -} - -uint8_t *ezlopi_factory_info_v2_set_ezlopi_mac(void) -{ - uint8_t *tmp_mac_arr = malloc(DEVICE_MAC_LENGTH); - - if (tmp_mac_arr) - { - if (ezlopi_factory_info_v2_init()) - { - memset(tmp_mac_arr, 0, DEVICE_MAC_LENGTH); - esp_partition_read(partition_ctx_v2, DEVICE_MAC_OFFSET, &tmp_mac_arr, DEVICE_MAC_LENGTH); - } - } - - return tmp_mac_arr; -} - -char *ezlopi_factory_info_v2_set_cloud_server(void) -{ - return ezlopi_factory_info_v2_read_string(CLOUD_SERVER_OFFSET, CLOUD_SERVER_LENGTH); -} - -char *ezlopi_factory_info_v2_set_ca_certificate(void) -{ - return ezlopi_factory_info_v2_read_string(CA_CERTIFICATE_OFFSET, CA_CERTIFICATE_LENGTH); -} - -char *ezlopi_factory_info_v2_set_ssl_private_key(void) -{ - return ezlopi_factory_info_v2_read_string(SSL_PRIVATE_KEY_OFFSET, SSL_PRIVATE_KEY_LENGTH); -} - -char *ezlopi_factory_info_v2_set_ssl_shared_key(void) -{ - return ezlopi_factory_info_v2_read_string(SSL_SHARED_KEY_OFFSET, SSL_SHARED_KEY_LENGTH); -} -#endif - -#define UPDATE_STRING_VALUE(buffer, data, offset, length) \ - { \ - if (data) \ - { \ - snprintf(buffer + offset, length, "%s", data); \ - } \ - } - int ezlopi_factory_info_v2_set_basic(s_basic_factory_info_t *ezlopi_config_basic) { int ret = 0; @@ -1589,57 +1472,61 @@ int ezlopi_factory_info_v2_set_wifi(char *ssid, char *password) int ezlopi_factory_info_v2_set_ssl_private_key(char *data) { - return ezlopi_factory_info_v2_set_4kb(data, 0x4000); + return ezlopi_factory_info_v2_set_4kb(ezlopi_ssl_private_key_str, data, SSL_PRIVATE_KEY_OFFSET); } int ezlopi_factory_info_v2_set_ssl_public_key(char *data) { - return ezlopi_factory_info_v2_set_4kb(data, 0x4000); + // return ezlopi_factory_info_v2_set_4kb("ssl-public-key", data, 0x4000); + return 0; } int ezlopi_factory_info_v2_set_ssl_shared_key(char *data) { - return ezlopi_factory_info_v2_set_4kb(data, 0x5000); + return ezlopi_factory_info_v2_set_4kb("ssl-shared-key", data, SSL_SHARED_KEY_OFFSET); } int ezlopi_factory_info_v2_set_ca_cert(char *data) { - return ezlopi_factory_info_v2_set_4kb(data, 0x3000); + return ezlopi_factory_info_v2_set_4kb("ca-cert", data, CA_CERTIFICATE_OFFSET); } int ezlopi_factory_info_v2_set_ezlopi_config(char *data) { - int ret = ezlopi_factory_info_v2_set_4kb(data, 0x1000); - if (ret) + int ret = ezlopi_factory_info_v2_set_4kb("ezlopi-config", data, EZLOPI_CONFIG_OFFSET); + if (1 == ret) { - free(g_ezlopi_config); - g_ezlopi_config = NULL; - ezlopi_factory_info_v2_get_ezlopi_config(); + ezlopi_factory_info_v2_free_ezlopi_config(); } return ret; } -static int ezlopi_factory_info_v2_set_4kb(char *data, uint32_t offset) +static int ezlopi_factory_info_v2_set_4kb(char *name, char *data, uint32_t offset) { int ret = 0; if (data) { - if (ESP_OK == esp_partition_erase_range(partition_ctx_v2, offset, 0x1000)) + if (ESP_OK == (ret = esp_partition_erase_range(partition_ctx_v2, offset, 0x1000))) { - if (ESP_OK == esp_partition_write(partition_ctx_v2, offset, data, strlen(data) + 1)) + if (ESP_OK == (ret = esp_partition_write(partition_ctx_v2, offset, data, strlen(data) + 1))) { - TRACE_I("Flash write succeessful"); + char *updated_data_from_flash = ezlopi_factory_info_v2_read_string(offset, 0x1000); + if (updated_data_from_flash) + { + free(updated_data_from_flash); + } + ret = 1; } else { - TRACE_E("esp-partition write failed!"); + TRACE_E("%s: esp-partition write failed!", name ? name : ezlopi__str); } } else { - TRACE_E("esp-partition erase failed!"); + TRACE_E("%s: esp-partition erase failed!", name ? name : ezlopi__str); } } @@ -1661,6 +1548,16 @@ int ezlopi_factory_info_v2_factory_reset(void) return ret; } +/** Free **/ +void ezlopi_factory_info_v2_free_ezlopi_config(void) +{ + if (g_ezlopi_config) + { + free(g_ezlopi_config); + g_ezlopi_config = NULL; + } +} + /** Reader */ static char *ezlopi_factory_info_v2_read_string(e_ezlopi_factory_info_v2_offset_t offset, e_ezlopi_factory_info_v2_length_t length) { diff --git a/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.h b/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.h index 4c3ad56a7..443fcfe8f 100644 --- a/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.h +++ b/ezlopi-core/ezlopi_factory_info/ezlopi_factory_info.h @@ -126,7 +126,7 @@ typedef enum e_ezlopi_factory_info_v2_offset PROVISIONING_SERVER_OFFSET = 0X000 + 0X14, PROVISIONING_TOKEN_OFFSET = 0X000 + 0X114, PROVISIONING_UUID_OFFSET = 0x0000 + 0x0314, - CONFIG_VERSION_OFFSET = 0X000 + 0x02, + CONFIG_VERSION_OFFSET = 0X0000 + 0x02, CA_CERTIFICATE_OFFSET = 0x0000 + 0x3000, SSL_PRIVATE_KEY_OFFSET = 0x0000 + 0x4000, SSL_SHARED_KEY_OFFSET = 0x0000 + 0x5000, @@ -354,7 +354,10 @@ static const char *ezlopi_config_test = int ezlopi_factory_info_v2_set_ssl_public_key(char *data); int ezlopi_factory_info_v2_set_ssl_private_key(char *data); + /** Functions to free **/ void ezlopi_factory_info_v2_free(void *arg); + void ezlopi_factory_info_v2_free_ezlopi_config(void); + int ezlopi_factory_info_v2_factory_reset(void); #endif // v2 diff --git a/ezlopi-core/ezlopi_gateway/CMakeLists.txt b/ezlopi-core/ezlopi_gateway/CMakeLists.txt index c10e28501..244e340be 100644 --- a/ezlopi-core/ezlopi_gateway/CMakeLists.txt +++ b/ezlopi-core/ezlopi_gateway/CMakeLists.txt @@ -4,4 +4,4 @@ file(GLOB_RECURSE config_src "*.c") idf_component_register(SRCS "ezlopi_gateway.c" "${config_src}" INCLUDE_DIRS "." - REQUIRES trace) + REQUIRES trace ezlopi_cloud_constants) diff --git a/ezlopi-core/ezlopi_gateway/ezlopi_gateway.c b/ezlopi-core/ezlopi_gateway/ezlopi_gateway.c index fdc468d23..20c70630c 100644 --- a/ezlopi-core/ezlopi_gateway/ezlopi_gateway.c +++ b/ezlopi-core/ezlopi_gateway/ezlopi_gateway.c @@ -4,6 +4,7 @@ #include #include "ezlopi_gateway.h" +#include "ezlopi_cloud_constants.h" static s_ezlopi_gateway_t ezlopi_gateway = { .ready = true, diff --git a/ezlopi-core/ezlopi_hal/ezlopi_uart.c b/ezlopi-core/ezlopi_hal/ezlopi_uart.c index fcb244ea0..1e9694eee 100644 --- a/ezlopi-core/ezlopi_hal/ezlopi_uart.c +++ b/ezlopi-core/ezlopi_hal/ezlopi_uart.c @@ -81,7 +81,7 @@ static ezlo_uart_channel_t get_available_channel() static void ezlopi_uart_channel_task(void *args) { uart_event_t event; - uint8_t *buffer = (uint8_t *)malloc(256); + uint8_t *buffer = NULL; // s_ezlopi_uart_object_t *ezlopi_uart_object = (s_ezlopi_uart_object_t*)args; s_ezlopi_uart_object_handle_t ezlopi_uart_object = (s_ezlopi_uart_object_handle_t)args; @@ -93,12 +93,24 @@ static void ezlopi_uart_channel_task(void *args) // Start reveceiving UART events for first channel. if (xQueueReceive(ezlopi_uart_object->ezlopi_uart_queue_handle, (void *)&event, portMAX_DELAY)) { - memset(buffer, 0, 256); switch (event.type) { case UART_DATA: { - data_len = uart_read_bytes(ezlopi_uart_object->ezlopi_uart.channel, buffer, event.size, 100 / portTICK_PERIOD_MS); + uint32_t bufferred_data_len = 0; + esp_err_t error = uart_get_buffered_data_len(ezlopi_uart_object->ezlopi_uart.channel, &bufferred_data_len); + if (bufferred_data_len) + { + buffer = malloc(bufferred_data_len); + if (buffer) + { + data_len = uart_read_bytes(ezlopi_uart_object->ezlopi_uart.channel, buffer, event.size, 100 / portTICK_PERIOD_MS); + } + else + { + uart_flush_input(ezlopi_uart_object->ezlopi_uart.channel); + } + } break; } case UART_BREAK: @@ -118,6 +130,11 @@ static void ezlopi_uart_channel_task(void *args) } ezlopi_uart_object->upcall(buffer, data_len, ezlopi_uart_object); + if (buffer) + { + free(buffer); + buffer = NULL; + } data_len = 0; } } diff --git a/ezlopi-core/ezlopi_hal/ezlopi_uart.h b/ezlopi-core/ezlopi_hal/ezlopi_uart.h index a60fcdcf9..fc674a1b2 100644 --- a/ezlopi-core/ezlopi_hal/ezlopi_uart.h +++ b/ezlopi-core/ezlopi_hal/ezlopi_uart.h @@ -8,13 +8,19 @@ #include "freertos/FreeRTOS.h" #include "freertos/queue.h" -#define EZLOPI_UART_CHANNEL_1 1 +typedef int ezlo_uart_channel_t; + +typedef enum e_ezlopi_uart_channel +{ + EZLOPI_UART_CHANNEL_UNDEFINED = -1, + EZLOPI_UART_CHANNEL_0 = UART_NUM_0, + EZLOPI_UART_CHANNEL_1 = UART_NUM_1, #if UART_NUM_MAX > 2 -#define EZLOPI_UART_CHANNEL_2 2 + EZLOPI_UART_CHANNEL_2 = UART_NUM_2, #endif #define EZLOPI_UART_CHANNEL_MAX UART_NUM_MAX +} e_ezlopi_uart_channel_t; -typedef int ezlo_uart_channel_t; typedef struct s_ezlopi_uart_object *s_ezlopi_uart_object_handle_t; typedef void (*__uart_upcall)(uint8_t *buffer, uint32_t output_len, s_ezlopi_uart_object_handle_t uart_object_handle); diff --git a/ezlopi-core/ezlopi_net/ezlopi_net.c b/ezlopi-core/ezlopi_net/ezlopi_net.c index 1b6585316..7e2dcbd6d 100644 --- a/ezlopi-core/ezlopi_net/ezlopi_net.c +++ b/ezlopi-core/ezlopi_net/ezlopi_net.c @@ -1,23 +1,19 @@ #include "ezlopi_net.h" +static s_ezlopi_net_status_t net_stat; + void ezlopi_net_init(void) { + memset(&net_stat, 0, sizeof(s_ezlopi_net_status_t)); ezlopi_ethernet_init(); } s_ezlopi_net_status_t *ezlopi_get_net_status(void) { - s_ezlopi_net_status_t *net_stat = (s_ezlopi_net_status_t *)malloc(sizeof(s_ezlopi_net_status_t)); - if (net_stat) - { - net_stat->wifi_status = ezlopi_wifi_status(); - net_stat->internet_status = ezlopi_ping_get_internet_status(); - net_stat->nma_cloud_connection_status = ezlopi_websocket_client_is_connected(); - } - else - { - net_stat = NULL; - } - return net_stat; + net_stat.wifi_status = ezlopi_wifi_status(); + net_stat.internet_status = ezlopi_ping_get_internet_status(); + net_stat.nma_cloud_connection_status = ezlopi_websocket_client_is_connected(); + + return &net_stat; } \ No newline at end of file diff --git a/ezlopi-core/ezlopi_nvs/ezlopi_nvs.c b/ezlopi-core/ezlopi_nvs/ezlopi_nvs.c index 989ea1d68..34d23e7b9 100644 --- a/ezlopi-core/ezlopi_nvs/ezlopi_nvs.c +++ b/ezlopi-core/ezlopi_nvs/ezlopi_nvs.c @@ -21,8 +21,10 @@ static const char *ezlopi_scenes_nvs_name = "ezlopi_scenes"; static const char *ezlopi_scenes_v2_nvs_name = "ez_scenes_v2"; static const char *ezlopi_scripts_nvs_ids = "ezlopi_scripts"; static const char *settings_initialized_status_name = "settings_magic"; -static const char *config_info_update_time_name = "conf_update_time"; +static const char *config_info_update_time_name = "config_time"; static const char *config_info_version_number = "conf_ver_no"; +static const char *ezlopi_scenes_expression_ids = "ezlopi_exp"; +static const char *ezlopi_room_ids_nvs_name = "ezlopi_room"; int ezlopi_nvs_init(void) { @@ -96,10 +98,12 @@ void ezlopi_nvs_config_info_update_time_set(uint32_t value) { if (ezlopi_nvs_init()) { + nvs_erase_key(ezlopi_nvs_handle, config_info_update_time_name); esp_err_t err = nvs_set_u32(ezlopi_nvs_handle, config_info_update_time_name, value); TRACE_W("nvs_set_u32 - error: %s", esp_err_to_name(err)); } } + void ezlopi_nvs_config_info_version_number_set(uint32_t value) { if (ezlopi_nvs_init()) @@ -111,12 +115,7 @@ void ezlopi_nvs_config_info_version_number_set(uint32_t value) int ezlopi_nvs_scene_set_v2(char *scene) { - int ret = 0; - if (scene) - { - ret = ezlopi_nvs_write_str(scene, strlen(scene) + 1, ezlopi_scenes_v2_nvs_name); - } - return ret; + return ezlopi_nvs_write_str(scene, strlen(scene) + 1, ezlopi_scenes_v2_nvs_name); } char *ezlopi_nvs_scene_get_v2(void) @@ -126,12 +125,7 @@ char *ezlopi_nvs_scene_get_v2(void) int ezlopi_nvs_scene_set(char *scene) { - int ret = 0; - if (scene) - { - ret = ezlopi_nvs_write_str(scene, strlen(scene) + 1, ezlopi_scenes_nvs_name); - } - return ret; + return ezlopi_nvs_write_str(scene, strlen(scene) + 1, ezlopi_scenes_nvs_name); } char *ezlopi_nvs_scene_get(void) @@ -152,14 +146,7 @@ int ezlopi_nvs_factory_reset(void) int ezlopi_nvs_write_scenes_scripts(char *data) { - int ret = 0; - - if (1 == ezlopi_nvs_write_str(data, strlen(data), (char *)ezlopi_scripts_nvs_ids)) - { - ret = 1; - } - - return ret; + return ezlopi_nvs_write_str(data, strlen(data), (char *)ezlopi_scripts_nvs_ids); } char *ezlopi_nvs_read_scenes_scripts(void) @@ -167,14 +154,29 @@ char *ezlopi_nvs_read_scenes_scripts(void) return ezlopi_nvs_read_str(ezlopi_scripts_nvs_ids); } +int ezlopi_nvs_write_scenes_expressions(char *data) +{ + return ezlopi_nvs_write_str(data, strlen(data), ezlopi_scenes_expression_ids); +} + +char *ezlopi_nvs_read_scenes_expressions(void) +{ + return ezlopi_nvs_read_str(ezlopi_scenes_expression_ids); +} + +char *ezlopi_nvs_read_rooms(void) +{ + return ezlopi_nvs_read_str(ezlopi_room_ids_nvs_name); +} + +int ezlopi_nvs_write_rooms(char *data) +{ + return ezlopi_nvs_write_str(data, strlen(data), ezlopi_room_ids_nvs_name); +} + int ezlopi_nvs_write_config_data_str(char *data) { - int ret = 0; - if (1 == ezlopi_nvs_write_str(data, strlen(data), config_nvs_name)) - { - ret = 1; - } - return ret; + return ezlopi_nvs_write_str(data, strlen(data), config_nvs_name); } char *ezlopi_nvs_read_config_data_str(void) @@ -231,12 +233,7 @@ int ezlopi_nvs_write_ble_passkey(uint32_t passkey) int ezlopi_nvs_write_wifi(const char *wifi_info, uint32_t len) { - int ret = 0; - if (1 == ezlopi_nvs_write_str(wifi_info, len, wifi_info_nvs_name)) - { - ret = 1; - } - return ret; + return ezlopi_nvs_write_str(wifi_info, len, wifi_info_nvs_name); } int ezlopi_nvs_read_wifi(char *wifi_info, uint32_t len) @@ -522,47 +519,50 @@ char *ezlopi_nvs_read_str(char *nvs_name) { char *return_str = NULL; - if (1 == ezlopi_nvs_init()) + if (nvs_name) { - esp_err_t err = ESP_OK; - size_t buf_len_needed = 0; - err = nvs_get_str(ezlopi_nvs_handle, nvs_name, NULL, &buf_len_needed); - - if (buf_len_needed && (ESP_OK == err)) + if (1 == ezlopi_nvs_init()) { - return_str = malloc(buf_len_needed + 1); + esp_err_t err = ESP_OK; + size_t buf_len_needed = 0; + err = nvs_get_str(ezlopi_nvs_handle, nvs_name, NULL, &buf_len_needed); - if (return_str) + if (buf_len_needed && (ESP_OK == err)) { - memset(return_str, 0, buf_len_needed + 1); - err = nvs_get_str(ezlopi_nvs_handle, nvs_name, return_str, &buf_len_needed); + return_str = malloc(buf_len_needed + 1); - if (ESP_OK == err) + if (return_str) { - // TRACE_D("%s read success. \r\nData[%d]: \r\n%s", nvs_name, buf_len_needed, return_str); + memset(return_str, 0, buf_len_needed + 1); + err = nvs_get_str(ezlopi_nvs_handle, nvs_name, return_str, &buf_len_needed); + + if (ESP_OK == err) + { + // TRACE_D("%s read success. \r\nData[%d]: \r\n%s", nvs_name, buf_len_needed, return_str); + } + else + { + TRACE_E("%s read error: %s", nvs_name, esp_err_to_name(err)); + free(return_str); + return_str = NULL; + } } else { - TRACE_E("%s read error: %s", nvs_name, esp_err_to_name(err)); - free(return_str); - return_str = NULL; + TRACE_E("MALLOC ERROR"); } } else { - TRACE_E("MALLOC ERROR"); + TRACE_E("%s: buf_len_needed: %d, err: %s", nvs_name, buf_len_needed, esp_err_to_name(err)); } } - else - { - TRACE_E("%s: buf_len_needed: %d, err: %s", nvs_name, buf_len_needed, esp_err_to_name(err)); - } } return return_str; } -void ezlopi_nvs_delete_stored_script(uint32_t script_id) +void ezlopi_nvs_delete_stored_data_by_id(uint32_t script_id) { esp_err_t err = ESP_OK; if (1 == ezlopi_nvs_init()) diff --git a/ezlopi-core/ezlopi_nvs/ezlopi_nvs.h b/ezlopi-core/ezlopi_nvs/ezlopi_nvs.h index 5ea9cb890..b4b06abb9 100644 --- a/ezlopi-core/ezlopi_nvs/ezlopi_nvs.h +++ b/ezlopi-core/ezlopi_nvs/ezlopi_nvs.h @@ -68,7 +68,13 @@ extern "C" char *ezlopi_nvs_read_scenes_scripts(void); int ezlopi_nvs_write_scenes_scripts(char *data); - void ezlopi_nvs_delete_stored_script(uint32_t script_id); + void ezlopi_nvs_delete_stored_data_by_id(uint32_t script_id); + + char *ezlopi_nvs_read_scenes_expressions(void); + int ezlopi_nvs_write_scenes_expressions(char *data); + + char *ezlopi_nvs_read_rooms(void); + int ezlopi_nvs_write_rooms(char *data); #ifdef __cplusplus } diff --git a/ezlopi-core/ezlopi_ota/CMakeLists.txt b/ezlopi-core/ezlopi_ota/CMakeLists.txt index 2018feca4..585fad09a 100644 --- a/ezlopi-core/ezlopi_ota/CMakeLists.txt +++ b/ezlopi-core/ezlopi_ota/CMakeLists.txt @@ -2,6 +2,6 @@ file(GLOB_RECURSE config_src "./*.c*") -idf_component_register(SRCS "ezlopi_ota.c" "${config_src}" +idf_component_register(SRCS "${config_src}" INCLUDE_DIRS "." - REQUIRES nvs_flash trace json esp_http_client ezlopi_factory_info esp-tls app_update esp_https_ota version) + REQUIRES nvs_flash trace json esp_http_client ezlopi_factory_info esp-tls app_update esp_https_ota app_update ezlopi_reboot version) diff --git a/ezlopi-core/ezlopi_ota/ezlopi_ota.c b/ezlopi-core/ezlopi_ota/ezlopi_ota.c index 0a24559bb..5699fa752 100644 --- a/ezlopi-core/ezlopi_ota/ezlopi_ota.c +++ b/ezlopi-core/ezlopi_ota/ezlopi_ota.c @@ -1,13 +1,20 @@ -#include "string.h" +#include +#include +#include +#include +#include +#include -#include "cJSON.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" -#include "esp_http_client.h" -#include "esp_tls.h" +#include +#include +#include +#include +#include #include "trace.h" + +#include "ezlopi_ota.h" +#include "ezlopi_reboot.h" #include "ezlopi_factory_info.h" // #include "esp_system.h" diff --git a/ezlopi-core/ezlopi_reboot/CMakeLists.txt b/ezlopi-core/ezlopi_reboot/CMakeLists.txt new file mode 100644 index 000000000..904fb50cd --- /dev/null +++ b/ezlopi-core/ezlopi_reboot/CMakeLists.txt @@ -0,0 +1,5 @@ +file(GLOB_RECURSE config_src "./*.c*") + +idf_component_register(SRCS "ezlopi_reboot.c" "${config_src}" + INCLUDE_DIRS "." + REQUIRES trace web_provisioning) diff --git a/ezlopi-core/ezlopi_reboot/ezlopi_reboot.c b/ezlopi-core/ezlopi_reboot/ezlopi_reboot.c new file mode 100644 index 000000000..b98b6b8b9 --- /dev/null +++ b/ezlopi-core/ezlopi_reboot/ezlopi_reboot.c @@ -0,0 +1,9 @@ +#include + +#include "web_provisioning.h" + +void ezlopi_reboot(void) +{ + web_provisioning_deinit(); + esp_restart(); +} \ No newline at end of file diff --git a/ezlopi-core/ezlopi_reboot/ezlopi_reboot.h b/ezlopi-core/ezlopi_reboot/ezlopi_reboot.h new file mode 100644 index 000000000..eb17633c4 --- /dev/null +++ b/ezlopi-core/ezlopi_reboot/ezlopi_reboot.h @@ -0,0 +1,6 @@ +#ifndef __EZLOPI_REBOOT_H__ +#define __EZLOPI_REBOOT_H__ + +void ezlopi_reboot(void); + +#endif // __EZLOPI_REBOOT_H__ diff --git a/ezlopi-core/ezlopi_room/CMakeLists.txt b/ezlopi-core/ezlopi_room/CMakeLists.txt new file mode 100644 index 000000000..eb2c7d1ad --- /dev/null +++ b/ezlopi-core/ezlopi_room/CMakeLists.txt @@ -0,0 +1,5 @@ +file(GLOB_RECURSE config_src "*.c") + +idf_component_register(SRCS "ezlopi_room.c" "${config_src}" + INCLUDE_DIRS "." + REQUIRES trace json ezlopi_devices lua core ezlopi_meshbot_service ezlopi_nvs web_provisioning) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_room/ezlopi_room.c b/ezlopi-core/ezlopi_room/ezlopi_room.c new file mode 100644 index 000000000..fe7f30977 --- /dev/null +++ b/ezlopi-core/ezlopi_room/ezlopi_room.c @@ -0,0 +1,612 @@ +#include "cJSON.h" +#include "trace.h" + +#include "ezlopi_nvs.h" +#include "ezlopi_room.h" +#include "ezlopi_cloud.h" +#include "web_provisioning.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" + +static s_ezlopi_room_t *l_room_head = NULL; + +static const char *sc_room_subtype_name[] = { +#ifndef ROOM_SUBTYPE +#define ROOM_SUBTYPE(name, e_num) name, +#undef ROOM_SUBTYPE +#endif +}; + +static void __sort_by_pos(void); +static void __free_nodes(s_ezlopi_room_t *room); +static int __free_room_from_list_by_id(uint32_t room_id); +static void __update_cloud_room_deleted(uint32_t room_id); +static int __remove_room_from_nvs_by_id(uint32_t a_room_id); +static e_room_subtype_t __get_subtype_enum(char *subtype_str); +static const char *__get_subtype_name(e_room_subtype_t subtype); + +s_ezlopi_room_t *ezlopi_room_get_room_head(void) +{ + return l_room_head; +} + +int ezlopi_room_name_set(cJSON *cj_room) +{ + int ret = 0; + if (cj_room) + { + cJSON *cj_room_id = cJSON_GetObjectItem(cj_room, ezlopi__id_str); + cJSON *cj_room_name = cJSON_GetObjectItem(cj_room, ezlopi_name_str); + + if (cj_room_id && cj_room_id->valuestring && cj_room_name && cj_room_name->valuestring) + { + uint32_t room_id = strtoul(cj_room_id->valuestring, NULL, 16); + if (room_id) + { + char *rooms_str = ezlopi_nvs_read_rooms(); + if (rooms_str) + { + cJSON *cj_rooms = cJSON_Parse(rooms_str); + free(rooms_str); + + if (cj_rooms) + { + int idx = 0; + cJSON *cj_room_tmp = NULL; + while (NULL != (cj_room_tmp = cJSON_GetArrayItem(cj_rooms, idx++))) + { + cJSON *cj_room_tmp_id = cJSON_GetObjectItem(cj_room_tmp, ezlopi__id_str); + if (cj_room_tmp_id && cj_room_tmp_id->valuestring) + { + uint32_t tmp_room_id = strtoul(cj_room_tmp_id->valuestring, NULL, 16); + if (room_id == tmp_room_id) + { + cJSON_DeleteItemFromObject(cj_room_tmp, ezlopi_name_str); + ret = cJSON_AddItemReferenceToObject(cj_room_tmp, ezlopi_name_str, cj_room_name); + + s_ezlopi_room_t *room_node = l_room_head; + while (room_node) + { + if (room_node->_id == room_id) + { + snprintf(room_node->name, sizeof(room_node->name), "%s", cj_room_name->valuestring); + break; + } + room_node = room_node->next; + } + break; + } + } + } + + if (ret) + { + char *updated_rooms = cJSON_Print(cj_rooms); + if (updated_rooms) + { + cJSON_Minify(updated_rooms); + ezlopi_nvs_write_rooms(updated_rooms); + free(updated_rooms); + } + } + + cJSON_Delete(cj_rooms); + } + } + } + } + } + + return ret; +} + +int ezlopi_room_delete(cJSON *cj_room) +{ + int ret = 0; + if (cj_room) + { + CJSON_TRACE("cj_room", cj_room); + cJSON *cj_room_id = cJSON_GetObjectItem(cj_room, ezlopi__id_str); + + if (cj_room_id && cj_room_id->valuestring) + { + CJSON_TRACE("cj_room_id", cj_room_id); + uint32_t room_id = strtoul(cj_room_id->valuestring, NULL, 16); + + if (room_id) + { + TRACE_B("room_id: %8x", room_id); + + int ret = __free_room_from_list_by_id(room_id); + TRACE_B("ret: %d", ret); + ret = __remove_room_from_nvs_by_id(room_id); + TRACE_B("ret: %d", ret); + } + } + } + + return ret; +} + +int ezlopi_room_delete_all(void) +{ + int ret = 1; + + __free_nodes(l_room_head); + l_room_head = NULL; + + ezlopi_nvs_write_rooms("[]"); + + return ret; +} + +int ezlopi_room_add_to_nvs(cJSON *cj_room) +{ + CJSON_TRACE("cj_room", cj_room); + + int ret = 0; + if (cj_room) + { + int room_free = 1; + char *rooms_str = ezlopi_nvs_read_rooms(); + + if (NULL == rooms_str) + { + rooms_str = "[]"; + room_free = 0; + } + + cJSON *cj_rooms = cJSON_Parse(rooms_str); + if (room_free) + { + free(rooms_str); + } + + CJSON_TRACE("cj_rooms", cj_rooms); + + if (cj_rooms) + { + if (cJSON_AddItemReferenceToArray(cj_rooms, cj_room)) + { + + CJSON_TRACE("cj_rooms", cj_rooms); + char *updated_rooms_str = cJSON_Print(cj_rooms); + if (updated_rooms_str) + { + cJSON_Minify(updated_rooms_str); + + if (ezlopi_nvs_write_rooms(updated_rooms_str)) + { + TRACE_B("room saved"); + ret = 1; + } + + free(updated_rooms_str); + } + } + + cJSON_Delete(cj_rooms); + } + } + + return ret; +} + +int ezlopi_room_reorder(cJSON *cj_rooms_ids) +{ + int ret = 0; + + if (cj_rooms_ids) + { + CJSON_TRACE("new-order ids", cj_rooms_ids); + uint32_t rooms_id_arr_size = cJSON_GetArraySize(cj_rooms_ids); + + if (rooms_id_arr_size) + { + int idx = 0; + cJSON *cj_reordered_rooms = cJSON_CreateArray(); + + if (cj_reordered_rooms) + { + cJSON *cj_room_id = NULL; + + while (NULL != (cj_room_id = cJSON_GetArrayItem(cj_rooms_ids, idx))) + { + uint32_t room_id = strtoul(cj_room_id->valuestring, NULL, 16); + if (room_id) + { + s_ezlopi_room_t *room_node = l_room_head; + while (room_node) + { + if (room_id == room_node->_id) + { + room_node->_pos = idx; + + cJSON *cj_room = cJSON_CreateObject(); + if (cj_room) + { + cJSON_AddStringToObject(cj_room, ezlopi__id_str, cj_room_id->valuestring); + cJSON_AddStringToObject(cj_room, ezlopi_name_str, room_node->name); + + if (!cJSON_AddItemToArray(cj_reordered_rooms, cj_room)) + { + cJSON_Delete(cj_room); + } + } + } + + room_node = room_node->next; + } + } + + idx++; + } + + __sort_by_pos(); + + if (cJSON_GetArraySize(cj_reordered_rooms)) + { + char *reordered_rooms_str = cJSON_Print(cj_reordered_rooms); + if (reordered_rooms_str) + { + cJSON_Minify(reordered_rooms_str); + ezlopi_nvs_write_rooms(reordered_rooms_str); + free(reordered_rooms_str); + } + } + + cJSON_Delete(cj_reordered_rooms); + } + +#if 0 + int idx = 0; + cJSON *cj_room_id = NULL; + uint32_t rooms_id_arr[rooms_id_arr_size]; + + while (NULL != (cj_room_id = cJSON_GetArrayItem(cj_rooms_ids, idx))) + { + rooms_id_arr[idx] = strtoul(cj_room_id->valuestring, NULL, 16); + idx++; + } + char *room_list_str = ezlopi_nvs_read_rooms(); + + if (room_list_str) + { + TRACE_D("old-order list: %s", room_list_str); + cJSON *cj_stored_room_list = cJSON_Parse(room_list_str); + free(room_list_str); + + if (cj_stored_room_list) + { + cJSON *cj_reordered_rooms = cJSON_CreateArray(); + + if (cj_reordered_rooms) + { + int idx1 = 0; + while (idx1 < rooms_id_arr_size) + { + int idx2 = 0; + cJSON *cj_stored_room = NULL; + while (NULL != (cj_stored_room = cJSON_GetArrayItem(cj_stored_room_list, idx2))) + { + cJSON *cj_stored_room_id = cJSON_GetObjectItem(cj_stored_room, ezlopi__id_str); + if (cj_stored_room_id && cj_stored_room_id->valuestring) + { + uint32_t stored_room_id = strtoul(cj_stored_room_id->valuestring, NULL, 16); + if (stored_room_id == rooms_id_arr[idx1]) + { + cJSON_AddItemReferenceToArray(cj_reordered_rooms, cj_stored_room); + } + } + + idx2++; + } + + __rooms_move_to_pos(rooms_id_arr[idx1], idx1); + idx1++; + } + + char *reordered_rooms_str = cJSON_Print(cj_reordered_rooms); + cJSON_Delete(cj_reordered_rooms); + + if (reordered_rooms_str) + { + TRACE_D("reordered room list: %s", reordered_rooms_str); + cJSON_Minify(reordered_rooms_str); + // ezlopi_nvs_write_rooms(reordered_rooms_str); + free(reordered_rooms_str); + } + } + + cJSON_Delete(cj_stored_room_list); + } + } +#endif + } + } + + return ret; +} + +s_ezlopi_room_t *ezlopi_room_add_to_list(cJSON *cj_room) +{ + s_ezlopi_room_t *new_room = malloc(sizeof(s_ezlopi_room_t)); + if (new_room) + { + memset(new_room, 0, sizeof(s_ezlopi_room_t)); + + if (cj_room) + { + CJSON_TRACE("room", cj_room); + + cJSON *cj_room_id = cJSON_GetObjectItem(cj_room, ezlopi__id_str); + if (cj_room_id && cj_room_id->valuestring) + { + new_room->_id = strtoul(cj_room_id->valuestring, NULL, 16); + ezlopi_cloud_update_room_id(new_room->_id); + } + else + { + char tmp_str[32]; + new_room->_id = ezlopi_cloud_generate_room_id(); + snprintf(tmp_str, sizeof(tmp_str), "%08x", new_room->_id); + cJSON_AddStringToObject(cj_room, ezlopi__id_str, tmp_str); + } + + cJSON *cj_room_subtype = cJSON_GetObjectItem(cj_room, ezlopi_subtype_str); + if (cj_room_subtype && cj_room_subtype->valuestring) + { + new_room->subtype = __get_subtype_enum(cj_room_subtype->valuestring); + } + else + { + new_room->subtype = ROOM_SUBTYPE_NONE; + } + + cJSON *cj_room_name = cJSON_GetObjectItem(cj_room, ezlopi_name_str); + if (cj_room_name && cj_room_name->valuestring) + { + snprintf(new_room->name, sizeof(new_room->name), "%s", cj_room_name->valuestring); + } + } + } + + return new_room; +} + +void ezlopi_room_init(void) +{ + char *rooms_str = ezlopi_nvs_read_rooms(); + if (rooms_str) + { + cJSON *cj_rooms = cJSON_Parse(rooms_str); + free(rooms_str); + + if (cj_rooms) + { + int idx = 0; + cJSON *cj_room = NULL; + s_ezlopi_room_t *curr_room_node = NULL; + + while (NULL != (cj_room = cJSON_GetArrayItem(cj_rooms, idx))) + { + s_ezlopi_room_t *new_room = ezlopi_room_add_to_list(cj_rooms); + if (new_room) + { + new_room->_pos = idx; + if (curr_room_node) + { + curr_room_node->next = new_room; + curr_room_node = curr_room_node->next; + } + else + { + l_room_head = new_room; + curr_room_node = l_room_head; + } + } + + idx++; + } + } + } +} + +static void __update_cloud_room_deleted(uint32_t room_id) +{ + cJSON *cj_response = cJSON_CreateObject(); + { + cJSON_AddStringToObject(cj_response, ezlopi_id_str, ezlopi_ui_broadcast_str); + cJSON_AddStringToObject(cj_response, ezlopi_msg_subclass_str, ezlopi_hub_room_deleted_str); + cJSON_AddStringToObject(cj_response, ezlopi_key_method_str, "hub.room.all.delete"); + + cJSON *cj_result = cJSON_AddObjectToObject(cj_response, ezlopi_result_str); + if (cj_result) + { + char tmp_str[32]; + snprintf(tmp_str, sizeof(tmp_str), "%08x", room_id); + cJSON_AddStringToObject(cj_result, ezlopi__id_str, tmp_str); + } + + char *data_str = cJSON_Print(cj_response); + cJSON_Delete(cj_response); + + if (data_str) + { + cJSON_Minify(data_str); + web_provisioning_send_str_data_to_nma_websocket(data_str, TRACE_TYPE_B); + free(data_str); + } + } +} + +static void __free_nodes(s_ezlopi_room_t *room) +{ + if (room) + { + __free_nodes(room->next); + __update_cloud_room_deleted(room->_id); + free(room); + } +} + +static void __sort_by_pos(void) +{ + uint32_t size = 0; + s_ezlopi_room_t *room_ptr_arr[32]; + s_ezlopi_room_t *curr_room = l_room_head; + + curr_room = l_room_head; + memset(room_ptr_arr, 0, sizeof(room_ptr_arr)); + + while (curr_room) + { + size++; + room_ptr_arr[curr_room->_pos] = curr_room; + curr_room = curr_room->next; + } + + l_room_head = room_ptr_arr[0]; + l_room_head->next = NULL; + curr_room = l_room_head; + + int idx = 1; + + while (idx < size) + { + curr_room->next = room_ptr_arr[idx]; + curr_room->next->next = NULL; + idx++; + } +} + +static const char *__get_subtype_name(e_room_subtype_t subtype) +{ + const char *ret = ezlopi__str; + if ((subtype > ROOM_SUBTYPE_NONE) && (subtype < ROOM_SUBTYPE_MAX)) + { + ret = sc_room_subtype_name[subtype]; + } + + return ret; +} + +static e_room_subtype_t __get_subtype_enum(char *subtype_str) +{ + e_room_subtype_t ret = ROOM_SUBTYPE_NONE; + + if (subtype_str) + { + for (int idx = ROOM_SUBTYPE_NONE; idx < ROOM_SUBTYPE_MAX; idx++) + { + if (0 == strcmp(sc_room_subtype_name[idx], subtype_str)) + { + ret = idx; + break; + } + } + } + + return ret; +} + +static int __free_room_from_list_by_id(uint32_t room_id) +{ + int ret = 0; + + if (l_room_head) + { + TRACE_B("l_room_head->_id == room_id -> %8x == %8x", l_room_head->_id, room_id); + + if (l_room_head->_id == room_id) + { + ret = 1; + s_ezlopi_room_t *free_node = l_room_head; + l_room_head = l_room_head->next; + free(free_node); + } + else + { + s_ezlopi_room_t *room_node = l_room_head; + + while (room_node->next) + { + s_ezlopi_room_t *free_node = room_node->next; + TRACE_B("free_node->_id == room_id -> %8x == %8x", free_node->_id, room_id); + + if (free_node->_id == room_id) + { + ret = 1; + room_node->next = free_node->next; + free(free_node); + break; + } + + room_node = room_node->next; + } + } + } + + return ret; +} + +static int __remove_room_from_nvs_by_id(uint32_t a_room_id) +{ + int ret = 0; + char *rooms_str = ezlopi_nvs_read_rooms(); + + if (rooms_str) + { + TRACE_B("rooms_str: %s", rooms_str); + cJSON *cj_rooms = cJSON_Parse(rooms_str); + free(rooms_str); + + if (cj_rooms) + { + CJSON_TRACE("cj_rooms", cj_rooms); + + int idx = 0; + cJSON *cj_room_tmp = NULL; + + while (NULL != (cj_room_tmp = cJSON_GetArrayItem(cj_rooms, idx))) + { + CJSON_TRACE("cj_room_tmp", cj_room_tmp); + + cJSON *cj_room_tmp_id = cJSON_GetObjectItem(cj_room_tmp, ezlopi__id_str); + if (cj_room_tmp_id && cj_room_tmp_id->valuestring) + { + CJSON_TRACE("cj_room_tmp_id", cj_room_tmp_id); + + uint32_t tmp_room_id = strtoul(cj_room_tmp_id->valuestring, NULL, 16); + if (a_room_id == tmp_room_id) + { + TRACE_B("tmp_room_id: %8x", tmp_room_id); + cJSON_DeleteItemFromArray(cj_rooms, idx); + ret = 1; + break; + } + } + + idx++; + } + + if (ret) + { + CJSON_TRACE("cj_rooms", cj_rooms); + char *updated_rooms = cJSON_Print(cj_rooms); + + if (updated_rooms) + { + ret = 1; + cJSON_Minify(updated_rooms); + ezlopi_nvs_write_rooms(updated_rooms); + free(updated_rooms); + } + } + + cJSON_Delete(cj_rooms); + } + } + + return ret; +} diff --git a/ezlopi-core/ezlopi_room/ezlopi_room.h b/ezlopi-core/ezlopi_room/ezlopi_room.h new file mode 100644 index 000000000..5705ef200 --- /dev/null +++ b/ezlopi-core/ezlopi_room/ezlopi_room.h @@ -0,0 +1,39 @@ +#ifndef __EZLOPI_ROOM_H__ +#define __EZLOPI_ROOM_H__ + +#include "string.h" +#include "stdlib.h" +#include "stdio.h" + +#include "cJSON.h" + +typedef enum e_room_subtype +{ +#ifndef ROOM_SUBTYPE +#define ROOM_SUBTYPE(name, e_num) ROOM_SUBTYPE_##e_num, +#include "ezlopi_room_subtype_macro.h" +#undef ROOM_SUBTYPE +#endif +} e_room_subtype_t; + +typedef struct s_ezlopi_room +{ + uint32_t _pos; + char name[32]; + uint32_t _id; + e_room_subtype_t subtype; + uint32_t image_id; + struct s_ezlopi_room *next; +} s_ezlopi_room_t; + +s_ezlopi_room_t *ezlopi_room_get_room_head(void); + +void ezlopi_room_init(void); +int ezlopi_room_delete(cJSON *cj_room); +int ezlopi_room_delete_all(void); +int ezlopi_room_name_set(cJSON *cj_room); +int ezlopi_room_add_to_nvs(cJSON *cj_room); +int ezlopi_room_reorder(cJSON *cj_rooms_ids); +s_ezlopi_room_t *ezlopi_room_add_to_list(cJSON *cj_room); + +#endif // __EZLOPI_ROOM_H__ \ No newline at end of file diff --git a/ezlopi-core/ezlopi_room/ezlopi_room_subtype_macro.h b/ezlopi-core/ezlopi_room/ezlopi_room_subtype_macro.h new file mode 100644 index 000000000..33450766e --- /dev/null +++ b/ezlopi-core/ezlopi_room/ezlopi_room_subtype_macro.h @@ -0,0 +1,5 @@ +ROOM_SUBTYPE("none", NONE) +ROOM_SUBTYPE("room", ROOM) + +////////////////////// +ROOM_SUBTYPE("max", MAX) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/CMakeLists.txt b/ezlopi-core/ezlopi_scenes/CMakeLists.txt index 840c95b18..87f166ada 100644 --- a/ezlopi-core/ezlopi_scenes/CMakeLists.txt +++ b/ezlopi-core/ezlopi_scenes/CMakeLists.txt @@ -2,7 +2,7 @@ file(GLOB_RECURSE config_src "*.c") -idf_component_register(SRCS "ezlopi_scenes_then_methods.c" "ezlopi_scenes_when_methods.c" "ezlopi_scenes_print.c" "ezlopi_scenes_cjson.c" "ezlopi_scenes_delete.c" "ezlopi_scenes_status_changed.c" "ezlopi_scenes_v2.c" "${config_src}" +idf_component_register(SRCS "ezlopi_scenes_populate.c" "ezlopi_scenes_edit.c" "ezlopi_scenes_when_methods.c" "ezlopi_scenes_operators.c" "ezlopi_scenes_then_methods.c" "ezlopi_scenes_print.c" "ezlopi_scenes_cjson.c" "ezlopi_scenes_v2.c" "ezlopi_scenes_notifications.c" "${config_src}" INCLUDE_DIRS "." REQUIRES trace json ezlopi_devices lua core ezlopi_meshbot_service) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/__operators_macros/__numeric_comparision_operators_macros.h b/ezlopi-core/ezlopi_scenes/__operators_macros/__numeric_comparision_operators_macros.h new file mode 100755 index 000000000..4467b4894 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/__operators_macros/__numeric_comparision_operators_macros.h @@ -0,0 +1,14 @@ +SCENES_NUM_COMP_OPERATORS(NONE, NULL, NULL, NULL) +////////////////////////////////////////////////////////////////////////////// +SCENES_NUM_COMP_OPERATORS(LESS, "<", "less", "compareNumbers") +SCENES_NUM_COMP_OPERATORS(GREATER, ">", "greater", "compareNumbers") +SCENES_NUM_COMP_OPERATORS(LESS_EQUAL, "<=", "less equal", "compareNumbers") +SCENES_NUM_COMP_OPERATORS(GREATER_EQUAL, ">=", "greater equal", "compareNumbers") +SCENES_NUM_COMP_OPERATORS(EQUAL, "==", "equal", "compareNumbers") +SCENES_NUM_COMP_OPERATORS(NOT_EQUAL, "!=", "not equal", "compareNumbers") +SCENES_NUM_COMP_OPERATORS(BETWEEN, "between", "between", "compareNumberRange") +SCENES_NUM_COMP_OPERATORS(NOT_BETWEEN, "not_between", "not between", "compareNumberRange") +// SCENES_NUM_COMP_OPERATORS(ANY_OF, "any_of", "any of", "numbersArray") +// SCENES_NUM_COMP_OPERATORS(NONE_OF, "none_of", "none of", "numbersArray") +////////////////////////////////////////////////////////////////////////////// +SCENES_NUM_COMP_OPERATORS(MAX, NULL, NULL, NULL) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/__operators_macros/__strings_comparision_operators_macros.h b/ezlopi-core/ezlopi_scenes/__operators_macros/__strings_comparision_operators_macros.h new file mode 100644 index 000000000..c8e4a7aa6 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/__operators_macros/__strings_comparision_operators_macros.h @@ -0,0 +1,19 @@ +SCENES_STRINGS_OPERATORS(NONE, NULL, NULL, NULL) +////////////////////////////////////////////////////////////////////////////// +SCENES_STRINGS_OPERATORS(LESS, "<", "less", "compareStrings") +SCENES_STRINGS_OPERATORS(GREATER, ">", "greater", "compareStrings") +SCENES_STRINGS_OPERATORS(LESS_EQUAL, "<=", "less equal", "compareStrings") +SCENES_STRINGS_OPERATORS(GREATER_EQUAL, ">=", "greater equal", "compareStrings") +SCENES_STRINGS_OPERATORS(EQUAL, "==", "equal", "compareStrings") +SCENES_STRINGS_OPERATORS(NOT_EQUAL, "!=", "not equal", "compareStrings") + +SCENES_STRINGS_OPERATORS(BEGINS_WITH, "begin", "begins with", "stringOperation") +SCENES_STRINGS_OPERATORS(ENDS_WITH, "end", "ends with", "stringOperation") +SCENES_STRINGS_OPERATORS(CONTAINS, "contain", "contains", "stringOperation") +SCENES_STRINGS_OPERATORS(LENGTH, "length", "length equal to", "stringOperation") +SCENES_STRINGS_OPERATORS(NOT_BEGIN, "not_begin", "doesn't begin with", "stringOperation") +SCENES_STRINGS_OPERATORS(NOT_END, "not_end", "doesn't end with", "stringOperation") +SCENES_STRINGS_OPERATORS(NOT_CONTAIN, "not_contain", "doesn't contain", "stringOperation") +SCENES_STRINGS_OPERATORS(NOT_LENGTH, "not_length", "length not equal to", "stringOperation") +////////////////////////////////////////////////////////////////////////////// +SCENES_STRINGS_OPERATORS(MAX, NULL, NULL, NULL) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/__operators_macros/__value_with_less_comparision_operators_macros.h b/ezlopi-core/ezlopi_scenes/__operators_macros/__value_with_less_comparision_operators_macros.h new file mode 100644 index 000000000..936538af2 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/__operators_macros/__value_with_less_comparision_operators_macros.h @@ -0,0 +1,10 @@ +SCENES_VALUES_WITH_LESS_OPERATORS(NONE, NULL, NULL, NULL) +////////////////////////////////////////////////////////////////////////////// +SCENES_VALUES_WITH_LESS_OPERATORS(LESS, "<", "less", "compareValues") +SCENES_VALUES_WITH_LESS_OPERATORS(GREATER, ">", "greater", "compareValues") +SCENES_VALUES_WITH_LESS_OPERATORS(LESS_EQUAL, "<=", "less equal", "compareValues") +SCENES_VALUES_WITH_LESS_OPERATORS(GREATER_EQUAL, ">=", "greater equal", "compareValues") +SCENES_VALUES_WITH_LESS_OPERATORS(EQUAL, "==", "equal", "compareValues") +SCENES_VALUES_WITH_LESS_OPERATORS(NOT_EQUAL, "!=", "not equal", "compareValues") +////////////////////////////////////////////////////////////////////////////// +SCENES_VALUES_WITH_LESS_OPERATORS(MAX, NULL, NULL, NULL) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/__operators_macros/__value_without_less_comparision_operators_macros.h b/ezlopi-core/ezlopi_scenes/__operators_macros/__value_without_less_comparision_operators_macros.h new file mode 100644 index 000000000..fc4f01fd8 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/__operators_macros/__value_without_less_comparision_operators_macros.h @@ -0,0 +1,6 @@ +SCENES_VALUES_WITHOUT_LESS_OPERATORS(NONE, NULL, NULL, NULL) +////////////////////////////////////////////////////////////////////////////// +SCENES_VALUES_WITHOUT_LESS_OPERATORS(EQUAL, "==", "equal", "compareValues") +SCENES_VALUES_WITHOUT_LESS_OPERATORS(NOT_EQUAL, "!=", "not equal", "compareValues") +////////////////////////////////////////////////////////////////////////////// +SCENES_VALUES_WITHOUT_LESS_OPERATORS(MAX, NULL, NULL, NULL) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_cjson.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_cjson.c index 7ea819fb3..9dfd8af23 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_cjson.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_cjson.c @@ -2,32 +2,168 @@ #include "cJSON.h" #include "trace.h" -#include "ezlopi_scenes_v2.h" +#include "ezlopi_scenes_cjson.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" -static void ezlopi_scenes_cjson_add_action_block_options(cJSON *cj_block_array, l_action_block_v2_t *then_block); -static void ezlopi_scenes_cjson_add_action_delay(cJSON *cj_then_block, s_action_delay_v2_t *action_delay); -static void ezlopi_scenes_cjson_add_fields(cJSON *cj_block, l_fields_v2_t *fields); +static void __cjson_add_fields(cJSON *cj_block, l_fields_v2_t *fields); +static void __cjson_add_string(cJSON *root, const char *key, const char *value); +static void __cjson_add_action_delay(cJSON *cj_then_block, s_action_delay_v2_t *action_delay); +static void __cjson_add_action_block_options(cJSON *cj_block_array, l_action_block_v2_t *then_block); -static void ezlopi_scenes_cjson_add_string(cJSON *root, char *key, const char *value) +cJSON *ezlopi_scene_cjson_get_field(l_fields_v2_t *field_node) { - if (root && key && value) + cJSON *cj_field = NULL; + if (field_node) { - if (isprint(value[0])) - { - cJSON_AddStringToObject(root, key, value); - } - else + cj_field = cJSON_CreateObject(); { - cJSON_AddNullToObject(root, key); + const char *value_type_str = ezlopi_scene_get_scene_value_type_name_v2(field_node->value_type); + if (value_type_str) + { + cJSON_AddStringToObject(cj_field, ezlopi_type_str, value_type_str); + cJSON_AddStringToObject(cj_field, ezlopi_name_str, field_node->name); + } + + if (field_node->scale) + { + cJSON_AddStringToObject(cj_field, ezlopi_scale_str, field_node->scale); + } + + switch (field_node->value_type) + { + case EZLOPI_VALUE_TYPE_INT: + case EZLOPI_VALUE_TYPE_FLOAT: + { + cJSON_AddNumberToObject(cj_field, ezlopi_value_str, field_node->value.value_double); + break; + } + case EZLOPI_VALUE_TYPE_BOOL: + { + if (0 == field_node->value.value_double) + { + cJSON_AddFalseToObject(cj_field, ezlopi_value_str); + } + else + { + cJSON_AddTrueToObject(cj_field, ezlopi_value_str); + } + break; + } + case EZLOPI_VALUE_TYPE_ITEM: + case EZLOPI_VALUE_TYPE_STRING: + case EZLOPI_VALUE_TYPE_INTERVAL: + { + __cjson_add_string(cj_field, ezlopi_value_str, field_node->value.value_string); + break; + } + case EZLOPI_VALUE_TYPE_BLOCKS: + { + cJSON *vlaue_block_array = cJSON_AddArrayToObject(cj_field, ezlopi_value_str); + if (vlaue_block_array) + { + l_when_block_v2_t *curr_when_block = field_node->value.when_block; + while (curr_when_block) + { + cJSON *cj_when_block = ezlopi_scenes_cjson_create_when_block(curr_when_block); + if (cj_when_block) + { + if (!cJSON_AddItemToArray(vlaue_block_array, cj_when_block)) + { + cJSON_Delete(cj_when_block); + } + } + curr_when_block = curr_when_block->next; + } + + CJSON_TRACE(ezlopi_value_str, vlaue_block_array); + } + break; + } + case EZLOPI_VALUE_TYPE_DICTIONARY: + case EZLOPI_VALUE_TYPE_ARRAY: + case EZLOPI_VALUE_TYPE_RGB: + case EZLOPI_VALUE_TYPE_CAMERA_STREAM: + case EZLOPI_VALUE_TYPE_USER_CODE: + case EZLOPI_VALUE_TYPE_WEEKLY_INTERVAL: + case EZLOPI_VALUE_TYPE_DAILY_INTERVAL: + case EZLOPI_VALUE_TYPE_TOKEN: + case EZLOPI_VALUE_TYPE_BUTTON_STATE: + case EZLOPI_VALUE_TYPE_USER_LOCK_OPERATION: + case EZLOPI_VALUE_TYPE_USER_CODE_ACTION: + case EZLOPI_VALUE_TYPE_SOUND_INFO: + case EZLOPI_VALUE_TYPE_CAMERA_HOTZONE: + case EZLOPI_VALUE_TYPE_HOTZONE_MATCH: + case EZLOPI_VALUE_TYPE_GEOFENCE: + case EZLOPI_VALUE_TYPE_ILLUMINANCE: + case EZLOPI_VALUE_TYPE_PRESSURE: + case EZLOPI_VALUE_TYPE_SUBSTANCE_AMOUNT: + case EZLOPI_VALUE_TYPE_POWER: + case EZLOPI_VALUE_TYPE_VELOCITY: + case EZLOPI_VALUE_TYPE_ACCELERATION: + case EZLOPI_VALUE_TYPE_DIRECTION: + case EZLOPI_VALUE_TYPE_GENERAL_PURPOSE: + case EZLOPI_VALUE_TYPE_ACIDITY: + case EZLOPI_VALUE_TYPE_ELECTRIC_POTENTIAL: + case EZLOPI_VALUE_TYPE_ELECTRIC_CURRENT: + case EZLOPI_VALUE_TYPE_FORCE: + case EZLOPI_VALUE_TYPE_IRRADIANCE: + case EZLOPI_VALUE_TYPE_PRECIPITATION: + case EZLOPI_VALUE_TYPE_LENGTH: + case EZLOPI_VALUE_TYPE_MASS: + case EZLOPI_VALUE_TYPE_VOLUME_FLOW: + case EZLOPI_VALUE_TYPE_VOLUME: + case EZLOPI_VALUE_TYPE_ANGLE: + case EZLOPI_VALUE_TYPE_FREQUENCY: + case EZLOPI_VALUE_TYPE_SEISMIC_INTENSITY: + case EZLOPI_VALUE_TYPE_SEISMIC_MAGNITUDE: + case EZLOPI_VALUE_TYPE_ULTRAVIOLET: + case EZLOPI_VALUE_TYPE_ELECTRICAL_RESISTANCE: + case EZLOPI_VALUE_TYPE_ELECTRICAL_CONDUCTIVITY: + case EZLOPI_VALUE_TYPE_LOUDNESS: + case EZLOPI_VALUE_TYPE_MOISTURE: + case EZLOPI_VALUE_TYPE_TIME: + case EZLOPI_VALUE_TYPE_RADON_CONCENTRATION: + case EZLOPI_VALUE_TYPE_BLOOD_PRESSURE: + case EZLOPI_VALUE_TYPE_ENERGY: + case EZLOPI_VALUE_TYPE_RF_SIGNAL_STRENGTH: + case EZLOPI_VALUE_TYPE_TEMPERATURE: + case EZLOPI_VALUE_TYPE_HUMIDITY: + case EZLOPI_VALUE_TYPE_KILO_VOLT_AMPERE_HOUR: + case EZLOPI_VALUE_TYPE_REACTIVE_POWER_INSTANT: + case EZLOPI_VALUE_TYPE_AMOUNT_OF_USEFUL_ENERGY: + case EZLOPI_VALUE_TYPE_REACTIVE_POWER_CONSUMPTION: + case EZLOPI_VALUE_TYPE_DEVICE: + case EZLOPI_VALUE_TYPE_EXPRESSION: + case EZLOPI_VALUE_TYPE_24_HOURS_TIME: + case EZLOPI_VALUE_TYPE_24_HOURS_TIME_ARRAY: + case EZLOPI_VALUE_TYPE_INT_ARRAY: + case EZLOPI_VALUE_TYPE_HMS_INTERVAL: + + case EZLOPI_VALUE_TYPE_NONE: + case EZLOPI_VALUE_TYPE_MAX: + { + TRACE_W("Value type not implemented!, curr-type: %d", field_node->value_type); + break; + } + default: + { + const char *value_type_name = ezlopi_scene_get_scene_value_type_name_v2(field_node->value_type); + TRACE_E("Value type not matched!, curr-type[%d]: %s ", field_node->value_type, value_type_name ? value_type_name : ezlopi_null_str); + break; + } + } } } + + return cj_field; } static void ezlopi_scenes_cjson_add_user_notifications(cJSON *root, l_user_notification_v2_t *user_notifications) { - if (root && user_notifications) + if (root) { - cJSON *cj_user_notifications_array = cJSON_AddArrayToObject(root, "user_notifications"); + cJSON *cj_user_notifications_array = cJSON_AddArrayToObject(root, ezlopi_user_notifications_str); if (cj_user_notifications_array) { while (user_notifications) @@ -48,9 +184,9 @@ static void ezlopi_scenes_cjson_add_user_notifications(cJSON *root, l_user_notif static void ezlopi_scenes_cjson_add_house_modes(cJSON *root, l_house_modes_v2_t *house_modes) { - if (root && house_modes) + if (root) { - cJSON *cj_user_notifications_array = cJSON_AddArrayToObject(root, "house_modes"); + cJSON *cj_user_notifications_array = cJSON_AddArrayToObject(root, ezlopi_house_modes_str); if (cj_user_notifications_array) { while (house_modes) @@ -73,24 +209,24 @@ static void ezlopi_scenes_cjson_add_when_block_options(cJSON *cj_block_array, l_ { if (cj_block_array && when_block) { - cJSON *cj_block_options = cJSON_AddObjectToObject(cj_block_array, "blockOptions"); + cJSON *cj_block_options = cJSON_AddObjectToObject(cj_block_array, ezlopi_blockOptions_str); if (cj_block_options) { - cJSON *cj_method = cJSON_AddObjectToObject(cj_block_options, "method"); + cJSON *cj_method = cJSON_AddObjectToObject(cj_block_options, ezlopi_key_method_str); if (cj_method) { if ((when_block->block_options.method.type > EZLOPI_SCENE_METHOD_TYPE_NONE) && (when_block->block_options.method.type < EZLOPI_SCENE_METHOD_TYPE_MAX)) { const char *method_type_name = ezlopi_scene_get_scene_method_name(when_block->block_options.method.type); - ezlopi_scenes_cjson_add_string(cj_method, "name", method_type_name ? method_type_name : ""); + __cjson_add_string(cj_method, ezlopi_name_str, method_type_name ? method_type_name : ezlopi__str); } else { TRACE_E("Method type error"); } - cJSON *cj_args = cJSON_AddObjectToObject(cj_method, "args"); + cJSON *cj_args = cJSON_AddObjectToObject(cj_method, ezlopi_args_str); if (cj_args) { l_fields_v2_t *curr_field = when_block->fields; @@ -105,30 +241,30 @@ static void ezlopi_scenes_cjson_add_when_block_options(cJSON *cj_block_array, l_ } } -static void ezlopi_scenes_cjson_add_action_block_options(cJSON *cj_block_array, l_action_block_v2_t *action_block) +static void __cjson_add_action_block_options(cJSON *cj_block_array, l_action_block_v2_t *action_block) { if (cj_block_array && action_block) { - cJSON *cj_block_options = cJSON_AddObjectToObject(cj_block_array, "blockOptions"); + cJSON *cj_block_options = cJSON_AddObjectToObject(cj_block_array, ezlopi_blockOptions_str); if (cj_block_options) { - cJSON *cj_method = cJSON_AddObjectToObject(cj_block_options, "method"); + cJSON *cj_method = cJSON_AddObjectToObject(cj_block_options, ezlopi_key_method_str); if (cj_method) { if ((action_block->block_options.method.type > EZLOPI_SCENE_METHOD_TYPE_NONE) && (action_block->block_options.method.type < EZLOPI_SCENE_METHOD_TYPE_MAX)) { const char *method_type_name = ezlopi_scene_get_scene_method_name(action_block->block_options.method.type); - ezlopi_scenes_cjson_add_string(cj_method, "name", method_type_name ? method_type_name : ""); + __cjson_add_string(cj_method, ezlopi_name_str, method_type_name ? method_type_name : ezlopi__str); } else { TRACE_E("Method type error"); } - cJSON *cj_args = cJSON_AddObjectToObject(cj_method, "args"); + cJSON *cj_args = cJSON_AddObjectToObject(cj_method, ezlopi_args_str); if (cj_args) { l_fields_v2_t *curr_field = action_block->fields; @@ -143,13 +279,13 @@ static void ezlopi_scenes_cjson_add_action_block_options(cJSON *cj_block_array, } } -static void ezlopi_scenes_cjson_add_action_delay(cJSON *cj_action_block, s_action_delay_v2_t *action_delay) +static void __cjson_add_action_delay(cJSON *cj_action_block, s_action_delay_v2_t *action_delay) { if (cj_action_block && action_delay) { if (action_delay->days || action_delay->hours || action_delay->minutes || action_delay->seconds) { - cJSON *cj_action_delay = cJSON_AddObjectToObject(cj_action_block, "delay"); + cJSON *cj_action_delay = cJSON_AddObjectToObject(cj_action_block, ezlopi_delay_str); if (cj_action_delay) { cJSON_AddNumberToObject(cj_action_delay, "days", action_delay->days); @@ -161,11 +297,11 @@ static void ezlopi_scenes_cjson_add_action_delay(cJSON *cj_action_block, s_actio } } -static void ezlopi_scenes_cjson_add_fields(cJSON *cj_block, l_fields_v2_t *fields) +static void __cjson_add_fields(cJSON *cj_block, l_fields_v2_t *fields) { if (cj_block) { - cJSON *cj_fields_array = cJSON_AddArrayToObject(cj_block, "fields"); + cJSON *cj_fields_array = cJSON_AddArrayToObject(cj_block, ezlopi_fields_str); if (cj_fields_array) { l_fields_v2_t *curr_field = fields; @@ -174,9 +310,9 @@ static void ezlopi_scenes_cjson_add_fields(cJSON *cj_block, l_fields_v2_t *field cJSON *cj_field = cJSON_CreateObject(); if (cj_field) { - ezlopi_scenes_cjson_add_string(cj_field, "name", curr_field->name); + __cjson_add_string(cj_field, ezlopi_name_str, curr_field->name); const char *value_type_name = ezlopi_scene_get_scene_value_type_name_v2(curr_field->value_type); - ezlopi_scenes_cjson_add_string(cj_field, "type", value_type_name ? value_type_name : ""); + __cjson_add_string(cj_field, ezlopi_type_str, value_type_name ? value_type_name : ezlopi__str); switch (curr_field->value_type) { @@ -186,44 +322,44 @@ static void ezlopi_scenes_cjson_add_fields(cJSON *cj_block, l_fields_v2_t *field } case EZLOPI_VALUE_TYPE_INT: { - cJSON_AddNumberToObject(cj_field, "value", curr_field->value.value_double); + cJSON_AddNumberToObject(cj_field, ezlopi_value_str, curr_field->value.value_double); break; } case EZLOPI_VALUE_TYPE_BOOL: { if (0 == curr_field->value.value_double) { - cJSON_AddFalseToObject(cj_field, "value"); + cJSON_AddFalseToObject(cj_field, ezlopi_value_str); } else { - cJSON_AddTrueToObject(cj_field, "value"); + cJSON_AddTrueToObject(cj_field, ezlopi_value_str); } break; } case EZLOPI_VALUE_TYPE_FLOAT: { - cJSON_AddNumberToObject(cj_field, "value", curr_field->value.value_double); + cJSON_AddNumberToObject(cj_field, ezlopi_value_str, curr_field->value.value_double); break; } case EZLOPI_VALUE_TYPE_STRING: { - ezlopi_scenes_cjson_add_string(cj_field, "value", curr_field->value.value_string); + __cjson_add_string(cj_field, ezlopi_value_str, curr_field->value.value_string); break; } case EZLOPI_VALUE_TYPE_ITEM: { - ezlopi_scenes_cjson_add_string(cj_field, "value", curr_field->value.value_string); + __cjson_add_string(cj_field, ezlopi_value_str, curr_field->value.value_string); break; } case EZLOPI_VALUE_TYPE_INTERVAL: { - ezlopi_scenes_cjson_add_string(cj_field, "value", curr_field->value.value_string); + __cjson_add_string(cj_field, ezlopi_value_str, curr_field->value.value_string); break; } case EZLOPI_VALUE_TYPE_BLOCKS: { - cJSON *vlaue_block_array = cJSON_AddArrayToObject(cj_field, "value"); + cJSON *vlaue_block_array = cJSON_AddArrayToObject(cj_field, ezlopi_value_str); if (vlaue_block_array) { l_when_block_v2_t *curr_when_block = curr_field->value.when_block; @@ -241,12 +377,7 @@ static void ezlopi_scenes_cjson_add_fields(cJSON *cj_block, l_fields_v2_t *field curr_when_block = curr_when_block->next; } - char *str_vlaue = cJSON_Print(vlaue_block_array); - if (str_vlaue) - { - TRACE_I("value: %s", str_vlaue); - free(str_vlaue); - } + CJSON_TRACE(ezlopi_value_str, vlaue_block_array); } break; } @@ -317,42 +448,10 @@ static void ezlopi_scenes_cjson_add_fields(cJSON *cj_block, l_fields_v2_t *field default: { const char *value_type_name = ezlopi_scene_get_scene_value_type_name_v2(curr_field->value_type); - TRACE_E("Value type not matched!, curr-type[%d]: %s ", curr_field->value_type, value_type_name ? value_type_name : "null"); + TRACE_E("Value type not matched!, curr-type[%d]: %s ", curr_field->value_type, value_type_name ? value_type_name : ezlopi_null_str); break; } } -#if 0 - case SCENE_VALUE_TYPE_BOOL: - { - ezlopi_scenes_cjson_add_string(cj_field, "type", "bool"); - if (0 == curr_field->value.value_double) - { - cJSON_AddFalseToObject(cj_field, "value"); - } - else - { - cJSON_AddTrueToObject(cj_field, "value"); - } - break; - } - case SCENE_VALUE_TYPE_INT: - { - ezlopi_scenes_cjson_add_string(cj_field, "type", "int"); - cJSON_AddNumberToObject(cj_field, "value", curr_field->value.value_double); - break; - } - case SCENE_VALUE_TYPE_ITEM: - { - ezlopi_scenes_cjson_add_string(cj_field, "type", "item"); - ezlopi_scenes_cjson_add_string(cj_field, "value", curr_field->value.value_string); - break; - } - default: - { - break; - } - -#endif if (!cJSON_AddItemToArray(cj_fields_array, cj_field)) { @@ -374,17 +473,17 @@ cJSON *ezlopi_scenes_cjson_create_action_block(l_action_block_v2_t *action_block cj_action_block = cJSON_CreateObject(); if (cj_action_block) { - ezlopi_scenes_cjson_add_action_block_options(cj_action_block, action_block); - ezlopi_scenes_cjson_add_string(cj_action_block, "blockType", block_type_str); - ezlopi_scenes_cjson_add_action_delay(cj_action_block, &action_block->delay); - ezlopi_scenes_cjson_add_fields(cj_action_block, action_block->fields); + __cjson_add_action_block_options(cj_action_block, action_block); + __cjson_add_string(cj_action_block, ezlopi_blockType_str, block_type_str); + __cjson_add_action_delay(cj_action_block, &action_block->delay); + __cjson_add_fields(cj_action_block, action_block->fields); } } return cj_action_block; } -void ezlopi_scenes_cjson_add_action_blocks(cJSON *root, l_action_block_v2_t *action_blocks, char *block_type_str) +void ezlopi_scenes_cjson_add_action_blocks(cJSON *root, l_action_block_v2_t *action_blocks, const char *block_type_str) { if (root && action_blocks) { @@ -394,8 +493,7 @@ void ezlopi_scenes_cjson_add_action_blocks(cJSON *root, l_action_block_v2_t *act { while (action_blocks) { - cJSON *cj_then_block = NULL; - ezlopi_scenes_cjson_create_action_block(action_blocks, block_type_str); + cJSON *cj_then_block = ezlopi_scenes_cjson_create_action_block(action_blocks, block_type_str); if (cj_then_block) { if (!cJSON_AddItemToArray(cj_then_block_array, cj_then_block)) @@ -419,25 +517,24 @@ cJSON *ezlopi_scenes_cjson_create_when_block(l_when_block_v2_t *when_block) if (cj_when_block) { ezlopi_scenes_cjson_add_when_block_options(cj_when_block, when_block); - ezlopi_scenes_cjson_add_string(cj_when_block, "blockType", "when"); - ezlopi_scenes_cjson_add_fields(cj_when_block, when_block->fields); + __cjson_add_string(cj_when_block, ezlopi_blockType_str, ezlopi_when_str); + __cjson_add_fields(cj_when_block, when_block->fields); } } return cj_when_block; } -void ezlopi_scenes_cjson_add_when_blocks(cJSON *root, l_when_block_v2_t *when_blocks) +void ezlopi_scenes_cjson_add_when_blocks(cJSON *root, l_when_block_v2_t *when_block_node) { - if (root && when_blocks) + if (root) { - cJSON *cj_when_block_array = cJSON_AddArrayToObject(root, "when"); + cJSON *cj_when_block_array = cJSON_AddArrayToObject(root, ezlopi_when_str); if (cj_when_block_array) { - while (when_blocks) + while (when_block_node) { - cJSON *cj_when_block = NULL; - // ezlopi_scenes_cjson_create_when_block(when_blocks); + cJSON *cj_when_block = ezlopi_scenes_cjson_create_when_block(when_block_node); if (cj_when_block) { if (!cJSON_AddItemToArray(cj_when_block_array, cj_when_block)) @@ -446,7 +543,7 @@ void ezlopi_scenes_cjson_add_when_blocks(cJSON *root, l_when_block_v2_t *when_bl } } - when_blocks = when_blocks->next; + when_block_node = when_block_node->next; } } } @@ -462,20 +559,19 @@ cJSON *ezlopi_scenes_create_cjson_scene(l_scenes_list_v2_t *scene) { char tmp_str[16] = {0}; snprintf(tmp_str, sizeof(tmp_str), "%08x", scene->_id); - // cJSON_AddNumberToObject(cj_scene, "_id", scene->_id); - cJSON_AddStringToObject(cj_scene, "_id", tmp_str); - cJSON_AddBoolToObject(cj_scene, "enabled", scene->enabled); - ezlopi_scenes_cjson_add_string(cj_scene, "group_id", scene->group_id); - cJSON_AddBoolToObject(cj_scene, "is_group", scene->is_group); - ezlopi_scenes_cjson_add_string(cj_scene, "name", scene->name); - ezlopi_scenes_cjson_add_string(cj_scene, "parent_id", scene->parent_id); + cJSON_AddStringToObject(cj_scene, ezlopi__id_str, tmp_str); + cJSON_AddBoolToObject(cj_scene, ezlopi_enabled_str, scene->enabled); + __cjson_add_string(cj_scene, ezlopi_group_id_str, scene->group_id); + cJSON_AddBoolToObject(cj_scene, ezlopi_is_group_str, scene->is_group); + __cjson_add_string(cj_scene, ezlopi_name_str, scene->name); + __cjson_add_string(cj_scene, ezlopi_parent_id_str, scene->parent_id); ezlopi_scenes_cjson_add_user_notifications(cj_scene, scene->user_notifications); ezlopi_scenes_cjson_add_house_modes(cj_scene, scene->house_modes); - ezlopi_scenes_cjson_add_action_blocks(cj_scene, scene->then_block, "then"); ezlopi_scenes_cjson_add_when_blocks(cj_scene, scene->when_block); - ezlopi_scenes_cjson_add_action_blocks(cj_scene, scene->else_block, "else"); + ezlopi_scenes_cjson_add_action_blocks(cj_scene, scene->then_block, ezlopi_then_str); + ezlopi_scenes_cjson_add_action_blocks(cj_scene, scene->else_block, ezlopi_else_str); } } @@ -512,11 +608,27 @@ char *ezlopi_scenes_create_json_string(l_scenes_list_v2_t *scenes_list) if (cj_scenes_array) { scenes_list_str = cJSON_Print(cj_scenes_array); + cJSON_Delete(cj_scenes_array); + if (scenes_list_str) { cJSON_Minify(scenes_list_str); } - cJSON_Delete(cj_scenes_array); } return scenes_list_str; -} \ No newline at end of file +} + +static void __cjson_add_string(cJSON *root, const char *key, const char *value) +{ + if (root && key && value) + { + if (isprint(value[0])) + { + cJSON_AddStringToObject(root, key, value); + } + else + { + cJSON_AddNullToObject(root, key); + } + } +} diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_cjson.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_cjson.h new file mode 100644 index 000000000..7b1570edc --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_cjson.h @@ -0,0 +1,16 @@ +#ifndef __EZLOPI_SCENES_CJSON_H__ +#define __EZLOPI_SCENES_CJSON_H__ + +#include "cJSON.h" +#include "ezlopi_scenes_v2.h" + +cJSON *ezlopi_scenes_cjson_create_then_block(l_action_block_v2_t *then_block); +cJSON *ezlopi_scenes_cjson_create_when_block(l_when_block_v2_t *when_block); +void ezlopi_scenes_cjson_add_then_blocks(cJSON *root, l_action_block_v2_t *then_blocks); +void ezlopi_scenes_cjson_add_when_blocks(cJSON *root, l_when_block_v2_t *when_blocks); +cJSON *ezlopi_scenes_create_cjson_scene(l_scenes_list_v2_t *scene); +cJSON *ezlopi_scenes_create_cjson_scene_list(l_scenes_list_v2_t *scenes_list); +char *ezlopi_scenes_create_json_string(l_scenes_list_v2_t *scenes_list); +cJSON *ezlopi_scene_cjson_get_field(l_fields_v2_t *field_node); + +#endif // __EZLOPI_SCENES_CJSON_H__ \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_delete.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_delete.c index dc4642c13..95e3e0902 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_delete.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_delete.c @@ -27,6 +27,22 @@ void ezlopi_scenes_delete_fields(l_fields_v2_t *fields) { if (fields) { + switch (fields->value_type) + { + case EZLOPI_VALUE_TYPE_TOKEN: + { + if (fields->value.cj_value) + { + cJSON_Delete(fields->value.cj_value); + fields->value.cj_value = NULL; + } + break; + } + default: + { + break; + } + } ezlopi_scenes_delete_fields(fields->next); fields->next = NULL; free(fields); diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_edit.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_edit.c new file mode 100644 index 000000000..99895a9e5 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_edit.c @@ -0,0 +1,27 @@ +#include "ezlopi_scenes_v2.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" + +// static __edit_ + +int ezlopi_scenes_edit_by_id(uint32_t scene_id, cJSON *cj_updated_scene) +{ + CJSON_TRACE("cj_updated_scene", cj_updated_scene); + + int ret = 0; + if (scene_id && cj_updated_scene) + { + l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_scenes_head_v2(); + while (scene_node) + { + if (scene_id == scene_node->_id) + { + + break; + } + scene_node = scene_node->next; + } + } + + return ret; +} \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_expressions.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_expressions.c new file mode 100644 index 000000000..95b672276 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_expressions.c @@ -0,0 +1,856 @@ +#include +#include + +#include "ezlopi_nvs.h" +#include "ezlopi_devices.h" +#include "ezlopi_scenes_v2.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" + +#include "ezlopi_scenes_expressions.h" + +static s_ezlopi_expressions_t *l_expressions_head = NULL; + +static uint32_t __expression_store_to_nvs(uint32_t exp_id, cJSON *cj_expression); +static void __get_expressions_value(s_ezlopi_expressions_t *exp_node, cJSON *cj_value, e_scene_value_type_v2_t value_type); +static s_exp_items_t *__expressions_items_create(cJSON *cj_item); +static void __get_expressions_items(s_ezlopi_expressions_t *exp_node, cJSON *cj_items); + +static s_exp_device_item_names_t *__expressions_device_item_names_create(cJSON *cj_device_item_name); +static void __get_expressions_device_item_names(s_ezlopi_expressions_t *exp_node, cJSON *cj_device_item_names); +static s_ezlopi_expressions_t *__expressions_create_node(uint32_t exp_id, cJSON *cj_expression); + +static bool __check_expression_type_filter(s_ezlopi_expressions_t *exp_node, e_scene_value_type_v2_t *type_filter_arr); +static e_scene_value_type_v2_t *__parse_expression_type_filter(cJSON *cj_params); +static void __add_expression_value(s_ezlopi_expressions_t *exp_node, cJSON *cj_expr); +static void __add_expression_items(s_ezlopi_expressions_t *exp_node, cJSON *cj_params); +static void __add_expression_device_item_names(s_ezlopi_expressions_t *exp_node, cJSON *cj_params); + +int ezlopi_scenes_expressions_delete_by_name(char *expression_name) +{ + int ret = 0; + if (expression_name) + { + size_t del_name_len = strlen(expression_name); + s_ezlopi_expressions_t *curr_expr = l_expressions_head; + s_ezlopi_expressions_t *prev_expr = NULL; + + while (curr_expr) + { + size_t exp_name_len = strlen(curr_expr->name); + size_t cmp_len = del_name_len > exp_name_len ? del_name_len : exp_name_len; + if (0 == strncmp(curr_expr->name, expression_name, cmp_len)) + { + s_ezlopi_expressions_t *del_expression = curr_expr; + + if (del_expression == l_expressions_head) + { + l_expressions_head = l_expressions_head->next; + del_expression->next = NULL; + ret = ezlopi_scenes_expressions_delete_node(del_expression); + } + else + { + prev_expr->next = curr_expr->next; + del_expression->next = NULL; + ret = ezlopi_scenes_expressions_delete_node(del_expression); + } + + break; + } + + prev_expr = curr_expr; + curr_expr = curr_expr->next; + } + } + + return ret; +} + +void ezlopi_scenes_expressions_list_cjson(cJSON *cj_expresson_array, cJSON *cj_params) +{ + if (cj_expresson_array) + { + bool show_code = false; + e_scene_value_type_v2_t *type_filter_array = NULL; + + if (cj_params) + { + CJSON_GET_VALUE_BOOL(cj_params, ezlopi_showCode_str, show_code); + type_filter_array = __parse_expression_type_filter(cj_params); + } + + s_ezlopi_expressions_t *curr_exp = l_expressions_head; + while (curr_exp) + { + if (__check_expression_type_filter(curr_exp, type_filter_array)) + { + cJSON *cj_expr = cJSON_CreateObject(); + if (cj_expr) + { + char exp_id[32]; + snprintf(exp_id, sizeof(exp_id), "%08x", curr_exp->exp_id); + cJSON_AddStringToObject(cj_expr, ezlopi__id_str, exp_id); + cJSON_AddStringToObject(cj_expr, ezlopi_name_str, curr_exp->name); + if (show_code && curr_exp->code) + { + cJSON_AddStringToObject(cj_expr, ezlopi_code_str, curr_exp->code); + } + if (curr_exp->meta_data) + { + cJSON_AddItemReferenceToObject(cj_expr, ezlopi_metadata_str, curr_exp->meta_data); + } + + cJSON *cj_params = cJSON_AddObjectToObject(cj_expr, ezlopi_params_str); + if (cj_params) + { + __add_expression_items(curr_exp, cj_params); + __add_expression_device_item_names(curr_exp, cj_params); + } + + __add_expression_value(curr_exp, cj_expr); + } + + if (!cJSON_AddItemToArray(cj_expresson_array, cj_expr)) + { + cJSON_Delete(cj_expr); + } + } + + curr_exp = curr_exp->next; + } + } +} + +void ezlopi_scenes_expressions_print(s_ezlopi_expressions_t *exp_node) +{ + if (exp_node) + { + TRACE_D("----------------- Expression --------------------"); + TRACE_D("Name: %s", exp_node->name); + TRACE_D("Exp_id: %08x", exp_node->exp_id); + TRACE_D("code: %s", exp_node->code ? exp_node->code : ezlopi__str); + + TRACE_D("-- Items:"); + int count = 0; + s_exp_items_t *items = exp_node->items; + while (items) + { + TRACE_D("\t-------------- item-%d ------------", ++count); + TRACE_D("\tname: %s", items->name); + TRACE_D("\t_id: %08x", items->_id); + items = items->next; + } + TRACE_D("\t-----------------------------------"); + + TRACE_D("-- Device Item Names:"); + count = 0; + s_exp_device_item_names_t *device_item_names = exp_node->device_item_names; + while (device_item_names) + { + TRACE_D("\t----------device-item-names-%d-------", ++count); + TRACE_D("\tname: %s", device_item_names->name); + TRACE_D("\titem-name: %s", device_item_names->item_name); + TRACE_D("\tdevice-name: %s", device_item_names->device_name); + device_item_names = device_item_names->next; + } + TRACE_D("\t-----------------------------------"); + + CJSON_TRACE("meta-data", exp_node->meta_data); + TRACE_D("Is variable: %s", exp_node->variable ? "True" : "False"); + TRACE_D("value-type: %s", ezlopi_scene_get_scene_value_type_name_v2(exp_node->value_type)); + + if (exp_node->exp_value.type > EXPRESSION_VALUE_TYPE_UNDEFINED && exp_node->exp_value.type < EXPRESSION_VALUE_TYPE_MAX) + { + switch (exp_node->exp_value.type) + { + case EXPRESSION_VALUE_TYPE_STRING: + { + TRACE_D("value: %s", exp_node->exp_value.u_value.str_value ? exp_node->exp_value.u_value.str_value : ezlopi__str); + break; + } + case EXPRESSION_VALUE_TYPE_CJ: + { + CJSON_TRACE(ezlopi_value_str, exp_node->exp_value.u_value.cj_value); + break; + } + case EXPRESSION_VALUE_TYPE_BOOL: + { + TRACE_D("value: %s", exp_node->exp_value.u_value.boolean_value ? ezlopi_true_str : ezlopi_false_str); + break; + } + case EXPRESSION_VALUE_TYPE_NUMBER: + { + TRACE_D("value: %lf", exp_node->exp_value.u_value.number_value); + break; + } + + default: + { + TRACE_W("exp_node->exp_value.type: value type is undefined!"); + break; + } + } + } + + TRACE_D("----------------- ---------- --------------------"); + } +} + +uint32_t ezlopi_scenes_expressions_add_to_head(uint32_t exp_id, cJSON *cj_expression) +{ + uint32_t new_exp_id = 0; + + if (l_expressions_head) + { + s_ezlopi_expressions_t *current_exp = l_expressions_head; + while (current_exp->next) + { + current_exp = current_exp->next; + } + + current_exp->next = __expressions_create_node(exp_id, cj_expression); + if (current_exp->next) + { + ezlopi_scenes_expressions_print(current_exp->next); + new_exp_id = current_exp->next->exp_id; + } + } + else + { + l_expressions_head = __expressions_create_node(exp_id, cj_expression); + if (l_expressions_head) + { + ezlopi_scenes_expressions_print(l_expressions_head); + new_exp_id = l_expressions_head->exp_id; + } + } + + return new_exp_id; +} + +void ezlopi_scenes_expressions_delete_exp_item(s_exp_items_t *exp_items) +{ + if (exp_items) + { + ezlopi_scenes_expressions_delete_exp_item(exp_items->next); + free(exp_items); + } +} + +void ezlopi_scenes_expressions_delete_exp_device_item_names(s_exp_device_item_names_t *exp_device_item_names) +{ + if (exp_device_item_names) + { + ezlopi_scenes_expressions_delete_exp_device_item_names(exp_device_item_names->next); + free(exp_device_item_names); + } +} + +s_ezlopi_expressions_t *ezlopi_scenes_expressions_node_pop(void) +{ + s_ezlopi_expressions_t *popped_node = NULL; + + if (l_expressions_head) + { + // if ( check-condition ) + // { + + // } + + s_ezlopi_expressions_t *curr_node = l_expressions_head; + while (curr_node) + { + // if ( check-condition ) + { + } + } + } + + return popped_node; +} + +int ezlopi_scenes_expressions_delete_node(s_ezlopi_expressions_t *exp_node) +{ + int ret = 0; + if (exp_node) + { + ret = ezlopi_scenes_expressions_delete_node(exp_node->next); + + if (exp_node->code) + { + free(exp_node->code); + } + + switch (exp_node->exp_value.type) + { + case EXPRESSION_VALUE_TYPE_STRING: + { + if (exp_node->exp_value.u_value.str_value) + { + free(exp_node->exp_value.u_value.str_value); + exp_node->exp_value.u_value.str_value = NULL; + } + break; + } + case EXPRESSION_VALUE_TYPE_CJ: + { + if (exp_node->exp_value.u_value.cj_value) + { + cJSON_Delete(exp_node->exp_value.u_value.cj_value); + exp_node->exp_value.u_value.cj_value = NULL; + } + break; + } + default: + { + break; + } + } + + if (exp_node->meta_data) + { + free(exp_node->meta_data); + } + + ezlopi_scenes_expressions_delete_exp_item(exp_node->items); + ezlopi_scenes_expressions_delete_exp_device_item_names(exp_node->device_item_names); + + ezlopi_nvs_delete_stored_data_by_id(exp_node->exp_id); + char *exp_ids = ezlopi_nvs_read_scenes_expressions(); + if (exp_ids) + { + cJSON *cj_exp_ids = cJSON_Parse(exp_ids); + free(exp_ids); + + if (cj_exp_ids) + { + CJSON_TRACE("expression-ids", cj_exp_ids); + + uint32_t idx = 0; + cJSON *cj_exp_id = NULL; + while (NULL != (cj_exp_id = cJSON_GetArrayItem(cj_exp_ids, idx))) + { + uint32_t _id = strtoul(cj_exp_id->valuestring, NULL, 16); + if (_id == exp_node->exp_id) + { + cJSON_DeleteItemFromArray(cj_exp_ids, idx); + break; + } + + idx++; + } + + char *updated_ids = cJSON_Print(cj_exp_ids); + TRACE_D("updated-expression-ids: %s", updated_ids); + cJSON_Delete(cj_exp_ids); + + if (updated_ids) + { + cJSON_Minify(updated_ids); + ezlopi_nvs_write_scenes_expressions(updated_ids); + free(updated_ids); + ret = 1; + } + } + } + } + + return ret; +} + +void ezlopi_scenes_expressions_init(void) +{ + char *exp_id_list_str = ezlopi_nvs_read_scenes_expressions(); + if (exp_id_list_str) + { + TRACE_D("exp_id_list_str: %s", exp_id_list_str); + + cJSON *cj_exp_id_list = cJSON_Parse(exp_id_list_str); + if (cj_exp_id_list) + { + uint32_t exp_idx = 0; + cJSON *cj_exp_id = NULL; + + while (NULL != (cj_exp_id = cJSON_GetArrayItem(cj_exp_id_list, exp_idx++))) + { + if (cj_exp_id->valuestring) + { + uint32_t exp_id = strtoul(cj_exp_id->valuestring, NULL, 16); + if (exp_id) + { + char *exp_str = ezlopi_nvs_read_str(cj_exp_id->valuestring); + if (exp_str) + { + cJSON *cj_exp = cJSON_Parse(exp_str); + if (cj_exp) + { + ezlopi_scenes_expressions_add_to_head(exp_id, cj_exp); + cJSON_Delete(cj_exp); + } + + free(exp_str); + } + } + } + } + } + + free(exp_id_list_str); + } + else + { + TRACE_E("Expression's ids not found"); + } +} + +static s_exp_items_t *__expressions_items_create(cJSON *cj_item) +{ + s_exp_items_t *new_item_node = NULL; + + if (cj_item) + { + new_item_node = malloc(sizeof(s_exp_items_t)); + if (new_item_node) + { + memset(new_item_node, 0, sizeof(s_exp_items_t)); + CJSON_GET_VALUE_STRING_BY_COPY(cj_item, ezlopi_name_str, new_item_node->name); + char *item_id_string = NULL; + CJSON_GET_VALUE_STRING(cj_item, ezlopi__id_str, item_id_string); + if (item_id_string) + { + new_item_node->_id = strtoul(item_id_string, NULL, 16); + } + } + else + { + TRACE_E("Error: malloc failed!"); + } + } + + return new_item_node; +} + +static void __get_expressions_items(s_ezlopi_expressions_t *exp_node, cJSON *cj_items) +{ + if (cj_items) + { + uint32_t item_index = 0; + cJSON *cj_item = NULL; + s_exp_items_t *new_item_head = NULL; + s_exp_items_t *curr_item_node = NULL; + + while (NULL != (cj_item = cJSON_GetArrayItem(cj_items, item_index++))) + { + if (new_item_head) + { + curr_item_node->next = __expressions_items_create(cj_item); + curr_item_node = curr_item_node->next; + } + else + { + new_item_head = __expressions_items_create(cj_item); + curr_item_node = new_item_head; + } + } + + if (exp_node->items) + { + s_exp_items_t *exp_item_node = exp_node->items; + while (exp_item_node->next) + { + exp_item_node = exp_item_node->next; + } + + exp_item_node->next = new_item_head; + } + else + { + exp_node->items = new_item_head; + } + } +} + +static s_exp_device_item_names_t *__expressions_device_item_names_create(cJSON *cj_device_item_name) +{ + s_exp_device_item_names_t *new_device_item_name = NULL; + + if (cj_device_item_name) + { + new_device_item_name = malloc(sizeof(s_exp_device_item_names_t)); + if (new_device_item_name) + { + memset(new_device_item_name, 0, sizeof(s_exp_device_item_names_t)); + CJSON_GET_VALUE_STRING_BY_COPY(cj_device_item_name, ezlopi_name_str, new_device_item_name->name); + CJSON_GET_VALUE_STRING_BY_COPY(cj_device_item_name, ezlopi_deviceName_str, new_device_item_name->device_name); + CJSON_GET_VALUE_STRING_BY_COPY(cj_device_item_name, ezlopi_itemName_str, new_device_item_name->item_name); + } + } + + return new_device_item_name; +} + +static void __get_expressions_device_item_names(s_ezlopi_expressions_t *exp_node, cJSON *cj_device_item_names) +{ + if (cj_device_item_names) + { + uint32_t dev_item_name_index = 0; + cJSON *cj_dev_item_name = NULL; + s_exp_device_item_names_t *new_device_item_names_head = NULL; + s_exp_device_item_names_t *cur_device_item_names_head = NULL; + + while (NULL != (cj_dev_item_name = cJSON_GetArrayItem(cj_device_item_names, dev_item_name_index++))) + { + if (new_device_item_names_head) + { + cur_device_item_names_head->next = __expressions_device_item_names_create(cj_dev_item_name); + cur_device_item_names_head = cur_device_item_names_head->next; + } + else + { + new_device_item_names_head = __expressions_device_item_names_create(cj_dev_item_name); + cur_device_item_names_head = new_device_item_names_head; + } + } + + if (exp_node->device_item_names) + { + s_exp_device_item_names_t *exp_device_item_names_node = exp_node->device_item_names; + while (exp_device_item_names_node->next) + { + exp_device_item_names_node = exp_device_item_names_node->next; + } + + exp_device_item_names_node->next = new_device_item_names_head; + } + else + { + exp_node->device_item_names = new_device_item_names_head; + } + } +} + +static void __get_expressions_value(s_ezlopi_expressions_t *exp_node, cJSON *cj_value, e_scene_value_type_v2_t value_type) +{ + if (exp_node && cj_value && value_type) + { + switch (cj_value->type) + { + case cJSON_Number: + { + exp_node->exp_value.u_value.number_value = cj_value->valuedouble; + exp_node->exp_value.type = EXPRESSION_VALUE_TYPE_NUMBER; + TRACE_B("value: %lf", exp_node->exp_value.u_value.number_value); + break; + } + case cJSON_String: + { + uint32_t value_len = strlen(cj_value->valuestring) + 1; + exp_node->exp_value.type = EXPRESSION_VALUE_TYPE_STRING; + exp_node->exp_value.u_value.str_value = malloc(value_len); + if (exp_node->exp_value.u_value.str_value) + { + snprintf(exp_node->exp_value.u_value.str_value, value_len, "%s", cj_value->valuestring); + TRACE_B("value: %s", exp_node->exp_value.u_value.str_value); + } + else + { + TRACE_E("Malloc failed!"); + } + break; + } + case cJSON_True: + { + exp_node->exp_value.type = EXPRESSION_VALUE_TYPE_BOOL; + exp_node->exp_value.u_value.boolean_value = true; + TRACE_B("value: true"); + break; + } + case cJSON_False: + { + exp_node->exp_value.type = EXPRESSION_VALUE_TYPE_BOOL; + exp_node->exp_value.u_value.boolean_value = false; + TRACE_B("value: false"); + break; + } + case cJSON_Array: + case cJSON_Object: + { + TRACE_E("cj_value type: %d", cj_value->type); + exp_node->exp_value.type = EXPRESSION_VALUE_TYPE_CJ; + exp_node->exp_value.u_value.cj_value = cJSON_Duplicate(cj_value, cJSON_True); + break; + } + default: + { + exp_node->exp_value.u_value.number_value = 0; + exp_node->exp_value.type = EXPRESSION_VALUE_TYPE_UNDEFINED; + TRACE_E("Error: Undefine value type: %d", cj_value->type); + break; + } + } + } +} + +static s_ezlopi_expressions_t *__expressions_create_node(uint32_t exp_id, cJSON *cj_expression) +{ + s_ezlopi_expressions_t *new_exp_node = malloc(sizeof(s_ezlopi_expressions_t)); + + if (new_exp_node) + { + char *code_str = NULL; + memset(new_exp_node, 0, sizeof(s_ezlopi_expressions_t)); + + CJSON_GET_VALUE_STRING_BY_COPY(cj_expression, ezlopi_name_str, new_exp_node->name); + CJSON_GET_VALUE_STRING(cj_expression, ezlopi_code_str, code_str); + + if (code_str) + { + uint32_t code_str_len = strlen(code_str) + 1; + new_exp_node->code = malloc(code_str_len); + if (new_exp_node->code) + { + memset(new_exp_node->code, 0, code_str_len); + strcpy(new_exp_node->code, code_str); + } + else + { + TRACE_E("Error: Failed to alloc code-string!"); + } + } + + cJSON *cj_params = cJSON_GetObjectItem(cj_expression, ezlopi_params_str); + + if (cj_params) + { + cJSON *cj_items = cJSON_GetObjectItem(cj_params, ezlopi_items_str); + __get_expressions_items(new_exp_node, cj_items); + + cJSON *cj_device_item_names = cJSON_GetObjectItem(cj_params, ezlopi_device_item_names_str); + __get_expressions_device_item_names(new_exp_node, cj_device_item_names); + } + + CJSON_GET_VALUE_BOOL(cj_expression, ezlopi_variable_str, new_exp_node->variable); + + new_exp_node->meta_data = cJSON_DetachItemFromObject(cj_expression, ezlopi_metadata_str); + new_exp_node->value_type = ezlopi_scenes_get_expressions_value_type(cJSON_GetObjectItem(cj_expression, ezlopi_valueType_str)); + __get_expressions_value(new_exp_node, cJSON_GetObjectItem(cj_expression, ezlopi_value_str), new_exp_node->value_type); + + new_exp_node->exp_id = __expression_store_to_nvs(exp_id, cj_expression); + ezlopi_scenes_expressions_print(new_exp_node); + } + + return new_exp_node; +} + +static uint32_t __expression_store_to_nvs(uint32_t exp_id, cJSON *cj_expression) +{ + if (0 == exp_id) + { + char *exp_string = cJSON_Print(cj_expression); + if (exp_string) + { + cJSON_Minify(exp_string); + exp_id = ezlopi_cloud_generate_expression_id(); + if (exp_id) + { + char exp_id_str[32]; + snprintf(exp_id_str, sizeof(exp_id_str), "%08x", exp_id); + + if (ezlopi_nvs_write_str(exp_string, strlen(exp_string), exp_id_str)) + { + bool free_exp_id_list_str = 1; + char *exp_id_list_str = ezlopi_nvs_read_scenes_expressions(); + if (NULL == exp_id_list_str) + { + exp_id_list_str = "[]"; + free_exp_id_list_str = 0; + TRACE_W("Expressions ids-list not found in NVS"); + } + + TRACE_D("Expressions-IDs: %s", exp_id_list_str); + cJSON *cj_exp_id_list = cJSON_Parse(exp_id_list_str); + + if (free_exp_id_list_str) + { + free(exp_id_list_str); + exp_id_list_str = NULL; + } + + CJSON_TRACE("cj_esp-ids", cj_exp_id_list); + + if (cj_exp_id_list) + { + TRACE_D("Here"); + cJSON *cj_exp_id = cJSON_CreateString(exp_id_str); + if (cj_exp_id) + { + TRACE_D("Here"); + if (cJSON_AddItemToArray(cj_exp_id_list, cj_exp_id)) + { + exp_id_list_str = cJSON_Print(cj_exp_id_list); + if (exp_id_list_str) + { + cJSON_Minify(exp_id_list_str); + ezlopi_nvs_write_scenes_expressions(exp_id_list_str); + free(exp_id_list_str); + } + } + else + { + cJSON_Delete(cj_exp_id); + } + } + + cJSON_Delete(cj_exp_id_list); + } + } + else + { + } + } + + free(exp_string); + } + } + else + { + ezlopi_cloud_update_expression_id(exp_id); + } + + return exp_id; +} + +static bool __check_expression_type_filter(s_ezlopi_expressions_t *exp_node, e_scene_value_type_v2_t *type_filter_arr) +{ + bool ret = true; + if (type_filter_arr) + { + ret = false; + uint32_t idx = 0; + while (type_filter_arr[idx]) + { + if (type_filter_arr[idx] == exp_node->value_type) + { + ret = true; + break; + } + idx++; + } + } + + return ret; +} + +static e_scene_value_type_v2_t *__parse_expression_type_filter(cJSON *cj_params) +{ + e_scene_value_type_v2_t *type_filter_array = NULL; + cJSON *cj_types_filter_array = cJSON_GetObjectItem(cj_params, ezlopi_filterTypes_str); + if (cj_types_filter_array) + { + type_filter_array = calloc(sizeof(e_scene_value_type_v2_t), cJSON_GetArraySize(cj_types_filter_array) + 1); + if (type_filter_array) + { + uint32_t idx = 0; + cJSON *cj_type = NULL; + while (NULL != (cj_type = cJSON_GetArrayItem(cj_types_filter_array, idx))) + { + type_filter_array[idx] = ezlopi_scenes_get_expressions_value_type(cj_type); + idx++; + } + type_filter_array[idx] = 0; + } + } + + return type_filter_array; +} + +static void __add_expression_value(s_ezlopi_expressions_t *exp_node, cJSON *cj_expr) +{ + if (EZLOPI_VALUE_TYPE_NONE < exp_node->value_type && EZLOPI_VALUE_TYPE_MAX > exp_node->value_type) + { + cJSON_AddStringToObject(cj_expr, ezlopi_valueType_str, ezlopi_scene_get_scene_value_type_name_v2(exp_node->value_type)); + switch (exp_node->exp_value.type) + { + case EXPRESSION_VALUE_TYPE_STRING: + { + cJSON_AddStringToObject(cj_expr, ezlopi_value_str, exp_node->exp_value.u_value.str_value); + break; + } + case EXPRESSION_VALUE_TYPE_BOOL: + { + cJSON_AddBoolToObject(cj_expr, ezlopi_value_str, exp_node->exp_value.u_value.boolean_value); + break; + } + case EXPRESSION_VALUE_TYPE_NUMBER: + { + cJSON_AddNumberToObject(cj_expr, ezlopi_value_str, exp_node->exp_value.u_value.number_value); + break; + } + case EXPRESSION_VALUE_TYPE_CJ: + { + break; + } + + default: + break; + } + } +} + +static void __add_expression_items(s_ezlopi_expressions_t *exp_node, cJSON *cj_params) +{ + if (exp_node->items) + { + cJSON *cj_items = cJSON_AddArrayToObject(cj_params, ezlopi_items_str); + if (cj_items) + { + s_exp_items_t *curr_item = exp_node->items; + while (curr_item) + { + cJSON *cj_item = cJSON_CreateObject(); + if (cj_item) + { + cJSON_AddStringToObject(cj_item, ezlopi_name_str, curr_item->name); + + char id_str[32]; + snprintf(id_str, sizeof(id_str), "%08x", curr_item->_id); + cJSON_AddStringToObject(cj_item, ezlopi__id_str, id_str); + if (!cJSON_AddItemToArray(cj_items, cj_item)) + { + cJSON_Delete(cj_item); + } + } + curr_item = curr_item->next; + } + } + } +} + +static void __add_expression_device_item_names(s_ezlopi_expressions_t *exp_node, cJSON *cj_params) +{ + if (exp_node->device_item_names) + { + cJSON *cj_device_item_names = cJSON_AddArrayToObject(cj_params, ezlopi_device_item_names_str); + if (cj_device_item_names) + { + s_exp_device_item_names_t *curr_device_item_names = exp_node->device_item_names; + while (curr_device_item_names) + { + cJSON *cj_device_item_name = cJSON_CreateObject(); + if (cj_device_item_name) + { + cJSON_AddStringToObject(cj_device_item_name, ezlopi_name_str, curr_device_item_names->name); + cJSON_AddStringToObject(cj_device_item_name, ezlopi_deviceName_str, curr_device_item_names->device_name); + cJSON_AddStringToObject(cj_device_item_name, ezlopi_itemName_str, curr_device_item_names->item_name); + + if (!cJSON_AddItemToArray(cj_device_item_names, cj_device_item_name)) + { + cJSON_Delete(cj_device_item_name); + } + } + curr_device_item_names = curr_device_item_names->next; + } + } + } +} diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_expressions.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_expressions.h new file mode 100644 index 000000000..b2b578d3a --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_expressions.h @@ -0,0 +1,137 @@ +#ifndef __EZLOPI_SCENES_EXPRESSIONS_H__ +#define __EZLOPI_SCENES_EXPRESSIONS_H__ + +#include "string.h" +#include "inttypes.h" +#include "stdbool.h" + +#include "ezlopi_scenes_v2.h" + +typedef struct s_exp_items +{ + char name[48]; + uint32_t _id; + struct s_exp_items *next; + +} s_exp_items_t; + +typedef struct s_exp_device_item_names +{ + char name[48]; + char device_name[48]; + char item_name[48]; + struct s_exp_device_item_names *next; + +} s_exp_device_item_names_t; + +typedef enum e_exp_value_type +{ + EXPRESSION_VALUE_TYPE_UNDEFINED = 0, // type not defined + EXPRESSION_VALUE_TYPE_STRING, // char * + EXPRESSION_VALUE_TYPE_CJ, // cJSON * + EXPRESSION_VALUE_TYPE_BOOL, // bool + EXPRESSION_VALUE_TYPE_NUMBER, // number + + EXPRESSION_VALUE_TYPE_MAX, +} e_exp_value_type_t; + +typedef union u_exp_value +{ + char *str_value; + cJSON *cj_value; + bool boolean_value; + double number_value; +} u_exp_value_t; + +typedef struct s_exp_value +{ + u_exp_value_t u_value; + e_exp_value_type_t type; +} s_exp_value_t; + +typedef struct s_ezlopi_expressions +{ + uint32_t exp_id; + char name[32]; + char *code; + s_exp_items_t *items; + s_exp_device_item_names_t *device_item_names; + cJSON *meta_data; // not used for now + bool variable; + + s_exp_value_t exp_value; + e_scene_value_type_v2_t value_type; // value type returned by expression + // e_exp_value_type_t exp_value_type; + + struct s_ezlopi_expressions *next; + +} s_ezlopi_expressions_t; + +/** + * @brief Fetch expressions from NVS flash and populate to linklist + * + */ +void ezlopi_scenes_expressions_init(void); + +/** + * @brief Populate new expression to the linklist, and store it to the flash + * + * @param exp_id expression id, if is zero then 'cj_expression' is cosidered as new expression and stored to nvs + * @param cj_expression cJSON pointer to the expression + * @return uint32_t expression id, if input 'exp_id' is zero then new expression-id is created and returned + */ +uint32_t ezlopi_scenes_expressions_add_to_head(uint32_t exp_id, cJSON *cj_expression); + +/** + * @brief POP the expression from linklist + * + * @return s_ezlopi_expressions_t* + */ +s_ezlopi_expressions_t *ezlopi_scenes_expressions_node_pop(void); + +/** + * @brief Delete the expression-item and its childs + * + * @param exp_items + */ +void ezlopi_scenes_expressions_delete_exp_item(s_exp_items_t *exp_items); + +/** + * @brief Delete the expression-device item name and its childs + * + * @param exp_device_item_names + */ +void ezlopi_scenes_expressions_delete_exp_device_item_names(s_exp_device_item_names_t *exp_device_item_names); + + + +/** + * @brief construct expressions in cJSON and add it to cj_expression_array + * + * @param cj_expresson_array + */ +void ezlopi_scenes_expressions_list_cjson(cJSON *cj_expresson_array, cJSON *cj_params); + +/** + * @brief print the informations inside exp-node + * + * @param exp_node + */ +void ezlopi_scenes_expressions_print(s_ezlopi_expressions_t *exp_node); + +/** + * @brief Delete all the expressions in the linked list + * + * @param exp_node + */ +int ezlopi_scenes_expressions_delete_node(s_ezlopi_expressions_t *exp_node); + +/** + * @brief Delete the expression by its name + * + * @param expression_name + * @return int return 1 on suceess and 0 on failed + */ +int ezlopi_scenes_expressions_delete_by_name(char *expression_name); + +#endif // __EZLOPI_SCENES_EXPRESSIONS_H__ diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_notifications.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_notifications.c new file mode 100644 index 000000000..6b959a021 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_notifications.c @@ -0,0 +1,70 @@ +#include + +#include "trace.h" + +#include "ezlopi_nvs.h" +#include "ezlopi_scenes_v2.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" +#include "ezlopi_scenes_notifications.h" + +void ezlopi_scenes_notifications_add(cJSON *cj_notifications) +{ + if (cj_notifications) + { + cJSON *cj_scene_id = cJSON_GetObjectItem(cj_notifications, ezlopi_notifications_str); + cJSON *cj_user_id = cJSON_GetObjectItem(cj_notifications, ezlopi_userId_str); + + if (cj_scene_id && cj_scene_id->valuestring && cj_user_id && cj_user_id->valuestring) + { + char *scene_str = ezlopi_nvs_read_str(cj_scene_id->valuestring); + if (scene_str) + { + cJSON *cj_scene = cJSON_Parse(scene_str); + free(scene_str); + + if (cj_scene) + { + cJSON *cj_user_notifications = cJSON_GetObjectItem(cj_scene, ezlopi_user_notifications_str); + if (cj_user_notifications) + { + cJSON_AddItemReferenceToArray(cj_user_notifications, cj_user_id); + } + + CJSON_TRACE("updated-scene", cj_scene); + char *updated_scene_str = cJSON_Print(cj_scene); + cJSON_Delete(cj_scene); + + if (updated_scene_str) + { + ezlopi_nvs_write_str(updated_scene_str, strlen(updated_scene_str), cj_scene_id->valuestring); + free(updated_scene_str); + } + } + } + + uint32_t scene_id = strtoul(cj_scene_id->valuestring, NULL, 16); + + if (scene_id) + { + l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_scenes_head_v2(); + while (scene_node) + { + if (scene_id == scene_node->_id) + { + ezlopi_scene_add_users_in_notifications(scene_node, cj_user_id); + break; + } + scene_node = scene_node->next; + } + } + } + } +} + +void ezlopi_scenes_notifications_remove(cJSON *cj_notifications) +{ + if (cj_notifications) + { + } +} diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_notifications.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_notifications.h new file mode 100644 index 000000000..f4d27ed36 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_notifications.h @@ -0,0 +1,10 @@ +#ifndef __EZLOPI_SCENES_NOTIFICATIONS_H__ +#define __EZLOPI_SCENES_NOTIFICATIONS_H__ + +#include "cJSON.h" +#include "ezlopi_scenes_v2.h" + +// void ezlopi_scenes_notifications_add(cJSON *cj_notifications); +void ezlopi_scenes_notifications_remove(cJSON *cj_notifications); + +#endif // __EZLOPI_SCENES_NOTIFICATIONS_H__ diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.c index c0dcd5853..d03b340eb 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.c @@ -5,34 +5,36 @@ #include "ezlopi_devices.h" #include "ezlopi_scenes_v2.h" +#include "ezlopi_cloud_constants.h" #include "ezlopi_scenes_operators.h" -static const char *const ezlopi_scenes_operators_op[] = { -#define SCENES_OPERATORS(OPERATOR, op, name, method) op, -#include "ezlopi_scenes_operators_macros.h" -#undef SCENES_OPERATORS +/************* Numeric ************/ +static const char *const ezlopi_scenes_num_cmp_operators_op[] = { +#define SCENES_NUM_COMP_OPERATORS(OPERATOR, op, name, method) op, +#include "__operators_macros/__numeric_comparision_operators_macros.h" +#undef SCENES_NUM_COMP_OPERATORS }; -static const char *const ezlopi_scenes_operators_name[] = { -#define SCENES_OPERATORS(OPERATOR, op, name, method) name, -#include "ezlopi_scenes_operators_macros.h" -#undef SCENES_OPERATORS +static const char *const ezlopi_scenes_num_cmp_operators_name[] = { +#define SCENES_NUM_COMP_OPERATORS(OPERATOR, op, name, method) name, +#include "__operators_macros/__numeric_comparision_operators_macros.h" +#undef SCENES_NUM_COMP_OPERATORS }; -static const char *const ezlopi_scenes_operators_method[] = { -#define SCENES_OPERATORS(OPERATOR, op, name, method) method, -#include "ezlopi_scenes_operators_macros.h" -#undef SCENES_OPERATORS +static const char *const ezlopi_scenes_num_cmp_operators_method[] = { +#define SCENES_NUM_COMP_OPERATORS(OPERATOR, op, name, method) method, +#include "__operators_macros/__numeric_comparision_operators_macros.h" +#undef SCENES_NUM_COMP_OPERATORS }; -e_scene_cmp_operators_t ezlopi_scenes_operators_get_enum(char *operator_str) +e_scene_num_cmp_operators_t ezlopi_scenes_numeric_comparator_operators_get_enum(char *operator_str) { - e_scene_cmp_operators_t ret = SCENES_OPERATORS_LESS; + e_scene_num_cmp_operators_t ret = SCENES_NUM_COMP_OPERATORS_NONE + 1; if (operator_str) { - while ((ret <= SCENES_OPERATORS_MAX) && ezlopi_scenes_operators_op[ret]) + while ((ret <= SCENES_NUM_COMP_OPERATORS_MAX) && ezlopi_scenes_num_cmp_operators_op[ret]) { - if (0 == strcmp(ezlopi_scenes_operators_op[ret], operator_str)) + if (0 == strcmp(ezlopi_scenes_num_cmp_operators_op[ret], operator_str)) { break; } @@ -42,40 +44,39 @@ e_scene_cmp_operators_t ezlopi_scenes_operators_get_enum(char *operator_str) return ret; } -char *ezlopi_scenes_operators_get_op(e_scene_cmp_operators_t operator) +const char *ezlopi_scenes_numeric_comparator_operators_get_op(e_scene_num_cmp_operators_t operator) { - char *ret = NULL; - if ((operator> SCENES_OPERATORS_NONE) && (operator SCENES_NUM_COMP_OPERATORS_NONE) && (operator SCENES_OPERATORS_NONE) && (operator SCENES_NUM_COMP_OPERATORS_NONE) && (operator SCENES_OPERATORS_NONE) && (operator SCENES_NUM_COMP_OPERATORS_NONE) && (operatorfunc(EZLOPI_ACTION_GET_EZLOPI_VALUE, item, (void *)cj_item_value, NULL); - cJSON *cj_value = cJSON_GetObjectItem(cj_item_value, "value"); + cJSON *cj_value = cJSON_GetObjectItem(cj_item_value, ezlopi_value_str); if (cj_value) { item_value = cj_value->valuedouble; } - break; } + + break; } item = item->next; } device = device->next; } - switch (ezlopi_scenes_operators_get_enum(comparator_field->value.value_string)) + switch (ezlopi_scenes_numeric_comparator_operators_get_enum(comparator_field->value.value_string)) { - case SCENES_OPERATORS_LESS: + case SCENES_NUM_COMP_OPERATORS_LESS: { ret = (item_value < value_field->value.value_double); break; } - case SCENES_OPERATORS_LESS_EQUAL: + case SCENES_NUM_COMP_OPERATORS_LESS_EQUAL: { ret = (item_value <= value_field->value.value_double); break; } - case SCENES_OPERATORS_GREATER: + case SCENES_NUM_COMP_OPERATORS_GREATER: { ret = (item_value > value_field->value.value_double); break; } - case SCENES_OPERATORS_GREATER_EQUAL: + case SCENES_NUM_COMP_OPERATORS_GREATER_EQUAL: { ret = (item_value >= value_field->value.value_double); break; } - case SCENES_OPERATORS_EQUAL: + case SCENES_NUM_COMP_OPERATORS_EQUAL: { ret = (item_value == value_field->value.value_double); break; } - case SCENES_OPERATORS_NOT_EQUAL: + case SCENES_NUM_COMP_OPERATORS_NOT_EQUAL: { ret = (item_value != value_field->value.value_double); break; } - case SCENES_OPERATORS_BETWEEN: + case SCENES_NUM_COMP_OPERATORS_BETWEEN: { TRACE_W("'SCENES_OPERATORS_BETWEEN' not implemented!"); break; } - case SCENES_OPERATORS_NOT_BETWEEN: + case SCENES_NUM_COMP_OPERATORS_NOT_BETWEEN: { TRACE_W("'SCENES_OPERATORS_NOT_BETWEEN' not implemented!"); break; } - case SCENES_OPERATORS_ANY_OF: +#if 0 + case SCENES_NUM_COMP_OPERATORS_ANY_OF: { TRACE_W("'SCENES_OPERATORS_ANY_OF' not implemented!"); break; } - case SCENES_OPERATORS_NONE_OF: + case SCENES_NUM_COMP_OPERATORS_NONE_OF: { TRACE_W("'SCENES_OPERATORS_NONE_OF' not implemented!"); break; } +#endif + default: + { + break; + } + } + + if (cj_item_value) + { + cJSON_Delete(cj_item_value); + } + } + + return ret; +} + +/************* Strings ************/ +static const char *const ezlopi_scenes_str_cmp_operators_op[] = { +#define SCENES_STRINGS_OPERATORS(OPERATOR, op, name, method) op, +#include "__operators_macros/__strings_comparision_operators_macros.h" +#undef SCENES_STRINGS_OPERATORS +}; + +static const char *const ezlopi_scenes_str_cmp_operators_name[] = { +#define SCENES_STRINGS_OPERATORS(OPERATOR, op, name, method) name, +#include "__operators_macros/__strings_comparision_operators_macros.h" +#undef SCENES_STRINGS_OPERATORS +}; + +static const char *const ezlopi_scenes_str_cmp_operators_method[] = { +#define SCENES_STRINGS_OPERATORS(OPERATOR, op, name, method) method, +#include "__operators_macros/__strings_comparision_operators_macros.h" +#undef SCENES_STRINGS_OPERATORS +}; + +e_scene_str_cmp_operators_t ezlopi_scenes_strings_comparator_operators_get_enum(char *operator_str) +{ + e_scene_str_cmp_operators_t ret = SCENES_STRINGS_OPERATORS_NONE + 1; + if (operator_str) + { + while ((ret <= SCENES_STRINGS_OPERATORS_MAX) && ezlopi_scenes_str_cmp_operators_op[ret]) + { + if (0 == strcmp(ezlopi_scenes_str_cmp_operators_op[ret], operator_str)) + { + break; + } + + ret++; + } + } + + return ret; +} + +const char *ezlopi_scenes_strings_comparator_operators_get_op(e_scene_str_cmp_operators_t operator) +{ + const char *ret = NULL; + if ((operator>= SCENES_STRINGS_OPERATORS_NONE) && (operator SCENES_STRINGS_OPERATORS_NONE) && (operator SCENES_STRINGS_OPERATORS_NONE) && (operatoritems; + while (item) + { + if (item->cloud_properties.item_id == item_id) + { + if (cj_item_value) + { + item->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, item, (void *)cj_item_value, NULL); + cJSON *cj_value = cJSON_GetObjectItem(cj_item_value, ezlopi_value_str); + if (cj_value) + { +#warning "Krishna needs to complete this" + item_value = cj_value->valuedouble; + } + } + + break; + } + item = item->next; + } + device = device->next; + } + + e_scene_str_cmp_operators_t string_operator = ezlopi_scenes_strings_comparator_operators_get_enum(comparator_field->value.value_string); + + switch (string_operator) + { + case SCENES_STRINGS_OPERATORS_LESS: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_LESS' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_GREATER: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_GREATER' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_LESS_EQUAL: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_LESS_EQUAL' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_GREATER_EQUAL: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_GREATER_EQUAL' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_EQUAL: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_EQUAL' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_NOT_EQUAL: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_NOT_EQUAL' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_BEGINS_WITH: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_BEGINS_WITH' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_ENDS_WITH: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_ENDS_WITH' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_CONTAINS: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_CONTAINS' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_LENGTH: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_LENGTH' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_NOT_BEGIN: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_NOT_BEGIN' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_NOT_END: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_NOT_END' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_NOT_CONTAIN: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_NOT_CONTAIN' not implemented!"); + break; + } + case SCENES_STRINGS_OPERATORS_NOT_LENGTH: + { + TRACE_W("'SCENES_STRINGS_OPERATORS_NOT_LENGTH' not implemented!"); + break; + } default: { + TRACE_E("'SCENES_STRINGS_OPERATORS_* [%d]' out of range!", string_operator); break; } } @@ -171,4 +366,288 @@ int ezlopi_scenes_operators_compare_value_number(uint32_t item_id, l_fields_v2_t } return ret; -} \ No newline at end of file +} + +/************* Values with Less ************/ +static const char *const ezlopi_scenes_value_with_less_cmp_operators_op[] = { +#define SCENES_VALUES_WITH_LESS_OPERATORS(OPERATOR, op, name, method) op, +#include "__operators_macros/__value_with_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITH_LESS_OPERATORS +}; + +static const char *const ezlopi_scenes_value_with_less_cmp_operators_name[] = { +#define SCENES_VALUES_WITH_LESS_OPERATORS(OPERATOR, op, name, method) name, +#include "__operators_macros/__value_with_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITH_LESS_OPERATORS +}; + +static const char *const ezlopi_scenes_value_with_less_cmp_operators_method[] = { +#define SCENES_VALUES_WITH_LESS_OPERATORS(OPERATOR, op, name, method) method, +#include "__operators_macros/__value_with_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITH_LESS_OPERATORS +}; + +e_scene_value_with_less_cmp_operators_t ezlopi_scenes_value_with_less_comparator_operators_get_enum(char *operator_str) +{ + e_scene_value_with_less_cmp_operators_t ret = SCENES_VALUES_WITH_LESS_OPERATORS_NONE + 1; + if (operator_str) + { + while ((ret <= SCENES_VALUES_WITH_LESS_OPERATORS_MAX) && ezlopi_scenes_value_with_less_cmp_operators_op[ret]) + { + if (0 == strcmp(ezlopi_scenes_value_with_less_cmp_operators_op[ret], operator_str)) + { + break; + } + + ret++; + } + } + + return ret; +} + +const char *ezlopi_scenes_value_with_less_comparator_operators_get_op(e_scene_value_with_less_cmp_operators_t operator) +{ + const char *ret = NULL; + if ((operator>= SCENES_VALUES_WITH_LESS_OPERATORS_NONE) && (operator SCENES_VALUES_WITH_LESS_OPERATORS_NONE) && (operator SCENES_VALUES_WITH_LESS_OPERATORS_NONE) && (operatoritems; + while (item) + { + if (item->cloud_properties.item_id == item_id) + { + if (cj_item_value) + { + item->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, item, (void *)cj_item_value, NULL); + cJSON *cj_value = cJSON_GetObjectItem(cj_item_value, ezlopi_value_str); + if (cj_value) + { +#warning "Krishna needs to complete this" + item_value = cj_value->valuedouble; + } + } + + break; + } + item = item->next; + } + device = device->next; + } + + e_scene_value_with_less_cmp_operators_t value_with_less_operator = ezlopi_scenes_value_with_less_comparator_operators_get_enum(comparator_field->value.value_string); + + switch (value_with_less_operator) + { + case SCENES_VALUES_WITH_LESS_OPERATORS_LESS: + { + TRACE_W("'SCENES_VALUES_WITH_LESS_OPERATORS_LESS' not implemented!"); + break; + } + case SCENES_VALUES_WITH_LESS_OPERATORS_GREATER: + { + TRACE_W("'SCENES_VALUES_WITH_LESS_OPERATORS_GREATER' not implemented!"); + break; + } + case SCENES_VALUES_WITH_LESS_OPERATORS_LESS_EQUAL: + { + TRACE_W("'SCENES_VALUES_WITH_LESS_OPERATORS_LESS_EQUAL' not implemented!"); + break; + } + case SCENES_VALUES_WITH_LESS_OPERATORS_GREATER_EQUAL: + { + TRACE_W("'SCENES_VALUES_WITH_LESS_OPERATORS_GREATER_EQUAL' not implemented!"); + break; + } + case SCENES_VALUES_WITH_LESS_OPERATORS_EQUAL: + { + TRACE_W("'SCENES_VALUES_WITH_LESS_OPERATORS_EQUAL' not implemented!"); + break; + } + case SCENES_VALUES_WITH_LESS_OPERATORS_NOT_EQUAL: + { + TRACE_W("'SCENES_VALUES_WITH_LESS_OPERATORS_NOT_EQUAL' not implemented!"); + break; + } + default: + { + TRACE_E("'SCENES_VALUES_WITH_LESS_OPERATORS_* [%d]' out of range!", value_with_less_operator); + break; + } + } + + if (cj_item_value) + { + cJSON_Delete(cj_item_value); + } + } + + return ret; +} + +/************* Values without less ************/ +static const char *const ezlopi_scenes_value_without_less_cmp_operators_op[] = { +#define SCENES_VALUES_WITHOUT_LESS_OPERATORS(OPERATOR, op, name, method) op, +#include "__operators_macros/__value_without_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITHOUT_LESS_OPERATORS +}; + +static const char *const ezlopi_scenes_value_without_less_cmp_operators_name[] = { +#define SCENES_VALUES_WITHOUT_LESS_OPERATORS(OPERATOR, op, name, method) name, +#include "__operators_macros/__value_without_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITHOUT_LESS_OPERATORS +}; + +static const char *const ezlopi_scenes_value_without_less_cmp_operators_method[] = { +#define SCENES_VALUES_WITHOUT_LESS_OPERATORS(OPERATOR, op, name, method) method, +#include "__operators_macros/__value_without_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITHOUT_LESS_OPERATORS +}; + +e_scene_value_without_less_cmp_operators_t ezlopi_scenes_value_without_less_comparator_operators_get_enum(char *operator_str) +{ + e_scene_value_without_less_cmp_operators_t ret = SCENES_VALUES_WITHOUT_LESS_OPERATORS_NONE + 1; // 1st element of enum after *_NONE + if (operator_str) + { + while ((ret <= SCENES_VALUES_WITHOUT_LESS_OPERATORS_MAX) && ezlopi_scenes_value_without_less_cmp_operators_op[ret]) + { + if (0 == strcmp(ezlopi_scenes_value_without_less_cmp_operators_op[ret], operator_str)) + { + break; + } + + ret++; + } + } + + return ret; +} + +const char *ezlopi_scenes_value_without_less_comparator_operators_get_op(e_scene_value_without_less_cmp_operators_t operator) +{ + const char *ret = NULL; + if ((operator>= SCENES_VALUES_WITHOUT_LESS_OPERATORS_NONE) && (operator SCENES_VALUES_WITHOUT_LESS_OPERATORS_NONE) && (operator SCENES_VALUES_WITHOUT_LESS_OPERATORS_NONE) && (operatoritems; + while (item) + { + if (item->cloud_properties.item_id == item_id) + { + if (cj_item_value) + { + item->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, item, (void *)cj_item_value, NULL); + cJSON *cj_value = cJSON_GetObjectItem(cj_item_value, ezlopi_value_str); + if (cj_value) + { +#warning "Krishna needs to complete this" + item_value = cj_value->valuedouble; + } + } + + break; + } + + item = item->next; + } + + device = device->next; + } + + e_scene_value_without_less_cmp_operators_t value_without_less_operator = ezlopi_scenes_value_without_less_comparator_operators_get_enum(comparator_field->value.value_string); + + switch (value_without_less_operator) + { + case SCENES_VALUES_WITHOUT_LESS_OPERATORS_EQUAL: + { + TRACE_W("'SCENES_VALUES_WITHOUT_LESS_OPERATORS_EQUAL' not implemented!"); + break; + } + case SCENES_VALUES_WITHOUT_LESS_OPERATORS_NOT_EQUAL: + { + TRACE_W("'SCENES_VALUES_WITHOUT_LESS_OPERATORS_NOT_EQUAL' not implemented!"); + break; + } + default: + { + TRACE_E("'SCENES_VALUES_WITHOUT_LESS_OPERATORS_* [%d]' out of range!", value_without_less_operator); + break; + } + } + + if (cj_item_value) + { + cJSON_Delete(cj_item_value); + } + } + + return ret; +} diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.h index f4e2f1a0d..b210b0493 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.h +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators.h @@ -4,19 +4,64 @@ #include #include "ezlopi_scenes_v2.h" -typedef enum e_scene_cmp_operators +typedef enum e_scene_num_cmp_operators { -#define SCENES_OPERATORS(OPERATOR, op, name, method) SCENES_OPERATORS_##OPERATOR, -#include "ezlopi_scenes_operators_macros.h" -#undef SCENES_OPERATORS -} e_scene_cmp_operators_t; - -// Operators -char *ezlopi_scenes_operators_get_op(e_scene_cmp_operators_t operator); -char *ezlopi_scenes_operators_get_name(e_scene_cmp_operators_t operator); -const char *ezlopi_scenes_operators_get_method(e_scene_cmp_operators_t operator); - -e_scene_cmp_operators_t ezlopi_scenes_operators_get_enum(char *operator_str); -int ezlopi_scenes_operators_compare_value_number(uint32_t item_id, l_fields_v2_t *value_field, l_fields_v2_t *comparator_field); +#define SCENES_NUM_COMP_OPERATORS(OPERATOR, op, name, method) SCENES_NUM_COMP_OPERATORS_##OPERATOR, +#include "__operators_macros/__numeric_comparision_operators_macros.h" +#undef SCENES_NUM_COMP_OPERATORS +} e_scene_num_cmp_operators_t; + +typedef enum e_scene_str_cmp_operators +{ +#define SCENES_STRINGS_OPERATORS(OPERATOR, op, name, method) SCENES_STRINGS_OPERATORS_##OPERATOR, +#include "__operators_macros/__strings_comparision_operators_macros.h" +#undef SCENES_STRINGS_OPERATORS +} e_scene_str_cmp_operators_t; + +typedef enum e_scene_value_with_less_cmp_operators +{ +#define SCENES_VALUES_WITH_LESS_OPERATORS(OPERATOR, op, name, method) SCENES_VALUES_WITH_LESS_OPERATORS_##OPERATOR, +#include "__operators_macros/__value_with_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITH_LESS_OPERATORS +} e_scene_value_with_less_cmp_operators_t; + +typedef enum e_scene_value_without_less_cmp_operators +{ +#define SCENES_VALUES_WITHOUT_LESS_OPERATORS(OPERATOR, op, name, method) SCENES_VALUES_WITHOUT_LESS_OPERATORS_##OPERATOR, +#include "__operators_macros/__value_without_less_comparision_operators_macros.h" +#undef SCENES_VALUES_WITHOUT_LESS_OPERATORS +} e_scene_value_without_less_cmp_operators_t; + +// Numeric Operators +e_scene_num_cmp_operators_t ezlopi_scenes_numeric_comparator_operators_get_enum(char *operator_str); +const char *ezlopi_scenes_numeric_comparator_operators_get_op(e_scene_num_cmp_operators_t operator); +const char *ezlopi_scenes_numeric_comparator_operators_get_name(e_scene_num_cmp_operators_t operator); +const char *ezlopi_scenes_numeric_comparator_operators_get_method(e_scene_num_cmp_operators_t operator); + +int ezlopi_scenes_operators_value_number_operations(uint32_t item_id, l_fields_v2_t *value_field, l_fields_v2_t *comparator_field); + +// Strings Operators +e_scene_str_cmp_operators_t ezlopi_scenes_strings_comparator_operators_get_enum(char *operator_str); +const char *ezlopi_scenes_strings_comparator_operators_get_op(e_scene_str_cmp_operators_t operator); +const char *ezlopi_scenes_strings_comparator_operators_get_name(e_scene_str_cmp_operators_t operator); +const char *ezlopi_scenes_strings_comparator_operators_get_method(e_scene_str_cmp_operators_t operator); + +int ezlopi_scenes_operators_value_strings_operations(uint32_t item_id, l_fields_v2_t *value_field, l_fields_v2_t *comparator_field); + +// Value with less Operators +e_scene_value_with_less_cmp_operators_t ezlopi_scenes_value_with_less_comparator_operators_get_enum(char *operator_str); +const char *ezlopi_scenes_value_with_less_comparator_operators_get_op(e_scene_value_with_less_cmp_operators_t operator); +const char *ezlopi_scenes_value_with_less_comparator_operators_get_name(e_scene_value_with_less_cmp_operators_t operator); +const char *ezlopi_scenes_value_with_less_comparator_operators_get_method(e_scene_value_with_less_cmp_operators_t operator); + +int ezlopi_scenes_operators_value_with_less_operations(uint32_t item_id, l_fields_v2_t *value_field, l_fields_v2_t *comparator_field); + +// Value without less Operators +e_scene_value_without_less_cmp_operators_t ezlopi_scenes_value_without_less_comparator_operators_get_enum(char *operator_str); +const char *ezlopi_scenes_value_without_less_comparator_operators_get_op(e_scene_value_without_less_cmp_operators_t operator); +const char *ezlopi_scenes_value_without_less_comparator_operators_get_name(e_scene_value_without_less_cmp_operators_t operator); +const char *ezlopi_scenes_value_without_less_comparator_operators_get_method(e_scene_value_without_less_cmp_operators_t operator); + +int ezlopi_scenes_operators_value_without_less_operations(uint32_t item_id, l_fields_v2_t *value_field, l_fields_v2_t *comparator_field); #endif // __EZLOPI_SCENES_OPERATORS_H__ diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators_macros.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators_macros.h deleted file mode 100755 index 37450fb05..000000000 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_operators_macros.h +++ /dev/null @@ -1,14 +0,0 @@ -SCENES_OPERATORS(NONE, NULL, NULL, NULL) -////////////////////////////////////////////////////////////////////////////// -SCENES_OPERATORS(LESS, "<", "less", "compareNumbers") -SCENES_OPERATORS(GREATER, ">", "greater", "compareNumbers") -SCENES_OPERATORS(LESS_EQUAL, "<=", "less equal", "compareNumbers") -SCENES_OPERATORS(GREATER_EQUAL, ">=", "greater equal", "compareNumbers") -SCENES_OPERATORS(EQUAL, "==", "equal", "compareNumbers") -SCENES_OPERATORS(NOT_EQUAL, "!=", "not equal", "compareNumbers") -SCENES_OPERATORS(BETWEEN, "between", "between", "compareNumberRange") -SCENES_OPERATORS(NOT_BETWEEN, "not_between", "not between", "compareNumberRange") -SCENES_OPERATORS(ANY_OF, "any_of", "any of", "numbersArray") -SCENES_OPERATORS(NONE_OF, "none_of", "none of", "numbersArray") -////////////////////////////////////////////////////////////////////////////// -SCENES_OPERATORS(MAX, NULL, NULL, NULL) \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_populate.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_populate.c new file mode 100644 index 000000000..0ce4fa8b1 --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_populate.c @@ -0,0 +1,454 @@ +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" +#include "ezlopi_scenes_populate.h" + +l_scenes_list_v2_t *ezlopi_scenes_populate_new_scene(cJSON *cj_scene, uint32_t scene_id) +{ + l_scenes_list_v2_t *new_scene = NULL; + if (cj_scene) + { + new_scene = malloc(sizeof(l_scenes_list_v2_t)); + if (new_scene) + { + uint32_t tmp_success_creating_scene = 1; + + memset(new_scene, 0, sizeof(l_scenes_list_v2_t)); + + new_scene->_id = scene_id; + new_scene->task_handle = NULL; + new_scene->status = EZLOPI_SCENE_STATUS_STOPPED; + + CJSON_GET_VALUE_INT(cj_scene, ezlopi_enabled_str, new_scene->enabled); + CJSON_GET_VALUE_INT(cj_scene, ezlopi_is_group_str, new_scene->is_group); + + { + CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, ezlopi_group_id_str, new_scene->group_id); + TRACE_D("group_id: %s", new_scene->group_id); + } + + CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, ezlopi_name_str, new_scene->name); + CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, ezlopi_parent_id_str, new_scene->parent_id); + + { + cJSON *cj_user_notifications = cJSON_GetObjectItem(cj_scene, ezlopi_user_notifications_str); + if (cj_user_notifications && (cJSON_Array == cj_user_notifications->type)) + { + new_scene->user_notifications = ezlopi_scenes_populate_user_notifications(cj_user_notifications); + } + } + + { + cJSON *cj_house_modes = cJSON_GetObjectItem(cj_scene, ezlopi_house_modes_str); + if (cj_house_modes && (cJSON_Array == cj_house_modes->type)) + { + new_scene->house_modes = ezlopi_scenes_populate_house_modes(cj_house_modes); + } + } + + { + cJSON *cj_then_blocks = cJSON_GetObjectItem(cj_scene, ezlopi_then_str); + if (cj_then_blocks && (cJSON_Array == cj_then_blocks->type)) + { + new_scene->then_block = ezlopi_scenes_populate_action_blocks(cj_then_blocks, SCENE_BLOCK_TYPE_THEN); + } + } + + { + cJSON *cj_then_blocks = cJSON_GetObjectItem(cj_scene, ezlopi_when_str); + if (cj_then_blocks && (cJSON_Array == cj_then_blocks->type)) + { + new_scene->when_block = ezlopi_scenes_populate_when_blocks(cj_then_blocks); + } + } + + { + cJSON *cj_else_blocks = cJSON_GetObjectItem(cj_scene, ezlopi_else_str); + if (cj_else_blocks && (cJSON_Array == cj_else_blocks->type)) + { + new_scene->else_block = ezlopi_scenes_populate_action_blocks(cj_else_blocks, SCENE_BLOCK_TYPE_ELSE); + } + } + + if (0 == tmp_success_creating_scene) + { + // delete new_scene + } + } + } + + return new_scene; +} + +l_user_notification_v2_t *ezlopi_scenes_populate_user_notifications(cJSON *cj_user_notifications) +{ + l_user_notification_v2_t *tmp_user_notifications_head = NULL; + if (cj_user_notifications && (cJSON_Array == cj_user_notifications->type)) + { + cJSON *cj_user_notification = NULL; + int user_notifications_idx = 0; + + while (NULL != (cj_user_notification = cJSON_GetArrayItem(cj_user_notifications, user_notifications_idx++))) + { + if (tmp_user_notifications_head) + { + l_user_notification_v2_t *curr_user_notification = tmp_user_notifications_head; + while (curr_user_notification->next) + { + curr_user_notification = curr_user_notification->next; + } + curr_user_notification->next = ezlopi_scenes_populate_new_user_notification(cj_user_notification); + } + else + { + tmp_user_notifications_head = ezlopi_scenes_populate_new_user_notification(cj_user_notification); + } + } + } + + return tmp_user_notifications_head; +} + +l_user_notification_v2_t *ezlopi_scenes_populate_new_user_notification(cJSON *cj_user_notification) +{ + l_user_notification_v2_t *new_user_notification = NULL; + + if (cj_user_notification && cj_user_notification->valuestring) + { + new_user_notification = malloc(sizeof(l_user_notification_v2_t)); + if (new_user_notification) + { + memset(new_user_notification, 0, sizeof(l_user_notification_v2_t)); + snprintf(new_user_notification->user_id, sizeof(new_user_notification->user_id), "%s", cj_user_notification->valuestring); + new_user_notification->next = NULL; + } + } + + return new_user_notification; +} + +l_house_modes_v2_t *ezlopi_scenes_populate_house_modes(cJSON *cj_house_modes) +{ + l_house_modes_v2_t *tmp_house_mode_head = NULL; + if (cj_house_modes) + { + int house_mode_idx = 0; + cJSON *cj_house_mode = NULL; + + while (NULL != (cj_house_mode = cJSON_GetArrayItem(cj_house_modes, house_mode_idx++))) + { + if (tmp_house_mode_head) + { + l_house_modes_v2_t *tmp_house_mode = tmp_house_mode_head; + while (tmp_house_mode->next) + { + tmp_house_mode = tmp_house_mode->next; + } + + tmp_house_mode->next = ezlopi_scenes_populate_new_house_mode(cj_house_mode); + } + else + { + tmp_house_mode_head = ezlopi_scenes_populate_new_house_mode(cj_house_mode); + } + } + } + + return tmp_house_mode_head; +} + +l_house_modes_v2_t *ezlopi_scenes_populate_new_house_mode(cJSON *cj_house_mode) +{ + l_house_modes_v2_t *new_house_mode = NULL; + + if (cj_house_mode && cj_house_mode->valuestring) + { + new_house_mode = malloc(sizeof(l_house_modes_v2_t)); + if (new_house_mode) + { + memset(new_house_mode, 0, sizeof(l_house_modes_v2_t)); + strncpy(new_house_mode->house_mode, cj_house_mode->valuestring, sizeof(new_house_mode->house_mode)); + new_house_mode->next = NULL; + } + } + + return new_house_mode; +} + +l_action_block_v2_t *ezlopi_scenes_populate_action_blocks(cJSON *cj_action_blocks, e_scenes_block_type_v2_t block_type) +{ + l_action_block_v2_t *tmp_action_block_head = NULL; + if (cj_action_blocks) + { + int then_block_idx = 0; + cJSON *cj_action_block = NULL; + + while (NULL != (cj_action_block = cJSON_GetArrayItem(cj_action_blocks, then_block_idx++))) + { + if (tmp_action_block_head) + { + l_action_block_v2_t *tmp_then_block = tmp_action_block_head; + while (tmp_then_block->next) + { + tmp_then_block = tmp_then_block->next; + } + + tmp_then_block->next = ezlopi_scenes_populate_new_action_block(cj_action_block, block_type); + } + else + { + tmp_action_block_head = ezlopi_scenes_populate_new_action_block(cj_action_block, block_type); + } + } + } + + return tmp_action_block_head; +} + +l_action_block_v2_t *ezlopi_scenes_populate_new_action_block(cJSON *cj_action_block, e_scenes_block_type_v2_t block_type) +{ + l_action_block_v2_t *new_then_block = malloc(sizeof(l_action_block_v2_t)); + if (new_then_block) + { + memset(new_then_block, 0, sizeof(l_action_block_v2_t)); + cJSON *cj_block_options = cJSON_GetObjectItem(cj_action_block, ezlopi_blockOptions_str); + if (cj_block_options) + { + ezlopi_scenes_populate_new_block_options(&new_then_block->block_options, cj_block_options); + } + + new_then_block->block_type = block_type; + cJSON *cj_delay = cJSON_GetObjectItem(cj_action_block, ezlopi_delay_str); + if (cj_delay) + { + ezlopi_scenes_populate_new_action_delay(&new_then_block->delay, cj_delay); + } + + cJSON *cj_fields = cJSON_GetObjectItem(cj_action_block, ezlopi_fields_str); + if (cj_fields) + { + new_then_block->fields = ezlopi_scenes_populate_fields(cj_fields); + } + + cJSON *cj__temp_id = cJSON_GetObjectItem(cj_action_block, "_tempId"); + if (cj__temp_id && cj__temp_id->valuestring) + { + snprintf(new_then_block->_tempId, sizeof(new_then_block->_tempId), "%s", cj__temp_id->valuestring); + } + } + + return new_then_block; +} + +l_when_block_v2_t *ezlopi_scenes_populate_when_blocks(cJSON *cj_when_blocks) +{ + l_when_block_v2_t *tmp_when_block_head = NULL; + if (cj_when_blocks) + { + int when_block_idx = 0; + cJSON *cj_when_block = NULL; + + while (NULL != (cj_when_block = cJSON_GetArrayItem(cj_when_blocks, when_block_idx++))) + { + if (tmp_when_block_head) + { + l_when_block_v2_t *tmp_when_block = tmp_when_block_head; + while (tmp_when_block->next) + { + tmp_when_block = tmp_when_block->next; + } + + tmp_when_block->next = ezlopi_scenes_populate_new_when_block(cj_when_block); + } + else + { + tmp_when_block_head = ezlopi_scenes_populate_new_when_block(cj_when_block); + } + } + } + + return tmp_when_block_head; +} + +l_when_block_v2_t *ezlopi_scenes_populate_new_when_block(cJSON *cj_when_block) +{ + l_when_block_v2_t *new_when_block = malloc(sizeof(l_when_block_v2_t)); + if (new_when_block) + { + memset(new_when_block, 0, sizeof(l_when_block_v2_t)); + cJSON *cj_block_options = cJSON_GetObjectItem(cj_when_block, ezlopi_blockOptions_str); + if (cj_block_options) + { + ezlopi_scenes_populate_new_block_options(&new_when_block->block_options, cj_block_options); + } + + new_when_block->block_type = SCENE_BLOCK_TYPE_WHEN; + + cJSON *cj_fields = cJSON_GetObjectItem(cj_when_block, ezlopi_fields_str); + if (cj_fields) + { + new_when_block->fields = ezlopi_scenes_populate_fields(cj_fields); + } + } + + return new_when_block; +} + +void ezlopi_scenes_populate_new_block_options(s_block_options_v2_t *p_block_options, cJSON *cj_block_options) +{ + cJSON *cj_method = cJSON_GetObjectItem(cj_block_options, ezlopi_key_method_str); + if (cj_method) + { + ezlopi_scenes_populate_new_method(&p_block_options->method, cj_method); + } +} + +void ezlopi_scenes_populate_new_method(s_method_v2_t *p_method, cJSON *cj_method) +{ + CJSON_GET_VALUE_STRING_BY_COPY(cj_method, ezlopi_name_str, p_method->name); + p_method->type = ezlopi_scenes_method_get_type_enum(p_method->name); +} + +void ezlopi_scenes_populate_new_action_delay(s_action_delay_v2_t *action_delay, cJSON *cj_delay) +{ + if (action_delay && cj_delay) + { + CJSON_GET_VALUE_DOUBLE(cj_delay, "days", action_delay->days); + CJSON_GET_VALUE_DOUBLE(cj_delay, "hours", action_delay->hours); + CJSON_GET_VALUE_DOUBLE(cj_delay, "minutes", action_delay->minutes); + CJSON_GET_VALUE_DOUBLE(cj_delay, "seconds", action_delay->seconds); + } +} + +l_fields_v2_t *ezlopi_scenes_populate_fields(cJSON *cj_fields) +{ + l_fields_v2_t *tmp_fields_head = NULL; + if (cj_fields) + { + int fields_idx = 0; + cJSON *cj_field = NULL; + while (NULL != (cj_field = cJSON_GetArrayItem(cj_fields, fields_idx++))) + { + if (tmp_fields_head) + { + l_fields_v2_t *tmp_flield = tmp_fields_head; + while (tmp_flield->next) + { + tmp_flield = tmp_flield->next; + } + + tmp_flield->next = ezlopi_scenes_populate_new_field(cj_field); + } + else + { + tmp_fields_head = ezlopi_scenes_populate_new_field(cj_field); + } + } + } + + return tmp_fields_head; +} + +void ezlopi_scenes_populate_fields_get_value(l_fields_v2_t *field, cJSON *cj_value) +{ + if (field && cj_value) + { + trace("type: %s", ezlopi_scene_get_scene_value_type_name_v2(field->value_type)); + switch (cj_value->type) + { + case cJSON_Number: + { + field->value.type = VALUE_TYPE_NUMBER; + field->value.value_double = cj_value->valuedouble; + TRACE_B("value: %f", field->value.value_double); + break; + } + case cJSON_String: + { + field->value.type = VALUE_TYPE_STRING; + uint32_t value_len = strlen(cj_value->valuestring) + 1; + field->value.value_string = malloc(value_len); + if (field->value.value_string) + { + snprintf(field->value.value_string, value_len, "%s", cj_value->valuestring); + TRACE_B("value: %s", field->value.value_string); + } + else + { + TRACE_E("Malloc failed!"); + } + break; + } + case cJSON_True: + { + field->value.type = VALUE_TYPE_BOOL; + field->value.value_bool = true; + TRACE_B("value: true"); + break; + } + case cJSON_False: + { + field->value.type = VALUE_TYPE_BOOL; + field->value.value_bool = false; + TRACE_B("value: false"); + break; + } + case cJSON_Object: + { + field->value.type = VALUE_TYPE_CJSON; + field->value.cj_value = cJSON_Duplicate(cj_value, cJSON_True); + CJSON_TRACE("value", field->value.cj_value); + break; + } + case cJSON_Array: + { + int block_idx = 0; + cJSON *cj_block = NULL; + field->value.type = VALUE_TYPE_BLOCK; + CJSON_TRACE("value", cj_value); + + while (NULL != (cj_block = cJSON_GetArrayItem(cj_value, block_idx++))) + { + if (field->value.when_block) + { + l_when_block_v2_t *curr_when_block = field->value.when_block; + while (curr_when_block->next) + { + curr_when_block = curr_when_block->next; + } + curr_when_block->next = ezlopi_scenes_populate_new_when_block(cj_block); + } + else + { + field->value.when_block = ezlopi_scenes_populate_new_when_block(cj_block); + } + } + break; + } + default: + { + field->value.type = VALUE_TYPE_UNDEFINED; + TRACE_E("cj_value type: %d", cj_value->type); + break; + } + } + } +} + +l_fields_v2_t *ezlopi_scenes_populate_new_field_populate(cJSON *cj_field) +{ + l_fields_v2_t *field = NULL; + if (cj_field) + { + field = malloc(sizeof(l_fields_v2_t)); + if (field) + { + memset(field, 0, sizeof(l_fields_v2_t)); + CJSON_GET_VALUE_STRING_BY_COPY(cj_field, ezlopi_name_str, field->name); + + field->value_type = ezlopi_scenes_get_expressions_value_type(cJSON_GetObjectItem(cj_field, ezlopi_type_str)); + ezlopi_scenes_populate_fields_get_value(field, cJSON_GetObjectItem(cj_field, ezlopi_value_str)); + } + } + + return field; +} diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_populate.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_populate.h new file mode 100644 index 000000000..dfe6fe67b --- /dev/null +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_populate.h @@ -0,0 +1,33 @@ +#ifndef __EZLOPI_SCENES_POPULATE_H__ +#define __EZLOPI_SCENES_POPULATE_H__ + +#include +#include + +#include "ezlopi_scenes_v2.h" + +l_when_block_v2_t *ezlopi_scenes_populate_new_when_block(cJSON *cj_when_block); + +l_fields_v2_t *ezlopi_scenes_populate_new_field(cJSON *cj_field); +l_fields_v2_t *ezlopi_scenes_populate_fields(cJSON *cj_fields); + +void ezlopi_scenes_populate_new_action_delay(s_action_delay_v2_t *action_delay, cJSON *cj_delay); +void ezlopi_scenes_populate_new_block_options(s_block_options_v2_t *p_block_options, cJSON *cj_block_options); +void ezlopi_scenes_populate_new_method(s_method_v2_t *p_method, cJSON *cj_method); + +l_when_block_v2_t *ezlopi_scenes_populate_new_when_block(cJSON *cj_when_block); +l_when_block_v2_t *ezlopi_scenes_populate_when_blocks(cJSON *cj_when_blocks); + +l_action_block_v2_t *ezlopi_scenes_populate_new_action_block(cJSON *cj_then_block, e_scenes_block_type_v2_t block_type); +l_action_block_v2_t *ezlopi_scenes_populate_action_blocks(cJSON *cj_then_blocks, e_scenes_block_type_v2_t block_type); + +l_house_modes_v2_t *ezlopi_scenes_populate_new_house_mode(cJSON *cj_house_mode); +l_house_modes_v2_t *ezlopi_scenes_populate_house_modes(cJSON *cj_house_modes); + +l_user_notification_v2_t *ezlopi_scenes_populate_new_user_notification(cJSON *cj_user_notification); +l_user_notification_v2_t *ezlopi_scenes_populate_user_notifications(cJSON *cj_user_notifications); + +l_scenes_list_v2_t *ezlopi_scenes_populate_new_scene(cJSON *cj_scene, uint32_t scene_id); +l_scenes_list_v2_t *ezlopi_scenes_populate_scenes(cJSON *cj_scene, uint32_t scene_id); + +#endif // __EZLOPI_SCENES_POPULATE_H__ diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_print.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_print.c index 07db6a2e0..61eb1feff 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_print.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_print.c @@ -1,5 +1,6 @@ #include "trace.h" #include "ezlopi_scenes_v2.h" +#include "ezlopi_cloud_constants.h" void ezlopi_print_when_blocks(l_when_block_v2_t *when_blocks); @@ -27,7 +28,7 @@ void ezlopi_print_fields(l_fields_v2_t *fields) TRACE_D("\t\t\t|-- name: %s", fields->name); const char *value_type_name = ezlopi_scene_get_scene_value_type_name_v2(fields->value_type); - TRACE_D("\t\t\t|-- type: %s", value_type_name ? value_type_name : ""); + TRACE_D("\t\t\t|-- type: %s", value_type_name ? value_type_name : ezlopi__str); switch (fields->value_type) { @@ -38,7 +39,7 @@ void ezlopi_print_fields(l_fields_v2_t *fields) } case EZLOPI_VALUE_TYPE_BOOL: { - TRACE_D("\t\t\t|-- value: [%d]%s", fields->value.value_bool, fields->value.value_bool ? "true" : "false"); + TRACE_D("\t\t\t|-- value: [%d]%s", fields->value.value_bool, fields->value.value_bool ? ezlopi_true_str : ezlopi_false_str); break; } case EZLOPI_VALUE_TYPE_FLOAT: @@ -139,10 +140,6 @@ void ezlopi_print_fields(l_fields_v2_t *fields) } fields = fields->next; - if (fields) - { - TRACE_D(""); - } } TRACE_D("\t\t\t|------------------------------------"); } diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_scripts.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_scripts.c index 2c2914f7e..ac9ea941c 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_scripts.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_scripts.c @@ -88,7 +88,7 @@ void ezlopi_scenes_scripts_delete_by_id(uint32_t script_id) if (script_to_delete) { ezlopi_scenes_scripts_stop(script_to_delete); - ezlopi_nvs_delete_stored_script(script_to_delete->id); // deleting script from nvs + ezlopi_nvs_delete_stored_data_by_id(script_to_delete->id); // deleting script from nvs __scripts_remove_id_and_update_list(script_to_delete->id); if (script_to_delete->code) @@ -115,12 +115,18 @@ uint32_t ezlopi_scenes_scripts_add_to_head(uint32_t script_id, cJSON *cj_script) } curr_script->next = __scripts_create_node(script_id, cj_script); - new_script_id = curr_script->next->id; + if (curr_script) + { + new_script_id = curr_script->next->id; + } } else { script_head = __scripts_create_node(script_id, cj_script); - new_script_id = script_head->id; + if (script_head) + { + new_script_id = script_head->id; + } } return new_script_id; @@ -169,7 +175,7 @@ void ezlopi_scenes_scripts_update(cJSON *cj_script) } { - cJSON *cj_name = cJSON_GetObjectItem(cj_script, "name"); + cJSON *cj_name = cJSON_GetObjectItem(cj_script, ezlopi_name_str); if (cj_name && cj_name->valuestring) { uint32_t len = strlen(cj_name->valuestring) + 1; @@ -179,7 +185,7 @@ void ezlopi_scenes_scripts_update(cJSON *cj_script) } { - cJSON *cj_code = cJSON_GetObjectItem(cj_script, "code"); + cJSON *cj_code = cJSON_GetObjectItem(cj_script, ezlopi_code_str); if (cj_code && cj_code->valuestring) { uint32_t len = strlen(cj_code->valuestring) + 1; @@ -191,7 +197,9 @@ void ezlopi_scenes_scripts_update(cJSON *cj_script) char *script_to_update = cJSON_Print(cj_script); if (script_to_update) { + cJSON_Minify(script_to_update); ezlopi_nvs_write_str(script_to_update, strlen(script_to_update), cj_script_id->valuestring); + free(script_to_update); } break; @@ -294,6 +302,7 @@ static void __scripts_add_script_id(uint32_t script_id) char *script_ids_str_updated = cJSON_Print(cj_script_ids); if (script_ids_str_updated) { + cJSON_Minify(script_ids_str_updated); ezlopi_nvs_write_scenes_scripts(script_ids_str_updated); free(script_ids_str_updated); } @@ -409,8 +418,8 @@ static l_ezlopi_scenes_script_t *__scripts_create_node(uint32_t script_id, cJSON if (cj_script) { - cJSON *cj_script_name = cJSON_GetObjectItem(cj_script, "name"); - cJSON *cj_script_code = cJSON_GetObjectItem(cj_script, "code"); + cJSON *cj_script_name = cJSON_GetObjectItem(cj_script, ezlopi_name_str); + cJSON *cj_script_code = cJSON_GetObjectItem(cj_script, ezlopi_code_str); if (cj_script_name && cj_script_name->valuestring && cj_script_code && cj_script_code->string) { @@ -424,6 +433,7 @@ static l_ezlopi_scenes_script_t *__scripts_create_node(uint32_t script_id, cJSON char *script_str = cJSON_Print(cj_script); if (script_str) { + cJSON_Minify(script_str); char scrpt_id_str[32]; snprintf(scrpt_id_str, sizeof(scrpt_id_str), "%08x", script_id); ezlopi_nvs_write_str(script_str, strlen(script_str), scrpt_id_str); @@ -472,7 +482,7 @@ static char *__script_report(lua_State *lua_state, int status) { if (status == LUA_OK) { - return; + return NULL; } const char *msg = lua_tostring(lua_state, -1); diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.c index ae61278f5..f8e8e5263 100644 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.c @@ -43,12 +43,22 @@ int ezlopi_scenes_status_change_broadcast(l_scenes_list_v2_t *scene_node, const cJSON_AddFalseToObject(cj_result, ezlopi_userNotification_str); } - cJSON_AddStringToObject(cj_result, ezlopi_room_id_str, ""); - cJSON_AddStringToObject(cj_result, ezlopi_room_name_str, ""); + cJSON_AddStringToObject(cj_result, ezlopi_room_id_str, ezlopi__str); + cJSON_AddStringToObject(cj_result, ezlopi_room_name_str, ezlopi__str); } - ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_I); + char *data_to_send = cJSON_Print(cj_response); cJSON_Delete(cj_response); + + if (data_to_send) + { + cJSON_Minify(data_to_send); + ret = web_provisioning_send_str_data_to_nma_websocket(data_to_send, TRACE_TYPE_D); + free(data_to_send); + } + + // ret = web_provisioning_send_to_nma_websocket(cj_response, TRACE_TYPE_I); + // cJSON_Delete(cj_response); } } diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.h index 3bff70475..874381db2 100644 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.h +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_status_changed.h @@ -3,12 +3,6 @@ #include "ezlopi_scenes_v2.h" -const static char *scene_status_started_str = "started"; -const static char *scene_status_finished_str = "finished"; -const static char *scene_status_partially_finished_str = "partially_finished"; -const static char *scene_status_failed_str = "failed"; -const static char *scene_status_stopped_str = "stopped"; - const char *ezlopi_scenes_status_to_string(e_scene_status_v2_t scene_status); int ezlopi_scenes_status_change_broadcast(l_scenes_list_v2_t *scene_node, const char *status_str); diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_then_methods.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_then_methods.c index 631969cd8..014245790 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_then_methods.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_then_methods.c @@ -2,14 +2,13 @@ #include "ezlopi_devices.h" #include "ezlopi_scenes_v2.h" #include "ezlopi_devices_list.h" +#include "ezlopi_cloud_constants.h" #include "ezlopi_scenes_then_methods.h" int ezlopi_scene_then_set_item_value(l_scenes_list_v2_t *curr_scene, void *arg) { int ret = 0; uint32_t item_id = 0; - uint32_t value = 0; - cJSON *cj_params = cJSON_CreateObject(); if (cj_params) @@ -22,25 +21,25 @@ int ezlopi_scene_then_set_item_value(l_scenes_list_v2_t *curr_scene, void *arg) { if (0 == strncmp(curr_field->name, "item", 4)) { - cJSON_AddStringToObject(cj_params, "_id", curr_field->value.value_string); + cJSON_AddStringToObject(cj_params, ezlopi__id_str, curr_field->value.value_string); item_id = strtoul(curr_field->value.value_string, NULL, 16); TRACE_D("item_id: %s", curr_field->value.value_string); } - else if (0 == strncmp(curr_field->name, "value", 5)) + else if (0 == strncmp(curr_field->name, ezlopi_value_str, 5)) { if (EZLOPI_VALUE_TYPE_INT == curr_field->value_type) { - cJSON_AddNumberToObject(cj_params, "value", curr_field->value.value_int); + cJSON_AddNumberToObject(cj_params, ezlopi_value_str, curr_field->value.value_double); TRACE_D("value: %f", curr_field->value.value_double); } else if (EZLOPI_VALUE_TYPE_BOOL == curr_field->value_type) { - cJSON_AddBoolToObject(cj_params, "value", curr_field->value.value_bool); - TRACE_D("value: %s", curr_field->value.value_bool ? "true" : "false"); + cJSON_AddBoolToObject(cj_params, ezlopi_value_str, curr_field->value.value_bool); + TRACE_D("value: %s", curr_field->value.value_bool ? ezlopi_true_str : ezlopi_false_str); } else if (EZLOPI_VALUE_TYPE_STRING == curr_field->value_type) { - cJSON_AddBoolToObject(cj_params, "value", curr_field->value.value_string); + cJSON_AddStringToObject(cj_params, ezlopi_value_str, curr_field->value.value_string); TRACE_D("value: %s", curr_field->value.value_string); } } diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.c index e6ba53457..000862994 100644 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.c @@ -6,12 +6,15 @@ #include "ezlopi_cloud.h" #include "ezlopi_devices.h" #include "ezlopi_scenes_v2.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_factory_info.h" #include "ezlopi_scenes_methods.h" +#include "ezlopi_cloud_constants.h" #include "ezlopi_meshbot_service.h" #include "ezlopi_scenes_when_methods.h" #include "ezlopi_scenes_then_methods.h" #include "ezlopi_scenes_status_changed.h" +#include "ezlopi_scenes_cjson.h" static l_scenes_list_v2_t *scenes_list_head_v2 = NULL; @@ -28,7 +31,6 @@ static const f_scene_method_v2_t ezlopi_scenes_methods[] = { }; static l_when_block_v2_t *__new_when_block_populate(cJSON *cj_when_block); -static e_scene_value_type_v2_t __new_get_value_type(cJSON *cj_field); static l_fields_v2_t *__new_field_populate(cJSON *cj_field); static l_fields_v2_t *__fields_populate(cJSON *cj_fields); @@ -52,6 +54,26 @@ static l_user_notification_v2_t *__user_notifications_populate(cJSON *cj_user_no static l_scenes_list_v2_t *__new_scene_populate(cJSON *cj_scene, uint32_t scene_id); static l_scenes_list_v2_t *__scenes_populate(cJSON *cj_scene, uint32_t scene_id); +void ezlopi_scene_add_users_in_notifications(l_scenes_list_v2_t *scene_node, cJSON *cj_user) +{ + if (scene_node && cj_user && cj_user->valuestring) + { + if (scene_node->user_notifications) + { + l_user_notification_v2_t *user_node = scene_node->user_notifications; + while (user_node->next) + { + user_node = user_node->next; + } + user_node->next = __new_user_notification_populate(cj_user); + } + else + { + scene_node->user_notifications = __new_user_notification_populate(cj_user); + } + } +} + uint32_t ezlopi_store_new_scene_v2(cJSON *cj_new_scene) { uint32_t new_scene_id = 0; @@ -60,7 +82,7 @@ uint32_t ezlopi_store_new_scene_v2(cJSON *cj_new_scene) new_scene_id = ezlopi_cloud_generate_scene_id(); char tmp_buffer[32]; snprintf(tmp_buffer, sizeof(tmp_buffer), "%08x", new_scene_id); - cJSON_AddStringToObject(cj_new_scene, "_id", tmp_buffer); + cJSON_AddStringToObject(cj_new_scene, ezlopi__id_str, tmp_buffer); char *new_scnee_str = cJSON_Print(cj_new_scene); if (new_scnee_str) { @@ -81,7 +103,7 @@ uint32_t ezlopi_store_new_scene_v2(cJSON *cj_new_scene) if (!cJSON_AddItemToArray(cj_scenes_list, cj_new_scene_id)) { cJSON_Delete(cj_new_scene_id); - ezlopi_nvs_delete_stored_script(new_scene_id); + ezlopi_nvs_delete_stored_data_by_id(new_scene_id); new_scene_id = 0; } else @@ -150,6 +172,9 @@ uint32_t ezlopi_scenes_get_list_v2(cJSON *cj_scenes_array) cJSON *cj_scene = cJSON_Parse(scene_str); if (cj_scene) { + char scene_id_str[32]; + snprintf(scene_id_str, sizeof(scene_id_str), "%08x", (uint32_t)cj_scene_id->valuedouble); + cJSON_AddStringToObject(cj_scene, ezlopi__id_str, scene_id_str); if (!cJSON_AddItemToArray(cj_scenes_array, cj_scene)) { cJSON_Delete(cj_scene); @@ -285,8 +310,8 @@ f_scene_method_v2_t ezlopi_scene_get_method_v2(e_scene_method_type_t scene_metho const char *ezlopi_scene_get_scene_value_type_name_v2(e_scene_value_type_v2_t value_type) { - const char *ret = NULL; - if ((value_type > EZLOPI_VALUE_TYPE_NONE) && (value_type < EZLOPI_VALUE_TYPE_MAX)) + const char *ret = ezlopi__str; + if ((value_type >= EZLOPI_VALUE_TYPE_NONE) && (value_type < EZLOPI_VALUE_TYPE_MAX)) { ret = scenes_value_type_name[value_type]; } @@ -323,8 +348,15 @@ void ezlopi_scenes_init_v2(void) cJSON *cj_scene = cJSON_Parse(scene_str); if (cj_scene) { - __scenes_populate(cj_scene, scene_id); + l_scenes_list_v2_t *new_scene = __scenes_populate(cj_scene, scene_id); cJSON_Delete(cj_scene); + + if (new_scene) + { + cJSON *cj_new_scene = ezlopi_scenes_create_cjson_scene(new_scene); + CJSON_TRACE("new_scene", cj_new_scene); + cJSON_Delete(cj_new_scene); + } } free(scene_str); @@ -384,19 +416,19 @@ static l_scenes_list_v2_t *__new_scene_populate(cJSON *cj_scene, uint32_t scene_ new_scene->task_handle = NULL; new_scene->status = EZLOPI_SCENE_STATUS_STOPPED; - CJSON_GET_VALUE_INT(cj_scene, "enabled", new_scene->enabled); - CJSON_GET_VALUE_INT(cj_scene, "is_group", new_scene->is_group); + CJSON_GET_VALUE_INT(cj_scene, ezlopi_enabled_str, new_scene->enabled); + CJSON_GET_VALUE_INT(cj_scene, ezlopi_is_group_str, new_scene->is_group); { - CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, "group_id", new_scene->group_id); + CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, ezlopi_group_id_str, new_scene->group_id); TRACE_D("group_id: %s", new_scene->group_id); } - CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, "name", new_scene->name); - CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, "parent_id", new_scene->parent_id); + CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, ezlopi_name_str, new_scene->name); + CJSON_GET_VALUE_STRING_BY_COPY(cj_scene, ezlopi_parent_id_str, new_scene->parent_id); { - cJSON *cj_user_notifications = cJSON_GetObjectItem(cj_scene, "user_notifications"); + cJSON *cj_user_notifications = cJSON_GetObjectItem(cj_scene, ezlopi_user_notifications_str); if (cj_user_notifications && (cJSON_Array == cj_user_notifications->type)) { new_scene->user_notifications = __user_notifications_populate(cj_user_notifications); @@ -404,7 +436,7 @@ static l_scenes_list_v2_t *__new_scene_populate(cJSON *cj_scene, uint32_t scene_ } { - cJSON *cj_house_modes = cJSON_GetObjectItem(cj_scene, "house_modes"); + cJSON *cj_house_modes = cJSON_GetObjectItem(cj_scene, ezlopi_house_modes_str); if (cj_house_modes && (cJSON_Array == cj_house_modes->type)) { new_scene->house_modes = __house_modes_populate(cj_house_modes); @@ -412,7 +444,7 @@ static l_scenes_list_v2_t *__new_scene_populate(cJSON *cj_scene, uint32_t scene_ } { - cJSON *cj_then_blocks = cJSON_GetObjectItem(cj_scene, "then"); + cJSON *cj_then_blocks = cJSON_GetObjectItem(cj_scene, ezlopi_then_str); if (cj_then_blocks && (cJSON_Array == cj_then_blocks->type)) { new_scene->then_block = __action_blocks_populate(cj_then_blocks, SCENE_BLOCK_TYPE_THEN); @@ -420,7 +452,7 @@ static l_scenes_list_v2_t *__new_scene_populate(cJSON *cj_scene, uint32_t scene_ } { - cJSON *cj_then_blocks = cJSON_GetObjectItem(cj_scene, "when"); + cJSON *cj_then_blocks = cJSON_GetObjectItem(cj_scene, ezlopi_when_str); if (cj_then_blocks && (cJSON_Array == cj_then_blocks->type)) { new_scene->when_block = __when_blocks_populate(cj_then_blocks); @@ -428,7 +460,7 @@ static l_scenes_list_v2_t *__new_scene_populate(cJSON *cj_scene, uint32_t scene_ } { - cJSON *cj_else_blocks = cJSON_GetObjectItem(cj_scene, "else"); + cJSON *cj_else_blocks = cJSON_GetObjectItem(cj_scene, ezlopi_else_str); if (cj_else_blocks && (cJSON_Array == cj_else_blocks->type)) { new_scene->else_block = __action_blocks_populate(cj_else_blocks, SCENE_BLOCK_TYPE_ELSE); @@ -576,20 +608,20 @@ static l_action_block_v2_t *__new_action_block_populate(cJSON *cj_action_block, if (new_then_block) { memset(new_then_block, 0, sizeof(l_action_block_v2_t)); - cJSON *cj_block_options = cJSON_GetObjectItem(cj_action_block, "blockOptions"); + cJSON *cj_block_options = cJSON_GetObjectItem(cj_action_block, ezlopi_blockOptions_str); if (cj_block_options) { __new_block_options_populate(&new_then_block->block_options, cj_block_options); } new_then_block->block_type = block_type; - cJSON *cj_delay = cJSON_GetObjectItem(cj_action_block, "delay"); + cJSON *cj_delay = cJSON_GetObjectItem(cj_action_block, ezlopi_delay_str); if (cj_delay) { __new_action_delay(&new_then_block->delay, cj_delay); } - cJSON *cj_fields = cJSON_GetObjectItem(cj_action_block, "fields"); + cJSON *cj_fields = cJSON_GetObjectItem(cj_action_block, ezlopi_fields_str); if (cj_fields) { new_then_block->fields = __fields_populate(cj_fields); @@ -641,7 +673,7 @@ static l_when_block_v2_t *__new_when_block_populate(cJSON *cj_when_block) if (new_when_block) { memset(new_when_block, 0, sizeof(l_when_block_v2_t)); - cJSON *cj_block_options = cJSON_GetObjectItem(cj_when_block, "blockOptions"); + cJSON *cj_block_options = cJSON_GetObjectItem(cj_when_block, ezlopi_blockOptions_str); if (cj_block_options) { __new_block_options_populate(&new_when_block->block_options, cj_block_options); @@ -649,7 +681,7 @@ static l_when_block_v2_t *__new_when_block_populate(cJSON *cj_when_block) new_when_block->block_type = SCENE_BLOCK_TYPE_WHEN; - cJSON *cj_fields = cJSON_GetObjectItem(cj_when_block, "fields"); + cJSON *cj_fields = cJSON_GetObjectItem(cj_when_block, ezlopi_fields_str); if (cj_fields) { new_when_block->fields = __fields_populate(cj_fields); @@ -661,7 +693,7 @@ static l_when_block_v2_t *__new_when_block_populate(cJSON *cj_when_block) static void __new_block_options_populate(s_block_options_v2_t *p_block_options, cJSON *cj_block_options) { - cJSON *cj_method = cJSON_GetObjectItem(cj_block_options, "method"); + cJSON *cj_method = cJSON_GetObjectItem(cj_block_options, ezlopi_key_method_str); if (cj_method) { __new_method_populate(&p_block_options->method, cj_method); @@ -670,7 +702,7 @@ static void __new_block_options_populate(s_block_options_v2_t *p_block_options, static void __new_method_populate(s_method_v2_t *p_method, cJSON *cj_method) { - CJSON_GET_VALUE_STRING_BY_COPY(cj_method, "name", p_method->name); + CJSON_GET_VALUE_STRING_BY_COPY(cj_method, ezlopi_name_str, p_method->name); p_method->type = ezlopi_scenes_method_get_type_enum(p_method->name); } @@ -714,100 +746,118 @@ static l_fields_v2_t *__fields_populate(cJSON *cj_fields) return tmp_fields_head; } -static l_fields_v2_t *__new_field_populate(cJSON *cj_field) +static void __fields_get_value(l_fields_v2_t *field, cJSON *cj_value) { - l_fields_v2_t *field = NULL; - if (cj_field) + if (field && cj_value) { - field = malloc(sizeof(l_fields_v2_t)); - if (field) + trace("type: %s", ezlopi_scene_get_scene_value_type_name_v2(field->value_type)); + switch (cj_value->type) { - memset(field, 0, sizeof(l_fields_v2_t)); - CJSON_GET_VALUE_STRING_BY_COPY(cj_field, "name", field->name); - field->value_type = __new_get_value_type(cj_field); - - cJSON *cj_value = cJSON_GetObjectItem(cj_field, "value"); + case cJSON_Number: + { + field->value.type = VALUE_TYPE_NUMBER; + field->value.value_double = cj_value->valuedouble; + TRACE_B("value: %f", field->value.value_double); + break; + } + case cJSON_String: + { + field->value.type = VALUE_TYPE_STRING; + uint32_t value_len = strlen(cj_value->valuestring) + 1; + field->value.value_string = malloc(value_len); + if (field->value.value_string) + { + snprintf(field->value.value_string, value_len, "%s", cj_value->valuestring); + TRACE_B("value: %s", field->value.value_string); + } + else + { + TRACE_E("Malloc failed!"); + } + break; + } + case cJSON_True: + { + field->value.type = VALUE_TYPE_BOOL; + field->value.value_bool = true; + TRACE_B("value: true"); + break; + } + case cJSON_False: + { + field->value.type = VALUE_TYPE_BOOL; + field->value.value_bool = false; + TRACE_B("value: false"); + break; + } + case cJSON_Object: + { + field->value.type = VALUE_TYPE_CJSON; + field->value.cj_value = cJSON_Duplicate(cj_value, cJSON_True); + CJSON_TRACE("value", field->value.cj_value); + break; + } + case cJSON_Array: + { + int block_idx = 0; + cJSON *cj_block = NULL; + field->value.type = VALUE_TYPE_BLOCK; + CJSON_TRACE("value", cj_value); - if (cj_value) + while (NULL != (cj_block = cJSON_GetArrayItem(cj_value, block_idx++))) { - switch (cj_value->type) - { - case cJSON_Number: - { - field->value.value_double = cj_value->valuedouble; - TRACE_B("value: %f", field->value.value_double); - break; - } - case cJSON_String: + if (field->value.when_block) { - uint32_t value_len = strlen(cj_value->valuestring) + 1; - field->value.value_string = malloc(value_len); - if (field->value.value_string) - { - snprintf(field->value.value_string, value_len, "%s", cj_value->valuestring); - TRACE_B("value: %s", field->value.value_string); - } - else + l_when_block_v2_t *curr_when_block = field->value.when_block; + while (curr_when_block->next) { - TRACE_E("Malloc failed!"); + curr_when_block = curr_when_block->next; } - break; + curr_when_block->next = __new_when_block_populate(cj_block); } - case cJSON_True: + else { - field->value.value_bool = true; - TRACE_B("value: true"); - break; - } - case cJSON_False: - { - field->value.value_bool = false; - TRACE_B("value: false"); - break; - } - case cJSON_Array: - { - cJSON *cj_block = NULL; - int block_idx = 0; - - while (NULL != (cj_block = cJSON_GetArrayItem(cj_value, block_idx++))) - { - if (field->value.when_block) - { - l_when_block_v2_t *curr_when_block = field->value.when_block; - while (curr_when_block->next) - { - curr_when_block = curr_when_block->next; - } - curr_when_block->next = __new_when_block_populate(cj_block); - } - else - { - field->value.when_block = __new_when_block_populate(cj_block); - } - } - break; - } - default: - { - TRACE_E("cj_value type: %d", cj_value->type); - break; - } + field->value.when_block = __new_when_block_populate(cj_block); } } + break; + } + default: + { + field->value.type = VALUE_TYPE_UNDEFINED; + TRACE_E("cj_value type: %d", cj_value->type); + break; + } + } + } +} + +static l_fields_v2_t *__new_field_populate(cJSON *cj_field) +{ + l_fields_v2_t *field = NULL; + if (cj_field) + { + field = malloc(sizeof(l_fields_v2_t)); + if (field) + { + memset(field, 0, sizeof(l_fields_v2_t)); + CJSON_GET_VALUE_STRING_BY_COPY(cj_field, ezlopi_name_str, field->name); + + field->value_type = ezlopi_scenes_get_expressions_value_type(cJSON_GetObjectItem(cj_field, ezlopi_type_str)); + __fields_get_value(field, cJSON_GetObjectItem(cj_field, ezlopi_value_str)); } } return field; } -static e_scene_value_type_v2_t __new_get_value_type(cJSON *cj_field) +e_scene_value_type_v2_t ezlopi_scenes_get_value_type(cJSON *cj_field) { e_scene_value_type_v2_t ret = EZLOPI_VALUE_TYPE_NONE; if (cj_field) { char *type_str = NULL; - CJSON_GET_VALUE_STRING(cj_field, "type", type_str); + CJSON_GET_VALUE_STRING(cj_field, ezlopi_type_str, type_str); if (type_str) { uint32_t type_str_len = strlen(type_str); @@ -825,3 +875,23 @@ static e_scene_value_type_v2_t __new_get_value_type(cJSON *cj_field) } return ret; } + +e_scene_value_type_v2_t ezlopi_scenes_get_expressions_value_type(cJSON *cj_value_type) +{ + e_scene_value_type_v2_t ret = EZLOPI_VALUE_TYPE_NONE; + if (cj_value_type && cj_value_type->valuestring) + { + uint32_t type_str_len = strlen(cj_value_type->valuestring); + for (int i = EZLOPI_VALUE_TYPE_NONE; i < EZLOPI_VALUE_TYPE_MAX; i++) + { + uint32_t check_str_len = strlen(scenes_value_type_name[i]); + uint32_t check_len = (check_str_len < type_str_len) ? type_str_len : check_str_len; + if (0 == strncmp(scenes_value_type_name[i], cj_value_type->valuestring, check_len)) + { + ret = i; + break; + } + } + } + return ret; +} \ No newline at end of file diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.h b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.h index ef573314c..fa827c5d2 100644 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.h +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_v2.h @@ -56,13 +56,26 @@ typedef struct s_action_delay_v2 uint16_t seconds; } s_action_delay_v2_t; +typedef enum e_value_type +{ + VALUE_TYPE_UNDEFINED = 0, + VALUE_TYPE_NUMBER, + VALUE_TYPE_STRING, + VALUE_TYPE_BOOL, + VALUE_TYPE_CJSON, + VALUE_TYPE_BLOCK, + VALUE_TYPE_MAX +} e_value_type_t; + typedef union u_field_value_v2 { + e_value_type_t type; char *value_string; double value_double; - int value_int; - uint32_t value_uint; + // int value_int; + // uint32_t value_uint; bool value_bool; + cJSON *cj_value; struct l_when_block_v2 *when_block; } u_field_value_v2_t; @@ -73,7 +86,7 @@ typedef struct l_fields_v2 e_scene_value_type_v2_t value_type; // 0: double, 1: string u_field_value_v2_t value; char *scale; - void *user_arg; + void *user_arg; // user by when-methods struct l_fields_v2 *next; } l_fields_v2_t; @@ -125,7 +138,7 @@ typedef struct l_scenes_list_v2 TaskHandle_t task_handle; uint32_t _id; - uint32_t enabled; + bool enabled; uint32_t is_group; char group_id[32]; char name[32]; @@ -146,12 +159,6 @@ void ezlopi_scenes_init_v2(void); uint32_t ezlopi_store_new_scene_v2(cJSON *cj_new_scene); uint32_t ezlopi_scenes_get_list_v2(cJSON *cj_scenes_array); -void ezlopi_scenes_print(l_scenes_list_v2_t *scene_link_list); -void ezlopi_scenes_delete(l_scenes_list_v2_t *scenes_list); -l_scenes_list_v2_t *ezlopi_scenes_pop_by_id_v2(uint32_t _id); -void ezlopi_scenes_depopulate_by_id_v2(uint32_t _id); -void ezlopi_scenes_remove_id_from_list_v2(uint32_t _id); - l_scenes_list_v2_t *ezlopi_scenes_get_scenes_head_v2(void); f_scene_method_v2_t ezlopi_scene_get_method_v2(e_scene_method_type_t scene_method_type); const char *ezlopi_scene_get_scene_value_type_name_v2(e_scene_value_type_v2_t value_type); @@ -159,12 +166,20 @@ const char *ezlopi_scene_get_scene_value_type_name_v2(e_scene_value_type_v2_t va l_scenes_list_v2_t *ezlopi_scenes_get_by_id_v2(uint32_t _id); l_scenes_list_v2_t *ezlopi_scenes_new_scene_populate(cJSON *cj_new_scene, uint32_t scene_id); -cJSON *ezlopi_scenes_cjson_create_then_block(l_action_block_v2_t *then_block); -cJSON *ezlopi_scenes_cjson_create_when_block(l_when_block_v2_t *when_block); -void ezlopi_scenes_cjson_add_then_blocks(cJSON *root, l_action_block_v2_t *then_blocks); -void ezlopi_scenes_cjson_add_when_blocks(cJSON *root, l_when_block_v2_t *when_blocks); -cJSON *ezlopi_scenes_create_cjson_scene(l_scenes_list_v2_t *scene); -cJSON *ezlopi_scenes_create_cjson_scene_list(l_scenes_list_v2_t *scenes_list); -char *ezlopi_scenes_create_json_string(l_scenes_list_v2_t *scenes_list); +e_scene_value_type_v2_t ezlopi_scenes_get_value_type(cJSON *cj_field); +e_scene_value_type_v2_t ezlopi_scenes_get_expressions_value_type(cJSON *cj_value_type); + +void ezlopi_scene_add_users_in_notifications(l_scenes_list_v2_t *scene_node, cJSON *cj_user); + +void ezlopi_scenes_delete(l_scenes_list_v2_t *scenes_list); +l_scenes_list_v2_t *ezlopi_scenes_pop_by_id_v2(uint32_t _id); +void ezlopi_scenes_depopulate_by_id_v2(uint32_t _id); +void ezlopi_scenes_remove_id_from_list_v2(uint32_t _id); +void ezlopi_scenes_delete_user_notifications(l_user_notification_v2_t *user_notifications); + +void ezlopi_scenes_print(l_scenes_list_v2_t *scene_link_list); + +void ezlopi_scenes_notifications_add(cJSON *cj_notifications); +int ezlopi_scenes_edit_by_id(uint32_t scene_id, cJSON *cj_updated_scene); #endif // __EZLOPI_SCENES_V2_H__ diff --git a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_when_methods.c b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_when_methods.c index f2d4adc0d..a3e18f9ac 100755 --- a/ezlopi-core/ezlopi_scenes/ezlopi_scenes_when_methods.c +++ b/ezlopi-core/ezlopi_scenes/ezlopi_scenes_when_methods.c @@ -1,8 +1,10 @@ #include #include "trace.h" + #include "ezlopi_devices.h" #include "ezlopi_scenes_v2.h" +#include "ezlopi_cloud_constants.h" #include "ezlopi_scenes_operators.h" #include "ezlopi_scenes_when_methods.h" @@ -23,7 +25,7 @@ int ezlopi_scene_when_is_item_state(l_scenes_list_v2_t *scene_node, void *arg) { item_id = strtoul(curr_field->value.value_string, NULL, 16); } - else if (0 == strncmp(curr_field->name, "value", 4)) + else if (0 == strncmp(curr_field->name, ezlopi_value_str, 4)) { value_field = curr_field; } @@ -44,7 +46,7 @@ int ezlopi_scene_when_is_item_state(l_scenes_list_v2_t *scene_node, void *arg) if (cj_tmp_value) { curr_item->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, curr_item, (void *)cj_tmp_value, NULL); - cJSON *cj_value = cJSON_GetObjectItem(cj_tmp_value, "value"); + cJSON *cj_value = cJSON_GetObjectItem(cj_tmp_value, ezlopi_value_str); if (cj_value) { switch (cj_value->type) @@ -135,14 +137,14 @@ int ezlopi_scene_when_is_interval(l_scenes_list_v2_t *scene_node, void *arg) if (((xTaskGetTickCount() - last_update) / 1000) > interval) { - scene_node->when_block->fields->user_arg = xTaskGetTickCount(); + scene_node->when_block->fields->user_arg = (void *)xTaskGetTickCount(); ret = 1; } } else { ret = 1; - scene_node->when_block->fields->user_arg = xTaskGetTickCount(); + scene_node->when_block->fields->user_arg = (void *)xTaskGetTickCount(); } } @@ -263,7 +265,7 @@ int ezlopi_scene_when_compare_numbers(l_scenes_list_v2_t *scene_node, void *arg) { item_id = strtoul(curr_field->value.value_string, NULL, 16); } - else if (0 == strncmp(curr_field->name, "value", 4)) + else if (0 == strncmp(curr_field->name, ezlopi_value_str, 4)) { value_field = curr_field; } @@ -276,7 +278,7 @@ int ezlopi_scene_when_compare_numbers(l_scenes_list_v2_t *scene_node, void *arg) if (item_id && value_field && comparator_field) { - ret = ezlopi_scenes_operators_compare_value_number(item_id, value_field, comparator_field); + ret = ezlopi_scenes_operators_value_number_operations(item_id, value_field, comparator_field); } } diff --git a/ezlopi-core/ezlopi_valueformatter/ezlopi_valueformatter.c b/ezlopi-core/ezlopi_valueformatter/ezlopi_valueformatter.c index eacb704b1..d9bc591ff 100755 --- a/ezlopi-core/ezlopi_valueformatter/ezlopi_valueformatter.c +++ b/ezlopi-core/ezlopi_valueformatter/ezlopi_valueformatter.c @@ -3,9 +3,11 @@ #include #include +#include "ezlopi_cloud_constants.h" + char *ezlopi_valueformatter_bool(bool val) { - return val ? "true" : "false"; + return val ? ezlopi_true_str : ezlopi_false_str; } char *ezlopi_valueformatter_float(float val) diff --git a/ezlopi-core/ezlopi_websocket_client/ezlopi_websocket_client.c b/ezlopi-core/ezlopi_websocket_client/ezlopi_websocket_client.c index 3caa462e2..147bb45cd 100644 --- a/ezlopi-core/ezlopi_websocket_client/ezlopi_websocket_client.c +++ b/ezlopi-core/ezlopi_websocket_client/ezlopi_websocket_client.c @@ -93,7 +93,7 @@ esp_websocket_client_handle_t ezlopi_websocket_client_init(cJSON *uri, void (*ms esp_websocket_client_config_t websocket_cfg = { .uri = uri->valuestring, .task_stack = 8 * 1024, - .buffer_size = 2 * 1024, + .buffer_size = 12 * 1024, .cert_pem = ezlopi_factory_info_v3_get_ca_certificate(), .client_cert = ezlopi_factory_info_v3_get_ssl_shared_key(), .client_key = ezlopi_factory_info_v3_get_ssl_private_key(), @@ -219,85 +219,3 @@ static void ezlopi_ws_data_buffer_free(s_ws_data_buffer_t *buffer) free(buffer); } } - -#if 0 -using namespace std; - -int websocket_client::send(std::string &_str) -{ - int ret = false; - if (esp_websocket_client_is_connected(client) && _str.length()) - { - ret = esp_websocket_client_send_text(client, _str.c_str(), _str.length(), portMAX_DELAY); - } - else - { - TRACE_E("ERROR: websocket client is not connected!"); - } - return ret; -} - -int websocket_client::send(char *c_str) -{ - int ret = false; - if (esp_websocket_client_is_connected(client)) - { - ret = esp_websocket_client_send_text(client, c_str, strlen(c_str), portMAX_DELAY); - } - else - { - TRACE_E("ERROR: websocket is not connected!"); - } - return ret; -} - -esp_websocket_client_handle_t websocket_client::websocket_app_start(string &uri, void (*msg_upcall)(const char *, uint32_t)) -{ - if (!client) - { - static s_ws_event_arg_t event_arg = { - .msg_upcall = msg_upcall, - }; - - // ezlopi_factory_info *factory = ezlopi_factory_info::get_instance(); - s_ezlopi_factory_info_t *factory = ezlopi_factory_info_get_info(); - - esp_websocket_client_config_t websocket_cfg = { - .uri = uri.c_str(), - .task_stack = 8 * 1024, - .cert_pem = factory->ca_certificate, - .client_cert = factory->ssl_shared_key, - .client_key = factory->ssl_private_key, - .pingpong_timeout_sec = 20, - .keep_alive_enable = 1, - .ping_interval_sec = 10, - }; - - TRACE_I("Connecting to %s...", websocket_cfg.uri); - - client = esp_websocket_client_init(&websocket_cfg); - esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)&event_arg); - esp_websocket_client_start(client); - event_arg.client = client; - } - else - { - TRACE_I("Client already active!"); - } - - return client; -} - -bool websocket_client::is_connected(void) -{ - return esp_websocket_client_is_connected(client); -} - -void websocket_client::websocket_client_kill(void) -{ - esp_websocket_client_stop(client); - TRACE_I("Websocket Stopped"); - esp_websocket_client_destroy(client); - client = NULL; -} -#endif diff --git a/ezlopi-core/ezlopi_wifi/ezlopi_wifi.c b/ezlopi-core/ezlopi_wifi/ezlopi_wifi.c index da9332f6a..b0dcc0d45 100644 --- a/ezlopi-core/ezlopi_wifi/ezlopi_wifi.c +++ b/ezlopi-core/ezlopi_wifi/ezlopi_wifi.c @@ -41,7 +41,7 @@ static int station_got_ip = 0; static const char *const wifi_no_error_str = "NO_ERROR"; static const char *last_disconnect_reason = wifi_no_error_str; static ll_ezlopi_wifi_event_upcall_t *__event_upcall_head = NULL; -static bool ezlopi_flag_wifi_status = false; +static volatile bool ezlopi_flag_wifi_status = false; static ll_ezlopi_wifi_event_upcall_t *ezlopi_wifi_event_upcall_create(f_ezlopi_wifi_event_upcall *upcall, void *arg); @@ -111,33 +111,14 @@ ezlopi_wifi_status_t *ezlopi_wifi_status(void) return wifi_stat; } -static void set_wifi_station_host_name(void) +static esp_err_t set_wifi_station_host_name(void) { static char station_host_name[32]; snprintf(station_host_name, sizeof(station_host_name), "EZLOPI-%llu", ezlopi_factory_info_v3_get_id()); esp_err_t err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, station_host_name); - TRACE_W("'tcpip_adapter_set_hostname' ERROR: %s", esp_err_to_name(err)); -} - -#if 0 -static void alert_qt_wifi_got_ip(void) -{ - if (new_wifi) - { - new_wifi = 0; - char *qt_resp = "{\"cmd\":2,\"status_write\":1,\"status_connect\":1}"; - qt_serial_tx_data(strlen(qt_resp), (uint8_t *)qt_resp); - } - else - { - - char *qt_resp = "{\"cmd\":2,\"status_connect\":1}"; - qt_serial_tx_data(strlen(qt_resp), (uint8_t *)qt_resp); - } + return err; } -#endif - static void __event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) @@ -163,7 +144,6 @@ static void __event_handler(void *arg, esp_event_base_t event_base, int32_t even else { ezlopi_event_group_set_event(EZLOPI_EVENT_WIFI_FAIL); - // alert_qt_wifi_fail(); s_retry_num = 0; } TRACE_W("connect to the AP fail"); @@ -283,24 +263,40 @@ esp_err_t ezlopi_wifi_connect(const char *ssid, const char *pass) wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; esp_wifi_stop(); - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - err = esp_wifi_set_config(WIFI_IF_STA, &wifi_config); - ESP_ERROR_CHECK(esp_wifi_start()); - set_wifi_station_host_name(); + esp_err_t _err = esp_wifi_set_mode(WIFI_MODE_STA); + // TRACE_W("WiFi Err : %s", esp_err_to_name(_err)); + if (_err == ESP_OK) + { + _err = esp_wifi_set_config(WIFI_IF_STA, &wifi_config); + // TRACE_W("WiFi Err : %s", esp_err_to_name(_err)); + if (_err == ESP_OK) + { + _err = esp_wifi_start(); + // TRACE_W("WiFi Err : %s", esp_err_to_name(_err)); + if (_err == ESP_OK) + { + _err = set_wifi_station_host_name(); + // TRACE_W("WiFi Err : %s", esp_err_to_name(_err)); + if (_err != ESP_OK) + TRACE_W("'tcpip_adapter_set_hostname' ERROR: %s", esp_err_to_name(err)); + } + } + } + err = _err; } - + TRACE_W("WiFi Err : %s", esp_err_to_name(err)); return err; } -uint32_t ezlopi_wait_for_wifi_to_connect(uint32_t wait_time_ms) +int ezlopi_wait_for_wifi_to_connect(uint32_t wait_time_ms) { uint32_t ret = 0; - while (-1 == ezlopi_event_group_wait_for_event(EZLOPI_EVENT_WIFI_CONNECTED, wait_time_ms / portTICK_RATE_MS, 0)) + while (-1 == ezlopi_event_group_wait_for_event(EZLOPI_EVENT_WIFI_CONNECTED, wait_time_ms, 0)) { vTaskDelay(100 / portTICK_PERIOD_MS); } - ret = ezlopi_event_group_wait_for_event(EZLOPI_EVENT_WIFI_CONNECTED, wait_time_ms / portTICK_RATE_MS, 0); + ret = ezlopi_event_group_wait_for_event(EZLOPI_EVENT_WIFI_CONNECTED, wait_time_ms, 0); return ret; } diff --git a/ezlopi-core/ezlopi_wifi/ezlopi_wifi.h b/ezlopi-core/ezlopi_wifi/ezlopi_wifi.h index 3acc02a69..e48b67d41 100644 --- a/ezlopi-core/ezlopi_wifi/ezlopi_wifi.h +++ b/ezlopi-core/ezlopi_wifi/ezlopi_wifi.h @@ -31,7 +31,7 @@ extern "C" esp_err_t ezlopi_wifi_connect(const char *ssid, const char *pass); void ezlopi_wifi_connect_from_nvs(void); void ezlopi_wifi_connect_from_id_bin(void); - uint32_t ezlopi_wait_for_wifi_to_connect(uint32_t wait_time_ms); + int ezlopi_wait_for_wifi_to_connect(uint32_t wait_time_ms); esp_netif_ip_info_t *ezlopi_wifi_get_ip_infos(void); const char *ezlopi_wifi_get_last_disconnect_reason(void); diff --git a/lua_modules/core/core_getters_api.c b/lua_modules/core/core_getters_api.c index 69abdc610..eaf8a5650 100644 --- a/lua_modules/core/core_getters_api.c +++ b/lua_modules/core/core_getters_api.c @@ -27,7 +27,7 @@ int lcore_get_product_name(lua_State *lua_state) } else { - lua_pushstring(lua_state, ""); + lua_pushstring(lua_state, ezlopi__str); } return ret; } @@ -217,48 +217,50 @@ static int __create_lua_table_for_device(lua_State *lua_state, l_ezlopi_device_t snprintf(tmp_str, sizeof(tmp_str), "%08x", ezlopi_gateway_get()->_id); lua_create_table_string_key_value(ezlopi_gateway_id_str, &tmp_str[0]); - lua_create_table_string_key_value("name", device_prop->cloud_properties.device_name); - lua_create_table_string_key_value("category", device_prop->cloud_properties.category); - lua_create_table_string_key_value("subcategory", device_prop->cloud_properties.subcategory); - lua_create_table_string_key_value("type", device_prop->cloud_properties.device_type); - lua_create_table_string_key_value("device_type_id", controller_info->device_type_id); - lua_create_table_string_key_value("room_id", controller_info->room_id); - lua_create_table_string_key_value("parent_device_id", controller_info->parent_device_id); + lua_create_table_string_key_value(ezlopi_name_str, device_prop->cloud_properties.device_name); + lua_create_table_string_key_value(ezlopi_category_str, device_prop->cloud_properties.category); + lua_create_table_string_key_value(ezlopi_subcategory_str, device_prop->cloud_properties.subcategory); + lua_create_table_string_key_value(ezlopi_type_str, device_prop->cloud_properties.device_type); + lua_create_table_string_key_value(ezlopi_device_type_id_str, controller_info->device_type_id); + lua_create_table_string_key_value(ezlopi_room_id_str, controller_info->room_id); + lua_create_table_string_key_value(ezlopi_parent_device_id_str, controller_info->parent_device_id); - lua_pushstring(lua_state, "info"); + lua_pushstring(lua_state, ezlopi_info_str); lua_newtable(lua_state); char *manufacturer = ezlopi_factory_info_v3_get_manufacturer(); - lua_create_table_string_key_value("manufacturer", manufacturer); + lua_create_table_string_key_value(ezlopi_manufacturer_str, manufacturer); free(manufacturer); char *model = ezlopi_factory_info_v3_get_model(); - lua_create_table_string_key_value("model", model); + lua_create_table_string_key_value(ezlopi_model_str, model); free(model); char *brand = ezlopi_factory_info_v3_get_brand(); - lua_create_table_string_key_value("brand", brand); + lua_create_table_string_key_value(ezlopi_brand_str, brand); free(brand); lua_settable(lua_state, -3); - lua_pushstring(lua_state, "firmware"); + lua_pushstring(lua_state, ezlopi_firmware_str); lua_newtable(lua_state); - lua_create_table_string_key_value("version", VERSION_STR); - lua_create_table_string_key_value("build-date", COMPILE_TIME); - lua_create_table_string_key_value("hash", COMMIT_HASH); - lua_create_table_string_key_value("branch", CURRENT_BRANCH); - lua_create_table_string_key_value("developer", DEVELOPER); + lua_create_table_string_key_value(ezlopi_version_str, VERSION_STR); + lua_create_table_string_key_value(ezlopi_build__date_str, COMPILE_TIME); + lua_create_table_string_key_value(ezlopi_hash_str, COMMIT_HASH); + lua_create_table_string_key_value(ezlopi_branch_str, CURRENT_BRANCH); + lua_create_table_string_key_value(ezlopi_developer_str, DEVELOPER); lua_settable(lua_state, -3); - lua_create_table_bool_key_value("battery_powered", controller_info->battery_powered); - lua_create_table_bool_key_value("reachable", controller_info->reachable); - lua_create_table_bool_key_value("persistent", controller_info->persistent); - lua_create_table_string_key_value("security", controller_info->security); - lua_create_table_bool_key_value("ready", controller_info->ready); - lua_create_table_string_key_value("status", controller_info->status ? controller_info->status : ""); - lua_create_table_bool_key_value("house_modes_options", ""); - lua_create_table_bool_key_value("parent_room", ""); + lua_create_table_bool_key_value(ezlopi_battery_powered_str, controller_info->battery_powered); + lua_create_table_bool_key_value(ezlopi_reachable_str, controller_info->reachable); + lua_create_table_bool_key_value(ezlopi_persistent_str, controller_info->persistent); + lua_create_table_string_key_value(ezlopi_security_str, controller_info->security); + lua_create_table_bool_key_value(ezlopi_ready_str, controller_info->ready); + lua_create_table_string_key_value(ezlopi_status_str, controller_info->status ? controller_info->status : ezlopi__str); + +#warning "Krishna needs to verify below 2 lines" + lua_create_table_bool_key_value(ezlopi_house_modes_options_str, ezlopi__str); + lua_create_table_bool_key_value(ezlopi_parent_room_str, ezlopi__str); return 1; } @@ -274,21 +276,21 @@ static int __create_lua_table_for_item(lua_State *lua_state, l_ezlopi_item_t *it lua_create_table_string_key_value(ezlopi_id_str, &tmp_str[0]); snprintf(tmp_str, sizeof(tmp_str), "%08x", device_id); - lua_create_table_string_key_value("device_id", &tmp_str[0]); + lua_create_table_string_key_value(ezlopi_device_id_str, &tmp_str[0]); - lua_create_table_string_key_value("name", item_prop->cloud_properties.item_name); - lua_create_table_bool_key_value("has_getter", item_prop->cloud_properties.has_getter); - lua_create_table_bool_key_value("has_setter", item_prop->cloud_properties.has_setter); - lua_create_table_bool_key_value("show", item_prop->cloud_properties.show); - lua_create_table_string_key_value("scale", item_prop->cloud_properties.scale); - lua_create_table_string_key_value("value_type", item_prop->cloud_properties.value_type); + lua_create_table_string_key_value(ezlopi_name_str, item_prop->cloud_properties.item_name); + lua_create_table_bool_key_value(ezlopi_has_getter_str, item_prop->cloud_properties.has_getter); + lua_create_table_bool_key_value(ezlopi_has_setter_str, item_prop->cloud_properties.has_setter); + lua_create_table_bool_key_value(ezlopi_show_str, item_prop->cloud_properties.show); + lua_create_table_string_key_value(ezlopi_scale_str, item_prop->cloud_properties.scale); + lua_create_table_string_key_value(ezlopi_value_type_str, item_prop->cloud_properties.value_type); if (value_type_token == item_prop->cloud_properties.value_type) { - lua_pushstring(lua_state, "enum"); + lua_pushstring(lua_state, ezlopi_enum_str); lua_newtable(lua_state); - lua_create_table_string_value(1, "None"); // Remains for future + lua_create_table_string_value(1, ezlopi_None_str); // Remains for future lua_settable(lua_state, -3); } @@ -298,7 +300,7 @@ static int __create_lua_table_for_item(lua_State *lua_state, l_ezlopi_item_t *it if (cj_result) { item_prop->func(EZLOPI_ACTION_GET_EZLOPI_VALUE, item_prop, cj_result, item_prop->user_arg); - cJSON *cj_value = cJSON_GetObjectItem(cj_result, "value"); + cJSON *cj_value = cJSON_GetObjectItem(cj_result, ezlopi_value_str); if (cj_value) { switch (cj_value->type) @@ -306,19 +308,19 @@ static int __create_lua_table_for_item(lua_State *lua_state, l_ezlopi_item_t *it case cJSON_True: case cJSON_False: { - lua_create_table_bool_key_value("value", cj_value->valuedouble ? true : false); + lua_create_table_bool_key_value(ezlopi_value_str, cj_value->valuedouble ? true : false); break; } case cJSON_Number: { - lua_create_table_number_key_value("value", cj_value->valuedouble); + lua_create_table_number_key_value(ezlopi_value_str, cj_value->valuedouble); break; } case cJSON_String: { if (cj_value->valuestring) { - lua_create_table_string_key_value("value", cj_value->valuestring); + lua_create_table_string_key_value(ezlopi_value_str, cj_value->valuestring); } break; } @@ -343,17 +345,20 @@ static int __create_lua_table_for_gateway(lua_State *lua_state, s_ezlopi_gateway snprintf(tmp_str, sizeof(tmp_str), "%08x", gateway_prop->_id); lua_create_table_string_key_value(ezlopi_id_str, &tmp_str[0]); - lua_create_table_string_key_value("name", gateway_prop->name); - lua_create_table_bool_key_value("plugin_id", gateway_prop->pluginid); - lua_create_table_bool_key_value("lable", gateway_prop->label); - lua_create_table_bool_key_value("reason", ""); - lua_create_table_string_key_value("unreachable_reasons", ""); - lua_create_table_string_key_value("unreachable_actions", ""); - lua_create_table_bool_key_value("manual_device_adding", gateway_prop->manual_device_adding); + lua_create_table_string_key_value(ezlopi_name_str, gateway_prop->name); + +#warning "Krishna needs to check below 3 lines" + lua_create_table_bool_key_value(ezlopi_plugin_id_str, gateway_prop->pluginid); + lua_create_table_bool_key_value(ezlopi_label_str, gateway_prop->label); + lua_create_table_bool_key_value(ezlopi_reason_str, ezlopi__str); + + lua_create_table_string_key_value(ezlopi_unreachable_reasons_str, ezlopi__str); + lua_create_table_string_key_value(ezlopi_unreachable_actions_str, ezlopi__str); + lua_create_table_bool_key_value(ezlopi_manual_device_adding_str, gateway_prop->manual_device_adding); - lua_pushstring(lua_state, "info"); + lua_pushstring(lua_state, ezlopi_info_str); lua_newtable(lua_state); - lua_create_table_bool_key_value("manual_device_adding", gateway_prop->status); + lua_create_table_bool_key_value(ezlopi_manual_device_adding_str, gateway_prop->status); lua_settable(lua_state, -3); return 1; diff --git a/lua_modules/core/core_updaters_api.c b/lua_modules/core/core_updaters_api.c index d6c5d425d..eced1be87 100644 --- a/lua_modules/core/core_updaters_api.c +++ b/lua_modules/core/core_updaters_api.c @@ -5,6 +5,8 @@ #include "trace.h" #include "ezlopi_devices.h" +#include "ezlopi_cloud_constants.h" + #include "core_updaters_api.h" int lcore_update_item_value(lua_State *lua_state) @@ -23,7 +25,7 @@ int lcore_update_item_value(lua_State *lua_state) { char tmp_buffer[32]; snprintf(tmp_buffer, sizeof(tmp_buffer), "%08x", item_node->cloud_properties.item_id); - cJSON_AddStringToObject(cj_params, "_id", tmp_buffer); + cJSON_AddStringToObject(cj_params, ezlopi__id_str, tmp_buffer); int check_val = lua_type(lua_state, 2); @@ -32,21 +34,21 @@ int lcore_update_item_value(lua_State *lua_state) case LUA_TBOOLEAN: { int bool_val = lua_toboolean(lua_state, 2); - cJSON_AddBoolToObject(cj_params, "value", bool_val); + cJSON_AddBoolToObject(cj_params, ezlopi_value_str, bool_val); ret = 1; break; } case LUA_TNUMBER: { double number_val = lua_tonumber(lua_state, 2); - cJSON_AddNumberToObject(cj_params, "value", number_val); + cJSON_AddNumberToObject(cj_params, ezlopi_value_str, number_val); ret = 1; break; } case LUA_TSTRING: { char *string_val = lua_tostring(lua_state, 2); - cJSON_AddStringToObject(cj_params, "value", string_val); + cJSON_AddStringToObject(cj_params, ezlopi_value_str, string_val); ret = 1; break; } diff --git a/lua_modules/helper_functions/lua_helper_functions.h b/lua_modules/helper_functions/lua_helper_functions.h index eb0e98656..73e5e3e1a 100644 --- a/lua_modules/helper_functions/lua_helper_functions.h +++ b/lua_modules/helper_functions/lua_helper_functions.h @@ -1,11 +1,11 @@ #ifndef __LUA_HELPER_FUNCTIONS_H__ #define __LUA_HELPER_FUNCTIONS_H__ -#define lua_create_table_string_key_value(key, value) \ - { \ - lua_pushstring(lua_state, key); \ - lua_pushstring(lua_state, value ? value : ""); \ - lua_settable(lua_state, -3); \ +#define lua_create_table_string_key_value(key, value) \ + { \ + lua_pushstring(lua_state, key); \ + lua_pushstring(lua_state, value ? value : ezlopi__str); \ + lua_settable(lua_state, -3); \ } #define lua_create_table_bool_key_value(key, value) \ { \ @@ -21,11 +21,11 @@ lua_settable(lua_state, -3); \ } -#define lua_create_table_string_value(idx, value) \ - { \ - lua_pushnumber(lua_state, idx); \ - lua_pushstring(lua_state, value ? value : ""); \ - lua_settable(lua_state, -3); \ +#define lua_create_table_string_value(idx, value) \ + { \ + lua_pushnumber(lua_state, idx); \ + lua_pushstring(lua_state, value ? value : ezlopi__str); \ + lua_settable(lua_state, -3); \ } #endif // __LUA_HELPER_FUNCTIONS_H__ diff --git a/sdkconfig b/sdkconfig index 9ae98b718..cafbebad3 100644 --- a/sdkconfig +++ b/sdkconfig @@ -44,12 +44,12 @@ CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF is not set # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE is not set # CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set -# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set +CONFIG_BOOTLOADER_LOG_LEVEL_ERROR=y # CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set -CONFIG_BOOTLOADER_LOG_LEVEL_INFO=y +# CONFIG_BOOTLOADER_LOG_LEVEL_INFO is not set # CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set # CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set -CONFIG_BOOTLOADER_LOG_LEVEL=3 +CONFIG_BOOTLOADER_LOG_LEVEL=1 # CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_8V is not set CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y # CONFIG_BOOTLOADER_FACTORY_RESET is not set @@ -1532,12 +1532,12 @@ CONFIG_LUA_ROOT="/lua" # Deprecated options for backward compatibility CONFIG_TOOLPREFIX="xtensa-esp32-elf-" # CONFIG_LOG_BOOTLOADER_LEVEL_NONE is not set -# CONFIG_LOG_BOOTLOADER_LEVEL_ERROR is not set +CONFIG_LOG_BOOTLOADER_LEVEL_ERROR=y # CONFIG_LOG_BOOTLOADER_LEVEL_WARN is not set -CONFIG_LOG_BOOTLOADER_LEVEL_INFO=y +# CONFIG_LOG_BOOTLOADER_LEVEL_INFO is not set # CONFIG_LOG_BOOTLOADER_LEVEL_DEBUG is not set # CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE is not set -CONFIG_LOG_BOOTLOADER_LEVEL=3 +CONFIG_LOG_BOOTLOADER_LEVEL=1 # CONFIG_APP_ROLLBACK_ENABLE is not set # CONFIG_FLASH_ENCRYPTION_ENABLED is not set # CONFIG_FLASHMODE_QIO is not set diff --git a/sdkconfig.old b/sdkconfig.old index adca43678..3483521b6 100644 --- a/sdkconfig.old +++ b/sdkconfig.old @@ -44,12 +44,12 @@ CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF is not set # CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE is not set # CONFIG_BOOTLOADER_LOG_LEVEL_NONE is not set -# CONFIG_BOOTLOADER_LOG_LEVEL_ERROR is not set +CONFIG_BOOTLOADER_LOG_LEVEL_ERROR=y # CONFIG_BOOTLOADER_LOG_LEVEL_WARN is not set -CONFIG_BOOTLOADER_LOG_LEVEL_INFO=y +# CONFIG_BOOTLOADER_LOG_LEVEL_INFO is not set # CONFIG_BOOTLOADER_LOG_LEVEL_DEBUG is not set # CONFIG_BOOTLOADER_LOG_LEVEL_VERBOSE is not set -CONFIG_BOOTLOADER_LOG_LEVEL=3 +CONFIG_BOOTLOADER_LOG_LEVEL=1 # CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_8V is not set CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y # CONFIG_BOOTLOADER_FACTORY_RESET is not set @@ -994,17 +994,20 @@ CONFIG_HEAP_TRACING_OFF=y # # Log output # -# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set +CONFIG_LOG_DEFAULT_LEVEL_NONE=y # CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set # CONFIG_LOG_DEFAULT_LEVEL_WARN is not set -CONFIG_LOG_DEFAULT_LEVEL_INFO=y +# CONFIG_LOG_DEFAULT_LEVEL_INFO is not set # CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set # CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set -CONFIG_LOG_DEFAULT_LEVEL=3 +CONFIG_LOG_DEFAULT_LEVEL=0 CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y +# CONFIG_LOG_MAXIMUM_LEVEL_ERROR is not set +# CONFIG_LOG_MAXIMUM_LEVEL_WARN is not set +# CONFIG_LOG_MAXIMUM_LEVEL_INFO is not set # CONFIG_LOG_MAXIMUM_LEVEL_DEBUG is not set # CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set -CONFIG_LOG_MAXIMUM_LEVEL=3 +CONFIG_LOG_MAXIMUM_LEVEL=0 CONFIG_LOG_COLORS=y CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y # CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set diff --git a/services/ezlopi_ble_service/CMakeLists.txt b/services/ezlopi_ble_service/CMakeLists.txt index a33a22eb2..d8595eaca 100644 --- a/services/ezlopi_ble_service/CMakeLists.txt +++ b/services/ezlopi_ble_service/CMakeLists.txt @@ -1,6 +1,6 @@ # file( GLOB_RECURSE provisioning_src "*.c*" "actions/*.c*" ) file(GLOB_RECURSE src_files "*.c*") -idf_component_register(SRCS "ezlopi_ble_service_device_info.c" "ezlopi_ble_service_dynamic_config.c" "ezlopi_ble_service.c" "ezlopi_ble_auth.c" "ezlopi_ble_service_provisioning.c" "${src_files}" +idf_component_register(SRCS "ezlopi_ble_service_security.c" "ezlopi_ble_service_wifi.c" "ezlopi_ble_service_device_info.c" "ezlopi_ble_service_dynamic_config.c" "ezlopi_ble_service.c" "ezlopi_ble_auth.c" "ezlopi_ble_service_provisioning.c" "${src_files}" INCLUDE_DIRS "." - REQUIRES driver freertos trace ezlopi_devices ezlopi_ble ezlopi_factory_info ezlopi_nvs ezlopi_ping) + REQUIRES driver freertos trace ezlopi_devices ezlopi_ble ezlopi_factory_info ezlopi_nvs ezlopi_ping ezlopi_reboot) diff --git a/services/ezlopi_ble_service/ezlopi_ble_service.c b/services/ezlopi_ble_service/ezlopi_ble_service.c index f0a1725a2..76c824747 100644 --- a/services/ezlopi_ble_service/ezlopi_ble_service.c +++ b/services/ezlopi_ble_service/ezlopi_ble_service.c @@ -54,6 +54,7 @@ void ezlopi_ble_service_init(void) CHECK_PRINT_ERROR(esp_ble_gatts_app_register(BLE_DYNAMIC_CONFIG_HANDLE), "gatts 'dynamic-config-app' register error"); CHECK_PRINT_ERROR(esp_ble_gatt_set_local_mtu(517), "set local MTU failed"); + #if (1 == EZLOPI_BLE_ENALBE_PAIRING) #if (1 == EZLOPI_BLE_ENALBE_PASSKEY) ezlopi_ble_start_secure_gatt_server(); @@ -74,14 +75,21 @@ static void ezlopi_ble_start_secure_gatt_server_open_pairing(void) const uint8_t init_key = (ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK); const uint8_t rsp_key = (ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK); - CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_AUTHEN_REQ_MODE"); - CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_IOCAP_MODE"); - CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH"); - CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_OOB_SUPPORT"); - CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_SET_INIT_KEY"); - CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_SET_RSP_KEY"); + CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t)), + "failed -set - ESP_BLE_SM_AUTHEN_REQ_MODE"); + CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t)), + "failed -set - ESP_BLE_SM_IOCAP_MODE"); + CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t)), + "failed -set - ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH"); + CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t)), + "failed -set - ESP_BLE_SM_OOB_SUPPORT"); + CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)), + "failed -set - ESP_BLE_SM_SET_INIT_KEY"); + CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)), + "failed -set - ESP_BLE_SM_SET_RSP_KEY"); } +#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) static void ezlopi_ble_start_secure_gatt_server(void) { const uint32_t default_passkey = 123456; @@ -108,6 +116,7 @@ static void ezlopi_ble_start_secure_gatt_server(void) CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_SET_INIT_KEY"); CHECK_PRINT_ERROR(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)), "failed -set - ESP_BLE_SM_SET_RSP_KEY"); } +#endif static void ezlopi_ble_basic_init(void) { diff --git a/services/ezlopi_ble_service/ezlopi_ble_service_device_info.c b/services/ezlopi_ble_service/ezlopi_ble_service_device_info.c index f45cb46a7..8fb9fad41 100644 --- a/services/ezlopi_ble_service/ezlopi_ble_service_device_info.c +++ b/services/ezlopi_ble_service/ezlopi_ble_service_device_info.c @@ -12,14 +12,14 @@ #include "ezlopi_wifi.h" #include "ezlopi_nvs.h" -#include "ezlopi_ble_gatt.h" -#include "ezlopi_ble_profile.h" - #include "ezlopi_ping.h" +#include "ezlopi_ble_gatt.h" #include "ezlopi_ble_buffer.h" +#include "ezlopi_ble_profile.h" #include "ezlopi_ble_service.h" -#include "ezlopi_factory_info.h" #include "ezlopi_system_info.h" +#include "ezlopi_factory_info.h" +#include "ezlopi_cloud_constants.h" static s_gatt_service_t *g_device_info_service = NULL; @@ -105,47 +105,47 @@ static char *device_info_jsonify(void) uint64_t uptime_us = esp_timer_get_time(); uint64_t uptime_sec = uptime_us / 1000000; - cJSON_AddStringToObject(root, "firmware_version", VERSION_STR); - cJSON_AddNumberToObject(root, "firmware_build", BUILD); - cJSON_AddStringToObject(root, "chip", CONFIG_IDF_TARGET); + cJSON_AddStringToObject(root, ezlopi_firmware_version_str, VERSION_STR); + cJSON_AddNumberToObject(root, ezlopi_firmware_build_str, BUILD); + cJSON_AddStringToObject(root, ezlopi_chip_str, CONFIG_IDF_TARGET); // cJSON_AddStringToObject(root, "flash_size", CONFIG_ESPTOOLPY_FLASHSIZE); // cJSON_AddStringToObject(root, "version_idf", esp_get_idf_version()); - cJSON_AddNumberToObject(root, "uptime", uptime_sec); - cJSON_AddNumberToObject(root, "build_date", BUILD_DATE); + cJSON_AddNumberToObject(root, ezlopi_uptime_str, uptime_sec); + cJSON_AddNumberToObject(root, ezlopi_build_date_str, BUILD_DATE); // cJSON_AddNumberToObject(root, "boot_count", ezlopi_system_info_get_boot_count()); // cJSON_AddNumberToObject(root, "boot_reason", esp_reset_reason()); - cJSON_AddBoolToObject(root, "provisioned_status", ezlopi_factory_info_v3_get_provisioning_status()); - cJSON_AddStringToObject(root, "mac", ezlopi_factory_info_v3_get_ezlopi_mac()); + cJSON_AddBoolToObject(root, ezlopi_provisioned_status_str, ezlopi_factory_info_v3_get_provisioning_status()); + cJSON_AddStringToObject(root, ezlopi_mac_str, ezlopi_factory_info_v3_get_ezlopi_mac()); - cJSON_AddStringToObject(root, "ezlopi_device_type", ezlopi_factory_info_v3_get_device_type()); - __add_factory_info_to_root(root, "model", ezlopi_factory_info_v3_get_model()); - __add_factory_info_to_root(root, "device_name", ezlopi_factory_info_v3_get_name()); - __add_factory_info_to_root(root, "brand", ezlopi_factory_info_v3_get_brand()); - __add_factory_info_to_root(root, "manufacturer", ezlopi_factory_info_v3_get_manufacturer()); - cJSON_AddNumberToObject(root, "serial", ezlopi_factory_info_v3_get_id()); + cJSON_AddStringToObject(root, ezlopi_ezlopi_device_type_str, ezlopi_factory_info_v3_get_device_type()); + __add_factory_info_to_root(root, ezlopi_model_str, ezlopi_factory_info_v3_get_model()); + __add_factory_info_to_root(root, ezlopi_device_name_str, ezlopi_factory_info_v3_get_name()); + __add_factory_info_to_root(root, ezlopi_brand_str, ezlopi_factory_info_v3_get_brand()); + __add_factory_info_to_root(root, ezlopi_manufacturer_str, ezlopi_factory_info_v3_get_manufacturer()); + cJSON_AddNumberToObject(root, ezlopi_serial_str, ezlopi_factory_info_v3_get_id()); char *ssid = ezlopi_factory_info_v3_get_ssid(); if (ssid) { - cJSON_AddStringToObject(root, "wifi_ssid", (isprint(ssid[0])) ? ssid : ""); + cJSON_AddStringToObject(root, ezlopi_wifi_ssid_str, (isprint(ssid[0])) ? ssid : ezlopi__str); } - esp_netif_ip_info_t *wifi_ip_info = ezlopi_wifi_get_ip_infos(); + // esp_netif_ip_info_t *wifi_ip_info = ezlopi_wifi_get_ip_infos(); // cJSON_AddStringToObject(root, "wifi-ip", ip4addr_ntoa((const ip4_addr_t *)&wifi_ip_info->ip)); // cJSON_AddStringToObject(root, "wifi-gw", ip4addr_ntoa((const ip4_addr_t *)&wifi_ip_info->gw)); // cJSON_AddStringToObject(root, "wifi-netmask", ip4addr_ntoa((const ip4_addr_t *)&wifi_ip_info->netmask)); - cJSON_AddNumberToObject(root, "wifi-connection_status", ezlopi_wifi_got_ip()); + cJSON_AddNumberToObject(root, ezlopi_wifi_connection_status_str, ezlopi_wifi_got_ip()); // cJSON_AddStringToObject(root, "wifi-error", ezlopi_wifi_get_last_disconnect_reason()); uint8_t flag_internet_status = (EZLOPI_PING_STATUS_LIVE == ezlopi_ping_get_internet_status()) ? 1 : 0; - cJSON_AddNumberToObject(root, "internet_status", flag_internet_status); + cJSON_AddNumberToObject(root, ezlopi_internet_status_str, flag_internet_status); device_info = cJSON_Print(root); + cJSON_Delete(root); + if (device_info) { cJSON_Minify(device_info); TRACE_I("Created device info: %s", device_info); } - - cJSON_Delete(root); } return device_info; @@ -161,7 +161,7 @@ void __add_factory_info_to_root(cJSON *root, char *key, char *value) } else { - cJSON_AddStringToObject(root, key, "unknown"); + cJSON_AddStringToObject(root, key, ezlopi_unknown_str); } // free(value); } diff --git a/services/ezlopi_ble_service/ezlopi_ble_service_dynamic_config.c b/services/ezlopi_ble_service/ezlopi_ble_service_dynamic_config.c index f0011fdc8..4954adf1d 100644 --- a/services/ezlopi_ble_service/ezlopi_ble_service_dynamic_config.c +++ b/services/ezlopi_ble_service/ezlopi_ble_service_dynamic_config.c @@ -7,6 +7,7 @@ #include "mbedtls/base64.h" #include "trace.h" +#include "esp_gatt_common_api.h" #include "ezlopi_nvs.h" #include "ezlopi_wifi.h" @@ -16,8 +17,9 @@ #include "ezlopi_ble_buffer.h" #include "ezlopi_ble_profile.h" #include "ezlopi_ble_service.h" +#include "ezlopi_cjson_macros.h" #include "ezlopi_factory_info.h" -#include "esp_gatt_common_api.h" +#include "ezlopi_cloud_constants.h" #define CJ_GET_STRING(name) cJSON_GetStringValue(cJSON_GetObjectItem(root, name)) #define CJ_GET_NUMBER(name) cJSON_GetNumberValue(cJSON_GetObjectItem(root, name)) @@ -25,20 +27,12 @@ static s_gatt_service_t *g_dynamic_config_service = NULL; static s_linked_buffer_t *g_dynamic_config_linked_buffer = NULL; -static char *__dynamic_config_jsonify(void); static char *__dynamic_config_base64(void); static char *__base64_decode_dynamic_config(uint32_t total_size); static void __dynamic_config_write_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); static void __dynamic_config_read_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); -#if 0 -static void __process_dynamic_config(uint8_t *value, uint32_t len); -static void __dynamic_config_write_exec_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); -#endif - -static void __provisioning_status_read_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); - void ezlopi_ble_service_dynamic_config_init(void) { esp_bt_uuid_t uuid; @@ -56,87 +50,6 @@ void ezlopi_ble_service_dynamic_config_init(void) ezlopi_ble_gatt_add_characteristic(g_dynamic_config_service, &uuid, permission, properties, __dynamic_config_read_func, __dynamic_config_write_func, NULL); // reliable-write is not implemented for now } -static char *__provisioning_status_jsonify(void) -{ - char *prov_status_jstr = NULL; - cJSON *root = cJSON_CreateObject(); - if (root) - { - uint32_t prov_stat = ezlopi_nvs_get_provisioning_status(); - if (1 == prov_stat) - { - cJSON_AddNumberToObject(root, "version", ezlopi_factory_info_v3_get_version()); - cJSON_AddNumberToObject(root, "status", prov_stat); - } - else - { - cJSON_AddNumberToObject(root, "version", 0); - cJSON_AddNumberToObject(root, "status", 0); - } - - char tmp_buffer[32]; - snprintf(tmp_buffer, sizeof(tmp_buffer), "%08x", ezlopi_nvs_config_info_version_number_get()); - cJSON_AddStringToObject(root, "config_id", tmp_buffer); - cJSON_AddNumberToObject(root, "config_time", ezlopi_nvs_config_info_update_time_get()); - - prov_status_jstr = cJSON_Print(root); - if (prov_status_jstr) - { - cJSON_Minify(prov_status_jstr); - } - - cJSON_Delete(root); - } - - return prov_status_jstr; -} - -static void __provisioning_status_read_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param) -{ - if (value) - { - static char *prov_status_jstr; - if (NULL == prov_status_jstr) - { - prov_status_jstr = __provisioning_status_jsonify(); - } - - if (NULL != prov_status_jstr) - { - uint32_t total_data_len = strlen(prov_status_jstr); - uint32_t max_data_buffer_size = ezlopi_ble_gatt_get_max_data_size(); - uint32_t copy_size = ((total_data_len - param->read.offset) < max_data_buffer_size) ? (total_data_len - param->read.offset) : max_data_buffer_size; - - if ((0 != total_data_len) && (total_data_len > param->read.offset)) - { - strncpy((char *)value->value, prov_status_jstr + param->read.offset, copy_size); - value->len = copy_size; - } - else - { - value->len = 1; - value->value[0] = 0; // Read 0 if the device not provisioned yet. - } - - if ((param->read.offset + copy_size) >= total_data_len) - { - free(prov_status_jstr); - prov_status_jstr = NULL; - } - } - else - { - TRACE_E("Unable to create json string"); - value->len = 1; - value->value[0] = 0; // Read 0 if the device not provisioned yet. - } - } - else - { - TRACE_E("VALUE IS NULL"); - } -} - static void __dynamic_config_write_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param) { TRACE_D("Write function called!"); @@ -161,9 +74,9 @@ static void __dynamic_config_write_func(esp_gatt_value_t *value, esp_ble_gatts_c if (root) { - uint32_t len = CJ_GET_NUMBER("len"); - uint32_t tot_len = CJ_GET_NUMBER("total_len"); - uint32_t sequence = CJ_GET_NUMBER("sequence"); + uint32_t len = CJ_GET_NUMBER(ezlopi_len_str); + uint32_t tot_len = CJ_GET_NUMBER(ezlopi_total_len_str); + uint32_t sequence = CJ_GET_NUMBER(ezlopi_sequence_str); TRACE_D("Len: %d", len); TRACE_D("tot_len: %d", tot_len); @@ -189,15 +102,17 @@ static void __dynamic_config_write_func(esp_gatt_value_t *value, esp_ble_gatts_c if (cj_config) { char *user_id = NULL; - CJSON_GET_VALUE_STRING(cj_config, "user_id", user_id); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_user_id_str, user_id); if (user_id && (BLE_AUTH_SUCCESS == ezlopi_ble_auth_check_user_id(user_id))) { - cJSON_DeleteItemFromObject(cj_config, "user_id"); + cJSON_DeleteItemFromObject(cj_config, ezlopi_user_id_str); + time_t now; + time(&now); + cJSON_AddNumberToObject(cj_config, ezlopi_coinfig_time_str, now); char *save_config_str = cJSON_Print(cj_config); if (save_config_str) { - TRACE_D("here"); ezlopi_factory_info_v2_set_ezlopi_config(save_config_str); free(save_config_str); } @@ -280,10 +195,10 @@ static void __dynamic_config_read_func(esp_gatt_value_t *value, esp_ble_gatts_cb static char data_buffer[400 + 1]; snprintf(data_buffer, sizeof(data_buffer), "%.*s", copy_size, g_dynamic_config_base64 + (g_dynamic_config_sequence_no * 400)); - cJSON_AddNumberToObject(cj_response, "len", copy_size); - cJSON_AddNumberToObject(cj_response, "total_len", total_data_len); - cJSON_AddNumberToObject(cj_response, "sequence", g_dynamic_config_sequence_no); - cJSON_AddStringToObject(cj_response, "data", data_buffer); + cJSON_AddNumberToObject(cj_response, ezlopi_len_str, copy_size); + cJSON_AddNumberToObject(cj_response, ezlopi_total_len_str, total_data_len); + cJSON_AddNumberToObject(cj_response, ezlopi_sequence_str, g_dynamic_config_sequence_no); + cJSON_AddStringToObject(cj_response, ezlopi_data_str, data_buffer); char *send_data = cJSON_Print(cj_response); if (send_data) @@ -368,67 +283,6 @@ static void __dynamic_config_read_func(esp_gatt_value_t *value, esp_ble_gatts_cb } } -#if 0 -static void __dynamic_config_write_exec_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param) -{ - if (g_dynamic_config_linked_buffer) - { - TRACE_D("Write execute function called."); - ezlopi_ble_buffer_accumulate_to_start(g_dynamic_config_linked_buffer); - __process_dynamic_config(g_dynamic_config_linked_buffer->buffer, g_dynamic_config_linked_buffer->len); - ezlopi_ble_buffer_free_buffer(g_dynamic_config_linked_buffer); - g_dynamic_config_linked_buffer = NULL; - } -} - -static void __process_dynamic_config(uint8_t *value, uint32_t len) -{ - if ((NULL != value) && (len > 0)) - { - cJSON *cj_config = cJSON_Parse((const char *)value); - if (cj_config) - { - char *user_id = NULL; - CJSON_GET_VALUE_STRING(cj_config, "user_id", user_id); - - if (user_id && (BLE_AUTH_SUCCESS == ezlopi_ble_auth_check_user_id(user_id))) - { - cJSON *cj_device_config = cJSON_GetObjectItem(cj_config, "device_config"); - if (cj_device_config) - { - cJSON_DeleteItemFromObject(cj_config, "cmd"); - cJSON_AddNumberToObject(cj_device_config, "cmd", 3); - char *tmp_str = cJSON_Print(cj_device_config); - if (tmp_str) - { - TRACE_D("device_config: %s", tmp_str); - ezlopi_factory_info_v2_set_ezlopi_config(tmp_str); - free(tmp_str); - } - } - } - else - { - TRACE_E("User varification failed!"); - - char *curr_user_id = ezlopi_nvs_read_user_id_str(); - if (curr_user_id) - { - TRACE_D("current user: %s", curr_user_id); - free(curr_user_id); - } - } - - cJSON_Delete(cj_config); - } - else - { - TRACE_E("Invalid json packet received!"); - } - } -} -#endif - static char *__base64_decode_dynamic_config(uint32_t total_size) { char *decoded_config_json = NULL; @@ -445,10 +299,10 @@ static char *__base64_decode_dynamic_config(uint32_t total_size) cJSON *root = cJSON_ParseWithLength((const char *)tmp_prov_buffer->buffer, tmp_prov_buffer->len); if (root) { - uint32_t len = CJ_GET_NUMBER("len"); - // uint32_t tot_len = CJ_GET_NUMBER("total_len"); - // uint32_t sequence = CJ_GET_NUMBER("sequence"); - char *data = CJ_GET_STRING("data"); + uint32_t len = CJ_GET_NUMBER(ezlopi_len_str); + // uint32_t tot_len = CJ_GET_NUMBER(ezlopi_total_len_str); + // uint32_t sequence = CJ_GET_NUMBER(ezlopi_sequence_str); + char *data = CJ_GET_STRING(ezlopi_data_str); if (data) { memcpy(base64_buffer + pos, data, len); @@ -489,35 +343,6 @@ static char *__base64_decode_dynamic_config(uint32_t total_size) return decoded_config_json; } -#if 0 -static char *__dynamic_config_jsonify(void) -{ - char *str_json_prov_info = NULL; - - char *device_config = ezlopi_factory_info_v2_get_ezlopi_config(); // don't free this, it is being used by other modules as well - - if (device_config) - { - TRACE_D("device-config: %s", device_config); - cJSON *cj_device_config = cJSON_Parse(device_config); - - if (cj_device_config) - { - cJSON_DeleteItemFromObject(cj_device_config, "cmd"); - cJSON_AddNumberToObject(cj_device_config, "config_time", ezlopi_nvs_config_info_update_time_get()); - cJSON_AddNumberToObject(cj_device_config, "_") - str_json_prov_info = cJSON_Print(cj_device_config); - if (str_json_prov_info) - { - cJSON_Minify(str_json_prov_info); - } - } - } - - return str_json_prov_info; -} -#endif - static char *__dynamic_config_base64(void) { const uint32_t base64_data_len = 4096; @@ -525,12 +350,15 @@ static char *__dynamic_config_base64(void) if (base64_data) { uint32_t out_put_len = 0; - char *str_provisioning_data = ezlopi_factory_info_v3_get_ezlopi_config(); // do not free 'str_provisioning_data', it is used by other modules - if (str_provisioning_data) + char *str_ezlopi_config = ezlopi_factory_info_v3_get_ezlopi_config(); // do not free 'str_provisioning_data', it is used by other modules + if (str_ezlopi_config) { - TRACE_D("str_provisioning_data[len: %d]: %s", strlen(str_provisioning_data), str_provisioning_data); + TRACE_D("str_ezlopi_config[len: %d]: %s", strlen(str_ezlopi_config), str_ezlopi_config); + + int ret = mbedtls_base64_encode((unsigned char *)base64_data, base64_data_len, &out_put_len, + (const unsigned char *)str_ezlopi_config, strlen(str_ezlopi_config)); - int ret = mbedtls_base64_encode((unsigned char *)base64_data, base64_data_len, &out_put_len, (const unsigned char *)str_provisioning_data, strlen(str_provisioning_data)); + ezlopi_factory_info_v3_free(str_ezlopi_config); TRACE_D("'mbedtls_base64_encode' returned: %04x", ret); } diff --git a/services/ezlopi_ble_service/ezlopi_ble_service_provisioning.c b/services/ezlopi_ble_service/ezlopi_ble_service_provisioning.c index f819953f1..857186b9b 100644 --- a/services/ezlopi_ble_service/ezlopi_ble_service_provisioning.c +++ b/services/ezlopi_ble_service/ezlopi_ble_service_provisioning.c @@ -8,15 +8,18 @@ #include "trace.h" -#include "ezlopi_wifi.h" #include "ezlopi_nvs.h" -#include "ezlopi_ble_buffer.h" +#include "ezlopi_wifi.h" +#include "ezlopi_devices.h" #include "ezlopi_ble_gatt.h" +#include "ezlopi_ble_auth.h" +#include "ezlopi_ble_buffer.h" #include "ezlopi_ble_profile.h" #include "ezlopi_ble_service.h" #include "ezlopi_factory_info.h" -#include "ezlopi_devices.h" -#include "ezlopi_ble_auth.h" +#include "ezlopi_cjson_macros.h" +#include "ezlopi_cloud_constants.h" + #include "esp_gatt_common_api.h" #define CJ_GET_STRING(name) cJSON_GetStringValue(cJSON_GetObjectItem(root, name)) @@ -71,27 +74,27 @@ static char *__provisioning_status_jsonify(void) uint32_t prov_stat = ezlopi_nvs_get_provisioning_status(); if (1 == prov_stat) { - cJSON_AddNumberToObject(root, "version", ezlopi_factory_info_v3_get_version()); - cJSON_AddNumberToObject(root, "status", prov_stat); + cJSON_AddNumberToObject(root, ezlopi_version_str, ezlopi_factory_info_v3_get_version()); + cJSON_AddNumberToObject(root, ezlopi_status_str, prov_stat); } else { - cJSON_AddNumberToObject(root, "version", 0); - cJSON_AddNumberToObject(root, "status", 0); + cJSON_AddNumberToObject(root, ezlopi_version_str, 0); + cJSON_AddNumberToObject(root, ezlopi_status_str, 0); } char tmp_buffer[32]; snprintf(tmp_buffer, sizeof(tmp_buffer), "%08x", ezlopi_nvs_config_info_version_number_get()); - cJSON_AddStringToObject(root, "config_id", tmp_buffer); - cJSON_AddNumberToObject(root, "config_time", ezlopi_nvs_config_info_update_time_get()); + cJSON_AddStringToObject(root, ezlopi_config_id_str, tmp_buffer); + cJSON_AddNumberToObject(root, ezlopi_config_time_str, ezlopi_nvs_config_info_update_time_get()); prov_status_jstr = cJSON_Print(root); + cJSON_Delete(root); + if (prov_status_jstr) { cJSON_Minify(prov_status_jstr); } - - cJSON_Delete(root); } return prov_status_jstr; @@ -167,9 +170,9 @@ static void __provisioning_info_write_func(esp_gatt_value_t *value, esp_ble_gatt if (root) { - uint32_t len = CJ_GET_NUMBER("len"); - uint32_t tot_len = CJ_GET_NUMBER("total_len"); - uint32_t sequence = CJ_GET_NUMBER("sequence"); + uint32_t len = CJ_GET_NUMBER(ezlopi_len_str); + uint32_t tot_len = CJ_GET_NUMBER(ezlopi_total_len_str); + uint32_t sequence = CJ_GET_NUMBER(ezlopi_sequence_str); TRACE_D("Len: %d", len); TRACE_D("tot_len: %d", tot_len); @@ -186,7 +189,7 @@ static void __provisioning_info_write_func(esp_gatt_value_t *value, esp_ble_gatt if (cj_config) { char *user_id = NULL; - CJSON_GET_VALUE_STRING(cj_config, "user_id", user_id); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_user_id_str, user_id); if (user_id && (BLE_AUTH_SUCCESS == ezlopi_ble_auth_check_user_id(user_id))) { @@ -196,18 +199,20 @@ static void __provisioning_info_write_func(esp_gatt_value_t *value, esp_ble_gatt // ezlopi_config_basic->user_id = user_id; memset(ezlopi_config_basic, 0, sizeof(s_basic_factory_info_t)); - CJSON_GET_VALUE_STRING(cj_config, "device_name", ezlopi_config_basic->device_name); - CJSON_GET_VALUE_STRING(cj_config, "manufacturer_name", ezlopi_config_basic->manufacturer); - CJSON_GET_VALUE_STRING(cj_config, "brand", ezlopi_config_basic->brand); - CJSON_GET_VALUE_STRING(cj_config, "model_number", ezlopi_config_basic->model_number); - CJSON_GET_VALUE_DOUBLE(cj_config, "serial", ezlopi_config_basic->id); - CJSON_GET_VALUE_STRING(cj_config, "uuid", ezlopi_config_basic->device_uuid); - CJSON_GET_VALUE_STRING(cj_config, "uuid_provisioning", ezlopi_config_basic->prov_uuid); - CJSON_GET_VALUE_STRING(cj_config, "mac", ezlopi_config_basic->device_mac); - CJSON_GET_VALUE_STRING(cj_config, "provision_server", ezlopi_config_basic->provision_server); - CJSON_GET_VALUE_STRING(cj_config, "cloud_server", ezlopi_config_basic->cloud_server); - CJSON_GET_VALUE_STRING(cj_config, "provision_token", ezlopi_config_basic->provision_token); - CJSON_GET_VALUE_STRING(cj_config, "device_type_ezlopi", ezlopi_config_basic->device_type); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_device_name_str, ezlopi_config_basic->device_name); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_manufacturer_name_str, ezlopi_config_basic->manufacturer); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_brand_str, ezlopi_config_basic->brand); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_model_number_str, ezlopi_config_basic->model_number); + CJSON_GET_VALUE_DOUBLE(cj_config, ezlopi_serial_str, ezlopi_config_basic->id); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_uuid_str, ezlopi_config_basic->device_uuid); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_uuid_provisioning_str, ezlopi_config_basic->prov_uuid); + + CJSON_GET_VALUE_STRING(cj_config, ezlopi_mac_str, ezlopi_config_basic->device_mac); + + CJSON_GET_VALUE_STRING(cj_config, ezlopi_provision_server_str, ezlopi_config_basic->provision_server); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_cloud_server_str, ezlopi_config_basic->cloud_server); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_provision_token_str, ezlopi_config_basic->provision_token); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_device_type_ezlopi_str, ezlopi_config_basic->device_type); ezlopi_factory_info_v3_set_basic(ezlopi_config_basic); uint32_t version_no = ezlopi_nvs_config_info_version_number_get() + 1; @@ -215,6 +220,7 @@ static void __provisioning_info_write_func(esp_gatt_value_t *value, esp_ble_gatt time_t now; time(&now); + TRACE_D("time now{size: %u}: %lu", sizeof(time_t), now); ezlopi_nvs_config_info_update_time_set(now); free(ezlopi_config_basic); @@ -224,10 +230,10 @@ static void __provisioning_info_write_func(esp_gatt_value_t *value, esp_ble_gatt char *ssl_shared_key = NULL; char *ssl_private_key = NULL; - CJSON_GET_VALUE_STRING(cj_config, "ssl_private_key", ssl_private_key); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_ssl_private_key_str, ssl_private_key); // CJSON_GET_VALUE_STRING(cj_config, "ssl_public_key", ssl_public_key); - CJSON_GET_VALUE_STRING(cj_config, "ssl_shared_key", ssl_shared_key); - CJSON_GET_VALUE_STRING(cj_config, "signing_ca_certificate", ca_certs); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_ssl_shared_key_str, ssl_shared_key); + CJSON_GET_VALUE_STRING(cj_config, ezlopi_signing_ca_certificate_str, ca_certs); ezlopi_factory_info_v3_set_ca_cert(ca_certs); ezlopi_factory_info_v3_set_ssl_shared_key(ssl_shared_key); @@ -313,10 +319,10 @@ static void __provisioning_info_read_func(esp_gatt_value_t *value, esp_ble_gatts static char data_buffer[400 + 1]; snprintf(data_buffer, sizeof(data_buffer), "%.*s", copy_size, g_provisioning_info_base64 + (g_provisioning_sequence_no * 400)); - cJSON_AddNumberToObject(cj_response, "len", copy_size); - cJSON_AddNumberToObject(cj_response, "total_len", total_data_len); - cJSON_AddNumberToObject(cj_response, "sequence", g_provisioning_sequence_no); - cJSON_AddStringToObject(cj_response, "data", data_buffer); + cJSON_AddNumberToObject(cj_response, ezlopi_len_str, copy_size); + cJSON_AddNumberToObject(cj_response, ezlopi_total_len_str, total_data_len); + cJSON_AddNumberToObject(cj_response, ezlopi_sequence_str, g_provisioning_sequence_no); + cJSON_AddStringToObject(cj_response, ezlopi_data_str, data_buffer); char *send_data = cJSON_Print(cj_response); if (send_data) @@ -422,34 +428,34 @@ static void __process_provisioning_info(uint8_t *value, uint32_t len) if (root) { TRACE_D("value = %s", value); - char *user_id = cJSON_GetObjectItemCaseSensitive(root, "user_id")->valuestring; - char *device_name = cJSON_GetObjectItemCaseSensitive(root, "device_name")->valuestring; - char *brand = cJSON_GetObjectItemCaseSensitive(root, "brand")->valuestring; - char *manufacturer_name = cJSON_GetObjectItemCaseSensitive(root, "manufacturer_name")->valuestring; - char *model_number = cJSON_GetObjectItemCaseSensitive(root, "model_number")->valuestring; - char *uuid = cJSON_GetObjectItemCaseSensitive(root, "uuid")->valuestring; - char *uuid_provisioning = cJSON_GetObjectItemCaseSensitive(root, "uuid_provisioning")->valuestring; - double serial = cJSON_GetObjectItemCaseSensitive(root, "serial")->valuedouble; - char *cloud_server = cJSON_GetObjectItemCaseSensitive(root, "cloud_server")->valuestring; - char *ssl_private_key = cJSON_GetObjectItemCaseSensitive(root, "ssl_private_key")->valuestring; + char *user_id = cJSON_GetObjectItemCaseSensitive(root, ezlopi_user_id_str)->valuestring; + char *device_name = cJSON_GetObjectItemCaseSensitive(root, ezlopi_device_name_str)->valuestring; + char *brand = cJSON_GetObjectItemCaseSensitive(root, ezlopi_brand_str)->valuestring; + char *manufacturer_name = cJSON_GetObjectItemCaseSensitive(root, ezlopi_manufacturer_name_str)->valuestring; + char *model_number = cJSON_GetObjectItemCaseSensitive(root, ezlopi_model_number_str)->valuestring; + char *uuid = cJSON_GetObjectItemCaseSensitive(root, ezlopi_uuid_str)->valuestring; + char *uuid_provisioning = cJSON_GetObjectItemCaseSensitive(root, ezlopi_uuid_provisioning_str)->valuestring; + double serial = cJSON_GetObjectItemCaseSensitive(root, ezlopi_serial_str)->valuedouble; + char *cloud_server = cJSON_GetObjectItemCaseSensitive(root, ezlopi_cloud_server_str)->valuestring; + char *ssl_private_key = cJSON_GetObjectItemCaseSensitive(root, ezlopi_ssl_private_key_str)->valuestring; char *ssl_public_key = cJSON_GetObjectItemCaseSensitive(root, "ssl_public_key")->valuestring; - char *ca_cert = cJSON_GetObjectItemCaseSensitive(root, "ca_cert")->valuestring; - char *device_type_ezlopi = cJSON_GetObjectItemCaseSensitive(root, "device_type_ezlopi")->valuestring; + char *ca_cert = cJSON_GetObjectItemCaseSensitive(root, ezlopi_ca_cert_str)->valuestring; + char *device_type_ezlopi = cJSON_GetObjectItemCaseSensitive(root, ezlopi_device_type_ezlopi_str)->valuestring; TRACE_D("************************* BLE-PROVISIONING *************************"); - TRACE_D("user_id: %s", user_id ? user_id : ""); - TRACE_D("device_name: %s", device_name ? device_name : ""); - TRACE_D("brand: %s", brand ? brand : ""); - TRACE_D("manufacturer_name: %s", manufacturer_name ? manufacturer_name : ""); - TRACE_D("model_number: %s", model_number ? model_number : ""); - TRACE_D("uuid: %s", uuid ? uuid : ""); - TRACE_D("uuid_provisioning: %s", uuid_provisioning ? uuid_provisioning : ""); + TRACE_D("user_id: %s", user_id ? user_id : ezlopi__str); + TRACE_D("device_name: %s", device_name ? device_name : ezlopi__str); + TRACE_D("brand: %s", brand ? brand : ezlopi__str); + TRACE_D("manufacturer_name: %s", manufacturer_name ? manufacturer_name : ezlopi__str); + TRACE_D("model_number: %s", model_number ? model_number : ezlopi__str); + TRACE_D("uuid: %s", uuid ? uuid : ezlopi__str); + TRACE_D("uuid_provisioning: %s", uuid_provisioning ? uuid_provisioning : ezlopi__str); TRACE_D("serial: %f", serial); - TRACE_D("cloud_server: %s", cloud_server ? cloud_server : ""); - TRACE_D("ssl_private_key: %s", ssl_private_key ? ssl_private_key : ""); - TRACE_D("ssl_public_key: %s", ssl_public_key ? ssl_public_key : ""); - TRACE_D("ca_cert: %s", ca_cert ? ca_cert : ""); - TRACE_D("device_type_ezlopi: %s", device_type_ezlopi ? device_type_ezlopi : ""); + TRACE_D("cloud_server: %s", cloud_server ? cloud_server : ezlopi__str); + TRACE_D("ssl_private_key: %s", ssl_private_key ? ssl_private_key : ezlopi__str); + TRACE_D("ssl_public_key: %s", ssl_public_key ? ssl_public_key : ezlopi__str); + TRACE_D("ca_cert: %s", ca_cert ? ca_cert : ezlopi__str); + TRACE_D("device_type_ezlopi: %s", device_type_ezlopi ? device_type_ezlopi : ezlopi__str); TRACE_D("********************************************************************"); cJSON_Delete(root); @@ -478,10 +484,10 @@ static char *__base64_decode_provisioning_info(uint32_t total_size) cJSON *root = cJSON_ParseWithLength((const char *)tmp_prov_buffer->buffer, tmp_prov_buffer->len); if (root) { - uint32_t len = CJ_GET_NUMBER("len"); - // uint32_t tot_len = CJ_GET_NUMBER("total_len"); - // uint32_t sequence = CJ_GET_NUMBER("sequence"); - char *data = CJ_GET_STRING("data"); + uint32_t len = CJ_GET_NUMBER(ezlopi_len_str); + // uint32_t tot_len = CJ_GET_NUMBER(ezlopi_total_len_str); + // uint32_t sequence = CJ_GET_NUMBER(ezlopi_sequence_str); + char *data = CJ_GET_STRING(ezlopi_data_str); if (data) { memcpy(base64_buffer + pos, data, len); @@ -542,21 +548,20 @@ static char *__provisioning_info_jsonify(void) char *ca_cert = ezlopi_factory_info_v3_get_ca_certificate(); snprintf(tmp_buffer, sizeof(tmp_buffer), "%08x", ezlopi_nvs_config_info_version_number_get()); - cJSON_AddStringToObject(cj_prov_info, "config_id", tmp_buffer); - cJSON_AddNumberToObject(cj_prov_info, "config_time", ezlopi_nvs_config_info_update_time_get()); - cJSON_AddStringToObject(cj_prov_info, "device_name", device_name); - cJSON_AddStringToObject(cj_prov_info, "brand", brand); - cJSON_AddStringToObject(cj_prov_info, "manufacturer_name", manufacturer_name); - cJSON_AddStringToObject(cj_prov_info, "model_number", model_number); - cJSON_AddStringToObject(cj_prov_info, "uuid", uuid); - cJSON_AddStringToObject(cj_prov_info, "uuid_provisioning", uuid_provisioning); - cJSON_AddNumberToObject(cj_prov_info, "serial", ezlopi_factory_info_v3_get_id()); - cJSON_AddStringToObject(cj_prov_info, "cloud_server", cloud_server); - cJSON_AddStringToObject(cj_prov_info, "ssl_private_key", ssl_private_key); - cJSON_AddStringToObject(cj_prov_info, "ssl_shared_key", ssl_shared_key); - // cJSON_AddStringToObject(cj_prov_info, "ssl_public_key"); // Not needed by Ezlo_Pi stack for now - cJSON_AddStringToObject(cj_prov_info, "ca_cert", ca_cert); - cJSON_AddStringToObject(cj_prov_info, "device_type_ezlopi", ezlopi_factory_info_v3_get_device_type()); + cJSON_AddStringToObject(cj_prov_info, ezlopi_config_id_str, tmp_buffer); + cJSON_AddNumberToObject(cj_prov_info, ezlopi_config_time_str, ezlopi_nvs_config_info_update_time_get()); + cJSON_AddStringToObject(cj_prov_info, ezlopi_device_name_str, device_name); + cJSON_AddStringToObject(cj_prov_info, ezlopi_brand_str, brand); + cJSON_AddStringToObject(cj_prov_info, ezlopi_manufacturer_name_str, manufacturer_name); + cJSON_AddStringToObject(cj_prov_info, ezlopi_model_number_str, model_number); + cJSON_AddStringToObject(cj_prov_info, ezlopi_uuid_str, uuid); + cJSON_AddStringToObject(cj_prov_info, ezlopi_uuid_provisioning_str, uuid_provisioning); + cJSON_AddNumberToObject(cj_prov_info, ezlopi_serial_str, ezlopi_factory_info_v3_get_id()); + cJSON_AddStringToObject(cj_prov_info, ezlopi_cloud_server_str, cloud_server); + cJSON_AddStringToObject(cj_prov_info, ezlopi_ssl_private_key_str, ssl_private_key); + cJSON_AddStringToObject(cj_prov_info, ezlopi_ssl_shared_key_str, ssl_shared_key); + cJSON_AddStringToObject(cj_prov_info, ezlopi_ca_cert_str, ca_cert); + cJSON_AddStringToObject(cj_prov_info, ezlopi_device_type_ezlopi_str, ezlopi_factory_info_v3_get_device_type()); ezlopi_factory_info_v3_free(device_name); ezlopi_factory_info_v3_free(brand); @@ -570,12 +575,12 @@ static char *__provisioning_info_jsonify(void) ezlopi_factory_info_v3_free(ca_cert); str_json_prov_info = cJSON_Print(cj_prov_info); + cJSON_Delete(cj_prov_info); + if (str_json_prov_info) { cJSON_Minify(str_json_prov_info); } - - cJSON_Delete(cj_prov_info); } return str_json_prov_info; diff --git a/services/ezlopi_ble_service/ezlopi_ble_service_security.c b/services/ezlopi_ble_service/ezlopi_ble_service_security.c index 694b862e9..35fa3aac6 100644 --- a/services/ezlopi_ble_service/ezlopi_ble_service_security.c +++ b/services/ezlopi_ble_service/ezlopi_ble_service_security.c @@ -4,26 +4,46 @@ #include "cJSON.h" #include "trace.h" -#include "ezlopi_wifi.h" #include "ezlopi_nvs.h" +#include "ezlopi_wifi.h" +#include "ezlopi_reboot.h" #include "ezlopi_ble_gap.h" #include "ezlopi_ble_gatt.h" +#include "ezlopi_ble_buffer.h" +#include "ezlopi_ble_service.h" #include "ezlopi_ble_profile.h" #include "ezlopi_factory_info.h" -#include "ezlopi_nvs.h" +#include "ezlopi_cloud_constants.h" -#include "ezlopi_ble_service.h" -#include "ezlopi_ble_buffer.h" +typedef enum e_ble_security_commands +{ + BLE_CMD_UNDEFINED = 0, // 0 + BLE_CMD_REBOOT, // 1 + BLE_CMD_SOFTRESET, // 2 + BLE_CMD_HARDREST, // 3 + BLE_CMD_FACTORY_RESET, // 4 + BLE_CMD_AUTHENTICATE, // 5 + BLE_CMD_MAX, +} e_ble_security_commands_t; -s_gatt_service_t *security_service = NULL; +#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) +static uint32_t start_tick = 0; +static uint32_t authenticated_flag = 0; +#endif +static s_gatt_service_t *security_service = NULL; #if (1 == EZLOPI_BLE_ENALBE_PASSKEY) s_gatt_char_t *passkey_characterstic = NULL; static void passkey_write_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); #endif -s_gatt_char_t *factory_reset_characterstic = NULL; +static s_gatt_char_t *factory_reset_characterstic = NULL; +static void __process_auth_command(cJSON *root); +static void __process_soft_reset_command(void); +static void __process_hard_reset_command(void); +static void __process_factory_reset_command(void); + static void factory_reset_write_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); #define CJ_GET_NUMBER(name) cJSON_GetNumberValue(cJSON_GetObjectItem(root, name)) @@ -72,82 +92,112 @@ static void passkey_write_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t static void factory_reset_write_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param) { - if (param && param->write.len && param->write.value) { - cJSON *root = cJSON_ParseWithLength((const char *)param->write.value, param->write.len); if (root) { -#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) - static uint32_t authenticated_flag; - static uint32_t start_tick; -#endif - - uint32_t cmd = CJ_GET_NUMBER("cmd"); + uint32_t cmd = CJ_GET_NUMBER(ezlopi_cmd_str); TRACE_D("cmd: %d", cmd); - if (2 == cmd) // factory reset command + + switch (cmd) { -#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) - uint32_t current_tick = xTaskGetTickCount(); - if ((current_tick - start_tick) < (30 * 1000 / portTICK_RATE_MS) && (1 == authenticated_flag)) // once authenticated, valid for 30 seconds only - { -#endif - int ret = ezlopi_factory_info_v3_factory_reset(); - if (ret) - { - TRACE_I("FLASH RESET WAS DONE SUCCESSFULLY"); - } - - ret = ezlopi_nvs_factory_reset(); - if (ret) - { - TRACE_I("NVS-RESET WAS DONE SUCCESSFULLY"); - } - - TRACE_B("factory reset done, rebooting now ............................................."); - vTaskDelay(2000 / portTICK_RATE_MS); - esp_restart(); -#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) - } - else - { - authenticated_flag = 0; - TRACE_W("Not authenticated for factory-reset!"); - } -#endif + case BLE_CMD_REBOOT: + { + ezlopi_reboot(); + // esp_restart(); + break; } -#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) - else if (1 == cmd) // authentication request for soft-factory-reset + case BLE_CMD_FACTORY_RESET: // factory reset command { - - uint32_t passkey = CJ_GET_NUMBER("passkey"); - uint32_t original_passkey = 0; - ezlopi_nvs_read_ble_passkey(&original_passkey); - - TRACE_D("Old passkey: %u, current_passkey: %u", original_passkey, passkey); - - if (passkey == original_passkey) - { - authenticated_flag = 1; - TRACE_W("Authenticated!"); - start_tick = xTaskGetTickCount(); - } - else - { - authenticated_flag = 0; - TRACE_W("Not authenticated!"); - } + __process_factory_reset_command(); + break; } -#endif - else + case BLE_CMD_HARDREST: { - TRACE_W("Command not valid: [cmd: %u].", cmd); + __process_hard_reset_command(); + break; + } + case BLE_CMD_AUTHENTICATE: // authentication request for soft-factory-reset + { + __process_auth_command(root); + break; + } + default: + { + TRACE_W("Command not valid -> {cmd: %u}.", cmd); + break; + } } cJSON_free(root); } } +} + +static void __process_hard_reset_command(void) +{ + +#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) + uint32_t current_tick = xTaskGetTickCount(); + if ((1 == authenticated_flag) && (current_tick - start_tick) < (30 * 1000 / portTICK_RATE_MS)) // once authenticated, valid for 30 seconds only + { +#endif + int ret = ezlopi_factory_info_v3_factory_reset(); + if (ret) + { + TRACE_I("FLASH RESET WAS DONE SUCCESSFULLY"); + } + + ret = ezlopi_nvs_factory_reset(); + if (ret) + { + TRACE_I("NVS-RESET WAS DONE SUCCESSFULLY"); + } + + TRACE_B("factory reset done, rebooting now ............................................."); + vTaskDelay(2000 / portTICK_RATE_MS); + // esp_restart(); + ezlopi_reboot(); +#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) + } + else + { + authenticated_flag = 0; + TRACE_W("Not authenticated for factory-reset!"); + } +#endif +} + +static void __process_soft_reset_command(void) +{ +} + +static void __process_factory_reset_command(void) +{ +} + +static void __process_auth_command(cJSON *root) +{ +#if (1 == EZLOPI_BLE_ENALBE_PASSKEY) + uint32_t passkey = CJ_GET_NUMBER("passkey"); + uint32_t original_passkey = 0; + ezlopi_nvs_read_ble_passkey(&original_passkey); + + TRACE_D("Old passkey: %u, current_passkey: %u", original_passkey, passkey); + + if (passkey == original_passkey) + { + authenticated_flag = 1; + TRACE_W("Authenticated!"); + start_tick = xTaskGetTickCount(); + } + else + { + authenticated_flag = 0; + TRACE_W("Not authenticated!"); + } +#endif } \ No newline at end of file diff --git a/services/ezlopi_ble_service/ezlopi_ble_service_wifi.c b/services/ezlopi_ble_service/ezlopi_ble_service_wifi.c index 921ac010d..8b6ca026a 100644 --- a/services/ezlopi_ble_service/ezlopi_ble_service_wifi.c +++ b/services/ezlopi_ble_service/ezlopi_ble_service_wifi.c @@ -5,24 +5,24 @@ #include "esp_event_base.h" #include "trace.h" -#include "ezlopi_wifi.h" #include "ezlopi_nvs.h" +#include "ezlopi_wifi.h" +#include "ezlopi_ping.h" +#include "ezlopi_ble_auth.h" #include "ezlopi_ble_gatt.h" -#include "ezlopi_ble_profile.h" - -#include "ezlopi_ble_service.h" #include "ezlopi_ble_buffer.h" +#include "ezlopi_ble_service.h" +#include "ezlopi_ble_profile.h" #include "ezlopi_factory_info.h" -#include "ezlopi_ble_auth.h" -#include "ezlopi_ping.h" +#include "ezlopi_cloud_constants.h" static s_linked_buffer_t *wifi_creds_linked_buffer = NULL; static void wifi_creds_write_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); static void wifi_creds_write_exec_func(esp_gatt_value_t *value, esp_ble_gatts_cb_param_t *param); static void wifi_creds_parse_and_connect(uint8_t *value, uint32_t len); -static void wifi_event_notify_upcall(esp_event_base_t event, void *arg); +// static void wifi_event_notify_upcall(esp_event_base_t event, void *arg); static s_gatt_service_t *wifi_ble_service; @@ -84,9 +84,9 @@ static void wifi_creds_parse_and_connect(uint8_t *value, uint32_t len) cJSON *root = cJSON_Parse((const char *)value); if (root) { - cJSON *cj_user_id = cJSON_GetObjectItemCaseSensitive(root, "user_id"); - cJSON *cj_ssid = cJSON_GetObjectItemCaseSensitive(root, "wifi_ssid"); - cJSON *cj_password = cJSON_GetObjectItemCaseSensitive(root, "wifi_password"); + cJSON *cj_user_id = cJSON_GetObjectItemCaseSensitive(root, ezlopi_user_id_str); + cJSON *cj_ssid = cJSON_GetObjectItemCaseSensitive(root, ezlopi_wifi_ssid_str); + cJSON *cj_password = cJSON_GetObjectItemCaseSensitive(root, ezlopi_wifi_password_str); if (cj_user_id && cj_password && cj_ssid) { diff --git a/services/ezlopi_meshbot_service/ezlopi_meshbot_service.c b/services/ezlopi_meshbot_service/ezlopi_meshbot_service.c index 0f6898e86..8fd2b6c5a 100644 --- a/services/ezlopi_meshbot_service/ezlopi_meshbot_service.c +++ b/services/ezlopi_meshbot_service/ezlopi_meshbot_service.c @@ -5,6 +5,7 @@ #include "ezlopi_scenes_v2.h" #include "ezlopi_meshbot_service.h" +#include "ezlopi_cloud_constants.h" #include "ezlopi_scenes_status_changed.h" static void __scenes_process(void *arg); @@ -35,6 +36,43 @@ uint32_t ezlopi_meshbot_service_stop_for_scene_id(uint32_t _id) return ret; } +uint32_t ezlopi_meshobot_service_stop_scene(l_scenes_list_v2_t *scene_node) +{ + int ret = 0; + if (scene_node) + { + if (EZLOPI_SCENE_STATUS_RUNNING == scene_node->status) + { + scene_node->status = EZLOPI_SCENE_STATUS_STOP; + } + + while (EZLOPI_SCENE_STATUS_STOPPED != scene_node->status) + { + vTaskDelay(50 / portTICK_RATE_MS); + } + + ezlopi_scenes_status_change_broadcast(scene_node, scene_status_stopped_str); + + ret = 1; + } + return ret; +} + +uint32_t ezlopi_meshbot_service_start_scene(l_scenes_list_v2_t *scene_node) +{ + int ret = 0; + if (scene_node) + { + if ((EZLOPI_SCENE_STATUS_NONE == scene_node->status) || + (EZLOPI_SCENE_STATUS_STOPPED == scene_node->status)) + { + xTaskCreate(__scenes_process, scene_node->name, 2 * 2048, scene_node, 2, NULL); + ret = 1; + } + } + return ret; +} + // uint32_t ezlopi_scenes_service_pause_for_scene_id(uint32_t _id) // { // uint32_t ret = 0; @@ -97,10 +135,14 @@ void ezlopi_scenes_meshbot_init(void) l_scenes_list_v2_t *scene_node = ezlopi_scenes_get_scenes_head_v2(); while (scene_node) { - if (scene_node->enabled) + if (scene_node->enabled && scene_node->when_block && (scene_node->else_block || scene_node->then_block)) { xTaskCreate(__scenes_process, scene_node->name, 2 * 2048, scene_node, 2, NULL); } + else + { + scene_node->status = EZLOPI_SCENE_STATUS_STOPPED; + } scene_node = scene_node->next; } } diff --git a/services/ezlopi_meshbot_service/ezlopi_meshbot_service.h b/services/ezlopi_meshbot_service/ezlopi_meshbot_service.h index 300972071..79d2edc1a 100644 --- a/services/ezlopi_meshbot_service/ezlopi_meshbot_service.h +++ b/services/ezlopi_meshbot_service/ezlopi_meshbot_service.h @@ -2,9 +2,13 @@ #define __EZLOPI_SCENES_SERVICE__ #include "stdint.h" +#include "ezlopi_scenes_v2.h" void ezlopi_scenes_meshbot_init(void); uint32_t ezlopi_scenes_service_run_by_id(uint32_t _id); +uint32_t ezlopi_meshbot_service_start_scene(l_scenes_list_v2_t *scene_node); + uint32_t ezlopi_meshbot_service_stop_for_scene_id(uint32_t _id); +uint32_t ezlopi_meshobot_service_stop_scene(l_scenes_list_v2_t *scene_node); #endif // __EZLOPI_SCENES_SERVICE__ diff --git a/services/gpio_isr_service/gpio_isr_service.c b/services/gpio_isr_service/gpio_isr_service.c index 086a1f5d5..e4ca042e6 100644 --- a/services/gpio_isr_service/gpio_isr_service.c +++ b/services/gpio_isr_service/gpio_isr_service.c @@ -23,10 +23,8 @@ typedef struct s_event_arg f_interrupt_upcall_t __upcall; } s_event_arg_t; -static void IRAM_ATTR __gpio_isr_handler(void *arg); -static void gpio_isr_process(void *pv); - static void gpio_isr_process_v3(void *pv); +static void IRAM_ATTR __gpio_isr_handler(void *arg); void gpio_isr_service_init(void) { diff --git a/services/ota_service/ota_service.c b/services/ota_service/ota_service.c index dddef1f21..b1615fa2e 100644 --- a/services/ota_service/ota_service.c +++ b/services/ota_service/ota_service.c @@ -19,13 +19,13 @@ void ota_service_init(void) } static void ota_service_process(void *pv) { - ezlopi_wait_for_wifi_to_connect(portTICK_RATE_MS); + ezlopi_wait_for_wifi_to_connect(portMAX_DELAY); ezlopi_event_group_set_event(EZLOPI_EVENT_OTA); vTaskDelay(5000 / portTICK_RATE_MS); while (1) { - int ret_nma_reg = ezlopi_event_group_wait_for_event(EZLOPI_EVENT_NMA_REG, 60000 / portTICK_RATE_MS, false); + int ret_nma_reg = ezlopi_event_group_wait_for_event(EZLOPI_EVENT_NMA_REG, 60000, false); int ret_ota = ezlopi_event_group_wait_for_event(EZLOPI_EVENT_OTA, 86400 * 1000, 1); // 86400 seconds in a day (24 hrs) TRACE_D("Configuration Selection NMA Reg: %d", ret_nma_reg); TRACE_D("Configuration Selection OTA Trigger : %d", ret_ota); @@ -36,14 +36,20 @@ static void ota_service_process(void *pv) cJSON *firmware_info_request = firmware_send_firmware_query_to_nma_server(message_counter); if (NULL != firmware_info_request) { - web_provisioning_send_to_nma_websocket(firmware_info_request, TRACE_TYPE_B); + char *data_to_send = cJSON_Print(firmware_info_request); cJSON_Delete(firmware_info_request); firmware_info_request = NULL; + + if (data_to_send) + { + cJSON_Minify(data_to_send); + ret_ota = web_provisioning_send_str_data_to_nma_websocket(data_to_send, TRACE_TYPE_D); + free(data_to_send); + } } } else - { // firmware_send_firmware_query_to_nma_server(); - // send "cloud.firmware.info.get" + { vTaskDelay(1000 / portTICK_RATE_MS); } } diff --git a/services/qt_serial/CMakeLists.txt b/services/qt_serial/CMakeLists.txt index 478be1a4f..828a7a07b 100644 --- a/services/qt_serial/CMakeLists.txt +++ b/services/qt_serial/CMakeLists.txt @@ -3,4 +3,4 @@ file(GLOB_RECURSE src_files "./*.c*") idf_component_register(SRCS "qt_serial.c" "${src_files}" INCLUDE_DIRS "." - REQUIRES driver freertos nvs_flash ezlopi_wifi trace json ezlopi_nvs ezlopi_factory_info version log ezlopi_hal info ezlopi_net) \ No newline at end of file + REQUIRES driver freertos nvs_flash ezlopi_wifi trace json ezlopi_nvs ezlopi_factory_info version log ezlopi_hal info ezlopi_net ezlopi_reboot) \ No newline at end of file diff --git a/services/qt_serial/qt_serial.c b/services/qt_serial/qt_serial.c index fcc29648e..d28599f1e 100644 --- a/services/qt_serial/qt_serial.c +++ b/services/qt_serial/qt_serial.c @@ -9,27 +9,31 @@ #include "freertos/FreeRTOSConfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_system.h" -#include "esp_log.h" -#include "driver/uart.h" + +#include "nvs.h" +#include "cJSON.h" #include "string.h" +#include "esp_log.h" #include "nvs_flash.h" -#include "nvs.h" +#include "esp_netif.h" +#include "esp_system.h" +#include "driver/uart.h" #include "driver/gpio.h" -#include "cJSON.h" +#include "esp_idf_version.h" + #include "trace.h" -#include "esp_netif.h" +#include "version.h" +#include "qt_serial.h" #include "sdkconfig.h" -#include "esp_idf_version.h" #include "ezlopi_nvs.h" -#include "qt_serial.h" -#include "ezlopi_factory_info.h" -#include "version.h" #include "ezlopi_wifi.h" +#include "ezlopi_reboot.h" #include "ezlopi_system_info.h" #include "info.h" #include "ezlopi_net.h" +#include "ezlopi_factory_info.h" +#include "ezlopi_cloud_constants.h" static const int RX_BUF_SIZE = 3096; @@ -154,9 +158,9 @@ static int qt_serial_parse_rx_data(const char *data) if (root) { - if (cJSON_GetObjectItem(root, "cmd")) + if (cJSON_GetObjectItem(root, ezlopi_cmd_str)) { - uint8_t cmd_temp = cJSON_GetObjectItem(root, "cmd")->valueint; + uint8_t cmd_temp = cJSON_GetObjectItem(root, ezlopi_cmd_str)->valueint; switch (cmd_temp) { @@ -185,7 +189,8 @@ static int qt_serial_parse_rx_data(const char *data) const static char *reboot_response = "{\"cmd\":0,\"status\":1}"; qt_serial_tx_data(strlen(reboot_response), (uint8_t *)reboot_response); vTaskDelay(20); - esp_restart(); + // esp_restart(); + ezlopi_reboot(); break; } @@ -268,24 +273,24 @@ static void qt_serial_get_info() cJSON_AddStringToObject(json_chip_info, "firmware_SDK_version", esp_get_idf_version()); cJSON_AddItemToObject(get_info, "chip_info", json_chip_info); } - cJSON_AddStringToObject(get_info, "uptime", ezlopi_tick_to_time((uint32_t)(xTaskGetTickCount() / portTICK_PERIOD_MS))); - cJSON_AddNumberToObject(get_info, "build_date", BUILD_DATE); + cJSON_AddStringToObject(get_info, ezlopi_uptime_str, ezlopi_tick_to_time((uint32_t)(xTaskGetTickCount() / portTICK_PERIOD_MS))); + cJSON_AddNumberToObject(get_info, ezlopi_build_date_str, BUILD_DATE); cJSON_AddNumberToObject(get_info, "boot_count", ezlopi_system_info_get_boot_count()); cJSON_AddStringToObject(get_info, "boot_reason", ezlopi_esp_reset_reason_str(esp_reset_reason())); - cJSON_AddStringToObject(get_info, "mac", device_mac ? device_mac : ""); - cJSON_AddStringToObject(get_info, "uuid", controller_uuid ? controller_uuid : ""); - cJSON_AddStringToObject(get_info, "uuid_prov", provisioning_uuid ? provisioning_uuid : ""); - cJSON_AddNumberToObject(get_info, "serial", serial_id); + cJSON_AddStringToObject(get_info, ezlopi_mac_str, device_mac ? device_mac : ""); + cJSON_AddStringToObject(get_info, ezlopi_uuid_str, controller_uuid ? controller_uuid : ""); + cJSON_AddStringToObject(get_info, ezlopi_uuid_prov_str, provisioning_uuid ? provisioning_uuid : ""); + cJSON_AddNumberToObject(get_info, ezlopi_serial_str, serial_id); - cJSON_AddStringToObject(get_info, "ssid", wifi_ssid ? wifi_ssid : ""); - cJSON_AddStringToObject(get_info, "device_name", device_name ? device_name : ""); + cJSON_AddStringToObject(get_info, ezlopi_ssid_str, wifi_ssid ? wifi_ssid : ""); + cJSON_AddStringToObject(get_info, ezlopi_dev_name_str, device_name ? device_name : ""); cJSON_AddStringToObject(get_info, "ezlopi_device_type", device_type ? device_type : ""); - cJSON_AddStringToObject(get_info, "device_total_flash", CONFIG_ESPTOOLPY_FLASHSIZE); + cJSON_AddStringToObject(get_info, ezlopi_dev_flash_str, CONFIG_ESPTOOLPY_FLASHSIZE); // cJSON_AddStringToObject(get_info, "device_free_flash", ""); // TODO - cJSON_AddStringToObject(get_info, "brand", device_brand ? device_brand : ""); - cJSON_AddStringToObject(get_info, "manufacture", device_manufacturer ? device_manufacturer : ""); - cJSON_AddStringToObject(get_info, "model", device_model ? device_model : ""); + cJSON_AddStringToObject(get_info, ezlopi_brand_str, device_brand ? device_brand : ""); + cJSON_AddStringToObject(get_info, ezlopi_manf_name_str, device_manufacturer ? device_manufacturer : ""); + cJSON_AddStringToObject(get_info, ezlopi_model_num_str, device_model ? device_model : ""); cJSON *json_net_info = cJSON_CreateObject(); if (json_net_info) @@ -337,7 +342,6 @@ static void qt_serial_get_info() { cJSON_AddFalseToObject(json_net_info, "wifi_connection_status"); } - free(net_stat); } cJSON_AddItemToObject(get_info, "net_info", json_net_info); } @@ -371,41 +375,59 @@ static void qt_serial_set_wifi(const char *data) if (root) { - if (cJSON_GetObjectItem(root, "pass")) + cJSON *cj_wifi_ssid = cJSON_GetObjectItem(root, ezlopi_ssid_str); + cJSON *cj_wifi_pass = cJSON_GetObjectItem(root, ezlopi_pass_str); + if (cj_wifi_ssid && cj_wifi_pass && cj_wifi_ssid->valuestring && cj_wifi_pass->valuestring) { - char *ssid = cJSON_GetObjectItem(root, "ssid")->valuestring; - char *pass = cJSON_GetObjectItem(root, "pass")->valuestring; + char *ssid = cj_wifi_ssid->valuestring; + char *pass = cj_wifi_pass->valuestring; - if (ssid && pass && (strlen(pass) >= 8)) + if (strlen(pass) >= EZLOPI_WIFI_MIN_PASS_CHAR) { + // TRACE_I("SSID: %s\tPass : %s\r\n", ssid, pass); if (ezlopi_factory_info_v3_set_wifi(ssid, pass)) { status_write = 1; } ezlopi_wifi_set_new_wifi_flag(); - static uint8_t attempt = 5; - while (attempt > 0) + uint8_t attempt = 1; + while (attempt <= EZLOPI_WIFI_CONN_RETRY_ATTEMPT) { esp_err_t wifi_error = ezlopi_wifi_connect((const char *)ssid, (const char *)pass); - vTaskDelay(1000 / portTICK_PERIOD_MS); - if (wifi_error == ESP_OK) - { - status = 1; - break; - } - else + ezlopi_wait_for_wifi_to_connect((uint32_t)EZLOPI_WIFI_CONN_ATTEMPT_INTERVAL); + s_ezlopi_net_status_t *net_stat = ezlopi_get_net_status(); + if (net_stat) { - TRACE_W("wifi_error: %u", wifi_error); - status = 0; + if (net_stat->wifi_status->wifi_connection) + { + status = 1; + break; + } + else + { + TRACE_E("WiFi Connection to AP: %s failed !", ssid); + // printf("WiFi Connection to AP: %s failed !\r\n", ssid); + status = 0; + } } + TRACE_W("Trying to connect to AP : %s, attempt %d ....", ssid, attempt); + // printf("Trying to connect to AP : %s, attempt %d ....\r\n", ssid, attempt); + attempt++; + // vTaskDelay(EZLOPI_WIFI_CONNECT_ATTEMPT_INTERVAL / portTICK_PERIOD_MS); } + + qt_serial_response(2, status_write, status); + } + else + { + TRACE_E("Invalid WiFi SSID or Password, aborting!"); + // printf("Invalid WiFi SSID or Password, aborting!\r\n"); + qt_serial_response(2, 0, 0); } } cJSON_Delete(root); // free Json string } - - qt_serial_response(2, status_write, status); } static void qt_serial_response(uint8_t cmd, uint8_t status_write, uint8_t status_connect) @@ -415,8 +437,8 @@ static void qt_serial_response(uint8_t cmd, uint8_t status_write, uint8_t status if (response) { - cJSON_AddNumberToObject(response, "cmd", cmd); - cJSON_AddNumberToObject(response, "status_write", status_write); + cJSON_AddNumberToObject(response, ezlopi_cmd_str, cmd); + cJSON_AddNumberToObject(response, ezlopi_status_write_str, status_write); cJSON_AddNumberToObject(response, "status_connect", status_connect); char *my_json_string = cJSON_Print(response); @@ -425,11 +447,7 @@ static void qt_serial_response(uint8_t cmd, uint8_t status_write, uint8_t status if (my_json_string) { cJSON_Minify(my_json_string); - qt_serial_tx_data(strlen(my_json_string), (uint8_t *)my_json_string); - const int len = strlen(my_json_string); - // const int txBytes = uart_write_bytes(UART_NUM_0, my_json_string, len); // Send the data over uart - cJSON_free(my_json_string); } } diff --git a/services/qt_serial/qt_serial.h b/services/qt_serial/qt_serial.h index 1cf7d0857..d05a0b644 100644 --- a/services/qt_serial/qt_serial.h +++ b/services/qt_serial/qt_serial.h @@ -5,22 +5,27 @@ #include "driver/uart.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" + #ifdef __cplusplus extern "C" { #endif -// typedef enum e_ezlopi_item_type -// { -// EZLOPI_ITEM_NONE = 0, -// EZLOPI_ITEM_LED = 1, -// EZLOPI_ITEM_RELAY = 2, -// EZLOPI_ITEM_PLUG = 3, -// EZLOPI_ITEM_SPK = 4, // SPK -> [S: Switch][P: Push Buttom][K: Key] -// EZLOPI_ITEM_MPU6050 =5, -// EZLOPI_ITEM_ADXL345 = 6, -// EZLOPI_ITEM_ -// } e_ezlopi_item_type_t; + // typedef enum e_ezlopi_item_type + // { + // EZLOPI_ITEM_NONE = 0, + // EZLOPI_ITEM_LED = 1, + // EZLOPI_ITEM_RELAY = 2, + // EZLOPI_ITEM_PLUG = 3, + // EZLOPI_ITEM_SPK = 4, // SPK -> [S: Switch][P: Push Buttom][K: Key] + // EZLOPI_ITEM_MPU6050 =5, + // EZLOPI_ITEM_ADXL345 = 6, + // EZLOPI_ITEM_ + // } e_ezlopi_item_type_t; +#define EZLOPI_WIFI_CONNECT_TIMEOUT 5000 +#define EZLOPI_WIFI_MIN_PASS_CHAR 8 +#define EZLOPI_WIFI_CONN_ATTEMPT_INTERVAL 5000 +#define EZLOPI_WIFI_CONN_RETRY_ATTEMPT 2 void qt_serial_init(void); int qt_serial_tx_data(int len, uint8_t *data); diff --git a/services/web_provisioning/web_provisioning.c b/services/web_provisioning/web_provisioning.c index c97617ace..ebd1708d2 100644 --- a/services/web_provisioning/web_provisioning.c +++ b/services/web_provisioning/web_provisioning.c @@ -20,6 +20,7 @@ #include "firmware.h" #include "settings.h" #include "scenes_scripts.h" +#include "scenes_expressions.h" #include "ezlopi_wifi.h" #include "ezlopi_http.h" @@ -31,6 +32,7 @@ #include "ezlopi_websocket_client.h" static uint32_t message_counter = 0; +static xTaskHandle _task_handle = NULL; static TaskHandle_t ezlopi_update_config_notifier = NULL; @@ -40,6 +42,7 @@ static void __rpc_method_notfound(cJSON *cj_request, cJSON *cj_response); static void __hub_reboot(cJSON *cj_request, cJSON *cj_response); static void __fetch_wss_endpoint(void *pv); static void web_provisioning_config_check(void *pv); +static void __print_sending_data(char *data_str, e_trace_type_t print_type); typedef void (*f_method_func_t)(cJSON *cj_request, cJSON *cj_response); typedef struct s_method_list_v2 @@ -61,6 +64,35 @@ uint32_t web_provisioning_get_message_count(void) return message_counter; } +int web_provisioning_send_str_data_to_nma_websocket(char *str_data, e_trace_type_t print_type) +{ + int ret = 0; + if (str_data) + { + int retries = 3; + while (--retries) + { + if (ezlopi_websocket_client_send(str_data, strlen(str_data)) > 0) + { + ret = 1; + message_counter++; + break; + } + } + + if (ret) + { + __print_sending_data(str_data, print_type); + } + else + { + __print_sending_data(str_data, TRACE_TYPE_W); + } + } + + return ret; +} + int web_provisioning_send_to_nma_websocket(cJSON *cjson_data, e_trace_type_t print_type) { int ret = 0; @@ -72,31 +104,6 @@ int web_provisioning_send_to_nma_websocket(cJSON *cjson_data, e_trace_type_t pri if (cjson_str_data) { cJSON_Minify(cjson_str_data); - switch (print_type) - { - case TRACE_TYPE_B: - { - TRACE_B("## WSS-SENDING >>>>>>>>>>>>>>>>>>>\r\n%s", cjson_str_data); - break; - } - case TRACE_TYPE_D: - { - TRACE_D("## WSS-SENDING >>>>>>>>>>>>>>>>>>>\r\n%s", cjson_str_data); - break; - } - case TRACE_TYPE_E: - { - TRACE_E("## WSS-SENDING >>>>>>>>>>>>>>>>>>>\r\n%s", cjson_str_data); - break; - } - case TRACE_TYPE_I: - { - TRACE_I("## WSS-SENDING >>>>>>>>>>\r\n%s", cjson_str_data); - break; - } - default: - break; - } int retries = 3; while (--retries) @@ -109,6 +116,16 @@ int web_provisioning_send_to_nma_websocket(cJSON *cjson_data, e_trace_type_t pri break; } } + + if (ret) + { + __print_sending_data(cjson_str_data, print_type); + } + else + { + __print_sending_data(cjson_str_data, TRACE_TYPE_W); + } + free(cjson_str_data); } } @@ -314,7 +331,7 @@ static void web_provisioning_config_check(void *pv) while (1) { - ezlopi_wait_for_wifi_to_connect(UINT32_MAX); + ezlopi_wait_for_wifi_to_connect(portMAX_DELAY); UBaseType_t water_mark = uxTaskGetStackHighWaterMark(NULL); TRACE_D("water_mark: %d", water_mark); @@ -399,6 +416,16 @@ static void web_provisioning_config_check(void *pv) vTaskDelete(NULL); } +void web_provisioning_deinit(void) +{ + if (_task_handle) + { + vTaskDelete(_task_handle); + } + + ezlopi_websocket_client_kill(); +} + static void __fetch_wss_endpoint(void *pv) { s_ezlopi_http_data_t *ws_endpoint = NULL; @@ -407,7 +434,7 @@ static void __fetch_wss_endpoint(void *pv) { ulTaskNotifyTake(pdFALSE, portMAX_DELAY); - ezlopi_wait_for_wifi_to_connect(UINT32_MAX); + ezlopi_wait_for_wifi_to_connect(portMAX_DELAY); vTaskDelay(100 / portTICK_RATE_MS); @@ -455,6 +482,7 @@ static void __fetch_wss_endpoint(void *pv) static void __connection_upcall(bool connected) { + TRACE_D("connected: %d", connected); static bool prev_status; if (connected) { @@ -512,12 +540,19 @@ static void __call_method_and_send_response(cJSON *cj_request, cJSON *cj_method, cJSON_AddNumberToObject(cj_response, ezlopi_msg_id_str, message_counter); cJSON_AddItemReferenceToObject(cj_response, ezlopi_sender_str, cj_sender); - cJSON_AddNullToObject(cj_response, "error"); + cJSON_AddNullToObject(cj_response, ezlopi_error_str); method_func(cj_request, cj_response); - web_provisioning_send_to_nma_websocket(cj_response, print_type); + char *data_to_send = cJSON_Print(cj_response); cJSON_Delete(cj_response); + + if (data_to_send) + { + cJSON_Minify(data_to_send); + web_provisioning_send_str_data_to_nma_websocket(data_to_send, print_type); + free(data_to_send); + } } else { @@ -533,14 +568,14 @@ static void __message_upcall(const char *payload, uint32_t len) if (cj_request) { - cJSON *cj_error = cJSON_GetObjectItem(cj_request, "error"); - cJSON *cj_method = cJSON_GetObjectItem(cj_request, "method"); + cJSON *cj_error = cJSON_GetObjectItem(cj_request, ezlopi_error_str); + cJSON *cj_method = cJSON_GetObjectItem(cj_request, ezlopi_key_method_str); - if ((NULL == cj_error) || (cJSON_NULL == cj_error->type) || (NULL != cj_error->valuestring) || ((NULL != cj_error->valuestring) && (0 == strncmp(cj_error->valuestring, "null", 4)))) + if ((NULL == cj_error) || (cJSON_NULL == cj_error->type) || (NULL != cj_error->valuestring) || ((NULL != cj_error->valuestring) && (0 == strncmp(cj_error->valuestring, ezlopi_null_str, 4)))) { if ((NULL != cj_method) && (NULL != cj_method->valuestring)) { - TRACE_I("## WS Rx <<<<<<<<<< '%s'\r\n%.*s", (cj_method->valuestring ? cj_method->valuestring : ""), len, payload); + TRACE_I("## WS Rx <<<<<<<<<< '%s'\r\n%.*s", (cj_method->valuestring ? cj_method->valuestring : ezlopi__str), len, payload); uint32_t method_idx = __search_method_in_list(cj_method); @@ -558,8 +593,8 @@ static void __message_upcall(const char *payload, uint32_t len) } else { - TRACE_E("## WS Rx <<<<<<<<<< '%s'\r\n%.*s", (NULL != cj_method) ? (cj_method->valuestring ? cj_method->valuestring : "") : "", len, payload); - TRACE_E("cj_error: %p, cj_error->type: %u, cj_error->value_string: %s", cj_error, cj_error->type, cj_error ? (cj_error->valuestring ? cj_error->valuestring : "null") : "null"); + TRACE_E("## WS Rx <<<<<<<<<< '%s'\r\n%.*s", (NULL != cj_method) ? (cj_method->valuestring ? cj_method->valuestring : ezlopi__str) : ezlopi__str, len, payload); + TRACE_E("cj_error: %p, cj_error->type: %u, cj_error->value_string: %s", cj_error, cj_error->type, cj_error ? (cj_error->valuestring ? cj_error->valuestring : ezlopi_null_str) : ezlopi_null_str); } cJSON_Delete(cj_request); @@ -570,12 +605,12 @@ static void __rpc_method_notfound(cJSON *cj_request, cJSON *cj_response) { cJSON_AddItemReferenceToObject(cj_response, ezlopi_id_str, cJSON_GetObjectItem(cj_request, ezlopi_id_str)); cJSON_AddItemReferenceToObject(cj_response, ezlopi_key_method_str, cJSON_GetObjectItem(cj_request, ezlopi_key_method_str)); - cJSON *cjson_error = cJSON_AddObjectToObject(cj_response, "error"); + cJSON *cjson_error = cJSON_AddObjectToObject(cj_response, ezlopi_error_str); if (cjson_error) { - cJSON_AddNumberToObject(cjson_error, "code", -32602); - cJSON_AddStringToObject(cjson_error, "data", "rpc.method.notfound"); - cJSON_AddStringToObject(cjson_error, "message", "Unknown method"); + cJSON_AddNumberToObject(cjson_error, ezlopi_code_str, -32602); + cJSON_AddStringToObject(cjson_error, ezlopi_data_str, ezlopi_rpc_method_notfound_str); + cJSON_AddStringToObject(cjson_error, ezlopi_message_str, ezlopi_Unknown_method_str); } cJSON_AddObjectToObject(cj_response, ezlopi_result_str); @@ -583,6 +618,38 @@ static void __rpc_method_notfound(cJSON *cj_request, cJSON *cj_response) static void __hub_reboot(cJSON *cj_request, cJSON *cj_response) { + web_provisioning_deinit(); esp_restart(); - return NULL; } + +static void __print_sending_data(char *data_str, e_trace_type_t print_type) +{ + switch (print_type) + { + case TRACE_TYPE_W: + { + TRACE_W("## WSS-SENDING >>>>>>>>>>>>>>>>>>>\r\n%s", data_str); + break; + } + case TRACE_TYPE_D: + { + TRACE_D("## WSS-SENDING >>>>>>>>>>>>>>>>>>>\r\n%s", data_str); + break; + } + case TRACE_TYPE_E: + { + TRACE_E("## WSS-SENDING >>>>>>>>>>>>>>>>>>>\r\n%s", data_str); + break; + } + case TRACE_TYPE_I: + { + TRACE_I("## WSS-SENDING >>>>>>>>>>\r\n%s", data_str); + break; + } + default: + { + TRACE_E("## WSS-SENDING >>>>>>>>>>\r\n%s", data_str); + break; + } + } +} \ No newline at end of file diff --git a/services/web_provisioning/web_provisioning.h b/services/web_provisioning/web_provisioning.h index e248a6229..565467f9a 100644 --- a/services/web_provisioning/web_provisioning.h +++ b/services/web_provisioning/web_provisioning.h @@ -13,6 +13,7 @@ extern "C" typedef enum e_trace_type { TRACE_TYPE_NONE = 0, + TRACE_TYPE_W, // Warning (Orange) TRACE_TYPE_B, TRACE_TYPE_D, // debug (White) TRACE_TYPE_I, // Info (Blue) @@ -20,9 +21,11 @@ extern "C" } e_trace_type_t; void web_provisioning_init(void); + void web_provisioning_deinit(void); uint32_t web_provisioning_get_message_count(void); char *parse_device_from_web_payload(void *ws_ctx, char *web_payload); int web_provisioning_send_to_nma_websocket(cJSON *cjson_data, e_trace_type_t print_type); + int web_provisioning_send_str_data_to_nma_websocket(char *str_data, e_trace_type_t print_type); #ifdef __cplusplus } diff --git a/services/web_provisioning/web_provisioning_macro.h b/services/web_provisioning/web_provisioning_macro.h index 964f422ff..ea91a6266 100644 --- a/services/web_provisioning/web_provisioning_macro.h +++ b/services/web_provisioning/web_provisioning_macro.h @@ -3,8 +3,7 @@ CLOUD_METHOD("hub.items.list", items_list_v3, NULL) CLOUD_METHOD("hub.item.value.set", items_set_value_v3, items_update_v3) CLOUD_METHOD("hub.devices.list", devices_list_v3, NULL) - -CLOUD_METHOD("hub.room.list", room_list, NULL) +CLOUD_METHOD("hub.device.name.set", device_name_set, device_updated) CLOUD_METHOD("hub.modes.get", modes_get, NULL) @@ -24,21 +23,36 @@ CLOUD_METHOD("hub.device.setting.value.set", ezlopi_device_settings_value_set_v3 CLOUD_METHOD("hub.device.setting.reset", ezlopi_device_settings_reset_v3, NULL) CLOUD_METHOD("hub.scenes.list", scenes_list, NULL) -CLOUD_METHOD("hub.scenes.create", scenes_create, NULL) +CLOUD_METHOD("hub.scenes.create", scenes_create, scene_added) CLOUD_METHOD("hub.scenes.run", scenes_run, NULL) CLOUD_METHOD("hub.scenes.get", scenes_get, NULL) -// CLOUD_METHOD("hub.scenes.edit", scenes_edit, NULL) -CLOUD_METHOD("hub.scenes.delete", scenes_delete, NULL) +CLOUD_METHOD("hub.scenes.edit", scenes_edit, scene_changed) +CLOUD_METHOD("hub.scenes.delete", scenes_delete, scene_deleted) CLOUD_METHOD("hub.scenes.blocks.list", scenes_blocks_list, NULL) CLOUD_METHOD("hub.scenes.block.data.list", scenes_block_data_list, NULL) +CLOUD_METHOD("hub.scenes.enabled.set", scenes_enable_set, scene_changed) +CLOUD_METHOD("hub.scenes.notification.add", scenes_notification_add, scene_changed) +CLOUD_METHOD("hub.scenes.notification.remove", scenes_notification_remove, scene_changed) +// CLOUD_METHOD("hub.scenes.status.get", scenes_status_get, NULL) // Incomplete + +CLOUD_METHOD("hub.room.list", room_list, NULL) +CLOUD_METHOD("hub.room.create", room_create, room_created) +CLOUD_METHOD("hub.room.name.set", room_name_set, room_edited) +CLOUD_METHOD("hub.room.order.set", room_order_set, room_reordered) +CLOUD_METHOD("hub.room.get", room_get, NULL) +CLOUD_METHOD("hub.room.delete", room_delete, room_deleted) +CLOUD_METHOD("hub.room.all.delete", room_all_delete, NULL) + CLOUD_METHOD("hub.scenes.scripts.list", scenes_scripts_list, NULL) CLOUD_METHOD("hub.scenes.scripts.add", scenes_scripts_add, NULL) CLOUD_METHOD("hub.scenes.scripts.get", scenes_scripts_get, NULL) CLOUD_METHOD("hub.scenes.scripts.delete", scenes_scripts_delete, NULL) CLOUD_METHOD("hub.scenes.scripts.set", scenes_scripts_set, NULL) CLOUD_METHOD("hub.scenes.scripts.run", scenes_scripts_run, NULL) -// CLOUD_METHOD("hub.scenes.status.get", scenes_status_get, NULL) // Incomplete -// CLOUD_METHOD("hub.scenes.enabled.set", scenes_enable_set) // Not implemented + +CLOUD_METHOD("hub.scenes.expressions.set", scenes_expressions_set, NULL) +CLOUD_METHOD("hub.scenes.expressions.list", scenes_expressions_list, NULL) +CLOUD_METHOD("hub.scenes.expressions.delete", scenes_expressions_delete, NULL) CLOUD_METHOD("hub.nma.register.repeat", register_repeat, NULL)