HASerializer class¶
-
class HASerializer¶
This class allows to create JSON objects easily. Its main purpose is to handle configuration of a device type that’s going to be published to the MQTT broker.
Public Types
-
enum EntryType¶
Type of the object’s entry.
Values:
-
enumerator UnknownEntryType¶
-
enumerator PropertyEntryType¶
-
enumerator TopicEntryType¶
-
enumerator FlagEntryType¶
-
enumerator UnknownEntryType¶
Public Functions
-
HASerializer(HABaseDeviceType *deviceType, const uint8_t maxEntriesNb)¶
Creates instance of the serializer for the given device type. Please note that the number JSON object’s entries needs to be known upfront. This approach reduces number of memory allocations.
- Parameters
deviceType – The device type that owns the serializer.
maxEntriesNb – Maximum number of the output object entries.
-
~HASerializer()¶
Frees the dynamic memory allocated by the class.
-
inline uint8_t getEntriesNb() const¶
Returns the number of items that were added to the serializer.
-
inline SerializerEntry *getEntries() const¶
Returns pointer to the serializer’s entries.
-
void set(const __FlashStringHelper *property, const void *value, PropertyValueType valueType = ConstCharPropertyValue)¶
Adds a new entry to the serialized with a type of
PropertyEntryType
.- Parameters
property – Pointer to the name of the property (progmem string).
value – Pointer to the value that’s being set.
valueType – The type of the value that’s passed to the method.
-
void set(const FlagType flag)¶
Adds a new entry to the serializer with a type of
FlagEntryType
.- Parameters
flag – Flag to add.
-
void topic(const __FlashStringHelper *topic)¶
Adds a new entry to the serialize with a type of
TopicEntryType
.- Parameters
topic – The topic name to add (progmem string).
-
uint16_t calculateSize() const¶
Calculates the output size of the serialized JSON object.
-
bool flush() const¶
Flushes the JSON object to the MQTT stream. Please note that this method only writes the MQTT payload. The MQTT session needs to be opened before.
Public Static Functions
-
static uint16_t calculateConfigTopicLength(const __FlashStringHelper *component, const char *objectId)¶
Calculates the size of a configuration topic for the given component and object ID. The configuration topic has structure as follows:
[discovery prefix]/[component]/[device ID]_[objectId]/config
- Parameters
component – The name of the HA component (e.g.
binary_sensor
).objectId – The unique ID of a device type that’s going to publish the config.
-
static bool generateConfigTopic(char *output, const __FlashStringHelper *component, const char *objectId)¶
Generates the configuration topic for the given component and object ID. The topic will be stored in the
output
variable.- Parameters
output – Buffer where the topic will be written.
component – The name of the HA component (e.g.
binary_sensor
).objectId – The unique ID of a device type that’s going to publish the config.
-
static uint16_t calculateDataTopicLength(const char *objectId, const __FlashStringHelper *topic)¶
Calculates the size of the given data topic for the given objectId. The data topic has structure as follows:
[data prefix]/[device ID]_[objectId]/[topic]
- Parameters
objectId – The unique ID of a device type that’s going to publish the data.
topic – The topic name (progmem string).
-
static bool generateDataTopic(char *output, const char *objectId, const __FlashStringHelper *topic)¶
Generates the data topic for the given object ID. The topic will be stored in the
output
variable.- Parameters
output – Buffer where the topic will be written.
objectId – The unique ID of a device type that’s going to publish the data.
topic – The topic name (progmem string).
-
static bool compareDataTopics(const char *actualTopic, const char *objectId, const __FlashStringHelper *topic)¶
Checks whether the given topic matches the data topic that can be generated using the given objectId and topicP. This method can be used to check if the received message matches some data topic.
- Parameters
actualTopic – The actual topic to compare.
objectId – The unique ID of a device type that may be the owner of the topic.
topic – The topic name (progmem string).
Private Functions
-
SerializerEntry *addEntry()¶
Creates a new entry in the serializer’s memory. If the limit of entries is hit, the nullptr is returned.
-
uint16_t calculateEntrySize(const SerializerEntry *entry) const¶
Calculates the serialized size of the given entry. Internally, this method recognizes the type of the entry and calls a proper calculate method listed below.
-
uint16_t calculateTopicEntrySize(const SerializerEntry *entry) const¶
Calculates the size of the entry of type
TopicEntryType
.
-
uint16_t calculateFlagSize(const FlagType flag) const¶
Calculates the size of the entry of type
FlagEntryType
.
-
uint16_t calculatePropertyValueSize(const SerializerEntry *entry) const¶
Calculates the size of the entry’s value if the entry is
PropertyEntryType
.
-
uint16_t calculateArraySize(const HASerializerArray *array) const¶
Calculates the size of the array if the property’s value is a type of
ArrayPropertyType
.
-
bool flushEntry(const SerializerEntry *entry) const¶
Flushes the given entry to the MQTT. Internally this method recognizes the type of the entry and calls a proper flush method listed below.
-
bool flushEntryValue(const SerializerEntry *entry) const¶
Flushes the value of the
PropertyEntryType
entry.
-
bool flushTopic(const SerializerEntry *entry) const¶
Flushes the entry of type
TopicEntryType
to the MQTT.
-
bool flushFlag(const SerializerEntry *entry) const¶
Flushes the entry of type
FlagEntryType
to the MQTT.
Private Members
-
HABaseDeviceType *_deviceType¶
Pointer to the device type that owns the serializer.
-
uint8_t _entriesNb¶
The number of entries added to the serializer.
-
uint8_t _maxEntriesNb¶
Maximum number of entries that can be added to the serializer.
-
SerializerEntry *_entries¶
Pointer to the serializer entries.
-
enum EntryType¶