2022-04-11 19:44:51 +02:00
|
|
|
#include "WebApi.h"
|
2022-04-12 23:53:58 +02:00
|
|
|
#include "ArduinoJson.h"
|
|
|
|
|
#include "AsyncJson.h"
|
2022-04-11 19:44:51 +02:00
|
|
|
#include "defaults.h"
|
|
|
|
|
|
|
|
|
|
WebApiClass::WebApiClass()
|
|
|
|
|
: _server(HTTP_PORT)
|
2022-06-18 01:14:31 +02:00
|
|
|
, _ws("/livedata")
|
2022-04-11 19:44:51 +02:00
|
|
|
, _events("/events")
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
2022-06-15 23:46:22 +02:00
|
|
|
_webApiDtu.init(&_server);
|
2022-07-06 22:17:26 +02:00
|
|
|
_webApiEventlog.init(&_server);
|
2022-06-15 23:46:22 +02:00
|
|
|
_webApiFirmware.init(&_server);
|
|
|
|
|
_webApiInverter.init(&_server);
|
|
|
|
|
_webApiMqtt.init(&_server);
|
|
|
|
|
_webApiNetwork.init(&_server);
|
|
|
|
|
_webApiNtp.init(&_server);
|
|
|
|
|
_webApiSysstatus.init(&_server);
|
|
|
|
|
_webApiWebapp.init(&_server);
|
2022-05-22 22:42:06 +02:00
|
|
|
|
2022-06-16 01:44:42 +02:00
|
|
|
_webApiWsLive.init(&_ws);
|
|
|
|
|
|
2022-04-11 19:44:51 +02:00
|
|
|
_server.begin();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 22:53:05 +02:00
|
|
|
void WebApiClass::loop()
|
|
|
|
|
{
|
2022-06-15 23:46:22 +02:00
|
|
|
_webApiDtu.loop();
|
2022-07-06 22:17:26 +02:00
|
|
|
_webApiEventlog.loop();
|
2022-06-15 23:46:22 +02:00
|
|
|
_webApiFirmware.loop();
|
|
|
|
|
_webApiInverter.loop();
|
|
|
|
|
_webApiMqtt.loop();
|
|
|
|
|
_webApiNetwork.loop();
|
|
|
|
|
_webApiNtp.loop();
|
|
|
|
|
_webApiSysstatus.loop();
|
|
|
|
|
_webApiWebapp.loop();
|
|
|
|
|
|
2022-06-16 01:44:42 +02:00
|
|
|
_webApiWsLive.loop();
|
|
|
|
|
|
2022-06-15 22:53:05 +02:00
|
|
|
// see: https://github.com/me-no-dev/ESPAsyncWebServer#limiting-the-number-of-web-socket-clients
|
2022-07-06 19:25:30 +02:00
|
|
|
if (millis() - _lastWsCleanup > 1000) {
|
2022-07-05 23:42:07 +02:00
|
|
|
_ws.cleanupClients();
|
2022-07-06 19:25:30 +02:00
|
|
|
_lastWsCleanup = millis();
|
2022-07-05 23:42:07 +02:00
|
|
|
}
|
2022-06-15 22:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-11 19:44:51 +02:00
|
|
|
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;
|