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

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

@@ -61,25 +61,25 @@
//#define SSDP_ENABLED //#define SSDP_ENABLED
#ifdef ESP_MODE #ifdef ESP_MODE
//#define EnableButtonIn #define EnableButtonIn
//#define EnableButtonOut #define EnableButtonOut
//#define EnableCountDown #define EnableCountDown
//#define EnableImpulsOut #define EnableImpulsOut
//#define EnableInput #define EnableInput
//#define EnableLogging #define EnableLogging
//#define EnableOutput #define EnableOutput
//#define EnablePwmOut #define EnablePwmOut
//#define EnableSensorAnalog #define EnableSensorAnalog
//#define EnableSensorBme280 #define EnableSensorBme280
//#define EnableSensorBmp280 #define EnableSensorBmp280
//#define EnableSensorCcs811 #define EnableSensorCcs811
//#define EnableSensorDallas #define EnableSensorDallas
//#define EnableSensorDht #define EnableSensorDht
//#define EnableSensorPzem #define EnableSensorPzem
//#define EnableSensorUltrasonic #define EnableSensorUltrasonic
//#define EnableSensorUptime #define EnableSensorUptime
//#define EnableTelegram #define EnableTelegram
//#define EnableUart #define EnableUart
#endif #endif
#ifdef GATE_MODE #ifdef GATE_MODE

View File

@@ -1,7 +1,6 @@
#pragma once #pragma once
//===================Libraries=================================================================================================================================================== //===================Libraries===================================================================================================================================================
#include <Arduino.h> #include <Arduino.h>
#include "CTBot.h"
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <PubSubClient.h> #include <PubSubClient.h>
@@ -10,7 +9,7 @@
#include <Wire.h> #include <Wire.h>
#include <time.h> #include <time.h>
#include "CTBot.h"
#include "Clock.h" #include "Clock.h"
#include "Consts.h" #include "Consts.h"
#include "ESP32.h" #include "ESP32.h"
@@ -40,6 +39,7 @@ extern AsyncWebServer server;
extern boolean just_load; extern boolean just_load;
extern boolean telegramInitBeen; extern boolean telegramInitBeen;
extern boolean savedFromWeb; extern boolean savedFromWeb;
extern boolean wsSetupFlag;
// Json // Json
extern String configSetupJson; //все настройки extern String configSetupJson; //все настройки
@@ -68,6 +68,7 @@ extern int currentBroker;
// orders and events // orders and events
extern String orderBuf; extern String orderBuf;
extern String wsBuf;
extern String eventBuf; extern String eventBuf;
extern String mysensorBuf; extern String mysensorBuf;
extern String itemsFile; extern String itemsFile;

View File

@@ -3,5 +3,7 @@
#include "Global.h" #include "Global.h"
void wsInit(); void wsInit();
void wsSendSetup(); void wsSendSetup();
void wsSendSetupBuffer();
void wsPublishData(String topic, String data); void wsPublishData(String topic, String data);
void sendDataWs(); void sendDataWs();
void loopWsExecute();

View File

@@ -18,6 +18,7 @@ AsyncWebServer server(80);
boolean just_load = true; boolean just_load = true;
boolean telegramInitBeen = false; boolean telegramInitBeen = false;
boolean savedFromWeb = false; boolean savedFromWeb = false;
boolean wsSetupFlag = false;
// Json // Json
String configSetupJson = "{}"; String configSetupJson = "{}";
@@ -45,6 +46,7 @@ int currentBroker = 1;
// orders and events // orders and events
String orderBuf = ""; String orderBuf = "";
String wsBuf = "";
String eventBuf = ""; String eventBuf = "";
String mysensorBuf = ""; String mysensorBuf = "";
String itemsFile = ""; String itemsFile = "";

View File

@@ -116,7 +116,8 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
Serial.printf("%s\n", msg.c_str()); Serial.printf("%s\n", msg.c_str());
if (msg.startsWith("/config")) { if (msg.startsWith("/config")) {
myNotAsyncActions->make(do_webSocketSendSetup); // myNotAsyncActions->make(do_webSocketSendSetup);
wsSetupFlag = true;
} }
if (info->opcode == WS_TEXT) { if (info->opcode == WS_TEXT) {

View File

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

View File

@@ -109,6 +109,13 @@ void loop() {
return; return;
} }
if (wsSetupFlag) {
wsSetupFlag = false;
wsSendSetup();
}
loopWsExecute();
#ifdef OTA_UPDATES_ENABLED #ifdef OTA_UPDATES_ENABLED
ArduinoOTA.handle(); ArduinoOTA.handle();
#endif #endif