проверка

This commit is contained in:
Dmitry Borisenko
2022-01-20 13:36:00 +01:00
parent c4a3dec67c
commit f893a3623d
13 changed files with 311 additions and 20 deletions

View File

@@ -12,6 +12,8 @@
#define STANDARD_WEB_SERVER #define STANDARD_WEB_SERVER
#define STANDARD_WEB_SOCKETS #define STANDARD_WEB_SOCKETS
//#define QUEUE_FROM_STR
//#define REST_FILE_OPERATIONS //#define REST_FILE_OPERATIONS
#define MQTT_RECONNECT_INTERVAL 20000 #define MQTT_RECONNECT_INTERVAL 20000

View File

@@ -0,0 +1,46 @@
#pragma once
#include "Global.h"
#ifdef QUEUE_FROM_STR
#include <queue>
#include <iostream>
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<QueueItems> 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

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include "Global.h" #include "Global.h"
#ifdef QUEUE_FROM_STR
#include "classes/QueueFromStruct.h"
class SendJson; class SendJson;
@@ -8,7 +10,7 @@ class SendJson {
SendJson(); SendJson();
~SendJson(); ~SendJson();
void sendFile(String path, uint8_t num); void addFileToQueue(String path, uint8_t num);
void loop(); void loop();
@@ -16,12 +18,14 @@ class SendJson {
void sendMqtt(String& jsonArrayElement); void sendMqtt(String& jsonArrayElement);
uint8_t _num; QueueItems myItem;
private: private:
File file; File file;
String _path; String _path;
uint8_t _num;
bool sendingInProgress = false;
}; };
extern SendJson* sendConfigJson; extern SendJson* sendJsonFiles;
extern SendJson* sendWigdetsJson; #endif

View File

@@ -38,8 +38,9 @@ void setup() {
mqttInit(); mqttInit();
//создаем объект класса выгружающего json массив из файла //создаем объект класса выгружающего json массив из файла
sendConfigJson = new SendJson; #ifdef QUEUE_FROM_STR
sendWigdetsJson = new SendJson; sendJsonFiles = new SendJson;
#endif
configure("/config.json"); configure("/config.json");
@@ -62,8 +63,9 @@ void loop() {
ts.update(); ts.update();
//отправка json //отправка json
if (sendConfigJson) sendConfigJson->loop(); #ifdef QUEUE_FROM_STR
if (sendWigdetsJson) sendWigdetsJson->loop(); if (sendJsonFiles) sendJsonFiles->loop();
#endif
#ifdef STANDARD_WEB_SERVER #ifdef STANDARD_WEB_SERVER
//обработка web сервера //обработка web сервера

View File

@@ -218,9 +218,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
// не нравится мне это, нужно что бы класс строил очередь и отправлял вначале первый файл потом второй // не нравится мне это, нужно что бы класс строил очередь и отправлял вначале первый файл потом второй
// очередь задавалась бы так: /widgets.json,/config.json, // очередь задавалась бы так: /widgets.json,/config.json,
// хорошая идея на завтра) // хорошая идея на завтра)
#ifdef QUEUE_FROM_STR
if (sendWigdetsJson) sendWigdetsJson->sendFile("/widgets.json", num); if (sendJsonFiles) sendJsonFiles->addFileToQueue("/widgets.json", num);
if (sendConfigJson) sendConfigJson->sendFile("/config.json", num); if (sendJsonFiles) sendJsonFiles->addFileToQueue("/config.json", num);
#endif
} }
if (payloadStr.startsWith("/gifnoc.json")) { //если прилетел измененный пакет с меткой /gifnoc (config наоборот) то перепишем файл, пока переписываем целеком if (payloadStr.startsWith("/gifnoc.json")) { //если прилетел измененный пакет с меткой /gifnoc (config наоборот) то перепишем файл, пока переписываем целеком

View File

@@ -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

View File

@@ -1,18 +1,33 @@
#include "classes/SendJson.h" #include "classes/SendJson.h"
#ifdef QUEUE_FROM_STR
SendJson::SendJson() {} SendJson::SendJson() {
filesQueue = new QueueFromStruct;
}
SendJson::~SendJson() {} SendJson::~SendJson() {}
void SendJson::sendFile(String path, uint8_t num) { void SendJson::addFileToQueue(String path, uint8_t num) {
_path = path; myItem.myword = path;
_num = num; myItem.num = num;
file = seekFile(path); filesQueue->push(myItem);
SerialPrint(F("i"), F("WS"), "file added to Queue " + path);
} }
void SendJson::loop() { 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()) { if (file.available()) {
String jsonArrayElement = _path + file.readStringUntil('}') + "}"; String jsonArrayElement = _path + file.readStringUntil('}') + "}";
sendWs(jsonArrayElement); sendWs(jsonArrayElement);
} else {
sendingInProgress = false;
} }
} }
@@ -24,5 +39,5 @@ void SendJson::sendMqtt(String& jsonArrayElement) {
// mqtt send to do // mqtt send to do
} }
SendJson* sendConfigJson; SendJson* sendJsonFiles;
SendJson* sendWigdetsJson; #endif

View File

@@ -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

42
training/QueueFromChar.h Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,26 @@
#pragma once
#include "Global.h"
#ifdef QUEUE_FROM_INST
#include "classes/QueueInst.h"
#include <queue>
#include <iostream>
using namespace std;
class QueueFromInstance;
class QueueFromInstance {
public:
QueueFromInstance();
~QueueFromInstance();
void push(QueueInstance instance);
void pop();
QueueInstance front();
private:
queue<QueueInstance> queue1;
};
// extern QueueFromInstance* myQueue;
#endif

24
training/QueueInst.cpp Normal file
View File

@@ -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

21
training/QueueInst.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include "Global.h"
#ifdef QUEUE_FROM_INST
#include <queue>
#include <iostream>
using namespace std;
class QueueInstance;
class QueueInstance {
public:
QueueInstance(String text);
~QueueInstance();
String get();
private:
String _text;
};
#endif