Version 1.0.3

Added checking device availability.
This commit is contained in:
Alexey Zholtikov 2024-06-10 18:46:44 +03:00
parent 079429fde0
commit 482903cecf
3 changed files with 78 additions and 4 deletions

View File

@ -75,6 +75,8 @@ void app_main(void)
gateway_config->espnow_ota_data_semaphore = xSemaphoreCreateBinary();
gateway_config->self_ota_in_progress_mutex = xSemaphoreCreateMutex();
gateway_config->espnow_ota_in_progress_mutex = xSemaphoreCreateMutex();
gateway_config->device_check_in_progress_mutex = xSemaphoreCreateMutex();
zh_vector_init(&gateway_config->available_device_vector, sizeof(available_device_t), false);
const esp_partition_t *running = esp_ota_get_running_partition();
esp_ota_img_states_t ota_state = {0};
esp_ota_get_state_partition(running, &ota_state);
@ -226,6 +228,24 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
zh_espnow_send_mqtt_json_attributes_message(data, recv_data->mac_addr, gateway_config);
break;
case ZHPT_KEEP_ALIVE:
if (xSemaphoreTake(gateway_config->device_check_in_progress_mutex, portTICK_PERIOD_MS) == pdTRUE)
{
for (uint16_t i = 0; i < zh_vector_get_size(&gateway_config->available_device_vector); ++i)
{
available_device_t *available_device = zh_vector_get_item(&gateway_config->available_device_vector, i);
if (memcmp(recv_data->mac_addr, available_device->mac_addr, 6) == 0)
{
zh_vector_delete_item(&gateway_config->available_device_vector, i);
}
}
available_device_t available_device = {0};
available_device.device_type = data->device_type;
memcpy(available_device.mac_addr, recv_data->mac_addr, 6);
available_device.frequency = data->payload_data.keep_alive_message.message_frequency;
available_device.time = esp_timer_get_time() / 1000000;
zh_vector_push_back(&gateway_config->available_device_vector, &available_device);
xSemaphoreGive(gateway_config->device_check_in_progress_mutex);
}
zh_espnow_send_mqtt_json_keep_alive_message(data, recv_data->mac_addr, gateway_config);
break;
case ZHPT_CONFIG:
@ -348,6 +368,7 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
zh_gateway_send_mqtt_json_config_message(gateway_config);
xTaskCreatePinnedToCore(&zh_gateway_send_mqtt_json_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, gateway_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&gateway_config->gateway_attributes_message_task, tskNO_AFFINITY);
xTaskCreatePinnedToCore(&zh_gateway_send_mqtt_json_keep_alive_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, gateway_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&gateway_config->gateway_keep_alive_message_task, tskNO_AFFINITY);
xTaskCreatePinnedToCore(&zh_device_availability_check_task, "NULL", ZH_CHECK_STACK_SIZE, gateway_config, ZH_CHECK_TASK_PRIORITY, (TaskHandle_t *)&gateway_config->device_availability_check_task, tskNO_AFFINITY);
}
gateway_config->mqtt_is_connected = true;
break;
@ -356,6 +377,7 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
{
vTaskDelete(gateway_config->gateway_attributes_message_task);
vTaskDelete(gateway_config->gateway_keep_alive_message_task);
vTaskDelete(gateway_config->device_availability_check_task);
zh_espnow_data_t data = {0};
data.device_type = ZHDT_GATEWAY;
data.payload_type = ZHPT_KEEP_ALIVE;
@ -868,6 +890,37 @@ void zh_send_espnow_current_time_task(void *pvParameter)
vTaskDelete(NULL);
}
void zh_device_availability_check_task(void *pvParameter)
{
gateway_config_t *gateway_config = pvParameter;
for (;;)
{
xSemaphoreTake(gateway_config->device_check_in_progress_mutex, portMAX_DELAY);
for (uint16_t i = 0; i < zh_vector_get_size(&gateway_config->available_device_vector); ++i)
{
CHECK:
available_device_t *available_device = zh_vector_get_item(&gateway_config->available_device_vector, i);
if (available_device == NULL)
{
break;
}
if (esp_timer_get_time() / 1000000 > available_device->time + (available_device->frequency * 3))
{
char *topic = (char *)heap_caps_malloc(strlen(CONFIG_MQTT_TOPIC_PREFIX) + strlen(zh_get_device_type_value_name(available_device->device_type)) + 27, MALLOC_CAP_8BIT);
memset(topic, 0, strlen(CONFIG_MQTT_TOPIC_PREFIX) + strlen(zh_get_device_type_value_name(available_device->device_type)) + 27);
sprintf(topic, "%s/%s/" MAC_STR "/status", CONFIG_MQTT_TOPIC_PREFIX, zh_get_device_type_value_name(available_device->device_type), MAC2STR(available_device->mac_addr));
esp_mqtt_client_publish(gateway_config->mqtt_client, topic, "offline", 0, 2, true);
heap_caps_free(topic);
zh_vector_delete_item(&gateway_config->available_device_vector, i);
goto CHECK; // Since the vector is shifted after item deletion, the item needs to be re-checked.
}
}
xSemaphoreGive(gateway_config->device_check_in_progress_mutex);
vTaskDelay(10000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
uint8_t zh_gpio_number_check(int gpio)
{
return (gpio >= 0 && gpio <= ZH_MAX_GPIO_NUMBERS) ? gpio : ZH_NOT_USED;

View File

@ -16,6 +16,7 @@
#include "esp_mac.h"
#include "mqtt_client.h"
#include "zh_json.h"
#include "zh_vector.h"
#include "zh_config.h"
#ifdef CONFIG_NETWORK_TYPE_DIRECT
@ -53,6 +54,8 @@
#define ZH_MESSAGE_STACK_SIZE 3072 // The stack size of the task of sending messages to the MQTT.
#define ZH_SNTP_TASK_PRIORITY 2 // Prioritize the task to get the current time.
#define ZH_SNTP_STACK_SIZE 2048 // The stack size of the task to get the current time.
#define ZH_CHECK_TASK_PRIORITY 2 // Prioritize the task to checking device availability.
#define ZH_CHECK_STACK_SIZE 2048 // The stack size of the task to checking device availability.
#define ZH_OTA_TASK_PRIORITY 3 // Prioritize the task of OTA updates.
#define ZH_OTA_STACK_SIZE 8192 // The stack size of the task of OTA updates.
@ -70,6 +73,7 @@ typedef struct // Structure of data exchange between tasks, functions and event
TaskHandle_t gateway_attributes_message_task; // Unique task handle for zh_gateway_send_mqtt_json_attributes_message_task().
TaskHandle_t gateway_keep_alive_message_task; // Unique task handle for zh_gateway_send_mqtt_json_keep_alive_message_task().
TaskHandle_t gateway_current_time_task; // Unique task handle for zh_send_espnow_current_time_task().
TaskHandle_t device_availability_check_task; // Unique task handle for zh_device_availability_check_task().
struct // Structure for initial transfer system data to the node update task.
{
zh_device_type_t device_type; // ESP-NOW device type.
@ -80,8 +84,18 @@ typedef struct // Structure of data exchange between tasks, functions and event
SemaphoreHandle_t espnow_ota_data_semaphore; // Semaphore for control the acknowledgement of successful receipt of an update package from a node.
SemaphoreHandle_t self_ota_in_progress_mutex; // Mutex blocking the second run of the gateway update task.
SemaphoreHandle_t espnow_ota_in_progress_mutex; // Mutex blocking the second run of the node update task.
SemaphoreHandle_t device_check_in_progress_mutex; // Mutex blocking the second access to the vector for struct for storing data about available nodes.
zh_vector_t available_device_vector; // Vector for struct for storing data about available nodes.
} gateway_config_t;
typedef struct // Struct for storing data about available nodes.
{
zh_device_type_t device_type; // ESP-NOW device type.
uint8_t mac_addr[6]; // ESP-NOW node MAC address.
uint8_t frequency; // Keep alive message frequency.
uint64_t time; // Last keep alive message time.
} available_device_t;
extern const uint8_t server_certificate_pem_start[] asm("_binary_certificate_pem_start");
extern const uint8_t server_certificate_pem_end[] asm("_binary_certificate_pem_end");
@ -137,6 +151,13 @@ void zh_espnow_ota_update_task(void *pvParameter);
*/
void zh_send_espnow_current_time_task(void *pvParameter);
/**
* @brief The task of checking device availability and sending a message to the MQTT broker in case of unavailability.
*
* @param[in,out] pvParameter Pointer to the structure of data exchange between tasks, functions and event handlers.
*/
void zh_device_availability_check_task(void *pvParameter);
/**
* @brief Function for checking the correctness of the GPIO number value.
*

View File

@ -1 +1 @@
1.0.2
1.0.3