рабочая версия выгрузки длинного пассива в веб

This commit is contained in:
Dmitry Borisenko
2021-12-20 20:46:16 +01:00
parent d3046a921f
commit 329d1cd382
7 changed files with 95 additions and 74 deletions

View File

@@ -1,84 +1,92 @@
#include "WebSocket.h"
#include <ArduinoJson.h>
#include "ArduinoJson.h"
#include "Class/NotAsync.h"
#include "Global.h"
void wsInit() {
myNotAsyncActions->add(
do_webSocketSendSetup, [&](void*) {
wsSendSetup();
},
nullptr);
// myNotAsyncActions->add(
// do_webSocketSendSetup, [&](void*) {
// delay(100);
// wsSendSetup();
// },
// nullptr);
}
void wsPublishData(String topic, String data) {
if (ws.enabled()) {
// if (ws.availableForWriteAll()) {
data = "[" + topic + "]" + data;
ws.textAll(data);
//}
}
}
//отправка setup массива в sockets способом через string
void wsSendSetup() {
File file = seekFile("/setup.json");
DynamicJsonDocument doc(2048);
AsyncWebSocketMessageBuffer(20480);
int i = 0;
file.find("[");
SerialPrint("I", F("WS"), F("start send config"));
do {
i++;
deserializeJson(doc, file);
wsBuf += doc.as<String>() + "\n";
// wsPublishData("config", doc.as<String>());
// Serial.println(doc.as<String>());
} while (file.findUntil(",", "]"));
SerialPrint("I", F("WS"), F("completed send config"));
}
// static bool flag = false;
void loopWsExecute() {
if (wsBuf.length()) {
if (ws.availableForWriteAll()) {
String tmp = selectToMarker(wsBuf, "\n");
wsPublishData("config", tmp);
wsBuf = deleteBeforeDelimiter(wsBuf, "\n");
}
}
}
//отправка setup массива в sockets способом прямой загрузки в ws buffer
void wsSendSetupBuffer() {
File file = seekFile("/setup.json");
DynamicJsonDocument doc(2048);
int i = 0;
file.find("[");
SerialPrint("I", F("WS"), F("start send config"));
do {
i++;
deserializeJson(doc, file);
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");
if (ws.availableForWriteAll()) {
ws.textAll(buffer);
}
ws.textAll(buffer);
Serial.println(String(i) + ") ready");
// 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>());
// Serial.println(doc.as<String>());
} while (file.findUntil(",", "]"));
SerialPrint("I", F("WS"), F("completed send config"));
}
void wsPublishData(String topic, String data) {
if (ws.enabled()) {
data = "[" + topic + "]" + data;
ws.textAll(data);
//Пример прямого доступа к web socket buffer переделанный для arduino json 6
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);
if (buffer) {
serializeJson(doc, (char*)buffer->get(), len);
ws.textAll(buffer);
}
}
// wsPublishData(F("config"), doc.as<String>());
// if (ws.enabled()) {
//}
// if (ws.enabled()) Serial.println("on");
// 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);
// }
// }