MQTT on interval

This commit is contained in:
Gunnar Skjold
2024-04-21 10:21:39 +02:00
parent efacbd4b31
commit 3f1861deda
11 changed files with 125 additions and 47 deletions

View File

@@ -6,16 +6,29 @@
#include "DomoticzMqttHandler.h"
#include "json/domoticz_json.h"
#include "Uptime.h"
bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
bool DomoticzMqttHandler::publish(AmsData* update, AmsData* previousState, EnergyAccounting* ea, PriceService* ps) {
bool ret = false;
AmsData data;
if(mqttConfig.stateUpdate) {
uint64_t now = millis64();
if(now-lastStateUpdate < mqttConfig.stateUpdateInterval * 1000) return false;
data.apply(*previousState);
data.apply(*update);
lastStateUpdate = now;
} else {
data = *update;
}
if (config.elidx > 0) {
if(data->getActiveImportCounter() > 1.0) {
energy = data->getActiveImportCounter();
if(data.getActiveImportCounter() > 1.0 && !data.isCounterEstimated()) {
energy = data.getActiveImportCounter();
}
if(energy > 0.0) {
char val[16];
snprintf_P(val, 16, PSTR("%.1f;%.1f"), (data->getActiveImportPower()/1.0), energy*1000.0);
snprintf_P(val, 16, PSTR("%.1f;%.1f"), (data.getActiveImportPower()/1.0), energy*1000.0);
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.elidx,
val
@@ -25,12 +38,12 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
}
}
if(data->getListType() == 1)
if(data.getListType() == 1)
return ret;
if (config.vl1idx > 0){
char val[16];
snprintf_P(val, 16, PSTR("%.2f"), data->getL1Voltage());
snprintf_P(val, 16, PSTR("%.2f"), data.getL1Voltage());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl1idx,
val
@@ -41,7 +54,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
if (config.vl2idx > 0){
char val[16];
snprintf_P(val, 16, PSTR("%.2f"), data->getL2Voltage());
snprintf_P(val, 16, PSTR("%.2f"), data.getL2Voltage());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl2idx,
val
@@ -52,7 +65,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
if (config.vl3idx > 0){
char val[16];
snprintf(val, 16, "%.2f", data->getL3Voltage());
snprintf(val, 16, "%.2f", data.getL3Voltage());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl3idx,
val
@@ -63,7 +76,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyA
if (config.cl1idx > 0){
char val[16];
snprintf(val, 16, "%.1f;%.1f;%.1f", data->getL1Current(), data->getL2Current(), data->getL3Current());
snprintf(val, 16, "%.1f;%.1f;%.1f", data.getL1Current(), data.getL2Current(), data.getL3Current());
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.cl1idx,
val