Added led hardware config message processing

This commit is contained in:
2024-06-23 23:06:03 +03:00
parent 6552998f1a
commit 71591c95c7
2 changed files with 181 additions and 7 deletions

View File

@ -331,11 +331,13 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
zh_espnow_switch_send_mqtt_json_hardware_config_message(data, recv_data->mac_addr, gateway_config); zh_espnow_switch_send_mqtt_json_hardware_config_message(data, recv_data->mac_addr, gateway_config);
break; break;
case ZHDT_LED: case ZHDT_LED:
zh_espnow_led_send_mqtt_json_hardware_config_message(data, recv_data->mac_addr, gateway_config);
break; break;
case ZHDT_SENSOR: case ZHDT_SENSOR:
zh_espnow_sensor_send_mqtt_json_hardware_config_message(data, recv_data->mac_addr, gateway_config); zh_espnow_sensor_send_mqtt_json_hardware_config_message(data, recv_data->mac_addr, gateway_config);
break; break;
case ZHDT_BINARY_SENSOR: case ZHDT_BINARY_SENSOR:
zh_espnow_binary_sensor_send_mqtt_json_hardware_config_message(data, recv_data->mac_addr, gateway_config);
break; break;
default: default:
break; break;
@ -679,6 +681,46 @@ void zh_mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event
data.payload_type = ZHPT_RGB; data.payload_type = ZHPT_RGB;
zh_send_message(incoming_data_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); zh_send_message(incoming_data_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
break; break;
case ZHPT_HARDWARE:
char *extracted_hardware_data = strtok(incoming_payload, ","); // Extract led type value.
if (extracted_hardware_data == NULL)
{
break;
}
data.payload_data.config_message.led_hardware_config_message.led_type = zh_led_type_check(atoi(extracted_hardware_data));
extracted_hardware_data = strtok(incoming_payload, ","); // Extract first white gpio number value.
if (extracted_hardware_data == NULL)
{
break;
}
data.payload_data.config_message.led_hardware_config_message.first_white_pin = zh_gpio_number_check(atoi(extracted_hardware_data));
break;
extracted_hardware_data = strtok(NULL, ","); // Extract second white gpio number value.
if (extracted_hardware_data == NULL)
{
break;
}
data.payload_data.config_message.led_hardware_config_message.second_white_pin = zh_gpio_number_check(atoi(extracted_hardware_data));
extracted_hardware_data = strtok(NULL, ","); // Extract red gpio number value.
if (extracted_hardware_data == NULL)
{
break;
}
data.payload_data.config_message.led_hardware_config_message.red_pin = zh_gpio_number_check(atoi(extracted_hardware_data));
extracted_hardware_data = strtok(NULL, ","); // Extract green gpio number value.
if (extracted_hardware_data == NULL)
{
break;
}
data.payload_data.config_message.led_hardware_config_message.green_pin = zh_gpio_number_check(atoi(extracted_hardware_data));
extracted_hardware_data = strtok(NULL, ","); // Extract blue gpio number value.
if (extracted_hardware_data == NULL)
{
break;
}
data.payload_data.config_message.led_hardware_config_message.blue_pin = zh_gpio_number_check(atoi(extracted_hardware_data));
data.payload_type = ZHPT_HARDWARE;
zh_send_message(incoming_data_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
default: default:
break; break;
} }
@ -993,6 +1035,11 @@ uint8_t zh_sensor_type_check(int type)
return (type > HAST_NONE && type < HAST_MAX) ? type : HAST_NONE; return (type > HAST_NONE && type < HAST_MAX) ? type : HAST_NONE;
} }
uint8_t zh_led_type_check(int type)
{
return (type > HALT_NONE && type < HALT_MAX) ? type : HALT_NONE;
}
uint16_t zh_uint16_value_check(int value) uint16_t zh_uint16_value_check(int value)
{ {
return (value >= 0 && value <= 65536) ? value : 60; return (value >= 0 && value <= 65536) ? value : 60;
@ -1365,6 +1412,108 @@ void zh_espnow_led_send_mqtt_json_config_message(const zh_espnow_data_t *device_
heap_caps_free(topic); heap_caps_free(topic);
} }
void zh_espnow_led_send_mqtt_json_hardware_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config)
{
char *first_white_pin = (char *)heap_caps_malloc(4, MALLOC_CAP_8BIT);
memset(first_white_pin, 0, 4);
char *second_white_pin = (char *)heap_caps_malloc(4, MALLOC_CAP_8BIT);
memset(second_white_pin, 0, 4);
char *red_pin = (char *)heap_caps_malloc(4, MALLOC_CAP_8BIT);
memset(red_pin, 0, 4);
char *green_pin = (char *)heap_caps_malloc(4, MALLOC_CAP_8BIT);
memset(green_pin, 0, 4);
char *blue_pin = (char *)heap_caps_malloc(4, MALLOC_CAP_8BIT);
memset(blue_pin, 0, 4);
zh_json_t json = {0};
char buffer[512] = {0};
zh_json_init(&json);
if (device_data->payload_data.config_message.led_hardware_config_message.first_white_pin == ZH_NOT_USED || device_data->payload_data.config_message.led_hardware_config_message.red_pin == ZH_NOT_USED)
{
zh_json_add(&json, "Led type", "Not used");
}
else
{
switch (device_data->payload_data.config_message.led_hardware_config_message.led_type)
{
case HALT_W:
zh_json_add(&json, "Led type", "W");
break;
case HALT_WW:
zh_json_add(&json, "Led type", "WW");
break;
case HALT_RGB:
zh_json_add(&json, "Led type", "RGB");
break;
case HALT_RGBW:
zh_json_add(&json, "Led type", "RGBW");
break;
case HALT_RGBWW:
zh_json_add(&json, "Led type", "RGBWW");
break;
default:
zh_json_add(&json, "Led type", "Not used");
break;
}
}
if (device_data->payload_data.config_message.led_hardware_config_message.first_white_pin == ZH_NOT_USED)
{
zh_json_add(&json, "First white GPIO number", "Not used");
}
else
{
sprintf(first_white_pin, "%d", device_data->payload_data.config_message.led_hardware_config_message.first_white_pin);
zh_json_add(&json, "First white GPIO number", first_white_pin);
}
if (device_data->payload_data.config_message.led_hardware_config_message.second_white_pin == ZH_NOT_USED)
{
zh_json_add(&json, "Second white GPIO number", "Not used");
}
else
{
sprintf(second_white_pin, "%d", device_data->payload_data.config_message.led_hardware_config_message.second_white_pin);
zh_json_add(&json, "Second white GPIO number", second_white_pin);
}
if (device_data->payload_data.config_message.led_hardware_config_message.red_pin == ZH_NOT_USED)
{
zh_json_add(&json, "Red GPIO number", "Not used");
}
else
{
sprintf(red_pin, "%d", device_data->payload_data.config_message.led_hardware_config_message.red_pin);
zh_json_add(&json, "Red GPIO number", red_pin);
}
if (device_data->payload_data.config_message.led_hardware_config_message.green_pin == ZH_NOT_USED)
{
zh_json_add(&json, "Green GPIO number", "Not used");
}
else
{
sprintf(green_pin, "%d", device_data->payload_data.config_message.led_hardware_config_message.green_pin);
zh_json_add(&json, "Green GPIO number", green_pin);
}
if (device_data->payload_data.config_message.led_hardware_config_message.blue_pin == ZH_NOT_USED)
{
zh_json_add(&json, "Blue GPIO number", "Not used");
}
else
{
sprintf(blue_pin, "%d", device_data->payload_data.config_message.led_hardware_config_message.blue_pin);
zh_json_add(&json, "Blue GPIO number", blue_pin);
}
zh_json_create(&json, buffer);
zh_json_free(&json);
char *topic = (char *)heap_caps_malloc(strlen(gateway_config->software_config.mqtt_topic_prefix) + strlen(zh_get_device_type_value_name(device_data->device_type)) + 27, MALLOC_CAP_8BIT);
memset(topic, 0, strlen(gateway_config->software_config.mqtt_topic_prefix) + strlen(zh_get_device_type_value_name(device_data->device_type)) + 27);
sprintf(topic, "%s/%s/" MAC_STR "/config", gateway_config->software_config.mqtt_topic_prefix, zh_get_device_type_value_name(device_data->device_type), MAC2STR(device_mac));
esp_mqtt_client_publish(gateway_config->mqtt_client, topic, buffer, 0, 2, true);
heap_caps_free(first_white_pin);
heap_caps_free(second_white_pin);
heap_caps_free(red_pin);
heap_caps_free(green_pin);
heap_caps_free(blue_pin);
heap_caps_free(topic);
}
void zh_espnow_led_send_mqtt_json_status_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config) void zh_espnow_led_send_mqtt_json_status_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config)
{ {
char *brightness = (char *)heap_caps_malloc(4, MALLOC_CAP_8BIT); char *brightness = (char *)heap_caps_malloc(4, MALLOC_CAP_8BIT);
@ -1538,13 +1687,7 @@ void zh_espnow_sensor_send_mqtt_json_status_message(const zh_espnow_data_t *devi
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature); sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature);
zh_json_add(&json, "temperature", temperature); zh_json_add(&json, "temperature", temperature);
break; break;
case HAST_DHT11: case HAST_DHT:
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature);
sprintf(humidity, "%f", device_data->payload_data.status_message.sensor_status_message.humidity);
zh_json_add(&json, "temperature", temperature);
zh_json_add(&json, "humidity", humidity);
break;
case HAST_DHT22:
sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature); sprintf(temperature, "%f", device_data->payload_data.status_message.sensor_status_message.temperature);
sprintf(humidity, "%f", device_data->payload_data.status_message.sensor_status_message.humidity); sprintf(humidity, "%f", device_data->payload_data.status_message.sensor_status_message.humidity);
zh_json_add(&json, "temperature", temperature); zh_json_add(&json, "temperature", temperature);
@ -1631,6 +1774,10 @@ void zh_espnow_binary_sensor_send_mqtt_json_config_message(const zh_espnow_data_
heap_caps_free(topic); heap_caps_free(topic);
} }
void zh_espnow_binary_sensor_send_mqtt_json_hardware_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config)
{
}
void zh_espnow_binary_sensor_send_mqtt_json_status_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config) void zh_espnow_binary_sensor_send_mqtt_json_status_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config)
{ {
zh_json_t json = {0}; zh_json_t json = {0};

View File

@ -211,6 +211,15 @@ bool zh_bool_value_check(const int value);
*/ */
uint8_t zh_sensor_type_check(const int type); uint8_t zh_sensor_type_check(const int type);
/**
* @brief Function for checking the correctness of the led type value.
*
* @param type Value for check.
*
* @return Led type value or 0 if error
*/
uint8_t zh_led_type_check(const int type);
/** /**
* @brief Function for checking the correctness of the uint16_t variable value. * @brief Function for checking the correctness of the uint16_t variable value.
* *
@ -295,6 +304,15 @@ void zh_espnow_switch_send_mqtt_json_status_message(const zh_espnow_data_t *devi
*/ */
void zh_espnow_led_send_mqtt_json_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config); void zh_espnow_led_send_mqtt_json_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config);
/**
* @brief Function for converting to JSON and sending to the MQTT broker the hardware configuration message received from a zh_espnow_led node.
*
* @param[in] device_data Pointer to structure for data exchange between ESP-NOW devices.
* @param[in] device_mac Pointer to ESP-NOW node MAC address.
* @param[in] gateway_config Pointer to the structure of data exchange between tasks, functions and event handlers.
*/
void zh_espnow_led_send_mqtt_json_hardware_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config);
/** /**
* @brief Function for converting to JSON and sending to the MQTT broker the status message received from a zh_espnow_led node. * @brief Function for converting to JSON and sending to the MQTT broker the status message received from a zh_espnow_led node.
* *
@ -340,6 +358,15 @@ void zh_espnow_sensor_send_mqtt_json_status_message(const zh_espnow_data_t *devi
*/ */
void zh_espnow_binary_sensor_send_mqtt_json_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config); void zh_espnow_binary_sensor_send_mqtt_json_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config);
/**
* @brief Function for converting to JSON and sending to the MQTT broker the hardware configuration message received from a zh_espnow_binary_sensor node.
*
* @param[in] device_data Pointer to structure for data exchange between ESP-NOW devices.
* @param[in] device_mac Pointer to ESP-NOW node MAC address.
* @param[in] gateway_config Pointer to the structure of data exchange between tasks, functions and event handlers.
*/
void zh_espnow_binary_sensor_send_mqtt_json_hardware_config_message(const zh_espnow_data_t *device_data, const uint8_t *device_mac, const gateway_config_t *gateway_config);
/** /**
* @brief Function for converting to JSON and sending to the MQTT broker the status message received from a zh_espnow_binary_sensor node. * @brief Function for converting to JSON and sending to the MQTT broker the status message received from a zh_espnow_binary_sensor node.
* *