diff --git a/README.md b/README.md index d32849f..059748a 100644 --- a/README.md +++ b/README.md @@ -365,7 +365,7 @@ You may listen for changes to state by registering an update handler callback. I ```cpp // register an update handler update_handler_id_t myUpdateHandler = lightStateService.addUpdateHandler( - [&](String originId) { + [&](const String& originId) { Serial.println("The light's state has been updated"); } ); @@ -588,7 +588,7 @@ Observe changes to the WiFiSettings: ```cpp esp8266React.getWiFiSettingsService()->addUpdateHandler( - [&](String originId) { + [&](const String& originId) { Serial.println("The WiFi Settings were updated!"); } ); diff --git a/lib/framework/APSettingsService.cpp b/lib/framework/APSettingsService.cpp index bab880b..b8ed252 100644 --- a/lib/framework/APSettingsService.cpp +++ b/lib/framework/APSettingsService.cpp @@ -8,9 +8,8 @@ APSettingsService::APSettingsService(AsyncWebServer* server, FS* fs, SecurityMan AP_SETTINGS_SERVICE_PATH, securityManager), _fsPersistence(APSettings::serialize, APSettings::deserialize, this, fs, AP_SETTINGS_FILE), - _lastManaged(0), _dnsServer(nullptr) { - addUpdateHandler([&](String originId) { reconfigureAP(); }, false); + addUpdateHandler([&](const String& originId) { reconfigureAP(); }, false); } void APSettingsService::begin() { @@ -47,11 +46,11 @@ void APSettingsService::manageAP() { } void APSettingsService::startAP() { - Serial.println("Starting software access point"); + Serial.println(F("Starting software access point")); WiFi.softAP(_state.ssid.c_str(), _state.password.c_str()); if (!_dnsServer) { IPAddress apIp = WiFi.softAPIP(); - Serial.print("Starting captive portal on "); + Serial.print(F("Starting captive portal on ")); Serial.println(apIp); _dnsServer = new DNSServer; _dnsServer->start(DNS_PORT, "*", apIp); @@ -60,12 +59,12 @@ void APSettingsService::startAP() { void APSettingsService::stopAP() { if (_dnsServer) { - Serial.println("Stopping captive portal"); + Serial.println(F("Stopping captive portal")); _dnsServer->stop(); delete _dnsServer; _dnsServer = nullptr; } - Serial.println("Stopping software access point"); + Serial.println(F("Stopping software access point")); WiFi.softAPdisconnect(true); } diff --git a/lib/framework/FSPersistence.h b/lib/framework/FSPersistence.h index 3d3daeb..152f5b2 100644 --- a/lib/framework/FSPersistence.h +++ b/lib/framework/FSPersistence.h @@ -32,7 +32,8 @@ class FSPersistence { DynamicJsonDocument jsonDocument = DynamicJsonDocument(MAX_FILE_SIZE); DeserializationError error = deserializeJson(jsonDocument, settingsFile); if (error == DeserializationError::Ok && jsonDocument.is()) { - updateSettings(jsonDocument.as()); + JsonObject jsonObject = jsonDocument.as(); + _statefulService->updateWithoutPropagation(jsonObject, _jsonDeserializer); settingsFile.close(); return; } @@ -74,7 +75,7 @@ class FSPersistence { void enableUpdateHandler() { if (!_updateHandlerId) { - _updateHandlerId = _statefulService->addUpdateHandler([&](String originId) { writeToFS(); }); + _updateHandlerId = _statefulService->addUpdateHandler([&](const String& originId) { writeToFS(); }); } } @@ -86,17 +87,13 @@ class FSPersistence { char const* _filePath; update_handler_id_t _updateHandlerId = 0; - // update the settings, but do not call propogate - void updateSettings(JsonObject root) { - _statefulService->updateWithoutPropagation(root, _jsonDeserializer); - } - protected: // We assume the deserializer supplies sensible defaults if an empty object // is supplied, this virtual function allows that to be changed. virtual void applyDefaults() { DynamicJsonDocument jsonDocument = DynamicJsonDocument(MAX_FILE_SIZE); - updateSettings(jsonDocument.to()); + JsonObject jsonObject = jsonDocument.as(); + _statefulService->updateWithoutPropagation(jsonObject, _jsonDeserializer); } }; diff --git a/lib/framework/JsonUtils.h b/lib/framework/JsonUtils.h index 74903d1..7d956b5 100644 --- a/lib/framework/JsonUtils.h +++ b/lib/framework/JsonUtils.h @@ -7,14 +7,14 @@ class JsonUtils { public: - static void readIP(JsonObject& root, String key, IPAddress& _ip) { - if (!root[key].is() || !_ip.fromString(root[key].as())) { - _ip = INADDR_NONE; + static void readIP(JsonObject& root, const String& key, IPAddress& ip) { + if (!root[key].is() || !ip.fromString(root[key].as())) { + ip = INADDR_NONE; } } - static void writeIP(JsonObject& root, String key, IPAddress& _ip) { - if (_ip != INADDR_NONE) { - root[key] = _ip.toString(); + static void writeIP(JsonObject& root, const String& key, const IPAddress& ip) { + if (ip != INADDR_NONE) { + root[key] = ip.toString(); } } }; diff --git a/lib/framework/MqttPubSub.h b/lib/framework/MqttPubSub.h index 2403a90..937063f 100644 --- a/lib/framework/MqttPubSub.h +++ b/lib/framework/MqttPubSub.h @@ -34,12 +34,12 @@ class MqttPub : virtual public MqttConnector { MqttPub(JsonSerializer jsonSerializer, StatefulService* statefulService, AsyncMqttClient* mqttClient, - String pubTopic = "") : + const String& pubTopic = "") : MqttConnector(statefulService, mqttClient), _jsonSerializer(jsonSerializer), _pubTopic(pubTopic) { - MqttConnector::_statefulService->addUpdateHandler([&](String originId) { publish(); }, false); + MqttConnector::_statefulService->addUpdateHandler([&](const String& originId) { publish(); }, false); } - void setPubTopic(String pubTopic) { + void setPubTopic(const String& pubTopic) { _pubTopic = pubTopic; publish(); } @@ -76,7 +76,7 @@ class MqttSub : virtual public MqttConnector { MqttSub(JsonDeserializer jsonDeserializer, StatefulService* statefulService, AsyncMqttClient* mqttClient, - String subTopic = "") : + const String& subTopic = "") : MqttConnector(statefulService, mqttClient), _jsonDeserializer(jsonDeserializer), _subTopic(subTopic) { MqttConnector::_mqttClient->onMessage(std::bind(&MqttSub::onMqttMessage, this, @@ -88,7 +88,7 @@ class MqttSub : virtual public MqttConnector { std::placeholders::_6)); } - void setSubTopic(String subTopic) { + void setSubTopic(const String& subTopic) { if (!_subTopic.equals(subTopic)) { // unsubscribe from the existing topic if one was set if (_subTopic.length() > 0) { @@ -143,15 +143,15 @@ class MqttPubSub : public MqttPub, public MqttSub { JsonDeserializer jsonDeserializer, StatefulService* statefulService, AsyncMqttClient* mqttClient, - String pubTopic = "", - String subTopic = "") : + const String& pubTopic = "", + const String& subTopic = "") : MqttConnector(statefulService, mqttClient), MqttPub(jsonSerializer, statefulService, mqttClient, pubTopic), MqttSub(jsonDeserializer, statefulService, mqttClient, subTopic) { } public: - void configureTopics(String pubTopic, String subTopic) { + void configureTopics(const String& pubTopic, const String& subTopic) { MqttSub::setSubTopic(subTopic); MqttPub::setPubTopic(pubTopic); } diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index a6ae82b..b367377 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -42,7 +42,7 @@ MqttSettingsService::MqttSettingsService(AsyncWebServer* server, FS* fs, Securit #endif _mqttClient.onConnect(std::bind(&MqttSettingsService::onMqttConnect, this, std::placeholders::_1)); _mqttClient.onDisconnect(std::bind(&MqttSettingsService::onMqttDisconnect, this, std::placeholders::_1)); - addUpdateHandler([&](String originId) { onConfigUpdated(); }, false); + addUpdateHandler([&](const String& originId) { onConfigUpdated(); }, false); } MqttSettingsService::~MqttSettingsService() { @@ -84,13 +84,16 @@ AsyncMqttClient* MqttSettingsService::getMqttClient() { } void MqttSettingsService::onMqttConnect(bool sessionPresent) { - Serial.print("Connected to MQTT, "); - Serial.print(sessionPresent ? "with" : "without"); - Serial.println(" persistent session"); + Serial.print(F("Connected to MQTT, ")); + if (sessionPresent) { + Serial.println(F("with persistent session")); + } else { + Serial.println(F("without persistent session")); + } } void MqttSettingsService::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { - Serial.print("Disconnected from MQTT reason: "); + Serial.print(F("Disconnected from MQTT reason: ")); Serial.println((uint8_t)reason); _disconnectReason = reason; _disconnectedAt = millis(); @@ -104,28 +107,28 @@ void MqttSettingsService::onConfigUpdated() { #ifdef ESP32 void MqttSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { if (_state.enabled) { - Serial.println("WiFi connection dropped, starting MQTT client."); + Serial.println(F("WiFi connection dropped, starting MQTT client.")); onConfigUpdated(); } } void MqttSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { if (_state.enabled) { - Serial.println("WiFi connection dropped, stopping MQTT client."); + Serial.println(F("WiFi connection dropped, stopping MQTT client.")); onConfigUpdated(); } } #elif defined(ESP8266) void MqttSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) { if (_state.enabled) { - Serial.println("WiFi connection dropped, starting MQTT client."); + Serial.println(F("WiFi connection dropped, starting MQTT client.")); onConfigUpdated(); } } void MqttSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { if (_state.enabled) { - Serial.println("WiFi connection dropped, stopping MQTT client."); + Serial.println(F("WiFi connection dropped, stopping MQTT client.")); onConfigUpdated(); } } @@ -137,7 +140,7 @@ void MqttSettingsService::configureMqtt() { // only connect if WiFi is connected and MQTT is enabled if (_state.enabled && WiFi.isConnected()) { - Serial.println("Connecting to MQTT..."); + Serial.println(F("Connecting to MQTT...")); _mqttClient.setServer(retainCstr(_state.host.c_str(), &_retainedHost), _state.port); if (_state.username.length() > 0) { _mqttClient.setCredentials( diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index e6394f1..baa8c9d 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -20,7 +20,7 @@ NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityM _onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1)); #endif - addUpdateHandler([&](String originId) { configureNTP(); }, false); + addUpdateHandler([&](const String& originId) { configureNTP(); }, false); } void NTPSettingsService::begin() { @@ -30,29 +30,29 @@ void NTPSettingsService::begin() { #ifdef ESP32 void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.println("Got IP address, starting NTP Synchronization"); + Serial.println(F("Got IP address, starting NTP Synchronization")); configureNTP(); } void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.println("WiFi connection dropped, stopping NTP."); + Serial.println(F("WiFi connection dropped, stopping NTP.")); configureNTP(); } #elif defined(ESP8266) void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) { - Serial.println("Got IP address, starting NTP Synchronization"); + Serial.println(F("Got IP address, starting NTP Synchronization")); configureNTP(); } void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { - Serial.println("WiFi connection dropped, stopping NTP."); + Serial.println(F("WiFi connection dropped, stopping NTP.")); configureNTP(); } #endif void NTPSettingsService::configureNTP() { if (WiFi.isConnected() && _state.enabled) { - Serial.println("Starting NTP..."); + Serial.println(F("Starting NTP...")); #ifdef ESP32 configTzTime(_state.tzFormat.c_str(), _state.server.c_str()); #elif defined(ESP8266) diff --git a/lib/framework/OTASettingsService.cpp b/lib/framework/OTASettingsService.cpp index 8f47fb6..f075cac 100644 --- a/lib/framework/OTASettingsService.cpp +++ b/lib/framework/OTASettingsService.cpp @@ -16,7 +16,7 @@ OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs, SecurityM _onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1)); #endif - addUpdateHandler([&](String originId) { configureArduinoOTA(); }, false); + addUpdateHandler([&](const String& originId) { configureArduinoOTA(); }, false); } void OTASettingsService::begin() { @@ -39,27 +39,27 @@ void OTASettingsService::configureArduinoOTA() { _arduinoOTA = nullptr; } if (_state.enabled) { - Serial.println("Starting OTA Update Service..."); + Serial.println(F("Starting OTA Update Service...")); _arduinoOTA = new ArduinoOTAClass; _arduinoOTA->setPort(_state.port); _arduinoOTA->setPassword(_state.password.c_str()); - _arduinoOTA->onStart([]() { Serial.println("Starting"); }); - _arduinoOTA->onEnd([]() { Serial.println("\nEnd"); }); + _arduinoOTA->onStart([]() { Serial.println(F("Starting")); }); + _arduinoOTA->onEnd([]() { Serial.println(F("\r\nEnd")); }); _arduinoOTA->onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + Serial.printf_P(PSTR("Progress: %u%%\r\n"), (progress / (total / 100))); }); _arduinoOTA->onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) - Serial.println("Auth Failed"); + Serial.println(F("Auth Failed")); else if (error == OTA_BEGIN_ERROR) - Serial.println("Begin Failed"); + Serial.println(F("Begin Failed")); else if (error == OTA_CONNECT_ERROR) - Serial.println("Connect Failed"); + Serial.println(F("Connect Failed")); else if (error == OTA_RECEIVE_ERROR) - Serial.println("Receive Failed"); + Serial.println(F("Receive Failed")); else if (error == OTA_END_ERROR) - Serial.println("End Failed"); + Serial.println(F("End Failed")); }); _arduinoOTA->begin(); } diff --git a/lib/framework/SecurityManager.h b/lib/framework/SecurityManager.h index 229ad4f..0133b6b 100644 --- a/lib/framework/SecurityManager.h +++ b/lib/framework/SecurityManager.h @@ -65,7 +65,7 @@ class SecurityManager { /* * Authenticate, returning the user if found */ - virtual Authentication authenticate(String& username, String& password) = 0; + virtual Authentication authenticate(const String& username, const String& password) = 0; /* * Check the request header for the Authorization token diff --git a/lib/framework/SecuritySettingsService.cpp b/lib/framework/SecuritySettingsService.cpp index 9fb68f0..30b6940 100644 --- a/lib/framework/SecuritySettingsService.cpp +++ b/lib/framework/SecuritySettingsService.cpp @@ -8,7 +8,7 @@ SecuritySettingsService::SecuritySettingsService(AsyncWebServer* server, FS* fs) SECURITY_SETTINGS_PATH, this), _fsPersistence(SecuritySettings::serialize, SecuritySettings::deserialize, this, fs, SECURITY_SETTINGS_FILE) { - addUpdateHandler([&](String originId) { configureJWTHandler(); }, false); + addUpdateHandler([&](const String& originId) { configureJWTHandler(); }, false); } void SecuritySettingsService::begin() { @@ -51,7 +51,7 @@ Authentication SecuritySettingsService::authenticateJWT(String& jwt) { return Authentication(); } -Authentication SecuritySettingsService::authenticate(String& username, String& password) { +Authentication SecuritySettingsService::authenticate(const String& username, const String& password) { for (User _user : _state.users) { if (_user.username == username && _user.password == password) { return Authentication(_user); diff --git a/lib/framework/SecuritySettingsService.h b/lib/framework/SecuritySettingsService.h index 2e0720c..5db6a94 100644 --- a/lib/framework/SecuritySettingsService.h +++ b/lib/framework/SecuritySettingsService.h @@ -67,7 +67,7 @@ class SecuritySettingsService : public StatefulService, public void begin(); // Functions to implement SecurityManager - Authentication authenticate(String& username, String& password); + Authentication authenticate(const String& username, const String& password); Authentication authenticateRequest(AsyncWebServerRequest* request); String generateJWT(User* user); ArRequestFilterFunction filterRequest(AuthenticationPredicate predicate); diff --git a/lib/framework/StatefulService.h b/lib/framework/StatefulService.h index 7a8f474..54a876f 100644 --- a/lib/framework/StatefulService.h +++ b/lib/framework/StatefulService.h @@ -13,7 +13,7 @@ #endif typedef size_t update_handler_id_t; -typedef std::function StateUpdateCallback; +typedef std::function StateUpdateCallback; static update_handler_id_t currentUpdatedHandlerId; typedef struct StateUpdateHandlerInfo { @@ -76,7 +76,7 @@ class StatefulService { #endif } - void update(std::function callback, String originId) { + void update(std::function callback, const String& originId) { #ifdef ESP32 xSemaphoreTakeRecursive(_accessMutex, portMAX_DELAY); #endif @@ -87,7 +87,7 @@ class StatefulService { #endif } - void update(JsonObject& jsonObject, JsonDeserializer deserializer, String originId) { + void update(JsonObject& jsonObject, JsonDeserializer deserializer, const String& originId) { #ifdef ESP32 xSemaphoreTakeRecursive(_accessMutex, portMAX_DELAY); #endif @@ -118,7 +118,7 @@ class StatefulService { #endif } - void callUpdateHandlers(String originId) { + void callUpdateHandlers(const String& originId) { for (const StateUpdateHandlerInfo_t& updateHandler : _updateHandlers) { updateHandler._cb(originId); } diff --git a/lib/framework/WebSocketTxRx.h b/lib/framework/WebSocketTxRx.h index 7f5faa4..c0a45dc 100644 --- a/lib/framework/WebSocketTxRx.h +++ b/lib/framework/WebSocketTxRx.h @@ -79,8 +79,8 @@ class WebSocketTx : virtual public WebSocketConnector { AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) : WebSocketConnector(statefulService, server, webSocketPath, securityManager, authenticationPredicate), _jsonSerializer(jsonSerializer) { - WebSocketConnector::_statefulService->addUpdateHandler([&](String originId) { transmitData(nullptr, originId); }, - false); + WebSocketConnector::_statefulService->addUpdateHandler( + [&](const String& originId) { transmitData(nullptr, originId); }, false); } WebSocketTx(JsonSerializer jsonSerializer, @@ -88,8 +88,8 @@ class WebSocketTx : virtual public WebSocketConnector { AsyncWebServer* server, char const* webSocketPath) : WebSocketConnector(statefulService, server, webSocketPath), _jsonSerializer(jsonSerializer) { - WebSocketConnector::_statefulService->addUpdateHandler([&](String originId) { transmitData(nullptr, originId); }, - false); + WebSocketConnector::_statefulService->addUpdateHandler( + [&](const String& originId) { transmitData(nullptr, originId); }, false); } protected: @@ -129,7 +129,7 @@ class WebSocketTx : virtual public WebSocketConnector { * Original implementation sent clients their own IDs so they could ignore updates they initiated. This approach * simplifies the client and the server implementation but may not be sufficent for all use-cases. */ - void transmitData(AsyncWebSocketClient* client, String originId) { + void transmitData(AsyncWebSocketClient* client, const String& originId) { DynamicJsonDocument jsonDocument = DynamicJsonDocument(WEB_SOCKET_MSG_SIZE); JsonObject root = jsonDocument.to(); root["type"] = "payload"; diff --git a/lib/framework/WiFiSettingsService.cpp b/lib/framework/WiFiSettingsService.cpp index 98637cd..42c96eb 100644 --- a/lib/framework/WiFiSettingsService.cpp +++ b/lib/framework/WiFiSettingsService.cpp @@ -31,7 +31,7 @@ WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, Securit std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); #endif - addUpdateHandler([&](String originId) { reconfigureWiFiConnection(); }, false); + addUpdateHandler([&](const String& originId) { reconfigureWiFiConnection(); }, false); } void WiFiSettingsService::begin() { @@ -68,7 +68,7 @@ void WiFiSettingsService::manageSTA() { } // Connect or reconnect as required if ((WiFi.getMode() & WIFI_STA) == 0) { - Serial.println("Connecting to WiFi."); + Serial.println(F("Connecting to WiFi.")); if (_state.staticIPConfig) { // configure for static IP WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); diff --git a/lib/framework/WiFiStatus.cpp b/lib/framework/WiFiStatus.cpp index 9971e01..de69542 100644 --- a/lib/framework/WiFiStatus.cpp +++ b/lib/framework/WiFiStatus.cpp @@ -18,36 +18,32 @@ WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager) #ifdef ESP32 void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.println("WiFi Connected."); + Serial.println(F("WiFi Connected.")); } void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.print("WiFi Disconnected. Reason code="); + Serial.print(F("WiFi Disconnected. Reason code=")); Serial.println(info.disconnected.reason); } void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.print("WiFi Got IP. localIP="); - Serial.print(WiFi.localIP().toString()); - Serial.print(", hostName="); - Serial.println(WiFi.getHostname()); + Serial.printf_P( + PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), WiFi.localIP().toString().c_str(), WiFi.getHostname()); } #elif defined(ESP8266) void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) { - Serial.print("WiFi Connected. SSID="); + Serial.print(F("WiFi Connected. SSID=")); Serial.println(event.ssid); } void WiFiStatus::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { - Serial.print("WiFi Disconnected. Reason code="); + Serial.print(F("WiFi Disconnected. Reason code=")); Serial.println(event.reason); } void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) { - Serial.print("WiFi Got IP. localIP="); - Serial.print(event.ip); - Serial.print(", hostName="); - Serial.println(WiFi.hostname()); + Serial.printf_P( + PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), event.ip.toString().c_str(), WiFi.hostname().c_str()); } #endif diff --git a/src/LightStateService.cpp b/src/LightStateService.cpp index b2dd251..a5c7f15 100644 --- a/src/LightStateService.cpp +++ b/src/LightStateService.cpp @@ -28,10 +28,10 @@ LightStateService::LightStateService(AsyncWebServer* server, _mqttClient->onConnect(std::bind(&LightStateService::registerConfig, this)); // configure update handler for when the light settings change - _lightMqttSettingsService->addUpdateHandler([&](String originId) { registerConfig(); }, false); + _lightMqttSettingsService->addUpdateHandler([&](const String& originId) { registerConfig(); }, false); // configure settings service update handler to update LED state - addUpdateHandler([&](String originId) { onConfigUpdated(); }, false); + addUpdateHandler([&](const String& originId) { onConfigUpdated(); }, false); } void LightStateService::begin() { @@ -48,14 +48,14 @@ void LightStateService::registerConfig() { return; } String configTopic; - String setTopic; - String stateTopic; + String subTopic; + String pubTopic; DynamicJsonDocument doc(256); _lightMqttSettingsService->read([&](LightMqttSettings& settings) { configTopic = settings.mqttPath + "/config"; - setTopic = settings.mqttPath + "/set"; - stateTopic = settings.mqttPath + "/state"; + subTopic = settings.mqttPath + "/set"; + pubTopic = settings.mqttPath + "/state"; doc["~"] = settings.mqttPath; doc["name"] = settings.name; doc["unique_id"] = settings.uniqueId; @@ -69,5 +69,5 @@ void LightStateService::registerConfig() { serializeJson(doc, payload); _mqttClient->publish(configTopic.c_str(), 0, false, payload.c_str()); - _mqttPubSub.configureTopics(stateTopic, setTopic); + _mqttPubSub.configureTopics(pubTopic, subTopic); }