improve library API

This commit is contained in:
Dawid Chyrzynski
2020-12-05 00:23:07 +01:00
parent 3c9cf1005e
commit b4697ca641
6 changed files with 84 additions and 21 deletions
+15 -4
View File
@@ -1,13 +1,24 @@
#include <Arduino.h>
#include "HADevice.h"
#include "HAUtils.h"
#define HADEVICE_INIT \
_manufacturer(nullptr), \
_model(nullptr), \
_name(nullptr), \
_softwareVersion(nullptr)
HADevice::HADevice(const char* uniqueId) :
_uniqueId(uniqueId),
_manufacturer(nullptr),
_model(nullptr),
_name(nullptr),
_softwareVersion(nullptr)
HADEVICE_INIT
{
}
HADevice::HADevice(const byte* uniqueId, const uint16_t& length) :
_uniqueId(HAUtils::byteArrayToStr(uniqueId, length)),
HADEVICE_INIT
{
}
+1
View File
@@ -7,6 +7,7 @@ class HADevice
{
public:
HADevice(const char* uniqueId);
HADevice(const byte* uniqueId, const uint16_t& length);
inline const char* getUniqueId() const
{ return _uniqueId; }
+23 -13
View File
@@ -6,6 +6,21 @@
#include "ArduinoHADefines.h"
#include "device-types/BaseDeviceType.h"
#define HAMQTT_INIT \
_netClient(netClient), \
_device(device), \
_hasDevice(true), \
_initialized(false), \
_discoveryPrefix(DefaultDiscoveryPrefix), \
_mqtt(new PubSubClient(netClient)), \
_serverIp(new IPAddress()), \
_serverPort(0), \
_username(nullptr), \
_password(nullptr), \
_lastConnectionAttemptAt(0), \
_devicesTypesNb(0), \
_devicesTypes(nullptr)
static const char* DefaultDiscoveryPrefix = "homeassistant";
HAMqtt* instance = nullptr;
@@ -18,21 +33,16 @@ void onMessageReceived(char* topic, uint8_t* payload, uint16_t length)
instance->processMessage(topic, payload, length);
}
HAMqtt::HAMqtt(Client& netClient, HADevice& device) :
_clientId(device.getUniqueId()),
HAMQTT_INIT
{
instance = this;
}
HAMqtt::HAMqtt(const char* clientId, Client& netClient, HADevice& device) :
_clientId(clientId),
_netClient(netClient),
_device(device),
_hasDevice(true),
_initialized(false),
_discoveryPrefix(DefaultDiscoveryPrefix),
_mqtt(new PubSubClient(netClient)),
_serverIp(new IPAddress()),
_serverPort(0),
_username(nullptr),
_password(nullptr),
_lastConnectionAttemptAt(0),
_devicesTypesNb(0),
_devicesTypes(nullptr)
HAMQTT_INIT
{
instance = this;
}
+1
View File
@@ -14,6 +14,7 @@ class HAMqtt
public:
static const uint16_t ReconnectInterval = 5000; // ms
HAMqtt(Client& netClient, HADevice& device);
HAMqtt(const char* clientId, Client& netClient, HADevice& device);
/**
+31 -3
View File
@@ -2,14 +2,14 @@
#include "HAUtils.h"
bool HAUtils::endsWith(const char *str, const char *suffix)
bool HAUtils::endsWith(const char* str, const char* suffix)
{
if (str == nullptr || suffix == nullptr) {
return false;
}
uint16_t lenstr = strlen(str);
uint16_t lensuffix = strlen(suffix);
const uint16_t& lenstr = strlen(str);
const uint16_t& lensuffix = strlen(suffix);
if (lensuffix > lenstr) {
return false;
@@ -17,3 +17,31 @@ bool HAUtils::endsWith(const char *str, const char *suffix)
return (strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0);
}
void HAUtils::byteArrayToStr(
char* dst,
const byte* src,
const uint16_t& length
)
{
const uint16_t& finalLength = (length * 2);
const char* map = "0123456789abcdef"; // todo: move to progmem
for (uint8_t i = 0; i < length; i++) {
dst[i*2] = map[((char)src[i] & 0XF0) >> 4];
dst[i*2+1] = map[((char)src[i] & 0x0F)];
}
dst[finalLength] = '\0';
}
char* HAUtils::byteArrayToStr(
const byte* src,
const uint16_t& length
)
{
char* dst = malloc((length * 2) + 1); // include null terminator
byteArrayToStr(dst, src, length);
return dst;
}
+13 -1
View File
@@ -4,7 +4,19 @@
class HAUtils
{
public:
static bool endsWith(const char *str, const char *suffix);
static bool endsWith(
const char* str,
const char* suffi
);
static void byteArrayToStr(
char* dst,
const byte* src,
const uint16_t& length
);
static char* byteArrayToStr(
const byte* src,
const uint16_t& length
);
};
#endif