очистка и реорганизация файлов

This commit is contained in:
Dmitry Borisenko
2022-02-02 21:17:50 +01:00
parent e376693028
commit a2f5927172
12 changed files with 211 additions and 272 deletions

View File

@@ -1,8 +0,0 @@
#pragma once
#include "Global.h"
extern void createWidget(String& parameters);
extern bool loadWidget(const String& filename, String& buf);
extern const String getWidgetFile(const String& name);

View File

@@ -1,7 +1,6 @@
#pragma once
#include "Global.h"
#include "Classes/IoTSensor.h"
#include "CreateWidget.h"
extern std::vector<IoTSensor*> iotSensors; // вектор ссылок базового класса IoTSensor - список всех запущенных сенсоров

View File

@@ -8,3 +8,4 @@
#include "classes/NotAsync.h"
#include "ESPConfiguration.h"
#include "MqttClient.h"
#include "WsServer.h"

View File

@@ -19,7 +19,7 @@ boolean publishData(const String& topic, const String& data);
boolean publishChart(const String& topic, const String& data);
boolean publishControl(String id, String topic, String state);
boolean publishChart_test(const String& topic, const String& data);
boolean publishStatus(const String& topic, const String& data);
boolean publishStatusMqtt(const String& topic, const String& data);
boolean publishEvent(const String& topic, const String& data);
boolean publishInfo(const String& topic, const String& data);
boolean publishAnyJsonKey(const String& topic, const String& key, const String& data);

View File

@@ -12,16 +12,3 @@ extern void handleFileCreate();
extern void handleFileList();
#endif
#endif
#ifdef STANDARD_WEB_SOCKETS
extern void standWebSocketsInit();
extern void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length);
#ifdef ESP32
extern void hexdump(const void* mem, uint32_t len, uint8_t cols);
#endif
#endif
void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize);
// void sendMark(const char* filename, const char* mark, uint8_t num);
// void sendFileToWs3(const String& filename, uint8_t num);
// void sendFileToWs4(const String& filename, uint8_t num);

15
include/WsServer.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include "Global.h"
#ifdef STANDARD_WEB_SOCKETS
extern void standWebSocketsInit();
extern void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length);
#ifdef ESP32
extern void hexdump(const void* mem, uint32_t len, uint8_t cols);
#endif
#endif
void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize);
// void sendMark(const char* filename, const char* mark, uint8_t num);
// void sendFileToWs3(const String& filename, uint8_t num);
// void sendFileToWs4(const String& filename, uint8_t num);

View File

@@ -1,65 +0,0 @@
#include "CreateWidget.h"
void createWidget(String& parameters) {
String id = jsonReadStr(parameters, "id");
String widget = jsonReadStr(parameters, "widget");
String order = jsonReadStr(parameters, "order");
String page = jsonReadStr(parameters, "page");
String descr = jsonReadStr(parameters, "descr");
SerialPrint(F("i"), F("Widget"), "Start create widget: " + widget);
if (widget != "na") {
String buf = "{}";
if (!loadWidget(widget, buf)) {
return;
}
// if (_cnt != "") {
// if (widget.indexOf("chart") != -1) jsonWriteStr(buf, "maxCount", _cnt);
// }
#ifdef GATE_MODE
jsonWriteStr(buf, "info", " ");
#endif
jsonWriteStr(buf, "page", page);
jsonWriteStr(buf, "order", order);
jsonWriteStr(buf, "descr", descr);
jsonWriteStr(buf, "topic", prex + "/" + id);
SerialPrint(F("i"), F("Widget"), buf);
#ifdef LAYOUT_IN_RAM
all_widgets += widget + "\r\n";
#else
addFileLn("layout.json", buf);
#endif
}
}
bool loadWidget(const String& widget, String& buf) {
File file = seekFile("/widgets.json");
file.find("[");
bool ret = false;
while (file.available()) {
String jsonArrayElement = file.readStringUntil('}') + "}";
if (jsonArrayElement.startsWith(",")) {
jsonArrayElement = jsonArrayElement.substring(1, jsonArrayElement.length()); //это нужно оптимизировать в последствии
}
String name;
if (!jsonRead(jsonArrayElement, F("name"), name)) { //если нет такого ключа в представленном json или он не валидный
SerialPrint(F("E"), F("Config"), "json error " + name);
continue;
} else {
if (name == widget) {
buf = jsonArrayElement;
ret = true;
}
}
}
return ret;
}
const String getWidgetFile(const String& name) {
return "/widgets/" + name + ".json";
}

View File

@@ -12,7 +12,6 @@ bool fileSystemInit() {
void globalVarsSync() {
settingsFlashJson = readFile(F("settings.json"), 4096);
settingsFlashJson.replace("\r\n", "");
writeFile("layout.json", "");
}
void saveSettingsFlashJson() {

View File

@@ -278,7 +278,7 @@ boolean publishChart_test(const String& topic, const String& data) {
return mqtt.publish(path.c_str(), data.c_str(), false);
}
boolean publishStatus(const String& topic, const String& data) {
boolean publishStatusMqtt(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/status";
String json = "{}";
jsonWriteStr(json, "status", data);
@@ -364,7 +364,7 @@ void publishState() {
//
// if (topic != "" && state != "") {
// if (topic != "timenow") {
// publishStatus(topic, state);
// publishStatusMqtt(topic, state);
// }
// }
// str = deleteBeforeDelimiter(str, ",");

View File

@@ -188,178 +188,3 @@ void handleFileList() {
}
#endif
#endif
#ifdef STANDARD_WEB_SOCKETS
void standWebSocketsInit() {
standWebSocket.begin();
standWebSocket.onEvent(webSocketEvent);
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_ERROR: {
Serial.printf("[%u] Error!\n", num);
} break;
case WStype_DISCONNECTED: {
Serial.printf("[%u] Disconnected!\n", num);
} break;
case WStype_CONNECTED: {
IPAddress ip = standWebSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
standWebSocket.sendTXT(num, "Connected");
} break;
case WStype_TEXT: {
//максимальное количество символов заголовка
size_t headerLenth = 7;
String headerStr;
headerStr.reserve(headerLenth + 1);
for (size_t i = 0; i < headerLenth; i++) {
headerStr += (char)payload[i];
}
if (headerStr == "/config") {
sendFileToWs5("/items.json", num, 1024);
sendFileToWs5("/widgets.json", num, 1024);
sendFileToWs5("/config.json", num, 1024);
}
if (headerStr == "/gifnoc") {
writeFileUint8tByFrames("config.json", payload, length, headerLenth, 256);
}
if (headerStr == "/tuoyal") {
writeFileUint8tByFrames("layout.json", payload, length, headerLenth, 256);
}
} break;
case WStype_BIN: {
Serial.printf("[%u] get binary length: %u\n", num, length);
// hexdump(payload, length);
// standWebSocket.sendBIN(num, payload, length);
} break;
case WStype_FRAGMENT_TEXT_START: {
Serial.printf("[%u] fragment test start: %u\n", num, length);
} break;
case WStype_FRAGMENT_BIN_START: {
Serial.printf("[%u] fragment bin start: %u\n", num, length);
} break;
case WStype_FRAGMENT: {
Serial.printf("[%u] fragment: %u\n", num, length);
} break;
case WStype_FRAGMENT_FIN: {
Serial.printf("[%u] fragment finish: %u\n", num, length);
} break;
case WStype_PING: {
Serial.printf("[%u] ping: %u\n", num, length);
} break;
case WStype_PONG: {
Serial.printf("[%u] pong: %u\n", num, length);
} break;
default: {
Serial.printf("[%u] not recognized: %u\n", num, length);
} break;
}
}
#ifdef ESP32
void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) {
const uint8_t* src = (const uint8_t*)mem;
Serial.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
for (uint32_t i = 0; i < len; i++) {
if (i % cols == 0) {
Serial.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
}
Serial.printf("%02X ", *src);
src++;
}
Serial.printf("\n");
}
#endif
#endif
//посылка данных из файла в бинарном виде
void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize) {
standWebSocket.sendTXT(num, "/st" + String(filename));
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) {
standWebSocket.sendBIN(num, payload, countRead);
countRead = file.read(payload, sizeof(payload));
}
file.close();
standWebSocket.sendTXT(num, "/end" + String(filename));
}
// void sendMark(const char* filename, const char* mark, uint8_t num) {
// char outChar[strlen(filename) + strlen(mark) + 1];
// strcpy(outChar, mark);
// strcat(outChar, filename);
// size_t size = strlen(outChar);
// uint8_t outUint[size];
// for (size_t i = 0; i < size; i++) {
// outUint[i] = uint8_t(outChar[i]);
// }
// 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);
// }
//}

187
src/WsServer.cpp Normal file
View File

@@ -0,0 +1,187 @@
#include "WsServer.h"
#ifdef STANDARD_WEB_SOCKETS
void standWebSocketsInit() {
standWebSocket.begin();
standWebSocket.onEvent(webSocketEvent);
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_ERROR: {
Serial.printf("[%u] Error!\n", num);
} break;
case WStype_DISCONNECTED: {
Serial.printf("[%u] Disconnected!\n", num);
} break;
case WStype_CONNECTED: {
IPAddress ip = standWebSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
standWebSocket.sendTXT(num, "Connected");
} break;
case WStype_TEXT: {
//максимальное количество символов заголовка
size_t headerLenth = 7;
String headerStr;
headerStr.reserve(headerLenth + 1);
for (size_t i = 0; i < headerLenth; i++) {
headerStr += (char)payload[i];
}
if (headerStr == "/") {
sendFileToWs5("/layout.json", num, 1024);
}
if (headerStr == "/config") {
sendFileToWs5("/items.json", num, 1024);
sendFileToWs5("/widgets.json", num, 1024);
sendFileToWs5("/config.json", num, 1024);
}
if (headerStr == "/gifnoc") {
writeFileUint8tByFrames("config.json", payload, length, headerLenth, 256);
}
if (headerStr == "/tuoyal") {
writeFileUint8tByFrames("layout.json", payload, length, headerLenth, 256);
}
} break;
case WStype_BIN: {
Serial.printf("[%u] get binary length: %u\n", num, length);
// hexdump(payload, length);
// standWebSocket.sendBIN(num, payload, length);
} break;
case WStype_FRAGMENT_TEXT_START: {
Serial.printf("[%u] fragment test start: %u\n", num, length);
} break;
case WStype_FRAGMENT_BIN_START: {
Serial.printf("[%u] fragment bin start: %u\n", num, length);
} break;
case WStype_FRAGMENT: {
Serial.printf("[%u] fragment: %u\n", num, length);
} break;
case WStype_FRAGMENT_FIN: {
Serial.printf("[%u] fragment finish: %u\n", num, length);
} break;
case WStype_PING: {
Serial.printf("[%u] ping: %u\n", num, length);
} break;
case WStype_PONG: {
Serial.printf("[%u] pong: %u\n", num, length);
} break;
default: {
Serial.printf("[%u] not recognized: %u\n", num, length);
} break;
}
}
#ifdef ESP32
void hexdump(const void* mem, uint32_t len, uint8_t cols = 16) {
const uint8_t* src = (const uint8_t*)mem;
Serial.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
for (uint32_t i = 0; i < len; i++) {
if (i % cols == 0) {
Serial.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
}
Serial.printf("%02X ", *src);
src++;
}
Serial.printf("\n");
}
#endif
#endif
//посылка данных из файла в бинарном виде
void sendFileToWs5(const char* filename, uint8_t num, size_t frameSize) {
standWebSocket.sendTXT(num, "/st" + String(filename));
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) {
standWebSocket.sendBIN(num, payload, countRead);
countRead = file.read(payload, sizeof(payload));
}
file.close();
standWebSocket.sendTXT(num, "/end" + String(filename));
}
void publishStatusWs(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/status";
String json = "{}";
jsonWriteStr(json, "status", data);
// standWebSocket.sendTXT(num, json);
// mqtt.publish(path.c_str(), json.c_str(), false);
}
// void sendMark(const char* filename, const char* mark, uint8_t num) {
// char outChar[strlen(filename) + strlen(mark) + 1];
// strcpy(outChar, mark);
// strcat(outChar, filename);
// size_t size = strlen(outChar);
// uint8_t outUint[size];
// for (size_t i = 0; i < size; i++) {
// outUint[i] = uint8_t(outChar[i]);
// }
// 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);
// }
//}

View File

@@ -3,7 +3,6 @@
#include "Classes/ScenarioClass3.h"
#include "Classes/IoTSensor.h"
IoTSensor::IoTSensor(String parameters) {
jsonRead(parameters, "int", _interval);
_interval = _interval * 1000;
@@ -12,7 +11,7 @@ IoTSensor::IoTSensor(String parameters) {
jsonRead(parameters, "multiply", _multiply);
jsonRead(parameters, "plus", _plus);
jsonRead(parameters, "round", _round);
String map;
jsonRead(parameters, "map", map);
if (map != "") {
@@ -20,7 +19,7 @@ IoTSensor::IoTSensor(String parameters) {
_map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
_map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
_map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
}
}
}
IoTSensor::~IoTSensor() {}
@@ -44,14 +43,14 @@ void IoTSensor::loop() {
void IoTSensor::regEvent(String value, String consoleInfo = "") {
eventGen2(_id, value);
jsonWriteStr(paramsFlashJson, _id, value);
publishStatus(_id, value);
publishStatusMqtt(_id, value);
SerialPrint("I", "Sensor " + consoleInfo, "'" + _id + "' data: " + value + "'");
}
void IoTSensor::regEvent(float value, String consoleInfo = "") {
void IoTSensor::regEvent(float value, String consoleInfo = "") {
if (_multiply) value = value * _multiply;
if (_plus) value = value + _multiply;
if (_round != 0) {
if (_round != 0) {
if (value > 0) {
value = (int)(value * _round + 0.5F);
value = value / _round;