diff --git a/include/Configuration.h b/include/Configuration.h index a01ad315b..4c4f7b304 100644 --- a/include/Configuration.h +++ b/include/Configuration.h @@ -30,8 +30,6 @@ #define DEV_MAX_MAPPING_NAME_STRLEN 63 -#define VICTRON_MAX_COUNT 2 - #define POWERMETER_MAX_PHASES 3 #define POWERMETER_MAX_HTTP_URL_STRLEN 1024 #define POWERMETER_MAX_USERNAME_STRLEN 64 diff --git a/include/VictronMppt.h b/include/VictronMppt.h index 6e3cfdee1..39e85aad7 100644 --- a/include/VictronMppt.h +++ b/include/VictronMppt.h @@ -24,6 +24,7 @@ class VictronMpptClass { uint32_t getDataAgeMillis() const; uint32_t getDataAgeMillis(size_t idx) const; + size_t controllerAmount() const { return _controllers.size(); } std::optional getData(size_t idx = 0) const; // total output of all MPPT charge controllers in Watts diff --git a/include/WebApi_ws_vedirect_live.h b/include/WebApi_ws_vedirect_live.h index 7b16c8878..d46de4cc2 100644 --- a/include/WebApi_ws_vedirect_live.h +++ b/include/WebApi_ws_vedirect_live.h @@ -25,7 +25,7 @@ class WebApiWsVedirectLiveClass { uint32_t _lastFullPublish = 0; uint32_t _lastPublish = 0; - static constexpr uint16_t _responseSize = VICTRON_MAX_COUNT * (1024 + 128); + uint16_t responseSize() const; std::mutex _mutex; diff --git a/src/MqttHandlVedirectHass.cpp b/src/MqttHandlVedirectHass.cpp index e3e0655dc..01b718abe 100644 --- a/src/MqttHandlVedirectHass.cpp +++ b/src/MqttHandlVedirectHass.cpp @@ -57,7 +57,7 @@ void MqttHandleVedirectHassClass::publishConfig() } // device info - for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) { + for (int idx = 0; idx < VictronMppt.controllerAmount(); ++idx) { // ensure data is received from victron if (!VictronMppt.isDataValid(idx)) { continue; diff --git a/src/MqttHandleVedirect.cpp b/src/MqttHandleVedirect.cpp index b363493f2..8cfd6efce 100644 --- a/src/MqttHandleVedirect.cpp +++ b/src/MqttHandleVedirect.cpp @@ -58,7 +58,7 @@ void MqttHandleVedirectClass::loop() } #endif - for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) { + for (int idx = 0; idx < VictronMppt.controllerAmount(); ++idx) { if (!VictronMppt.isDataValid(idx)) { continue; } diff --git a/src/WebApi_ws_vedirect_live.cpp b/src/WebApi_ws_vedirect_live.cpp index 2bebc17c2..58aaba035 100644 --- a/src/WebApi_ws_vedirect_live.cpp +++ b/src/WebApi_ws_vedirect_live.cpp @@ -60,6 +60,11 @@ bool WebApiWsVedirectLiveClass::hasUpdate(size_t idx) return dataAgeMillis < publishAgeMillis; } +uint16_t WebApiWsVedirectLiveClass::responseSize() const +{ + return VictronMppt.controllerAmount() * (1024 + 128); +} + void WebApiWsVedirectLiveClass::sendDataTaskCb() { // do nothing if no WS client is connected @@ -69,7 +74,7 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb() bool fullUpdate = (millis() - _lastFullPublish > (10 * 1000)); bool updateAvailable = false; if (!fullUpdate) { - for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) { + for (size_t idx = 0; idx < VictronMppt.controllerAmount(); ++idx) { if (hasUpdate(idx)) { updateAvailable = true; break; @@ -80,7 +85,7 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb() if (fullUpdate || updateAvailable) { try { std::lock_guard lock(_mutex); - DynamicJsonDocument root(_responseSize); + DynamicJsonDocument root(responseSize()); if (Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) { JsonVariant var = root; generateJsonResponse(var, fullUpdate); @@ -114,7 +119,7 @@ void WebApiWsVedirectLiveClass::generateJsonResponse(JsonVariant& root, bool ful const JsonObject &array = root["vedirect"].createNestedObject("instances"); root["vedirect"]["full_update"] = fullUpdate; - for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) { + for (size_t idx = 0; idx < VictronMppt.controllerAmount(); ++idx) { std::optional spOptMpptData = VictronMppt.getData(idx); if (!spOptMpptData.has_value()) { continue; @@ -213,7 +218,7 @@ void WebApiWsVedirectLiveClass::onLivedataStatus(AsyncWebServerRequest* request) } try { std::lock_guard lock(_mutex); - AsyncJsonResponse* response = new AsyncJsonResponse(false, _responseSize); + AsyncJsonResponse* response = new AsyncJsonResponse(false, responseSize()); auto& root = response->getRoot(); generateJsonResponse(root, true/*fullUpdate*/);