Skip to content

Commit edb87fb

Browse files
committed
Add config support
1 parent 54af296 commit edb87fb

File tree

4 files changed

+105
-88
lines changed

4 files changed

+105
-88
lines changed

src/ConfigEntry.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef ConfigEntry_h
2+
#define ConfigEntry_h
3+
4+
#include "FS.h" // for SPIFFS
5+
#include "MqttEntry.h"
6+
7+
class ConfigEntry : public MqttEntry {
8+
public:
9+
ConfigEntry(PubSubClient& mqttClient, MqttEntry& parent) : MqttEntry("config", mqttClient, parent) {
10+
SPIFFS.begin();
11+
load();
12+
}
13+
14+
String getString(String key, String defaultValue) {
15+
key.replace(".", "/");
16+
return get(key).getValue();
17+
}
18+
19+
protected:
20+
String getKey(MqttEntry* entry) {
21+
String key = entry->getTopic();
22+
key.replace(getTopic(), "");
23+
key.replace("/", ".");
24+
return key;
25+
}
26+
27+
String toString(MqttEntry* entry) {
28+
return getKey(entry) + "=" + entry->getValue();
29+
}
30+
31+
private:
32+
void load() {
33+
File f = SPIFFS.open("/config.cfg", "r");
34+
if (f) {
35+
while(f.available()) {
36+
String line = f.readStringUntil('\n');
37+
int pos = line.indexOf("=");
38+
String key = line.substring(0, pos);
39+
String value = line.substring(pos+1);
40+
key.replace(".", "/");
41+
get(key).setValue(value);
42+
}
43+
f.close();
44+
}
45+
}
46+
47+
void store() {
48+
File f = SPIFFS.open("/config.cfg", "w");
49+
if (f) {
50+
each([&](MqttEntry* entry) {
51+
f.println(toString(entry).c_str());
52+
});
53+
f.close();
54+
}
55+
}
56+
};
57+
58+
#endif

src/EasyMqtt.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
#include <PubSubClient.h>
66
#include "MqttEntry.h"
77
#include "WebPortal.h"
8+
#include "ConfigEntry.h"
89

910
class EasyMqtt : public MqttEntry {
1011
private:
1112
WiFiClient wifiClient;
1213
PubSubClient mqttClient;
1314
WebPortal webPortal;
1415

16+
ConfigEntry* config;
17+
1518
String deviceId = "deviceId";
1619
const char* mqtt_username = "N/A";
1720
const char* mqtt_password = "N/A";
@@ -40,6 +43,9 @@ class EasyMqtt : public MqttEntry {
4043
EasyMqtt() : MqttEntry("easyMqtt", mqttClient) {
4144
deviceId = String(ESP.getChipId());
4245

46+
config = new ConfigEntry(mqttClient, *this);
47+
addChild(config);
48+
4349
get("system").setInterval(30);
4450
get("system")["mem"]["heap"] << []() {
4551
return String(ESP.getFreeHeap());
@@ -55,6 +61,10 @@ class EasyMqtt : public MqttEntry {
5561
};
5662
}
5763

64+
ConfigEntry* getConfig() {
65+
return config;
66+
}
67+
5868
void debug(String msg) {
5969
#ifdef DEBUG
6070
Serial.println(msg);

src/MqttConfigEntry.h

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/MqttEntry.h

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class MqttEntry {
1111
std::function<String()> inFunction = NULL;
1212

1313
char* name = "N/A";
14-
int interval = -1;
1514
int force = 0;
15+
int interval = -1;
16+
int forceUpdate = -1;
1617
unsigned long lastUpdate = 0; // Last read of data
1718
String lastValue = "";
1819

@@ -29,17 +30,29 @@ class MqttEntry {
2930

3031
protected:
3132
MqttEntry(const char* name, PubSubClient& mqttClient, MqttEntry& parent) {
32-
MqttEntry::parent = &parent;
33+
setParent(parent);
3334
client = &mqttClient;
34-
interval = -1;
3535
setName(name);
3636
}
3737

3838
MqttEntry(const char* name, PubSubClient& mqttClient) {
39-
client = &mqttClient;
4039
interval = 5;
40+
forceUpdate = 10;
41+
client = &mqttClient;
4142
setName(name);
4243
}
44+
45+
void setParent(MqttEntry& parent) {
46+
MqttEntry::parent = &parent;
47+
}
48+
49+
MqttEntry* addChild(MqttEntry* child) {
50+
child->setParent(*this);
51+
MqttEntry * oldChild = children;
52+
children = child;
53+
children->next = oldChild;
54+
return child;
55+
}
4356

4457
public:
4558
void callback(const char* topic, uint8_t* payload, unsigned int length) {
@@ -61,15 +74,13 @@ class MqttEntry {
6174
if (isIn()) {
6275
unsigned long time = millis();
6376
if (time >= (lastUpdate + (getInterval() * 1000))) {
77+
force++;
6478
lastUpdate = time;
6579
String value = inFunction();
6680
if (value != "") {
67-
if(value != lastValue || force >= 10) {
68-
publish(value);
69-
lastValue = value;
81+
if (value != lastValue || force > getForce()) {
82+
setValue(value);
7083
force = 0;
71-
} else {
72-
force++;
7384
}
7485
}
7586
}
@@ -113,13 +124,29 @@ class MqttEntry {
113124
MqttEntry::interval = interval;
114125
}
115126

127+
int getForce() {
128+
if(forceUpdate < 0) {
129+
return parent->getForce();
130+
}
131+
return forceUpdate;
132+
}
133+
134+
void setForce(int force) {
135+
forceUpdate = force;
136+
}
137+
116138
/**
117139
* Get last value
118140
*/
119141
String getValue() {
120142
return lastValue;
121143
}
122144

145+
void setValue(String value) {
146+
lastValue = value;
147+
publish(value);
148+
}
149+
123150
/**
124151
*
125152
*/
@@ -169,10 +196,7 @@ class MqttEntry {
169196
}
170197
child = child->next;
171198
}
172-
MqttEntry * oldChild = children;
173-
children = new MqttEntry(name, *client, *this);
174-
children->next = oldChild;
175-
return *children;
199+
return *addChild(new MqttEntry(name, *client, *this));
176200
}
177201

178202
/**

0 commit comments

Comments
 (0)