Support fw upgrade via MQTT with JSON payload (#941)

This commit is contained in:
Gunnar Skjold
2025-03-24 09:00:05 +01:00
committed by GitHub
parent 8ee3f53714
commit 4d681ed2e2
3 changed files with 31 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ public:
bool publishPrices(PriceService*);
bool publishSystem(HwTools* hw, PriceService* ps, EnergyAccounting* ea);
bool publishRaw(String data);
bool publishFirmware();
void onMessage(String &topic, String &payload);

View File

@@ -432,5 +432,26 @@ bool JsonMqttHandler::publishRaw(String data) {
return false;
}
void JsonMqttHandler::onMessage(String &topic, String &payload) {
bool JsonMqttHandler::publishFirmware() {
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}"),
FirmwareVersion::VersionString,
strlen(updater->getNextVersion()) == 0 ? FirmwareVersion::VersionString : updater->getNextVersion(),
strlen(updater->getNextVersion()) == 0 ? FirmwareVersion::VersionString : updater->getNextVersion(),
updater->getProgress() < 0 ? "null" : String(updater->getProgress(), 0)
);
char topic[192];
snprintf_P(topic, 192, PSTR("%s/firmware"), mqttConfig.publishTopic);
bool ret = mqtt.publish(topic, json);
loop();
return ret;
}
void JsonMqttHandler::onMessage(String &topic, String &payload) {
if(strncmp(topic.c_str(), mqttConfig.subscribeTopic, 12) == 0) {
if(payload.equals("fwupgrade")) {
if(strcmp(updater->getNextVersion(), FirmwareVersion::VersionString) != 0) {
updater->setTargetVersion(updater->getNextVersion());
}
}
}
}

View File

@@ -7,6 +7,7 @@
#include "RawMqttHandler.h"
#include "hexutils.h"
#include "Uptime.h"
#include "FirmwareVersion.h"
bool RawMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
if(topic.isEmpty() || !mqtt.connected())
@@ -382,4 +383,11 @@ bool RawMqttHandler::publishRaw(String data) {
}
void RawMqttHandler::onMessage(String &topic, String &payload) {
if(strncmp(topic.c_str(), mqttConfig.subscribeTopic, 12) == 0) {
if(payload.equals("fwupgrade")) {
if(strcmp(updater->getNextVersion(), FirmwareVersion::VersionString) != 0) {
updater->setTargetVersion(updater->getNextVersion());
}
}
}
}