Reworked storage for JSON generation

This commit is contained in:
Indu Prakash
2022-10-04 19:36:13 -05:00
parent 91f1c66782
commit 684a175a3c
3 changed files with 64 additions and 61 deletions

View File

@@ -1,23 +1,35 @@
#include "new_http.h"
#include "../cJSON/cJSON.h"
#include "../new_pins.h"
#include "../mqtt/new_mqtt.h"
typedef enum {
ENTITY_RELAY = 0,
ENTITY_LIGHT = 1
} ENTITY_TYPE;
//unique_id is based on CFG_GetDeviceName() whose size is CGF_DEVICE_NAME_SIZE (see hass_populate_unique_id)
#define HASS_UNIQUE_ID_SIZE (CGF_DEVICE_NAME_SIZE + 1 + 5 + 1 + 4)
//channel is based on unique_id (see hass_populate_device_config_channel)
#define HASS_CHANNEL_SIZE (HASS_UNIQUE_ID_SIZE + 32)
//Size of JSON (1 less than MQTT queue holding)
#define HASS_JSON_SIZE (MQTT_PUBLISH_ITEM_VALUE_LENGTH - 1)
/// @brief HomeAssistant device discovery information
typedef struct HassDeviceInfo_s{
char *unique_id;
char *channel;
char *json;
char unique_id[HASS_UNIQUE_ID_SIZE];
char channel[HASS_CHANNEL_SIZE];
char json[HASS_JSON_SIZE];
cJSON *root;
cJSON *device;
cJSON *ids;
} HassDeviceInfo;
char *hass_build_unique_id(ENTITY_TYPE type, int index);
char *hass_build_unique_id(ENTITY_TYPE type, int index);
char *hass_build_discovery_json(HassDeviceInfo *info);
void hass_print_unique_id(http_request_t *request, const char *fmt, ENTITY_TYPE type, int index);
HassDeviceInfo *hass_init_device_info(ENTITY_TYPE type, int index, char *payload_on, char *payload_off);
char *hass_build_discovery_json(HassDeviceInfo *info);
void hass_free_device_info(HassDeviceInfo *info);