Added api endpoint to get livedata in json format with get request

This commit is contained in:
Thomas Basler 2022-07-20 20:54:57 +02:00
parent 184183e979
commit 3f8ee18de9
2 changed files with 67 additions and 47 deletions

View File

@ -12,7 +12,9 @@ public:
void loop(); void loop();
private: private:
void addField(JsonDocument& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, String topic = ""); void generateJsonResponse(JsonVariant& root);
void addField(JsonVariant& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, String topic = "");
void onLivedataStatus(AsyncWebServerRequest* request);
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len); void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
AsyncWebServer* _server; AsyncWebServer* _server;

View File

@ -16,6 +16,7 @@ void WebApiWsLiveClass::init(AsyncWebServer* server)
using namespace std::placeholders; using namespace std::placeholders;
_server = server; _server = server;
_server->on("/api/livedata/status", HTTP_GET, std::bind(&WebApiWsLiveClass::onLivedataStatus, this, _1));
_server->addHandler(&_ws); _server->addHandler(&_ws);
_ws.onEvent(std::bind(&WebApiWsLiveClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6)); _ws.onEvent(std::bind(&WebApiWsLiveClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6));
@ -52,6 +53,22 @@ void WebApiWsLiveClass::loop()
if (millis() - _lastWsPublish > (10 * 1000) || (maxTimeStamp != _newestInverterTimestamp)) { if (millis() - _lastWsPublish > (10 * 1000) || (maxTimeStamp != _newestInverterTimestamp)) {
DynamicJsonDocument root(40960); DynamicJsonDocument root(40960);
JsonVariant var = root;
generateJsonResponse(var);
size_t len = measureJson(root);
AsyncWebSocketMessageBuffer* buffer = _ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char*)buffer->get(), len + 1);
_ws.textAll(buffer);
}
_lastWsPublish = millis();
}
}
void WebApiWsLiveClass::generateJsonResponse(JsonVariant& root)
{
// Loop all inverters // Loop all inverters
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) { for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i); auto inv = Hoymiles.getInverterByPos(i);
@ -97,19 +114,9 @@ void WebApiWsLiveClass::loop()
_newestInverterTimestamp = inv->getLastStatsUpdate(); _newestInverterTimestamp = inv->getLastStatsUpdate();
} }
} }
size_t len = measureJson(root);
AsyncWebSocketMessageBuffer* buffer = _ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char*)buffer->get(), len + 1);
_ws.textAll(buffer);
}
_lastWsPublish = millis();
}
} }
void WebApiWsLiveClass::addField(JsonDocument& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, String topic) void WebApiWsLiveClass::addField(JsonVariant& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, String topic)
{ {
if (inv->Statistics()->hasChannelFieldValue(channel, fieldId)) { if (inv->Statistics()->hasChannelFieldValue(channel, fieldId)) {
String chanName; String chanName;
@ -135,3 +142,14 @@ void WebApiWsLiveClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketC
Serial.println(str); Serial.println(str);
} }
} }
void WebApiWsLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
{
AsyncJsonResponse* response = new AsyncJsonResponse(true, 40960U);
JsonVariant root = response->getRoot().as<JsonVariant>();
generateJsonResponse(root);
response->setLength();
request->send(response);
}