Skip to content

Commit

Permalink
needs fixing on web-flasher side
Browse files Browse the repository at this point in the history
  • Loading branch information
work-krishna committed Jun 27, 2024
2 parents 3ab8d1c + 1dca78f commit 64786b0
Show file tree
Hide file tree
Showing 38 changed files with 1,302 additions and 269 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ message(STATUS "EXTRA_COMPONENTS_DIRS: ${EXTRA_COMPONENT_DIRS}\r\n")
set(EXCLUDE_COMPONENTS freemodbus)

## ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ##
project(Ezlo_Pi_v3x)
project(Ezlo_Pi_v4x)
2 changes: 2 additions & 0 deletions ezlopi-cloud/ezlopi-cloud-constants/ezlopi_cloud_keywords.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ 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_block_enable_str = "block_enable";
const char* ezlopi_blockId_str = "blockId";
const char* ezlopi_blockOptions_str = "blockOptions";
const char* ezlopi_blockName_str = "blockName";
const char* ezlopi_version_str = "version";
Expand Down
2 changes: 2 additions & 0 deletions ezlopi-cloud/ezlopi-cloud-constants/ezlopi_cloud_keywords.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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_block_enable_str;
extern const char* ezlopi_blockId_str;
extern const char* ezlopi_blockOptions_str;
extern const char* ezlopi_blockName_str;
extern const char* ezlopi_version_str;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@ const char *method_hub_reboot = "hub.reboot";
// const char *method_hub_network_enabled_set = "hub.network.enabled.set";
// 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 *method_hub_scene_run_progress = "hub.scene.run.progress";
const char *ezlopi_rpc_method_notfound_str = "rpc.method.notfound";

const char *ezlopi_hub_expression_added_str = "hub.expression.added";
const char *ezlopi_hub_expression_changed_str = "hub.expression.changed";
const char *ezlopi_hub_expression_deleted_str = "hub.expression.deleted";

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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ extern const char *method_hub_reboot;
// 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_expression_added_str;
extern const char *ezlopi_hub_expression_changed_str;
extern const char *ezlopi_hub_expression_deleted_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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static void __create_reg_packet(void)

static void __reg_loop(void *arg)
{
if (ezlopi_event_group_wait_for_event(EZLOPI_EVENT_NMA_REG, 5000, false) <= 0)
if (ezlopi_event_group_wait_for_event(EZLOPI_EVENT_NMA_REG, 0, false) <= 0)
{
__create_reg_packet();

Expand Down
1 change: 1 addition & 0 deletions ezlopi-cloud/ezlopi-cloud-scenes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(EZLOPI_COMPONENTS
set(EZLOPI_CORE_COMPONENTS
ezlopi-core-factory-info
ezlopi-core-scenes
ezlopi-core-http
)
# Define cloud components
set(EZLOPI_CLOUD_COMPONENTS
Expand Down
205 changes: 190 additions & 15 deletions ezlopi-cloud/ezlopi-cloud-scenes/ezlopi_cloud_scenes.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#include "ezlopi_core_cjson_macros.h"
#include "ezlopi_service_meshbot.h"
#include "ezlopi_cloud_constants.h"
#include "ezlopi_core_scenes_populate.h"
#include "ezlopi_core_scenes_operators.h"
#include "ezlopi_core_scenes_notifications.h"
#include "ezlopi_core_scenes_then_methods_helper_func.h"
#include "ezlopi_core_scenes_when_methods_helper_functions.h"

void scenes_list(cJSON* cj_request, cJSON* cj_response)
Expand Down Expand Up @@ -349,39 +351,193 @@ void scenes_notification_remove(cJSON* cj_request, cJSON* cj_response)
}
}

void scenes_block_status_reset(cJSON* cj_request, cJSON* cj_response)
void scenes_house_modes_set(cJSON* cj_request, cJSON* cj_response)
{
cJSON* cj_result = cJSON_AddObjectToObject(__FUNCTION__, cj_response, ezlopi_result_str);
cJSON* cj_params = cJSON_GetObjectItem(__FUNCTION__, cj_request, ezlopi_params_str);
if (cj_result && cj_params)
{
uint32_t scene_id = 0;
CJSON_GET_ID(scene_id, cJSON_GetObjectItem(__FUNCTION__, cj_params, ezlopi_sceneId_str));
cJSON* cj_scene_id = cJSON_GetObjectItem(__FUNCTION__, cj_params, ezlopi__id_str);
cJSON* cj_house_mode_arr = cJSON_GetObjectItem(__FUNCTION__, cj_params, "houseModes");

if (scene_id)
if (cj_scene_id && cj_scene_id->valuestring && cj_house_mode_arr && cJSON_IsArray(cj_house_mode_arr))
{
l_scenes_list_v2_t* scene_node = ezlopi_scenes_get_scenes_head_v2();
while (scene_node)
char* scene_str = ezlopi_nvs_read_str(cj_scene_id->valuestring);
if (scene_str)
{
cJSON* cj_scene = cJSON_Parse(__FUNCTION__, scene_str);
ezlopi_free(__FUNCTION__, scene_str);

if (cj_scene)
{
cJSON* cj_house_modes = cJSON_GetObjectItem(__FUNCTION__, cj_scene, ezlopi_house_modes_str);
if (cj_house_modes)
{
cJSON_DeleteItemFromObject(__FUNCTION__, cj_scene, ezlopi_house_modes_str);
}

cJSON_AddItemToObject(__FUNCTION__, cj_scene, ezlopi_house_modes_str, cJSON_Duplicate(__FUNCTION__, cj_house_mode_arr, true));

cJSON* cj_test = cJSON_GetObjectItem(__FUNCTION__, cj_scene, ezlopi_house_modes_str);
if (cj_test)
{
CJSON_TRACE("new-house_mode:", cj_test);
}

char* updated_scene_str = cJSON_PrintBuffered(__FUNCTION__, cj_scene, 4096, false);
cJSON_Delete(__FUNCTION__, cj_scene);

if (updated_scene_str)
{
ezlopi_nvs_write_str(updated_scene_str, strlen(updated_scene_str), cj_scene_id->valuestring);
ezlopi_free(__FUNCTION__, updated_scene_str);
}
}
}

uint32_t scene_id = strtoul(cj_scene_id->valuestring, NULL, 16);
if (scene_id)
{
if (scene_node->_id == scene_id)
l_scenes_list_v2_t* scene_node = ezlopi_scenes_get_by_id_v2(scene_id);
if (scene_node && scene_node->house_modes)
{
s_when_function_t* function_state = (s_when_function_t*)scene_node->when_block->fields->user_arg;
if (function_state)
ezlopi_scenes_delete_house_modes(scene_node->house_modes);
if (NULL != (scene_node->house_modes = ezlopi_scenes_populate_house_modes(cJSON_Duplicate(__FUNCTION__, cj_house_mode_arr, true))))
{
function_state->current_state = false;
TRACE_S("Updating ... House_modes ; Success");
}
break;
}
scene_node = scene_node->next;
}
}
}
}

void scenes_action_block_test(cJSON * cj_request, cJSON * cj_response)
{
cJSON* cj_result = cJSON_AddObjectToObject(__FUNCTION__, cj_response, ezlopi_result_str);
if (cj_result)
{
cJSON* cj_params = cJSON_GetObjectItem(__FUNCTION__, cj_request, ezlopi_params_str);
if (cj_result && cj_params)
{
cJSON* cj_block = cJSON_GetObjectItem(__FUNCTION__, cj_params, "block");
if (cj_block)
{
// 1. populate --> 'test_then_block' ;
l_action_block_v2_t * test_then_block = (l_action_block_v2_t*)ezlopi_malloc(__FUNCTION__, sizeof(l_action_block_v2_t));
if (test_then_block)
{
memset(test_then_block, 0, sizeof(l_action_block_v2_t));


cJSON* dupli = cJSON_Duplicate(__FUNCTION__, cj_block, true);
if (dupli) // duplicate the 'cj_block' to avoid crashes
{
ezlopi_scenes_populate_assign_action_block(test_then_block, dupli, SCENE_BLOCK_TYPE_THEN);
// CJSON_TRACE("test_then:", dupli);
cJSON_Delete(__FUNCTION__, dupli);
}

// 2. Now to fill the 's_ezlopi_core_http_mbedtls_t' variable and execute 'send_http_request'.
s_ezlopi_core_http_mbedtls_t* tmp_http_data = (s_ezlopi_core_http_mbedtls_t*)ezlopi_malloc(__FUNCTION__, sizeof(s_ezlopi_core_http_mbedtls_t));
if (tmp_http_data)
{
memset(tmp_http_data, 0, sizeof(s_ezlopi_core_http_mbedtls_t));
l_fields_v2_t* curr_field = test_then_block->fields;

const s_sendhttp_method_t __sendhttp_method[] = {
{.field_name = "request", .field_func = parse_http_request_type},
{.field_name = "url", .field_func = parse_http_url},
{.field_name = "credential", .field_func = parse_http_creds},
{.field_name = "contentType", .field_func = parse_http_content_type},
{.field_name = "content", .field_func = parse_http_content},
{.field_name = "headers", .field_func = parse_http_headers},
{.field_name = "skipSecurity", .field_func = parse_http_skipsecurity},
{.field_name = NULL, .field_func = NULL},
};

while (NULL != curr_field) // fields
{
for (uint8_t i = 0; i < ((sizeof(__sendhttp_method) / sizeof(__sendhttp_method[i]))); i++)
{
if (0 == strncmp(__sendhttp_method[i].field_name, curr_field->name, strlen(__sendhttp_method[i].field_name) + 1))
{
(__sendhttp_method[i].field_func)(tmp_http_data, curr_field);
break;
}
}
curr_field = curr_field->next;
}
// now to trigger http_request and extract the response.
tmp_http_data->response = NULL;
tmp_http_data->response_maxlen = 0;
ezlopi_core_http_mbedtls_req(tmp_http_data); // Returns:- [response_buffer = &Memory_block]

if (tmp_http_data->response)
{
int code = 400;
char detail[100] = { 0 };
if (sscanf(tmp_http_data->response, "HTTP/1.1 %d %99s[^\n]", &code, detail) == 2)
{
cJSON_AddNumberToObject(__FUNCTION__, cj_result, "httpAnswerCode", code);
}
else
{
cJSON_AddNumberToObject(__FUNCTION__, cj_result, "httpAnswerCode", code);
}
}
else
{
cJSON_AddNumberToObject(__FUNCTION__, cj_result, "httpAnswerCode", 404);
}

free_http_mbedtls_struct(tmp_http_data);
ezlopi_free(__FUNCTION__, tmp_http_data);
}

ezlopi_scenes_delete_action_blocks(test_then_block);
}
}
}
}
}

void scenes_block_enabled_set(cJSON* cj_request, cJSON* cj_response)
{
// cJSON* cj_result = cJSON_AddObjectToObject(__FUNCTION__, cj_response, ezlopi_result_str);
cJSON* cj_params = cJSON_GetObjectItem(__FUNCTION__, cj_request, ezlopi_params_str);
if (cj_params)
{
cJSON *cj_scene_id = cJSON_GetObjectItem(__FUNCTION__, cj_params, ezlopi_sceneId_str);
cJSON *cj_block_id = cJSON_GetObjectItem(__FUNCTION__, cj_params, ezlopi_blockId_str);
bool block_en = false;
CJSON_GET_VALUE_BOOL(cj_params, "enabled", block_en);
if (cj_scene_id && (NULL != cj_scene_id->valuestring) && cj_block_id && (NULL != cj_block_id->valuestring))
{
ezlopi_core_scene_block_enable_set_reset(cj_scene_id->valuestring, cj_block_id->valuestring, block_en);
}
}
}

void scenes_block_status_reset(cJSON * cj_request, cJSON * cj_response)
{
// cJSON* cj_result = cJSON_AddObjectToObject(__FUNCTION__, cj_response, ezlopi_result_str);
cJSON* cj_params = cJSON_GetObjectItem(__FUNCTION__, cj_request, ezlopi_params_str);
if (cj_params)
{
cJSON *cj_scene_id = cJSON_GetObjectItem(__FUNCTION__, cj_params, ezlopi_sceneId_str);
cJSON *cj_block_id = cJSON_GetObjectItem(__FUNCTION__, cj_params, ezlopi_blockId_str);
if (cj_scene_id && (NULL != cj_scene_id->valuestring) && cj_block_id && (NULL != cj_block_id->valuestring))
{
ezlopi_core_scene_set_reset_latch(cj_scene_id->valuestring, cj_block_id->valuestring, false);
}
}
}


////// updater for scene
////// useful for 'hub.scenes.enabled.set'
void scene_changed(cJSON* cj_request, cJSON* cj_response)
void scene_changed(cJSON * cj_request, cJSON * cj_response)
{
cJSON_DeleteItemFromObject(__FUNCTION__, cj_response, ezlopi_sender_str);
cJSON_DeleteItemFromObject(__FUNCTION__, cj_response, ezlopi_error_str);
Expand All @@ -402,12 +558,13 @@ void scene_changed(cJSON* cj_request, cJSON* cj_response)
if (scene_str)
{
cJSON_AddRawToObject(__FUNCTION__, cj_response, ezlopi_result_str, scene_str);
ezlopi_free(__FUNCTION__, scene_str);
}
}
}
}

void scene_added(cJSON* cj_request, cJSON* cj_response)
void scene_added(cJSON * cj_request, cJSON * cj_response)
{
cJSON_DeleteItemFromObject(__FUNCTION__, cj_response, ezlopi_sender_str);
cJSON_DeleteItemFromObject(__FUNCTION__, cj_response, ezlopi_error_str);
Expand All @@ -425,9 +582,27 @@ void scene_added(cJSON* cj_request, cJSON* cj_response)
ezlopi_free(__FUNCTION__, new_scene);
}
}
else /* sometime request contain the key: 'sceneId' instead of '_id' */
{
cJSON* cj_params = cJSON_GetObjectItem(__FUNCTION__, cj_request, ezlopi_params_str);
if (cj_params)
{
cJSON* new_scene_id = cJSON_GetObjectItem(__FUNCTION__, cj_params, ezlopi_sceneId_str);
if (new_scene_id && new_scene_id->valuestring)
{
char* new_scene = ezlopi_nvs_read_str(new_scene_id->valuestring);
if (new_scene)
{
cJSON_AddRawToObject(__FUNCTION__, cj_response, ezlopi_result_str, new_scene);
ezlopi_free(__FUNCTION__, new_scene);
}
}
}

}
}

void scene_deleted(cJSON* cj_request, cJSON* cj_response)
void scene_deleted(cJSON * cj_request, cJSON * cj_response)
{
cJSON_DeleteItemFromObject(__FUNCTION__, cj_response, ezlopi_sender_str);
cJSON_DeleteItemFromObject(__FUNCTION__, cj_response, ezlopi_error_str);
Expand Down
3 changes: 3 additions & 0 deletions ezlopi-cloud/ezlopi-cloud-scenes/ezlopi_cloud_scenes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ extern "C"
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);
void scenes_block_enabled_set(cJSON* cj_request, cJSON* cj_response);
void scenes_house_modes_set(cJSON* cj_request, cJSON* cj_response);
void scenes_action_block_test(cJSON * cj_request, cJSON * cj_response);
void scenes_block_status_reset(cJSON* cj_request, cJSON* cj_response);

///////////// updaters
Expand Down
Loading

0 comments on commit 64786b0

Please sign in to comment.