добавил наследника для класса отправки в сокеты

This commit is contained in:
Dmitry Borisenko
2021-12-28 00:01:01 +01:00
parent 4a7b130b26
commit be35992c84
9 changed files with 54 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Global.h" #include "Global.h"
#include "Web.h" #include "сlasses/sendJson.h"
#ifdef STANDARD_WEB_SERVER #ifdef STANDARD_WEB_SERVER
extern void standWebServerInit(); extern void standWebServerInit();

View File

@@ -5,4 +5,4 @@
#include "Utils/WiFiUtils.h" #include "Utils/WiFiUtils.h"
#include "AsyncWebServer.h" #include "AsyncWebServer.h"
#include "StandWebServer.h" #include "StandWebServer.h"
#include "Web.h" #include "сlasses/sendJson.h"

View File

@@ -1,21 +1,23 @@
#pragma once #pragma once
#include "Global.h" #include "Global.h"
#include "сlasses/sendJsonWs.h"
class StreamJsonArray; class SendJson;
class StreamJsonArray { class SendJson : SendJsonWs {
public: public:
StreamJsonArray(); SendJson();
~StreamJsonArray(); ~SendJson();
void sendFile(String path, uint8_t num); void sendFile(String path, uint8_t num);
void loop(); void loop();
uint8_t _num;
private: private:
File file; File file;
String _path; String _path;
uint8_t _num;
}; };
extern StreamJsonArray* myStreamJsonArray; extern SendJson* mySendJson;

View File

@@ -0,0 +1,12 @@
#pragma once
#include "Global.h"
class SendJsonWs;
class SendJsonWs {
public:
SendJsonWs();
~SendJsonWs();
void send(String& jsonArrayElement, uint8_t& _num);
};

View File

@@ -211,7 +211,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
} }
if (payloadStr.startsWith("/config")) { if (payloadStr.startsWith("/config")) {
if (myStreamJsonArray) myStreamJsonArray->sendFile("/config.json", num); if (mySendJson) mySendJson->sendFile("/config.json", num);
} }
if (payloadStr.startsWith("/changed")) { if (payloadStr.startsWith("/changed")) {

View File

@@ -1,26 +0,0 @@
#include "Web.h"
StreamJsonArray::StreamJsonArray() {}
StreamJsonArray::~StreamJsonArray() {}
void StreamJsonArray::sendFile(String path, uint8_t num) {
_path = path;
_num = num;
file = seekFile(path);
// file.find("[");
}
void StreamJsonArray::loop() {
// if (ws.availableForWriteAll()) {
if (file.available()) {
String jsonArrayElement = _path + file.readStringUntil('}') + "}";
//jsonArrayElement.replace("]}", "]");
// Serial.println(jsonArrayElement);
standWebSocket.sendTXT(_num, jsonArrayElement);
// ws.textAll(jsonArrayElement);
}
//}
}
StreamJsonArray* myStreamJsonArray;

View File

@@ -32,7 +32,7 @@ void setup() {
#endif #endif
//создаем объект класса выгружающего json массив из файла //создаем объект класса выгружающего json массив из файла
myStreamJsonArray = new StreamJsonArray; mySendJson = new SendJson;
//выводим остаток оперативной памяти после старта //выводим остаток оперативной памяти после старта
// 22.12.21 пустой код без wifi остаток = 50.28 kB // 22.12.21 пустой код без wifi остаток = 50.28 kB
@@ -52,7 +52,7 @@ void loop() {
//обновление задач таскера //обновление задач таскера
ts.update(); ts.update();
if (myStreamJsonArray) myStreamJsonArray->loop(); if (mySendJson) mySendJson->loop();
#ifdef STANDARD_WEB_SERVER #ifdef STANDARD_WEB_SERVER
HTTP.handleClient(); HTTP.handleClient();

21
src/сlasses/sendJson.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include "сlasses/sendJson.h"
//базовый класс
SendJson::SendJson() {}
SendJson::~SendJson() {}
void SendJson::sendFile(String path, uint8_t num) {
_path = path;
_num = num;
file = seekFile(path);
}
void SendJson::loop() {
if (file.available()) {
String jsonArrayElement = _path + file.readStringUntil('}') + "}";
send(jsonArrayElement, _num);
}
}
SendJson* mySendJson;

View File

@@ -0,0 +1,8 @@
#include "сlasses/sendJsonWs.h"
SendJsonWs::SendJsonWs() {}
SendJsonWs::~SendJsonWs() {}
void SendJsonWs::send(String& jsonArrayElement, uint8_t& _num) {
standWebSocket.sendTXT(_num, jsonArrayElement);
}