From a2f5927172dcc1acb09c9268b9433cdb50e8d3c7 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Wed, 2 Feb 2022 21:17:50 +0100 Subject: [PATCH] =?UTF-8?q?=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D1=80=D0=B5=D0=BE=D1=80=D0=B3=D0=B0=D0=BD=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/CreateWidget.h | 8 -- include/ESPConfiguration.h | 1 - include/Main.h | 1 + include/MqttClient.h | 2 +- include/StandWebServer.h | 13 --- include/WsServer.h | 15 +++ src/CreateWidget.cpp | 65 ------------- src/EspFileSystem.cpp | 1 - src/MqttClient.cpp | 4 +- src/StandWebServer.cpp | 175 ---------------------------------- src/WsServer.cpp | 187 +++++++++++++++++++++++++++++++++++++ src/classes/IoTSensor.cpp | 11 +-- 12 files changed, 211 insertions(+), 272 deletions(-) delete mode 100644 include/CreateWidget.h create mode 100644 include/WsServer.h delete mode 100644 src/CreateWidget.cpp create mode 100644 src/WsServer.cpp diff --git a/include/CreateWidget.h b/include/CreateWidget.h deleted file mode 100644 index 3e85ecae..00000000 --- a/include/CreateWidget.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "Global.h" - -extern void createWidget(String& parameters); - -extern bool loadWidget(const String& filename, String& buf); - -extern const String getWidgetFile(const String& name); \ No newline at end of file diff --git a/include/ESPConfiguration.h b/include/ESPConfiguration.h index 9ec4dcc8..69d190cf 100644 --- a/include/ESPConfiguration.h +++ b/include/ESPConfiguration.h @@ -1,7 +1,6 @@ #pragma once #include "Global.h" #include "Classes/IoTSensor.h" -#include "CreateWidget.h" extern std::vector iotSensors; // вектор ссылок базового класса IoTSensor - список всех запущенных сенсоров diff --git a/include/Main.h b/include/Main.h index c8243d38..56ff622b 100644 --- a/include/Main.h +++ b/include/Main.h @@ -8,3 +8,4 @@ #include "classes/NotAsync.h" #include "ESPConfiguration.h" #include "MqttClient.h" +#include "WsServer.h" diff --git a/include/MqttClient.h b/include/MqttClient.h index 3612b1af..92674571 100644 --- a/include/MqttClient.h +++ b/include/MqttClient.h @@ -19,7 +19,7 @@ boolean publishData(const String& topic, const String& data); boolean publishChart(const String& topic, const String& data); boolean publishControl(String id, String topic, String state); boolean publishChart_test(const String& topic, const String& data); -boolean publishStatus(const String& topic, const String& data); +boolean publishStatusMqtt(const String& topic, const String& data); boolean publishEvent(const String& topic, const String& data); boolean publishInfo(const String& topic, const String& data); boolean publishAnyJsonKey(const String& topic, const String& key, const String& data); diff --git a/include/StandWebServer.h b/include/StandWebServer.h index 5b1d31d4..230a712b 100644 --- a/include/StandWebServer.h +++ b/include/StandWebServer.h @@ -12,16 +12,3 @@ extern void handleFileCreate(); extern void handleFileList(); #endif #endif - -#ifdef STANDARD_WEB_SOCKETS -extern void standWebSocketsInit(); -extern void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length); -#ifdef ESP32 -extern void hexdump(const void* mem, uint32_t len, uint8_t cols); -#endif -#endif - -void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize); -// void sendMark(const char* filename, const char* mark, uint8_t num); -// void sendFileToWs3(const String& filename, uint8_t num); -// void sendFileToWs4(const String& filename, uint8_t num); diff --git a/include/WsServer.h b/include/WsServer.h new file mode 100644 index 00000000..829e3845 --- /dev/null +++ b/include/WsServer.h @@ -0,0 +1,15 @@ +#pragma once +#include "Global.h" + +#ifdef STANDARD_WEB_SOCKETS +extern void standWebSocketsInit(); +extern void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length); +#ifdef ESP32 +extern void hexdump(const void* mem, uint32_t len, uint8_t cols); +#endif +#endif + +void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize); +// void sendMark(const char* filename, const char* mark, uint8_t num); +// void sendFileToWs3(const String& filename, uint8_t num); +// void sendFileToWs4(const String& filename, uint8_t num); \ No newline at end of file diff --git a/src/CreateWidget.cpp b/src/CreateWidget.cpp deleted file mode 100644 index 0e152f94..00000000 --- a/src/CreateWidget.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "CreateWidget.h" - -void createWidget(String& parameters) { - String id = jsonReadStr(parameters, "id"); - String widget = jsonReadStr(parameters, "widget"); - String order = jsonReadStr(parameters, "order"); - String page = jsonReadStr(parameters, "page"); - String descr = jsonReadStr(parameters, "descr"); - - SerialPrint(F("i"), F("Widget"), "Start create widget: " + widget); - - if (widget != "na") { - String buf = "{}"; - if (!loadWidget(widget, buf)) { - return; - } - // if (_cnt != "") { - // if (widget.indexOf("chart") != -1) jsonWriteStr(buf, "maxCount", _cnt); - // } - -#ifdef GATE_MODE - jsonWriteStr(buf, "info", " "); -#endif - - jsonWriteStr(buf, "page", page); - jsonWriteStr(buf, "order", order); - jsonWriteStr(buf, "descr", descr); - jsonWriteStr(buf, "topic", prex + "/" + id); - - SerialPrint(F("i"), F("Widget"), buf); - -#ifdef LAYOUT_IN_RAM - all_widgets += widget + "\r\n"; -#else - addFileLn("layout.json", buf); -#endif - } -} - -bool loadWidget(const String& widget, String& buf) { - File file = seekFile("/widgets.json"); - file.find("["); - bool ret = false; - while (file.available()) { - String jsonArrayElement = file.readStringUntil('}') + "}"; - if (jsonArrayElement.startsWith(",")) { - jsonArrayElement = jsonArrayElement.substring(1, jsonArrayElement.length()); //это нужно оптимизировать в последствии - } - String name; - if (!jsonRead(jsonArrayElement, F("name"), name)) { //если нет такого ключа в представленном json или он не валидный - SerialPrint(F("E"), F("Config"), "json error " + name); - continue; - } else { - if (name == widget) { - buf = jsonArrayElement; - ret = true; - } - } - } - return ret; -} - -const String getWidgetFile(const String& name) { - return "/widgets/" + name + ".json"; -} diff --git a/src/EspFileSystem.cpp b/src/EspFileSystem.cpp index 4923a351..0a2f4636 100644 --- a/src/EspFileSystem.cpp +++ b/src/EspFileSystem.cpp @@ -12,7 +12,6 @@ bool fileSystemInit() { void globalVarsSync() { settingsFlashJson = readFile(F("settings.json"), 4096); settingsFlashJson.replace("\r\n", ""); - writeFile("layout.json", ""); } void saveSettingsFlashJson() { diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 423a253e..78023b62 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -278,7 +278,7 @@ boolean publishChart_test(const String& topic, const String& data) { return mqtt.publish(path.c_str(), data.c_str(), false); } -boolean publishStatus(const String& topic, const String& data) { +boolean publishStatusMqtt(const String& topic, const String& data) { String path = mqttRootDevice + "/" + topic + "/status"; String json = "{}"; jsonWriteStr(json, "status", data); @@ -364,7 +364,7 @@ void publishState() { // // if (topic != "" && state != "") { // if (topic != "timenow") { - // publishStatus(topic, state); + // publishStatusMqtt(topic, state); // } // } // str = deleteBeforeDelimiter(str, ","); diff --git a/src/StandWebServer.cpp b/src/StandWebServer.cpp index b4951284..7dd065eb 100644 --- a/src/StandWebServer.cpp +++ b/src/StandWebServer.cpp @@ -188,178 +188,3 @@ void handleFileList() { } #endif #endif - -#ifdef STANDARD_WEB_SOCKETS -void standWebSocketsInit() { - standWebSocket.begin(); - standWebSocket.onEvent(webSocketEvent); -} - -void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) { - switch (type) { - case WStype_ERROR: { - Serial.printf("[%u] Error!\n", num); - } break; - - case WStype_DISCONNECTED: { - Serial.printf("[%u] Disconnected!\n", num); - } break; - - case WStype_CONNECTED: { - IPAddress ip = standWebSocket.remoteIP(num); - Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); - standWebSocket.sendTXT(num, "Connected"); - } break; - - case WStype_TEXT: { - //максимальное количество символов заголовка - size_t headerLenth = 7; - String headerStr; - headerStr.reserve(headerLenth + 1); - for (size_t i = 0; i < headerLenth; i++) { - headerStr += (char)payload[i]; - } - - if (headerStr == "/config") { - sendFileToWs5("/items.json", num, 1024); - sendFileToWs5("/widgets.json", num, 1024); - sendFileToWs5("/config.json", num, 1024); - } - - if (headerStr == "/gifnoc") { - writeFileUint8tByFrames("config.json", payload, length, headerLenth, 256); - } - - if (headerStr == "/tuoyal") { - writeFileUint8tByFrames("layout.json", payload, length, headerLenth, 256); - } - - } break; - - case WStype_BIN: { - Serial.printf("[%u] get binary length: %u\n", num, length); - // hexdump(payload, length); - // standWebSocket.sendBIN(num, payload, length); - } break; - - case WStype_FRAGMENT_TEXT_START: { - Serial.printf("[%u] fragment test start: %u\n", num, length); - } break; - - case WStype_FRAGMENT_BIN_START: { - Serial.printf("[%u] fragment bin start: %u\n", num, length); - } break; - - case WStype_FRAGMENT: { - Serial.printf("[%u] fragment: %u\n", num, length); - } break; - - case WStype_FRAGMENT_FIN: { - Serial.printf("[%u] fragment finish: %u\n", num, length); - } break; - - case WStype_PING: { - Serial.printf("[%u] ping: %u\n", num, length); - } break; - - case WStype_PONG: { - Serial.printf("[%u] pong: %u\n", num, length); - } break; - - default: { - Serial.printf("[%u] not recognized: %u\n", num, length); - } break; - } -} - -#ifdef ESP32 -void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) { - const uint8_t* src = (const uint8_t*)mem; - Serial.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); - for (uint32_t i = 0; i < len; i++) { - if (i % cols == 0) { - Serial.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); - } - Serial.printf("%02X ", *src); - src++; - } - Serial.printf("\n"); -} -#endif -#endif - -//посылка данных из файла в бинарном виде -void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize) { - standWebSocket.sendTXT(num, "/st" + String(filename)); - String path = filepath(filename); - auto file = FileFS.open(path, "r"); - if (!file) { - SerialPrint(F("E"), F("FS"), F("reed file error")); - return; - } - size_t fileSize = file.size(); - SerialPrint(F("i"), F("FS"), "Send file '" + String(filename) + "', file size: " + String(fileSize)); - uint8_t payload[frameSize]; - int countRead = file.read(payload, sizeof(payload)); - while (countRead > 0) { - standWebSocket.sendBIN(num, payload, countRead); - countRead = file.read(payload, sizeof(payload)); - } - file.close(); - standWebSocket.sendTXT(num, "/end" + String(filename)); -} - -// void sendMark(const char* filename, const char* mark, uint8_t num) { -// char outChar[strlen(filename) + strlen(mark) + 1]; -// strcpy(outChar, mark); -// strcat(outChar, filename); -// size_t size = strlen(outChar); -// uint8_t outUint[size]; -// for (size_t i = 0; i < size; i++) { -// outUint[i] = uint8_t(outChar[i]); -// } -// standWebSocket.sendBIN(num, outUint, sizeof(outUint)); -// } - -//посылка данных из файла в string -// void sendFileToWs3(const String& filename, uint8_t num) { -// standWebSocket.sendTXT(num, "/st" + filename); -// size_t ws_buffer = 512; -// String path = filepath(filename); -// auto file = FileFS.open(path, "r"); -// if (!file) { -// SerialPrint(F("E"), F("FS"), F("reed file error")); -// } -// size_t fileSize = file.size(); -// SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize)); -// String ret; -// char temp[ws_buffer + 1]; -// int countRead = file.readBytes(temp, sizeof(temp) - 1); -// while (countRead > 0) { -// temp[countRead] = 0; -// ret = temp; -// standWebSocket.sendTXT(num, ret); -// countRead = file.readBytes(temp, sizeof(temp) - 1); -// } -// standWebSocket.sendTXT(num, "/end" + filename); -//} - -//посылка данных из файла в char -// void sendFileToWs4(const String& filename, uint8_t num) { -// standWebSocket.sendTXT(num, "/st" + filename); -// size_t ws_buffer = 512; -// String path = filepath(filename); -// auto file = FileFS.open(path, "r"); -// if (!file) { -// SerialPrint(F("E"), F("FS"), F("reed file error")); -// } -// size_t fileSize = file.size(); -// SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize)); -// char temp[ws_buffer + 1]; -// int countRead = file.readBytes(temp, sizeof(temp) - 1); -// while (countRead > 0) { -// temp[countRead] = 0; -// standWebSocket.sendTXT(num, temp, countRead); -// countRead = file.readBytes(temp, sizeof(temp) - 1); -// } -//} diff --git a/src/WsServer.cpp b/src/WsServer.cpp new file mode 100644 index 00000000..6fafa10b --- /dev/null +++ b/src/WsServer.cpp @@ -0,0 +1,187 @@ +#include "WsServer.h" +#ifdef STANDARD_WEB_SOCKETS +void standWebSocketsInit() { + standWebSocket.begin(); + standWebSocket.onEvent(webSocketEvent); +} + +void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) { + switch (type) { + case WStype_ERROR: { + Serial.printf("[%u] Error!\n", num); + } break; + + case WStype_DISCONNECTED: { + Serial.printf("[%u] Disconnected!\n", num); + } break; + + case WStype_CONNECTED: { + IPAddress ip = standWebSocket.remoteIP(num); + Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); + standWebSocket.sendTXT(num, "Connected"); + } break; + + case WStype_TEXT: { + //максимальное количество символов заголовка + size_t headerLenth = 7; + String headerStr; + headerStr.reserve(headerLenth + 1); + for (size_t i = 0; i < headerLenth; i++) { + headerStr += (char)payload[i]; + } + + if (headerStr == "/") { + sendFileToWs5("/layout.json", num, 1024); + } + + if (headerStr == "/config") { + sendFileToWs5("/items.json", num, 1024); + sendFileToWs5("/widgets.json", num, 1024); + sendFileToWs5("/config.json", num, 1024); + } + + if (headerStr == "/gifnoc") { + writeFileUint8tByFrames("config.json", payload, length, headerLenth, 256); + } + + if (headerStr == "/tuoyal") { + writeFileUint8tByFrames("layout.json", payload, length, headerLenth, 256); + } + + } break; + + case WStype_BIN: { + Serial.printf("[%u] get binary length: %u\n", num, length); + // hexdump(payload, length); + // standWebSocket.sendBIN(num, payload, length); + } break; + + case WStype_FRAGMENT_TEXT_START: { + Serial.printf("[%u] fragment test start: %u\n", num, length); + } break; + + case WStype_FRAGMENT_BIN_START: { + Serial.printf("[%u] fragment bin start: %u\n", num, length); + } break; + + case WStype_FRAGMENT: { + Serial.printf("[%u] fragment: %u\n", num, length); + } break; + + case WStype_FRAGMENT_FIN: { + Serial.printf("[%u] fragment finish: %u\n", num, length); + } break; + + case WStype_PING: { + Serial.printf("[%u] ping: %u\n", num, length); + } break; + + case WStype_PONG: { + Serial.printf("[%u] pong: %u\n", num, length); + } break; + + default: { + Serial.printf("[%u] not recognized: %u\n", num, length); + } break; + } +} + +#ifdef ESP32 +void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) { + const uint8_t* src = (const uint8_t*)mem; + Serial.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); + for (uint32_t i = 0; i < len; i++) { + if (i % cols == 0) { + Serial.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); + } + Serial.printf("%02X ", *src); + src++; + } + Serial.printf("\n"); +} +#endif +#endif + +//посылка данных из файла в бинарном виде +void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize) { + standWebSocket.sendTXT(num, "/st" + String(filename)); + String path = filepath(filename); + auto file = FileFS.open(path, "r"); + if (!file) { + SerialPrint(F("E"), F("FS"), F("reed file error")); + return; + } + size_t fileSize = file.size(); + SerialPrint(F("i"), F("FS"), "Send file '" + String(filename) + "', file size: " + String(fileSize)); + uint8_t payload[frameSize]; + int countRead = file.read(payload, sizeof(payload)); + while (countRead > 0) { + standWebSocket.sendBIN(num, payload, countRead); + countRead = file.read(payload, sizeof(payload)); + } + file.close(); + standWebSocket.sendTXT(num, "/end" + String(filename)); +} + +void publishStatusWs(const String& topic, const String& data) { + String path = mqttRootDevice + "/" + topic + "/status"; + String json = "{}"; + jsonWriteStr(json, "status", data); + // standWebSocket.sendTXT(num, json); + // mqtt.publish(path.c_str(), json.c_str(), false); +} + +// void sendMark(const char* filename, const char* mark, uint8_t num) { +// char outChar[strlen(filename) + strlen(mark) + 1]; +// strcpy(outChar, mark); +// strcat(outChar, filename); +// size_t size = strlen(outChar); +// uint8_t outUint[size]; +// for (size_t i = 0; i < size; i++) { +// outUint[i] = uint8_t(outChar[i]); +// } +// standWebSocket.sendBIN(num, outUint, sizeof(outUint)); +// } + +//посылка данных из файла в string +// void sendFileToWs3(const String& filename, uint8_t num) { +// standWebSocket.sendTXT(num, "/st" + filename); +// size_t ws_buffer = 512; +// String path = filepath(filename); +// auto file = FileFS.open(path, "r"); +// if (!file) { +// SerialPrint(F("E"), F("FS"), F("reed file error")); +// } +// size_t fileSize = file.size(); +// SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize)); +// String ret; +// char temp[ws_buffer + 1]; +// int countRead = file.readBytes(temp, sizeof(temp) - 1); +// while (countRead > 0) { +// temp[countRead] = 0; +// ret = temp; +// standWebSocket.sendTXT(num, ret); +// countRead = file.readBytes(temp, sizeof(temp) - 1); +// } +// standWebSocket.sendTXT(num, "/end" + filename); +//} + +//посылка данных из файла в char +// void sendFileToWs4(const String& filename, uint8_t num) { +// standWebSocket.sendTXT(num, "/st" + filename); +// size_t ws_buffer = 512; +// String path = filepath(filename); +// auto file = FileFS.open(path, "r"); +// if (!file) { +// SerialPrint(F("E"), F("FS"), F("reed file error")); +// } +// size_t fileSize = file.size(); +// SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize)); +// char temp[ws_buffer + 1]; +// int countRead = file.readBytes(temp, sizeof(temp) - 1); +// while (countRead > 0) { +// temp[countRead] = 0; +// standWebSocket.sendTXT(num, temp, countRead); +// countRead = file.readBytes(temp, sizeof(temp) - 1); +// } +//} diff --git a/src/classes/IoTSensor.cpp b/src/classes/IoTSensor.cpp index 611b5530..3cd33cde 100644 --- a/src/classes/IoTSensor.cpp +++ b/src/classes/IoTSensor.cpp @@ -3,7 +3,6 @@ #include "Classes/ScenarioClass3.h" #include "Classes/IoTSensor.h" - IoTSensor::IoTSensor(String parameters) { jsonRead(parameters, "int", _interval); _interval = _interval * 1000; @@ -12,7 +11,7 @@ IoTSensor::IoTSensor(String parameters) { jsonRead(parameters, "multiply", _multiply); jsonRead(parameters, "plus", _plus); jsonRead(parameters, "round", _round); - + String map; jsonRead(parameters, "map", map); if (map != "") { @@ -20,7 +19,7 @@ IoTSensor::IoTSensor(String parameters) { _map2 = selectFromMarkerToMarker(map, ",", 1).toInt(); _map3 = selectFromMarkerToMarker(map, ",", 2).toInt(); _map4 = selectFromMarkerToMarker(map, ",", 3).toInt(); - } + } } IoTSensor::~IoTSensor() {} @@ -44,14 +43,14 @@ void IoTSensor::loop() { void IoTSensor::regEvent(String value, String consoleInfo = "") { eventGen2(_id, value); jsonWriteStr(paramsFlashJson, _id, value); - publishStatus(_id, value); + publishStatusMqtt(_id, value); SerialPrint("I", "Sensor " + consoleInfo, "'" + _id + "' data: " + value + "'"); } -void IoTSensor::regEvent(float value, String consoleInfo = "") { +void IoTSensor::regEvent(float value, String consoleInfo = "") { if (_multiply) value = value * _multiply; if (_plus) value = value + _multiply; - if (_round != 0) { + if (_round != 0) { if (value > 0) { value = (int)(value * _round + 0.5F); value = value / _round;