Skip to content

Commit

Permalink
adding totals as webapi live
Browse files Browse the repository at this point in the history
prevent sending 0 as "e_energy_exported" to avoid peaks and gaps in solarweb statistic
  • Loading branch information
Alois Klingler committed Jun 28, 2023
1 parent 5684efa commit 634f962
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"C_Cpp.clang_format_style": "WebKit"
"C_Cpp.clang_format_style": "WebKit",
"files.associations": {
"new": "cpp"
}
}
2 changes: 2 additions & 0 deletions include/WebApi_ws_live.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class WebApiWsLiveClass {

private:
void generateJsonResponse(JsonVariant& root);
void generateJsonResponseTotals(JsonVariant& root);
void addField(JsonObject& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, String topic = "");
void addTotalField(JsonObject& root, String name, float value, String unit, uint8_t digits);
void onLivedataStatus(AsyncWebServerRequest* request);
void onLivedataTotals(AsyncWebServerRequest* request);
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);

AsyncWebServer* _server;
Expand Down
26 changes: 21 additions & 5 deletions src/ModbusDtu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ void ModbusDtuClass::init()
mb.addHreg(0x9c86, 124); //40070
for (uint16_t i = 40071; i <= 40194; i++) mb.addHreg(i, 0); //40071 - 40194 smartmeter data
mb.addHreg(0x9d03, 65535); //40195 end block identifier
mb.addHreg(0x9d04, 0); //40196
mb.addHreg(0x9d04, 0); //40196
mb.removeHreg(0x9cc1); // do not report 0 values ...
mb.removeHreg(0x9cc2); // do not report 0 values ...
}

void ModbusDtuClass::loop()
Expand Down Expand Up @@ -174,8 +176,15 @@ void ModbusDtuClass::loop()
// mb.Hreg(0x9cbf, 0);
// mb.Hreg(0x9cc0, 0);
value = (inv->Statistics()->getChannelFieldValue(TYPE_AC, CH0, FLD_YT)*1000);
mb.Hreg(0x9cc1, hexbytes[1]);
mb.Hreg(0x9cc2, hexbytes[0]);
if (value == 0) {
mb.removeHreg(0x9cc1);
mb.removeHreg(0x9cc2);
} else {
mb.addHreg(0x9cc1, 0);
mb.addHreg(0x9cc2, 0);
mb.Hreg(0x9cc1, hexbytes[1]);
mb.Hreg(0x9cc2, hexbytes[0]);
}
}
}
}
Expand All @@ -186,8 +195,15 @@ void ModbusDtuClass::loop()
mb.Hreg(0x9ca1, hexbytes[1]);
mb.Hreg(0x9ca2, hexbytes[0]);
value = (Datastore.getTotalAcYieldTotalEnabled()*1000);
mb.Hreg(0x9cc1, hexbytes[1]);
mb.Hreg(0x9cc2, hexbytes[0]);
if (value == 0) {
mb.removeHreg(0x9cc1);
mb.removeHreg(0x9cc2);
} else {
mb.addHreg(0x9cc1, 0);
mb.addHreg(0x9cc2, 0);
mb.Hreg(0x9cc1, hexbytes[1]);
mb.Hreg(0x9cc2, hexbytes[0]);
}
}
_lastPublish = millis();
}
Expand Down
32 changes: 32 additions & 0 deletions src/WebApi_ws_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void WebApiWsLiveClass::init(AsyncWebServer* server)

_server = server;
_server->on("/api/livedata/status", HTTP_GET, std::bind(&WebApiWsLiveClass::onLivedataStatus, this, _1));
_server->on("/api/livedata/totals", HTTP_GET, std::bind(&WebApiWsLiveClass::onLivedataTotals, this, _1));

_server->addHandler(&_ws);
_ws.onEvent(std::bind(&WebApiWsLiveClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6));
Expand Down Expand Up @@ -177,6 +178,15 @@ void WebApiWsLiveClass::generateJsonResponse(JsonVariant& root)
}
}

void WebApiWsLiveClass::generateJsonResponseTotals(JsonVariant& root)
{
JsonObject totalObj = root.createNestedObject("total");
addTotalField(totalObj, "Power", Datastore.getTotalAcPowerEnabled(), "W", Datastore.getTotalAcPowerDigits());
addTotalField(totalObj, "YieldDay", Datastore.getTotalAcYieldDayEnabled(), "Wh", Datastore.getTotalAcYieldDayDigits());
addTotalField(totalObj, "YieldTotal", Datastore.getTotalAcYieldTotalEnabled(), "kWh", Datastore.getTotalAcYieldTotalDigits());

}

void WebApiWsLiveClass::addField(JsonObject& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, String topic)
{
if (inv->Statistics()->hasChannelFieldValue(type, channel, fieldId)) {
Expand Down Expand Up @@ -232,6 +242,28 @@ void WebApiWsLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
} catch (std::bad_alloc& bad_alloc) {
MessageOutput.printf("Call to /api/livedata/status temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what());

WebApi.sendTooManyRequests(request);
}
}

void WebApiWsLiveClass::onLivedataTotals(AsyncWebServerRequest* request)
{
if (!WebApi.checkCredentialsReadonly(request)) {
return;
}

try {
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
JsonVariant root = response->getRoot();

generateJsonResponseTotals(root);

response->setLength();
request->send(response);

} catch (std::bad_alloc& bad_alloc) {
MessageOutput.printf("Call to /api/livedata/totals temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what());

WebApi.sendTooManyRequests(request);
}
}

0 comments on commit 634f962

Please sign in to comment.