From f04dc554dbf70bfa19bf01d3bf92e2bed5be7ba6 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Thu, 18 Jul 2024 11:10:03 +0300 Subject: [PATCH] Added offline message --- main/zh_espnow_switch.c | 69 ++++++++++++++++++++++++++++++----------- main/zh_espnow_switch.h | 1 + version.txt | 2 +- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/main/zh_espnow_switch.c b/main/zh_espnow_switch.c index fdac6b1..2e72d2c 100644 --- a/main/zh_espnow_switch.c +++ b/main/zh_espnow_switch.c @@ -505,8 +505,8 @@ void zh_send_sensor_status_message_task(void *pvParameter) else { data.payload_type = ZHPT_ERROR; - char *message = (char *)heap_caps_malloc(151, MALLOC_CAP_8BIT); - memset(message, 0, 151); + char *message = (char *)heap_caps_malloc(150, MALLOC_CAP_8BIT); + memset(message, 0, 150); sprintf(message, "Sensor %s reading error. Error - %s.", zh_get_sensor_type_value_name(switch_config->hardware_config.sensor_type), esp_err_to_name(err)); strcpy(data.payload_data.status_message.error_message.message, message); zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); @@ -555,13 +555,30 @@ 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); - 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->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); + 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); - 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); + 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 + { + vTaskResume(switch_config->sensor_status_message_task); + vTaskResume(switch_config->sensor_attributes_message_task); + } } } } @@ -573,12 +590,12 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve 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); + 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) { - vTaskDelete(switch_config->sensor_attributes_message_task); - vTaskDelete(switch_config->sensor_status_message_task); + vTaskSuspend(switch_config->sensor_attributes_message_task); + vTaskSuspend(switch_config->sensor_status_message_task); } } } @@ -602,6 +619,12 @@ 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); + 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)); + vTaskDelay(1000 / portTICK_PERIOD_MS); esp_restart(); break; case ZHPT_UPDATE:; @@ -657,10 +680,20 @@ 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_SUCCESS; zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t)); + 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)); vTaskDelay(1000 / portTICK_PERIOD_MS); esp_restart(); break; case ZHPT_RESTART: + 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)); + vTaskDelay(1000 / portTICK_PERIOD_MS); esp_restart(); break; default: @@ -681,12 +714,12 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve 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); + 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) { - vTaskDelete(switch_config->sensor_attributes_message_task); - vTaskDelete(switch_config->sensor_status_message_task); + vTaskSuspend(switch_config->sensor_attributes_message_task); + vTaskSuspend(switch_config->sensor_status_message_task); } } } @@ -702,12 +735,12 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve 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); + 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) { - vTaskDelete(switch_config->sensor_attributes_message_task); - vTaskDelete(switch_config->sensor_status_message_task); + vTaskSuspend(switch_config->sensor_attributes_message_task); + vTaskSuspend(switch_config->sensor_status_message_task); } } } diff --git a/main/zh_espnow_switch.h b/main/zh_espnow_switch.h index 7cad0d0..edced46 100644 --- a/main/zh_espnow_switch.h +++ b/main/zh_espnow_switch.h @@ -77,6 +77,7 @@ typedef struct // Structure of data exchange between tasks, functions and event } status; volatile bool gpio_processing; // GPIO processing flag. @note Used to prevent a repeated interrupt from triggering during GPIO processing. volatile bool gateway_is_available; // Gateway availability status flag. @note Used to control the tasks when the gateway connection is established / lost. + volatile bool is_first_connection; // First connection status flag. @note Used to control the tasks when the gateway connection is established / lost. uint8_t gateway_mac[6]; // Gateway MAC address. TaskHandle_t switch_attributes_message_task; // Unique task handle for zh_send_switsh_attributes_message_task(). TaskHandle_t switch_keep_alive_message_task; // Unique task handle for zh_send_switch_keep_alive_message_task(). diff --git a/version.txt b/version.txt index d180b0e..4639d66 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.4-1 \ No newline at end of file +1.0.4-2 \ No newline at end of file