diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index e80666bf..00000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} diff --git a/include/Class/CircularBuffer.h b/include/Class/CircularBuffer.h new file mode 100644 index 00000000..d44bb300 --- /dev/null +++ b/include/Class/CircularBuffer.h @@ -0,0 +1,106 @@ +#pragma once + +#include + +template + +class CircularBuffer { + public: + CircularBuffer() : _head{0}, _tail{0}, _full{false} {} + + ~CircularBuffer() {} + + 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 CircularBuffer *myWsBuffer; diff --git a/include/Global.h b/include/Global.h index 994691ff..a9b3cfaf 100644 --- a/include/Global.h +++ b/include/Global.h @@ -66,6 +66,10 @@ 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; diff --git a/include/Tests.h b/include/Tests.h index cfec2e53..94006e7f 100644 --- a/include/Tests.h +++ b/include/Tests.h @@ -1,3 +1,4 @@ #pragma once -extern void testsPerform(); \ No newline at end of file +extern void testsPerform(); +extern void testLoop(); \ No newline at end of file diff --git a/include/WebSocket.h b/include/WebSocket.h index f70a4511..f022f2ce 100644 --- a/include/WebSocket.h +++ b/include/WebSocket.h @@ -1,5 +1,5 @@ #pragma once - +#include "Class/CircularBuffer.h" #include "Global.h" void wsInit(); void wsSendSetup(); diff --git a/src/Class/CircularBuffer.cpp b/src/Class/CircularBuffer.cpp new file mode 100644 index 00000000..a972ac39 --- /dev/null +++ b/src/Class/CircularBuffer.cpp @@ -0,0 +1,3 @@ +#include "Class/CircularBuffer.h" + +CircularBuffer *myWsBuffer; \ No newline at end of file diff --git a/src/Global.cpp b/src/Global.cpp index e7a6ac9f..60d9e952 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -44,6 +44,10 @@ int mqttConnectAttempts = 0; bool changeBroker = false; int currentBroker = 1; +// web sockets +int wsAttempts = 100; +//char* wsBufChar = ""; + // orders and events String orderBuf = ""; String wsBuf = ""; diff --git a/src/Tests.cpp b/src/Tests.cpp index 1778cc17..191d062b 100644 --- a/src/Tests.cpp +++ b/src/Tests.cpp @@ -1,6 +1,7 @@ #include "Tests.h" #include "BufferExecute.h" +#include "Class/CircularBuffer.h" #include "Global.h" #include "ItemsList.h" #include "Macro.h" @@ -8,31 +9,52 @@ #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 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===="); + //ТЕСТ КОЛЬЦЕВОГО БУФЕРА============================================================================= + // CircularBuffer* myCircularBuffer; + // myCircularBuffer = new CircularBuffer; + // + // char* buf = "text"; + // + // for (int i = 1; i <= 5; i++) { + // myCircularBuffer->push(buf); + //} + // + // char* item; + // + // while (myCircularBuffer->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 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); } \ No newline at end of file diff --git a/src/WebSocket.cpp b/src/WebSocket.cpp index 2c90838d..f5765956 100644 --- a/src/WebSocket.cpp +++ b/src/WebSocket.cpp @@ -1,6 +1,7 @@ #include "WebSocket.h" #include "ArduinoJson.h" +#include "Class/CircularBuffer.h" #include "Class/NotAsync.h" #include "Global.h" @@ -22,11 +23,10 @@ void wsPublishData(String topic, String data) { } } -//отправка setup массива в sockets способом через string +//отправка setup массива в sockets способом через буфер string, рабочий способ но буфер стринг - плохой метод void wsSendSetup() { File file = seekFile("/setup.json"); DynamicJsonDocument doc(2048); - AsyncWebSocketMessageBuffer(20480); int i = 0; file.find("["); SerialPrint("I", F("WS"), F("start send config")); @@ -41,15 +41,52 @@ void wsSendSetup() { } void loopWsExecute() { + static int attampts = wsAttempts; if (wsBuf.length()) { - if (ws.availableForWriteAll()) { - String tmp = selectToMarker(wsBuf, "\n"); - wsPublishData("config", tmp); - wsBuf = deleteBeforeDelimiter(wsBuf, "\n"); + 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; } } } +//отправка setup массива в sockets способом через кольщевой буфер char +void wsSendSetup2() { + // myWsBuffer = new CircularBuffer; + // 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 = ""; + // serializeJson(doc, (char*)element, 1024); + // Serial.println(element); + // myWsBuffer->push(element); + // + //} while (file.findUntil(",", "]")); + // SerialPrint("I", F("WS"), F("completed send config")); +} + +void loopWsExecute2() { + // char* item; + // if (myWsBuffer->pop(item)) { + // Serial.println(item); + // } +} + //отправка setup массива в sockets способом прямой загрузки в ws buffer void wsSendSetupBuffer() { File file = seekFile("/setup.json"); diff --git a/src/main.cpp b/src/main.cpp index aa4d1df7..ae88d4fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,7 +85,7 @@ void setup() { getFSInfo(); - // testsPerform(); + testsPerform(); just_load = false; initialized = true; @@ -109,6 +109,8 @@ void loop() { return; } + testLoop(); + if (wsSetupFlag) { wsSetupFlag = false; wsSendSetup();