From caff040a91a2c2ff601d41ee50ee0438927042c9 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Sun, 9 Oct 2022 21:38:01 +0200 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=BE=D1=81=D1=81=D1=82=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DeviceList.cpp | 4 +-- src/EspFileSystem.cpp | 4 +-- src/WsServer.cpp | 31 ++++++--------------- src/modules/virtual/Loging/Loging.cpp | 40 ++++++++++++++++----------- 4 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/DeviceList.cpp b/src/DeviceList.cpp index 1b817d98..be3e4b2d 100644 --- a/src/DeviceList.cpp +++ b/src/DeviceList.cpp @@ -2,7 +2,7 @@ const String getThisDevice() { String thisDevice = "{}"; - // jsonWriteStr_(thisDevice, F("devicelist_"), ""); //метка для парсинга + 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"))); @@ -68,7 +68,7 @@ void asyncUdpInit() { } bool udpPacketValidation(String& data) { - if (data.indexOf("devicelist") != -1) { + if (data.indexOf("devicelist_") != -1) { return true; } else { return false; diff --git a/src/EspFileSystem.cpp b/src/EspFileSystem.cpp index 817aa8bf..9ede484a 100644 --- a/src/EspFileSystem.cpp +++ b/src/EspFileSystem.cpp @@ -19,8 +19,8 @@ void globalVarsSync() { jsonWriteStr_(settingsFlashJson, "root", mqttRootDevice); jsonWriteStr_(settingsFlashJson, "id", chipId); - // jsonWriteStr_(errorsHeapJson, "errors_", ""); //метка для парсинга - // jsonWriteStr_(ssidListHeapJson, "ssids_", ""); //метка для парсинга + // jsonWriteStr_(errorsHeapJson, "errors_", ""); //метка для парсинга удалить + // jsonWriteStr_(ssidListHeapJson, "ssids_", ""); //метка для парсинга удалить } String getParamsJson() { diff --git a/src/WsServer.cpp b/src/WsServer.cpp index fb7bd9a8..f4de865e 100644 --- a/src/WsServer.cpp +++ b/src/WsServer.cpp @@ -62,7 +62,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) //отвечаем на запрос параметров if (headerStr == "/params|") { String params = "{}"; - // jsonWriteStr(params, "params_", ""); //метка для парсинга + // jsonWriteStr(params, "params_", ""); //метка для парсинга удалить for (std::list::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { if ((*it)->getSubtype() != "Loging") { if ((*it)->iAmLocal) jsonWriteStr(params, (*it)->getID(), (*it)->getValue()); @@ -258,28 +258,13 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) //публикация статус сообщений (недостаток в том что делаем бродкаст всем клиентам поднятым в свелте!!!) void publishStatusWs(const String& topic, const String& data) { - // String path = mqttRootDevice + "/" + topic; - // String json = "{}"; - // jsonWriteStr(json, "status", data); - // jsonWriteStr(json, "topic", path); - // standWebSocket.broadcastTXT(json); + String path = mqttRootDevice + "/" + topic; + String json = "{}"; + jsonWriteStr(json, "status", data); + jsonWriteStr(json, "topic", path); + sendStringToWs("status", json, -1); } -//публикация статус сообщений -// void publishChartWs2(int num, String& data) { -// bool ok = false; -// if (num == -1) { -// ok = standWebSocket.broadcastTXT(data); -// } else { -// ok = standWebSocket.sendTXT(num, data); -// } -// if (ok) { -// SerialPrint(F("i"), F("WS"), F("sent sucsess")); -// } else { -// SerialPrint(F("E"), F("WS"), F("sent error")); -// } -//} - void publishChartWs(int num, String& path) { sendFileToWs(path, num, 1000); } @@ -407,8 +392,8 @@ void sendFileToWsByFrames(const String& filename, const String& header, const St return; } - size_t totalSize = file.size(); - // Serial.println("Send file '" + String(filename) + "', file size: " + String(totalSize)); + // size_t totalSize = file.size(); + // Serial.println("Send file '" + String(filename) + "', file size: " + String(totalSize)); char buf[32]; sprintf(buf, "%04d", json.length() + 12); diff --git a/src/modules/virtual/Loging/Loging.cpp b/src/modules/virtual/Loging/Loging.cpp index 72048ec6..edc80831 100644 --- a/src/modules/virtual/Loging/Loging.cpp +++ b/src/modules/virtual/Loging/Loging.cpp @@ -176,13 +176,15 @@ class Loging : public IoTItem { unsigned long reqUnixTime = strDateToUnix(getItemValue(id + "-date")); if (fileUnixTimeLocal > reqUnixTime && fileUnixTimeLocal < reqUnixTime + 86400) { noData = false; + 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("Loging"), String(f) + ") " + path + ", " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", sent"); } else { @@ -197,6 +199,26 @@ class Loging : public IoTItem { } } + String getAdditionalJson() { + String topic = mqttRootDevice + "/" + id; + String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\"}"; + return json; + } + + 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 clearValue() { + String topic = mqttRootDevice + "/" + id; + String json = "{\"maxCount\":0,\"topic\":\"" + topic + "\",\"status\":[]}"; + String pk = "/string/chart.json|" + json; + // standWebSocket.broadcastTXT(pk); + } + void clearHistory() { String dir = "/lg/" + id; cleanDirectory(dir); @@ -224,20 +246,6 @@ class Loging : public IoTItem { } } - void clearValue() { - String topic = mqttRootDevice + "/" + id; - String json = "{\"maxCount\":0,\"topic\":\"" + topic + "\",\"status\":[]}"; - 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; _wsNum = wsNum;