продолжение переписи 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++) { for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
client = &_clients[i]; client = &_clients[i];
if(clientIsConnected(client)) { if(clientIsConnected(client)) {
if(!sendFrame(client, WSop_binary, payload, length, fin, headerToPayload)) { if(continuation) {
ret = false; ret = sendFrame(client, WSop_continuation, payload, length, fin, headerToPayload);
} else {
ret = sendFrame(client, WSop_binary, payload, length, fin, headerToPayload);
} }
} }
WEBSOCKETS_YIELD(); WEBSOCKETS_YIELD();

View File

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

View File

@@ -132,5 +132,5 @@ void saveUserDataToFlash() {
void handleUpdateStatus(bool send, int state) { void handleUpdateStatus(bool send, int state) {
jsonWriteInt_(errorsHeapJson, F("upd"), 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|") { 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; } 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) { void sendFileToWs(String filename, int num, size_t frameSize) {
String st = "/st" + String(filename); // String st = "/st" + String(filename);
if (num == -1) { // if (num == -1) {
standWebSocket.broadcastTXT(st); // standWebSocket.broadcastTXT(st);
} else { // } else {
standWebSocket.sendTXT(num, st); // 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) {
SerialPrint(F("E"), F("FS"), F("reed file error")); // SerialPrint(F("E"), F("FS"), F("reed file error"));
return; // return;
} //}
size_t fileSize = file.size(); // size_t fileSize = file.size();
SerialPrint(F("i"), F("FS"), "Send file '" + String(filename) + "', file size: " + String(fileSize)); // SerialPrint(F("i"), F("FS"), "Send file '" + String(filename) + "', file size: " + String(fileSize));
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) {
if (num == -1) { // if (num == -1) {
standWebSocket.broadcastBIN(payload, countRead); // standWebSocket.broadcastBIN(payload, countRead);
} else { // } else {
standWebSocket.sendBIN(num, payload, countRead); // 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);
if (num == -1) { // if (num == -1) {
standWebSocket.broadcastTXT(end); // standWebSocket.broadcastTXT(end);
} else { //} else {
standWebSocket.sendTXT(num, end); // standWebSocket.sendTXT(num, end);
} //}
} }
//посылка данных из string //посылка данных из 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) { void publishChartToWs(String filename, int num, size_t frameSize, int maxCount, String id) {
String json; // String json;
jsonWriteStr(json, "topic", mqttRootDevice + "/" + id); // jsonWriteStr(json, "topic", mqttRootDevice + "/" + id);
jsonWriteInt(json, "maxCount", maxCount); // jsonWriteInt(json, "maxCount", maxCount);
//
String st = "/st/chart.json|" + json; // String st = "/st/chart.json|" + json;
if (num == -1) { // if (num == -1) {
standWebSocket.broadcastTXT(st); // standWebSocket.broadcastTXT(st);
} else { //} else {
standWebSocket.sendTXT(num, st); // 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) {
SerialPrint(F("E"), F("FS"), F("reed file error")); // SerialPrint(F("E"), F("FS"), F("reed file error"));
return; // return;
} //}
size_t fileSize = file.size(); // size_t fileSize = file.size();
SerialPrint(F("i"), F("FS"), "Send file '" + String(filename) + "', file size: " + String(fileSize)); // SerialPrint(F("i"), F("FS"), "Send file '" + String(filename) + "', file size: " + String(fileSize));
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) {
if (num == -1) { // if (num == -1) {
standWebSocket.broadcastBIN(payload, countRead); // standWebSocket.broadcastBIN(payload, countRead);
} else { // } else {
standWebSocket.sendBIN(num, payload, countRead); // standWebSocket.sendBIN(num, payload, countRead);
} // }
countRead = file.read(payload, sizeof(payload)); // countRead = file.read(payload, sizeof(payload));
} //}
file.close(); // file.close();
String end = "/end/chart.json|" + json; // String end = "/end/chart.json|" + json;
if (num == -1) { // if (num == -1) {
standWebSocket.broadcastTXT(end); // standWebSocket.broadcastTXT(end);
} else { //} else {
standWebSocket.sendTXT(num, end); // standWebSocket.sendTXT(num, end);
} //}
} }
void sendFileToWsByFrames(const String& filename, const String& header, const String& json, uint8_t client_id, size_t frameSize) { 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(); 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]; char buf[32];
sprintf(buf, "%04d", json.length() + 12); sprintf(buf, "%04d", json.length() + 12);
@@ -452,7 +448,7 @@ void sendFileToWsByFrames(const String& filename, const String& header, const St
continuation = true; 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); standWebSocket.sendBIN(client_id, frameBuf, size, fin, continuation);
} }
i++; i++;
@@ -470,7 +466,7 @@ void sendStringToWs(const String& header, String& payload, int client_id) {
String msg = header + "|0012|" + payload; String msg = header + "|0012|" + payload;
size_t totalSize = msg.length(); 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]; char dataArray[totalSize];
msg.toCharArray(dataArray, totalSize + 1); msg.toCharArray(dataArray, totalSize + 1);

View File

@@ -228,14 +228,14 @@ class Loging : public IoTItem {
String topic = mqttRootDevice + "/" + id; String topic = mqttRootDevice + "/" + id;
String json = "{\"maxCount\":0,\"topic\":\"" + topic + "\",\"status\":[]}"; String json = "{\"maxCount\":0,\"topic\":\"" + topic + "\",\"status\":[]}";
String pk = "/string/chart.json|" + json; String pk = "/string/chart.json|" + json;
standWebSocket.broadcastTXT(pk); //standWebSocket.broadcastTXT(pk);
} }
void publishChartToWsSinglePoint(String value) { void publishChartToWsSinglePoint(String value) {
String topic = mqttRootDevice + "/" + id; String topic = mqttRootDevice + "/" + id;
String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}"; String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}";
String pk = "/string/chart.json|" + json; String pk = "/string/chart.json|" + json;
standWebSocket.broadcastTXT(pk); //standWebSocket.broadcastTXT(pk);
} }
void setPublishDestination(int publishType, int wsNum = -1) { void setPublishDestination(int publishType, int wsNum = -1) {

View File

@@ -192,7 +192,7 @@ class LogingDaily : public IoTItem {
String topic = mqttRootDevice + "/" + id; String topic = mqttRootDevice + "/" + id;
String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}"; String json = "{\"maxCount\":" + String(calculateMaxCount()) + ",\"topic\":\"" + topic + "\",\"status\":[{\"x\":" + String(unixTime) + ",\"y1\":" + value + "}]}";
String pk = "/string/chart.json|" + json; String pk = "/string/chart.json|" + json;
standWebSocket.broadcastTXT(pk); //standWebSocket.broadcastTXT(pk);
} }
void setPublishDestination(int publishType, int wsNum = -1) { void setPublishDestination(int publishType, int wsNum = -1) {

View File

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