diff --git a/include/Const.h b/include/Const.h index c6f18da2..aa92e40e 100644 --- a/include/Const.h +++ b/include/Const.h @@ -25,6 +25,8 @@ #define USE_LITTLEFS false #endif +#define QUEUE_FROM_STR + //задачи таскера enum TimerTask_t { WIFI_SCAN, WIFI_MQTT_CONNECTION_CHECK, diff --git a/include/Main.h b/include/Main.h index ded2c075..37a77c8a 100644 --- a/include/Main.h +++ b/include/Main.h @@ -9,7 +9,7 @@ #include "classes/NotAsync.h" #include "ESPConfiguration.h" #include "MqttClient.h" -#include "classes/QueueFromChar.h" -#include "classes/QueueFromInstance.h" +//#include "queue/QueueFromChar.h" +//#include "queue/QueueFromInstance.h" +//#include "queue/QueueInst.h" #include "classes/QueueFromStruct.h" -#include "classes/QueueInst.h" diff --git a/include/classes/QueueFromStruct.h b/include/classes/QueueFromStruct.h index 2ca7623f..020c919b 100644 --- a/include/classes/QueueFromStruct.h +++ b/include/classes/QueueFromStruct.h @@ -1,5 +1,6 @@ #pragma once #include "Global.h" +#ifdef QUEUE_FROM_STR #include #include @@ -7,6 +8,7 @@ using namespace std; struct QueueItems { String myword; + uint8_t num; }; class QueueFromStruct; @@ -19,13 +21,14 @@ class QueueFromStruct { void push(QueueItems word); void pop(); QueueItems front(); + bool empty(); private: queue queue1; QueueItems tmpItem; }; -extern QueueFromStruct* myQueueStruct; +extern QueueFromStruct* filesQueue; //=======проверка очереди из структур================= // myQueueStruct = new QueueFromStruct; @@ -39,3 +42,5 @@ extern QueueFromStruct* myQueueStruct; // Serial.println(myQueueStruct->front().myword); // Serial.println(myQueueStruct->front().myword); // Serial.println(myQueueStruct->front().myword); + +#endif diff --git a/include/classes/SendJson.h b/include/classes/SendJson.h index 69a3c956..915ec16d 100644 --- a/include/classes/SendJson.h +++ b/include/classes/SendJson.h @@ -1,5 +1,6 @@ #pragma once #include "Global.h" +#include "classes/QueueFromStruct.h" class SendJson; @@ -8,7 +9,7 @@ class SendJson { SendJson(); ~SendJson(); - void sendFile(String path, uint8_t num); + void addFileToQueue(String path, uint8_t num); void loop(); @@ -16,12 +17,13 @@ class SendJson { void sendMqtt(String& jsonArrayElement); - uint8_t _num; + QueueItems myItem; private: File file; String _path; + uint8_t _num; + bool sendingInProgress = false; }; -extern SendJson* sendConfigJson; -extern SendJson* sendWigdetsJson; +extern SendJson* sendJsonFiles; diff --git a/include/classes/QueueFromChar.h b/include/queue/QueueFromChar.h similarity index 92% rename from include/classes/QueueFromChar.h rename to include/queue/QueueFromChar.h index 110c5a26..c16b799e 100644 --- a/include/classes/QueueFromChar.h +++ b/include/queue/QueueFromChar.h @@ -1,5 +1,6 @@ #pragma once #include "Global.h" +#ifdef QUEUE_FROM_CHAR #define MAX_COMMAND_LENGTH 16 #define BUFFER 128 @@ -25,7 +26,7 @@ class QueueFromChar { int commandCount = 0; }; -extern QueueFromChar* myBuf; +// extern QueueFromChar* myBuf; //========проверка очереди===================== // myBuf = new QueueFromChar; @@ -37,3 +38,5 @@ extern QueueFromChar* myBuf; // myBuf->getLastCommand(); // myBuf->getLastCommand(); // myBuf->printCommands(); + +#endif diff --git a/include/classes/QueueFromInstance.h b/include/queue/QueueFromInstance.h similarity index 84% rename from include/classes/QueueFromInstance.h rename to include/queue/QueueFromInstance.h index 67102226..0a509bf8 100644 --- a/include/classes/QueueFromInstance.h +++ b/include/queue/QueueFromInstance.h @@ -1,6 +1,7 @@ #pragma once -#include "classes/QueueInst.h" #include "Global.h" +#ifdef QUEUE_FROM_INST +#include "classes/QueueInst.h" #include #include @@ -21,4 +22,5 @@ class QueueFromInstance { queue queue1; }; -extern QueueFromInstance* myQueue; +// extern QueueFromInstance* myQueue; +#endif \ No newline at end of file diff --git a/include/classes/QueueInst.h b/include/queue/QueueInst.h similarity index 100% rename from include/classes/QueueInst.h rename to include/queue/QueueInst.h diff --git a/src/Main.cpp b/src/Main.cpp index 3394cf54..f16e6a9c 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -35,11 +35,11 @@ void setup() { #endif //инициализация mqtt - mqttInit(); + // mqttInit(); //создаем объект класса выгружающего json массив из файла - sendConfigJson = new SendJson; - sendWigdetsJson = new SendJson; + + sendJsonFiles = new SendJson; configure("/config.json"); @@ -62,8 +62,7 @@ void loop() { ts.update(); //отправка json - if (sendConfigJson) sendConfigJson->loop(); - if (sendWigdetsJson) sendWigdetsJson->loop(); + if (sendJsonFiles) sendJsonFiles->loop(); #ifdef STANDARD_WEB_SERVER //обработка web сервера diff --git a/src/StandWebServer.cpp b/src/StandWebServer.cpp index 84cab220..710fda57 100644 --- a/src/StandWebServer.cpp +++ b/src/StandWebServer.cpp @@ -219,8 +219,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) // очередь задавалась бы так: /widgets.json,/config.json, // хорошая идея на завтра) - if (sendWigdetsJson) sendWigdetsJson->sendFile("/widgets.json", num); - if (sendConfigJson) sendConfigJson->sendFile("/config.json", num); + if (sendJsonFiles) sendJsonFiles->addFileToQueue("/widgets.json", num); + if (sendJsonFiles) sendJsonFiles->addFileToQueue("/config.json", num); } if (payloadStr.startsWith("/gifnoc.json")) { //если прилетел измененный пакет с меткой /gifnoc (config наоборот) то перепишем файл, пока переписываем целеком diff --git a/src/classes/QueueFromStruct.cpp b/src/classes/QueueFromStruct.cpp index f44567cc..52c5fb4c 100644 --- a/src/classes/QueueFromStruct.cpp +++ b/src/classes/QueueFromStruct.cpp @@ -1,5 +1,5 @@ #include "classes/QueueFromStruct.h" - +#ifdef QUEUE_FROM_STR QueueFromStruct::QueueFromStruct() {} QueueFromStruct::~QueueFromStruct() {} @@ -24,4 +24,9 @@ QueueItems QueueFromStruct::front() { return tmpItem; } -QueueFromStruct* myQueueStruct; +bool QueueFromStruct::empty() { + return queue1.empty(); +} + +QueueFromStruct* filesQueue; +#endif diff --git a/src/classes/SendJson.cpp b/src/classes/SendJson.cpp index 4b1bc970..10b5ffb0 100644 --- a/src/classes/SendJson.cpp +++ b/src/classes/SendJson.cpp @@ -1,18 +1,32 @@ #include "classes/SendJson.h" -SendJson::SendJson() {} +SendJson::SendJson() { + filesQueue = new QueueFromStruct; +} SendJson::~SendJson() {} -void SendJson::sendFile(String path, uint8_t num) { - _path = path; - _num = num; - file = seekFile(path); +void SendJson::addFileToQueue(String path, uint8_t num) { + myItem.myword = path; + myItem.num = num; + filesQueue->push(myItem); + SerialPrint(F("i"), F("WS"), "file added to Queue " + path); } void SendJson::loop() { + if (!filesQueue->empty() && !sendingInProgress) { + Serial.println("Queue not empty"); + myItem = filesQueue->front(); + _path = myItem.myword; + _num = myItem.num; + file = seekFile(_path); + SerialPrint(F("i"), F("WS"), "seek File to WS " + _path); + sendingInProgress = true; + } if (file.available()) { String jsonArrayElement = _path + file.readStringUntil('}') + "}"; sendWs(jsonArrayElement); + } else { + sendingInProgress = false; } } @@ -24,5 +38,4 @@ void SendJson::sendMqtt(String& jsonArrayElement) { // mqtt send to do } -SendJson* sendConfigJson; -SendJson* sendWigdetsJson; +SendJson* sendJsonFiles; diff --git a/src/classes/QueueFromChar.cpp b/src/queue/QueueFromChar.cpp similarity index 96% rename from src/classes/QueueFromChar.cpp rename to src/queue/QueueFromChar.cpp index 8f52955e..3d7a4bc3 100644 --- a/src/classes/QueueFromChar.cpp +++ b/src/queue/QueueFromChar.cpp @@ -1,3 +1,4 @@ +#ifdef QUEUE_FROM_CHAR #include "classes/QueueFromChar.h" QueueFromChar::QueueFromChar() { @@ -40,4 +41,6 @@ String QueueFromChar::getLastCommand() { return ret; } -QueueFromChar* myBuf; +// QueueFromChar* myBuf; + +#endif diff --git a/src/classes/QueueFromInstance.cpp b/src/queue/QueueFromInstance.cpp similarity index 92% rename from src/classes/QueueFromInstance.cpp rename to src/queue/QueueFromInstance.cpp index 50ff3e4f..69321913 100644 --- a/src/classes/QueueFromInstance.cpp +++ b/src/queue/QueueFromInstance.cpp @@ -1,3 +1,4 @@ +#ifdef QUEUE_FROM_INST #include "classes/QueueFromInstance.h" QueueFromInstance::QueueFromInstance() {} @@ -25,4 +26,5 @@ QueueInstance QueueFromInstance::front() { return instance; } -QueueFromInstance* myQueue; +// QueueFromInstance* myQueue; +#endif diff --git a/src/classes/QueueInst.cpp b/src/queue/QueueInst.cpp similarity index 86% rename from src/classes/QueueInst.cpp rename to src/queue/QueueInst.cpp index f31b0ac5..3d296462 100644 --- a/src/classes/QueueInst.cpp +++ b/src/queue/QueueInst.cpp @@ -1,4 +1,4 @@ -#include "classes/QueueInst.h" +#include "queue/QueueInst.h" QueueInstance::QueueInstance(String text) { _text = text;