Apply updates alternative (#135)
* Rename "serialize" and "deserialize" functions to "read" and "update" to reflect API in StatefulService * Move new definitions to StatefulService.h so it is obvious it is not general purpose * Update README
This commit is contained in:
@ -1,14 +1,14 @@
|
||||
#include <LightMqttSettingsService.h>
|
||||
|
||||
LightMqttSettingsService::LightMqttSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
|
||||
_httpEndpoint(LightMqttSettings::serialize,
|
||||
LightMqttSettings::deserialize,
|
||||
_httpEndpoint(LightMqttSettings::read,
|
||||
LightMqttSettings::update,
|
||||
this,
|
||||
server,
|
||||
LIGHT_BROKER_SETTINGS_PATH,
|
||||
securityManager,
|
||||
AuthenticationPredicates::IS_AUTHENTICATED),
|
||||
_fsPersistence(LightMqttSettings::serialize, LightMqttSettings::deserialize, this, fs, LIGHT_BROKER_SETTINGS_FILE) {
|
||||
_fsPersistence(LightMqttSettings::read, LightMqttSettings::update, this, fs, LIGHT_BROKER_SETTINGS_FILE) {
|
||||
}
|
||||
|
||||
void LightMqttSettingsService::begin() {
|
||||
|
@ -14,16 +14,17 @@ class LightMqttSettings {
|
||||
String name;
|
||||
String uniqueId;
|
||||
|
||||
static void serialize(LightMqttSettings& settings, JsonObject& root) {
|
||||
static void read(LightMqttSettings& settings, JsonObject& root) {
|
||||
root["mqtt_path"] = settings.mqttPath;
|
||||
root["name"] = settings.name;
|
||||
root["unique_id"] = settings.uniqueId;
|
||||
}
|
||||
|
||||
static void deserialize(JsonObject& root, LightMqttSettings& settings) {
|
||||
static StateUpdateResult update(JsonObject& root, LightMqttSettings& settings) {
|
||||
settings.mqttPath = root["mqtt_path"] | ESPUtils::defaultDeviceValue("homeassistant/light/");
|
||||
settings.name = root["name"] | ESPUtils::defaultDeviceValue("light-");
|
||||
settings.uniqueId = root["unique_id"] | ESPUtils::defaultDeviceValue("light-");
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -4,16 +4,16 @@ LightStateService::LightStateService(AsyncWebServer* server,
|
||||
SecurityManager* securityManager,
|
||||
AsyncMqttClient* mqttClient,
|
||||
LightMqttSettingsService* lightMqttSettingsService) :
|
||||
_httpEndpoint(LightState::serialize,
|
||||
LightState::deserialize,
|
||||
_httpEndpoint(LightState::read,
|
||||
LightState::update,
|
||||
this,
|
||||
server,
|
||||
LIGHT_SETTINGS_ENDPOINT_PATH,
|
||||
securityManager,
|
||||
AuthenticationPredicates::IS_AUTHENTICATED),
|
||||
_mqttPubSub(LightState::haSerialize, LightState::haDeserialize, this, mqttClient),
|
||||
_webSocket(LightState::serialize,
|
||||
LightState::deserialize,
|
||||
_mqttPubSub(LightState::haRead, LightState::haUpdate, this, mqttClient),
|
||||
_webSocket(LightState::read,
|
||||
LightState::update,
|
||||
this,
|
||||
server,
|
||||
LIGHT_SETTINGS_SOCKET_PATH,
|
||||
@ -40,6 +40,7 @@ void LightStateService::begin() {
|
||||
}
|
||||
|
||||
void LightStateService::onConfigUpdated() {
|
||||
Serial.printf_P(PSTR("The light is now: %s\r\n"), _state.ledOn ? "on" : "off");
|
||||
digitalWrite(BLINK_LED, _state.ledOn ? LED_ON : LED_OFF);
|
||||
}
|
||||
|
||||
|
@ -31,21 +31,38 @@ class LightState {
|
||||
public:
|
||||
bool ledOn;
|
||||
|
||||
static void serialize(LightState& settings, JsonObject& root) {
|
||||
static void read(LightState& settings, JsonObject& root) {
|
||||
root["led_on"] = settings.ledOn;
|
||||
}
|
||||
|
||||
static void deserialize(JsonObject& root, LightState& settings) {
|
||||
settings.ledOn = root["led_on"] | DEFAULT_LED_STATE;
|
||||
static StateUpdateResult update(JsonObject& root, LightState& lightState) {
|
||||
boolean newState = root["led_on"] | DEFAULT_LED_STATE;
|
||||
if (lightState.ledOn != newState) {
|
||||
lightState.ledOn = newState;
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
return StateUpdateResult::UNCHANGED;
|
||||
}
|
||||
|
||||
static void haSerialize(LightState& settings, JsonObject& root) {
|
||||
static void haRead(LightState& settings, JsonObject& root) {
|
||||
root["state"] = settings.ledOn ? ON_STATE : OFF_STATE;
|
||||
}
|
||||
|
||||
static void haDeserialize(JsonObject& root, LightState& settings) {
|
||||
static StateUpdateResult haUpdate(JsonObject& root, LightState& lightState) {
|
||||
String state = root["state"];
|
||||
settings.ledOn = strcmp(ON_STATE, state.c_str()) ? false : true;
|
||||
// parse new led state
|
||||
boolean newState = false;
|
||||
if (state.equals(ON_STATE)) {
|
||||
newState = true;
|
||||
} else if (!state.equals(OFF_STATE)) {
|
||||
return StateUpdateResult::ERROR;
|
||||
}
|
||||
// change the new state, if required
|
||||
if (lightState.ledOn != newState) {
|
||||
lightState.ledOn = newState;
|
||||
return StateUpdateResult::CHANGED;
|
||||
}
|
||||
return StateUpdateResult::UNCHANGED;
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user