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

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_FAILED,
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
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);

View File

@@ -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();

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) {
if ((*it)->getSubtype() == "Loging") {
(*it)->setPublishDestination(1, -1);
(*it)->setPublishDestination(TO_MQTT);
(*it)->publishValue();
}
}

View File

@@ -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

View File

@@ -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();
}