Merge pull request #108 from DmitryBorisenko33/ver4

Ver4
This commit is contained in:
Dmitry Borisenko
2021-12-21 22:01:59 +01:00
committed by GitHub
15 changed files with 358 additions and 73 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

7
data_svelte/lile.json Normal file
View File

@@ -0,0 +1,7 @@
{
"type": "binary",
"subtype": "button-out",
"id": "btn1",
"gpio": 1,
"inv": false
}

View File

@@ -0,0 +1,107 @@
#pragma once
#include <Arduino.h>
template <typename T, size_t BUFFER_SIZE>
class TCircularBuffer {
public:
TCircularBuffer() : _head{0}, _tail{0}, _full{false} {}
~TCircularBuffer() {}
void reset() {
_head = _tail = _full = 0;
}
bool empty() const {
return _head == _tail && !_full;
}
bool full() const {
return _full;
}
/**
* Get the number of elements currently stored in the circular_buffer.
*/
size_t size() const {
size_t res = 0;
if (!_full) {
if (_head < _tail)
res = BUFFER_SIZE + _head - _tail;
else
res = _head - _tail;
} else {
res = BUFFER_SIZE;
}
return res;
}
/** Push the transaction to the buffer. This overwrites the buffer if it's full.
* Загрузить данные в буфер
* @param data item to be pushed to the buffer.
*/
void push(const T &item) {
if (_full) {
_tail++;
if (_tail == BUFFER_SIZE)
_tail = 0;
}
_pool[_head++] = item;
if (_head == BUFFER_SIZE)
_head = 0;
if (_head == _tail)
_full = true;
}
/** Pop from the buffer.
* Забрать данные из буфера
* @param data item to store the data to be popped from the buffer.
* @return True if data popped.
*/
bool pop(T &item) {
bool res = false;
if (!empty()) {
item = _pool[_tail++];
if (_tail == BUFFER_SIZE) _tail = 0;
_full = false;
res = true;
}
return res;
}
bool pop_back(T &item) {
bool res = false;
if (!empty()) {
item = _pool[--_head];
_full = false;
res = true;
}
return res;
}
/** Peek into circular buffer without popping.
*
* @param data item to be peeked from the buffer.
* @return True if the buffer is not empty and data contains a transaction, false otherwise.
*/
bool peek(T &item) const {
bool res = false;
if (!empty()) {
item = _pool[_tail];
res = true;
}
return res;
}
private:
T _pool[BUFFER_SIZE];
size_t _head;
size_t _tail;
bool _full;
};
extern TCircularBuffer<char *, 1024> *myTCircularBuffer;
extern TCircularBuffer<char *, 20480> *myWsBuffer;

View File

@@ -39,6 +39,7 @@ extern AsyncWebServer server;
extern boolean just_load;
extern boolean telegramInitBeen;
extern boolean savedFromWeb;
extern boolean wsSetupFlag;
// Json
extern String configSetupJson; //все настройки
@@ -65,8 +66,13 @@ extern int mqttConnectAttempts;
extern bool changeBroker;
extern int currentBroker;
// web sockets
extern int wsAttempts;
//extern char* wsBufChar;
// orders and events
extern String orderBuf;
extern String wsBuf;
extern String eventBuf;
extern String mysensorBuf;
extern String itemsFile;

View File

@@ -1,3 +1,4 @@
#pragma once
extern void testsPerform();
extern void testLoop();

View File

@@ -1,6 +1,11 @@
#pragma once
#include "Class/TCircularBuffer.h"
#include "Global.h"
void wsInit();
void wsSendSetup();
void wsPublishData(String topic, String data);
// void wsSendSetup();
// void wsSendSetupBuffer();
//
// void sendDataWs();
// void loopWsExecute();

View File

@@ -0,0 +1,4 @@
#include "Class/TCircularBuffer.h"
TCircularBuffer<char *, 1024> *myTCircularBuffer;
TCircularBuffer<char *, 20480> *myWsBuffer;

View File

@@ -18,6 +18,7 @@ AsyncWebServer server(80);
boolean just_load = true;
boolean telegramInitBeen = false;
boolean savedFromWeb = false;
boolean wsSetupFlag = false;
// Json
String configSetupJson = "{}";
@@ -43,8 +44,13 @@ int mqttConnectAttempts = 0;
bool changeBroker = false;
int currentBroker = 1;
// web sockets
int wsAttempts = 100;
//char* wsBufChar = "";
// orders and events
String orderBuf = "";
String wsBuf = "";
String eventBuf = "";
String mysensorBuf = "";
String itemsFile = "";

View File

@@ -75,8 +75,8 @@ void deviceInit() {
}
savedFromWeb = false;
//publishWidgets();
//publishState();
// publishWidgets();
// publishState();
}
void loadScenario() {

View File

@@ -1,6 +1,7 @@
#include "Tests.h"
#include "BufferExecute.h"
#include "Class/TCircularBuffer.h"
#include "Global.h"
#include "ItemsList.h"
#include "Macro.h"
@@ -8,31 +9,51 @@
#include "Utils/StringUtils.h"
void testsPerform() {
//Serial.println("====some tests section====");
////===========================================================================
//String str = "0;1;2;3;4";
//char* mychar = new char[str.length() + 1];
//strcpy(mychar, str.c_str());
//test(mychar);
////===========================================================================
//String myJson;
//const int capacity = JSON_ARRAY_SIZE(2) + 3 * JSON_OBJECT_SIZE(3);
//StaticJsonBuffer<capacity> jb;
//JsonArray& arr = jb.createArray();
//JsonObject& obj1 = jb.createObject();
//obj1["test1"] = 1;
//obj1["test2"] = 2;
//obj1["test3"] = 3;
//arr.add(obj1);
//arr.printTo(myJson);
//Serial.println(myJson);
Serial.println("====some tests section====");
//ТЕСТ КОЛЬЦЕВОГО БУФЕРА=============================================================================
myTCircularBuffer = new TCircularBuffer<char*, 1024>;
char* buf = "text";
for (int i = 1; i <= 5; i++) {
myTCircularBuffer->push(buf);
}
char* item;
while (myTCircularBuffer->pop(item)) {
Serial.println(item);
}
//===================================================================================================
// String str = "0;1;2;3;4";
// char* mychar = new char[str.length() + 1];
// strcpy(mychar, str.c_str());
// test(mychar);
//===========================================================================
// String myJson;
// const int capacity = JSON_ARRAY_SIZE(2) + 3 * JSON_OBJECT_SIZE(3);
// StaticJsonBuffer<capacity> jb;
// JsonArray& arr = jb.createArray();
// JsonObject& obj1 = jb.createObject();
// obj1["test1"] = 1;
// obj1["test2"] = 2;
// obj1["test3"] = 3;
// arr.add(obj1);
// arr.printTo(myJson);
// Serial.println(myJson);
//
//
//
////===========================================================================
////Serial.println(isDigitDotCommaStr("-12552.5555"));
////String str = "Geeks for Geeks ";
////Serial.println(itemsCount2(str, " "));
//===========================================================================
// Serial.println(isDigitDotCommaStr("-12552.5555"));
// String str = "Geeks for Geeks ";
// Serial.println(itemsCount2(str, " "));
//
//Serial.println("==========end============");
Serial.println("==========end============");
}
void testLoop() {
// char* item;
// myCircularBuffer->pop(item);
// Serial.println(item);
}

View File

@@ -1,4 +1,5 @@
#include "Web.h"
#include "BufferExecute.h"
#include "Class/NotAsync.h"
#include "Global.h"
@@ -174,7 +175,7 @@ void web_init() {
}
//==============================mqtt settings=============================================
//primary
// primary
if (request->hasArg(F("mqttServer"))) {
jsonWriteStr(configSetupJson, F("mqttServer"), request->getParam(F("mqttServer"))->value());
saveConfig();
@@ -206,7 +207,7 @@ void web_init() {
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
//secondary
// secondary
if (request->hasArg(F("mqttServer2"))) {
jsonWriteStr(configSetupJson, F("mqttServer2"), request->getParam(F("mqttServer2"))->value());
saveConfig();
@@ -240,7 +241,7 @@ void web_init() {
}
if (request->hasArg("mqttsend")) {
//myNotAsyncActions->make(do_MQTTUDP);
// myNotAsyncActions->make(do_MQTTUDP);
request->send(200);
}
@@ -336,7 +337,7 @@ void web_init() {
serverIP = jsonReadStr(configSetupJson, "serverip");
request->send(200);
}
//set?order=button_1
// set?order=button_1
if (request->hasArg("order")) {
String order = request->getParam("order")->value();
order.replace("_", " ");
@@ -351,7 +352,7 @@ void web_init() {
request->send(200);
}
//gate mode
// gate mode
if (request->hasArg("gateAuto")) {
bool value = request->getParam("gateAuto")->value().toInt();
@@ -359,19 +360,18 @@ void web_init() {
saveConfig();
request->send(200);
}
});
//server.on("/del", HTTP_GET, [](AsyncWebServerRequest* request) {
// if (request->hasArg("file") && request->hasArg("line")) {
// String fileName = request->getParam("file")->value();
// Serial.println(fileName);
// int line = request->getParam("line")->value().toInt();
// Serial.println(line);
// myNotAsyncActions->make(do_delChoosingItems);
// request->redirect(F("/?set.device"));
// }
//});
// server.on("/del", HTTP_GET, [](AsyncWebServerRequest* request) {
// if (request->hasArg("file") && request->hasArg("line")) {
// String fileName = request->getParam("file")->value();
// Serial.println(fileName);
// int line = request->getParam("line")->value().toInt();
// Serial.println(line);
// myNotAsyncActions->make(do_delChoosingItems);
// request->redirect(F("/?set.device"));
// }
// });
server.on("/check", HTTP_GET, [](AsyncWebServerRequest* request) {
myNotAsyncActions->make(do_GETLASTVERSION);
@@ -402,8 +402,8 @@ void web_init() {
});
/*
* Upgrade
*/
* Upgrade
*/
server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest* request) {
myNotAsyncActions->make(do_UPGRADE);
request->send(200, "text/html");

View File

@@ -11,7 +11,6 @@ AsyncWebSocket ws("/ws");
AsyncEventSource events("/events");
void HttpServerinit() {
wsInit();
String login = jsonReadStr(configSetupJson, "weblogin");
String pass = jsonReadStr(configSetupJson, "webpass");
#ifdef ESP32
@@ -20,6 +19,12 @@ void HttpServerinit() {
server.addHandler(new FSEditor(login, pass));
#endif
//#ifdef CORS_DEBUG
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Origin"), F("*"));
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Headers"), F("content-type"));
//#endif
// server.sendHeader("Access-Control-Allow-Origin", "*");
server.serveStatic("/css/", FileFS, "/css/").setCacheControl("max-age=600");
server.serveStatic("/js/", FileFS, "/js/").setCacheControl("max-age=600");
server.serveStatic("/favicon.ico", FileFS, "/favicon.ico").setCacheControl("max-age=600");
@@ -32,9 +37,17 @@ void HttpServerinit() {
server.serveStatic("/", FileFS, "/").setDefaultFile("index.htm").setAuthentication(login.c_str(), pass.c_str());
#endif
//server.onNotFound([](AsyncWebServerRequest *request) {
// SerialPrint("[E]", "WebServer", "not found:\n" + getRequestInfo(request));
// request->send(404);
//});
server.onNotFound([](AsyncWebServerRequest *request) {
SerialPrint("[E]", "WebServer", "not found:\n" + getRequestInfo(request));
request->send(404);
if (request->method() == HTTP_OPTIONS) {
request->send(200);
} else {
request->send(404);
}
});
server.onFileUpload([](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) {
@@ -47,6 +60,11 @@ void HttpServerinit() {
}
});
server.on("/file.json", HTTP_GET, [](AsyncWebServerRequest *request) {
String file = readFile("file.json", 1024);
request->send(200, "application/json", file);
});
// динамические данные
server.on("/config.live.json", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "application/json", configLiveJson);
@@ -116,7 +134,8 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
Serial.printf("%s\n", msg.c_str());
if (msg.startsWith("/config")) {
myNotAsyncActions->make(do_webSocketSendSetup);
// myNotAsyncActions->make(do_webSocketSendSetup);
// wsSetupFlag = true;
}
if (info->opcode == WS_TEXT) {

View File

@@ -1,39 +1,135 @@
#include "WebSocket.h"
#include "ArduinoJson.h"
#include "Class/NotAsync.h"
#include "Class/TCircularBuffer.h"
#include "Global.h"
void wsInit() {
myNotAsyncActions->add(
do_webSocketSendSetup, [&](void*) {
wsSendSetup();
},
nullptr);
// myWsBuffer = new TCircularBuffer<char*, 20480>;
// myNotAsyncActions->add(
// do_webSocketSendSetup, [&](void*) {
// delay(100);
// wsSendSetup();
// },
// nullptr);
}
void wsSendSetup() {
SerialPrint("I", F("WS"), F("start send config"));
void wsPublishData(String topic, String data) {
if (ws.enabled()) {
if (ws.availableForWriteAll()) {
data = "[" + topic + "]" + data;
ws.textAll(data);
}
}
}
//отправка setup массива в sockets способом через буфер string, рабочий способ но буфер стринг - плохой метод
void wsSendSetup3() {
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);
ws.textAll("[config]" + doc.as<char>());
Serial.println(doc.as<String>());
wsBuf += doc.as<String>() + "\n";
} while (file.findUntil(",", "]"));
SerialPrint("I", F("WS"), F("completed send config"));
}
void wsPublishData(String topic, String data) {
data = "[" + topic + "]" + data;
ws.textAll(data);
void loopWsExecute3() {
static int attampts = wsAttempts;
if (wsBuf.length()) {
if (attampts > 0) {
if (ws.availableForWriteAll()) {
String tmp = selectToMarker(wsBuf, "\n");
wsPublishData("config", tmp);
wsBuf = deleteBeforeDelimiter(wsBuf, "\n");
attampts = wsAttempts;
} else {
attampts--;
SerialPrint("I", F("WS"), String(attampts));
}
} else {
SerialPrint("I", F("WS"), F("socket fatal error"));
attampts = wsAttempts;
}
}
}
// wsPublishData(F("config"), doc.as<String>());
// if (ws.enabled()) {
//отправка setup массива в sockets способом через кольцевой буфер char
// void wsSendSetup() {
// 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);
// char* element;
// char* element = new char[1024];
// serializeJson(doc, (char*)element, 1024);
// Serial.println(element);
//
// } while (file.findUntil(",", "]"));
// SerialPrint("I", F("WS"), F("completed send config"));
//
// char* itemsend = "element";
// myWsBuffer->push(itemsend);
//
// char* item;
// while (myWsBuffer->pop(item)) {
// Serial.println(item);
// }
//}
// void loopWsExecute() {
// char* item;
// if (myWsBuffer->pop(item)) {
// Serial.println(item);
// }
// }
//отправка 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);
// size_t len = measureJson(doc);
// AsyncWebSocketMessageBuffer* buffer = ws.makeBuffer(len);
// if (buffer) {
// serializeJson(doc, (char*)buffer->get(), len);
// if (ws.enabled()) {
// if (ws.availableForWriteAll()) {
// ws.textAll(buffer);
// }
// }
// }
// // Serial.println(doc.as<String>());
// } while (file.findUntil(",", "]"));
// SerialPrint("I", F("WS"), F("completed send config"));
//}
//Пример прямого доступа к web socket buffer переделанный для arduino json 6
// 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);
// if (buffer) {
// serializeJson(doc, (char*)buffer->get(), len);
// ws.textAll(buffer);
// }
//}
// if (ws.enabled()) Serial.println("on");

View File

@@ -20,6 +20,7 @@
#include "Utils/Timings.h"
#include "Utils/WebUtils.h"
#include "WebServer.h"
#include "WebSocket.h"
#include "items/ButtonInClass.h"
#include "items/vCountDown.h"
#include "items/vImpulsOut.h"
@@ -52,6 +53,8 @@ void setup() {
myNotAsyncActions = new NotAsync(do_LAST);
myScenario = new Scenario();
wsInit();
//=========================================initialisation==============================================================
setChipId();
fileSystemInit();
@@ -84,7 +87,7 @@ void setup() {
getFSInfo();
// testsPerform();
testsPerform();
just_load = false;
initialized = true;
@@ -100,13 +103,23 @@ void setup() {
// setupESP();
SerialPrint("I", F("System"), F("✔ Initialization completed"));
SerialPrint("I", F("System"), F("✔ Initialization completed"));
}
void loop() {
if (!initialized) {
return;
}
testLoop();
// if (wsSetupFlag) {
// wsSetupFlag = false;
// wsSendSetup();
//}
// loopWsExecute();
#ifdef OTA_UPDATES_ENABLED
ArduinoOTA.handle();
#endif