Skip to content

Commit 1202ac2

Browse files
author
Bjarne Loft
committed
Change lastValue to be a c string
1 parent 2ec98c1 commit 1202ac2

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

src/Entry.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void Entry::update() {
9696
String value = inFunction();
9797
if (value != "") {
9898
if (value != getValue() || force > getForce()) {
99-
setValue(value);
99+
setValue(value.c_str());
100100
force = 0;
101101
}
102102
}
@@ -162,28 +162,26 @@ int Entry::getForce() {
162162
return forceUpdate;
163163
}
164164

165-
String Entry::getValue() {
165+
char *Entry::getValue() {
166166
return lastValue;
167167
}
168168

169-
void Entry::getCValue(char * destination) {
170-
strcpy(destination, lastValue.c_str());
171-
}
172-
173-
void Entry::setValue(String value) {
169+
void Entry::setValue(const char *value) {
174170
lastUpdate = millis();
175-
lastValue = value;
171+
free(lastValue);
172+
lastValue = (char*)malloc(strlen(value)+1);
173+
strcpy(lastValue, value);
176174
publish(value);
177175
}
178176

179177
long Entry::getLastUpdate() {
180178
return lastUpdate;
181179
}
182180

183-
void Entry::publish(String message) {
181+
void Entry::publish(const char *message) {
184182
auto function = getPublishFunction();
185183
if(function) {
186-
function(this, message);
184+
function(this, String(message));
187185
}
188186
}
189187

src/Entry.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Entry {
1515
int interval = -1;
1616
int forceUpdate = -1;
1717
unsigned long lastUpdate = 0;
18-
String lastValue = "";
18+
char* lastValue = "";
1919

2020
Entry* parent = NULL;
2121
Entry* next = NULL;
@@ -62,9 +62,8 @@ class Entry {
6262
/**
6363
* Get last value
6464
*/
65-
String getValue();
66-
void getCValue(char * destination);
67-
void setValue(String value);
65+
char *getValue();
66+
void setValue(const char *value);
6867

6968
/**
7069
*
@@ -74,7 +73,7 @@ class Entry {
7473
/**
7574
* Publish value to mqtt
7675
*/
77-
void publish(String message);
76+
void publish(const char *message);
7877

7978
/**
8079
* Iterate over each child, including sub children

src/WebPortal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void WebPortal::handleRoot() {
6868
});
6969

7070
String page = FPSTR(HTML_MAIN5);
71-
page.replace("{device_id}", mqtt->get("$system")["deviceId"].getValue());
71+
page.replace("{device_id}", String(mqtt->get("$system")["deviceId"].getValue()));
7272
page.replace("{topic}", mqtt->getTopic());
7373
webServer->sendContent(page);
7474

@@ -134,7 +134,7 @@ void WebPortal::handleRest() {
134134
if(!auth()) return;
135135
Entry* entry = &mqtt->get(webServer->uri().substring(6).c_str());
136136
if(webServer->method() == HTTP_GET && entry->isIn()) {
137-
webServer->send(200, "application/json", "{\"value\":\"" + entry->getValue() + "\",\"updated\":\"" + time(entry->getLastUpdate()) + "\"}");
137+
webServer->send(200, "application/json", "{\"value\":\"" + String(entry->getValue()) + "\",\"updated\":\"" + time(entry->getLastUpdate()) + "\"}");
138138
} else if(webServer->method() == HTTP_POST && entry->isOut()) {
139139
entry->update(webServer->arg("plain"));
140140
webServer->send(200, "text/plain", webServer->uri() + " Update");

0 commit comments

Comments
 (0)