From 24018a14321d66e2247972b7650464ec49f47831 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Wed, 13 Sep 2023 12:14:29 +0200 Subject: [PATCH] JK BMS: Support for MQTT (#432) * JK BMS: avoid trailing whitespace in debug output * JK BMS: publish data points through MQTT * JK BMS: updateFrom: skip data points with equal value this changes the interpretation of the timestamp in data containers that are merely updated from other data containers: this is the oldest timestamp known where the value was as recorded by the data point in its respective container. the data container constructed from an answer will -- naturally -- have the timetamps of its data points set to the time they were constructed. * JK BMS: only publish changed values to MQTT broker all values are still published once every minute if the MQTT retain flag is NOT set. otherwise, the constant values are only published once on startup. --- include/BatteryStats.h | 2 ++ include/JkBmsDataPoints.h | 4 ++++ src/BatteryStats.cpp | 31 +++++++++++++++++++++++++++++++ src/JkBmsController.cpp | 4 ++-- src/JkBmsDataPoints.cpp | 9 ++++++++- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/BatteryStats.h b/include/BatteryStats.h index ebdca11fd..6dc816a8c 100644 --- a/include/BatteryStats.h +++ b/include/BatteryStats.h @@ -95,4 +95,6 @@ class JkBmsBatteryStats : public BatteryStats { private: JkBms::DataPointContainer _dataPoints; + mutable uint32_t _lastMqttPublish = 0; + mutable uint32_t _lastFullMqttPublish = 0; }; diff --git a/include/JkBmsDataPoints.h b/include/JkBmsDataPoints.h index db7cd37ab..bf3a0f9bd 100644 --- a/include/JkBmsDataPoints.h +++ b/include/JkBmsDataPoints.h @@ -185,6 +185,10 @@ class DataPoint { std::string const& getUnitText() const { return _strUnit; } uint32_t getTimestamp() const { return _timestamp; } + bool operator==(DataPoint const& other) const { + return _value == other._value; + } + private: std::string _strLabel; std::string _strValue; diff --git a/src/BatteryStats.cpp b/src/BatteryStats.cpp index 2c4997e97..fb1d2f2de 100644 --- a/src/BatteryStats.cpp +++ b/src/BatteryStats.cpp @@ -1,5 +1,8 @@ // SPDX-License-Identifier: GPL-2.0-or-later +#include +#include #include "BatteryStats.h" +#include "Configuration.h" #include "MqttSettings.h" #include "JkBmsDataPoints.h" @@ -145,6 +148,34 @@ void PylontechBatteryStats::mqttPublish() const void JkBmsBatteryStats::mqttPublish() const { BatteryStats::mqttPublish(); + + using Label = JkBms::DataPointLabel; + + static std::vector