Skip to content

Commit 63f0ab2

Browse files
author
Bjarne Loft
committed
Make everything avalible from the rest interface, and add support for persisting values
1 parent ff541e5 commit 63f0ab2

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/EasyMqtt.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
119119
return deviceId;
120120
};
121121
get("$system")["mem"]["heap"] << []() {
122-
return String(ESP.getFreeHeap());
122+
return ESP.getFreeHeap();
123123
};
124124
get("$system")["uptime"] << []() {
125-
return String(millis() / 1000);
125+
return millis() / 1000;
126126
};
127127
get("$system")["wifi"]["rssi"] << []() {
128-
return String(WiFi.RSSI());
128+
return WiFi.RSSI();
129129
};
130130
get("$system")["wifi"]["quality"] << []() {
131131
int quality = (WiFi.RSSI()+100)*1.6667;
132132
if(quality < 0) quality = 0;
133133
if(quality > 100) quality = 100;
134-
return String(quality);
134+
return quality;
135135
};
136136
get("$system")["wifi"]["ssid"] << []() {
137137
return WiFi.SSID();
@@ -146,7 +146,7 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
146146
get("$system")["time"].setInterval(900); // every 15 min
147147
get("$system")["time"] << [this]() {
148148
ntp().update();
149-
return String(ntp().getTime());
149+
return ntp().getTime();
150150
};
151151

152152
get("$system")["restart"] >> [this](String value) {

src/Entry.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Entry.h"
2+
#include <FS.h>
23

34
std::function<void(Entry*, String)> Entry::getPublishFunction() {
45
if(publishFunction == NULL && parent) {
@@ -154,6 +155,17 @@ void Entry::setInterval(int interval, int force) {
154155
forceUpdate = force;
155156
}
156157

158+
void Entry::setPersist(bool persist) {
159+
this->persist = persist;
160+
if(persist) {
161+
File f = SPIFFS.open(getTopic(), "r");
162+
if (f) {
163+
setValue(f.readStringUntil('\n').c_str(), true);
164+
f.close();
165+
}
166+
}
167+
}
168+
157169
int Entry::getForce() {
158170
if(forceUpdate < 0) {
159171
return parent->getForce();
@@ -174,6 +186,12 @@ bool Entry::setValue(const char *value, bool force) {
174186
lastValue = (char*)malloc(strlen(value)+1);
175187
strcpy(lastValue, value);
176188
publish(value);
189+
190+
if(persist) {
191+
File f = SPIFFS.open(getTopic(), "w");
192+
f.println(value);
193+
f.close();
194+
}
177195
return true;
178196
}
179197
return false;

src/Entry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Entry {
1616
int forceUpdate = -1;
1717
unsigned long lastUpdate = 0;
1818
char* lastValue = NULL;
19+
bool persist = false;
1920

2021
Entry* parent = NULL;
2122
Entry* next = NULL;
@@ -57,6 +58,8 @@ class Entry {
5758
void setInterval(int interval);
5859
void setInterval(int interval, int force);
5960

61+
void setPersist(bool persist);
62+
6063
int getForce();
6164

6265
/**

src/WebPortal.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ void WebPortal::setup(Entry *mqttEntry, Config *config, NTPClient *ntpClient) {
2727
webServer->on("/", std::bind(&WebPortal::handleRoot, this));
2828
webServer->on("/save", std::bind(&WebPortal::handleSaveConfig, this));
2929
mqtt->each([&](Entry* entry) {
30-
if(entry->isIn() || entry->isOut()) {
31-
webServer->on(getRestPath(entry).c_str(), std::bind(&WebPortal::handleRest, this));
32-
}
30+
webServer->on(getRestPath(entry).c_str(), std::bind(&WebPortal::handleRest, this));
3331
});
3432
webServer->onNotFound(std::bind(&WebPortal::handleNotFound, this));
3533
webServer->begin();
@@ -133,7 +131,7 @@ void WebPortal::sendRestApi(Entry* entry) {
133131
void WebPortal::handleRest() {
134132
if(!auth()) return;
135133
Entry* entry = &mqtt->get(webServer->uri().substring(6).c_str());
136-
if(webServer->method() == HTTP_GET && entry->isIn()) {
134+
if(webServer->method() == HTTP_GET) {
137135
webServer->send(200, "application/json", "{\"value\":\"" + String(entry->getValue()) + "\",\"updated\":\"" + time(entry->getLastUpdate()) + "\"}");
138136
} else if(webServer->method() == HTTP_POST && entry->isOut()) {
139137
entry->update(webServer->arg("plain"));

0 commit comments

Comments
 (0)