Skip to content

Commit

Permalink
replace VICTRON_MAX_COUNT
Browse files Browse the repository at this point in the history
determine the amount of controllers actually in use dynamically,
especially to avoid indices which are invalid, causing an error
to be printed, even though the user did not do anything wrong.
  • Loading branch information
schlimmchen committed Mar 20, 2024
1 parent 8e5e8d1 commit b449dd1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
2 changes: 0 additions & 2 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/VictronMppt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<VeDirectMpptController::spData_t> getData(size_t idx = 0) const;

// total output of all MPPT charge controllers in Watts
Expand Down
2 changes: 1 addition & 1 deletion include/WebApi_ws_vedirect_live.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/MqttHandlVedirectHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/MqttHandleVedirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 9 additions & 4 deletions src/WebApi_ws_vedirect_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -80,7 +85,7 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb()
if (fullUpdate || updateAvailable) {
try {
std::lock_guard<std::mutex> lock(_mutex);
DynamicJsonDocument root(_responseSize);
DynamicJsonDocument root(responseSize());
if (Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
JsonVariant var = root;
generateJsonResponse(var, fullUpdate);
Expand Down Expand Up @@ -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<VeDirectMpptController::spData_t> spOptMpptData = VictronMppt.getData(idx);
if (!spOptMpptData.has_value()) {
continue;
Expand Down Expand Up @@ -213,7 +218,7 @@ void WebApiWsVedirectLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
}
try {
std::lock_guard<std::mutex> lock(_mutex);
AsyncJsonResponse* response = new AsyncJsonResponse(false, _responseSize);
AsyncJsonResponse* response = new AsyncJsonResponse(false, responseSize());
auto& root = response->getRoot();

generateJsonResponse(root, true/*fullUpdate*/);
Expand Down

0 comments on commit b449dd1

Please sign in to comment.