#include "Configurator.h" #include #include 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::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 callback) { shadeConfiguredCallbacks.push_back(callback); }