2021-12-16 00:19:08 +01:00
|
|
|
#include "WebSocket.h"
|
|
|
|
|
|
2021-12-20 02:19:54 +01:00
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
2021-12-16 01:56:14 +01:00
|
|
|
#include "Class/NotAsync.h"
|
2021-12-16 00:19:08 +01:00
|
|
|
#include "Global.h"
|
|
|
|
|
|
2021-12-16 01:56:14 +01:00
|
|
|
void wsInit() {
|
|
|
|
|
myNotAsyncActions->add(
|
|
|
|
|
do_webSocketSendSetup, [&](void*) {
|
|
|
|
|
wsSendSetup();
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 00:19:08 +01:00
|
|
|
void wsSendSetup() {
|
|
|
|
|
File file = seekFile("/setup.json");
|
2021-12-16 01:35:46 +01:00
|
|
|
|
|
|
|
|
DynamicJsonDocument doc(2048);
|
|
|
|
|
|
2021-12-20 02:19:54 +01:00
|
|
|
int i = 0;
|
|
|
|
|
|
2021-12-16 00:19:08 +01:00
|
|
|
file.find("[");
|
2021-12-20 02:19:54 +01:00
|
|
|
|
|
|
|
|
SerialPrint("I", F("WS"), F("start send config"));
|
2021-12-16 00:19:08 +01:00
|
|
|
do {
|
2021-12-20 02:19:54 +01:00
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
// static bool flag = false;
|
2021-12-16 00:19:08 +01:00
|
|
|
deserializeJson(doc, file);
|
2021-12-20 02:19:54 +01:00
|
|
|
size_t len = measureJson(doc);
|
|
|
|
|
AsyncWebSocketMessageBuffer* buffer = ws.makeBuffer(len);
|
|
|
|
|
if (buffer) {
|
|
|
|
|
serializeJson(doc, (char*)buffer->get(), len);
|
|
|
|
|
if (ws.enabled()) {
|
|
|
|
|
while (!ws.availableForWriteAll()) {
|
|
|
|
|
Serial.println(String(i) + ") not ready");
|
|
|
|
|
}
|
|
|
|
|
ws.textAll(buffer);
|
|
|
|
|
Serial.println(String(i) + ") ready");
|
2021-12-16 01:35:46 +01:00
|
|
|
|
2021-12-20 02:19:54 +01:00
|
|
|
// if (ws.availableForWriteAll()) {
|
|
|
|
|
// ws.textAll(buffer);
|
|
|
|
|
// Serial.println(String(i) + ") ready");
|
|
|
|
|
// } else {
|
|
|
|
|
// Serial.println(String(i) + ") not ready");
|
|
|
|
|
// delay(100);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Serial.println(doc.as<String>());
|
2021-12-16 01:35:46 +01:00
|
|
|
|
2021-12-16 00:19:08 +01:00
|
|
|
} while (file.findUntil(",", "]"));
|
2021-12-20 02:19:54 +01:00
|
|
|
|
2021-12-16 01:35:46 +01:00
|
|
|
SerialPrint("I", F("WS"), F("completed send config"));
|
2021-12-16 00:19:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wsPublishData(String topic, String data) {
|
2021-12-20 02:19:54 +01:00
|
|
|
if (ws.enabled()) {
|
|
|
|
|
data = "[" + topic + "]" + data;
|
|
|
|
|
ws.textAll(data);
|
|
|
|
|
}
|
2021-12-16 00:19:08 +01:00
|
|
|
}
|
2021-12-19 21:18:50 +01:00
|
|
|
|
|
|
|
|
// wsPublishData(F("config"), doc.as<String>());
|
|
|
|
|
// if (ws.enabled()) {
|
|
|
|
|
//}
|
|
|
|
|
// if (ws.enabled()) Serial.println("on");
|
2021-12-20 02:19:54 +01:00
|
|
|
|
|
|
|
|
// void sendDataWs() {
|
|
|
|
|
// DynamicJsonDocument doc(1024);
|
|
|
|
|
//
|
|
|
|
|
// doc["a"] = "abc";
|
|
|
|
|
// doc["b"] = "abcd";
|
|
|
|
|
// doc["c"] = "abcde";
|
|
|
|
|
// doc["d"] = "abcdef";
|
|
|
|
|
// doc["e"] = "abcdefg";
|
|
|
|
|
// size_t len = measureJson(doc);
|
|
|
|
|
// AsyncWebSocketMessageBuffer* buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
|
|
|
|
|
// if (buffer) {
|
|
|
|
|
// serializeJson(doc, (char*)buffer->get(), len);
|
|
|
|
|
// ws.textAll(buffer);
|
|
|
|
|
// }
|
|
|
|
|
// }
|