продолжение переписи api

This commit is contained in:
Dmitry Borisenko
2022-10-09 17:04:05 +02:00
parent 8287bb9ebb
commit fe30d7c27d
7 changed files with 84 additions and 86 deletions

View File

@@ -257,8 +257,10 @@ bool WebSocketsServerCore::broadcastBIN(uint8_t * payload, size_t length, bool f
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
client = &_clients[i];
if(clientIsConnected(client)) {
if(!sendFrame(client, WSop_binary, payload, length, fin, headerToPayload)) {
ret = false;
if(continuation) {
ret = sendFrame(client, WSop_continuation, payload, length, fin, headerToPayload);
} else {
ret = sendFrame(client, WSop_binary, payload, length, fin, headerToPayload);
}
}
WEBSOCKETS_YIELD();

View File

@@ -325,14 +325,15 @@ void handleMqttStatus(bool send) {
String stateStr = getStateStr(mqtt.state());
// Serial.println(stateStr);
jsonWriteStr_(errorsHeapJson, F("mqtt"), stateStr);
if (!send) standWebSocket.broadcastTXT(errorsHeapJson);
if (!send) sendStringToWs("errors", errorsHeapJson, -1);
}
void handleMqttStatus(bool send, int state) {
String stateStr = getStateStr(state);
// Serial.println(stateStr);
jsonWriteStr_(errorsHeapJson, F("mqtt"), stateStr);
if (!send) standWebSocket.broadcastTXT(errorsHeapJson);
if (!send) sendStringToWs("errors", errorsHeapJson, -1);
}
// log-20384820.txt

View File

@@ -132,5 +132,5 @@ void saveUserDataToFlash() {
void handleUpdateStatus(bool send, int state) {
jsonWriteInt_(errorsHeapJson, F("upd"), state);
if (!send) standWebSocket.broadcastTXT(errorsHeapJson);
if (!send) sendStringToWs("errors", errorsHeapJson, -1);
}

View File

@@ -217,10 +217,6 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
}
if (headerStr == "/test|") {
sendFileToWsByFrames("/items.json", "itemsj", "", num, WEB_SOCKETS_FRAME_SIZE);
sendFileToWsByFrames("/widgets.json", "widget", "", num, WEB_SOCKETS_FRAME_SIZE);
sendFileToWsByFrames("/config.json", "config", "", num, WEB_SOCKETS_FRAME_SIZE);
sendFileToWsByFrames("/scenario.txt", "scenar", "", num, WEB_SOCKETS_FRAME_SIZE);
}
} break;
@@ -313,38 +309,38 @@ void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) {
//посылка данных из файла в бинарном виде
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 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
@@ -361,41 +357,41 @@ void sendFileToWs(String filename, int num, size_t frameSize) {
//особая функция отправки графиков в веб
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);
}
// 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) {
@@ -412,7 +408,7 @@ void sendFileToWsByFrames(const String& filename, const String& header, const St
}
size_t totalSize = file.size();
SerialPrint("I", "FS", "Send file '" + String(filename) + "', file size: " + String(totalSize));
// Serial.println("Send file '" + String(filename) + "', file size: " + String(totalSize));
char buf[32];
sprintf(buf, "%04d", json.length() + 12);
@@ -452,7 +448,7 @@ void sendFileToWsByFrames(const String& filename, const String& header, const St
continuation = true;
}
SerialPrint("I", "FS", String(i) + ") fr sz: " + String(size) + " fin: " + String(fin) + " cnt: " + String(continuation));
// Serial.println(String(i) + ") fr sz: " + String(size) + " fin: " + String(fin) + " cnt: " + String(continuation));
standWebSocket.sendBIN(client_id, frameBuf, size, fin, continuation);
}
i++;
@@ -470,7 +466,7 @@ void sendStringToWs(const String& header, String& payload, int client_id) {
String msg = header + "|0012|" + payload;
size_t totalSize = msg.length();
SerialPrint("I", "FS", "Send string '" + header + "', str size: " + String(totalSize));
// Serial.println("Send string '" + header + "', str size: " + String(totalSize));
char dataArray[totalSize];
msg.toCharArray(dataArray, totalSize + 1);

View File

@@ -228,14 +228,14 @@ class Loging : public IoTItem {
String topic = mqttRootDevice + "/" + id;
String json = "{\"maxCount\":0,\"topic\":\"" + topic + "\",\"status\":[]}";
String pk = "/string/chart.json|" + json;
standWebSocket.broadcastTXT(pk);
//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);
//standWebSocket.broadcastTXT(pk);
}
void setPublishDestination(int publishType, int wsNum = -1) {

View File

@@ -192,7 +192,7 @@ class LogingDaily : public IoTItem {
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);
//standWebSocket.broadcastTXT(pk);
}
void setPublishDestination(int publishType, int wsNum = -1) {

View File

@@ -6,15 +6,14 @@ void SerialPrint(String errorLevel, String module, String msg) {
tosend = prettyMillis(millis());
// if (module == "Loging") {
tosend = tosend + " [" + errorLevel + "] [" + module + "] " + msg;
Serial.println(tosend);
if (isNetworkActive()) {
if (jsonReadInt(settingsFlashJson, F("log")) != 0) {
String pl = "/log|" + tosend;
standWebSocket.broadcastTXT(pl);
// String pl = "/log|" + tosend;
// standWebSocket.broadcastTXT(pl);
sendStringToWs("corelg", tosend, -1);
}
}
//}
}