From 57cec10c2d467bba4797fc248df409c5c5792e4c Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Thu, 18 Jul 2024 12:25:46 +0300 Subject: [PATCH] Changed task management --- main/zh_espnow_switch.c | 77 +++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/main/zh_espnow_switch.c b/main/zh_espnow_switch.c index 2e72d2c..01bf6a1 100644 --- a/main/zh_espnow_switch.c +++ b/main/zh_espnow_switch.c @@ -555,26 +555,26 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve { zh_send_switch_config_message(switch_config); zh_send_switch_status_message(switch_config); + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) + { + zh_send_sensor_config_message(switch_config); + } if (switch_config->is_first_connection == false) { xTaskCreatePinnedToCore(&zh_send_switch_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->switch_attributes_message_task, tskNO_AFFINITY); xTaskCreatePinnedToCore(&zh_send_switch_keep_alive_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->switch_keep_alive_message_task, tskNO_AFFINITY); + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) + { + xTaskCreatePinnedToCore(&zh_send_sensor_status_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->sensor_status_message_task, tskNO_AFFINITY); + xTaskCreatePinnedToCore(&zh_send_sensor_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->sensor_attributes_message_task, tskNO_AFFINITY); + } switch_config->is_first_connection = true; } else { vTaskResume(switch_config->switch_attributes_message_task); vTaskResume(switch_config->switch_keep_alive_message_task); - } - if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) - { - zh_send_sensor_config_message(switch_config); - if (switch_config->is_first_connection == false) - { - xTaskCreatePinnedToCore(&zh_send_sensor_status_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->sensor_status_message_task, tskNO_AFFINITY); - xTaskCreatePinnedToCore(&zh_send_sensor_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->sensor_attributes_message_task, tskNO_AFFINITY); - } - else + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) { vTaskResume(switch_config->sensor_status_message_task); vTaskResume(switch_config->sensor_attributes_message_task); @@ -619,6 +619,17 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve switch_config->hardware_config.sensor_pin = data->payload_data.config_message.switch_hardware_config_message.sensor_pin; switch_config->hardware_config.sensor_type = data->payload_data.config_message.switch_hardware_config_message.sensor_type; zh_save_config(switch_config); + switch_config->gateway_is_available = false; + if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) + { + vTaskDelete(switch_config->switch_attributes_message_task); + vTaskDelete(switch_config->switch_keep_alive_message_task); + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) + { + vTaskDelete(switch_config->sensor_attributes_message_task); + vTaskDelete(switch_config->sensor_status_message_task); + } + } data->device_type = ZHDT_SWITCH; data->payload_type = ZHPT_KEEP_ALIVE; data->payload_data.keep_alive_message.online_status = ZH_OFFLINE; @@ -628,6 +639,21 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve esp_restart(); break; case ZHPT_UPDATE:; + if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) + { + vTaskSuspend(switch_config->switch_attributes_message_task); + vTaskSuspend(switch_config->switch_keep_alive_message_task); + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) + { + vTaskSuspend(switch_config->sensor_attributes_message_task); + vTaskSuspend(switch_config->sensor_status_message_task); + } + } + data->device_type = ZHDT_SWITCH; + data->payload_type = ZHPT_KEEP_ALIVE; + data->payload_data.keep_alive_message.online_status = ZH_OFFLINE; + data->payload_data.keep_alive_message.message_frequency = ZH_SWITCH_KEEP_ALIVE_MESSAGE_FREQUENCY; + zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); const esp_app_desc_t *app_info = get_app_description(); switch_config->update_partition = esp_ota_get_next_update_partition(NULL); strcpy(data->payload_data.ota_message.espnow_ota_data.app_version, app_info->version); @@ -640,7 +666,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve #else strcpy(data->payload_data.ota_message.espnow_ota_data.app_name, app_info->project_name); #endif - data->device_type = ZHDT_SWITCH; data->payload_type = ZHPT_UPDATE; zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t)); break; @@ -667,6 +692,16 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve break; case ZHPT_UPDATE_ERROR: esp_ota_end(switch_config->update_handle); + if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) + { + vTaskResume(switch_config->switch_attributes_message_task); + vTaskResume(switch_config->switch_keep_alive_message_task); + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) + { + vTaskResume(switch_config->sensor_attributes_message_task); + vTaskResume(switch_config->sensor_status_message_task); + } + } break; case ZHPT_UPDATE_END: if (esp_ota_end(switch_config->update_handle) != ESP_OK) @@ -674,6 +709,16 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve data->device_type = ZHDT_SWITCH; data->payload_type = ZHPT_UPDATE_FAIL; zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t)); + if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) + { + vTaskResume(switch_config->switch_attributes_message_task); + vTaskResume(switch_config->switch_keep_alive_message_task); + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) + { + vTaskResume(switch_config->sensor_attributes_message_task); + vTaskResume(switch_config->sensor_status_message_task); + } + } break; } esp_ota_set_boot_partition(switch_config->update_partition); @@ -688,6 +733,16 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve esp_restart(); break; case ZHPT_RESTART: + if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) + { + vTaskDelete(switch_config->switch_attributes_message_task); + vTaskDelete(switch_config->switch_keep_alive_message_task); + if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE) + { + vTaskDelete(switch_config->sensor_attributes_message_task); + vTaskDelete(switch_config->sensor_status_message_task); + } + } data->device_type = ZHDT_SWITCH; data->payload_type = ZHPT_KEEP_ALIVE; data->payload_data.keep_alive_message.online_status = ZH_OFFLINE;