From 1668ab8aa87488af46edfcfcc438a007513a0c0d Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Mon, 10 Oct 2022 00:16:58 +0200 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/WsServer.h | 9 +- src/DeviceList.cpp | 2 +- src/WsServer.cpp | 209 +----------------- src/modules/virtual/Loging/Loging.cpp | 14 +- .../virtual/LogingDaily/LogingDaily.cpp | 24 +- 5 files changed, 28 insertions(+), 230 deletions(-) diff --git a/include/WsServer.h b/include/WsServer.h index 8c88e92d..ff08d671 100644 --- a/include/WsServer.h +++ b/include/WsServer.h @@ -13,16 +13,9 @@ extern void hexdump(const void* mem, uint32_t len, uint8_t cols); #endif #endif -void sendFileToWs(String filename, int num, size_t frameSize); void publishStatusWs(const String& topic, const String& data); void publishChartWs(int num, String& path); void periodicWsSend(); -void publishChartToWs(String filename, int num, size_t frameSize, int maxCount, String id); - void sendFileToWsByFrames(const String& filename, const String& header, const String& json, uint8_t client_id, size_t frameSize); -void sendStringToWs(const String& header, String& payload, int client_id); - -// 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 +void sendStringToWs(const String& header, String& payload, int client_id); \ No newline at end of file diff --git a/src/DeviceList.cpp b/src/DeviceList.cpp index be3e4b2d..d2f97724 100644 --- a/src/DeviceList.cpp +++ b/src/DeviceList.cpp @@ -2,7 +2,7 @@ const String getThisDevice() { String thisDevice = "{}"; - jsonWriteStr_(thisDevice, F("devicelist_"), ""); //метка для парсинга нужна для udp валидации + jsonWriteStr_(thisDevice, F("devicelist_"), ""); //метка для парсинга нужна для udp валидации может быть рабочей группой в последствии jsonWriteStr_(thisDevice, F("ip"), jsonReadStr(settingsFlashJson, F("ip"))); jsonWriteStr_(thisDevice, F("id"), jsonReadStr(settingsFlashJson, F("id"))); jsonWriteStr_(thisDevice, F("name"), jsonReadStr(settingsFlashJson, F("name"))); diff --git a/src/WsServer.cpp b/src/WsServer.cpp index f4de865e..b6f03380 100644 --- a/src/WsServer.cpp +++ b/src/WsServer.cpp @@ -266,7 +266,7 @@ void publishStatusWs(const String& topic, const String& data) { } void publishChartWs(int num, String& path) { - sendFileToWs(path, num, 1000); + // sendFileToWs(path, num, 1000); } //данные которые мы отправляем в сокеты переодически @@ -292,93 +292,6 @@ void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) { #endif #endif -//посылка данных из файла в бинарном виде -void sendFileToWs(String filename, int num, size_t frameSize) { - // String st = "/st" + String(filename); - // if (num == -1) { - // standWebSocket.broadcastTXT(st); - // } else { - // standWebSocket.sendTXT(num, st); - // } - // - // 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) { - // if (num == -1) { - // standWebSocket.broadcastBIN(payload, countRead); - // } else { - // standWebSocket.sendBIN(num, payload, countRead); - // } - // countRead = file.read(payload, sizeof(payload)); - //} - // file.close(); - // String end = "/end" + String(filename); - // if (num == -1) { - // standWebSocket.broadcastTXT(end); - //} else { - // standWebSocket.sendTXT(num, end); - //} -} - -//посылка данных из string -// void sendStringToWs(const String& msg, uint8_t num, String name) { -// String st = "/st" + String(name); -// standWebSocket.sendTXT(num, st); -// size_t size = msg.length(); -// char dataArray[size]; -// msg.toCharArray(dataArray, size); -// standWebSocket.sendBIN(num, (uint8_t*)dataArray, size); -// String end = "/end" + String(name); -// standWebSocket.sendTXT(num, end); -//} - -//особая функция отправки графиков в веб -void publishChartToWs(String filename, int num, size_t frameSize, int maxCount, String id) { - // String json; - // jsonWriteStr(json, "topic", mqttRootDevice + "/" + id); - // jsonWriteInt(json, "maxCount", maxCount); - // - // String st = "/st/chart.json|" + json; - // if (num == -1) { - // standWebSocket.broadcastTXT(st); - //} else { - // standWebSocket.sendTXT(num, st); - //} - // 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) { - // if (num == -1) { - // standWebSocket.broadcastBIN(payload, countRead); - // } else { - // standWebSocket.sendBIN(num, payload, countRead); - // } - // countRead = file.read(payload, sizeof(payload)); - //} - // file.close(); - // String end = "/end/chart.json|" + json; - // if (num == -1) { - // standWebSocket.broadcastTXT(end); - //} else { - // standWebSocket.sendTXT(num, end); - //} -} - void sendFileToWsByFrames(const String& filename, const String& header, const String& json, uint8_t client_id, size_t frameSize) { if (header.length() != 6) { SerialPrint("E", "FS", F("wrong header size")); @@ -434,7 +347,11 @@ void sendFileToWsByFrames(const String& filename, const String& header, const St } // Serial.println(String(i) + ") fr sz: " + String(size) + " fin: " + String(fin) + " cnt: " + String(continuation)); - standWebSocket.sendBIN(client_id, frameBuf, size, fin, continuation); + if (client_id == -1) { + standWebSocket.broadcastBIN(frameBuf, size, fin, continuation); + } else { + standWebSocket.sendBIN(client_id, frameBuf, size, fin, continuation); + } } i++; } @@ -461,117 +378,3 @@ void sendStringToWs(const String& header, String& payload, int client_id) { standWebSocket.sendBIN(client_id, (uint8_t*)dataArray, totalSize); } } - -// void sendFileToWsByFrames(const String& filename, const String& header, const String& json, uint8_t client_id, size_t frameSize) { -// if (header.length() != 6) { -// SerialPrint("E", "FS", F("wrong header size")); -// return; -// } -// // откроем файл -// auto path = filepath(filename); -// auto file = FileFS.open(path, "r"); -// if (!file) { -// SerialPrint("E", "FS", F("reed file error")); -// return; -// } -// -// size_t totalSize = file.size(); -// SerialPrint("I", "FS", "Send file '" + String(filename) + "', file size: " + String(totalSize)); -// -// char buf[32]; -// sprintf(buf, "%04d", json.length() + 12); -// -// String data = header + "|" + String(buf) + "|" + json; -// -// // размер заголовка -// auto headerSize = data.length(); -// // выделим буфер размером с фрейм -// auto frameBuf = new uint8_t[frameSize]; -// // заголовок у нас не меняется, запием его в начало буфера -// data.toCharArray((char*)frameBuf, frameSize); -// // указатель на начало полезной нагрузки -// auto payloadBuf = &frameBuf[headerSize]; -// // и сколько осталось места для нее -// auto maxPayloadSize = frameSize - headerSize; -// int i = 0; -// while (file.available()) { -// // прочитаем кусок в буфер -// size_t payloadSize = file.read(payloadBuf, maxPayloadSize); -// if (payloadSize) { -// size_t size = headerSize + payloadSize; -// -// bool fin = false; -// if (size == frameSize) { -// fin = false; -// } else { -// fin = true; -// } -// -// bool continuation = false; -// if (i == 0) { -// continuation = false; -// } else { -// continuation = true; -// } -// -// SerialPrint("I", "FS", String(i) + ") sz: " + String(size) + " fin: " + String(fin) + " cnt: " + String(continuation)); -// standWebSocket.sendBIN(client_id, frameBuf, size, fin, continuation); -// } -// i++; -// } -// } - -// 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/modules/virtual/Loging/Loging.cpp b/src/modules/virtual/Loging/Loging.cpp index edc80831..c0aea13b 100644 --- a/src/modules/virtual/Loging/Loging.cpp +++ b/src/modules/virtual/Loging/Loging.cpp @@ -181,10 +181,9 @@ class Loging : public IoTItem { publishChartFileToMqtt(path, id, calculateMaxCount()); } else if (_publishType == TO_WS) { sendFileToWsByFrames(path, "charta", json, _wsNum, WEB_SOCKETS_FRAME_SIZE); - } else if (_publishType == TO_MQTT_WS) { - publishChartFileToMqtt(path, id, calculateMaxCount()); sendFileToWsByFrames(path, "charta", json, _wsNum, WEB_SOCKETS_FRAME_SIZE); + publishChartFileToMqtt(path, id, calculateMaxCount()); } SerialPrint("i", F("Loging"), String(f) + ") " + path + ", " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", sent"); } else { @@ -208,15 +207,13 @@ class Loging : public IoTItem { void publishChartToWsSinglePoint(String value) { String topic = mqttRootDevice + "/" + id; String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}"; - String pk = "/string/chart.json|" + json; - // standWebSocket.broadcastTXT(pk); + sendStringToWs("chartb", json, -1); } void clearValue() { String topic = mqttRootDevice + "/" + id; String json = "{\"maxCount\":0,\"topic\":\"" + topic + "\",\"status\":[]}"; - String pk = "/string/chart.json|" + json; - // standWebSocket.broadcastTXT(pk); + sendStringToWs("chartb", json, -1); } void clearHistory() { @@ -246,7 +243,7 @@ class Loging : public IoTItem { } } - void setPublishDestination(int publishType, int wsNum = -1) { + void setPublishDestination(int publishType, int wsNum) { _publishType = publishType; _wsNum = wsNum; } @@ -321,8 +318,7 @@ class Date : public IoTItem { for (std::list::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { if ((*it)->getSubtype() == "Loging") { if ((*it)->getID() == selectToMarker(id, "-")) { - (*it)->setPublishDestination(TO_MQTT_WS); - (*it)->clearValue(); + (*it)->setPublishDestination(TO_MQTT_WS, -1); (*it)->publishValue(); } } diff --git a/src/modules/virtual/LogingDaily/LogingDaily.cpp b/src/modules/virtual/LogingDaily/LogingDaily.cpp index 529ca3ef..05981d39 100644 --- a/src/modules/virtual/LogingDaily/LogingDaily.cpp +++ b/src/modules/virtual/LogingDaily/LogingDaily.cpp @@ -168,14 +168,14 @@ class LogingDaily : public IoTItem { path = "/lgd/" + id + path; f++; - + String json = getAdditionalJson(); if (_publishType == TO_MQTT) { publishChartFileToMqtt(path, id, calculateMaxCount()); } else if (_publishType == TO_WS) { - publishChartToWs(path, _wsNum, 1000, calculateMaxCount(), id); + sendFileToWsByFrames(path, "charta", json, _wsNum, WEB_SOCKETS_FRAME_SIZE); } else if (_publishType == TO_MQTT_WS) { publishChartFileToMqtt(path, id, calculateMaxCount()); - publishChartToWs(path, _wsNum, 1000, calculateMaxCount(), id); + sendFileToWsByFrames(path, "charta", json, _wsNum, WEB_SOCKETS_FRAME_SIZE); } SerialPrint("i", F("LogingDaily"), String(f) + ") " + path + ", sent"); @@ -183,17 +183,23 @@ class LogingDaily : public IoTItem { } } + String getAdditionalJson() { + String topic = mqttRootDevice + "/" + id; + String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\"}"; + return json; + } + void clearHistory() { String dir = "/lgd/" + id; cleanDirectory(dir); } - void publishChartToWsSinglePoint(String value) { - String topic = mqttRootDevice + "/" + id; - String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}"; - String pk = "/string/chart.json|" + json; - //standWebSocket.broadcastTXT(pk); - } + // void publishChartToWsSinglePoint(String value) { + // String topic = mqttRootDevice + "/" + id; + // String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}"; + // String pk = "/string/chart.json|" + json; + // standWebSocket.broadcastTXT(pk); + // } void setPublishDestination(int publishType, int wsNum = -1) { _publishType = publishType;