не рабочая версия!

This commit is contained in:
Dmitry Borisenko
2022-09-23 02:24:50 +02:00
parent b1fe6af04d
commit 100477444c
6 changed files with 123 additions and 63 deletions

View File

@@ -70,4 +70,12 @@ enum UpdateStates { NOT_STARTED,
UPDATE_BUILD_COMPLETED, UPDATE_BUILD_COMPLETED,
UPDATE_BUILD_FAILED, UPDATE_BUILD_FAILED,
PATH_ERROR PATH_ERROR
}; };
enum distination {
TO_MQTT,
TO_WS,
TO_MQTT_WS,
};
#define WS_BROADCAST -1

View File

@@ -13,9 +13,9 @@ extern void hexdump(const void* mem, uint32_t len, uint8_t cols);
#endif #endif
#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 publishStatusWs(const String& topic, const String& data);
void publishChartWs(int num, String& json); void publishChartWs(int num, String& path);
void periodicWsSend(); void periodicWsSend();
void sendStringToWs(const String& msg, uint8_t num, String name); void sendStringToWs(const String& msg, uint8_t num, String name);

View File

@@ -51,7 +51,7 @@ class IoTItem {
//методы для графиков //методы для графиков
virtual void publishValue(); virtual void publishValue();
virtual void clearValue(); virtual void clearValue();
virtual void setPublishDestination(int type, int num); virtual void setPublishDestination(int type, int wsNum = -1);
virtual void clearHistory(); virtual void clearHistory();
virtual void setTodayDate(); virtual void setTodayDate();

View File

@@ -137,7 +137,7 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
//отправка данных графиков //отправка данных графиков
for (std::list<IoTItem*>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { for (std::list<IoTItem*>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
if ((*it)->getSubtype() == "Loging") { if ((*it)->getSubtype() == "Loging") {
(*it)->setPublishDestination(1, -1); (*it)->setPublishDestination(TO_MQTT);
(*it)->publishValue(); (*it)->publishValue();
} }
} }

View File

@@ -78,7 +78,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
// (*it)->setTodayDate(); // (*it)->setTodayDate();
//} //}
if ((*it)->getSubtype() == "Loging") { if ((*it)->getSubtype() == "Loging") {
(*it)->setPublishDestination(2, num); (*it)->setPublishDestination(TO_WS, num);
(*it)->publishValue(); (*it)->publishValue();
} }
} }
@@ -280,18 +280,22 @@ void publishStatusWs(const String& topic, const String& data) {
} }
//публикация статус сообщений //публикация статус сообщений
void publishChartWs(int num, String& data) { // void publishChartWs2(int num, String& data) {
bool ok = false; // bool ok = false;
if (num == -1) { // if (num == -1) {
ok = standWebSocket.broadcastTXT(data); // ok = standWebSocket.broadcastTXT(data);
} else { // } else {
ok = standWebSocket.sendTXT(num, data); // ok = standWebSocket.sendTXT(num, data);
} // }
if (ok) { // if (ok) {
SerialPrint(F("i"), F("WS"), F("sent sucsess")); // SerialPrint(F("i"), F("WS"), F("sent sucsess"));
} else { // } else {
SerialPrint(F("E"), F("WS"), F("sent error")); // 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 #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); String st = "/st" + String(filename);
standWebSocket.sendTXT(num, st); if (num == -1) {
standWebSocket.broadcastTXT(st);
} else {
standWebSocket.sendTXT(num, st);
}
String path = filepath(filename); String path = filepath(filename);
auto file = FileFS.open(path, "r"); auto file = FileFS.open(path, "r");
if (!file) { if (!file) {
@@ -332,12 +341,20 @@ void sendFileToWs(const char* filename, uint8_t num, size_t frameSize) {
uint8_t payload[frameSize]; uint8_t payload[frameSize];
int countRead = file.read(payload, sizeof(payload)); int countRead = file.read(payload, sizeof(payload));
while (countRead > 0) { 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)); countRead = file.read(payload, sizeof(payload));
} }
file.close(); file.close();
String end = "/end" + String(filename); String end = "/end" + String(filename);
standWebSocket.sendTXT(num, end); if (num == -1) {
standWebSocket.broadcastTXT(end);
} else {
standWebSocket.sendTXT(num, end);
}
} }
//посылка данных из string //посылка данных из string

View File

@@ -66,6 +66,7 @@ class Loging : public IoTItem {
regEvent(value, F("Loging")); regEvent(value, F("Loging"));
String logData; String logData;
jsonWriteInt(logData, "x", unixTime); jsonWriteInt(logData, "x", unixTime);
jsonWriteFloat(logData, "y1", value.toFloat()); jsonWriteFloat(logData, "y1", value.toFloat());
@@ -112,6 +113,8 @@ class Loging : public IoTItem {
return; return;
} }
//запишем в него данные //запишем в него данные
String topic = mqttRootDevice + "/" + id;
logData = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[" + logData;
if (addFile(path, logData) != "sucсess") { if (addFile(path, logData) != "sucсess") {
SerialPrint("E", F("Loging"), "'" + id + "' data writing error, return"); SerialPrint("E", F("Loging"), "'" + id + "' data writing error, return");
return; return;
@@ -158,25 +161,28 @@ class Loging : public IoTItem {
bool noData = true; bool noData = true;
while (filesList.length()) { while (filesList.length()) {
String buf = selectToMarker(filesList, ";"); String path = selectToMarker(filesList, ";");
buf = "/lg/" + id + buf; path = "/lg/" + id + path;
f++; f++;
int i = 0;
unsigned long fileUnixTimeLocal = getFileUnixLocalTime(buf); unsigned long fileUnixTimeLocal = getFileUnixLocalTime(path);
unsigned long reqUnixTime = strDateToUnix(getItemValue(id + "-date")); unsigned long reqUnixTime = strDateToUnix(getItemValue(id + "-date"));
if (fileUnixTimeLocal > reqUnixTime && fileUnixTimeLocal < reqUnixTime + 86400) { if (fileUnixTimeLocal > reqUnixTime && fileUnixTimeLocal < reqUnixTime + 86400) {
noData = false; noData = false;
if (!createJson(buf, i)) { if (_publishType == TO_MQTT) {
SerialPrint("E", F("Loging"), buf + " file reading error, json not created, return"); sendChartFileToMqtt(path);
return; } 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 { } 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, ";"); filesList = deleteBeforeDelimiter(filesList, ";");
@@ -190,7 +196,7 @@ class Loging : public IoTItem {
void clearValue() { void clearValue() {
SerialPrint("i", F("Loging"), "clear chart"); SerialPrint("i", F("Loging"), "clear chart");
String cleanJson = createEmtyJson(); String cleanJson = createEmtyJson();
publishJson(cleanJson); // publishJson(cleanJson);
} }
void clearHistory() { void clearHistory() {
@@ -206,14 +212,14 @@ class Loging : public IoTItem {
filesList = getFilesList(dir); filesList = getFilesList(dir);
int i = 0; int i = 0;
while (filesList.length()) { while (filesList.length()) {
String buf = selectToMarker(filesList, ";"); String path = selectToMarker(filesList, ";");
buf = dir + buf; path = dir + path;
i++; i++;
if (i == 1) { if (i == 1) {
removeFile(buf); removeFile(path);
SerialPrint("!", "Loging", String(i) + ") " + buf + " => oldest files been deleted"); SerialPrint("!", "Loging", String(i) + ") " + path + " => oldest files been deleted");
return; return;
} }
@@ -222,43 +228,72 @@ class Loging : public IoTItem {
} }
} }
bool createJson(String file, int &i) { bool sendChartFileToMqtt(String path) {
File configFile = FileFS.open(file, FILE_READ); File configFile = FileFS.open(path, FILE_READ);
if (!configFile) { if (!configFile) {
SerialPrint("E", F("Loging"), path + " file reading error, json not created, return");
return false; return false;
} }
String oneSingleJson = configFile.readString(); String oneSingleJson = configFile.readString();
configFile.close(); configFile.close();
// String topic = mqttRootDevice + "/" + id;
String topic = mqttRootDevice + "/" + id; // oneSingleJson = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[" + oneSingleJson + "]}";
oneSingleJson = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[" + oneSingleJson + "]}"; // oneSingleJson.replace("},]}", "}]}");
oneSingleJson.replace("},]}", "}]}");
SerialPrint("i", "Loging", "json size: " + String(oneSingleJson.length())); SerialPrint("i", "Loging", "json size: " + String(oneSingleJson.length()));
publishChartMqtt(id, oneSingleJson);
publishJson(oneSingleJson);
return true; return true;
} }
// publishType 1 - в mqtt, 2 - в ws, 3 - mqtt и ws, wsNum = -1 => broadcast void sendChartFileToWs(String filename, int num, size_t frameSize) {
void setPublishDestination(int publishType, int wsNum) { String st = "/st/chart.json";
_publishType = publishType; if (num == -1) {
_wsNum = wsNum; 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) { //посылка данных из string
if (_publishType == 1) { void sendStringToWs(const String &msg, uint8_t num, String name) {
publishChartMqtt(id, oneSingleJson); String st = "/st" + String(name);
} else if (_publishType == 2) { standWebSocket.sendTXT(num, st);
publishChartWs(_wsNum, oneSingleJson); size_t size = msg.length();
} else if (_publishType == 3) { char dataArray[size];
publishChartMqtt(id, oneSingleJson); msg.toCharArray(dataArray, size);
publishChartWs(_wsNum, oneSingleJson); standWebSocket.sendBIN(num, (uint8_t *)dataArray, size);
} else { String end = "/end" + String(name);
SerialPrint("E", F("Loging"), "wrong publishType"); standWebSocket.sendTXT(num, end);
} }
void setPublishDestination(int publishType, int wsNum = -1) {
_publishType = publishType;
_wsNum = wsNum;
} }
String getValue() { String getValue() {
@@ -342,7 +377,7 @@ class Date : public IoTItem {
if ((*it)->getSubtype() == "Loging") { if ((*it)->getSubtype() == "Loging") {
//отправляем только свои данные //отправляем только свои данные
if ((*it)->getID() == selectToMarker(id, "-")) { if ((*it)->getID() == selectToMarker(id, "-")) {
(*it)->setPublishDestination(3, -1); (*it)->setPublishDestination(TO_MQTT_WS);
(*it)->clearValue(); (*it)->clearValue();
(*it)->publishValue(); (*it)->publishValue();
} }