mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-27 14:42:18 +03:00
Добавляем функцию сохранения состояния элементов на flash
This commit is contained in:
@@ -14,6 +14,10 @@ void globalVarsSync() {
|
||||
settingsFlashJson = readFile(F("settings.json"), 4096);
|
||||
settingsFlashJson.replace("\r\n", "");
|
||||
|
||||
valuesFlashJson = readFile(F("values.json"), 4096);
|
||||
valuesFlashJson.replace("\r\n", "");
|
||||
|
||||
|
||||
mqttPrefix = jsonReadStr(settingsFlashJson, F("mqttPrefix"));
|
||||
mqttRootDevice = mqttPrefix + "/" + chipId;
|
||||
jsonWriteStr_(settingsFlashJson, "root", mqttRootDevice);
|
||||
@@ -34,6 +38,10 @@ void syncSettingsFlashJson() {
|
||||
writeFile(F("settings.json"), settingsFlashJson);
|
||||
}
|
||||
|
||||
void syncValuesFlashJson() {
|
||||
writeFile(F("values.json"), valuesFlashJson);
|
||||
}
|
||||
|
||||
const String getChipId() {
|
||||
return String(ESP_getChipId()) + "-" + String(ESP_getFlashChipId());
|
||||
}
|
||||
|
||||
@@ -31,8 +31,10 @@ WebSocketsServer standWebSocket = WebSocketsServer(81);
|
||||
**********************************************************************************************************************/
|
||||
IoTGpio IoTgpio(0);
|
||||
|
||||
String settingsFlashJson = "{}"; //переменная в которой хранятся все настройки, находится в оперативной памяти и синхронизированна с flash памятью
|
||||
String errorsHeapJson = "{}"; //переменная в которой хранятся все ошибки, находится в оперативной памяти только
|
||||
String settingsFlashJson = "{}"; // переменная в которой хранятся все настройки, находится в оперативной памяти и синхронизированна с flash памятью
|
||||
String valuesFlashJson = "{}"; // переменная в которой хранятся все значения элементов, которые необходимо сохранить на flash. Находится в оперативной памяти и синхронизированна с flash памятью
|
||||
String errorsHeapJson = "{}"; // переменная в которой хранятся все ошибки, находится в оперативной памяти только
|
||||
bool needSaveValues = false; // признак необходимости сбросить значения элементов на flash
|
||||
|
||||
// buf
|
||||
String orderBuf = "";
|
||||
|
||||
26
src/Main.cpp
26
src/Main.cpp
@@ -149,6 +149,14 @@ void loop() {
|
||||
loopPeriod = millis() - st;
|
||||
if (loopPeriod > 2) Serial.println(loopPeriod);
|
||||
#endif
|
||||
|
||||
// сохраняем значения IoTItems в файл каждую секунду, если были изменения (установлены маркеры на сохранение)
|
||||
if (needSaveValues && millis()%1000 == 0) {
|
||||
syncValuesFlashJson();
|
||||
needSaveValues = false;
|
||||
delay(1);
|
||||
Serial.println("syncValuesFlashJson()");
|
||||
}
|
||||
}
|
||||
|
||||
//отправка json
|
||||
@@ -162,24 +170,6 @@ void loop() {
|
||||
// delay(1);
|
||||
// }
|
||||
|
||||
// сохраняем значения IoTItems в файл каждую секунду, если были изменения (установлены маркеры на сохранение)
|
||||
// currentMillis = millis();
|
||||
// if (currentMillis - prevMillis >= 1000) {
|
||||
// prevMillis = millis();
|
||||
// volStrForSave = "";
|
||||
// for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
|
||||
// if ((*it)->needSave) {
|
||||
// (*it)->needSave = false;
|
||||
// volStrForSave = volStrForSave + (*it)->getID() + "=" + (*it)->getValue() + ";";
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (volStrForSave != "") {
|
||||
// Serial.print("volStrForSave: ");
|
||||
// Serial.println(volStrForSave.c_str());
|
||||
// }
|
||||
//}
|
||||
|
||||
// File dir = FileFS.open("/", "r");
|
||||
// String out;
|
||||
// printDirectory(dir, out);
|
||||
|
||||
@@ -34,16 +34,18 @@ void periodicTasksInit() {
|
||||
void printGlobalVarSize() {
|
||||
size_t settingsFlashJsonSize = settingsFlashJson.length();
|
||||
// SerialPrint(F("i"), F("settingsFlashJson"), String(settingsFlashJsonSize));
|
||||
size_t valuesFlashJsonSize = valuesFlashJson.length();
|
||||
size_t errorsHeapJsonSize = errorsHeapJson.length();
|
||||
// SerialPrint(F("i"), F("errorsHeapJson"), String(errorsHeapJsonSize));
|
||||
size_t devListHeapJsonSize = devListHeapJson.length();
|
||||
// SerialPrint(F("i"), F("devListHeapJson"), String(devListHeapJsonSize));
|
||||
|
||||
SerialPrint(F("i"), F("Var summ sz"), String(settingsFlashJsonSize + errorsHeapJsonSize + devListHeapJsonSize));
|
||||
SerialPrint(F("i"), F("Var summ sz"), String(settingsFlashJsonSize + valuesFlashJsonSize + errorsHeapJsonSize + devListHeapJsonSize));
|
||||
|
||||
size_t halfBuffer = JSON_BUFFER_SIZE / 2;
|
||||
|
||||
if (settingsFlashJsonSize > halfBuffer ||
|
||||
valuesFlashJsonSize > halfBuffer ||
|
||||
errorsHeapJsonSize > halfBuffer ||
|
||||
devListHeapJsonSize > halfBuffer) {
|
||||
SerialPrint(F("EE"), F("Json"), F("Insufficient buffer size!!!"));
|
||||
|
||||
@@ -20,11 +20,11 @@ IoTItem::IoTItem(String parameters) {
|
||||
|
||||
String valAsStr;
|
||||
if (jsonRead(parameters, F("val"), valAsStr, false)) // значение переменной или датчика при инициализации если есть в конфигурации
|
||||
if (value.isDecimal = isDigitDotCommaStr(valAsStr)) {
|
||||
value.valD = valAsStr.toFloat();
|
||||
} else {
|
||||
value.valS = valAsStr;
|
||||
}
|
||||
setValue(valAsStr, false);
|
||||
|
||||
jsonRead(parameters, F("needSave"), _needSave, false);
|
||||
if (_needSave && jsonRead(valuesFlashJson, _id, valAsStr, false)) // пробуем достать из сохранения значение элемента, если указано, что нужно сохранять
|
||||
setValue(valAsStr, false);
|
||||
|
||||
String map;
|
||||
jsonRead(parameters, F("map"), map, false);
|
||||
@@ -58,27 +58,33 @@ String IoTItem::getValue() {
|
||||
}
|
||||
|
||||
//определяем тип прилетевшей величины
|
||||
void IoTItem::setValue(String valStr) {
|
||||
void IoTItem::setValue(String valStr, bool generateEvent) {
|
||||
if (value.isDecimal = isDigitDotCommaStr(valStr)) {
|
||||
value.valD = valStr.toFloat();
|
||||
} else {
|
||||
value.valS = valStr;
|
||||
}
|
||||
setValue(value);
|
||||
if (generateEvent) setValue(value, generateEvent);
|
||||
}
|
||||
|
||||
//
|
||||
void IoTItem::setValue(IoTValue Value) {
|
||||
void IoTItem::setValue(IoTValue Value, bool generateEvent) {
|
||||
value = Value;
|
||||
if (value.isDecimal) {
|
||||
regEvent(value.valD, "");
|
||||
} else {
|
||||
regEvent(value.valS, "");
|
||||
}
|
||||
if (generateEvent)
|
||||
if (value.isDecimal) {
|
||||
regEvent(value.valD, "");
|
||||
} else {
|
||||
regEvent(value.valS, "");
|
||||
}
|
||||
}
|
||||
|
||||
//когда событие случилось
|
||||
void IoTItem::regEvent(String value, String consoleInfo = "") {
|
||||
if (_needSave) {
|
||||
jsonWriteStr_(valuesFlashJson, _id, value);
|
||||
needSaveValues = true;
|
||||
}
|
||||
|
||||
generateEvent(_id, value);
|
||||
publishStatusMqtt(_id, value);
|
||||
|
||||
|
||||
@@ -1,14 +1,31 @@
|
||||
#include "ESPConfiguration.h"
|
||||
|
||||
void* getAPI_Loging(String subtype, String params);
|
||||
void* getAPI_LogingDaily(String subtype, String params);
|
||||
void* getAPI_Timer(String subtype, String params);
|
||||
void* getAPI_Variable(String subtype, String params);
|
||||
void* getAPI_VButton(String subtype, String params);
|
||||
void* getAPI_Acs712(String subtype, String params);
|
||||
void* getAPI_AhtXX(String subtype, String params);
|
||||
void* getAPI_AnalogAdc(String subtype, String params);
|
||||
void* getAPI_Bme280(String subtype, String params);
|
||||
void* getAPI_Bmp280(String subtype, String params);
|
||||
void* getAPI_Dht1122(String subtype, String params);
|
||||
void* getAPI_Ds18b20(String subtype, String params);
|
||||
void* getAPI_GY21(String subtype, String params);
|
||||
void* getAPI_Hdc1080(String subtype, String params);
|
||||
void* getAPI_Max6675(String subtype, String params);
|
||||
void* getAPI_Pzem004(String subtype, String params);
|
||||
void* getAPI_RCswitch(String subtype, String params);
|
||||
void* getAPI_Sht20(String subtype, String params);
|
||||
void* getAPI_Sht30(String subtype, String params);
|
||||
void* getAPI_Sonar(String subtype, String params);
|
||||
void* getAPI_UART(String subtype, String params);
|
||||
void* getAPI_ButtonIn(String subtype, String params);
|
||||
void* getAPI_ButtonOut(String subtype, String params);
|
||||
void* getAPI_IoTServo(String subtype, String params);
|
||||
void* getAPI_Mcp23017(String subtype, String params);
|
||||
void* getAPI_Mp3(String subtype, String params);
|
||||
void* getAPI_Pcf8574(String subtype, String params);
|
||||
void* getAPI_Pwm8266(String subtype, String params);
|
||||
void* getAPI_TelegramLT(String subtype, String params);
|
||||
@@ -16,15 +33,32 @@ void* getAPI_Lcd2004(String subtype, String params);
|
||||
|
||||
void* getAPI(String subtype, String params) {
|
||||
void* tmpAPI;
|
||||
if ((tmpAPI = getAPI_Loging(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_LogingDaily(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Timer(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Variable(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_VButton(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Acs712(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_AhtXX(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_AnalogAdc(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Bme280(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Bmp280(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Dht1122(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Ds18b20(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_GY21(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Hdc1080(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Max6675(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Pzem004(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_RCswitch(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Sht20(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Sht30(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Sonar(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_UART(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_ButtonIn(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Pcf8574(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_Pwm8266(subtype, params)) != nullptr) return tmpAPI;
|
||||
if ((tmpAPI = getAPI_TelegramLT(subtype, params)) != nullptr) return tmpAPI;
|
||||
|
||||
Reference in New Issue
Block a user