Move websocket stuff to liveview class

This commit is contained in:
Thomas Basler 2022-07-20 19:36:07 +02:00
parent 608912e9f3
commit 184183e979
4 changed files with 42 additions and 37 deletions

View File

@ -21,7 +21,6 @@ public:
private:
AsyncWebServer _server;
AsyncWebSocket _ws;
AsyncEventSource _events;
WebApiDtuClass _webApiDtu;
@ -34,9 +33,6 @@ private:
WebApiSysstatusClass _webApiSysstatus;
WebApiWebappClass _webApiWebapp;
WebApiWsLiveClass _webApiWsLive;
unsigned long _lastWsCleanup = 0;
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
};
extern WebApiClass WebApi;

View File

@ -7,14 +7,19 @@
class WebApiWsLiveClass {
public:
void init(AsyncWebSocket* ws);
WebApiWsLiveClass();
void init(AsyncWebServer* server);
void loop();
private:
AsyncWebSocket* _ws;
void addField(JsonDocument& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, String topic = "");
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
AsyncWebServer* _server;
AsyncWebSocket _ws;
uint32_t _lastWsPublish = 0;
uint32_t _lastInvUpdateCheck = 0;
unsigned long _lastWsCleanup = 0;
uint32_t _newestInverterTimestamp = 0;
};

View File

@ -9,7 +9,6 @@
WebApiClass::WebApiClass()
: _server(HTTP_PORT)
, _ws("/livedata")
, _events("/events")
{
}
@ -18,11 +17,8 @@ void WebApiClass::init()
{
using namespace std::placeholders;
_server.addHandler(&_ws);
_server.addHandler(&_events);
_ws.onEvent(std::bind(&WebApiClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6));
_webApiDtu.init(&_server);
_webApiEventlog.init(&_server);
_webApiFirmware.init(&_server);
@ -32,8 +28,7 @@ void WebApiClass::init()
_webApiNtp.init(&_server);
_webApiSysstatus.init(&_server);
_webApiWebapp.init(&_server);
_webApiWsLive.init(&_ws);
_webApiWsLive.init(&_server);
_server.begin();
}
@ -49,27 +44,7 @@ void WebApiClass::loop()
_webApiNtp.loop();
_webApiSysstatus.loop();
_webApiWebapp.loop();
_webApiWsLive.loop();
// see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients
if (millis() - _lastWsCleanup > 1000) {
_ws.cleanupClients();
_lastWsCleanup = millis();
}
}
void WebApiClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
{
if (type == WS_EVT_CONNECT) {
char str[64];
sprintf(str, "Websocket: [%s][%u] connect", server->url(), client->id());
Serial.println(str);
} else if (type == WS_EVT_DISCONNECT) {
char str[64];
sprintf(str, "Websocket: [%s][%u] disconnect", server->url(), client->id());
Serial.println(str);
}
}
WebApiClass WebApi;

View File

@ -6,15 +6,31 @@
#include "AsyncJson.h"
#include "Configuration.h"
void WebApiWsLiveClass::init(AsyncWebSocket* ws)
WebApiWsLiveClass::WebApiWsLiveClass()
: _ws("/livedata")
{
_ws = ws;
}
void WebApiWsLiveClass::init(AsyncWebServer* server)
{
using namespace std::placeholders;
_server = server;
_server->addHandler(&_ws);
_ws.onEvent(std::bind(&WebApiWsLiveClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6));
}
void WebApiWsLiveClass::loop()
{
// see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients
if (millis() - _lastWsCleanup > 1000) {
_ws.cleanupClients();
_lastWsCleanup = millis();
}
// do nothing if no WS client is connected
if (_ws->count() == 0) {
if (_ws.count() == 0) {
return;
}
@ -83,10 +99,10 @@ void WebApiWsLiveClass::loop()
}
size_t len = measureJson(root);
AsyncWebSocketMessageBuffer* buffer = _ws->makeBuffer(len); // creates a buffer (len + 1) for you.
AsyncWebSocketMessageBuffer* buffer = _ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char*)buffer->get(), len + 1);
_ws->textAll(buffer);
_ws.textAll(buffer);
}
_lastWsPublish = millis();
@ -105,4 +121,17 @@ void WebApiWsLiveClass::addField(JsonDocument& root, uint8_t idx, std::shared_pt
root[idx][String(channel)][chanName]["v"] = inv->Statistics()->getChannelFieldValue(channel, fieldId);
root[idx][String(channel)][chanName]["u"] = inv->Statistics()->getChannelFieldUnit(channel, fieldId);
}
}
void WebApiWsLiveClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
{
if (type == WS_EVT_CONNECT) {
char str[64];
sprintf(str, "Websocket: [%s][%u] connect", server->url(), client->id());
Serial.println(str);
} else if (type == WS_EVT_DISCONNECT) {
char str[64];
sprintf(str, "Websocket: [%s][%u] disconnect", server->url(), client->id());
Serial.println(str);
}
}