69 lines
1.7 KiB
C++
69 lines
1.7 KiB
C++
#include "Configurator.h"
|
|
#include <cctype>
|
|
#include <ArduinoJson.h>
|
|
|
|
Configurator::Configurator() {
|
|
// TODO: Should be using MQTT directly here (and methods for handling methods should be private)
|
|
}
|
|
|
|
uint16_t Configurator::parseRadioId(const char* rawRadioId) {
|
|
if (!rawRadioId) {
|
|
return 0; // Or another appropriate error value/exception
|
|
}
|
|
|
|
uint16_t id = 0;
|
|
std::string radioId = std::string(rawRadioId);
|
|
|
|
if (radioId.size() != 4) {
|
|
return 0; // Invalid length
|
|
}
|
|
|
|
for (char c : radioId) {
|
|
if (!std::isxdigit(c)) {
|
|
return 0; // Invalid character, not a hex digit
|
|
}
|
|
|
|
int digit = std::toupper(c) - (c <= '9' ? '0' : 'A' - 10);
|
|
id = (id << 4) | digit;
|
|
}
|
|
|
|
return id;
|
|
}
|
|
|
|
void Configurator::processJson(std::string json) {
|
|
JsonDocument doc;
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
|
|
|
if (error) {
|
|
return;
|
|
}
|
|
|
|
JsonArray shades = doc["shades"];
|
|
|
|
for (JsonObject shade : shades) {
|
|
const char* rawRadioId = shade["radioId"];
|
|
const char* mqttId = shade["mqttId"];
|
|
const char* friendlyName = shade["friendly_name"];
|
|
|
|
if (rawRadioId && mqttId && friendlyName && std::char_traits<char>::length(mqttId) > 0) {
|
|
uint16_t id = parseRadioId(rawRadioId);
|
|
|
|
if (id != 0) {
|
|
Shade s{id, mqttId, friendlyName, "stopped", -1, -1};
|
|
for (const auto& callback : shadeConfiguredCallbacks) {
|
|
callback(s);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Invalid JSON - missing radioId, mqttId, or friendly_name"
|
|
}
|
|
}
|
|
}
|
|
|
|
void Configurator::addShadeConfiguredCallback(std::function<void (Shade)> callback) {
|
|
shadeConfiguredCallbacks.push_back(callback);
|
|
}
|