mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
избавился от string в приеме веб сокетов
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include "EspFileSystem.h"
|
#include "EspFileSystem.h"
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
|
|
||||||
|
extern void writeFileUint8(const String& filename, uint8_t*& payload, size_t length, size_t headerLenth);
|
||||||
extern File seekFile(const String& filename, size_t position = 0);
|
extern File seekFile(const String& filename, size_t position = 0);
|
||||||
extern const String writeFile(const String& filename, const String& str);
|
extern const String writeFile(const String& filename, const String& str);
|
||||||
extern const String readFile(const String& filename, size_t max_size);
|
extern const String readFile(const String& filename, size_t max_size);
|
||||||
|
|||||||
@@ -208,33 +208,21 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
|
|||||||
} break;
|
} break;
|
||||||
|
|
||||||
case WStype_TEXT: {
|
case WStype_TEXT: {
|
||||||
// Serial.printf("[%u] get Text: %s\n", num, payload);
|
//максимальное количество символов заголовка
|
||||||
|
size_t headerLenth = 7;
|
||||||
String payloadStr;
|
String headerStr;
|
||||||
payloadStr.reserve(length + 1);
|
headerStr.reserve(headerLenth + 1);
|
||||||
for (size_t i = 0; i < length; i++) {
|
for (size_t i = 0; i < headerLenth; i++) {
|
||||||
payloadStr += (char)payload[i];
|
headerStr += (char)payload[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
//если прилетел url страницы /config то отправим widgets.json и config.json
|
if (headerStr == "/config") {
|
||||||
if (payloadStr.startsWith("/config")) {
|
|
||||||
sendFileToWs5("/widgets.json", num);
|
sendFileToWs5("/widgets.json", num);
|
||||||
sendFileToWs5("/config.json", num);
|
sendFileToWs5("/config.json", num);
|
||||||
}
|
}
|
||||||
|
|
||||||
//если прилетел измененный пакет с меткой /gifnoc (config наоборот) то перепишем файл
|
if (headerStr == "/gifnoc") {
|
||||||
if (payloadStr.startsWith("/gifnoc.json")) {
|
writeFileUint8("config.json", payload, length, headerLenth);
|
||||||
payloadStr.replace("/gifnoc.json", "");
|
|
||||||
String path = filepath("config.json");
|
|
||||||
auto file = FileFS.open(path, "w");
|
|
||||||
if (!file) {
|
|
||||||
Serial.println("failed");
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < length; i++) {
|
|
||||||
file.print((char)payload[i]);
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
@@ -291,49 +279,6 @@ void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//посылка данных из файла в string
|
|
||||||
void sendFileToWs3(const String& filename, uint8_t num) {
|
|
||||||
standWebSocket.sendTXT(num, "/st" + filename);
|
|
||||||
size_t ws_buffer = 512;
|
|
||||||
String path = filepath(filename);
|
|
||||||
auto file = FileFS.open(path, "r");
|
|
||||||
if (!file) {
|
|
||||||
SerialPrint(F("E"), F("FS"), F("reed file error"));
|
|
||||||
}
|
|
||||||
size_t fileSize = file.size();
|
|
||||||
SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize));
|
|
||||||
String ret;
|
|
||||||
char temp[ws_buffer + 1];
|
|
||||||
int countRead = file.readBytes(temp, sizeof(temp) - 1);
|
|
||||||
while (countRead > 0) {
|
|
||||||
temp[countRead] = 0;
|
|
||||||
ret = temp;
|
|
||||||
standWebSocket.sendTXT(num, ret);
|
|
||||||
countRead = file.readBytes(temp, sizeof(temp) - 1);
|
|
||||||
}
|
|
||||||
standWebSocket.sendTXT(num, "/end" + filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
//посылка данных из файла в char
|
|
||||||
void sendFileToWs4(const String& filename, uint8_t num) {
|
|
||||||
standWebSocket.sendTXT(num, "/st" + filename);
|
|
||||||
size_t ws_buffer = 512;
|
|
||||||
String path = filepath(filename);
|
|
||||||
auto file = FileFS.open(path, "r");
|
|
||||||
if (!file) {
|
|
||||||
SerialPrint(F("E"), F("FS"), F("reed file error"));
|
|
||||||
}
|
|
||||||
size_t fileSize = file.size();
|
|
||||||
SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize));
|
|
||||||
char temp[ws_buffer + 1];
|
|
||||||
int countRead = file.readBytes(temp, sizeof(temp) - 1);
|
|
||||||
while (countRead > 0) {
|
|
||||||
temp[countRead] = 0;
|
|
||||||
standWebSocket.sendTXT(num, temp, countRead);
|
|
||||||
countRead = file.readBytes(temp, sizeof(temp) - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//посылка данных из файла в бинарном виде
|
//посылка данных из файла в бинарном виде
|
||||||
void sendFileToWs5(const char* filename, uint8_t num) {
|
void sendFileToWs5(const char* filename, uint8_t num) {
|
||||||
sendMark(filename, "/st", num);
|
sendMark(filename, "/st", num);
|
||||||
@@ -366,3 +311,46 @@ void sendMark(const char* filename, const char* mark, uint8_t num) {
|
|||||||
}
|
}
|
||||||
standWebSocket.sendBIN(num, outUint, sizeof(outUint));
|
standWebSocket.sendBIN(num, outUint, sizeof(outUint));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//посылка данных из файла в string
|
||||||
|
// void sendFileToWs3(const String& filename, uint8_t num) {
|
||||||
|
// standWebSocket.sendTXT(num, "/st" + filename);
|
||||||
|
// size_t ws_buffer = 512;
|
||||||
|
// String path = filepath(filename);
|
||||||
|
// auto file = FileFS.open(path, "r");
|
||||||
|
// if (!file) {
|
||||||
|
// SerialPrint(F("E"), F("FS"), F("reed file error"));
|
||||||
|
// }
|
||||||
|
// size_t fileSize = file.size();
|
||||||
|
// SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize));
|
||||||
|
// String ret;
|
||||||
|
// char temp[ws_buffer + 1];
|
||||||
|
// int countRead = file.readBytes(temp, sizeof(temp) - 1);
|
||||||
|
// while (countRead > 0) {
|
||||||
|
// temp[countRead] = 0;
|
||||||
|
// ret = temp;
|
||||||
|
// standWebSocket.sendTXT(num, ret);
|
||||||
|
// countRead = file.readBytes(temp, sizeof(temp) - 1);
|
||||||
|
// }
|
||||||
|
// standWebSocket.sendTXT(num, "/end" + filename);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//посылка данных из файла в char
|
||||||
|
// void sendFileToWs4(const String& filename, uint8_t num) {
|
||||||
|
// standWebSocket.sendTXT(num, "/st" + filename);
|
||||||
|
// size_t ws_buffer = 512;
|
||||||
|
// String path = filepath(filename);
|
||||||
|
// auto file = FileFS.open(path, "r");
|
||||||
|
// if (!file) {
|
||||||
|
// SerialPrint(F("E"), F("FS"), F("reed file error"));
|
||||||
|
// }
|
||||||
|
// size_t fileSize = file.size();
|
||||||
|
// SerialPrint(F("i"), F("WS"), "Send file '" + filename + "', file size: " + String(fileSize));
|
||||||
|
// char temp[ws_buffer + 1];
|
||||||
|
// int countRead = file.readBytes(temp, sizeof(temp) - 1);
|
||||||
|
// while (countRead > 0) {
|
||||||
|
// temp[countRead] = 0;
|
||||||
|
// standWebSocket.sendTXT(num, temp, countRead);
|
||||||
|
// countRead = file.readBytes(temp, sizeof(temp) - 1);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|||||||
@@ -1,5 +1,20 @@
|
|||||||
#include "Utils/FileUtils.h"
|
#include "Utils/FileUtils.h"
|
||||||
|
|
||||||
|
void writeFileUint8(const String& filename, uint8_t*& payload, size_t length, size_t headerLenth) {
|
||||||
|
String path = filepath(filename);
|
||||||
|
auto file = FileFS.open(path, "w");
|
||||||
|
if (!file) {
|
||||||
|
Serial.println(F("failed write file uint8"));
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < length; i++) {
|
||||||
|
if (i >= headerLenth) {
|
||||||
|
file.print((char)payload[i]);
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
File seekFile(const String& filename, size_t position) {
|
File seekFile(const String& filename, size_t position) {
|
||||||
String path = filepath(filename);
|
String path = filepath(filename);
|
||||||
auto file = FileFS.open(path, "r");
|
auto file = FileFS.open(path, "r");
|
||||||
|
|||||||
Reference in New Issue
Block a user