diff --git a/include/Const.h b/include/Const.h index c6f18da2..fdf10d25 100644 --- a/include/Const.h +++ b/include/Const.h @@ -12,6 +12,8 @@ #define STANDARD_WEB_SERVER #define STANDARD_WEB_SOCKETS +//#define QUEUE_FROM_STR + //#define REST_FILE_OPERATIONS #define MQTT_RECONNECT_INTERVAL 20000 diff --git a/include/classes/QueueFromStruct.h b/include/classes/QueueFromStruct.h new file mode 100644 index 00000000..020c919b --- /dev/null +++ b/include/classes/QueueFromStruct.h @@ -0,0 +1,46 @@ +#pragma once +#include "Global.h" +#ifdef QUEUE_FROM_STR +#include +#include + +using namespace std; + +struct QueueItems { + String myword; + uint8_t num; +}; + +class QueueFromStruct; + +class QueueFromStruct { + public: + QueueFromStruct(); + ~QueueFromStruct(); + + void push(QueueItems word); + void pop(); + QueueItems front(); + bool empty(); + + private: + queue queue1; + QueueItems tmpItem; +}; + +extern QueueFromStruct* filesQueue; + +//=======проверка очереди из структур================= +// myQueueStruct = new QueueFromStruct; +// QueueItems myItem; +// myItem.myword = "word1"; +// myQueueStruct->push(myItem); +// myItem.myword = "word2"; +// myQueueStruct->push(myItem); +// myItem.myword = "word3"; +// myQueueStruct->push(myItem); +// 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..0fabea32 100644 --- a/include/classes/SendJson.h +++ b/include/classes/SendJson.h @@ -1,5 +1,7 @@ #pragma once #include "Global.h" +#ifdef QUEUE_FROM_STR +#include "classes/QueueFromStruct.h" class SendJson; @@ -8,7 +10,7 @@ class SendJson { SendJson(); ~SendJson(); - void sendFile(String path, uint8_t num); + void addFileToQueue(String path, uint8_t num); void loop(); @@ -16,12 +18,14 @@ 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; +#endif diff --git a/src/Main.cpp b/src/Main.cpp index 3394cf54..488719c4 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -37,9 +37,10 @@ void setup() { //инициализация mqtt mqttInit(); - //создаем объект класса выгружающего json массив из файла - sendConfigJson = new SendJson; - sendWigdetsJson = new SendJson; +//создаем объект класса выгружающего json массив из файла +#ifdef QUEUE_FROM_STR + sendJsonFiles = new SendJson; +#endif configure("/config.json"); @@ -61,9 +62,10 @@ void loop() { //обновление задач таскера ts.update(); - //отправка json - if (sendConfigJson) sendConfigJson->loop(); - if (sendWigdetsJson) sendWigdetsJson->loop(); +//отправка json +#ifdef QUEUE_FROM_STR + if (sendJsonFiles) sendJsonFiles->loop(); +#endif #ifdef STANDARD_WEB_SERVER //обработка web сервера diff --git a/src/StandWebServer.cpp b/src/StandWebServer.cpp index 84cab220..bfc44c6d 100644 --- a/src/StandWebServer.cpp +++ b/src/StandWebServer.cpp @@ -218,9 +218,10 @@ 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); +#ifdef QUEUE_FROM_STR + if (sendJsonFiles) sendJsonFiles->addFileToQueue("/widgets.json", num); + if (sendJsonFiles) sendJsonFiles->addFileToQueue("/config.json", num); +#endif } if (payloadStr.startsWith("/gifnoc.json")) { //если прилетел измененный пакет с меткой /gifnoc (config наоборот) то перепишем файл, пока переписываем целеком diff --git a/src/classes/QueueFromStruct.cpp b/src/classes/QueueFromStruct.cpp new file mode 100644 index 00000000..52c5fb4c --- /dev/null +++ b/src/classes/QueueFromStruct.cpp @@ -0,0 +1,32 @@ +#include "classes/QueueFromStruct.h" +#ifdef QUEUE_FROM_STR +QueueFromStruct::QueueFromStruct() {} +QueueFromStruct::~QueueFromStruct() {} + +//добавим элемент в конец очереди +void QueueFromStruct::push(QueueItems word) { + queue1.push(word); +} + +//удалим элемент из начала очереди +void QueueFromStruct::pop() { + if (!queue1.empty()) { + queue1.pop(); + } +} + +//вернуть элемент из начала очереди и удалить его +QueueItems QueueFromStruct::front() { + if (!queue1.empty()) { + tmpItem = queue1.front(); + queue1.pop(); + } + return tmpItem; +} + +bool QueueFromStruct::empty() { + return queue1.empty(); +} + +QueueFromStruct* filesQueue; +#endif diff --git a/src/classes/SendJson.cpp b/src/classes/SendJson.cpp index 4b1bc970..9b84a4a0 100644 --- a/src/classes/SendJson.cpp +++ b/src/classes/SendJson.cpp @@ -1,18 +1,33 @@ #include "classes/SendJson.h" +#ifdef QUEUE_FROM_STR -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 +39,5 @@ void SendJson::sendMqtt(String& jsonArrayElement) { // mqtt send to do } -SendJson* sendConfigJson; -SendJson* sendWigdetsJson; +SendJson* sendJsonFiles; +#endif \ No newline at end of file diff --git a/training/QueueFromChar.cpp b/training/QueueFromChar.cpp new file mode 100644 index 00000000..3d7a4bc3 --- /dev/null +++ b/training/QueueFromChar.cpp @@ -0,0 +1,46 @@ +#ifdef QUEUE_FROM_CHAR +#include "classes/QueueFromChar.h" + +QueueFromChar::QueueFromChar() { + commandList = NULL; + commandCount = 0; +} +QueueFromChar::~QueueFromChar() {} + +//добавление команды в буфер +void QueueFromChar::addCommand(const char* command) { + commandList = (CharBufferStruct*)realloc(commandList, (commandCount + 1) * sizeof(CharBufferStruct)); + strncpy(commandList[commandCount].command, command, MAX_COMMAND_LENGTH); + Serial.println("command added: " + String(command) + " " + String(commandCount)); + commandCount++; +} + +//распечатаем все добавленные команды +void QueueFromChar::printCommands() { + if (commandCount > 0 && commandList != NULL) { + for (int i = 0; i < commandCount; i++) { + Serial.println(commandList[i].command); + } + } +} + +//заберем последнюю из положенных в буфер команд +String QueueFromChar::getLastCommand() { + String ret = "empty"; + if (commandList != NULL) { + int cnt = commandCount - 1; + ret = commandList[cnt].command; + if (cnt > 0) { + delete commandList[cnt].command; + } else if (cnt == 0) { + commandList = NULL; + } + Serial.println("command deleted: " + ret + " " + String(cnt)); + commandCount--; + } + return ret; +} + +// QueueFromChar* myBuf; + +#endif diff --git a/training/QueueFromChar.h b/training/QueueFromChar.h new file mode 100644 index 00000000..c16b799e --- /dev/null +++ b/training/QueueFromChar.h @@ -0,0 +1,42 @@ +#pragma once +#include "Global.h" +#ifdef QUEUE_FROM_CHAR + +#define MAX_COMMAND_LENGTH 16 +#define BUFFER 128 + +class QueueFromChar; + +class QueueFromChar { + public: + QueueFromChar(); + ~QueueFromChar(); + + void addCommand(const char* command); + + void printCommands(); + + String getLastCommand(); + + private: + struct CharBufferStruct { + char command[MAX_COMMAND_LENGTH + 1]; + }; + CharBufferStruct* commandList; + int commandCount = 0; +}; + +// extern QueueFromChar* myBuf; + +//========проверка очереди===================== +// myBuf = new QueueFromChar; +// myBuf->addCommand("zero"); +// myBuf->addCommand("one"); +// myBuf->addCommand("two"); +// myBuf->printCommands(); +// myBuf->getLastCommand(); +// myBuf->getLastCommand(); +// myBuf->getLastCommand(); +// myBuf->printCommands(); + +#endif diff --git a/training/QueueFromInstance.cpp b/training/QueueFromInstance.cpp new file mode 100644 index 00000000..69321913 --- /dev/null +++ b/training/QueueFromInstance.cpp @@ -0,0 +1,30 @@ +#ifdef QUEUE_FROM_INST +#include "classes/QueueFromInstance.h" + +QueueFromInstance::QueueFromInstance() {} +QueueFromInstance::~QueueFromInstance() {} + +//добавим элемент в конец очереди +void QueueFromInstance::push(QueueInstance instance) { + queue1.push(instance); +} + +//удалим элемент из начала очереди +void QueueFromInstance::pop() { + if (!queue1.empty()) { + queue1.pop(); + } +} + +//вернуть элемент из начала очереди и удалить его +QueueInstance QueueFromInstance::front() { + QueueInstance instance(""); + if (!queue1.empty()) { + instance = queue1.front(); + queue1.pop(); + } + return instance; +} + +// QueueFromInstance* myQueue; +#endif diff --git a/training/QueueFromInstance.h b/training/QueueFromInstance.h new file mode 100644 index 00000000..0a509bf8 --- /dev/null +++ b/training/QueueFromInstance.h @@ -0,0 +1,26 @@ +#pragma once +#include "Global.h" +#ifdef QUEUE_FROM_INST +#include "classes/QueueInst.h" +#include +#include + +using namespace std; + +class QueueFromInstance; + +class QueueFromInstance { + public: + QueueFromInstance(); + ~QueueFromInstance(); + + void push(QueueInstance instance); + void pop(); + QueueInstance front(); + + private: + queue queue1; +}; + +// extern QueueFromInstance* myQueue; +#endif \ No newline at end of file diff --git a/training/QueueInst.cpp b/training/QueueInst.cpp new file mode 100644 index 00000000..7439f3a5 --- /dev/null +++ b/training/QueueInst.cpp @@ -0,0 +1,24 @@ +#ifdef QUEUE_FROM_INST +#include "queue/QueueInst.h" + +QueueInstance::QueueInstance(String text) { + _text = text; +} +QueueInstance::~QueueInstance() {} + +String QueueInstance::get() { + return _text; +} + +//========проверка очереди из экземпляров====== + +// myQueue = new QueueFromInstance; + +// myQueue->push(QueueInstance("text1")); +// myQueue->push(QueueInstance("text2")); +// myQueue->push(QueueInstance("text3")); + +// Serial.println(myQueue->front().get()); +// Serial.println(myQueue->front().get()); +// Serial.println(myQueue->front().get()); +#endif \ No newline at end of file diff --git a/training/QueueInst.h b/training/QueueInst.h new file mode 100644 index 00000000..9731980c --- /dev/null +++ b/training/QueueInst.h @@ -0,0 +1,21 @@ +#pragma once +#include "Global.h" +#ifdef QUEUE_FROM_INST +#include +#include + +using namespace std; + +class QueueInstance; + +class QueueInstance { + public: + QueueInstance(String text); + ~QueueInstance(); + + String get(); + + private: + String _text; +}; +#endif