Skip to content

Commit

Permalink
fix: VE.Direct live data after supporting second MPPT
Browse files Browse the repository at this point in the history
an error was made when determining whether or not to push a VE.Direct
MPPT's state through the websocket based on its data's age.
  • Loading branch information
schlimmchen committed Mar 20, 2024
1 parent cffc5b1 commit 8e5e8d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/WebApi_ws_vedirect_live.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ class WebApiWsVedirectLiveClass {
static void populateJson(const JsonObject &root, const VeDirectMpptController::spData_t &spMpptData);
void onLivedataStatus(AsyncWebServerRequest* request);
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
bool hasUpdate(size_t idx);

AsyncWebServer* _server;
AsyncWebSocket _ws;

uint32_t _lastFullPublish = 0;
uint32_t _dataAgeMillis[VICTRON_MAX_COUNT] = { 0 };
uint32_t _lastPublish = 0;
static constexpr uint16_t _responseSize = VICTRON_MAX_COUNT * (1024 + 128);

std::mutex _mutex;
Expand Down
20 changes: 12 additions & 8 deletions src/WebApi_ws_vedirect_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ void WebApiWsVedirectLiveClass::wsCleanupTaskCb()
_ws.cleanupClients();
}

bool WebApiWsVedirectLiveClass::hasUpdate(size_t idx)
{
auto dataAgeMillis = VictronMppt.getDataAgeMillis(idx);
if (dataAgeMillis == 0) { return false; }
auto publishAgeMillis = millis() - _lastPublish;
return dataAgeMillis < publishAgeMillis;
}

void WebApiWsVedirectLiveClass::sendDataTaskCb()
{
// do nothing if no WS client is connected
Expand All @@ -62,8 +70,7 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb()
bool updateAvailable = false;
if (!fullUpdate) {
for (int idx = 0; idx < VICTRON_MAX_COUNT; ++idx) {
auto currentAgeMillis = VictronMppt.getDataAgeMillis(idx);
if (currentAgeMillis > 0 && currentAgeMillis < _dataAgeMillis[idx]) {
if (hasUpdate(idx)) {
updateAvailable = true;
break;
}
Expand Down Expand Up @@ -113,17 +120,14 @@ void WebApiWsVedirectLiveClass::generateJsonResponse(JsonVariant& root, bool ful
continue;
}

auto lastDataAgeMillis = _dataAgeMillis[idx];
_dataAgeMillis[idx] = VictronMppt.getDataAgeMillis(idx);
bool validAge = _dataAgeMillis[idx] > 0;
bool updateAvailable = _dataAgeMillis[idx] < lastDataAgeMillis;
if (!fullUpdate && !(validAge && updateAvailable)) { continue; }
if (!fullUpdate && !hasUpdate(idx)) { continue; }

VeDirectMpptController::spData_t &spMpptData = spOptMpptData.value();

const JsonObject &nested = array.createNestedObject(spMpptData->SER);
nested["data_age_ms"] = _dataAgeMillis[idx];
nested["data_age_ms"] = VictronMppt.getDataAgeMillis(idx);
populateJson(nested, spMpptData);
_lastPublish = millis();
}

// power limiter state
Expand Down

0 comments on commit 8e5e8d1

Please sign in to comment.