Skip to content

Commit 63efab0

Browse files
committed
Fix config
1 parent edb87fb commit 63efab0

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/ConfigEntry.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ class ConfigEntry : public MqttEntry {
1616
return get(key).getValue();
1717
}
1818

19+
void reset() {
20+
SPIFFS.remove("/config.cfg");
21+
}
22+
1923
protected:
2024
String getKey(MqttEntry* entry) {
2125
String key = entry->getTopic();
22-
key.replace(getTopic(), "");
26+
key.replace(getTopic() + "/", "");
2327
key.replace("/", ".");
2428
return key;
2529
}
@@ -45,12 +49,17 @@ class ConfigEntry : public MqttEntry {
4549
}
4650

4751
void store() {
52+
Serial.println("store");
4853
File f = SPIFFS.open("/config.cfg", "w");
4954
if (f) {
5055
each([&](MqttEntry* entry) {
51-
f.println(toString(entry).c_str());
56+
if(entry->getTopic() != getTopic()) {
57+
f.println(toString(entry).c_str());
58+
}
5259
});
5360
f.close();
61+
} else {
62+
Serial.println("Failed to store config");
5463
}
5564
}
5665
};

src/EasyMqtt.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class EasyMqtt : public MqttEntry {
2424
Handle connections to mqtt
2525
*/
2626
void mqttReconnect() {
27+
// if(WiFi.status() != WL_CONNECTED) {
2728
while (!mqttClient.connected()) {
2829
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password)) {
2930
debug("Connected to MQTT");
@@ -41,12 +42,17 @@ class EasyMqtt : public MqttEntry {
4142

4243
public:
4344
EasyMqtt() : MqttEntry("easyMqtt", mqttClient) {
45+
Serial.begin(115200);
4446
deviceId = String(ESP.getChipId());
4547

48+
debug("test");
4649
config = new ConfigEntry(mqttClient, *this);
4750
addChild(config);
4851

4952
get("system").setInterval(30);
53+
get("system")["deviceId"] << [this]() {
54+
return deviceId;
55+
};
5056
get("system")["mem"]["heap"] << []() {
5157
return String(ESP.getFreeHeap());
5258
};
@@ -59,6 +65,11 @@ class EasyMqtt : public MqttEntry {
5965
ESP.restart();
6066
}
6167
};
68+
get("system")["config"]["reset"] >> [this](String value) {
69+
if(value == "reset") {
70+
getConfig()->reset();
71+
}
72+
};
6273
}
6374

6475
ConfigEntry* getConfig() {

src/MqttEntry.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
class MqttEntry {
99
private:
10-
std::function<void(String payload)> outFunction = NULL;
10+
std::function<void(String)> outFunction = NULL;
1111
std::function<String()> inFunction = NULL;
12+
std::function<void(MqttEntry*, String)> publishFunction = NULL;
1213

1314
char* name = "N/A";
1415
int force = 0;

src/WebPortal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class WebPortal {
8181
}
8282
});
8383
page += FPSTR(HTML_FOOTER2);
84-
//page.replace("{device_id}", mqtt->getDeviceId());
84+
page.replace("{device_id}", mqtt->get("system/deviceId").getValue());
8585
page.replace("{topic}", mqtt->getTopic());
8686
webServer->send(200, "text/html", page);
8787
}

0 commit comments

Comments
 (0)