Skip to content

Commit 4d681ed

Browse files
authored
Support fw upgrade via MQTT with JSON payload (#941)
1 parent 8ee3f53 commit 4d681ed

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/JsonMqttHandler/include/JsonMqttHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class JsonMqttHandler : public AmsMqttHandler {
2525
bool publishPrices(PriceService*);
2626
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
2727
bool publishRaw(String data);
28+
bool publishFirmware();
2829

2930
void onMessage(String &topic, String &payload);
3031

lib/JsonMqttHandler/src/JsonMqttHandler.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,5 +432,26 @@ bool JsonMqttHandler::publishRaw(String data) {
432432
return false;
433433
}
434434

435+
bool JsonMqttHandler::publishFirmware() {
436+
snprintf_P(json, BufferSize, PSTR("{\"installed_version\":\"%s\",\"latest_version\":\"%s\",\"title\":\"amsreader firmware\",\"release_url\":\"https://github.com/UtilitechAS/amsreader-firmware/releases\",\"release_summary\":\"New version %s is available\",\"update_percentage\":%s}"),
437+
FirmwareVersion::VersionString,
438+
strlen(updater->getNextVersion()) == 0 ? FirmwareVersion::VersionString : updater->getNextVersion(),
439+
strlen(updater->getNextVersion()) == 0 ? FirmwareVersion::VersionString : updater->getNextVersion(),
440+
updater->getProgress() < 0 ? "null" : String(updater->getProgress(), 0)
441+
);
442+
char topic[192];
443+
snprintf_P(topic, 192, PSTR("%s/firmware"), mqttConfig.publishTopic);
444+
bool ret = mqtt.publish(topic, json);
445+
loop();
446+
return ret;
447+
}
448+
435449
void JsonMqttHandler::onMessage(String &topic, String &payload) {
450+
if(strncmp(topic.c_str(), mqttConfig.subscribeTopic, 12) == 0) {
451+
if(payload.equals("fwupgrade")) {
452+
if(strcmp(updater->getNextVersion(), FirmwareVersion::VersionString) != 0) {
453+
updater->setTargetVersion(updater->getNextVersion());
454+
}
455+
}
456+
}
436457
}

lib/RawMqttHandler/src/RawMqttHandler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "RawMqttHandler.h"
88
#include "hexutils.h"
99
#include "Uptime.h"
10+
#include "FirmwareVersion.h"
1011

1112
bool RawMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
1213
if(topic.isEmpty() || !mqtt.connected())
@@ -382,4 +383,11 @@ bool RawMqttHandler::publishRaw(String data) {
382383
}
383384

384385
void RawMqttHandler::onMessage(String &topic, String &payload) {
386+
if(strncmp(topic.c_str(), mqttConfig.subscribeTopic, 12) == 0) {
387+
if(payload.equals("fwupgrade")) {
388+
if(strcmp(updater->getNextVersion(), FirmwareVersion::VersionString) != 0) {
389+
updater->setTargetVersion(updater->getNextVersion());
390+
}
391+
}
392+
}
385393
}

0 commit comments

Comments
 (0)