diff --git a/include/BatteryStats.h b/include/BatteryStats.h index c3b0c6314..32c4af5a8 100644 --- a/include/BatteryStats.h +++ b/include/BatteryStats.h @@ -67,13 +67,15 @@ class BatteryStats { _lastUpdateCurrent = _lastUpdate = timestamp; } - String _manufacturer = "unknown"; + void setManufacturer(const String& m); + String _hwversion = ""; String _fwversion = ""; String _serial = ""; uint32_t _lastUpdate = 0; private: + String _manufacturer = "unknown"; uint32_t _lastMqttPublish = 0; float _soc = 0; uint8_t _socPrecision = 0; // decimal places @@ -98,7 +100,6 @@ class PylontechBatteryStats : public BatteryStats { float getChargeCurrentLimitation() const { return _chargeCurrentLimitation; } ; private: - void setManufacturer(String&& m) { _manufacturer = std::move(m); } void setLastUpdate(uint32_t ts) { _lastUpdate = ts; } float _chargeVoltage; @@ -137,7 +138,6 @@ class PytesBatteryStats : public BatteryStats { float getChargeCurrentLimitation() const { return _chargeCurrentLimit; } ; private: - void setManufacturer(String&& m) { _manufacturer = std::move(m); } void setLastUpdate(uint32_t ts) { _lastUpdate = ts; } void updateSerial() { if (!_serialPart1.isEmpty() && !_serialPart2.isEmpty()) { diff --git a/src/BatteryStats.cpp b/src/BatteryStats.cpp index 9f32931b7..c6d939266 100644 --- a/src/BatteryStats.cpp +++ b/src/BatteryStats.cpp @@ -54,6 +54,18 @@ static void addLiveViewAlarm(JsonVariant& root, std::string const& name, root["issues"][name] = 2; } +void BatteryStats::setManufacturer(const String& m) +{ + String sanitized(m); + for (int i = 0; i < sanitized.length(); i++) { + char c = sanitized[i]; + if (c < 0x20 || c >= 0x80) { + sanitized[i] = '?'; + } + } + _manufacturer = std::move(sanitized); +} + bool BatteryStats::updateAvailable(uint32_t since) const { if (_lastUpdate == 0) { return false; } // no data at all processed yet @@ -475,7 +487,7 @@ void JkBmsBatteryStats::updateFrom(JkBms::DataPointContainer const& dp) { using Label = JkBms::DataPointLabel; - _manufacturer = "JKBMS"; + setManufacturer("JKBMS"); auto oProductId = dp.get(); if (oProductId.has_value()) { // the first twelve chars are expected to be the "User Private Data" @@ -483,10 +495,10 @@ void JkBmsBatteryStats::updateFrom(JkBms::DataPointContainer const& dp) // name, which can be changed at will using the smartphone app. so // there is not always a "JK" in this string. if there is, we still cut // the string there to avoid possible regressions. - _manufacturer = oProductId->substr(12).c_str(); + setManufacturer(String(oProductId->substr(12).c_str())); auto pos = oProductId->rfind("JK"); if (pos != std::string::npos) { - _manufacturer = oProductId->substr(pos).c_str(); + setManufacturer(String(oProductId->substr(pos).c_str())); } } @@ -558,7 +570,7 @@ void VictronSmartShuntStats::updateFrom(VeDirectShuntController::data_t const& s _timeToGo = shuntData.TTG / 60; _chargedEnergy = static_cast(shuntData.H18) / 100; _dischargedEnergy = static_cast(shuntData.H17) / 100; - _manufacturer = String("Victron ") + shuntData.getPidAsString().data(); + setManufacturer(String("Victron ") + shuntData.getPidAsString().data()); _temperature = shuntData.T; _tempPresent = shuntData.tempPresent; _midpointVoltage = static_cast(shuntData.VM) / 1000; diff --git a/src/PylontechCanReceiver.cpp b/src/PylontechCanReceiver.cpp index d835c2f21..eae5558d0 100644 --- a/src/PylontechCanReceiver.cpp +++ b/src/PylontechCanReceiver.cpp @@ -2,6 +2,7 @@ #include "PylontechCanReceiver.h" #include "MessageOutput.h" #include "PinMapping.h" +#include #include #include @@ -98,7 +99,7 @@ void PylontechCanReceiver::onMessage(twai_message_t rx_message) case 0x35E: { String manufacturer(reinterpret_cast(rx_message.data), - rx_message.data_length_code); + std::min(5, static_cast(rx_message.data_length_code))); if (manufacturer.isEmpty()) { break; } @@ -106,7 +107,7 @@ void PylontechCanReceiver::onMessage(twai_message_t rx_message) MessageOutput.printf("[Pylontech] Manufacturer: %s\r\n", manufacturer.c_str()); } - _stats->setManufacturer(std::move(manufacturer)); + _stats->setManufacturer(manufacturer); break; } diff --git a/src/PytesCanReceiver.cpp b/src/PytesCanReceiver.cpp index 3e877cef3..e4b22e51a 100644 --- a/src/PytesCanReceiver.cpp +++ b/src/PytesCanReceiver.cpp @@ -127,7 +127,7 @@ void PytesCanReceiver::onMessage(twai_message_t rx_message) MessageOutput.printf("[Pytes] Manufacturer: %s\r\n", manufacturer.c_str()); } - _stats->setManufacturer(std::move(manufacturer)); + _stats->setManufacturer(manufacturer); break; }