Skip to content

Commit e959003

Browse files
committed
fixed fs persistence
1 parent 38f5624 commit e959003

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

lib/framework/FSPersistence.h

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class FSPersistence {
6161
// serialize the data to the file
6262
serializeJson(jsonDocument, settingsFile);
6363
settingsFile.close();
64+
Serial.println("Config file written:");
65+
serializeJsonPretty(jsonDocument,Serial);
6466
return true;
6567
}
6668

lib/framework/StatefulService.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class StatefulService {
7676
StateUpdateResult result = stateUpdater(_state);
7777
endTransaction();
7878
if (result == StateUpdateResult::CHANGED) {
79+
Serial.println("State has changed, calling updatehandlers");
7980
callUpdateHandlers(originId);
8081
}
8182
return result;
@@ -91,10 +92,11 @@ class StatefulService {
9192
StateUpdateResult update(JsonObject& jsonObject, JsonStateUpdater<T> stateUpdater, const String& originId) {
9293
beginTransaction();
9394
StateUpdateResult result = stateUpdater(jsonObject, _state);
94-
endTransaction();
9595
if (result == StateUpdateResult::CHANGED) {
96+
Serial.println("State has changed, calling updatehandlers");
9697
callUpdateHandlers(originId);
9798
}
99+
endTransaction();
98100
return result;
99101
}
100102

@@ -117,8 +119,11 @@ class StatefulService {
117119
endTransaction();
118120
}
119121

122+
123+
120124
void callUpdateHandlers(const String& originId) {
121125
for (const StateUpdateHandlerInfo_t& updateHandler : _updateHandlers) {
126+
Serial.printf("calling handler from: %s\n",originId.c_str());
122127
updateHandler._cb(originId);
123128
}
124129
}

src/SensorSettingsService.cpp

+3-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <SensorSettingsService.h>
22

33
constexpr const char* SensorConfig::driverList[];
4+
StaticJsonDocument<1024> SensorConfig::jsonState;
45

56
SensorSettingsService::SensorSettingsService(AsyncWebServer* server, FS* fs,
67
SecurityManager* securityManager) :
@@ -22,44 +23,9 @@ SensorSettingsService::SensorSettingsService(AsyncWebServer* server, FS* fs,
2223

2324
// configure settings service update handler to update LED state
2425
addUpdateHandler([&](const String& originId) { onConfigUpdated(); }, false);
25-
}
26-
27-
28-
void SensorConfig::read(SensorConfig& settings, JsonObject& root){
29-
Serial.println("In read with root as");
30-
serializeJsonPretty(root,Serial);
31-
32-
File configFile = LITTLEFS.open(SENSOR_SETTINGS_FILE,"r");
33-
if (configFile) {
34-
DynamicJsonDocument jsonDocument = DynamicJsonDocument(4096);
35-
DeserializationError error = deserializeJson(jsonDocument, configFile);
36-
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
37-
root.set(jsonDocument.as<JsonObject>());
38-
Serial.println("Got conf from file in read:");
39-
serializeJsonPretty(jsonDocument,Serial);
40-
41-
configFile.close();
42-
return;
43-
}
44-
configFile.close();
45-
}
46-
else{
47-
Serial.println("error could not find settings file");
48-
}
4926

27+
}
5028

51-
root.remove("drivers");
52-
JsonArray driverJList = root.createNestedArray("drivers");
53-
for (const char* driver : driverList) {
54-
if (driver == NULL)
55-
continue;
56-
StaticJsonDocument<200> doc;
57-
deserializeJson(doc, driver);
58-
driverJList.add(doc);
59-
}
60-
Serial.println("root is now:");
61-
serializeJsonPretty(root,Serial);
62-
}
6329

6430

6531
void SensorSettingsService::begin() {
@@ -69,5 +35,6 @@ void SensorSettingsService::begin() {
6935

7036
void SensorSettingsService::onConfigUpdated() {
7137
// digitalWrite(LED_PIN, _state.ledOn ? LED_ON : LED_OFF);
38+
Serial.println("Writing conf.");
7239
}
7340

src/SensorSettingsService.h

+30-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class SensorConfig {
2525

2626
CSensor* sensorList[5];
2727

28+
static StaticJsonDocument<1024> jsonState;
29+
2830
// Add all sensors here
2931
static constexpr const char* driverList[] = {
3032
BMP180Sensor::description,
@@ -35,7 +37,6 @@ class SensorConfig {
3537
AnalogInSensor::description
3638
};
3739

38-
static void read(SensorConfig& settings, JsonObject& root);
3940

4041
static CSensor* getSensor(JsonObject& sensorConf) {
4142
Serial.println("Adding sensor with conf");
@@ -66,6 +67,8 @@ class SensorConfig {
6667

6768
// Received updated settings from file/UI and update JsonObject
6869
static StateUpdateResult update(JsonObject& root, SensorConfig& settings) {
70+
root.remove("drivers");
71+
jsonState.set(root);
6972
for (size_t i = 0; i < (sizeof(sensorList) / sizeof(CSensor*)); i++) {
7073
if (settings.sensorList[i] != NULL) {
7174
settings.sensorList[i]->end();
@@ -74,21 +77,44 @@ class SensorConfig {
7477
settings.sensorList[i] = NULL;
7578
}
7679
}
80+
Serial.println("jsonstate is now:");
81+
serializeJsonPretty(jsonState,Serial);
7782
int i = 0;
7883
Serial.println("Adding sensors from json conf:");
7984
JsonArray jsensors = root.getMember("sensors");
80-
serializeJsonPretty(jsensors,Serial);
81-
8285
if (jsensors.size() > 0) {
8386
for (JsonObject jsensor : jsensors) {
8487
settings.sensorList[i] = getSensor(jsensor);
8588
i++;
8689
}
8790
}
88-
91+
92+
Serial.println("RETURNING CHANGED");
8993
return StateUpdateResult::CHANGED;
9094

9195
}
96+
97+
98+
static void read(SensorConfig& settings, JsonObject& root){
99+
Serial.println("In read with root as");
100+
serializeJsonPretty(root,Serial);
101+
102+
root.set(jsonState.as<JsonObject>());
103+
104+
JsonArray driverJList = root.createNestedArray("drivers");
105+
for (const char* driver : driverList) {
106+
if (driver == NULL)
107+
continue;
108+
StaticJsonDocument<200> doc;
109+
deserializeJson(doc, driver);
110+
driverJList.add(doc);
111+
}
112+
Serial.println("root is now:");
113+
serializeJsonPretty(root,Serial);
114+
}
115+
116+
FSPersistence<SensorConfig>* _fsPersistence;
117+
92118
};
93119

94120
class SensorSettingsService : public StatefulService<SensorConfig> {

0 commit comments

Comments
 (0)