From 100477444c3a6bb0a653e5dc413afcef783df11c Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Fri, 23 Sep 2022 02:24:50 +0200 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=87?= =?UTF-8?q?=D0=B0=D1=8F=20=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/Const.h | 10 ++- include/WsServer.h | 4 +- include/classes/IoTItem.h | 2 +- src/MqttClient.cpp | 2 +- src/WsServer.cpp | 51 +++++++---- src/modules/virtual/Logging/Loging.cpp | 117 ++++++++++++++++--------- 6 files changed, 123 insertions(+), 63 deletions(-) diff --git a/include/Const.h b/include/Const.h index 049ed5c7..56bbab57 100644 --- a/include/Const.h +++ b/include/Const.h @@ -70,4 +70,12 @@ enum UpdateStates { NOT_STARTED, UPDATE_BUILD_COMPLETED, UPDATE_BUILD_FAILED, PATH_ERROR -}; \ No newline at end of file +}; + +enum distination { + TO_MQTT, + TO_WS, + TO_MQTT_WS, +}; + +#define WS_BROADCAST -1 \ No newline at end of file diff --git a/include/WsServer.h b/include/WsServer.h index 2504f86f..d09f671b 100644 --- a/include/WsServer.h +++ b/include/WsServer.h @@ -13,9 +13,9 @@ extern void hexdump(const void* mem, uint32_t len, uint8_t cols); #endif #endif -void sendFileToWs(const char* filename, uint8_t num, size_t frameSize); +void sendFileToWs(String filename, int num, size_t frameSize); void publishStatusWs(const String& topic, const String& data); -void publishChartWs(int num, String& json); +void publishChartWs(int num, String& path); void periodicWsSend(); void sendStringToWs(const String& msg, uint8_t num, String name); diff --git a/include/classes/IoTItem.h b/include/classes/IoTItem.h index b61d582b..c8e2a2ed 100644 --- a/include/classes/IoTItem.h +++ b/include/classes/IoTItem.h @@ -51,7 +51,7 @@ class IoTItem { //методы для графиков virtual void publishValue(); virtual void clearValue(); - virtual void setPublishDestination(int type, int num); + virtual void setPublishDestination(int type, int wsNum = -1); virtual void clearHistory(); virtual void setTodayDate(); diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 5677124f..52902620 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -137,7 +137,7 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) { //отправка данных графиков for (std::list::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { if ((*it)->getSubtype() == "Loging") { - (*it)->setPublishDestination(1, -1); + (*it)->setPublishDestination(TO_MQTT); (*it)->publishValue(); } } diff --git a/src/WsServer.cpp b/src/WsServer.cpp index 7210c358..25084385 100644 --- a/src/WsServer.cpp +++ b/src/WsServer.cpp @@ -78,7 +78,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) // (*it)->setTodayDate(); //} if ((*it)->getSubtype() == "Loging") { - (*it)->setPublishDestination(2, num); + (*it)->setPublishDestination(TO_WS, num); (*it)->publishValue(); } } @@ -280,18 +280,22 @@ void publishStatusWs(const String& topic, const String& data) { } //публикация статус сообщений -void publishChartWs(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 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); } //данные которые мы отправляем в сокеты переодически @@ -318,9 +322,14 @@ void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) { #endif //посылка данных из файла в бинарном виде -void sendFileToWs(const char* filename, uint8_t num, size_t frameSize) { +void sendFileToWs(String filename, int num, size_t frameSize) { String st = "/st" + String(filename); - standWebSocket.sendTXT(num, st); + if (num == -1) { + standWebSocket.broadcastTXT(st); + } else { + standWebSocket.sendTXT(num, st); + } + String path = filepath(filename); auto file = FileFS.open(path, "r"); if (!file) { @@ -332,12 +341,20 @@ void sendFileToWs(const char* filename, uint8_t num, size_t frameSize) { uint8_t payload[frameSize]; int countRead = file.read(payload, sizeof(payload)); while (countRead > 0) { - standWebSocket.sendBIN(num, payload, countRead); + 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); - standWebSocket.sendTXT(num, end); + if (num == -1) { + standWebSocket.broadcastTXT(end); + } else { + standWebSocket.sendTXT(num, end); + } } //посылка данных из string diff --git a/src/modules/virtual/Logging/Loging.cpp b/src/modules/virtual/Logging/Loging.cpp index 95aed9a3..9b2b450d 100644 --- a/src/modules/virtual/Logging/Loging.cpp +++ b/src/modules/virtual/Logging/Loging.cpp @@ -66,6 +66,7 @@ class Loging : public IoTItem { regEvent(value, F("Loging")); String logData; + jsonWriteInt(logData, "x", unixTime); jsonWriteFloat(logData, "y1", value.toFloat()); @@ -112,6 +113,8 @@ class Loging : public IoTItem { return; } //запишем в него данные + String topic = mqttRootDevice + "/" + id; + logData = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[" + logData; if (addFile(path, logData) != "sucсess") { SerialPrint("E", F("Loging"), "'" + id + "' data writing error, return"); return; @@ -158,25 +161,28 @@ class Loging : public IoTItem { bool noData = true; while (filesList.length()) { - String buf = selectToMarker(filesList, ";"); + String path = selectToMarker(filesList, ";"); - buf = "/lg/" + id + buf; + path = "/lg/" + id + path; f++; - int i = 0; - unsigned long fileUnixTimeLocal = getFileUnixLocalTime(buf); + unsigned long fileUnixTimeLocal = getFileUnixLocalTime(path); unsigned long reqUnixTime = strDateToUnix(getItemValue(id + "-date")); if (fileUnixTimeLocal > reqUnixTime && fileUnixTimeLocal < reqUnixTime + 86400) { noData = false; - if (!createJson(buf, i)) { - SerialPrint("E", F("Loging"), buf + " file reading error, json not created, return"); - return; + if (_publishType == TO_MQTT) { + sendChartFileToMqtt(path); + } else if (_publishType == TO_WS) { + sendChartFileToWs(path, _wsNum, 1000); + } else if (_publishType == TO_MQTT_WS) { + sendChartFileToMqtt(path); + sendChartFileToWs(path, _wsNum, 1000); } - SerialPrint("i", F("Loging"), String(f) + ") " + buf + ", " + String(i) + ", " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", sent"); + SerialPrint("i", F("Loging"), String(f) + ") " + path + ", " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", sent"); } else { - SerialPrint("i", F("Loging"), String(f) + ") " + buf + ", nil, " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", skipped"); + SerialPrint("i", F("Loging"), String(f) + ") " + path + ", " + getDateTimeDotFormatedFromUnix(fileUnixTimeLocal) + ", skipped"); } filesList = deleteBeforeDelimiter(filesList, ";"); @@ -190,7 +196,7 @@ class Loging : public IoTItem { void clearValue() { SerialPrint("i", F("Loging"), "clear chart"); String cleanJson = createEmtyJson(); - publishJson(cleanJson); + // publishJson(cleanJson); } void clearHistory() { @@ -206,14 +212,14 @@ class Loging : public IoTItem { filesList = getFilesList(dir); int i = 0; while (filesList.length()) { - String buf = selectToMarker(filesList, ";"); + String path = selectToMarker(filesList, ";"); - buf = dir + buf; + path = dir + path; i++; if (i == 1) { - removeFile(buf); - SerialPrint("!", "Loging", String(i) + ") " + buf + " => oldest files been deleted"); + removeFile(path); + SerialPrint("!", "Loging", String(i) + ") " + path + " => oldest files been deleted"); return; } @@ -222,43 +228,72 @@ class Loging : public IoTItem { } } - bool createJson(String file, int &i) { - File configFile = FileFS.open(file, FILE_READ); + bool sendChartFileToMqtt(String path) { + File configFile = FileFS.open(path, FILE_READ); if (!configFile) { + SerialPrint("E", F("Loging"), path + " file reading error, json not created, return"); return false; } - String oneSingleJson = configFile.readString(); - configFile.close(); - - String topic = mqttRootDevice + "/" + id; - oneSingleJson = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[" + oneSingleJson + "]}"; - oneSingleJson.replace("},]}", "}]}"); - + // String topic = mqttRootDevice + "/" + id; + // oneSingleJson = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[" + oneSingleJson + "]}"; + // oneSingleJson.replace("},]}", "}]}"); SerialPrint("i", "Loging", "json size: " + String(oneSingleJson.length())); - - publishJson(oneSingleJson); + publishChartMqtt(id, oneSingleJson); return true; } - // publishType 1 - в mqtt, 2 - в ws, 3 - mqtt и ws, wsNum = -1 => broadcast - void setPublishDestination(int publishType, int wsNum) { - _publishType = publishType; - _wsNum = wsNum; + void sendChartFileToWs(String filename, int num, size_t frameSize) { + String st = "/st/chart.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"; + if (num == -1) { + standWebSocket.broadcastTXT(end); + } else { + standWebSocket.sendTXT(num, end); + } } - void publishJson(String &oneSingleJson) { - if (_publishType == 1) { - publishChartMqtt(id, oneSingleJson); - } else if (_publishType == 2) { - publishChartWs(_wsNum, oneSingleJson); - } else if (_publishType == 3) { - publishChartMqtt(id, oneSingleJson); - publishChartWs(_wsNum, oneSingleJson); - } else { - SerialPrint("E", F("Loging"), "wrong publishType"); - } + //посылка данных из 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 setPublishDestination(int publishType, int wsNum = -1) { + _publishType = publishType; + _wsNum = wsNum; } String getValue() { @@ -342,7 +377,7 @@ class Date : public IoTItem { if ((*it)->getSubtype() == "Loging") { //отправляем только свои данные if ((*it)->getID() == selectToMarker(id, "-")) { - (*it)->setPublishDestination(3, -1); + (*it)->setPublishDestination(TO_MQTT_WS); (*it)->clearValue(); (*it)->publishValue(); }