From 7cd1984d60c4ea0e0d3b7278e9e95737b60f84d6 Mon Sep 17 00:00:00 2001 From: Manuel Bruehl <179859524+MoleBre@users.noreply.github.com> Date: Sat, 31 Aug 2024 07:41:13 +0200 Subject: [PATCH] Feature: support for JBD BMS using serial connection --- include/BatteryStats.h | 30 +++ include/DataPoints.h | 119 +++++++++ include/JbdBmsController.h | 87 +++++++ include/JbdBmsDataPoints.h | 118 +++++++++ include/JbdBmsSerialMessage.h | 96 +++++++ include/JkBmsController.h | 3 +- include/JkBmsDataPoints.h | 106 +------- src/Battery.cpp | 4 + src/BatteryStats.cpp | 190 ++++++++++++++ src/{JkBmsDataPoints.cpp => DataPoints.cpp} | 23 +- src/JbdBmsController.cpp | 274 ++++++++++++++++++++ src/JbdBmsSerialMessage.cpp | 269 +++++++++++++++++++ webapp/src/locales/de.json | 2 + webapp/src/locales/en.json | 2 + webapp/src/locales/fr.json | 2 + webapp/src/views/BatteryAdminView.vue | 36 +++ 16 files changed, 1241 insertions(+), 120 deletions(-) create mode 100644 include/DataPoints.h create mode 100644 include/JbdBmsController.h create mode 100644 include/JbdBmsDataPoints.h create mode 100644 include/JbdBmsSerialMessage.h rename src/{JkBmsDataPoints.cpp => DataPoints.cpp} (67%) create mode 100644 src/JbdBmsController.cpp create mode 100644 src/JbdBmsSerialMessage.cpp diff --git a/include/BatteryStats.h b/include/BatteryStats.h index 9f5e533d3..cfce8ea88 100644 --- a/include/BatteryStats.h +++ b/include/BatteryStats.h @@ -6,6 +6,7 @@ #include "AsyncJson.h" #include "Arduino.h" #include "JkBmsDataPoints.h" +#include "JbdBmsDataPoints.h" #include "VeDirectShuntController.h" #include @@ -283,6 +284,35 @@ class JkBmsBatteryStats : public BatteryStats { uint32_t _cellVoltageTimestamp = 0; }; +class JbdBmsBatteryStats : public BatteryStats { + public: + void getLiveViewData(JsonVariant& root) const final { + getJsonData(root, false); + } + + void getInfoViewData(JsonVariant& root) const { + getJsonData(root, true); + } + + void mqttPublish() const final; + + uint32_t getMqttFullPublishIntervalMs() const final { return 60 * 1000; } + + void updateFrom(JbdBms::DataPointContainer const& dp); + + private: + void getJsonData(JsonVariant& root, bool verbose) const; + + JbdBms::DataPointContainer _dataPoints; + mutable uint32_t _lastMqttPublish = 0; + mutable uint32_t _lastFullMqttPublish = 0; + + uint16_t _cellMinMilliVolt = 0; + uint16_t _cellAvgMilliVolt = 0; + uint16_t _cellMaxMilliVolt = 0; + uint32_t _cellVoltageTimestamp = 0; +}; + class VictronSmartShuntStats : public BatteryStats { public: void getLiveViewData(JsonVariant& root) const final; diff --git a/include/DataPoints.h b/include/DataPoints.h new file mode 100644 index 000000000..91f19d92c --- /dev/null +++ b/include/DataPoints.h @@ -0,0 +1,119 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +using tCellVoltages = std::map; + +template +class DataPoint { + template class> + friend class DataPointContainer; + + public: + using tValue = std::variant; + + DataPoint() = delete; + + DataPoint(DataPoint const& other) + : _strLabel(other._strLabel) + , _strValue(other._strValue) + , _strUnit(other._strUnit) + , _value(other._value) + , _timestamp(other._timestamp) { } + + DataPoint(std::string const& strLabel, std::string const& strValue, + std::string const& strUnit, tValue value, uint32_t timestamp) + : _strLabel(strLabel) + , _strValue(strValue) + , _strUnit(strUnit) + , _value(std::move(value)) + , _timestamp(timestamp) { } + + std::string const& getLabelText() const { return _strLabel; } + std::string const& getValueText() const { return _strValue; } + 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; + std::string _strUnit; + tValue _value; + uint32_t _timestamp; +}; + +template std::string dataPointValueToStr(T const& v); + +template class Traits> +class DataPointContainer { + public: + DataPointContainer() = default; + + //template