mirror of
https://github.com/mrcory/arduino-home-assistant.git
synced 2026-08-02 15:24:26 -04:00
update BaseDeviceType
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#include "BaseDeviceType.h"
|
||||
#include "../HAMqtt.h"
|
||||
#include "../HADevice.h"
|
||||
|
||||
const char* BaseDeviceType::ConfigTopic = "config";
|
||||
const char* BaseDeviceType::EventTopic = "event";
|
||||
|
||||
BaseDeviceType::BaseDeviceType(const char* uniqueId, HAMqtt& mqtt) :
|
||||
_uniqueId(uniqueId),
|
||||
_mqtt(mqtt)
|
||||
{
|
||||
_mqtt.addDeviceType(this);
|
||||
}
|
||||
|
||||
BaseDeviceType::~BaseDeviceType()
|
||||
{
|
||||
_mqtt.removeDeviceType(this);
|
||||
}
|
||||
|
||||
uint16_t BaseDeviceType::calculateTopicLength(
|
||||
const char* haNamespace,
|
||||
const char* type,
|
||||
const char* subtype,
|
||||
const char* suffix,
|
||||
bool includeNullTerminator
|
||||
) const
|
||||
{
|
||||
// [discovery prefix]/[namespace]/[device id - optional]/subtype_type/[suffix]
|
||||
const char* prefix = _mqtt.getDiscoveryPrefix();
|
||||
uint16_t size =
|
||||
strlen(prefix) + 1 + // with slash
|
||||
strlen(haNamespace) + 1 + // with slash
|
||||
strlen(type) + 1 + // with underscore
|
||||
strlen(subtype) + 1 + // with slash
|
||||
strlen(suffix); // with null terminator
|
||||
|
||||
if (_mqtt.getDevice() != nullptr) {
|
||||
size += strlen(_mqtt.getDevice()->getUniqueId()) + 1; // with slash
|
||||
}
|
||||
|
||||
if (includeNullTerminator) {
|
||||
size += 1;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
uint16_t BaseDeviceType::generateTopic(
|
||||
char* output,
|
||||
const char* haNamespace,
|
||||
const char* type,
|
||||
const char* subtype,
|
||||
const char* suffix
|
||||
) const
|
||||
{
|
||||
// prevent from wasting RAM
|
||||
static const char Slash[] PROGMEM = {"/"};
|
||||
static const char Underscore[] PROGMEM = {"_"};
|
||||
|
||||
const char* prefix = _mqtt.getDiscoveryPrefix();
|
||||
strcpy(output, prefix);
|
||||
strcat_P(output, Slash);
|
||||
strcat(output, haNamespace);
|
||||
strcat_P(output, Slash);
|
||||
|
||||
if (_mqtt.getDevice() != nullptr) {
|
||||
strcat(output, _mqtt.getDevice()->getUniqueId());
|
||||
strcat_P(output, Slash);
|
||||
}
|
||||
|
||||
strcat(output, subtype);
|
||||
strcat_P(output, Underscore);
|
||||
strcat(output, type);
|
||||
strcat_P(output, Slash);
|
||||
strcat(output, suffix);
|
||||
return strlen(output) + 1; // size with null terminator
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef BASEDEVICETYPE_H
|
||||
#define BASEDEVICETYPE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class HAMqtt;
|
||||
|
||||
class BaseDeviceType
|
||||
{
|
||||
public:
|
||||
static const char* ConfigTopic;
|
||||
static const char* EventTopic;
|
||||
|
||||
BaseDeviceType(const char* uniqueId, HAMqtt& mqtt);
|
||||
virtual ~BaseDeviceType();
|
||||
|
||||
virtual void onMqttConnected() = 0;
|
||||
|
||||
// to do: onMqttMessage
|
||||
|
||||
protected:
|
||||
inline HAMqtt* mqtt() const
|
||||
{ return &_mqtt; }
|
||||
|
||||
virtual uint16_t calculateTopicLength(
|
||||
const char* haNamespace,
|
||||
const char* type,
|
||||
const char* subtype,
|
||||
const char* suffix,
|
||||
bool includeNullTerminator = true
|
||||
) const final;
|
||||
|
||||
virtual uint16_t generateTopic(
|
||||
char* output,
|
||||
const char* haNamespace,
|
||||
const char* type,
|
||||
const char* subtype,
|
||||
const char* suffix
|
||||
) const final;
|
||||
|
||||
private:
|
||||
const char* _uniqueId;
|
||||
HAMqtt& _mqtt;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user