mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-31 12:29:14 +03:00
delete
This commit is contained in:
@@ -1,213 +0,0 @@
|
||||
#include "BufferExecute.h"
|
||||
|
||||
#include "Global.h"
|
||||
#include "SoftUART.h"
|
||||
#include "items/test.h"
|
||||
#include "items/vButtonOut.h"
|
||||
#include "items/vCountDown.h"
|
||||
#include "items/vImpulsOut.h"
|
||||
#include "items/vInput.h"
|
||||
#include "items/vLogging.h"
|
||||
#include "items/vOutput.h"
|
||||
#include "items/vPwmOut.h"
|
||||
#include "items/vSensorAnalog.h"
|
||||
#include "items/vSensorBme280.h"
|
||||
#include "items/vSensorBmp280.h"
|
||||
#include "items/vSensorCcs811.h"
|
||||
#include "items/vSensorDallas.h"
|
||||
#include "items/vSensorDht.h"
|
||||
#include "items/vSensorNode.h"
|
||||
#include "items/vSensorPzem.h"
|
||||
#include "items/vSensorUltrasonic.h"
|
||||
#include "items/vSensorUptime.h"
|
||||
|
||||
void loopCmdAdd(const String& cmdStr) {
|
||||
if (cmdStr.endsWith(",")) {
|
||||
orderBuf += cmdStr;
|
||||
SerialPrint("I", F("Order add"), cmdStr);
|
||||
#ifdef EnableUart
|
||||
if (jsonReadBool(configSetupJson, "uart")) {
|
||||
if (jsonReadBool(configSetupJson, "uartEvents")) {
|
||||
if (myUART) {
|
||||
myUART->print(cmdStr);
|
||||
SerialPrint("I", F("<=UART"), cmdStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void loopCmdExecute() {
|
||||
if (orderBuf.length()) {
|
||||
String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1,
|
||||
sCmd.readStr(tmp); //выполняем
|
||||
if (tmp != "") {
|
||||
sCmd.readStr(tmp);
|
||||
SerialPrint("I", F("Order done L"), tmp);
|
||||
}
|
||||
orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем
|
||||
}
|
||||
}
|
||||
|
||||
void fileCmdExecute(const String& filename) {
|
||||
String cmdStr = readFile(filename, 4096);
|
||||
csvCmdExecute(cmdStr);
|
||||
}
|
||||
|
||||
void csvCmdExecute(String& cmdStr) {
|
||||
cmdStr.replace(";", " ");
|
||||
cmdStr += "\r\n";
|
||||
cmdStr.replace("\r\n", "\n");
|
||||
cmdStr.replace("\r", "\n");
|
||||
int count = 0;
|
||||
while (cmdStr.length()) {
|
||||
String buf = selectToMarker(cmdStr, "\n");
|
||||
|
||||
buf = deleteBeforeDelimiter(buf, " "); //отсечка чекбокса
|
||||
|
||||
count++;
|
||||
|
||||
if (count > 1) {
|
||||
//SerialPrint("I", "Items", buf);
|
||||
String order = selectToMarker(buf, " "); //отсечка самой команды
|
||||
if (order == F("button-out")) {
|
||||
#ifdef EnableButtonOut
|
||||
sCmd.addCommand(order.c_str(), buttonOut);
|
||||
#endif
|
||||
} else if (order == F("pwm-out")) {
|
||||
#ifdef EnablePwmOut
|
||||
sCmd.addCommand(order.c_str(), pwmOut);
|
||||
#endif
|
||||
} else if (order == F("button-in")) {
|
||||
#ifdef EnableButtonIn
|
||||
sCmd.addCommand(order.c_str(), buttonIn);
|
||||
#endif
|
||||
} else if (order == F("input-value")) {
|
||||
#ifdef EnableInput
|
||||
sCmd.addCommand(order.c_str(), inputValue);
|
||||
#endif
|
||||
} else if (order == F("output-value")) {
|
||||
#ifdef EnableOutput
|
||||
sCmd.addCommand(order.c_str(), outputValue);
|
||||
#endif
|
||||
} else if (order == F("analog-adc")) {
|
||||
#ifdef EnableSensorAnalog
|
||||
sCmd.addCommand(order.c_str(), analogAdc);
|
||||
#endif
|
||||
} else if (order == F("ultrasonic-cm")) {
|
||||
#ifdef EnableSensorUltrasonic
|
||||
sCmd.addCommand(order.c_str(), ultrasonic);
|
||||
#endif
|
||||
} else if (order == F("dallas-temp")) {
|
||||
#ifdef EnableSensorDallas
|
||||
sCmd.addCommand(order.c_str(), dallas);
|
||||
#endif
|
||||
} else if (order == F("dht")) {
|
||||
#ifdef EnableSensorDht
|
||||
sCmd.addCommand(order.c_str(), dhtSensor);
|
||||
#endif
|
||||
} else if (order == F("bme280")) {
|
||||
#ifdef EnableSensorBme280
|
||||
sCmd.addCommand(order.c_str(), bme280Sensor);
|
||||
#endif
|
||||
} else if (order == F("bmp280")) {
|
||||
#ifdef EnableSensorBmp280
|
||||
sCmd.addCommand(order.c_str(), bmp280Sensor);
|
||||
#endif
|
||||
} else if (order == F("ccs811")) {
|
||||
#ifdef EnableSensorCcs811
|
||||
sCmd.addCommand(order.c_str(), ccs811Sensor);
|
||||
#endif
|
||||
} else if (order == F("pzem")) {
|
||||
#ifdef EnableSensorPzem
|
||||
sCmd.addCommand(order.c_str(), pzemSensor);
|
||||
#endif
|
||||
} else if (order == F("uptime")) {
|
||||
#ifdef EnableSensorUptime
|
||||
sCmd.addCommand(order.c_str(), uptimeSensor);
|
||||
#endif
|
||||
} else if (order == F("logging")) {
|
||||
#ifdef EnableLogging
|
||||
sCmd.addCommand(order.c_str(), logging);
|
||||
#endif
|
||||
} else if (order == F("impuls-out")) {
|
||||
#ifdef EnableImpulsOut
|
||||
sCmd.addCommand(order.c_str(), impuls);
|
||||
#endif
|
||||
} else if (order == F("count-down")) {
|
||||
#ifdef EnableCountDown
|
||||
sCmd.addCommand(order.c_str(), countDown);
|
||||
#endif
|
||||
} else if (order == F("impuls-in")) {
|
||||
#ifdef EnableImpulsIn
|
||||
//sCmd.addCommand(order.c_str(), impulsInSensor);
|
||||
#endif
|
||||
} else if (order == F("sensor-node")) {
|
||||
#ifdef EnableSensorNode
|
||||
sCmd.addCommand(order.c_str(), nodeSensor);
|
||||
#endif
|
||||
}
|
||||
|
||||
sCmd.readStr(buf);
|
||||
}
|
||||
cmdStr = deleteBeforeDelimiter(cmdStr, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
void spaceCmdExecute(String& cmdStr) {
|
||||
cmdStr += "\r\n";
|
||||
cmdStr.replace("\r\n", "\n");
|
||||
cmdStr.replace("\r", "\n");
|
||||
while (cmdStr.length()) {
|
||||
String buf = selectToMarker(cmdStr, "\n");
|
||||
if (buf != "") {
|
||||
sCmd.readStr(buf);
|
||||
SerialPrint("I", F("Order done W"), buf);
|
||||
}
|
||||
cmdStr = deleteBeforeDelimiter(cmdStr, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
//альтернативный вариант распределяющий нагрузку более правильно
|
||||
void spaceCmdExecute2(String& cmdStr) {
|
||||
cmdStr += "\r\n";
|
||||
cmdStr.replace("\r\n", "\n");
|
||||
cmdStr.replace("\r", "\n");
|
||||
cmdStr.replace("\n", ",");
|
||||
cmdStr.replace(",,", ",");
|
||||
loopCmdAdd(cmdStr);
|
||||
}
|
||||
|
||||
void addKey(String& key, String& keyNumberTable, int number) {
|
||||
keyNumberTable += key + " " + String(number) + ",";
|
||||
}
|
||||
|
||||
int getKeyNum(String& key, String& keyNumberTable) {
|
||||
String keyNumberTableBuf = keyNumberTable;
|
||||
|
||||
int number = -1;
|
||||
while (keyNumberTableBuf.length()) {
|
||||
String tmp = selectToMarker(keyNumberTableBuf, ",");
|
||||
String keyIncomming = selectToMarker(tmp, " ");
|
||||
if (keyIncomming == key) {
|
||||
number = selectToMarkerLast(tmp, " ").toInt();
|
||||
}
|
||||
keyNumberTableBuf = deleteBeforeDelimiter(keyNumberTableBuf, ",");
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
String getValue(String& key) {
|
||||
String live = jsonReadStr(configLiveJson, key);
|
||||
String store = jsonReadStr(configStoreJson, key);
|
||||
if (live != nullptr) {
|
||||
return live;
|
||||
} else if (store != nullptr) {
|
||||
return store;
|
||||
} else if (store == nullptr && live == nullptr) {
|
||||
return "no value";
|
||||
} else {
|
||||
return "data error";
|
||||
}
|
||||
}
|
||||
41
src/Bus.cpp
41
src/Bus.cpp
@@ -1,41 +0,0 @@
|
||||
#include "Bus.h"
|
||||
#include "Class/NotAsync.h"
|
||||
#include "Global.h"
|
||||
|
||||
void busInit() {
|
||||
myNotAsyncActions->add(
|
||||
do_BUSSCAN, [&](void*) {
|
||||
String tmp = i2c_scan();
|
||||
if (tmp == "error") {
|
||||
tmp = i2c_scan();
|
||||
Serial.println(tmp);
|
||||
jsonWriteStr(configLiveJson, "i2c", tmp);
|
||||
}
|
||||
else {
|
||||
Serial.println(tmp);
|
||||
jsonWriteStr(configLiveJson, "i2c", tmp);
|
||||
}
|
||||
},
|
||||
nullptr);
|
||||
SerialPrint("I", F("Bus"), F("Bus Init"));
|
||||
}
|
||||
|
||||
String i2c_scan() {
|
||||
String out;
|
||||
byte count = 0;
|
||||
Wire.begin();
|
||||
for (byte i = 8; i < 120; i++) {
|
||||
Wire.beginTransmission(i);
|
||||
if (Wire.endTransmission() == 0) {
|
||||
count++;
|
||||
out += String(count) + ". 0x" + String(i, HEX) + "; ";
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
if (count == 0) {
|
||||
return "error";
|
||||
}
|
||||
else {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
#include "Class/CallBackTest.h"
|
||||
|
||||
CallBackTest::CallBackTest(){};
|
||||
|
||||
void CallBackTest::loop() {
|
||||
count++;
|
||||
if (count > 5000) {
|
||||
// Проверяем что переменная содержит указатель - не пустая не null
|
||||
// и непосредственно вызываем то, на что это указывает
|
||||
// просто пишем имя - без () - это указатель на фунецию.
|
||||
// () - вызываем функцию - с пустым набором параметров
|
||||
|
||||
if (_cb != NULL) {
|
||||
_cb();
|
||||
}
|
||||
//или ровно тоже самое
|
||||
//if (_cb) _cb();
|
||||
|
||||
if (_pcb) {
|
||||
if (_pcb("SomeTextValue")) {
|
||||
Serial.println("Got true!");
|
||||
} else {
|
||||
Serial.println("Got false!");
|
||||
}
|
||||
}
|
||||
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//передаем внутрь класса функцию любую void функцию без агрументов
|
||||
void CallBackTest::setCallback(AsyncActionCb cb) {
|
||||
_cb = cb;
|
||||
}
|
||||
|
||||
//передаем внутрь класса функцию любую void функцию с аргументами
|
||||
void CallBackTest::setCallback(AsyncParamActionCb pcb) {
|
||||
_pcb = pcb;
|
||||
}
|
||||
//CallBackTest* CB;
|
||||
|
||||
//CB->setCallback([]() {
|
||||
// Serial.println("123");
|
||||
//});
|
||||
//
|
||||
//CB->setCallback([](const String str) {
|
||||
// Serial.println(str);
|
||||
// return true;
|
||||
//});
|
||||
@@ -1,2 +0,0 @@
|
||||
#include "Class/LineParsing.h"
|
||||
LineParsing myLineParsing;
|
||||
@@ -1,30 +0,0 @@
|
||||
#include "Class/NotAsync.h"
|
||||
|
||||
NotAsync::NotAsync(uint8_t size) {
|
||||
this->items = new NotAsyncItem[size];
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
NotAsync::~NotAsync() {}
|
||||
|
||||
void NotAsync::add(uint8_t i, NotAsyncCb f, void* arg) {
|
||||
this->items[i].cb = f;
|
||||
this->items[i].cb_arg = arg;
|
||||
this->items[i].is_used = true;
|
||||
}
|
||||
|
||||
void NotAsync::loop() {
|
||||
if (this->items[task].is_used) {
|
||||
handle(this->items[task].cb, this->items[task].cb_arg);
|
||||
task = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void NotAsync::make(uint8_t task) {
|
||||
this->task = task;
|
||||
}
|
||||
|
||||
void NotAsync::handle(NotAsyncCb f, void* arg) {
|
||||
f(arg);
|
||||
}
|
||||
NotAsync* myNotAsyncActions;
|
||||
@@ -1,35 +0,0 @@
|
||||
#include "Class/ScenarioClass3.h"
|
||||
|
||||
#include "MqttClient.h"
|
||||
#include "RemoteOrdersUdp.h"
|
||||
Scenario* myScenario;
|
||||
|
||||
void eventGen2(String eventName, String eventValue) {
|
||||
if (!jsonReadBool(configSetupJson, "scen")) {
|
||||
return;
|
||||
}
|
||||
String event = eventName + " " + eventValue + ",";
|
||||
eventBuf += event;
|
||||
|
||||
SerialPrint("I", "Event add", eventName + " " + eventValue);
|
||||
|
||||
if (jsonReadBool(configSetupJson, "MqttOut")) {
|
||||
if (eventName != "timenow") {
|
||||
publishEvent(eventName, eventValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void streamEventUDP(String event) {
|
||||
#ifdef UDP_ENABLED
|
||||
|
||||
if (!jsonReadBool(configSetupJson, "snaUdp")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.indexOf("timenow") == -1) {
|
||||
event = "iotm;event:" + event;
|
||||
asyncUdp.broadcastTo(event.c_str(), 4210);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
#include "Class/TCircularBuffer.h"
|
||||
|
||||
TCircularBuffer<char *, 1024> *myTCircularBuffer;
|
||||
TCircularBuffer<char *, 20480> *myWsBuffer;
|
||||
@@ -1,16 +0,0 @@
|
||||
#include "Clock.h"
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
Clock* timeNow;
|
||||
void clockInit() {
|
||||
timeNow = new Clock;
|
||||
timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp"));
|
||||
timeNow->setTimezone(jsonReadStr(configSetupJson, "timezone").toInt());
|
||||
ts.add(
|
||||
TIME_SYNC, 30000, [&](void*) {
|
||||
timeNow->hasSync();
|
||||
},
|
||||
nullptr, true);
|
||||
SerialPrint("I", F("NTP"), F("Clock Init"));
|
||||
}
|
||||
334
src/FSEditor.cpp
334
src/FSEditor.cpp
@@ -1,334 +0,0 @@
|
||||
#include "FSEditor.h"
|
||||
|
||||
#include "FileSystem.h"
|
||||
|
||||
#define FS_MAXLENGTH_FILEPATH 32
|
||||
|
||||
const char *excludeListFile = "/.exclude.files";
|
||||
typedef struct ExcludeListS {
|
||||
char *item;
|
||||
ExcludeListS *next;
|
||||
} ExcludeList;
|
||||
|
||||
static ExcludeList *excludes = NULL;
|
||||
|
||||
static bool matchWild(const char *pattern, const char *testee) {
|
||||
const char *nxPat = NULL, *nxTst = NULL;
|
||||
|
||||
while (*testee) {
|
||||
if ((*pattern == '?') || (*pattern == *testee)) {
|
||||
pattern++;
|
||||
testee++;
|
||||
continue;
|
||||
}
|
||||
if (*pattern == '*') {
|
||||
nxPat = pattern++;
|
||||
nxTst = testee;
|
||||
continue;
|
||||
}
|
||||
if (nxPat) {
|
||||
pattern = nxPat + 1;
|
||||
testee = ++nxTst;
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
while (*pattern == '*') {
|
||||
pattern++;
|
||||
}
|
||||
return (*pattern == 0);
|
||||
}
|
||||
|
||||
static bool addExclude(const char *item) {
|
||||
size_t len = strlen(item);
|
||||
if (!len) {
|
||||
return false;
|
||||
}
|
||||
ExcludeList *e = (ExcludeList *)malloc(sizeof(ExcludeList));
|
||||
if (!e) {
|
||||
return false;
|
||||
}
|
||||
e->item = (char *)malloc(len + 1);
|
||||
if (!e->item) {
|
||||
free(e);
|
||||
return false;
|
||||
}
|
||||
memcpy(e->item, item, len + 1);
|
||||
e->next = excludes;
|
||||
excludes = e;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void loadExcludeList(fs::FS &_fs, const char *filename) {
|
||||
static char linebuf[FS_MAXLENGTH_FILEPATH];
|
||||
fs::File excludeFile = _fs.open(filename, "r");
|
||||
if (!excludeFile) {
|
||||
return;
|
||||
}
|
||||
if (excludeFile.isDirectory()) {
|
||||
excludeFile.close();
|
||||
return;
|
||||
}
|
||||
if (excludeFile.size() > 0) {
|
||||
uint8_t idx;
|
||||
bool isOverflowed = false;
|
||||
while (excludeFile.available()) {
|
||||
linebuf[0] = '\0';
|
||||
idx = 0;
|
||||
int lastChar;
|
||||
do {
|
||||
lastChar = excludeFile.read();
|
||||
if (lastChar != '\r') {
|
||||
linebuf[idx++] = (char)lastChar;
|
||||
}
|
||||
} while ((lastChar >= 0) && (lastChar != '\n') && (idx < FS_MAXLENGTH_FILEPATH));
|
||||
|
||||
if (isOverflowed) {
|
||||
isOverflowed = (lastChar != '\n');
|
||||
continue;
|
||||
}
|
||||
isOverflowed = (idx >= FS_MAXLENGTH_FILEPATH);
|
||||
linebuf[idx - 1] = '\0';
|
||||
if (!addExclude(linebuf)) {
|
||||
excludeFile.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
excludeFile.close();
|
||||
}
|
||||
|
||||
static bool isExcluded(fs::FS &_fs, const char *filename) {
|
||||
if (excludes == NULL) {
|
||||
loadExcludeList(_fs, excludeListFile);
|
||||
}
|
||||
if (strcmp(excludeListFile, filename) == 0) return true;
|
||||
ExcludeList *e = excludes;
|
||||
while (e) {
|
||||
if (matchWild(e->item, filename)) {
|
||||
return true;
|
||||
}
|
||||
e = e->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// WEB HANDLER IMPLEMENTATION
|
||||
|
||||
#ifdef ESP32
|
||||
FSEditor::FSEditor(const fs::FS &fs, const String &username, const String &password)
|
||||
#else
|
||||
FSEditor::FSEditor(const String &username, const String &password, const fs::FS &fs)
|
||||
#endif
|
||||
: _fs(fs), _username(username), _password(password), _authenticated(false), _startTime(0) {
|
||||
}
|
||||
|
||||
bool FSEditor::canHandle(AsyncWebServerRequest *request) {
|
||||
if (request->url().equalsIgnoreCase("/edit")) {
|
||||
if (request->method() == HTTP_GET) {
|
||||
if (request->hasParam("list"))
|
||||
return true;
|
||||
if (request->hasParam("edit")) {
|
||||
request->_tempFile = _fs.open(request->arg("edit"), "r");
|
||||
if (!request->_tempFile) {
|
||||
return false;
|
||||
}
|
||||
if (request->_tempFile.isDirectory()) {
|
||||
request->_tempFile.close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (request->hasParam("download")) {
|
||||
request->_tempFile = _fs.open(request->arg("download"), "r");
|
||||
if (!request->_tempFile) {
|
||||
return false;
|
||||
}
|
||||
if (request->_tempFile.isDirectory()) {
|
||||
request->_tempFile.close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
request->addInterestingHeader("If-Modified-Since");
|
||||
return true;
|
||||
} else if (request->method() == HTTP_POST)
|
||||
return true;
|
||||
else if (request->method() == HTTP_DELETE)
|
||||
return true;
|
||||
else if (request->method() == HTTP_PUT)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
void FSEditor::getDirList(const String &path, String &output) {
|
||||
auto dir = _fs.openDir(path.c_str());
|
||||
while (dir.next()) {
|
||||
String fname = dir.fileName();
|
||||
if (!path.endsWith("/") && !fname.startsWith("/")) {
|
||||
fname = "/" + fname;
|
||||
}
|
||||
fname = path + fname;
|
||||
if (isExcluded(_fs, fname.c_str())) {
|
||||
continue;
|
||||
}
|
||||
if (dir.isDirectory()) {
|
||||
getDirList(fname, output);
|
||||
continue;
|
||||
}
|
||||
if (output != "[") output += ',';
|
||||
char buf[128];
|
||||
sprintf(buf, "{\"type\":\"file\",\"name\":\"%s\",\"size\":%d}", fname.c_str(), dir.fileSize());
|
||||
output += buf;
|
||||
}
|
||||
}
|
||||
#else
|
||||
void FSEditor::getDirList(const String &path, String &output) {
|
||||
auto dir = _fs.open(path, FILE_READ);
|
||||
dir.rewindDirectory();
|
||||
while (dir.openNextFile()) {
|
||||
String fname = dir.name();
|
||||
if (!path.endsWith("/") && !fname.startsWith("/")) {
|
||||
fname = "/" + fname;
|
||||
}
|
||||
fname = path + fname;
|
||||
if (isExcluded(_fs, fname.c_str())) {
|
||||
continue;
|
||||
}
|
||||
if (dir.isDirectory()) {
|
||||
getDirList(fname, output);
|
||||
continue;
|
||||
}
|
||||
if (output != "[") output += ',';
|
||||
char buf[128];
|
||||
sprintf(buf, "{\"type\":\"file\",\"name\":\"%s\",\"size\":%d}", fname.c_str(), dir.size());
|
||||
output += buf;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void FSEditor::handleRequest(AsyncWebServerRequest *request) {
|
||||
if (_username.length() && _password.length() && !request->authenticate(_username.c_str(), _password.c_str()))
|
||||
return request->requestAuthentication();
|
||||
|
||||
if (request->method() == HTTP_GET) {
|
||||
if (request->hasParam("list")) {
|
||||
if (request->hasParam("list")) {
|
||||
String path = request->getParam("list")->value();
|
||||
String output = "[";
|
||||
getDirList(path, output);
|
||||
output += "]";
|
||||
request->send(200, "application/json", output);
|
||||
output = String();
|
||||
}
|
||||
} else if (request->hasParam("edit") || request->hasParam("download")) {
|
||||
#ifdef ESP8266
|
||||
request->send(request->_tempFile, request->_tempFile.fullName(), String(), request->hasParam("download"));
|
||||
#else
|
||||
request->send(request->_tempFile, request->_tempFile.name(), String(), request->hasParam("download"));
|
||||
#endif
|
||||
} else {
|
||||
const char *buildTime = __DATE__ " " __TIME__ " GMT";
|
||||
if (request->header("If-Modified-Since").equals(buildTime)) {
|
||||
request->send(304);
|
||||
} else {
|
||||
AsyncWebServerResponse *response = request->beginResponse(FileFS, "/edit.htm", "text/html");
|
||||
// response->addHeader("Content-Encoding", "gzip");
|
||||
response->addHeader("Last-Modified", buildTime);
|
||||
request->send(response);
|
||||
}
|
||||
}
|
||||
} else if (request->method() == HTTP_DELETE) {
|
||||
if (request->hasParam("path", true)) {
|
||||
if (!(_fs.remove(request->getParam("path", true)->value()))) {
|
||||
#ifdef ESP32
|
||||
_fs.rmdir(request->getParam("path", true)->value()); // try rmdir for littlefs
|
||||
#endif
|
||||
}
|
||||
|
||||
request->send(200, "", "DELETE: " + request->getParam("path", true)->value());
|
||||
} else
|
||||
request->send(404);
|
||||
} else if (request->method() == HTTP_POST) {
|
||||
if (request->hasParam("data", true, true) && _fs.exists(request->getParam("data", true, true)->value()))
|
||||
request->send(200, "", "UPLOADED: " + request->getParam("data", true, true)->value());
|
||||
|
||||
else if (request->hasParam("rawname", true) && request->hasParam("raw0", true)) {
|
||||
String rawnam = request->getParam("rawname", true)->value();
|
||||
|
||||
if (_fs.exists(rawnam)) _fs.remove(rawnam); // delete it to allow a mode
|
||||
|
||||
int k = 0;
|
||||
uint16_t i = 0;
|
||||
fs::File f = _fs.open(rawnam, "a");
|
||||
|
||||
while (request->hasParam("raw" + String(k), true)) { //raw0 .. raw1
|
||||
if (f) {
|
||||
i += f.print(request->getParam("raw" + String(k), true)->value());
|
||||
}
|
||||
k++;
|
||||
}
|
||||
f.close();
|
||||
request->send(200, "", "IPADWRITE: " + rawnam + ":" + String(i));
|
||||
|
||||
} else {
|
||||
request->send(500);
|
||||
}
|
||||
|
||||
} else if (request->method() == HTTP_PUT) {
|
||||
if (request->hasParam("path", true)) {
|
||||
String filename = request->getParam("path", true)->value();
|
||||
if (_fs.exists(filename)) {
|
||||
request->send(200);
|
||||
} else {
|
||||
/*******************************************************/
|
||||
#ifdef ESP32
|
||||
if (strchr(filename.c_str(), '/')) {
|
||||
// For file creation, silently make subdirs as needed. If any fail,
|
||||
// it will be caught by the real file open later on
|
||||
char *pathStr = strdup(filename.c_str());
|
||||
if (pathStr) {
|
||||
// Make dirs up to the final fnamepart
|
||||
char *ptr = strchr(pathStr, '/');
|
||||
while (ptr) {
|
||||
*ptr = 0;
|
||||
_fs.mkdir(pathStr);
|
||||
*ptr = '/';
|
||||
ptr = strchr(ptr + 1, '/');
|
||||
}
|
||||
}
|
||||
free(pathStr);
|
||||
}
|
||||
#endif
|
||||
/*******************************************************/
|
||||
fs::File f = _fs.open(filename, "w");
|
||||
if (f) {
|
||||
f.write((uint8_t)0x00);
|
||||
f.close();
|
||||
request->send(200, "", "CREATE: " + filename);
|
||||
} else {
|
||||
request->send(500);
|
||||
}
|
||||
}
|
||||
} else
|
||||
request->send(400);
|
||||
}
|
||||
}
|
||||
|
||||
void FSEditor::handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) {
|
||||
if (!index) {
|
||||
if (!_username.length() || request->authenticate(_username.c_str(), _password.c_str())) {
|
||||
_authenticated = true;
|
||||
request->_tempFile = _fs.open(filename, "w");
|
||||
_startTime = millis();
|
||||
}
|
||||
}
|
||||
if (_authenticated && request->_tempFile) {
|
||||
if (len) {
|
||||
request->_tempFile.write(data, len);
|
||||
}
|
||||
if (final) {
|
||||
request->_tempFile.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
#include "FileSystem.h"
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
#ifdef ESP8266
|
||||
bool getInfo(FSInfo& info) {
|
||||
return FileFS.info(info);
|
||||
}
|
||||
|
||||
// Информация о ФС
|
||||
void getFSInfo() {
|
||||
FSInfo buf;
|
||||
if (getInfo(buf)) {
|
||||
//SerialPrint("I", F("FS"), F("Get FS info completed"));
|
||||
|
||||
size_t totalBytes = buf.totalBytes; // всего
|
||||
size_t usedBytes = buf.usedBytes; // использовано
|
||||
//size_t maxOpenFiles = buf.maxOpenFiles; // лимит на открые файлы
|
||||
//size_t blockSize = buf.blockSize;
|
||||
//size_t pageSize = buf.pageSize;
|
||||
//size_t maxPathLength = buf.maxPathLength; // лимит на пути и имена файлов
|
||||
|
||||
size_t freeBytes = totalBytes - usedBytes;
|
||||
float freePer = ((float)freeBytes / totalBytes) * 100;
|
||||
|
||||
jsonWriteStr(configSetupJson, F("freeBytes"), String(freePer) + "% (" + prettyBytes(freeBytes) + ")");
|
||||
|
||||
//SerialPrint("I", F("FS"), "totalBytes=" + String(totalBytes));
|
||||
//SerialPrint("I", F("FS"), "usedBytes=" + String(usedBytes));
|
||||
//SerialPrint("I", F("FS"), "maxOpenFiles=" + String(maxOpenFiles));
|
||||
//SerialPrint("I", F("FS"), "blockSize=" + String(blockSize));
|
||||
//SerialPrint("I", F("FS"), "pageSize=" + String(pageSize));
|
||||
//SerialPrint("I", F("FS"), "maxPathLength=" + String(maxPathLength));
|
||||
//SerialPrint("I", F("FS"), "freeBytes=" + String(freeBytes));
|
||||
//SerialPrint("I", F("FS"), "freePer=" + String(freePer));
|
||||
} else {
|
||||
SerialPrint("E", F("FS"), F("FS info error"));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
void getFSInfo() {
|
||||
size_t totalBytes = FileFS.totalBytes(); // всего
|
||||
size_t usedBytes = FileFS.usedBytes(); // использовано
|
||||
size_t freeBytes = totalBytes - usedBytes;
|
||||
float freePer = ((float)freeBytes / totalBytes) * 100;
|
||||
jsonWriteStr(configSetupJson, F("freeBytes"), String(freePer) + "% (" + prettyBytes(freeBytes) + ")");
|
||||
}
|
||||
#endif
|
||||
@@ -1,95 +0,0 @@
|
||||
#include "Global.h"
|
||||
|
||||
#ifdef WS_enable
|
||||
AsyncWebSocket ws;
|
||||
AsyncEventSource events;
|
||||
#endif
|
||||
|
||||
TickerScheduler ts(MYTEST + 1);
|
||||
WiFiClient espClient;
|
||||
PubSubClient mqtt(espClient);
|
||||
StringCommand sCmd;
|
||||
AsyncWebServer server(80);
|
||||
|
||||
/*
|
||||
* Global vars
|
||||
*/
|
||||
|
||||
boolean just_load = true;
|
||||
boolean telegramInitBeen = false;
|
||||
boolean savedFromWeb = false;
|
||||
boolean wsSetupFlag = false;
|
||||
|
||||
// Json
|
||||
String configSetupJson = "{}";
|
||||
String configLiveJson = "{}";
|
||||
String configStoreJson = "{}";
|
||||
String configOptionJson = "{}";
|
||||
String telegramMsgJson = "{}";
|
||||
|
||||
// Mqtt
|
||||
String mqttServer = "";
|
||||
int mqttPort = 0;
|
||||
String mqttPrefix = "";
|
||||
String mqttUser = "";
|
||||
String mqttPass = "";
|
||||
|
||||
String chipId = "";
|
||||
String prex = "";
|
||||
String all_widgets = "";
|
||||
String scenario = "";
|
||||
String mqttRootDevice = "";
|
||||
|
||||
int mqttConnectAttempts = 0;
|
||||
bool changeBroker = false;
|
||||
int currentBroker = 1;
|
||||
|
||||
// web sockets
|
||||
int wsAttempts = 100;
|
||||
//char* wsBufChar = "";
|
||||
|
||||
// orders and events
|
||||
String orderBuf = "";
|
||||
String wsBuf = "";
|
||||
String eventBuf = "";
|
||||
String mysensorBuf = "";
|
||||
String itemsFile = "";
|
||||
String itemsLine = "";
|
||||
|
||||
// key lists and numbers
|
||||
//=========================================
|
||||
String impuls_KeyList = "";
|
||||
int impuls_EnterCounter = -1;
|
||||
//=========================================
|
||||
String buttonOut_KeyList = "";
|
||||
int buttonOut_EnterCounter = -1;
|
||||
//=========================================
|
||||
String input_KeyList = "";
|
||||
int input_EnterCounter = -1;
|
||||
//=========================================
|
||||
String output_KeyList = "";
|
||||
int output_EnterCounter = -1;
|
||||
//=========================================
|
||||
String pwmOut_KeyList = "";
|
||||
int pwmOut_EnterCounter = -1;
|
||||
//=========================================
|
||||
String countDown_KeyList = "";
|
||||
int countDown_EnterCounter = -1;
|
||||
//=========================================
|
||||
String logging_KeyList = "";
|
||||
int logging_EnterCounter = -1;
|
||||
//=========================================
|
||||
|
||||
String itemName;
|
||||
String presetName;
|
||||
|
||||
// Upgrade
|
||||
String serverIP;
|
||||
|
||||
// Scenario
|
||||
int scenario_line_status[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
int lastVersion;
|
||||
|
||||
boolean busScanFlag = false;
|
||||
boolean fsCheckFlag = false;
|
||||
boolean delElementFlag = false;
|
||||
215
src/Init.cpp
215
src/Init.cpp
@@ -1,215 +0,0 @@
|
||||
#include "Init.h"
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Cmd.h"
|
||||
#include "Global.h"
|
||||
#include "items/vButtonOut.h"
|
||||
#include "items/vCountDown.h"
|
||||
#include "items/vImpulsOut.h"
|
||||
#include "items/vInput.h"
|
||||
#include "items/vLogging.h"
|
||||
#include "items/vOutput.h"
|
||||
#include "items/vPwmOut.h"
|
||||
#include "items/vSensorAnalog.h"
|
||||
#include "items/vSensorBme280.h"
|
||||
#include "items/vSensorBmp280.h"
|
||||
#include "items/vSensorCcs811.h"
|
||||
#include "items/vSensorDallas.h"
|
||||
#include "items/vSensorDht.h"
|
||||
#include "items/vSensorNode.h"
|
||||
#include "items/vSensorPzem.h"
|
||||
#include "items/vSensorUltrasonic.h"
|
||||
#include "items/vSensorUptime.h"
|
||||
|
||||
void loadConfig() {
|
||||
configSetupJson = readFile("config.json", 4096);
|
||||
configSetupJson.replace("\r\n", "");
|
||||
|
||||
String tmp = readFile("store.json", 4096);
|
||||
if (tmp != "failed") {
|
||||
configStoreJson = tmp;
|
||||
}
|
||||
configStoreJson.replace("\r\n", "");
|
||||
|
||||
jsonWriteStr(configSetupJson, "warning1", "");
|
||||
jsonWriteStr(configSetupJson, "warning2", "");
|
||||
jsonWriteStr(configSetupJson, "warning3", "");
|
||||
|
||||
jsonWriteStr(configSetupJson, "chipID", chipId);
|
||||
jsonWriteInt(configSetupJson, "firmware_version", FIRMWARE_VERSION);
|
||||
jsonWriteStr(configSetupJson, "firmware_name", FIRMWARE_NAME);
|
||||
|
||||
selectBroker();
|
||||
|
||||
serverIP = jsonReadStr(configSetupJson, "serverip");
|
||||
|
||||
SerialPrint("I", F("Conf"), F("Config Json Init"));
|
||||
}
|
||||
|
||||
void espInit() {
|
||||
deviceInit();
|
||||
loadScenario();
|
||||
SerialPrint("I", F("esp"), F("esp Init"));
|
||||
}
|
||||
|
||||
void deviceInit() {
|
||||
clearVectors();
|
||||
|
||||
#ifdef LAYOUT_IN_RAM
|
||||
all_widgets = "";
|
||||
#else
|
||||
removeFile(String("layout.txt"));
|
||||
#endif
|
||||
|
||||
myLineParsing.clearErrors();
|
||||
|
||||
fileCmdExecute(String(DEVICE_CONFIG_FILE));
|
||||
|
||||
int errors = myLineParsing.getPinErrors();
|
||||
|
||||
if (errors > 0) {
|
||||
jsonWriteStr(configSetupJson, F("warning3"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Обнаружен неверный номер пина</p></font></div>"));
|
||||
} else {
|
||||
jsonWriteStr(configSetupJson, F("warning3"), "");
|
||||
}
|
||||
|
||||
savedFromWeb = false;
|
||||
// publishWidgets();
|
||||
// publishState();
|
||||
}
|
||||
|
||||
void loadScenario() {
|
||||
if (jsonReadStr(configSetupJson, "scen") == "1") {
|
||||
scenario = readFile(String(DEVICE_SCENARIO_FILE), 10240);
|
||||
scenario.replace("\r\n", "\n");
|
||||
scenario.replace("\r", "\n");
|
||||
scenario.replace("~\n", "");
|
||||
scenario.replace("~", "");
|
||||
scenario += "\n";
|
||||
if (scenario.startsWith("setup\n")) {
|
||||
String setup = selectToMarker(scenario, "end\n");
|
||||
setup.replace("setup\n", "");
|
||||
spaceCmdExecute(setup);
|
||||
scenario = deleteBeforeDelimiter(scenario, "end\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void uptime_init() {
|
||||
ts.add(
|
||||
UPTIME, 5000, [&](void*) {
|
||||
handle_uptime();
|
||||
},
|
||||
nullptr, true);
|
||||
SerialPrint("I", F("Uptime"), F("Uptime Init"));
|
||||
}
|
||||
|
||||
void handle_uptime() {
|
||||
jsonWriteStr(configSetupJson, "uptime", timeNow->getUptime());
|
||||
}
|
||||
|
||||
void clearVectors() {
|
||||
#ifdef EnableLogging
|
||||
if (myLogging != nullptr) {
|
||||
myLogging->clear();
|
||||
}
|
||||
logging_KeyList = "";
|
||||
logging_EnterCounter = -1;
|
||||
#endif
|
||||
#ifdef EnableImpulsOut
|
||||
if (myImpulsOut != nullptr) {
|
||||
myImpulsOut->clear();
|
||||
}
|
||||
impuls_KeyList = "";
|
||||
impuls_EnterCounter = -1;
|
||||
#endif
|
||||
|
||||
#ifdef EnableCountDown
|
||||
if (myCountDown != nullptr) {
|
||||
myCountDown->clear();
|
||||
}
|
||||
countDown_KeyList = "";
|
||||
countDown_EnterCounter = -1;
|
||||
#endif
|
||||
|
||||
#ifdef EnableButtonOut
|
||||
if (myButtonOut != nullptr) {
|
||||
myButtonOut->clear();
|
||||
}
|
||||
buttonOut_KeyList = "";
|
||||
buttonOut_EnterCounter = -1;
|
||||
#endif
|
||||
#ifdef EnableInput
|
||||
if (myInput != nullptr) {
|
||||
myInput->clear();
|
||||
}
|
||||
input_KeyList = "";
|
||||
input_EnterCounter = -1;
|
||||
#endif
|
||||
#ifdef EnableOutput
|
||||
if (myOutput != nullptr) {
|
||||
myOutput->clear();
|
||||
}
|
||||
output_KeyList = "";
|
||||
output_EnterCounter = -1;
|
||||
#endif
|
||||
#ifdef EnablePwmOut
|
||||
if (myPwmOut != nullptr) {
|
||||
myPwmOut->clear();
|
||||
}
|
||||
pwmOut_KeyList = "";
|
||||
pwmOut_EnterCounter = -1;
|
||||
#endif
|
||||
//==================================
|
||||
#ifdef EnableSensorDallas
|
||||
if (mySensorDallas2 != nullptr) {
|
||||
mySensorDallas2->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorUltrasonic
|
||||
if (mySensorUltrasonic != nullptr) {
|
||||
mySensorUltrasonic->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorAnalog
|
||||
if (mySensorAnalog != nullptr) {
|
||||
mySensorAnalog->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorDht
|
||||
if (mySensorDht != nullptr) {
|
||||
mySensorDht->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorBme280
|
||||
if (mySensorBme280 != nullptr) {
|
||||
mySensorBme280->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorBmp280
|
||||
if (mySensorBmp280 != nullptr) {
|
||||
mySensorBmp280->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorCcs811
|
||||
if (mySensorCcs811 != nullptr) {
|
||||
mySensorCcs811->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorPzem
|
||||
if (mySensorPzem != nullptr) {
|
||||
mySensorPzem->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorUptime
|
||||
if (mySensorUptime != nullptr) {
|
||||
mySensorUptime->clear();
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorNode
|
||||
if (mySensorNode != nullptr) {
|
||||
mySensorNode->clear();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -1,266 +0,0 @@
|
||||
#include "ItemsList.h"
|
||||
|
||||
#include "Class/NotAsync.h"
|
||||
#include "FileSystem.h"
|
||||
#include "Init.h"
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
static const char* firstLine PROGMEM = "Удалить;Тип элемента;Id;Виджет;Имя вкладки;Имя виджета;Позиция виджета";
|
||||
|
||||
void itemsListInit() {
|
||||
myNotAsyncActions->add(
|
||||
do_deviceInit, [&](void*) {
|
||||
deviceInit();
|
||||
},
|
||||
nullptr);
|
||||
|
||||
myNotAsyncActions->add(
|
||||
do_delChoosingItems, [&](void*) {
|
||||
delChoosingItems();
|
||||
},
|
||||
nullptr);
|
||||
|
||||
SerialPrint("I", F("Items"), F("Items Init"));
|
||||
}
|
||||
|
||||
void addItem2(int num) {
|
||||
File configFile = FileFS.open("/items/items.txt", "r");
|
||||
if (!configFile) {
|
||||
return;
|
||||
}
|
||||
configFile.seek(0, SeekSet);
|
||||
String seachingLine;
|
||||
int i = 0;
|
||||
while (configFile.position() != configFile.size()) {
|
||||
i++;
|
||||
String item = configFile.readStringUntil('*');
|
||||
if (i == num) {
|
||||
if (i == 1) {
|
||||
seachingLine = "\n" + item;
|
||||
} else {
|
||||
seachingLine = item;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
configFile.close();
|
||||
|
||||
randomSeed(micros());
|
||||
unsigned int rnd = random(0, 1000);
|
||||
seachingLine.replace("id", String(rnd));
|
||||
seachingLine.replace("order", String(getNewElementNumber("order.txt")));
|
||||
if (seachingLine.indexOf("gpio") != -1) {
|
||||
seachingLine.replace("gpio", "pin[" + String(getFreePinAll()) + "]");
|
||||
}
|
||||
|
||||
addFile(DEVICE_CONFIG_FILE, seachingLine);
|
||||
//Serial.println(seachingLine);
|
||||
}
|
||||
|
||||
void addItemAuto(int num, String key, String widget, String descr) {
|
||||
File configFile = FileFS.open("/items/itemsAuto.txt", "r");
|
||||
if (!configFile) {
|
||||
return;
|
||||
}
|
||||
configFile.seek(0, SeekSet);
|
||||
String seachingLine;
|
||||
int i = 0;
|
||||
while (configFile.position() != configFile.size()) {
|
||||
i++;
|
||||
String item = configFile.readStringUntil('*');
|
||||
if (i == num) {
|
||||
if (i == 1) {
|
||||
seachingLine = "\n" + item;
|
||||
} else {
|
||||
seachingLine = item;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
configFile.close();
|
||||
|
||||
seachingLine.replace("id", key);
|
||||
seachingLine.replace("file", widget);
|
||||
seachingLine.replace("descr", descr);
|
||||
seachingLine.replace("order", String(getNewElementNumber("order.txt")));
|
||||
|
||||
addFile(DEVICE_CONFIG_FILE, seachingLine);
|
||||
}
|
||||
|
||||
bool isItemAdded(String key) {
|
||||
File configFile = FileFS.open("/" + String(DEVICE_CONFIG_FILE), "r");
|
||||
if (!configFile) {
|
||||
return false;
|
||||
}
|
||||
configFile.seek(0, SeekSet);
|
||||
String seachingLine;
|
||||
while (configFile.position() != configFile.size()) {
|
||||
String item = configFile.readStringUntil('\n');
|
||||
String foundKey = selectFromMarkerToMarker(item, ";", 2);
|
||||
if (foundKey == key) {
|
||||
configFile.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
configFile.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
void addPreset2(int num) {
|
||||
File configFile = FileFS.open("/presets/presets.c.txt", "r");
|
||||
if (!configFile) {
|
||||
return;
|
||||
}
|
||||
configFile.seek(0, SeekSet);
|
||||
String config;
|
||||
int i1 = 0;
|
||||
while (configFile.position() != configFile.size()) {
|
||||
i1++;
|
||||
String item = configFile.readStringUntil('*');
|
||||
if (i1 == num) {
|
||||
if (i1 == 1) {
|
||||
config = "\n" + item;
|
||||
} else {
|
||||
config = item;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
configFile.close();
|
||||
addFile(DEVICE_CONFIG_FILE, config);
|
||||
//===========================================================================
|
||||
File scenFile = FileFS.open("/presets/presets.s.txt", "r");
|
||||
if (!scenFile) {
|
||||
return;
|
||||
}
|
||||
scenFile.seek(0, SeekSet);
|
||||
String scen;
|
||||
int i2 = 0;
|
||||
while (scenFile.position() != scenFile.size()) {
|
||||
i2++;
|
||||
String item = scenFile.readStringUntil('*');
|
||||
if (i2 == num) {
|
||||
if (i1 == 1) {
|
||||
scen = "\n" + item;
|
||||
} else {
|
||||
scen = item;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
scenFile.close();
|
||||
if (readFile(String(DEVICE_SCENARIO_FILE), 2048) == "//") {
|
||||
removeFile(DEVICE_SCENARIO_FILE);
|
||||
scen = deleteBeforeDelimiter(scen, "\n");
|
||||
addFile(DEVICE_SCENARIO_FILE, scen);
|
||||
} else {
|
||||
addFile(DEVICE_SCENARIO_FILE, scen);
|
||||
}
|
||||
}
|
||||
|
||||
void delAllItems() {
|
||||
removeFile(DEVICE_CONFIG_FILE);
|
||||
addFile(DEVICE_CONFIG_FILE, String(firstLine));
|
||||
removeFile(DEVICE_SCENARIO_FILE);
|
||||
addFile(DEVICE_SCENARIO_FILE, "//");
|
||||
removeFile("id.txt");
|
||||
removeFile("order.txt");
|
||||
removeFile("pins.txt");
|
||||
}
|
||||
|
||||
uint8_t getNewElementNumber(String file) {
|
||||
uint8_t number = readFile(file, 100).toInt();
|
||||
number++;
|
||||
removeFile(file);
|
||||
addFile(file, String(number));
|
||||
return number;
|
||||
}
|
||||
|
||||
uint8_t getFreePinAll() {
|
||||
#ifdef ESP8266
|
||||
uint8_t pins[] = {0, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5};
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
uint8_t pins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33, 34, 35, 36, 39};
|
||||
#endif
|
||||
uint8_t array_sz = sizeof(pins) / sizeof(pins[0]);
|
||||
uint8_t i = getNewElementNumber("pins.txt");
|
||||
if (i < array_sz) {
|
||||
return pins[i];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool isPinExist(unsigned int num) {
|
||||
bool ret = false;
|
||||
#ifdef ESP8266
|
||||
unsigned int pins[] = {0, 1, 2, 3, 4, 5, 9, 10, 12, 13, 14, 15, 16};
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
unsigned int pins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33, 34, 35, 36, 39};
|
||||
#endif
|
||||
uint8_t array_sz = sizeof(pins) / sizeof(pins[0]);
|
||||
for (uint8_t i = 0; i < array_sz; i++) {
|
||||
if (pins[i] == num) ret = true;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t getFreePinAnalog() {
|
||||
#ifdef ESP8266
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void delChoosingItems() {
|
||||
File configFile = FileFS.open("/" + String(DEVICE_CONFIG_FILE), "r");
|
||||
if (!configFile) {
|
||||
return;
|
||||
}
|
||||
configFile.seek(0, SeekSet);
|
||||
String finalConf;
|
||||
bool firstLine = true;
|
||||
while (configFile.position() != configFile.size()) {
|
||||
String item = configFile.readStringUntil('\n');
|
||||
if (firstLine) {
|
||||
finalConf += item;
|
||||
} else {
|
||||
int checkbox = selectToMarker(item, ";").toInt();
|
||||
if (checkbox == 0) {
|
||||
finalConf += "\n" + item;
|
||||
}
|
||||
}
|
||||
firstLine = false;
|
||||
}
|
||||
removeFile(String(DEVICE_CONFIG_FILE));
|
||||
addFile(String(DEVICE_CONFIG_FILE), finalConf);
|
||||
//Serial.println(finalConf);
|
||||
configFile.close();
|
||||
}
|
||||
|
||||
//void delChoosingItemsByNum(int num) {
|
||||
// File configFile = FileFS.open("/" + String(DEVICE_CONFIG_FILE), "r");
|
||||
// if (!configFile) {
|
||||
// return;
|
||||
// }
|
||||
// configFile.seek(0, SeekSet);
|
||||
// String finalConf;
|
||||
//
|
||||
// int i = -1;
|
||||
// while (configFile.position() != configFile.size()) {
|
||||
// i++;
|
||||
// String item = configFile.readStringUntil('\n');
|
||||
// if (i == 0) {
|
||||
// finalConf += item;
|
||||
// } else {
|
||||
// if (i != num) {
|
||||
// finalConf += "\n" + item;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// removeFile(String(DEVICE_CONFIG_FILE));
|
||||
// addFile(String(DEVICE_CONFIG_FILE), finalConf);
|
||||
// Serial.println(finalConf);
|
||||
// configFile.close();
|
||||
//}
|
||||
@@ -1,417 +0,0 @@
|
||||
#include "MqttClient.h"
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/NotAsync.h"
|
||||
#include "Global.h"
|
||||
#include "Init.h"
|
||||
#include "items/vLogging.h"
|
||||
#include "items/vSensorNode.h"
|
||||
|
||||
void mqttInit() {
|
||||
myNotAsyncActions->add(
|
||||
do_MQTTPARAMSCHANGED, [&](void*) {
|
||||
mqttReconnect();
|
||||
},
|
||||
nullptr);
|
||||
|
||||
mqtt.setCallback(mqttCallback);
|
||||
|
||||
ts.add(
|
||||
WIFI_MQTT_CONNECTION_CHECK, MQTT_RECONNECT_INTERVAL,
|
||||
[&](void*) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
SerialPrint("I", F("WIFI"), F("OK"));
|
||||
if (mqtt.connected()) {
|
||||
SerialPrint("I", F("MQTT"), "OK, broker No " + String(currentBroker));
|
||||
setLedStatus(LED_OFF);
|
||||
} else {
|
||||
SerialPrint("E", F("MQTT"), F("✖ Connection lost"));
|
||||
mqttConnect();
|
||||
}
|
||||
} else {
|
||||
SerialPrint("E", F("WIFI"), F("✖ Lost WiFi connection"));
|
||||
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
||||
startAPMode();
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
myNotAsyncActions->add(
|
||||
do_sendScenMQTT, [&](void*) {
|
||||
String scen = readFile(String(DEVICE_SCENARIO_FILE), 2048);
|
||||
publishInfo("scen", scen);
|
||||
},
|
||||
nullptr);
|
||||
}
|
||||
|
||||
void mqttDisconnect() {
|
||||
SerialPrint("I", F("MQTT"), F("disconnected"));
|
||||
mqtt.disconnect();
|
||||
}
|
||||
|
||||
void mqttReconnect() {
|
||||
mqttDisconnect();
|
||||
mqttConnect();
|
||||
}
|
||||
|
||||
void mqttLoop() {
|
||||
if (!isNetworkActive() || !mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
mqtt.loop();
|
||||
}
|
||||
|
||||
void mqttSubscribe() {
|
||||
SerialPrint("I", F("MQTT"), F("subscribed"));
|
||||
SerialPrint("I", "MQTT", mqttRootDevice);
|
||||
mqtt.subscribe(mqttPrefix.c_str());
|
||||
mqtt.subscribe((mqttRootDevice + "/+/control").c_str());
|
||||
mqtt.subscribe((mqttRootDevice + "/update").c_str());
|
||||
|
||||
if (jsonReadBool(configSetupJson, "MqttIn")) {
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/event").c_str());
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/order").c_str());
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void selectBroker() {
|
||||
if (changeBroker) {
|
||||
changeBroker = false;
|
||||
if (currentBroker == 1) {
|
||||
getMqttData2();
|
||||
} else if (currentBroker == 2) {
|
||||
getMqttData1();
|
||||
}
|
||||
} else {
|
||||
if (currentBroker == 1) {
|
||||
getMqttData1();
|
||||
} else if (currentBroker == 2) {
|
||||
getMqttData2();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void getMqttData1() {
|
||||
currentBroker = 1;
|
||||
mqttServer = jsonReadStr(configSetupJson, F("mqttServer"));
|
||||
mqttPort = jsonReadInt(configSetupJson, F("mqttPort"));
|
||||
mqttPrefix = jsonReadStr(configSetupJson, F("mqttPrefix"));
|
||||
mqttUser = jsonReadStr(configSetupJson, F("mqttUser"));
|
||||
mqttPass = jsonReadStr(configSetupJson, F("mqttPass"));
|
||||
prex = mqttPrefix + "/" + chipId;
|
||||
}
|
||||
|
||||
void getMqttData2() {
|
||||
currentBroker = 2;
|
||||
mqttServer = jsonReadStr(configSetupJson, F("mqttServer2"));
|
||||
mqttPort = jsonReadInt(configSetupJson, F("mqttPort2"));
|
||||
mqttPrefix = jsonReadStr(configSetupJson, F("mqttPrefix2"));
|
||||
mqttUser = jsonReadStr(configSetupJson, F("mqttUser2"));
|
||||
mqttPass = jsonReadStr(configSetupJson, F("mqttPass2"));
|
||||
prex = mqttPrefix + "/" + chipId;
|
||||
}
|
||||
|
||||
bool isSecondBrokerSet() {
|
||||
bool res = true;
|
||||
if (jsonReadStr(configSetupJson, F("mqttServer2")) == "") {
|
||||
res = false;
|
||||
}
|
||||
if (jsonReadStr(configSetupJson, F("mqttPrefix2")) == "") {
|
||||
res = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
boolean mqttConnect() {
|
||||
selectBroker();
|
||||
bool res = false;
|
||||
if (mqttServer == "") {
|
||||
SerialPrint("E", "MQTT", F("mqttServer empty"));
|
||||
return res;
|
||||
}
|
||||
SerialPrint("I", "MQTT", "connection started to broker No " + String(currentBroker));
|
||||
|
||||
mqttRootDevice = mqttPrefix + "/" + chipId;
|
||||
|
||||
SerialPrint("I", "MQTT", "broker " + mqttServer + ":" + String(mqttPort, DEC));
|
||||
SerialPrint("I", "MQTT", "topic " + mqttRootDevice);
|
||||
setLedStatus(LED_FAST);
|
||||
mqtt.setServer(mqttServer.c_str(), mqttPort);
|
||||
|
||||
if (!mqtt.connected()) {
|
||||
bool connected = false;
|
||||
if (mqttUser != "" && mqttPass != "") {
|
||||
connected = mqtt.connect(chipId.c_str(), mqttUser.c_str(), mqttPass.c_str());
|
||||
SerialPrint("I", F("MQTT"), F("Go to connection with login and password"));
|
||||
} else if (mqttUser == "" && mqttPass == "") {
|
||||
connected = mqtt.connect(chipId.c_str());
|
||||
SerialPrint("I", F("MQTT"), F("Go to connection without login and password"));
|
||||
} else {
|
||||
SerialPrint("E", F("MQTT"), F("✖ Login or password missing"));
|
||||
return res;
|
||||
}
|
||||
|
||||
if (connected) {
|
||||
SerialPrint("I", F("MQTT"), F("✔ connected"));
|
||||
if (currentBroker == 1) jsonWriteStr(configSetupJson, F("warning4"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #8ef584; padding: 10px;'>Подключено к основному брокеру</p></font></div>"));
|
||||
if (currentBroker == 2) jsonWriteStr(configSetupJson, F("warning4"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #8ef584; padding: 10px;'>Подключено к резервному брокеру</p></font></div>"));
|
||||
setLedStatus(LED_OFF);
|
||||
mqttSubscribe();
|
||||
res = true;
|
||||
} else {
|
||||
mqttConnectAttempts++;
|
||||
SerialPrint("E", F("MQTT"), "🡆 Attempt No: " + String(mqttConnectAttempts) + " could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
|
||||
setLedStatus(LED_FAST);
|
||||
jsonWriteStr(configSetupJson, F("warning4"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #fa987a; padding: 10px;'>Не подключено брокеру</p></font></div>"));
|
||||
if (mqttConnectAttempts >= CHANGE_BROKER_AFTER) {
|
||||
mqttConnectAttempts = 0;
|
||||
if (isSecondBrokerSet()) {
|
||||
changeBroker = true;
|
||||
SerialPrint("E", F("MQTT"), "✖ Broker fully missed (" + String(CHANGE_BROKER_AFTER) + " attempts passed), try connect to another one");
|
||||
} else {
|
||||
SerialPrint("E", F("MQTT"), F("Secound broker not seted"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void mqttCallback(char* topic, uint8_t* payload, size_t length) {
|
||||
String topicStr = String(topic);
|
||||
// SerialPrint("I", "=>MQTT", topicStr);
|
||||
String payloadStr;
|
||||
payloadStr.reserve(length + 1);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
payloadStr += (char)payload[i];
|
||||
}
|
||||
|
||||
// SerialPrint("I", "=>MQTT", payloadStr);
|
||||
|
||||
if (payloadStr.startsWith("HELLO")) {
|
||||
SerialPrint("I", F("MQTT"), F("Full update"));
|
||||
publishWidgets();
|
||||
publishState();
|
||||
#ifdef GATE_MODE
|
||||
publishTimes();
|
||||
#endif
|
||||
#ifdef EnableLogging
|
||||
choose_log_date_and_send();
|
||||
#endif
|
||||
}
|
||||
|
||||
else if (topicStr.indexOf("control") != -1) {
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
|
||||
String order;
|
||||
order += key;
|
||||
order += " ";
|
||||
order += payloadStr;
|
||||
order += ",";
|
||||
|
||||
loopCmdAdd(order);
|
||||
|
||||
SerialPrint("I", F("=>MQTT"), "Msg from iotmanager app: " + key + " " + payloadStr);
|
||||
}
|
||||
|
||||
else if (topicStr.indexOf("event") != -1) {
|
||||
if (!jsonReadBool(configSetupJson, "MqttIn")) {
|
||||
return;
|
||||
}
|
||||
if (topicStr.indexOf(chipId) == -1) {
|
||||
String devId = selectFromMarkerToMarker(topicStr, "/", 2);
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
SerialPrint("I", F("=>MQTT"), "Received event from other device: '" + devId + "' " + key + " " + payloadStr);
|
||||
String event = key + " " + payloadStr + ",";
|
||||
eventBuf += event;
|
||||
}
|
||||
}
|
||||
|
||||
else if (topicStr.indexOf("order") != -1) {
|
||||
if (!jsonReadBool(configSetupJson, "MqttIn")) {
|
||||
return;
|
||||
}
|
||||
String devId = selectFromMarkerToMarker(topicStr, "/", 2);
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
SerialPrint("I", F("=>MQTT"), "Received direct order " + key + " " + payloadStr);
|
||||
String order = key + " " + payloadStr + ",";
|
||||
loopCmdAdd(order);
|
||||
SerialPrint("I", "Order add", order);
|
||||
}
|
||||
|
||||
else if (topicStr.indexOf("info") != -1) {
|
||||
if (topicStr.indexOf("scen") != -1) {
|
||||
writeFile(String(DEVICE_SCENARIO_FILE), payloadStr);
|
||||
loadScenario();
|
||||
SerialPrint("I", F("=>MQTT"), F("Scenario received"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean publish(const String& topic, const String& data) {
|
||||
if (mqtt.beginPublish(topic.c_str(), data.length(), false)) {
|
||||
mqtt.print(data);
|
||||
return mqtt.endPublish();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean publishData(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic;
|
||||
if (!publish(path, data)) {
|
||||
SerialPrint("E", F("MQTT"), F("on publish data"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean publishChart(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/status";
|
||||
if (!publish(path, data)) {
|
||||
SerialPrint("E", F("MQTT"), F("on publish chart"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean publishControl(String id, String topic, String state) {
|
||||
String path = mqttPrefix + "/" + id + "/" + topic + "/control";
|
||||
return mqtt.publish(path.c_str(), state.c_str(), false);
|
||||
}
|
||||
|
||||
boolean publishChart_test(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/status";
|
||||
return mqtt.publish(path.c_str(), data.c_str(), false);
|
||||
}
|
||||
|
||||
boolean publishStatus(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/status";
|
||||
String json = "{}";
|
||||
jsonWriteStr(json, "status", data);
|
||||
return mqtt.publish(path.c_str(), json.c_str(), false);
|
||||
}
|
||||
|
||||
boolean publishAnyJsonKey(const String& topic, const String& key, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/status";
|
||||
String json = "{}";
|
||||
jsonWriteStr(json, key, data);
|
||||
return mqtt.publish(path.c_str(), json.c_str(), false);
|
||||
}
|
||||
|
||||
boolean publishEvent(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/event";
|
||||
return mqtt.publish(path.c_str(), data.c_str(), false);
|
||||
}
|
||||
|
||||
boolean publishInfo(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/info";
|
||||
return mqtt.publish(path.c_str(), data.c_str(), false);
|
||||
}
|
||||
|
||||
#ifdef LAYOUT_IN_RAM
|
||||
void publishWidgets() {
|
||||
if (all_widgets != "") {
|
||||
int counter = 0;
|
||||
String line;
|
||||
int psn_1 = 0;
|
||||
int psn_2;
|
||||
do {
|
||||
psn_2 = all_widgets.indexOf("\r\n", psn_1); //\r\n
|
||||
line = all_widgets.substring(psn_1, psn_2);
|
||||
line.replace("\n", "");
|
||||
line.replace("\r\n", "");
|
||||
// jsonWriteStr(line, "id", String(counter));
|
||||
// jsonWriteStr(line, "pageId", String(counter));
|
||||
counter++;
|
||||
sendMQTT("config", line);
|
||||
Serial.println("[V] " + line);
|
||||
psn_1 = psn_2 + 1;
|
||||
} while (psn_2 + 2 < all_widgets.length());
|
||||
getMemoryLoad("I after send all widgets");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef LAYOUT_IN_RAM
|
||||
void publishWidgets() {
|
||||
auto file = seekFile("layout.txt");
|
||||
if (!file) {
|
||||
SerialPrint("E", F("MQTT"), F("no file layout.txt"));
|
||||
return;
|
||||
}
|
||||
while (file.available()) {
|
||||
String payload = file.readStringUntil('\n');
|
||||
SerialPrint("I", F("MQTT"), "widgets: " + payload);
|
||||
publishData("config", payload);
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
#endif
|
||||
|
||||
void publishState() {
|
||||
// берет строку json и ключи превращает в топики а значения колючей в них посылает
|
||||
String str;
|
||||
if (configLiveJson != "{}") {
|
||||
str += configLiveJson;
|
||||
}
|
||||
if (configStoreJson != "{}") {
|
||||
str += "," + configStoreJson;
|
||||
}
|
||||
str.replace("{", "");
|
||||
str.replace("}", "");
|
||||
str.replace("\"", "");
|
||||
str += ",";
|
||||
|
||||
while (str.length() != 0) {
|
||||
String tmp = selectToMarker(str, ",");
|
||||
|
||||
String topic = selectToMarker(tmp, ":");
|
||||
String state = deleteBeforeDelimiter(tmp, ":");
|
||||
|
||||
if (topic != "" && state != "") {
|
||||
if (topic != "timenow") {
|
||||
publishStatus(topic, state);
|
||||
}
|
||||
}
|
||||
str = deleteBeforeDelimiter(str, ",");
|
||||
}
|
||||
}
|
||||
|
||||
const String getStateStr() {
|
||||
switch (mqtt.state()) {
|
||||
case -4:
|
||||
return F("no respond");
|
||||
break;
|
||||
case -3:
|
||||
return F("connection was broken");
|
||||
break;
|
||||
case -2:
|
||||
return F("connection failed");
|
||||
break;
|
||||
case -1:
|
||||
return F("client disconnected");
|
||||
break;
|
||||
case 0:
|
||||
return F("client connected");
|
||||
break;
|
||||
case 1:
|
||||
return F("doesn't support the requested version");
|
||||
break;
|
||||
case 2:
|
||||
return F("rejected the client identifier");
|
||||
break;
|
||||
case 3:
|
||||
return F("unable to accept the connection");
|
||||
break;
|
||||
case 4:
|
||||
return F("wrong username/password");
|
||||
break;
|
||||
case 5:
|
||||
return F("not authorized to connect");
|
||||
break;
|
||||
default:
|
||||
return F("unspecified");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,318 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef MYSENSORS
|
||||
#include "Class/NotAsync.h"
|
||||
#include "ItemsList.h"
|
||||
#include "MySensorsDataParse.h"
|
||||
#include "items/vSensorNode.h"
|
||||
|
||||
//для того что бы выключить оригинальный лог нужно перейти в файл библиотеки MyGatewayTransportSerial.cpp
|
||||
//и заккоментировать строку 36 MY_SERIALDEVICE.print(protocolMyMessage2Serial(message));
|
||||
|
||||
void loopMySensorsExecute() {
|
||||
if (mysensorBuf.length()) {
|
||||
String tmp = selectToMarker(mysensorBuf, ";");
|
||||
|
||||
String nodeId = selectFromMarkerToMarker(tmp, ",", 0); //node-id
|
||||
String childSensorId = selectFromMarkerToMarker(tmp, ",", 1); //child-sensor-id
|
||||
String type = selectFromMarkerToMarker(tmp, ",", 2); //type of var
|
||||
String command = selectFromMarkerToMarker(tmp, ",", 3); //command
|
||||
String value = selectFromMarkerToMarker(tmp, ",", 4); //value
|
||||
|
||||
static bool presentBeenStarted = false;
|
||||
|
||||
String key = nodeId + "-" + childSensorId;
|
||||
static String infoJson = "{}";
|
||||
|
||||
if (childSensorId == "255") {
|
||||
if (command == "3") { //это особое внутреннее сообщение
|
||||
if (type == "11") { //название ноды
|
||||
SerialPrint("I", "MySensor", "Node name: " + value);
|
||||
}
|
||||
if (type == "12") { //версия ноды
|
||||
SerialPrint("I", "MySensor", "Node version: " + value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (command == "0") { //это презентация
|
||||
presentBeenStarted = true;
|
||||
int num;
|
||||
String widget;
|
||||
String descr;
|
||||
sensorType(type.toInt(), num, widget, descr);
|
||||
if (jsonReadBool(configSetupJson, "gateAuto")) {
|
||||
if (!isItemAdded(key)) {
|
||||
addItemAuto(num, key, widget, descr);
|
||||
descr.replace("#", " ");
|
||||
SerialPrint("I", "MySensor", "Add new item: " + key + ": " + descr);
|
||||
} else {
|
||||
descr.replace("#", " ");
|
||||
SerialPrint("I", "MySensor", "Item already exist: " + key + ": " + descr);
|
||||
}
|
||||
} else {
|
||||
descr.replace("#", " ");
|
||||
SerialPrint("I", "MySensor", "Presentation: " + key + ": " + descr);
|
||||
}
|
||||
}
|
||||
if (command == "1") { //это данные
|
||||
if (value != "") {
|
||||
if (presentBeenStarted) {
|
||||
presentBeenStarted = false;
|
||||
SerialPrint("I", "MySensor", "!!!Presentation of node: " + nodeId + " completed successfully!!!");
|
||||
myNotAsyncActions->make(do_deviceInit);
|
||||
}
|
||||
if (mySensorNode != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorNode->size(); i++) {
|
||||
mySensorNode->at(i).onChange(value, key); //вызываем поочередно все экземпляры, там где подойдет там и выполнится
|
||||
}
|
||||
}
|
||||
SerialPrint("I", "MySensor", "node: " + nodeId + ", sensor: " + childSensorId + ", command: " + command + ", type: " + type + ", val: " + value);
|
||||
}
|
||||
}
|
||||
if (command == "2") { //это запрос значения переменной
|
||||
SerialPrint("I", "MySensor", "Request a variable value");
|
||||
}
|
||||
}
|
||||
|
||||
mysensorBuf = deleteBeforeDelimiter(mysensorBuf, ";");
|
||||
}
|
||||
}
|
||||
|
||||
void sensorType(int index, int &num, String &widget, String &descr) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
descr = F("Door#and#window#sensors");
|
||||
widget = F("alarm");
|
||||
num = 1;
|
||||
break;
|
||||
case 1:
|
||||
descr = F("Motion#sensors");
|
||||
widget = F("alarm");
|
||||
num = 1;
|
||||
break;
|
||||
case 2:
|
||||
descr = F("Smoke#sensor");
|
||||
widget = F("fillgauge");
|
||||
num = 1;
|
||||
break;
|
||||
case 3:
|
||||
descr = F("Binary#device#(on/off)");
|
||||
widget = F("toggleBtn");
|
||||
num = 2;
|
||||
break;
|
||||
case 4:
|
||||
descr = F("Dimmable#device");
|
||||
//to do
|
||||
//widget = F("range");
|
||||
//num = 2;
|
||||
break;
|
||||
case 5:
|
||||
descr = F("Window#covers#or#shades");
|
||||
//to do
|
||||
//widget = F("range");
|
||||
//num = 2;
|
||||
break;
|
||||
case 6:
|
||||
descr = F("Temperature#sensor");
|
||||
widget = F("anydataTemp");
|
||||
num = 1;
|
||||
break;
|
||||
case 7:
|
||||
descr = F("Humidity#sensor");
|
||||
widget = F("anydataHum");
|
||||
num = 1;
|
||||
break;
|
||||
case 8:
|
||||
descr = F("Pressure#sensor");
|
||||
widget = F("anydataPress");
|
||||
num = 1;
|
||||
break;
|
||||
case 9:
|
||||
descr = F("Wind#sensor");
|
||||
widget = F("anydataTime");
|
||||
num = 1;
|
||||
break;
|
||||
case 10:
|
||||
descr = F("Rain#sensor");
|
||||
widget = F("anydataTime");
|
||||
num = 1;
|
||||
break;
|
||||
case 11:
|
||||
descr = F("UV#sensor");
|
||||
widget = F("anydataTime");
|
||||
num = 1;
|
||||
break;
|
||||
case 12:
|
||||
descr = F("Weight#sensor");
|
||||
widget = F("anydataTime");
|
||||
num = 1;
|
||||
break;
|
||||
case 13:
|
||||
descr = F("Power#measuring#device");
|
||||
widget = F("anydataWtt");
|
||||
num = 1;
|
||||
break;
|
||||
case 14:
|
||||
descr = F("Heater#device");
|
||||
widget = F("anydataTemp");
|
||||
num = 1;
|
||||
break;
|
||||
case 15:
|
||||
descr = F("Distance#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 16:
|
||||
descr = F("Light#sensor");
|
||||
widget = F("anydataTime");
|
||||
num = 1;
|
||||
break;
|
||||
case 17:
|
||||
descr = F("Arduino#node#device");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 18:
|
||||
descr = F("Arduino#repeating#node#device");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 19:
|
||||
descr = F("Lock#device");
|
||||
widget = F("toggleBtn");
|
||||
num = 2;
|
||||
break;
|
||||
case 20:
|
||||
descr = F("Ir#sender/receiver#device");
|
||||
widget = F("toggleBtn");
|
||||
num = 2;
|
||||
break;
|
||||
case 21:
|
||||
descr = F("Water#meter");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 22:
|
||||
descr = F("Air#quality#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 23:
|
||||
descr = F("Custom#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 24:
|
||||
descr = F("Dust#level#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 25:
|
||||
descr = F("Scene#controller#device");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 26:
|
||||
descr = F("RGB#light");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 27:
|
||||
descr = F("RGBW#light#(with#separate#white#component)");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 28:
|
||||
descr = F("Color#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 29:
|
||||
descr = F("Thermostat/HVAC#device");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 30:
|
||||
descr = F("Multimeter#device");
|
||||
widget = F("anydataVlt");
|
||||
num = 1;
|
||||
break;
|
||||
case 31:
|
||||
descr = F("Sprinkler#device");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 32:
|
||||
descr = F("Water#leak#sensor");
|
||||
widget = F("alarm");
|
||||
num = 1;
|
||||
break;
|
||||
case 33:
|
||||
descr = F("Sound#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 34:
|
||||
descr = F("Vibration#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 35:
|
||||
descr = F("Moisture#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 36:
|
||||
descr = F("LCD#text#device");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 37:
|
||||
descr = F("Gas#meter");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 38:
|
||||
descr = F("GPS#Sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
case 39:
|
||||
descr = F("Water#quality#sensor");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
default:
|
||||
descr = F("Unknown");
|
||||
widget = F("anydata");
|
||||
num = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//отличный пример разбора строки
|
||||
//void test(char *inputString) {
|
||||
// char *str, *p;
|
||||
// uint8_t index = 0;
|
||||
// for (str = strtok_r(inputString, ";", &p);
|
||||
// str && index < 5;
|
||||
// str = strtok_r(NULL, ";", &p), index++) {
|
||||
// switch (index) {
|
||||
// case 0:
|
||||
// Serial.println(str);
|
||||
// break;
|
||||
// case 1:
|
||||
// Serial.println(str);
|
||||
// break;
|
||||
// case 2:
|
||||
// Serial.println(str);
|
||||
// break;
|
||||
// case 3:
|
||||
// Serial.println(str);
|
||||
// break;
|
||||
// case 4:
|
||||
// Serial.println(str);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -1,45 +0,0 @@
|
||||
#include "MySensorsDataRead.h"
|
||||
#ifdef MYSENSORS
|
||||
|
||||
void receive(const MyMessage &message) {
|
||||
String inMsg = String(message.getSender()) + "," + //node-id
|
||||
String(message.getSensor()) + "," + //child-sensor-id
|
||||
String(message.getType()) + "," + //type of var
|
||||
String(message.getCommand()) + "," + //command
|
||||
parseToString(message) + ";"; //value
|
||||
|
||||
mysensorBuf += inMsg;
|
||||
}
|
||||
|
||||
String parseToString(const MyMessage &message) {
|
||||
String value = "error";
|
||||
switch (message.getPayloadType()) {
|
||||
case 0: //Payload type is string
|
||||
value = message.getString();
|
||||
return value;
|
||||
case 1: //Payload type is byte
|
||||
value = String(message.getByte());
|
||||
return value;
|
||||
case 2: //Payload type is INT16
|
||||
value = String(message.getInt());
|
||||
return value;
|
||||
case 3: //Payload type is UINT16
|
||||
value = String(message.getUInt());
|
||||
return value;
|
||||
case 4: //Payload type is INT32
|
||||
value = String(message.getInt());
|
||||
return value;
|
||||
case 5: //Payload type is UINT32
|
||||
value = String(message.getUInt());
|
||||
return value;
|
||||
case 6: //Payload type is binary
|
||||
value = String(message.getBool());
|
||||
return value;
|
||||
case 7: //Payload type is float32
|
||||
value = String(message.getFloat());
|
||||
return value;
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,110 +0,0 @@
|
||||
#include "RemoteOrdersUdp.h"
|
||||
#include <Arduino.h>
|
||||
#include "Global.h"
|
||||
#include "Class/NotAsync.h"
|
||||
#include "Init.h"
|
||||
|
||||
#ifdef UDP_ENABLED
|
||||
AsyncUDP asyncUdp;
|
||||
|
||||
void asyncUdpInit() {
|
||||
|
||||
if (!jsonReadBool(configSetupJson, "snaUdp")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (asyncUdp.listenMulticast(IPAddress(239, 255, 255, 255), 4210)) {
|
||||
asyncUdp.onPacket([](AsyncUDPPacket packet) {
|
||||
|
||||
//Serial.print("UDP Packet Type: ");
|
||||
//Serial.println(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
|
||||
//
|
||||
//Serial.print("From: ");
|
||||
//Serial.print(packet.remoteIP());
|
||||
//Serial.print(":");
|
||||
//Serial.println(packet.remotePort());
|
||||
//
|
||||
//Serial.print("To: ");
|
||||
//Serial.print(packet.localIP());
|
||||
//Serial.print(":");
|
||||
//Serial.println(packet.localPort());
|
||||
//Serial.print(", Length: ");
|
||||
//Serial.print(packet.length());
|
||||
//
|
||||
//Serial.print(", Data: ");
|
||||
//Serial.write(packet.data(), packet.length());
|
||||
|
||||
String data = uint8tToString(packet.data(), packet.length());
|
||||
Serial.print("[i] [udp] Packet received: '");
|
||||
Serial.print(data);
|
||||
|
||||
if (udpPacketValidation(data)) {
|
||||
Serial.println("', packet valid");
|
||||
udpPacketParse(data);
|
||||
}
|
||||
|
||||
|
||||
//else {
|
||||
// //Serial.println("', Packet invalid");
|
||||
//}
|
||||
//reply to the client
|
||||
//String ip = WiFi.localIP().toString();
|
||||
//asyncUdp.broadcastTo(ip.c_str(), packet.remotePort());
|
||||
//packet.printf(ip.c_str(), packet.length());
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
myNotAsyncActions->add(
|
||||
do_sendScenUDP, [&](void*) {
|
||||
|
||||
String scen = "iotm;scen:";
|
||||
scen += readFile(String(DEVICE_SCENARIO_FILE), 2048);
|
||||
|
||||
asyncUdp.broadcastTo(scen.c_str(), 4210);
|
||||
|
||||
},
|
||||
nullptr);
|
||||
|
||||
//ts.add(
|
||||
//UDP, 10000, [&](void*) {
|
||||
//Serial.println("sended");
|
||||
//asyncUdp.broadcastTo("iotm;Anyone here?", 4210);
|
||||
//asyncUdp.broadcast("test");
|
||||
//asyncUdp.print("Hello Server!");
|
||||
//},
|
||||
//nullptr, true);
|
||||
|
||||
SerialPrint("I", F("UDP"), F("Udp Init"));
|
||||
|
||||
}
|
||||
|
||||
bool udpPacketValidation(String& data) {
|
||||
if (data.indexOf("iotm;") != -1) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void udpPacketParse(String& data) {
|
||||
if (data.indexOf("scen:") != -1) {
|
||||
data = deleteBeforeDelimiter(data, ":");
|
||||
writeFile(String(DEVICE_SCENARIO_FILE), data);
|
||||
loadScenario();
|
||||
}
|
||||
else if (data.indexOf("event:") != -1) {
|
||||
data = deleteBeforeDelimiter(data, ":");
|
||||
eventBuf += data;
|
||||
}
|
||||
}
|
||||
|
||||
String uint8tToString(uint8_t* data, size_t len) {
|
||||
String ret;
|
||||
while (len--) {
|
||||
ret += (char)*data++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
63
src/SSDP.cpp
63
src/SSDP.cpp
@@ -1,63 +0,0 @@
|
||||
#include "SSDP.h"
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
#ifdef SSDP_ENABLED
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266SSDP.h>
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
#include <ESP32SSDP.h>
|
||||
#endif
|
||||
|
||||
String xmlNode(String tags, String data);
|
||||
|
||||
String decToHex(uint32_t decValue, byte desiredStringLength);
|
||||
|
||||
void SsdpInit() {
|
||||
server.on("/description.xml", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
String ssdpSend = F("<root xmlns=\"urn:schemas-upnp-org:device-1-0\">");
|
||||
String ssdpHeder = xmlNode(F("major"), "1");
|
||||
ssdpHeder += xmlNode(F("minor"), "0");
|
||||
ssdpHeder = xmlNode(F("specVersion"), ssdpHeder);
|
||||
ssdpHeder += xmlNode(F("URLBase"), "http://" + WiFi.localIP().toString());
|
||||
String ssdpDescription = xmlNode(F("deviceType"), F("upnp:rootdevice"));
|
||||
ssdpDescription += xmlNode(F("friendlyName"), jsonReadStr(configSetupJson, F("name")));
|
||||
ssdpDescription += xmlNode(F("presentationURL"), "/");
|
||||
ssdpDescription += xmlNode(F("serialNumber"), getChipId());
|
||||
#ifdef ESP8266
|
||||
ssdpDescription += xmlNode(F("modelName"), F("ESP8266"));
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
ssdpDescription += xmlNode(F("modelName"), F("ESP32"));
|
||||
#endif
|
||||
ssdpDescription += xmlNode(F("modelNumber"), getChipId());
|
||||
ssdpDescription += xmlNode(F("modelURL"), F("https://github.com/IoTManagerProject/IoTManager/wiki"));
|
||||
ssdpDescription += xmlNode(F("manufacturer"), F("Borisenko Dmitry"));
|
||||
ssdpDescription += xmlNode(F("manufacturerURL"), F("https://github.com/IoTManagerProject/IoTManager"));
|
||||
ssdpDescription += xmlNode(F("UDN"), "uuid:38323636-4558-4dda-9188-cda0e6" + decToHex(ESP_getChipId(), 6));
|
||||
ssdpDescription = xmlNode("device", ssdpDescription);
|
||||
ssdpHeder += ssdpDescription;
|
||||
ssdpSend += ssdpHeder;
|
||||
ssdpSend += "</root>";
|
||||
Serial.println("->!!!SSDP Get request received");
|
||||
request->send(200, "text/xml", ssdpSend);
|
||||
});
|
||||
//Если версия 2.0.0 закаментируйте следующую строчку
|
||||
SSDP.setDeviceType(F("upnp:rootdevice"));
|
||||
SSDP.setSchemaURL(F("description.xml"));
|
||||
SSDP.begin();
|
||||
SerialPrint("I", F("SSDP"), F("Ssdp Init"));
|
||||
}
|
||||
|
||||
String xmlNode(String tags, String data) {
|
||||
String temp = "<" + tags + ">" + data + "</" + tags + ">";
|
||||
return temp;
|
||||
}
|
||||
|
||||
String decToHex(uint32_t decValue, byte desiredStringLength) {
|
||||
String hexString = String(decValue, HEX);
|
||||
while (hexString.length() < desiredStringLength) hexString = "0" + hexString;
|
||||
return hexString;
|
||||
}
|
||||
#endif
|
||||
@@ -1,40 +0,0 @@
|
||||
#include "Servo/Servos.h"
|
||||
|
||||
Servos myServo;
|
||||
|
||||
Servos::Servos(){};
|
||||
|
||||
Servo *Servos::create(uint8_t num, uint8_t pin) {
|
||||
// Ищем среди ранее созданных
|
||||
for (size_t i = 0; i < _items.size(); i++) {
|
||||
auto item = _items.at(i);
|
||||
if (item.num == num) {
|
||||
if (item.pin != pin) {
|
||||
item.obj->attach(pin);
|
||||
item.pin = pin;
|
||||
};
|
||||
return item.obj;
|
||||
}
|
||||
}
|
||||
// Добавляем новый
|
||||
Servo_t newItem{num, pin};
|
||||
newItem.obj = new Servo();
|
||||
newItem.obj->attach(pin);
|
||||
_items.push_back(newItem);
|
||||
return newItem.obj;
|
||||
}
|
||||
|
||||
Servo *Servos::get(uint8_t num) {
|
||||
// Ищем среди ранее созданных
|
||||
for (size_t i = 0; i < _items.size(); i++) {
|
||||
auto item = _items.at(i);
|
||||
if (item.num == num) {
|
||||
return item.obj;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t Servos::count() {
|
||||
return _items.size();
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
#include "SetupESP.h"
|
||||
|
||||
void setupESP() {
|
||||
File file1 = seekFile("/setup.json"); //читаем первый файл из памяти стримом
|
||||
File file2 = FileFS.open("/setup.json.tmp", "w"); //открыл второй файл для записи
|
||||
file2.println("[");
|
||||
|
||||
// WriteBufferingStream bfile2(file2, 64); //записываем стрим во второй файл для записи
|
||||
// ReadBufferingStream bfile1{file1, 64}; //стримим первый файл
|
||||
|
||||
DynamicJsonDocument doc(1024);
|
||||
Serial.println("during " + prettyBytes(ESP.getFreeHeap()));
|
||||
int i = 0;
|
||||
|
||||
file1.find("[");
|
||||
|
||||
do {
|
||||
i++;
|
||||
|
||||
deserializeJson(doc, file1);
|
||||
doc["web"]["order"] = i;
|
||||
serializeJsonPretty(doc, file2);
|
||||
|
||||
file2.println(",");
|
||||
|
||||
// DeserializationError error =
|
||||
// if (error) {
|
||||
// Serial.print("json error: ");
|
||||
// Serial.println(error.f_str());
|
||||
// }
|
||||
|
||||
Serial.println(
|
||||
String(i) + ") " +
|
||||
doc["type"].as<String>() + " " +
|
||||
doc["set"]["gpio"].as<String>() + " " +
|
||||
doc["web"]["order"].as<String>());
|
||||
|
||||
} while (file1.findUntil(",", "]"));
|
||||
|
||||
file2.println("]");
|
||||
|
||||
file2.close();
|
||||
|
||||
// if (cutFile("/setup.json.tmp", "/setup.json")) Serial.println("file overwrited");
|
||||
|
||||
Serial.println("-------------");
|
||||
Serial.println(readFile("/setup.json.tmp", 20000));
|
||||
Serial.println("-------------");
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableUart
|
||||
#include "BufferExecute.h"
|
||||
#include "Global.h"
|
||||
#include "SoftUART.h"
|
||||
|
||||
#ifdef ESP8266
|
||||
SoftwareSerial* myUART = nullptr;
|
||||
#else
|
||||
HardwareSerial* myUART = nullptr;
|
||||
#endif
|
||||
|
||||
void uartInit() {
|
||||
if (!jsonReadBool(configSetupJson, "uart")) {
|
||||
return;
|
||||
}
|
||||
if (!myUART) {
|
||||
#ifdef ESP8266
|
||||
myUART = new SoftwareSerial(jsonReadInt(configSetupJson, "uartTX"), jsonReadInt(configSetupJson, "uartRX"));
|
||||
myUART->begin(jsonReadInt(configSetupJson, "uartS"));
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
myUART = new HardwareSerial(2);
|
||||
long speed = jsonReadInt(configSetupJson, "uartS");
|
||||
int rxPin = jsonReadInt(configSetupJson, "uartRX");
|
||||
int txPin = jsonReadInt(configSetupJson, "uartTX");
|
||||
myUART->begin(speed, SERIAL_8N1, rxPin, txPin);
|
||||
#endif
|
||||
}
|
||||
SerialPrint("I", F("UART"), F("UART Init"));
|
||||
}
|
||||
|
||||
void uartHandle() {
|
||||
if (myUART) {
|
||||
if (!jsonReadBool(configSetupJson, "uart")) {
|
||||
return;
|
||||
}
|
||||
static String incStr;
|
||||
if (myUART->available()) {
|
||||
char inc;
|
||||
inc = myUART->read();
|
||||
incStr += inc;
|
||||
if (inc == '\n') {
|
||||
parse(incStr);
|
||||
incStr = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void parse(String& incStr) {
|
||||
incStr.replace("\r\n", "");
|
||||
incStr.replace("\r", "");
|
||||
incStr.replace("\n", "");
|
||||
if (incStr.indexOf("set") != -1) {
|
||||
incStr = deleteBeforeDelimiter(incStr, " ");
|
||||
loopCmdAdd(incStr);
|
||||
SerialPrint("I", "=>UART", incStr);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
124
src/Telegram.cpp
124
src/Telegram.cpp
@@ -1,124 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableTelegram
|
||||
#include "BufferExecute.h"
|
||||
#include "Telegram.h"
|
||||
CTBot* myBot{nullptr};
|
||||
|
||||
void telegramInit() {
|
||||
if (isEnableTelegramd()) {
|
||||
telegramInitBeen = true;
|
||||
sCmd.addCommand("telegram", sendTelegramMsg);
|
||||
String token = jsonReadStr(configSetupJson, "telegramApi");
|
||||
if (!myBot) {
|
||||
myBot = new CTBot();
|
||||
}
|
||||
#ifdef ESP32
|
||||
myBot->useDNS(true);
|
||||
#endif
|
||||
myBot->setTelegramToken(token);
|
||||
myBot->enableUTF8Encoding(true);
|
||||
if (myBot->testConnection()) {
|
||||
SerialPrint("I", F("Telegram"), F("Connected"));
|
||||
} else {
|
||||
SerialPrint("E", F("Telegram"), F("Not connected"));
|
||||
}
|
||||
SerialPrint("I", F("Telegram"), F("Telegram Init"));
|
||||
}
|
||||
}
|
||||
|
||||
void handleTelegram() {
|
||||
if (telegramInitBeen) {
|
||||
if (isEnableTelegramd()) {
|
||||
if (isTelegramInputOn()) {
|
||||
TBMessage msg;
|
||||
static unsigned long prevMillis;
|
||||
unsigned long currentMillis = millis();
|
||||
unsigned long difference = currentMillis - prevMillis;
|
||||
if (difference >= 10000) {
|
||||
prevMillis = millis();
|
||||
if (myBot->getNewMessage(msg)) {
|
||||
SerialPrint("->", F("Telegram"), "chat ID: " + String(msg.sender.id) + ", msg: " + String(msg.text));
|
||||
if (jsonReadBool(configSetupJson, "autos")) {
|
||||
jsonWriteInt(configSetupJson, "chatId", msg.sender.id);
|
||||
saveConfig();
|
||||
}
|
||||
telegramMsgParse(String(msg.text));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void telegramMsgParse(String msg) {
|
||||
if (msg.indexOf("set") != -1) {
|
||||
msg = deleteBeforeDelimiter(msg, "_");
|
||||
msg.replace("_", " ");
|
||||
loopCmdAdd(String(msg) + ",");
|
||||
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), "order done");
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + String(msg));
|
||||
} else if (msg.indexOf("get") != -1) {
|
||||
msg = deleteBeforeDelimiter(msg, "_");
|
||||
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), getValue(msg)); //jsonReadStr(configLiveJson , msg));
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + String(msg));
|
||||
} else if (msg.indexOf("all") != -1) {
|
||||
String list = returnListOfParams();
|
||||
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), list);
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + "\n" + list);
|
||||
} else {
|
||||
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), "ID: " + chipId + ", Name: " + jsonReadStr(configSetupJson, F("name")));
|
||||
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), F("Wrong order, use /all to get all values, /get_id to get value, or /set_id_value to set value"));
|
||||
}
|
||||
}
|
||||
|
||||
void sendTelegramMsg() {
|
||||
String sabject = sCmd.next();
|
||||
String msg = sCmd.next();
|
||||
if (sabject == "often") {
|
||||
msg.replace("#", " ");
|
||||
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg);
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg);
|
||||
} else {
|
||||
String prevMsg = jsonReadStr(telegramMsgJson, sabject);
|
||||
if (prevMsg != msg) {
|
||||
jsonWriteStr(telegramMsgJson, sabject, msg);
|
||||
msg.replace("#", " ");
|
||||
sabject.replace("#", " ");
|
||||
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg);
|
||||
SerialPrint("<-", F("Telegram"), "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isEnableTelegramd() {
|
||||
return jsonReadBool(configSetupJson, "telegonof");
|
||||
}
|
||||
|
||||
bool isTelegramInputOn() {
|
||||
return jsonReadBool(configSetupJson, "teleginput");
|
||||
}
|
||||
|
||||
String returnListOfParams() {
|
||||
String cmdStr = readFile(DEVICE_CONFIG_FILE, 4096);
|
||||
cmdStr += "\r\n";
|
||||
cmdStr.replace("\r\n", "\n");
|
||||
cmdStr.replace("\r", "\n");
|
||||
int count = 0;
|
||||
String out;
|
||||
while (cmdStr.length()) {
|
||||
String buf = selectToMarker(cmdStr, "\n");
|
||||
count++;
|
||||
if (count > 1) {
|
||||
String id = selectFromMarkerToMarker(buf, ";", 2);
|
||||
String value = getValue(id); //jsonReadStr(configLiveJson , id);
|
||||
String page = selectFromMarkerToMarker(buf, ";", 4);
|
||||
page.replace("#", " ");
|
||||
String name = selectFromMarkerToMarker(buf, ";", 5);
|
||||
name.replace("#", " ");
|
||||
out += page + " " + " " + name + " " + value + "\n";
|
||||
}
|
||||
cmdStr = deleteBeforeDelimiter(cmdStr, "\n");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
#endif
|
||||
@@ -1,59 +0,0 @@
|
||||
#include "Tests.h"
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/TCircularBuffer.h"
|
||||
#include "Global.h"
|
||||
#include "ItemsList.h"
|
||||
#include "Macro.h"
|
||||
#include "MySensorsDataParse.h"
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
void testsPerform() {
|
||||
Serial.println("====some tests section====");
|
||||
//ТЕСТ КОЛЬЦЕВОГО БУФЕРА=============================================================================
|
||||
myTCircularBuffer = new TCircularBuffer<char*, 1024>;
|
||||
|
||||
char* buf = "text";
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
myTCircularBuffer->push(buf);
|
||||
}
|
||||
|
||||
char* item;
|
||||
|
||||
while (myTCircularBuffer->pop(item)) {
|
||||
Serial.println(item);
|
||||
}
|
||||
//===================================================================================================
|
||||
// String str = "0;1;2;3;4";
|
||||
// char* mychar = new char[str.length() + 1];
|
||||
// strcpy(mychar, str.c_str());
|
||||
// test(mychar);
|
||||
//===========================================================================
|
||||
// String myJson;
|
||||
// const int capacity = JSON_ARRAY_SIZE(2) + 3 * JSON_OBJECT_SIZE(3);
|
||||
// StaticJsonBuffer<capacity> jb;
|
||||
// JsonArray& arr = jb.createArray();
|
||||
// JsonObject& obj1 = jb.createObject();
|
||||
// obj1["test1"] = 1;
|
||||
// obj1["test2"] = 2;
|
||||
// obj1["test3"] = 3;
|
||||
// arr.add(obj1);
|
||||
// arr.printTo(myJson);
|
||||
// Serial.println(myJson);
|
||||
//
|
||||
//
|
||||
//
|
||||
//===========================================================================
|
||||
// Serial.println(isDigitDotCommaStr("-12552.5555"));
|
||||
// String str = "Geeks for Geeks ";
|
||||
// Serial.println(itemsCount2(str, " "));
|
||||
//
|
||||
Serial.println("==========end============");
|
||||
}
|
||||
|
||||
void testLoop() {
|
||||
// char* item;
|
||||
// myCircularBuffer->pop(item);
|
||||
// Serial.println(item);
|
||||
}
|
||||
@@ -1,161 +0,0 @@
|
||||
#include "Class/NotAsync.h"
|
||||
#include "FileSystem.h"
|
||||
#include "Upgrade.h"
|
||||
#ifdef ESP8266
|
||||
#include "ESP8266.h"
|
||||
#else
|
||||
#include <HTTPUpdate.h>
|
||||
#endif
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
void upgradeInit() {
|
||||
myNotAsyncActions->add(
|
||||
do_UPGRADE, [&](void*) {
|
||||
upgrade_firmware(3);
|
||||
},
|
||||
nullptr);
|
||||
|
||||
myNotAsyncActions->add(
|
||||
do_GETLASTVERSION, [&](void*) {
|
||||
getLastVersion();
|
||||
},
|
||||
nullptr);
|
||||
|
||||
if (isNetworkActive()) {
|
||||
getLastVersion();
|
||||
if (lastVersion > 0) {
|
||||
SerialPrint("I", F("Update"), "available version: " + String(lastVersion));
|
||||
if (lastVersion > FIRMWARE_VERSION) {
|
||||
jsonWriteStr(configSetupJson, "warning2", F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Вышла новая версия прошивки, нажмите <b>обновить прошивку</b></p></font></div>"));
|
||||
}
|
||||
}
|
||||
};
|
||||
SerialPrint("I", F("Update"), F("Updater Init"));
|
||||
}
|
||||
|
||||
void getLastVersion() {
|
||||
if ((WiFi.status() == WL_CONNECTED)) {
|
||||
String tmp;
|
||||
#ifdef esp8266_4mb
|
||||
tmp = getURL(serverIP + F("/projects/iotmanager/esp8266/esp8266ver/esp8266ver.txt"));
|
||||
#endif
|
||||
#ifdef esp32_4mb
|
||||
tmp = getURL(serverIP + F("/projects/iotmanager/esp32/esp32ver/esp32ver.txt"));
|
||||
#endif
|
||||
#ifdef esp8266_mysensors_4mb
|
||||
tmp = getURL(serverIP + F("/projects/iotmanager/esp8266ms/esp8266ver/esp8266ver.txt"));
|
||||
#endif
|
||||
#ifdef esp32_mysensors_4mb
|
||||
tmp = getURL(serverIP + F("/projects/iotmanager/esp32ms/esp32ver/esp32ver.txt"));
|
||||
#endif
|
||||
if (tmp == "error") {
|
||||
lastVersion = -1;
|
||||
} else {
|
||||
lastVersion = tmp.toInt();
|
||||
}
|
||||
} else {
|
||||
lastVersion = -2;
|
||||
}
|
||||
jsonWriteInt(configSetupJson, "last_version", lastVersion);
|
||||
}
|
||||
|
||||
void upgrade_firmware(int type) {
|
||||
String scenario_ForUpdate;
|
||||
String devconfig_ForUpdate;
|
||||
String configSetup_ForUpdate;
|
||||
|
||||
scenario_ForUpdate = readFile(String(DEVICE_SCENARIO_FILE), 4096);
|
||||
devconfig_ForUpdate = readFile(String(DEVICE_CONFIG_FILE), 4096);
|
||||
configSetup_ForUpdate = configSetupJson;
|
||||
|
||||
if (type == 1) { //only build
|
||||
if (upgradeBuild()) restartEsp();
|
||||
}
|
||||
|
||||
else if (type == 2) { //only spiffs
|
||||
if (upgradeFS()) {
|
||||
writeFile(String(DEVICE_SCENARIO_FILE), scenario_ForUpdate);
|
||||
writeFile(String(DEVICE_CONFIG_FILE), devconfig_ForUpdate);
|
||||
writeFile("config.json", configSetup_ForUpdate);
|
||||
restartEsp();
|
||||
}
|
||||
}
|
||||
|
||||
else if (type == 3) { //spiffs and build
|
||||
if (upgradeFS()) {
|
||||
writeFile(String(DEVICE_SCENARIO_FILE), scenario_ForUpdate);
|
||||
writeFile(String(DEVICE_CONFIG_FILE), devconfig_ForUpdate);
|
||||
writeFile("config.json", configSetup_ForUpdate);
|
||||
saveConfig();
|
||||
if (upgradeBuild()) {
|
||||
restartEsp();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool upgradeFS() {
|
||||
bool ret = false;
|
||||
#ifndef esp8266_1mb
|
||||
WiFiClient wifiClient;
|
||||
Serial.printf("Start upgrade %s, please wait...", FS_NAME);
|
||||
#ifdef esp8266_4mb
|
||||
ESPhttpUpdate.rebootOnUpdate(false);
|
||||
t_httpUpdate_return retFS = ESPhttpUpdate.updateFS(wifiClient, serverIP + F("/projects/iotmanager/esp8266/littlefs/littlefs.bin"));
|
||||
#endif
|
||||
#ifdef esp32_4mb
|
||||
httpUpdate.rebootOnUpdate(false);
|
||||
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp32/littlefs/spiffs.bin"));
|
||||
#endif
|
||||
#ifdef esp8266_mysensors_4mb
|
||||
ESPhttpUpdate.rebootOnUpdate(false);
|
||||
t_httpUpdate_return retFS = ESPhttpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp8266ms/littlefs/littlefs.bin"));
|
||||
#endif
|
||||
#ifdef esp32_mysensors_4mb
|
||||
httpUpdate.rebootOnUpdate(false);
|
||||
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp32ms/littlefs/spiffs.bin"));
|
||||
#endif
|
||||
if (retFS == HTTP_UPDATE_OK) { //если FS обновилась успешно
|
||||
SerialPrint("I", F("Update"), F("FS upgrade done!"));
|
||||
ret = true;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool upgradeBuild() {
|
||||
bool ret = false;
|
||||
#ifndef esp8266_1mb
|
||||
WiFiClient wifiClient;
|
||||
Serial.println(F("Start upgrade BUILD, please wait..."));
|
||||
#ifdef esp8266_4mb
|
||||
ESPhttpUpdate.rebootOnUpdate(false);
|
||||
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp8266/firmware/firmware.bin"));
|
||||
#endif
|
||||
#ifdef esp32_4mb
|
||||
httpUpdate.rebootOnUpdate(false);
|
||||
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp32/firmware/firmware.bin"));
|
||||
#endif
|
||||
#ifdef esp8266_mysensors_4mb
|
||||
ESPhttpUpdate.rebootOnUpdate(false);
|
||||
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp8266ms/firmware/firmware.bin"));
|
||||
#endif
|
||||
#ifdef esp32_mysensors_4mb
|
||||
httpUpdate.rebootOnUpdate(false);
|
||||
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp32ms/firmware/firmware.bin"));
|
||||
#endif
|
||||
|
||||
if (retBuild == HTTP_UPDATE_OK) { //если BUILD обновился успешно
|
||||
SerialPrint("I", F("Update"), F("BUILD upgrade done!"));
|
||||
ret = true;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
void restartEsp() {
|
||||
Serial.println(F("Restart ESP...."));
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
@@ -1,208 +0,0 @@
|
||||
#include "Utils/FileUtils.h"
|
||||
|
||||
#include "FileSystem.h"
|
||||
#include "Utils/StringUtils.h"
|
||||
#include "Utils\SerialPrint.h"
|
||||
|
||||
const String filepath(const String& filename) {
|
||||
return filename.startsWith("/") ? filename : "/" + filename;
|
||||
}
|
||||
|
||||
bool fileSystemInit() {
|
||||
if (!FileFS.begin()) {
|
||||
SerialPrint("E", F("FS"), F("FS Init ERROR, may be FS was not flashed"));
|
||||
return false;
|
||||
}
|
||||
SerialPrint("I", F("FS"), F("FS Init completed"));
|
||||
return true;
|
||||
}
|
||||
|
||||
void removeFile(const String& filename) {
|
||||
String path = filepath(filename);
|
||||
if (FileFS.exists(path)) {
|
||||
if (!FileFS.remove(path)) {
|
||||
SerialPrint("I", "Files", "remove " + path);
|
||||
}
|
||||
} else {
|
||||
SerialPrint("E", "Files", "not exist" + path);
|
||||
}
|
||||
}
|
||||
|
||||
File seekFile(const String& filename, size_t position) {
|
||||
String path = filepath(filename);
|
||||
auto file = FileFS.open(path, "r");
|
||||
if (!file) {
|
||||
SerialPrint("[E]", "Files", "open " + path);
|
||||
}
|
||||
// поставим курсор в начало файла
|
||||
file.seek(position, SeekSet);
|
||||
return file;
|
||||
}
|
||||
|
||||
const String readFileString(const String& filename, const String& to_find) {
|
||||
String path = filepath(filename);
|
||||
String res = "failed";
|
||||
auto file = FileFS.open(path, "r");
|
||||
if (!file) {
|
||||
return "failed";
|
||||
}
|
||||
if (file.find(to_find.c_str())) {
|
||||
res = file.readStringUntil('\n');
|
||||
}
|
||||
file.close();
|
||||
return res;
|
||||
}
|
||||
|
||||
const String addFileLn(const String& filename, const String& str) {
|
||||
String path = filepath(filename);
|
||||
auto file = FileFS.open(path, "a");
|
||||
if (!file) {
|
||||
return "failed";
|
||||
}
|
||||
file.println(str);
|
||||
file.close();
|
||||
return "sucсess";
|
||||
}
|
||||
|
||||
const String addFile(const String& filename, const String& str) {
|
||||
String path = filepath(filename);
|
||||
auto file = FileFS.open(path, "a");
|
||||
if (!file) {
|
||||
return "failed";
|
||||
}
|
||||
file.print(str);
|
||||
file.close();
|
||||
return "sucсess";
|
||||
}
|
||||
|
||||
bool copyFile(const String& src, const String& dst, bool overwrite) {
|
||||
String srcPath = filepath(src);
|
||||
String dstPath = filepath(dst);
|
||||
SerialPrint("I", "Files", "copy " + srcPath + " to " + dstPath);
|
||||
if (!FileFS.exists(srcPath)) {
|
||||
SerialPrint("[E]", "Files", "not exist: " + srcPath);
|
||||
return false;
|
||||
}
|
||||
if (FileFS.exists(dstPath)) {
|
||||
if (!overwrite) {
|
||||
SerialPrint("[E]", "Files", "already exist: " + dstPath);
|
||||
return false;
|
||||
}
|
||||
FileFS.remove(dstPath);
|
||||
}
|
||||
auto srcFile = FileFS.open(srcPath, "r");
|
||||
auto dstFile = FileFS.open(dstPath, "w");
|
||||
|
||||
uint8_t buf[512];
|
||||
while (srcFile.available()) {
|
||||
size_t len = srcFile.read(buf, 512);
|
||||
dstFile.write(buf, len);
|
||||
}
|
||||
srcFile.close();
|
||||
dstFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
const String writeFile(const String& filename, const String& str) {
|
||||
String path = filepath(filename);
|
||||
auto file = FileFS.open(path, "w");
|
||||
if (!file) {
|
||||
return "failed";
|
||||
}
|
||||
file.print(str);
|
||||
file.close();
|
||||
return "sucсess";
|
||||
}
|
||||
|
||||
const String readFile(const String& filename, size_t max_size) {
|
||||
String path = filepath(filename);
|
||||
auto file = FileFS.open(path, "r");
|
||||
if (!file) {
|
||||
return "failed";
|
||||
}
|
||||
size_t size = file.size();
|
||||
if (size > max_size) {
|
||||
file.close();
|
||||
return "large";
|
||||
}
|
||||
String temp = file.readString();
|
||||
file.close();
|
||||
return temp;
|
||||
}
|
||||
|
||||
size_t countLines(const String filename) {
|
||||
size_t cnt = -1;
|
||||
String path = filepath(filename);
|
||||
auto file = FileFS.open(path, "r");
|
||||
if (!file) {
|
||||
return cnt;
|
||||
}
|
||||
file.seek(0, SeekSet);
|
||||
size_t size = file.size();
|
||||
size_t psn;
|
||||
do {
|
||||
cnt++;
|
||||
file.readStringUntil('\n');
|
||||
psn = file.position();
|
||||
} while (psn < size);
|
||||
file.close();
|
||||
return cnt;
|
||||
}
|
||||
|
||||
size_t getFileSize(const String filename) {
|
||||
size_t size = -1;
|
||||
String filepath(filename);
|
||||
auto file = FileFS.open(filepath, "r");
|
||||
if (!file) {
|
||||
return size;
|
||||
}
|
||||
size = file.size();
|
||||
file.close();
|
||||
return size;
|
||||
}
|
||||
|
||||
const String getFSSizeInfo() {
|
||||
String res;
|
||||
#ifdef ESP8266
|
||||
FSInfo info;
|
||||
if (FileFS.info(info)) {
|
||||
res = prettyBytes(info.usedBytes) + " of " + prettyBytes(info.totalBytes);
|
||||
} else {
|
||||
res = "error";
|
||||
}
|
||||
#else
|
||||
res = prettyBytes(FileFS.usedBytes()) + " of " + prettyBytes(FileFS.totalBytes());
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
|
||||
const String getConfigFile(uint8_t preset, ConfigType_t type) {
|
||||
char buf[64];
|
||||
sprintf(buf, "/conf/%s%03d.txt", (type == CT_CONFIG) ? "c" : "s", preset);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
bool cutFile(const String& src, const String& dst) {
|
||||
String srcPath = filepath(src);
|
||||
String dstPath = filepath(dst);
|
||||
Serial.println("cut " + srcPath + " to " + dstPath);
|
||||
if (!FileFS.exists(srcPath)) {
|
||||
Serial.println("not exist: " + srcPath);
|
||||
return false;
|
||||
}
|
||||
if (FileFS.exists(dstPath)) {
|
||||
FileFS.remove(dstPath);
|
||||
}
|
||||
auto srcFile = FileFS.open(srcPath, "r");
|
||||
auto dstFile = FileFS.open(dstPath, "w");
|
||||
|
||||
uint8_t buf[512];
|
||||
while (srcFile.available()) {
|
||||
size_t len = srcFile.read(buf, 512);
|
||||
dstFile.write(buf, len);
|
||||
}
|
||||
srcFile.close();
|
||||
dstFile.close();
|
||||
FileFS.remove(srcPath);
|
||||
return true;
|
||||
}
|
||||
@@ -1,179 +0,0 @@
|
||||
#include "Utils/JsonUtils.h"
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include "Global.h"
|
||||
#include "Utils/FileUtils.h"
|
||||
|
||||
//depricated======================================================================
|
||||
String jsonReadStr(String& json, String name) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) SerialPrint("EE", F("jsonRead"), error.f_str());
|
||||
return doc[name].as<String>();
|
||||
}
|
||||
|
||||
boolean jsonReadBool(String& json, String name) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) SerialPrint("EE", F("jsonRead"), error.f_str());
|
||||
return doc[name].as<bool>();
|
||||
}
|
||||
|
||||
int jsonReadInt(String& json, String name) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) SerialPrint("EE", F("jsonRead"), error.f_str());
|
||||
return doc[name].as<int>();
|
||||
}
|
||||
|
||||
//new==============================================================================
|
||||
bool jsonRead(String& json, String key, String& value) {
|
||||
bool ret = true;
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
SerialPrint("EE", F("jsonRead"), error.f_str());
|
||||
ret = false;
|
||||
} else if (!doc.containsKey(key)) {
|
||||
SerialPrint("EE", F("jsonRead"), key + " missing");
|
||||
ret = false;
|
||||
}
|
||||
value = doc[key].as<String>();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool jsonRead(String& json, String key, bool& value) {
|
||||
bool ret = true;
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
SerialPrint("EE", F("jsonRead"), error.f_str());
|
||||
ret = false;
|
||||
} else if (!doc.containsKey(key)) {
|
||||
SerialPrint("EE", F("jsonRead"), key + " missing");
|
||||
ret = false;
|
||||
}
|
||||
value = doc[key].as<bool>();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool jsonRead(String& json, String key, int& value) {
|
||||
bool ret = true;
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
SerialPrint("EE", F("jsonRead"), error.f_str());
|
||||
ret = false;
|
||||
} else if (!doc.containsKey(key)) {
|
||||
SerialPrint("EE", F("jsonRead"), key + " missing");
|
||||
ret = false;
|
||||
}
|
||||
value = doc[key].as<int>();
|
||||
return ret;
|
||||
}
|
||||
//depricated========================================================================
|
||||
String jsonWriteStr(String& json, String name, String value) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
doc[name] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
String jsonWriteBool(String& json, String name, boolean value) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
doc[name] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
String jsonWriteInt(String& json, String name, int value) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
doc[name] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
String jsonWriteFloat(String& json, String name, float value) {
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
doc[name] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
//new==============================================================================
|
||||
bool jsonWriteStr_(String& json, String key, String value) {
|
||||
bool ret = true;
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
ret = false;
|
||||
}
|
||||
doc[key] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool jsonWriteBool_(String& json, String key, bool value) {
|
||||
bool ret = true;
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
ret = false;
|
||||
}
|
||||
doc[key] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool jsonWriteInt_(String& json, String key, int value) {
|
||||
bool ret = true;
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
ret = false;
|
||||
}
|
||||
doc[key] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool jsonWriteFloat_(String& json, String key, float value) {
|
||||
bool ret = true;
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
if (error) {
|
||||
SerialPrint("EE", F("jsonWrite"), error.f_str());
|
||||
ret = false;
|
||||
}
|
||||
doc[key] = value;
|
||||
json = "";
|
||||
serializeJson(doc, json);
|
||||
return ret;
|
||||
}
|
||||
//=================================================================================
|
||||
void saveConfig() {
|
||||
writeFile(String("config.json"), configSetupJson);
|
||||
}
|
||||
|
||||
void saveStore() {
|
||||
writeFile(String("store.json"), configStoreJson);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "Utils\SerialPrint.h"
|
||||
|
||||
#include "Global.h"
|
||||
#include "WebServer.h"
|
||||
#include "WebSocket.h"
|
||||
|
||||
void SerialPrint(String errorLevel, String module, String msg) {
|
||||
String tosend = prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg;
|
||||
Serial.println(tosend);
|
||||
// wsPublishData(F("log"), tosend);
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
#include "Consts.h"
|
||||
|
||||
String selectToMarkerLast(String str, String found) {
|
||||
int p = str.lastIndexOf(found);
|
||||
return str.substring(p + found.length());
|
||||
}
|
||||
|
||||
String selectToMarker(String str, String found) {
|
||||
int p = str.indexOf(found);
|
||||
return str.substring(0, p);
|
||||
}
|
||||
|
||||
String extractInner(String str) {
|
||||
int p1 = str.indexOf("[");
|
||||
int p2 = str.indexOf("]");
|
||||
return str.substring(p1 + 1, p2);
|
||||
}
|
||||
|
||||
String deleteAfterDelimiter(String str, String found) {
|
||||
int p = str.indexOf(found);
|
||||
return str.substring(0, p);
|
||||
}
|
||||
|
||||
String deleteBeforeDelimiter(String str, String found) {
|
||||
int p = str.indexOf(found) + found.length();
|
||||
return str.substring(p);
|
||||
}
|
||||
|
||||
String deleteBeforeDelimiterTo(String str, String found) {
|
||||
int p = str.indexOf(found);
|
||||
return str.substring(p);
|
||||
}
|
||||
|
||||
String deleteToMarkerLast(String str, String found) {
|
||||
int p = str.lastIndexOf(found);
|
||||
return str.substring(0, p);
|
||||
}
|
||||
|
||||
String selectToMarkerPlus(String str, String found, int plus) {
|
||||
int p = str.indexOf(found);
|
||||
return str.substring(0, p + plus);
|
||||
}
|
||||
|
||||
String selectFromMarkerToMarker(String str, String tofind, int number) {
|
||||
if (str.indexOf(tofind) == -1) {
|
||||
return "not found";
|
||||
}
|
||||
str += tofind; // добавим для корректного поиска
|
||||
uint8_t i = 0; // Индекс перебора
|
||||
do {
|
||||
if (i == number) {
|
||||
// если индекс совпал с позицией
|
||||
return selectToMarker(str, tofind);
|
||||
}
|
||||
// отбросим проверенный блок до разделителя
|
||||
str = deleteBeforeDelimiter(str, tofind);
|
||||
i++;
|
||||
} while (str.length() != 0);
|
||||
|
||||
return "not found";
|
||||
}
|
||||
|
||||
uint8_t hexStringToUint8(String hex) {
|
||||
uint8_t tmp = strtol(hex.c_str(), NULL, 0);
|
||||
if (tmp >= 0x00 && tmp <= 0xFF) {
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t hexStringToUint16(String hex) {
|
||||
uint16_t tmp = strtol(hex.c_str(), NULL, 0);
|
||||
if (tmp >= 0x0000 && tmp <= 0xFFFF) {
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
size_t itemsCount2(String str, const String& separator) {
|
||||
// если строки поиск нет сразу выход
|
||||
if (str.indexOf(separator) == -1) {
|
||||
return 0;
|
||||
}
|
||||
// добавим для корректного поиска
|
||||
str += separator;
|
||||
size_t cnt = 0;
|
||||
while (str.length()) {
|
||||
// отбросим проверенный блок до разделителя
|
||||
str = deleteBeforeDelimiter(str, separator);
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
size_t itemsCount(String& str, const char* delim) {
|
||||
size_t cnt = 0;
|
||||
char* cstr = new char[str.length() + 1];
|
||||
strcpy(cstr, str.c_str());
|
||||
char* token;
|
||||
while ((token = strtok_r(cstr, delim, &cstr))) {
|
||||
cnt++;
|
||||
//printf("%s\n", token);
|
||||
}
|
||||
delete[] cstr;
|
||||
return cnt;
|
||||
}
|
||||
|
||||
char* stringToChar(String& str) {
|
||||
char* mychar = new char[str.length() + 1];
|
||||
strcpy(mychar, str.c_str());
|
||||
return mychar;
|
||||
}
|
||||
|
||||
boolean isDigitStr(const String& str) {
|
||||
for (size_t i = 0; i < str.length(); i++) {
|
||||
if (!isDigit(str.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return str.length();
|
||||
}
|
||||
|
||||
boolean isDigitDotCommaStr(const String& str) {
|
||||
for (size_t i = 0; i < str.length(); i++) {
|
||||
char latter = str.charAt(i);
|
||||
if (!isDigit(latter) && latter != '.' && latter != '-') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
String prettyBytes(size_t size) {
|
||||
if (size < 1024)
|
||||
return String(size) + "b";
|
||||
else if (size < (1024 * 1024))
|
||||
return String(size / 1024.0) + "kB";
|
||||
else if (size < (1024 * 1024 * 1024))
|
||||
return String(size / 1024.0 / 1024.0) + "MB";
|
||||
else
|
||||
return String(size / 1024.0 / 1024.0 / 1024.0) + "GB";
|
||||
}
|
||||
@@ -1,250 +0,0 @@
|
||||
#include "Utils/SysUtils.h"
|
||||
|
||||
#include "Global.h"
|
||||
|
||||
|
||||
const String getUniqueId(const char* name) {
|
||||
return String(name) + getMacAddress();
|
||||
}
|
||||
|
||||
uint32_t ESP_getChipId(void) {
|
||||
#ifdef ESP32
|
||||
uint32_t id = 0;
|
||||
for (uint32_t i = 0; i < 17; i = i + 8) {
|
||||
id |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
||||
}
|
||||
return id;
|
||||
#else
|
||||
return ESP.getChipId();
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t ESP_getFlashChipId(void) {
|
||||
#ifdef ESP32
|
||||
// Нет аналогичной (без доп.кода) функций в 32
|
||||
// надо использовать другой id - варианты есть
|
||||
return ESP_getChipId();
|
||||
#else
|
||||
return ESP.getFlashChipId();
|
||||
#endif
|
||||
}
|
||||
|
||||
const String getChipId() {
|
||||
return String(ESP_getChipId()) + "-" + String(ESP_getFlashChipId());
|
||||
}
|
||||
|
||||
void setChipId() {
|
||||
chipId = getChipId();
|
||||
SerialPrint("I", "System", "id: " + chipId);
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
static uint32_t total_memory = 52864;
|
||||
#else
|
||||
static uint32_t total_memory = ESP.getHeapSize();
|
||||
#endif
|
||||
|
||||
const String printMemoryStatus() {
|
||||
uint32_t free = ESP.getFreeHeap();
|
||||
uint32_t used = total_memory - free;
|
||||
uint32_t memory_load = (used * 100) / total_memory;
|
||||
char buf[64];
|
||||
sprintf(buf, "used: %d%% free: %s", memory_load, getHeapStats().c_str());
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
const String getHeapStats() {
|
||||
uint32_t free;
|
||||
uint16_t max;
|
||||
uint8_t frag;
|
||||
ESP.getHeapStats(&free, &max, &frag);
|
||||
String buf;
|
||||
buf += prettyBytes(free);
|
||||
buf += " frag: ";
|
||||
buf += frag;
|
||||
buf += '%';
|
||||
return buf;
|
||||
}
|
||||
#else
|
||||
const String getHeapStats() {
|
||||
String buf;
|
||||
buf = prettyBytes(ESP.getFreeHeap());
|
||||
return buf;
|
||||
}
|
||||
#endif
|
||||
|
||||
const String getMacAddress() {
|
||||
uint8_t mac[6];
|
||||
char buf[13] = {0};
|
||||
#if defined(ESP8266)
|
||||
WiFi.macAddress(mac);
|
||||
sprintf(buf, MACSTR, MAC2STR(mac));
|
||||
#else
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
sprintf(buf, MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
#endif
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
void setLedStatus(LedStatus_t status) {
|
||||
if (jsonReadBool(configSetupJson, "blink") == 1) {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
switch (status) {
|
||||
case LED_OFF:
|
||||
noTone(LED_PIN);
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
break;
|
||||
case LED_ON:
|
||||
noTone(LED_PIN);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
break;
|
||||
case LED_SLOW:
|
||||
tone(LED_PIN, 1);
|
||||
break;
|
||||
case LED_FAST:
|
||||
tone(LED_PIN, 20);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
void setLedStatus(LedStatus_t status) {
|
||||
if (jsonReadBool(configSetupJson, "blink") == 1) {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
switch (status) {
|
||||
case LED_OFF:
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
break;
|
||||
case LED_ON:
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
break;
|
||||
case LED_SLOW:
|
||||
break;
|
||||
case LED_FAST:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
//===================================================================
|
||||
/*
|
||||
void web_print (String text) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
jsonWriteStr(json, "test1", jsonReadStr(json, "test2"));
|
||||
jsonWriteStr(json, "test2", jsonReadStr(json, "test3"));
|
||||
jsonWriteStr(json, "test3", jsonReadStr(json, "test4"));
|
||||
jsonWriteStr(json, "test4", jsonReadStr(json, "test5"));
|
||||
jsonWriteStr(json, "test5", jsonReadStr(json, "test6"));
|
||||
|
||||
jsonWriteStr(json, "test6", GetTime() + " " + text);
|
||||
|
||||
ws.textAll(json);
|
||||
}
|
||||
}
|
||||
*/
|
||||
//===================================================================
|
||||
/*
|
||||
"socket": [
|
||||
"ws://{{ip}}/ws"
|
||||
],
|
||||
*/
|
||||
//===================================================================
|
||||
/*
|
||||
{
|
||||
"type": "h4",
|
||||
"title": "('{{build2}}'=='{{firmware_version}}'?'NEW':'OLD')"
|
||||
},
|
||||
*/
|
||||
//===================================================================
|
||||
/*
|
||||
{
|
||||
"type": "button",
|
||||
"title": "Конфигурация устройства",
|
||||
"socket": "test2",
|
||||
"class": "btn btn-block btn-primary"
|
||||
},
|
||||
{
|
||||
"type": "hr"
|
||||
},
|
||||
{
|
||||
"type": "h6",
|
||||
"title": "{{test1}}"
|
||||
},
|
||||
{
|
||||
"type": "h6",
|
||||
"title": "{{test2}}"
|
||||
},
|
||||
{
|
||||
"type": "h6",
|
||||
"title": "{{test3}}"
|
||||
},
|
||||
{
|
||||
"type": "h6",
|
||||
"title": "{{test4}}"
|
||||
},
|
||||
{
|
||||
"type": "h6",
|
||||
"title": "{{test5}}"
|
||||
},
|
||||
{
|
||||
"type": "h6",
|
||||
"title": "{{test6}}"
|
||||
},
|
||||
{
|
||||
"type": "hr"
|
||||
},
|
||||
*/
|
||||
//===================================================================
|
||||
|
||||
/*
|
||||
String getResetReason(uint8_t core) {
|
||||
int reason = rtc_get_reset_reason(core);
|
||||
switch (reason) {
|
||||
case 1 : return "Power on"; break; //Vbat power on reset
|
||||
case 3 : return "Software reset digital core"; break; //Software reset digital core
|
||||
case 4 : return "Legacy watch dog reset digital core"; break; //Legacy watch dog reset digital core
|
||||
case 5 : return "Deep Sleep reset digital core"; break; //Deep Sleep reset digital core
|
||||
case 6 : return "Reset by SLC module, reset digital core"; break; //Reset by SLC module, reset digital core
|
||||
case 7 : return "Timer Group0 Watch dog reset digital core"; break; //Timer Group0 Watch dog reset digital core
|
||||
case 8 : return "Timer Group1 Watch dog reset digital core"; break; //Timer Group1 Watch dog reset digital core
|
||||
case 9 : return "RTC Watch dog Reset digital core"; break; //
|
||||
case 10 : return "Instrusion tested to reset CPU"; break;
|
||||
case 11 : return "Time Group reset CPU"; break;
|
||||
case 12 : return "Software reset CPU"; break;
|
||||
case 13 : return "RTC Watch dog Reset CPU"; break;
|
||||
case 14 : return "for APP CPU, reseted by PRO CPU"; break;
|
||||
case 15 : return "Reset when the vdd voltage is not stable"; break;
|
||||
case 16 : return "RTC Watch dog reset digital core and rtc module"; break;
|
||||
default : return "NO_MEAN";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String EspClass::getResetReason(void) {
|
||||
char buff[32];
|
||||
if (resetInfo.reason == REASON_DEFAULT_RST) { // normal startup by power on
|
||||
strcpy_P(buff, PSTR("Power on"));
|
||||
} else if (resetInfo.reason == REASON_WDT_RST) { // hardware watch dog reset
|
||||
strcpy_P(buff, PSTR("Hardware Watchdog"));
|
||||
} else if (resetInfo.reason == REASON_EXCEPTION_RST) { // exception reset, GPIO status won’t change
|
||||
strcpy_P(buff, PSTR("Exception"));
|
||||
} else if (resetInfo.reason == REASON_SOFT_WDT_RST) { // software watch dog reset, GPIO status won’t change
|
||||
strcpy_P(buff, PSTR("Software Watchdog"));
|
||||
} else if (resetInfo.reason == REASON_SOFT_RESTART) { // software restart ,system_restart , GPIO status won’t change
|
||||
strcpy_P(buff, PSTR("Software/System restart"));
|
||||
} else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) { // wake up from deep-sleep
|
||||
strcpy_P(buff, PSTR("Deep-Sleep Wake"));
|
||||
} else if (resetInfo.reason == REASON_EXT_SYS_RST) { // external system reset
|
||||
strcpy_P(buff, PSTR("External System"));
|
||||
} else {
|
||||
strcpy_P(buff, PSTR("Unknown"));
|
||||
}
|
||||
return String(buff);
|
||||
}
|
||||
*/
|
||||
@@ -1,237 +0,0 @@
|
||||
#include "Utils/TimeUtils.h"
|
||||
#include "Global.h"
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
static const uint8_t days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
|
||||
//static const char* week_days[7] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };
|
||||
|
||||
// String getTimeUnix() {
|
||||
// time_t t;
|
||||
// struct tm* tm;
|
||||
|
||||
// t = time(NULL);
|
||||
// tm = localtime(&t);
|
||||
// Serial.printf("%04d/%02d/%02d(%s) %02d:%02d:%02d\n",
|
||||
// tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, week_days[tm->tm_wday],
|
||||
// tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
// delay(1000);
|
||||
// time_t now = time(nullptr);
|
||||
// if (now < 30000) {
|
||||
// return "failed";
|
||||
// }
|
||||
// return String(now);
|
||||
// }
|
||||
|
||||
// String getTime() {
|
||||
// time_t now = time(nullptr);
|
||||
// int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||
// now = now + zone;
|
||||
// String Time = ""; // Строка для результатов времени
|
||||
// Time += ctime(&now); // Преобразуем время в строку формата Thu Jan 19 00:55:35 2017
|
||||
// int i = Time.indexOf(":"); //Ишем позицию первого символа :
|
||||
// Time = Time.substring(i - 2, i + 6); // Выделяем из строки 2 символа перед символом : и 6 символов после
|
||||
// return Time; // Возврашаем полученное время
|
||||
// }
|
||||
|
||||
// String getTimeWOsec() {
|
||||
// time_t now = time(nullptr);
|
||||
// int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||
// now = now + zone;
|
||||
// String Time = ""; // Строка для результатов времени
|
||||
// Time += ctime(&now); // Преобразуем время в строку формата Thu Jan 19 00:55:35 2017
|
||||
// int i = Time.indexOf(":"); //Ишем позицию первого символа :
|
||||
// Time = Time.substring(i - 2, i + 3); // Выделяем из строки 2 символа перед символом : и 6 символов после
|
||||
// return Time; // Возврашаем полученное время
|
||||
// }
|
||||
|
||||
// String getDate() {
|
||||
// time_t now = time(nullptr);
|
||||
// int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
|
||||
// now = now + zone;
|
||||
// String Data = ""; // Строка для результатов времени
|
||||
// Data += ctime(&now); // Преобразуем время в строку формата Thu Jan 19 00:55:35 2017
|
||||
// Data.replace("\n", "");
|
||||
// uint8_t i = Data.lastIndexOf(" "); //Ишем позицию последнего символа пробел
|
||||
// String Time = Data.substring(i - 8, i + 1); // Выделяем время и пробел
|
||||
// Data.replace(Time, ""); // Удаляем из строки 8 символов времени и пробел
|
||||
// return Data; // Возврашаем полученную дату
|
||||
// }
|
||||
|
||||
// String getDateDigitalFormated() {
|
||||
// String date = getDate();
|
||||
|
||||
// date = deleteBeforeDelimiter(date, " ");
|
||||
|
||||
// date.replace("Jan", "01");
|
||||
// date.replace("Feb", "02");
|
||||
// date.replace("Mar", "03");
|
||||
// date.replace("Apr", "04");
|
||||
// date.replace("May", "05");
|
||||
// date.replace("Jun", "06");
|
||||
// date.replace("Jul", "07");
|
||||
// date.replace("Aug", "08");
|
||||
// date.replace("Sep", "09");
|
||||
// date.replace("Oct", "10");
|
||||
// date.replace("Nov", "11");
|
||||
// date.replace("Dec", "12");
|
||||
|
||||
// String month = date.substring(0, 2);
|
||||
// String day = date.substring(3, 5);
|
||||
// String year = date.substring(8, 10);
|
||||
|
||||
// String out = day;
|
||||
// out += ".";
|
||||
// out += month;
|
||||
// out += ".";
|
||||
// out += year;
|
||||
|
||||
// return out;
|
||||
// }
|
||||
|
||||
// int timeToMin(String Time) {
|
||||
// //"00:00:00" время в секунды
|
||||
// long min = selectToMarker(Time, ":").toInt() * 60; //общее количество секунд в полных часах
|
||||
// Time = deleteBeforeDelimiter(Time, ":"); // Теперь здесь минуты секунды
|
||||
// min += selectToMarker(Time, ":").toInt(); // Добавим секунды из полных минут
|
||||
// return min;
|
||||
// }
|
||||
|
||||
static const char* TIME_FORMAT PROGMEM = "%02d:%02d:%02d";
|
||||
static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %02d:%02d";
|
||||
|
||||
const String prettySeconds(unsigned long time_s) {
|
||||
unsigned long tmp = time_s;
|
||||
unsigned long seconds;
|
||||
unsigned long minutes;
|
||||
unsigned long hours;
|
||||
unsigned long days;
|
||||
seconds = tmp % 60;
|
||||
tmp = tmp / 60;
|
||||
|
||||
minutes = tmp % 60;
|
||||
tmp = tmp / 60;
|
||||
|
||||
hours = tmp % 24;
|
||||
days = tmp / 24;
|
||||
|
||||
char buf[32];
|
||||
|
||||
if (days) {
|
||||
sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes, seconds);
|
||||
}
|
||||
else {
|
||||
sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds);
|
||||
}
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
const String prettyMillis(unsigned long time_ms) {
|
||||
return prettySeconds(time_ms / 1000);
|
||||
}
|
||||
|
||||
unsigned long millis_since(unsigned long sinse) {
|
||||
return millis_passed(sinse, millis());
|
||||
}
|
||||
|
||||
unsigned long millis_passed(unsigned long start, unsigned long finish) {
|
||||
unsigned long result = 0;
|
||||
if (start <= finish) {
|
||||
unsigned long passed = finish - start;
|
||||
if (passed <= __LONG_MAX__) {
|
||||
result = static_cast<long>(passed);
|
||||
}
|
||||
else {
|
||||
result = static_cast<long>((__LONG_MAX__ - finish) + start + 1u);
|
||||
}
|
||||
}
|
||||
else {
|
||||
unsigned long passed = start - finish;
|
||||
if (passed <= __LONG_MAX__) {
|
||||
result = static_cast<long>(passed);
|
||||
result = -1 * result;
|
||||
}
|
||||
else {
|
||||
result = static_cast<long>((__LONG_MAX__ - start) + finish + 1u);
|
||||
result = -1 * result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int getOffsetInSeconds(int timezone) {
|
||||
return getOffsetInMinutes(timezone) * ONE_MINUTE_s;
|
||||
}
|
||||
|
||||
int getOffsetInMinutes(int timezone) {
|
||||
return timezone * ONE_HOUR_m;
|
||||
}
|
||||
|
||||
void breakEpochToTime(unsigned long epoch, Time_t& tm) {
|
||||
// break the given time_input into time components
|
||||
// this is a more compact version of the C library localtime function
|
||||
|
||||
unsigned long time = epoch;
|
||||
tm.second = time % 60;
|
||||
time /= 60; // now it is minutes
|
||||
tm.minute = time % 60;
|
||||
time /= 60; // now it is hours
|
||||
tm.hour = time % 24;
|
||||
time /= 24; // now it is days
|
||||
tm.days = time;
|
||||
tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1
|
||||
|
||||
uint8_t year = 0;
|
||||
unsigned long days = 0;
|
||||
|
||||
while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
|
||||
year++;
|
||||
}
|
||||
tm.year = year - 30;
|
||||
|
||||
days -= LEAP_YEAR(year) ? 366 : 365;
|
||||
time -= days; // now it is days in this year, starting at 0
|
||||
tm.day_of_year = time;
|
||||
|
||||
uint8_t month;
|
||||
uint8_t month_length;
|
||||
for (month = 0; month < 12; month++) {
|
||||
if (1 == month) { // february
|
||||
if (LEAP_YEAR(year)) {
|
||||
month_length = 29;
|
||||
}
|
||||
else {
|
||||
month_length = 28;
|
||||
}
|
||||
}
|
||||
else {
|
||||
month_length = days_in_month[month];
|
||||
}
|
||||
|
||||
if (time >= month_length) {
|
||||
time -= month_length;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tm.month = month + 1; // jan is month 1
|
||||
tm.day_of_month = time + 1; // day of month
|
||||
tm.valid = (epoch > MIN_DATETIME);
|
||||
}
|
||||
|
||||
void timeInit() {
|
||||
ts.add(
|
||||
TIME, 1000, [&](void*) {
|
||||
String timenow = timeNow->getTimeWOsec();
|
||||
static String prevTime;
|
||||
if (prevTime != timenow) {
|
||||
prevTime = timenow;
|
||||
jsonWriteStr(configLiveJson, "timenow", timenow);
|
||||
eventGen2("timenow", timenow);
|
||||
SerialPrint("I", F("NTP"), timenow);
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
SerialPrint("I", F("NTP"), F("Handle time init"));
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
#include "Utils/WebUtils.h"
|
||||
|
||||
#include "ESPAsyncWebServer.h"
|
||||
|
||||
String getURL(const String& urls) {
|
||||
String res = "";
|
||||
HTTPClient http;
|
||||
http.begin(espClient, urls);
|
||||
int httpCode = http.GET();
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
res = http.getString();
|
||||
} else {
|
||||
res = "error";
|
||||
}
|
||||
http.end();
|
||||
return res;
|
||||
}
|
||||
|
||||
const String getMethodName(AsyncWebServerRequest* request) {
|
||||
String res = F("UNKNOWN");
|
||||
if (request->method() == HTTP_GET)
|
||||
res = F("GET");
|
||||
else if (request->method() == HTTP_POST)
|
||||
res = F("POST");
|
||||
else if (request->method() == HTTP_DELETE)
|
||||
res = F("DELETE");
|
||||
else if (request->method() == HTTP_PUT)
|
||||
res = F("PUT");
|
||||
else if (request->method() == HTTP_PATCH)
|
||||
res = F("PATCH");
|
||||
else if (request->method() == HTTP_HEAD)
|
||||
res = F("HEAD");
|
||||
else if (request->method() == HTTP_OPTIONS)
|
||||
res = F("OPTIONS");
|
||||
return res;
|
||||
}
|
||||
|
||||
const String getRequestInfo(AsyncWebServerRequest* request) {
|
||||
String res = getMethodName(request);
|
||||
res += ' ';
|
||||
res += "http://";
|
||||
res += request->host();
|
||||
res += request->url();
|
||||
res += '\n';
|
||||
if (request->contentLength()) {
|
||||
res += "content-type: ";
|
||||
res += request->contentType();
|
||||
res += " content-lenght: ";
|
||||
res += prettyBytes(request->contentLength());
|
||||
res += '\n';
|
||||
}
|
||||
|
||||
if (request->headers()) {
|
||||
res += "headers:\n";
|
||||
for (size_t i = 0; i < request->headers(); i++) {
|
||||
AsyncWebHeader* h = request->getHeader(i);
|
||||
res += h->name();
|
||||
res += '=';
|
||||
res += h->value();
|
||||
res += '\n';
|
||||
}
|
||||
}
|
||||
|
||||
if (request->params()) {
|
||||
res += "params:\n";
|
||||
for (size_t i = 0; i < request->params(); i++) {
|
||||
AsyncWebParameter* p = request->getParam(i);
|
||||
if (p->isFile()) {
|
||||
res += "FILE";
|
||||
} else if (p->isPost()) {
|
||||
res += "POST";
|
||||
} else {
|
||||
res += "GET";
|
||||
}
|
||||
res += ' ';
|
||||
res += p->name();
|
||||
res += ':';
|
||||
res += p->value();
|
||||
if (p->isFile()) {
|
||||
res += " size:";
|
||||
res += p->size();
|
||||
}
|
||||
res += '\n';
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
#include "Utils/WiFiUtils.h"
|
||||
#include "FileSystem.h"
|
||||
|
||||
void routerConnect() {
|
||||
WiFi.setAutoConnect(false);
|
||||
WiFi.persistent(false);
|
||||
|
||||
setLedStatus(LED_SLOW);
|
||||
WiFi.mode(WIFI_STA);
|
||||
byte tries = 40;
|
||||
|
||||
String _ssid = jsonReadStr(configSetupJson, "routerssid");
|
||||
String _password = jsonReadStr(configSetupJson, "routerpass");
|
||||
|
||||
if (_ssid == "" && _password == "") {
|
||||
WiFi.begin();
|
||||
} else {
|
||||
WiFi.begin(_ssid.c_str(), _password.c_str());
|
||||
#ifdef ESP32
|
||||
WiFi.setTxPower(WIFI_POWER_19_5dBm);
|
||||
#else
|
||||
WiFi.setOutputPower(20.5);
|
||||
#endif
|
||||
SerialPrint("I", "WIFI", "ssid: " + _ssid);
|
||||
SerialPrint("I", "WIFI", "pass: " + _password);
|
||||
}
|
||||
|
||||
while (--tries && WiFi.status() != WL_CONNECTED) {
|
||||
if (WiFi.status() == WL_CONNECT_FAILED) {
|
||||
SerialPrint("E", "WIFI", "password is not correct");
|
||||
tries = 1;
|
||||
jsonWriteInt(configOptionJson, "pass_status", 1);
|
||||
}
|
||||
Serial.print(".");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("");
|
||||
startAPMode();
|
||||
} else {
|
||||
Serial.println("");
|
||||
SerialPrint("I", "WIFI", "http://" + WiFi.localIP().toString());
|
||||
jsonWriteStr(configSetupJson, "ip", WiFi.localIP().toString());
|
||||
setLedStatus(LED_OFF);
|
||||
mqttInit();
|
||||
}
|
||||
SerialPrint("I", F("WIFI"), F("Network Init"));
|
||||
}
|
||||
|
||||
bool startAPMode() {
|
||||
setLedStatus(LED_ON);
|
||||
SerialPrint("I", "WIFI", "AP Mode");
|
||||
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
String _ssidAP = jsonReadStr(configSetupJson, "apssid");
|
||||
String _passwordAP = jsonReadStr(configSetupJson, "appass");
|
||||
|
||||
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str());
|
||||
IPAddress myIP = WiFi.softAPIP();
|
||||
|
||||
SerialPrint("I", "WIFI", "AP IP: " + myIP.toString());
|
||||
jsonWriteStr(configSetupJson, "ip", myIP.toString());
|
||||
|
||||
//if (jsonReadInt(configOptionJson, "pass_status") != 1) {
|
||||
ts.add(
|
||||
WIFI_SCAN, 10 * 1000, [&](void*) {
|
||||
String sta_ssid = jsonReadStr(configSetupJson, "routerssid");
|
||||
|
||||
SerialPrint("I", "WIFI", "scanning for " + sta_ssid);
|
||||
|
||||
if (RouterFind(sta_ssid)) {
|
||||
ts.remove(WIFI_SCAN);
|
||||
WiFi.scanDelete();
|
||||
routerConnect();
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean RouterFind(String ssid) {
|
||||
bool res = false;
|
||||
int n = WiFi.scanComplete();
|
||||
SerialPrint("I", "WIFI", "scan result: " + String(n, DEC));
|
||||
|
||||
if (n == -2) { //Сканирование не было запущено, запускаем
|
||||
SerialPrint("I", "WIFI", "start scanning");
|
||||
WiFi.scanNetworks(true, false); //async, show_hidden
|
||||
}
|
||||
|
||||
else if (n == -1) { //Сканирование все еще выполняется
|
||||
SerialPrint("I", "WIFI", "scanning in progress");
|
||||
}
|
||||
|
||||
else if (n == 0) { //ни одна сеть не найдена
|
||||
SerialPrint("I", "WIFI", "no networks found");
|
||||
WiFi.scanNetworks(true, false);
|
||||
}
|
||||
|
||||
else if (n > 0) {
|
||||
for (int8_t i = 0; i < n; i++) {
|
||||
if (WiFi.SSID(i) == ssid) {
|
||||
res = true;
|
||||
}
|
||||
SerialPrint("I", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
|
||||
}
|
||||
}
|
||||
WiFi.scanDelete();
|
||||
return res;
|
||||
}
|
||||
|
||||
boolean isNetworkActive() {
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
uint8_t RSSIquality() {
|
||||
uint8_t res = 0;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
int rssi = WiFi.RSSI();
|
||||
if (rssi >= -50) {
|
||||
res = 6; //"Excellent";
|
||||
} else if (rssi < -50 && rssi >= -60) {
|
||||
res = 5; //"Very good";
|
||||
} else if (rssi < -60 && rssi >= -70) {
|
||||
res = 4; //"Good";
|
||||
} else if (rssi < -70 && rssi >= -80) {
|
||||
res = 3; //"Low";
|
||||
} else if (rssi < -80 && rssi > -100) {
|
||||
res = 2; //"Very low";
|
||||
} else if (rssi <= -100) {
|
||||
res = 1; //"No signal";
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void wifiSignalInit() {
|
||||
ts.add(
|
||||
SYGNAL, 1000 * 60, [&](void*) {
|
||||
SerialPrint("I", "System", printMemoryStatus());
|
||||
|
||||
getFSInfo();
|
||||
|
||||
switch (RSSIquality()) {
|
||||
case 0:
|
||||
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='red'>не подключено к роутеру</font>"));
|
||||
break;
|
||||
case 1:
|
||||
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='red'>нет сигнала</font>"));
|
||||
break;
|
||||
case 2:
|
||||
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='red'>очень низкий</font>"));
|
||||
break;
|
||||
case 3:
|
||||
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='orange'>низкий</font>"));
|
||||
break;
|
||||
case 4:
|
||||
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='green'>хороший</font>"));
|
||||
break;
|
||||
case 5:
|
||||
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='green'>очень хороший</font>"));
|
||||
break;
|
||||
case 6:
|
||||
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='green'>отличный</font>"));
|
||||
break;
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
}
|
||||
@@ -1,316 +0,0 @@
|
||||
#include "Utils/StatUtils.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
#include "Global.h"
|
||||
#include "ItemsList.h"
|
||||
|
||||
#ifdef ESP32
|
||||
#include <rom/rtc.h>
|
||||
#endif
|
||||
|
||||
String ESP_getResetReason(void);
|
||||
|
||||
void initSt() {
|
||||
addNewDevice();
|
||||
decide();
|
||||
if (TELEMETRY_UPDATE_INTERVAL_MIN) {
|
||||
ts.add(
|
||||
STATISTICS, TELEMETRY_UPDATE_INTERVAL_MIN * 60000, [&](void*) {
|
||||
static bool secondTime = false;
|
||||
if (secondTime) getNextNumber("totalhrs.txt");
|
||||
secondTime = true;
|
||||
updateDeviceStatus();
|
||||
},
|
||||
nullptr, true);
|
||||
}
|
||||
SerialPrint("I", F("Stat"), F("Stat Init"));
|
||||
}
|
||||
|
||||
void decide() {
|
||||
if ((WiFi.status() == WL_CONNECTED)) {
|
||||
uint8_t cnt = getNextNumber("stat.txt");
|
||||
SerialPrint("I", "Stat", "Total resets number: " + String(cnt));
|
||||
if (cnt <= 3) {
|
||||
Serial.println("(get)");
|
||||
getPsn();
|
||||
}
|
||||
else {
|
||||
if (cnt % 5) {
|
||||
Serial.println("(skip)");
|
||||
}
|
||||
else {
|
||||
Serial.println("(get)");
|
||||
getPsn();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void getPsn() {
|
||||
String res = getURL(F("http://ipinfo.io/?token=c60f88583ad1a4"));
|
||||
if (res != "") {
|
||||
String line = jsonReadStr(res, "loc");
|
||||
String lat = selectToMarker(line, ",");
|
||||
String lon = deleteBeforeDelimiter(line, ",");
|
||||
String geo = jsonReadStr(res, "city") + ", " + jsonReadStr(res, "country") + ", " + jsonReadStr(res, "region");
|
||||
updateDevicePsn(lat, lon, "0", geo);
|
||||
}
|
||||
}
|
||||
|
||||
String addNewDevice() {
|
||||
String ret;
|
||||
if ((WiFi.status() == WL_CONNECTED)) {
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
String json = "{}";
|
||||
String mac = WiFi.macAddress().c_str();
|
||||
//==============================================
|
||||
jsonWriteStr(json, "uniqueId", mac);
|
||||
jsonWriteStr(json, "name", FIRMWARE_NAME);
|
||||
jsonWriteStr(json, "model", getChipId());
|
||||
//==============================================
|
||||
http.begin(client, serverIP + F(":8082/api/devices/"));
|
||||
http.setAuthorization("admin", "admin");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
int httpCode = http.POST(json);
|
||||
if (httpCode > 0) {
|
||||
ret = httpCode;
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
String payload = http.getString();
|
||||
ret += " " + payload;
|
||||
//saveId("statid.txt", jsonReadInt(payload, "id"));
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = http.errorToString(httpCode).c_str();
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
SerialPrint("I", "Stat", "New device registaration: " + ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
String updateDevicePsn(String lat, String lon, String accur, String geo) {
|
||||
String ret;
|
||||
if ((WiFi.status() == WL_CONNECTED)) {
|
||||
float latfl = lat.toFloat();
|
||||
float lonfl = lon.toFloat();
|
||||
randomSeed(micros());
|
||||
float c = random(2, 9);
|
||||
if (c > 5) {
|
||||
latfl = latfl + (c / 100);
|
||||
lonfl = lonfl + (c / 100);
|
||||
}
|
||||
else {
|
||||
latfl = latfl - (c / 100);
|
||||
lonfl = lonfl - (c / 100);
|
||||
}
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.begin(client, serverIP + F(":5055/"));
|
||||
http.setAuthorization("admin", "admin");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
String mac = WiFi.macAddress().c_str();
|
||||
int httpCode = http.POST("?id=" + mac +
|
||||
"&lat=" + String(latfl) +
|
||||
"&lon=" + String(lonfl) +
|
||||
"&accuracy=" + accur + "");
|
||||
if (httpCode > 0) {
|
||||
ret = httpCode;
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
String payload = http.getString();
|
||||
ret += " " + payload;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = http.errorToString(httpCode).c_str();
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
SerialPrint("I", "Stat", "Update device psn: " + ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
String updateDeviceStatus() {
|
||||
String ret;
|
||||
if ((WiFi.status() == WL_CONNECTED)) {
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.begin(client, serverIP + F(":5055/"));
|
||||
http.setAuthorization("admin", "admin");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
String mac = WiFi.macAddress().c_str();
|
||||
int httpCode = http.POST("?id=" + mac +
|
||||
"&resetReason=" + ESP_getResetReason() +
|
||||
"&uptime=" + timeNow->getUptime() +
|
||||
"&uptimeTotal=" + getUptimeTotal() +
|
||||
"&version=" + FIRMWARE_VERSION +
|
||||
"&resetsTotal=" + String(getCurrentNumber("stat.txt")) +
|
||||
"&heap=" + String(ESP.getFreeHeap()) + "");
|
||||
if (httpCode > 0) {
|
||||
ret = httpCode;
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
String payload = http.getString();
|
||||
ret += " " + payload;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = http.errorToString(httpCode).c_str();
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
SerialPrint("I", "Stat", "Update device data: " + ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
String getUptimeTotal() {
|
||||
uint8_t hrs = getCurrentNumber("totalhrs.txt");
|
||||
String hrsStr = prettySeconds(hrs * 60 * 60);
|
||||
SerialPrint("I", "Stat", "Total running time: " + hrsStr);
|
||||
return hrsStr;
|
||||
}
|
||||
|
||||
uint8_t getNextNumber(String file) {
|
||||
uint8_t number = readFile(file, 100).toInt();
|
||||
number++;
|
||||
removeFile(file);
|
||||
addFile(file, String(number));
|
||||
return number;
|
||||
}
|
||||
|
||||
uint8_t getCurrentNumber(String file) {
|
||||
uint8_t number = readFile(file, 100).toInt();
|
||||
return number;
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
String ESP_getResetReason(void) {
|
||||
return ESP.getResetReason();
|
||||
}
|
||||
#else
|
||||
String ESP32GetResetReason(uint32_t cpu_no) {
|
||||
// tools\sdk\include\esp32\rom\rtc.h
|
||||
switch (rtc_get_reset_reason((RESET_REASON)cpu_no)) {
|
||||
case POWERON_RESET:
|
||||
return F("Vbat power on reset"); // 1
|
||||
case SW_RESET:
|
||||
return F("Software reset digital core"); // 3
|
||||
case OWDT_RESET:
|
||||
return F("Legacy watch dog reset digital core"); // 4
|
||||
case DEEPSLEEP_RESET:
|
||||
return F("Deep Sleep reset digital core"); // 5
|
||||
case SDIO_RESET:
|
||||
return F("Reset by SLC module, reset digital core"); // 6
|
||||
case TG0WDT_SYS_RESET:
|
||||
return F("Timer Group0 Watch dog reset digital core"); // 7
|
||||
case TG1WDT_SYS_RESET:
|
||||
return F("Timer Group1 Watch dog reset digital core"); // 8
|
||||
case RTCWDT_SYS_RESET:
|
||||
return F("RTC Watch dog Reset digital core"); // 9
|
||||
case INTRUSION_RESET:
|
||||
return F("Instrusion tested to reset CPU"); // 10
|
||||
case TGWDT_CPU_RESET:
|
||||
return F("Time Group reset CPU"); // 11
|
||||
case SW_CPU_RESET:
|
||||
return F("Software reset CPU"); // 12
|
||||
case RTCWDT_CPU_RESET:
|
||||
return F("RTC Watch dog Reset CPU"); // 13
|
||||
case EXT_CPU_RESET:
|
||||
return F("or APP CPU, reseted by PRO CPU"); // 14
|
||||
case RTCWDT_BROWN_OUT_RESET:
|
||||
return F("Reset when the vdd voltage is not stable"); // 15
|
||||
case RTCWDT_RTC_RESET:
|
||||
return F("RTC Watch dog reset digital core and rtc module"); // 16
|
||||
default:
|
||||
return F("NO_MEAN"); // 0
|
||||
}
|
||||
}
|
||||
|
||||
String ESP_getResetReason(void) {
|
||||
return ESP32GetResetReason(0); // CPU 0
|
||||
}
|
||||
#endif
|
||||
//String getUptimeTotal() {
|
||||
// static int hrs;
|
||||
// EEPROM.begin(512);
|
||||
// hrs = eeGetInt(0);
|
||||
// SerialPrint("I","Stat","Total running hrs: " + String(hrs));
|
||||
// String hrsStr = prettySeconds(hrs * 60 * 60);
|
||||
// SerialPrint("I","Stat","Total running hrs (f): " + hrsStr);
|
||||
// return hrsStr;
|
||||
//}
|
||||
//int plusOneHour() {
|
||||
// static int hrs;
|
||||
// EEPROM.begin(512);
|
||||
// hrs = eeGetInt(0);
|
||||
// hrs++;
|
||||
// eeWriteInt(0, hrs);
|
||||
// return hrs;
|
||||
//}
|
||||
//
|
||||
//void eeWriteInt(int pos, int val) {
|
||||
// byte* p = (byte*)&val;
|
||||
// EEPROM.write(pos, *p);
|
||||
// EEPROM.write(pos + 1, *(p + 1));
|
||||
// EEPROM.write(pos + 2, *(p + 2));
|
||||
// EEPROM.write(pos + 3, *(p + 3));
|
||||
// EEPROM.commit();
|
||||
//}
|
||||
//
|
||||
//int eeGetInt(int pos) {
|
||||
// int val;
|
||||
// byte* p = (byte*)&val;
|
||||
// *p = EEPROM.read(pos);
|
||||
// *(p + 1) = EEPROM.read(pos + 1);
|
||||
// *(p + 2) = EEPROM.read(pos + 2);
|
||||
// *(p + 3) = EEPROM.read(pos + 3);
|
||||
// if (val < 0) {
|
||||
// return 0;
|
||||
// } else {
|
||||
// return val;
|
||||
// }
|
||||
//}
|
||||
//========for updating list of device=================
|
||||
/*
|
||||
void updateDeviceList() {
|
||||
if ((WiFi.status() == WL_CONNECTED)) {
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
String json = "{}";
|
||||
String mac = WiFi.macAddress().c_str();
|
||||
//===============================================
|
||||
jsonWriteStr(json, "uniqueId", mac);
|
||||
jsonWriteStr(json, "name", FIRMWARE_NAME);
|
||||
jsonWriteStr(json, "model", FIRMWARE_VERSION);
|
||||
jsonWriteInt(json, "id", getId("statid.txt"));
|
||||
//===============================================
|
||||
http.begin(client, "http://") + serverIP + F(":8082/api/devices/" + mac + "/");
|
||||
http.setAuthorization("admin", "admin");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
int httpCode = http.PUT(json);
|
||||
if (httpCode > 0) {
|
||||
Serial.printf("update Device List... code: %d\n", httpCode);
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
const String& payload = http.getString();
|
||||
Serial.println("received payload:\n<<");
|
||||
Serial.println(payload);
|
||||
Serial.println(">>");
|
||||
}
|
||||
} else {
|
||||
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
}
|
||||
|
||||
void saveId(String file, int id) {
|
||||
removeFile(file);
|
||||
addFile(file, String(id));
|
||||
}
|
||||
|
||||
int getId(String file) {
|
||||
return readFile(file, 100).toInt();
|
||||
}
|
||||
*/
|
||||
419
src/Web.cpp
419
src/Web.cpp
@@ -1,419 +0,0 @@
|
||||
#include "Web.h"
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/NotAsync.h"
|
||||
#include "Global.h"
|
||||
#include "Init.h"
|
||||
#include "ItemsList.h"
|
||||
#include "RemoteOrdersUdp.h"
|
||||
#include "SoftUART.h"
|
||||
#include "Telegram.h"
|
||||
#include "items/vLogging.h"
|
||||
|
||||
bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) {
|
||||
if (request->hasArg("preset")) {
|
||||
preset = request->getParam("preset")->value().toInt();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void web_init() {
|
||||
server.on("/set", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
//==============================set.device.json====================================================================================================
|
||||
if (request->hasArg(F("addItem"))) {
|
||||
addItem2(request->getParam("addItem")->value().toInt());
|
||||
request->redirect("/?set.device");
|
||||
}
|
||||
|
||||
if (request->hasArg(F("addPreset"))) {
|
||||
addPreset2(request->getParam(F("addPreset"))->value().toInt());
|
||||
|
||||
jsonWriteStr(configSetupJson, F("warning1"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Требуется перезагрузка</p></font></div>"));
|
||||
|
||||
request->redirect(F("/?set.device"));
|
||||
}
|
||||
|
||||
if (request->hasArg(F("delChoosingItems"))) {
|
||||
jsonWriteStr(configSetupJson, F("warning1"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Требуется перезагрузка</p></font></div>"));
|
||||
myNotAsyncActions->make(do_delChoosingItems);
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("delAllItems"))) {
|
||||
delAllItems();
|
||||
#ifdef EnableLogging
|
||||
cleanLogAndData();
|
||||
#endif
|
||||
jsonWriteStr(configSetupJson, F("warning1"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Требуется перезагрузка</p></font></div>"));
|
||||
request->redirect(F("/?set.device"));
|
||||
}
|
||||
|
||||
if (request->hasArg(F("saveItems"))) {
|
||||
myNotAsyncActions->make(do_deviceInit);
|
||||
savedFromWeb = true;
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("scen"))) {
|
||||
bool value = request->getParam(F("scen"))->value().toInt();
|
||||
jsonWriteBool(configSetupJson, F("scen"), value);
|
||||
saveConfig();
|
||||
loadScenario();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("sceninit"))) {
|
||||
loadScenario();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("MqttIn"))) {
|
||||
bool value = request->getParam(F("MqttIn"))->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "MqttIn", value);
|
||||
saveConfig();
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/event").c_str());
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("MqttOut"))) {
|
||||
bool value = request->getParam(F("MqttOut"))->value().toInt();
|
||||
jsonWriteBool(configSetupJson, F("MqttOut"), value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("scenMqtt"))) {
|
||||
myNotAsyncActions->make(do_sendScenMQTT);
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("cleanlog"))) {
|
||||
#ifdef EnableLogging
|
||||
cleanLogAndData();
|
||||
#endif
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
//==============================wifi settings=============================================
|
||||
if (request->hasArg(F("devname"))) {
|
||||
jsonWriteStr(configSetupJson, F("name"), request->getParam(F("devname"))->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("routerssid"))) {
|
||||
jsonWriteStr(configSetupJson, F("routerssid"), request->getParam(F("routerssid"))->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("routerpass"))) {
|
||||
jsonWriteStr(configSetupJson, F("routerpass"), request->getParam(F("routerpass"))->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("apssid"))) {
|
||||
jsonWriteStr(configSetupJson, F("apssid"), request->getParam(F("apssid"))->value());
|
||||
saveConfig();
|
||||
request->send(200, "text/text", "OK");
|
||||
}
|
||||
|
||||
if (request->hasArg(F("appass"))) {
|
||||
jsonWriteStr(configSetupJson, F("appass"), request->getParam(F("appass"))->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("weblogin"))) {
|
||||
jsonWriteStr(configSetupJson, F("weblogin"), request->getParam(F("weblogin"))->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("webpass"))) {
|
||||
jsonWriteStr(configSetupJson, F("webpass"), request->getParam(F("webpass"))->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("timezone"))) {
|
||||
String timezoneStr = request->getParam(F("timezone"))->value();
|
||||
jsonWriteStr(configSetupJson, F("timezone"), timezoneStr);
|
||||
saveConfig();
|
||||
timeNow->setTimezone(timezoneStr.toInt());
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("ntp"))) {
|
||||
String ntpStr = request->getParam(F("ntp"))->value();
|
||||
jsonWriteStr(configSetupJson, F("ntp"), ntpStr);
|
||||
saveConfig();
|
||||
timeNow->setNtpPool(ntpStr);
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("blink"))) {
|
||||
bool value = request->getParam(F("blink"))->value().toInt();
|
||||
jsonWriteBool(configSetupJson, F("blink"), value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("reqReset"))) {
|
||||
String tmp = "{}";
|
||||
jsonWriteStr(tmp, "title", F("<button class=\"close\" onclick=\"toggle('reset-block')\">×</button>Вы действительно хотите перезагрузить устройство?<a href=\"#\" class=\"btn btn-block btn-danger\" onclick=\"send_request(this, '/set?reset');setTimeout(function(){ location.href='/?set.device'; }, 15000);html('reset-block','<span class=loader></span>Идет перезагрузка устройства')\">Перезагрузить</a>"));
|
||||
jsonWriteStr(tmp, "class", "pop-up");
|
||||
request->send(200, "text/html", tmp);
|
||||
}
|
||||
|
||||
if (request->hasArg(F("reset"))) {
|
||||
ESP.restart();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
//==============================mqtt settings=============================================
|
||||
// primary
|
||||
if (request->hasArg(F("mqttServer"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttServer"), request->getParam(F("mqttServer"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttPort"))) {
|
||||
int port = (request->getParam(F("mqttPort"))->value()).toInt();
|
||||
jsonWriteInt(configSetupJson, F("mqttPort"), port);
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttPrefix"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttPrefix"), request->getParam(F("mqttPrefix"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttUser"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttUser"), request->getParam(F("mqttUser"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttPass"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttPass"), request->getParam(F("mqttPass"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
// secondary
|
||||
if (request->hasArg(F("mqttServer2"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttServer2"), request->getParam(F("mqttServer2"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttPort2"))) {
|
||||
int port = (request->getParam(F("mqttPort2"))->value()).toInt();
|
||||
jsonWriteInt(configSetupJson, F("mqttPort2"), port);
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttPrefix2"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttPrefix2"), request->getParam(F("mqttPrefix2"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttUser2"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttUser2"), request->getParam(F("mqttUser2"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg(F("mqttPass2"))) {
|
||||
jsonWriteStr(configSetupJson, F("mqttPass2"), request->getParam(F("mqttPass2"))->value());
|
||||
saveConfig();
|
||||
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg("mqttsend")) {
|
||||
// myNotAsyncActions->make(do_MQTTUDP);
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg("mqttcheck")) {
|
||||
String buf = "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + getStateStr();
|
||||
|
||||
String payload = "{}";
|
||||
jsonWriteStr(payload, "title", buf);
|
||||
jsonWriteStr(payload, "class", "pop-up");
|
||||
|
||||
request->send(200, "text/html", payload);
|
||||
}
|
||||
|
||||
//==============================telegram settings=============================================
|
||||
if (request->hasArg("telegramApi")) {
|
||||
jsonWriteStr(configSetupJson, "telegramApi", request->getParam("telegramApi")->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("autos")) {
|
||||
bool value = request->getParam("autos")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "autos", value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("chatId")) {
|
||||
jsonWriteStr(configSetupJson, "chatId", request->getParam("chatId")->value());
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("telegonof")) {
|
||||
bool value = request->getParam("telegonof")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "telegonof", value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("teleginput")) {
|
||||
bool value = request->getParam("teleginput")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "teleginput", value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
//==============================utilities settings=============================================
|
||||
if (request->hasArg("i2c")) {
|
||||
myNotAsyncActions->make(do_BUSSCAN);
|
||||
request->redirect("/?set.utilities");
|
||||
}
|
||||
if (request->hasArg("uart")) {
|
||||
bool value = request->getParam("uart")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "uart", value);
|
||||
saveConfig();
|
||||
#ifdef EnableUart
|
||||
uartInit();
|
||||
#endif
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("uartEvents")) {
|
||||
bool value = request->getParam("uartEvents")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "uartEvents", value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("uartS")) {
|
||||
jsonWriteStr(configSetupJson, "uartS", request->getParam("uartS")->value());
|
||||
saveConfig();
|
||||
#ifdef EnableUart
|
||||
uartInit();
|
||||
#endif
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("uartTX")) {
|
||||
jsonWriteStr(configSetupJson, "uartTX", request->getParam("uartTX")->value());
|
||||
saveConfig();
|
||||
#ifdef EnableUart
|
||||
uartInit();
|
||||
#endif
|
||||
request->send(200);
|
||||
}
|
||||
if (request->hasArg("uartRX")) {
|
||||
jsonWriteStr(configSetupJson, "uartRX", request->getParam("uartRX")->value());
|
||||
saveConfig();
|
||||
#ifdef EnableUart
|
||||
uartInit();
|
||||
#endif
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
//==============================developer settings=============================================
|
||||
if (request->hasArg("serverip")) {
|
||||
jsonWriteStr(configSetupJson, "serverip", request->getParam("serverip")->value());
|
||||
saveConfig();
|
||||
serverIP = jsonReadStr(configSetupJson, "serverip");
|
||||
request->send(200);
|
||||
}
|
||||
// set?order=button_1
|
||||
if (request->hasArg("order")) {
|
||||
String order = request->getParam("order")->value();
|
||||
order.replace("_", " ");
|
||||
loopCmdAdd(order + ",");
|
||||
request->send(200, "text/html", order);
|
||||
}
|
||||
|
||||
if (request->hasArg("grafmax")) {
|
||||
int value = request->getParam("grafmax")->value().toInt();
|
||||
jsonWriteInt(configSetupJson, "grafmax", value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
// gate mode
|
||||
|
||||
if (request->hasArg("gateAuto")) {
|
||||
bool value = request->getParam("gateAuto")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "gateAuto", value);
|
||||
saveConfig();
|
||||
request->send(200);
|
||||
}
|
||||
});
|
||||
|
||||
// server.on("/del", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
// if (request->hasArg("file") && request->hasArg("line")) {
|
||||
// String fileName = request->getParam("file")->value();
|
||||
// Serial.println(fileName);
|
||||
// int line = request->getParam("line")->value().toInt();
|
||||
// Serial.println(line);
|
||||
// myNotAsyncActions->make(do_delChoosingItems);
|
||||
// request->redirect(F("/?set.device"));
|
||||
// }
|
||||
// });
|
||||
|
||||
server.on("/check", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
myNotAsyncActions->make(do_GETLASTVERSION);
|
||||
SerialPrint("I", "Update", "firmware version: " + String(lastVersion));
|
||||
|
||||
String msg = "";
|
||||
|
||||
if (USE_OTA) {
|
||||
if (lastVersion == FIRMWARE_VERSION) {
|
||||
msg = F("Актуальная версия прошивки уже установлена.");
|
||||
} else if (lastVersion > FIRMWARE_VERSION) {
|
||||
msg = F("Новая версия прошивки<a href=\"#\" class=\"btn btn-block btn-danger\" onclick=\"send_request(this, '/upgrade');setTimeout(function(){ location.href='/?set.device'; }, 90000);html('my-block','<span class=loader></span>Идет обновление прошивки, после обновления страница перезагрузится автоматически...')\">Установить</a>");
|
||||
} else if (lastVersion == -1) {
|
||||
msg = F("Cервер не найден. Попробуйте повторить позже...");
|
||||
} else if (lastVersion == -2) {
|
||||
msg = F("Устройство не подключено к роутеру!");
|
||||
} else if (lastVersion < FIRMWARE_VERSION) {
|
||||
msg = F("Ошибка версии. Попробуйте повторить позже...");
|
||||
}
|
||||
} else {
|
||||
msg = F("Обновление невозможно, память устройства 1 мб");
|
||||
}
|
||||
|
||||
String tmp = "{}";
|
||||
jsonWriteStr(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + msg);
|
||||
jsonWriteStr(tmp, "class", "pop-up");
|
||||
request->send(200, "text/html", tmp);
|
||||
});
|
||||
|
||||
/*
|
||||
* Upgrade
|
||||
*/
|
||||
server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
myNotAsyncActions->make(do_UPGRADE);
|
||||
request->send(200, "text/html");
|
||||
});
|
||||
|
||||
SerialPrint("I", F("Web"), F("WebAdmin Init"));
|
||||
}
|
||||
|
||||
void setConfigParam(const char* param, const String& value) {
|
||||
SerialPrint("I", "Web", "set " + String(param) + ": " + value);
|
||||
jsonWriteStr(configSetupJson, param, value);
|
||||
saveConfig();
|
||||
}
|
||||
@@ -1,230 +0,0 @@
|
||||
#include "WebServer.h"
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/NotAsync.h"
|
||||
#include "FSEditor.h"
|
||||
#include "Utils/FileUtils.h"
|
||||
#include "Utils/WebUtils.h"
|
||||
#include "WebSocket.h"
|
||||
|
||||
AsyncWebSocket ws("/ws");
|
||||
AsyncEventSource events("/events");
|
||||
|
||||
void HttpServerinit() {
|
||||
String login = jsonReadStr(configSetupJson, "weblogin");
|
||||
String pass = jsonReadStr(configSetupJson, "webpass");
|
||||
#ifdef ESP32
|
||||
server.addHandler(new FSEditor(FileFS, login, pass));
|
||||
#else
|
||||
server.addHandler(new FSEditor(login, pass));
|
||||
#endif
|
||||
|
||||
//#ifdef CORS_DEBUG
|
||||
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Origin"), F("*"));
|
||||
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Headers"), F("content-type"));
|
||||
//#endif
|
||||
|
||||
// server.sendHeader("Access-Control-Allow-Origin", "*");
|
||||
server.serveStatic("/css/", FileFS, "/css/").setCacheControl("max-age=600");
|
||||
server.serveStatic("/js/", FileFS, "/js/").setCacheControl("max-age=600");
|
||||
server.serveStatic("/favicon.ico", FileFS, "/favicon.ico").setCacheControl("max-age=600");
|
||||
server.serveStatic("/icon.jpeg", FileFS, "/icon.jpeg").setCacheControl("max-age=600");
|
||||
server.serveStatic("/edit", FileFS, "/edit").setCacheControl("max-age=600");
|
||||
|
||||
#ifdef svelte
|
||||
server.serveStatic("/", FileFS, "/").setDefaultFile("index.html").setAuthentication(login.c_str(), pass.c_str());
|
||||
#else
|
||||
server.serveStatic("/", FileFS, "/").setDefaultFile("index.htm").setAuthentication(login.c_str(), pass.c_str());
|
||||
#endif
|
||||
|
||||
//server.onNotFound([](AsyncWebServerRequest *request) {
|
||||
// SerialPrint("[E]", "WebServer", "not found:\n" + getRequestInfo(request));
|
||||
// request->send(404);
|
||||
//});
|
||||
|
||||
server.onNotFound([](AsyncWebServerRequest *request) {
|
||||
if (request->method() == HTTP_OPTIONS) {
|
||||
request->send(200);
|
||||
} else {
|
||||
request->send(404);
|
||||
}
|
||||
});
|
||||
|
||||
server.onFileUpload([](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) {
|
||||
// TODO
|
||||
if (!index) {
|
||||
SerialPrint("I", "WebServer", "start upload " + filename);
|
||||
}
|
||||
if (final) {
|
||||
SerialPrint("I", "WebServer", "finish upload: " + prettyBytes(index + len));
|
||||
}
|
||||
});
|
||||
|
||||
server.on("/file.json", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
String file = readFile("file.json", 1024);
|
||||
request->send(200, "application/json", file);
|
||||
});
|
||||
|
||||
// динамические данные
|
||||
server.on("/config.live.json", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
request->send(200, "application/json", configLiveJson);
|
||||
});
|
||||
|
||||
server.on("/config.store.json", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
request->send(200, "application/json", configStoreJson);
|
||||
});
|
||||
|
||||
// данные не являющиеся событиями
|
||||
server.on("/config.option.json", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
request->send(200, "application/json", configOptionJson);
|
||||
});
|
||||
|
||||
// для хранения постоянных данных
|
||||
server.on("/config.setup.json", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
request->send(200, "application/json", configSetupJson);
|
||||
});
|
||||
|
||||
server.on("/cmd", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
String cmdStr = request->getParam("command")->value();
|
||||
SerialPrint("I", "WebServer", "do: " + cmdStr);
|
||||
loopCmdAdd(cmdStr);
|
||||
request->send(200, "text/html", "OK");
|
||||
});
|
||||
|
||||
server.begin();
|
||||
|
||||
initOta();
|
||||
initMDNS();
|
||||
HttpServerinitWS();
|
||||
|
||||
SerialPrint("I", F("HTTP"), F("HttpServer Init"));
|
||||
}
|
||||
|
||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
||||
#ifdef WEBSOCKET_ENABLED
|
||||
if (type == WS_EVT_CONNECT) {
|
||||
SerialPrint("I", F("WS"), F("CONNECTED"));
|
||||
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
|
||||
// client->printf(json.c_str(), client->id());
|
||||
// client->ping();
|
||||
} else if (type == WS_EVT_DISCONNECT) {
|
||||
Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id());
|
||||
} else if (type == WS_EVT_ERROR) {
|
||||
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t *)arg), (char *)data);
|
||||
} else if (type == WS_EVT_PONG) {
|
||||
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char *)data : "");
|
||||
} else if (type == WS_EVT_DATA) {
|
||||
AwsFrameInfo *info = (AwsFrameInfo *)arg;
|
||||
String msg = "";
|
||||
if (info->final && info->index == 0 && info->len == len) {
|
||||
// the whole message is in a single frame and we got all of it's data
|
||||
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len);
|
||||
|
||||
if (info->opcode == WS_TEXT) {
|
||||
for (size_t i = 0; i < info->len; i++) {
|
||||
msg += (char)data[i];
|
||||
}
|
||||
} else {
|
||||
char buff[3];
|
||||
for (size_t i = 0; i < info->len; i++) {
|
||||
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
||||
msg += buff;
|
||||
}
|
||||
}
|
||||
Serial.printf("%s\n", msg.c_str());
|
||||
|
||||
if (msg.startsWith("/config")) {
|
||||
// myNotAsyncActions->make(do_webSocketSendSetup);
|
||||
// wsSetupFlag = true;
|
||||
}
|
||||
|
||||
if (info->opcode == WS_TEXT) {
|
||||
// client->text("{}");
|
||||
} else {
|
||||
// client->binary("{}");
|
||||
}
|
||||
} else {
|
||||
// message is comprised of multiple frames or the frame is split into multiple packets
|
||||
if (info->index == 0) {
|
||||
if (info->num == 0)
|
||||
Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
|
||||
Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
|
||||
}
|
||||
|
||||
Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT) ? "text" : "binary", info->index, info->index + len);
|
||||
|
||||
if (info->opcode == WS_TEXT) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
msg += (char)data[i];
|
||||
}
|
||||
} else {
|
||||
char buff[3];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
sprintf(buff, "%02x ", (uint8_t)data[i]);
|
||||
msg += buff;
|
||||
}
|
||||
}
|
||||
Serial.printf("%s\n", msg.c_str());
|
||||
|
||||
if ((info->index + len) == info->len) {
|
||||
Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
|
||||
if (info->final) {
|
||||
Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
|
||||
if (info->message_opcode == WS_TEXT)
|
||||
client->text("I got your text message");
|
||||
else
|
||||
client->binary("I got your binary message");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void initMDNS() {
|
||||
#ifdef MDNS_ENABLED
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
// TODO Add Adduino OTA
|
||||
#endif
|
||||
}
|
||||
|
||||
void initOta() {
|
||||
#ifdef OTA_UPDATES_ENABLED
|
||||
ArduinoOTA.onStart([]() {
|
||||
events.send("Update Start", "ota");
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
events.send("Update End", "ota");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
char p[32];
|
||||
sprintf(p, "Progress: %u%%\n", (progress / (total / 100)));
|
||||
events.send(p, "ota");
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
if (error == OTA_AUTH_ERROR)
|
||||
events.send("Auth Failed", "ota");
|
||||
else if (error == OTA_BEGIN_ERROR)
|
||||
events.send("Begin Failed", "ota");
|
||||
else if (error == OTA_CONNECT_ERROR)
|
||||
events.send("Connect Failed", "ota");
|
||||
else if (error == OTA_RECEIVE_ERROR)
|
||||
events.send("Recieve Failed", "ota");
|
||||
else if (error == OTA_END_ERROR)
|
||||
events.send("End Failed", "ota");
|
||||
});
|
||||
ArduinoOTA.setHostname(hostName);
|
||||
ArduinoOTA.begin();
|
||||
#endif
|
||||
}
|
||||
|
||||
void HttpServerinitWS() {
|
||||
#ifdef WEBSOCKET_ENABLED
|
||||
ws.onEvent(onWsEvent);
|
||||
server.addHandler(&ws);
|
||||
events.onConnect([](AsyncEventSourceClient *client) {
|
||||
// client->send("", NULL, millis(), 1000);
|
||||
});
|
||||
server.addHandler(&events);
|
||||
#endif
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
#include "WebSocket.h"
|
||||
|
||||
#include "ArduinoJson.h"
|
||||
#include "Class/NotAsync.h"
|
||||
#include "Class/TCircularBuffer.h"
|
||||
#include "Global.h"
|
||||
|
||||
void wsInit() {
|
||||
// myWsBuffer = new TCircularBuffer<char*, 20480>;
|
||||
// myNotAsyncActions->add(
|
||||
// do_webSocketSendSetup, [&](void*) {
|
||||
// delay(100);
|
||||
// wsSendSetup();
|
||||
// },
|
||||
// nullptr);
|
||||
}
|
||||
|
||||
void wsPublishData(String topic, String data) {
|
||||
if (ws.enabled()) {
|
||||
if (ws.availableForWriteAll()) {
|
||||
data = "[" + topic + "]" + data;
|
||||
ws.textAll(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//отправка setup массива в sockets способом через буфер string, рабочий способ но буфер стринг - плохой метод
|
||||
void wsSendSetup3() {
|
||||
File file = seekFile("/setup.json");
|
||||
DynamicJsonDocument doc(2048);
|
||||
int i = 0;
|
||||
file.find("[");
|
||||
SerialPrint("I", F("WS"), F("start send config"));
|
||||
do {
|
||||
i++;
|
||||
deserializeJson(doc, file);
|
||||
wsBuf += doc.as<String>() + "\n";
|
||||
} while (file.findUntil(",", "]"));
|
||||
SerialPrint("I", F("WS"), F("completed send config"));
|
||||
}
|
||||
|
||||
void loopWsExecute3() {
|
||||
static int attampts = wsAttempts;
|
||||
if (wsBuf.length()) {
|
||||
if (attampts > 0) {
|
||||
if (ws.availableForWriteAll()) {
|
||||
String tmp = selectToMarker(wsBuf, "\n");
|
||||
wsPublishData("config", tmp);
|
||||
wsBuf = deleteBeforeDelimiter(wsBuf, "\n");
|
||||
attampts = wsAttempts;
|
||||
} else {
|
||||
attampts--;
|
||||
SerialPrint("I", F("WS"), String(attampts));
|
||||
}
|
||||
} else {
|
||||
SerialPrint("I", F("WS"), F("socket fatal error"));
|
||||
attampts = wsAttempts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//отправка setup массива в sockets способом через кольцевой буфер char
|
||||
// void wsSendSetup() {
|
||||
// File file = seekFile("/setup.json");
|
||||
// DynamicJsonDocument doc(2048);
|
||||
// int i = 0;
|
||||
// file.find("[");
|
||||
// SerialPrint("I", F("WS"), F("start send config"));
|
||||
// do {
|
||||
// i++;
|
||||
// deserializeJson(doc, file);
|
||||
// char* element;
|
||||
// char* element = new char[1024];
|
||||
// serializeJson(doc, (char*)element, 1024);
|
||||
// Serial.println(element);
|
||||
//
|
||||
// } while (file.findUntil(",", "]"));
|
||||
// SerialPrint("I", F("WS"), F("completed send config"));
|
||||
//
|
||||
// char* itemsend = "element";
|
||||
// myWsBuffer->push(itemsend);
|
||||
//
|
||||
// char* item;
|
||||
// while (myWsBuffer->pop(item)) {
|
||||
// Serial.println(item);
|
||||
// }
|
||||
//}
|
||||
|
||||
// void loopWsExecute() {
|
||||
// char* item;
|
||||
// if (myWsBuffer->pop(item)) {
|
||||
// Serial.println(item);
|
||||
// }
|
||||
// }
|
||||
|
||||
//отправка setup массива в sockets способом прямой загрузки в ws buffer
|
||||
// void wsSendSetupBuffer() {
|
||||
// File file = seekFile("/setup.json");
|
||||
// DynamicJsonDocument doc(2048);
|
||||
// int i = 0;
|
||||
// file.find("[");
|
||||
// SerialPrint("I", F("WS"), F("start send config"));
|
||||
// do {
|
||||
// i++;
|
||||
// deserializeJson(doc, file);
|
||||
// size_t len = measureJson(doc);
|
||||
// AsyncWebSocketMessageBuffer* buffer = ws.makeBuffer(len);
|
||||
// if (buffer) {
|
||||
// serializeJson(doc, (char*)buffer->get(), len);
|
||||
// if (ws.enabled()) {
|
||||
// if (ws.availableForWriteAll()) {
|
||||
// ws.textAll(buffer);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // Serial.println(doc.as<String>());
|
||||
// } while (file.findUntil(",", "]"));
|
||||
// SerialPrint("I", F("WS"), F("completed send config"));
|
||||
//}
|
||||
|
||||
//Пример прямого доступа к web socket buffer переделанный для arduino json 6
|
||||
// void sendDataWs() {
|
||||
// DynamicJsonDocument doc(1024);
|
||||
// doc["a"] = "abc";
|
||||
// doc["b"] = "abcd";
|
||||
// doc["c"] = "abcde";
|
||||
// doc["d"] = "abcdef";
|
||||
// doc["e"] = "abcdefg";
|
||||
// size_t len = measureJson(doc);
|
||||
// AsyncWebSocketMessageBuffer* buffer = ws.makeBuffer(len);
|
||||
// if (buffer) {
|
||||
// serializeJson(doc, (char*)buffer->get(), len);
|
||||
// ws.textAll(buffer);
|
||||
// }
|
||||
//}
|
||||
@@ -1,92 +0,0 @@
|
||||
#include "Global.h"
|
||||
|
||||
|
||||
const String getWidgetFile(const String& name);
|
||||
|
||||
bool loadWidget(const String& filename, String& buf) {
|
||||
buf = readFile(getWidgetFile(filename), 2048);
|
||||
bool res = !(buf == "Failed" || buf == "Large");
|
||||
if (!res) {
|
||||
SerialPrint("[E]","Widgets","on load" + filename);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void createWidget(String descr, String page, String order, String filename, String topic) {
|
||||
String buf = "{}";
|
||||
if (!loadWidget(filename, buf)) {
|
||||
return;
|
||||
}
|
||||
descr.replace("#", " ");
|
||||
page.replace("#", " ");
|
||||
|
||||
jsonWriteStr(buf, "page", page);
|
||||
jsonWriteStr(buf, "order", order);
|
||||
jsonWriteStr(buf, "descr", descr);
|
||||
jsonWriteStr(buf, "topic", prex + "/" + topic);
|
||||
|
||||
#ifdef LAYOUT_IN_RAM
|
||||
all_widgets += widget + "\r\n";
|
||||
#else
|
||||
addFileLn("layout.txt", buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
//TODO Вот эта процедура, и несколько оберток
|
||||
void createWidgetParam(String widget, String page, String pageNumber, String filename, String topic,
|
||||
String name1, String param1, String name2, String param2, String name3, String param3) {
|
||||
String buf = "";
|
||||
if (!loadWidget(filename, buf)) {
|
||||
return;
|
||||
}
|
||||
|
||||
widget.replace("#", " ");
|
||||
page.replace("#", " ");
|
||||
|
||||
jsonWriteStr(buf, "page", page);
|
||||
jsonWriteStr(buf, "order", pageNumber);
|
||||
jsonWriteStr(buf, "descr", widget);
|
||||
jsonWriteStr(buf, "topic", prex + "/" + topic);
|
||||
|
||||
if (name1) jsonWriteStr(buf, name1, param1);
|
||||
if (name2) jsonWriteStr(buf, name2, param2);
|
||||
if (name3) jsonWriteStr(buf, name3, param3);
|
||||
|
||||
#ifdef LAYOUT_IN_RAM
|
||||
all_widgets += widget + "\r\n";
|
||||
#else
|
||||
addFileLn("layout.txt", buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
void createChart(String widget, String page, String pageNumber, String filename, String topic,
|
||||
String maxCount) {
|
||||
String buf = "";
|
||||
if (!loadWidget(filename, buf)) {
|
||||
return;
|
||||
}
|
||||
|
||||
widget.replace("#", " ");
|
||||
page.replace("#", " ");
|
||||
|
||||
jsonWriteStr(buf, "page", page);
|
||||
jsonWriteStr(buf, "order", pageNumber);
|
||||
//jsonWriteStr(widget, "descr", widget_name);
|
||||
jsonWriteStr(buf, "series", widget);
|
||||
jsonWriteStr(buf, "maxCount", maxCount);
|
||||
jsonWriteStr(buf, "topic", prex + "/" + topic);
|
||||
|
||||
#ifdef LAYOUT_IN_RAM
|
||||
all_widgets += widget + "\r\n";
|
||||
#else
|
||||
addFileLn("layout.txt", buf);
|
||||
#endif
|
||||
}
|
||||
|
||||
void createWidgetByType(String widget, String page, String pageNumber, String type, String topic) {
|
||||
createWidget(widget, page, pageNumber, getWidgetFile(type), topic);
|
||||
}
|
||||
|
||||
const String getWidgetFile(const String& name) {
|
||||
return "/widgets/" + name + ".json";
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableButtonIn
|
||||
#include "BufferExecute.h"
|
||||
#include "items/ButtonInClass.h"
|
||||
//==========================================Модуль физических кнопок========================================
|
||||
//button-in switch1 toggle Кнопки Свет 1 pin[2] db[20]
|
||||
//==========================================================================================================
|
||||
|
||||
boolean but[NUM_BUTTONS];
|
||||
Bounce *buttons = new Bounce[NUM_BUTTONS];
|
||||
|
||||
ButtonInClass myButtonIn;
|
||||
void buttonIn() {
|
||||
myButtonIn.update();
|
||||
String key = myButtonIn.gkey();
|
||||
String pin = myButtonIn.gpin();
|
||||
sCmd.addCommand(key.c_str(), buttonInSet);
|
||||
myButtonIn.init();
|
||||
myButtonIn.switchStateSetDefault();
|
||||
myButtonIn.clear();
|
||||
}
|
||||
|
||||
void buttonInSet() {
|
||||
String key = sCmd.order();
|
||||
String state = sCmd.next();
|
||||
myButtonIn.switchChangeVirtual(key, state);
|
||||
}
|
||||
#endif
|
||||
@@ -1,47 +0,0 @@
|
||||
//#include "items/test.h"
|
||||
//#include "BufferExecute.h"
|
||||
//#include "Class/LineParsing.h"
|
||||
//#include "Global.h"
|
||||
//
|
||||
//// Outside of class
|
||||
//SensorImpulsIn *pointerToClass; // declare a pointer to SensorImpulsIn class
|
||||
//
|
||||
//static void outsideInterruptHandler(void) { // define global handler
|
||||
// pointerToClass->classInterruptHandler(); // calls class member handler
|
||||
//}
|
||||
//
|
||||
//SensorImpulsIn::SensorImpulsIn(const paramsImpulsIn& paramsImpuls) {
|
||||
// _paramsImpuls = paramsImpulsIn(paramsImpuls);
|
||||
//
|
||||
// pinMode(paramsImpuls.pin, INPUT);
|
||||
//
|
||||
// pointerToClass = this; // assign current instance to pointer (IMPORTANT!!!)
|
||||
// attachInterrupt(digitalPinToInterrupt(_paramsImpuls.pin), outsideInterruptHandler, CHANGE);
|
||||
//}
|
||||
//
|
||||
//MySensorImpulsInVector* mySensorImpulsIn = nullptr;
|
||||
//
|
||||
//void SensorImpulsIn::classInterruptHandler(void) {
|
||||
// localPointerToCallback(digitalRead(_paramsImpuls.pin));
|
||||
//}
|
||||
//
|
||||
//void impulsInSensor() {
|
||||
// myLineParsing.update();
|
||||
// String key = myLineParsing.gkey();
|
||||
// String pin = myLineParsing.gpin();
|
||||
// String c = myLineParsing.gc();
|
||||
// String k = myLineParsing.gk();
|
||||
// myLineParsing.clear();
|
||||
//
|
||||
// static paramsImpulsIn paramsImpuls;
|
||||
//
|
||||
// paramsImpuls.key = key;
|
||||
// paramsImpuls.pin = pin.toInt();
|
||||
// paramsImpuls.c = c.toFloat();
|
||||
// paramsImpuls.k = k.toFloat();
|
||||
//
|
||||
// static bool firstTime = true;
|
||||
// if (firstTime) mySensorImpulsIn = new MySensorImpulsInVector();
|
||||
// firstTime = false;
|
||||
// mySensorImpulsIn->push_back(SensorImpulsIn(paramsImpuls));
|
||||
//}
|
||||
@@ -1,94 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableButtonOut
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
#include "SoftUART.h"
|
||||
#include "items/vButtonOut.h"
|
||||
//this class save data to flash
|
||||
ButtonOut::ButtonOut(String pin, boolean inv, String key, String type) {
|
||||
_pin = pin;
|
||||
_inv = inv;
|
||||
_key = key;
|
||||
_type = type;
|
||||
#ifdef ESP_MODE
|
||||
if (_pin != "") {
|
||||
pinMode(_pin.toInt(), OUTPUT);
|
||||
}
|
||||
int state = jsonReadInt(configStoreJson, key); //прочитали из памяти
|
||||
this->execute(String(state)); //установили это состояние
|
||||
#endif
|
||||
#ifdef GATE_MODE
|
||||
//TO DO запросили ноду о состоянии реле
|
||||
//установили в это состояние кнопку в приложении
|
||||
//если нода не ответила - кнопку сделать красным цветом
|
||||
#endif
|
||||
}
|
||||
ButtonOut::~ButtonOut() {}
|
||||
|
||||
void ButtonOut::execute(String state) {
|
||||
#ifdef ESP_MODE
|
||||
if (state != "" && _pin != "") {
|
||||
if (state == "change") {
|
||||
state = String(!digitalRead(_pin.toInt()));
|
||||
digitalWrite(_pin.toInt(), state.toInt());
|
||||
} else {
|
||||
if (_inv) {
|
||||
digitalWrite(_pin.toInt(), !state.toInt());
|
||||
} else {
|
||||
digitalWrite(_pin.toInt(), state.toInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef GATE_MODE
|
||||
//отправили ноде команду на вкл выкл
|
||||
//получили обратную связь - переставили кнопку в приложении
|
||||
//не получили обратную связь - сделали кнопку красной
|
||||
#endif
|
||||
eventGen2(_key, state);
|
||||
jsonWriteInt(configStoreJson, _key, state.toInt());
|
||||
saveStore();
|
||||
publishStatus(_key, state);
|
||||
}
|
||||
|
||||
MyButtonOutVector* myButtonOut = nullptr;
|
||||
|
||||
void buttonOut() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String pin = myLineParsing.gpin();
|
||||
String inv = myLineParsing.ginv();
|
||||
String type = myLineParsing.gtype();
|
||||
|
||||
bool invb = false;
|
||||
if (inv.toInt() == 1) invb = true;
|
||||
|
||||
myLineParsing.clear();
|
||||
|
||||
buttonOut_EnterCounter++;
|
||||
addKey(key, buttonOut_KeyList, buttonOut_EnterCounter);
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) myButtonOut = new MyButtonOutVector();
|
||||
firstTime = false;
|
||||
myButtonOut->push_back(ButtonOut(pin, invb, key, type));
|
||||
|
||||
sCmd.addCommand(key.c_str(), buttonOutExecute);
|
||||
}
|
||||
|
||||
void buttonOutExecute() {
|
||||
String key = sCmd.order();
|
||||
String state = sCmd.next();
|
||||
|
||||
int number = getKeyNum(key, buttonOut_KeyList);
|
||||
|
||||
if (myButtonOut != nullptr) {
|
||||
if (number != -1) {
|
||||
myButtonOut->at(number).execute(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,79 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableCountDown
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
#include "items/vCountDown.h"
|
||||
|
||||
CountDownClass::CountDownClass(String key) {
|
||||
_key = key;
|
||||
}
|
||||
|
||||
CountDownClass::~CountDownClass() {}
|
||||
|
||||
void CountDownClass::execute(unsigned int countDownPeriod) {
|
||||
_countDownPeriod = countDownPeriod * 1000;
|
||||
_start = true;
|
||||
}
|
||||
|
||||
void CountDownClass::loop() {
|
||||
if (_countDownPeriod > 0 && _start) {
|
||||
prevMillis = millis();
|
||||
sec = (_countDownPeriod / 1000);
|
||||
_start = false;
|
||||
}
|
||||
|
||||
difference = millis() - prevMillis;
|
||||
if (difference > 1000 && _countDownPeriod > 0) {
|
||||
prevMillis = millis();
|
||||
eventGen2(_key, String(sec));
|
||||
String time = String(prettyMillis(sec * 1000));
|
||||
jsonWriteStr(configLiveJson, _key, time);
|
||||
publishStatus(_key, time);
|
||||
sec--;
|
||||
if (sec < 0) {
|
||||
_countDownPeriod = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MyCountDownVector* myCountDown = nullptr;
|
||||
|
||||
void countDown() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
myLineParsing.clear();
|
||||
|
||||
countDown_EnterCounter++;
|
||||
addKey(key, countDown_KeyList, countDown_EnterCounter);
|
||||
|
||||
//Serial.println(countDown_EnterCounter);
|
||||
//Serial.println(countDown_KeyList);
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) myCountDown = new MyCountDownVector();
|
||||
firstTime = false;
|
||||
myCountDown->push_back(CountDownClass(key));
|
||||
|
||||
sCmd.addCommand(key.c_str(), countDownExecute);
|
||||
}
|
||||
|
||||
void countDownExecute() {
|
||||
String key = sCmd.order();
|
||||
String value = sCmd.next();
|
||||
|
||||
if (!isDigitStr(value)) { //если значение - текст
|
||||
value = getValue(value);
|
||||
}
|
||||
|
||||
int number = getKeyNum(key, countDown_KeyList);
|
||||
|
||||
if (myCountDown != nullptr) {
|
||||
if (number != -1) {
|
||||
myCountDown->at(number).execute(value.toInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,73 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableImpulsOut
|
||||
#include "items/vImpulsOut.h"
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
#include "BufferExecute.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
ImpulsOutClass::ImpulsOutClass(unsigned int impulsPin) {
|
||||
_impulsPin = impulsPin;
|
||||
pinMode(impulsPin, OUTPUT);
|
||||
}
|
||||
|
||||
ImpulsOutClass::~ImpulsOutClass() {}
|
||||
|
||||
void ImpulsOutClass::execute(unsigned long impulsPeriod, unsigned int impulsCount) {
|
||||
_impulsPeriod = impulsPeriod;
|
||||
_impulsCount = impulsCount * 2;
|
||||
_impulsCountBuf = _impulsCount;
|
||||
}
|
||||
|
||||
void ImpulsOutClass::loop() {
|
||||
currentMillis = millis();
|
||||
difference = currentMillis - prevMillis;
|
||||
if (_impulsCountBuf > 0) {
|
||||
if (difference > _impulsPeriod) {
|
||||
_impulsCountBuf--;
|
||||
prevMillis = millis();
|
||||
yield();
|
||||
digitalWrite(_impulsPin, !digitalRead(_impulsPin));
|
||||
yield();
|
||||
}
|
||||
}
|
||||
if (_impulsCountBuf <= 0) {
|
||||
digitalWrite(_impulsPin, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
MyImpulsOutVector* myImpulsOut = nullptr;
|
||||
|
||||
void impuls() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String pin = myLineParsing.gpin();
|
||||
myLineParsing.clear();
|
||||
|
||||
impuls_EnterCounter++;
|
||||
addKey(key, impuls_KeyList, impuls_EnterCounter);
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) myImpulsOut = new MyImpulsOutVector();
|
||||
firstTime = false;
|
||||
myImpulsOut->push_back(ImpulsOutClass(pin.toInt()));
|
||||
|
||||
sCmd.addCommand(key.c_str(), impulsExecute);
|
||||
}
|
||||
|
||||
void impulsExecute() {
|
||||
String key = sCmd.order();
|
||||
String impulsPeriod = sCmd.next();
|
||||
String impulsCount = sCmd.next();
|
||||
|
||||
int number = getKeyNum(key, impuls_KeyList);
|
||||
|
||||
if (myImpulsOut != nullptr) {
|
||||
if (number != -1) {
|
||||
myImpulsOut->at(number).execute(impulsPeriod.toInt(), impulsCount.toInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,92 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableInput
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Clock.h"
|
||||
#include "Global.h"
|
||||
#include "items/vInput.h"
|
||||
|
||||
//this class save date to flash
|
||||
Input::Input(String key, String widget) {
|
||||
_key = key;
|
||||
String value = jsonReadStr(configStoreJson, key);
|
||||
|
||||
if (value == "") {
|
||||
if (widget.indexOf("Digit") != -1) {
|
||||
value = "25";
|
||||
}
|
||||
if (widget.indexOf("Time") != -1) {
|
||||
value = "12:00";
|
||||
}
|
||||
}
|
||||
|
||||
this->execute(value);
|
||||
}
|
||||
Input::~Input() {}
|
||||
|
||||
void Input::execute(String value) {
|
||||
eventGen2(_key, value);
|
||||
jsonWriteStr(configStoreJson, _key, value);
|
||||
saveStore();
|
||||
publishStatus(_key, value);
|
||||
}
|
||||
|
||||
MyInputVector* myInput = nullptr;
|
||||
|
||||
void inputValue() {
|
||||
myLineParsing.update();
|
||||
String widget = myLineParsing.gfile();
|
||||
String key = myLineParsing.gkey();
|
||||
myLineParsing.clear();
|
||||
|
||||
input_EnterCounter++;
|
||||
addKey(key, input_KeyList, input_EnterCounter);
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) myInput = new MyInputVector();
|
||||
firstTime = false;
|
||||
myInput->push_back(Input(key, widget));
|
||||
|
||||
sCmd.addCommand(key.c_str(), inputExecute);
|
||||
}
|
||||
|
||||
void inputExecute() {
|
||||
String key = sCmd.order();
|
||||
String value = sCmd.next();
|
||||
|
||||
if (!isDigitStr(value)) { //если значение - текст
|
||||
if (value.indexOf(":") == -1) { //если этот текст не время
|
||||
if (value.indexOf("++") != -1) { //если тест - инкримент
|
||||
String prevValue = getValue(key);
|
||||
if (prevValue != "no value") {
|
||||
int prevValueInt = prevValue.toInt();
|
||||
prevValueInt++;
|
||||
value = String(prevValueInt);
|
||||
}
|
||||
} else if (value.indexOf("--") != -1) { //если тест - дикремент
|
||||
String prevValue = getValue(key);
|
||||
if (prevValue != "no value") {
|
||||
int prevValueInt = prevValue.toInt();
|
||||
prevValueInt--;
|
||||
value = String(prevValueInt);
|
||||
}
|
||||
} else { //остальные случаи любого текста
|
||||
String valueJson = getValue(value);
|
||||
if (valueJson != "no value") { //если это ключ переменной
|
||||
value = valueJson;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int number = getKeyNum(key, input_KeyList);
|
||||
|
||||
if (myInput != nullptr) {
|
||||
if (number != -1) {
|
||||
myInput->at(number).execute(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,233 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableLogging
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "FileSystem.h"
|
||||
#include "Global.h"
|
||||
#include "items/vLogging.h"
|
||||
|
||||
LoggingClass::LoggingClass(String interval, unsigned int maxPoints, String loggingValueKey, String key, String startState, bool savedFromWeb) {
|
||||
_interval = interval;
|
||||
_maxPoints = maxPoints;
|
||||
_loggingValueKey = loggingValueKey;
|
||||
_key = key;
|
||||
|
||||
if (_interval.indexOf(":") != -1) {
|
||||
_type = 3; //тип 3 логгирование в указанное время
|
||||
_intervalSec = 1000;
|
||||
if (savedFromWeb) { //если было сохранено из веб интерфейса
|
||||
removeFile("/logs/prv" + _key + ".txt");
|
||||
addFile("/logs/prv" + _key + ".txt", startState);
|
||||
}
|
||||
SerialPrint("I", "Logging", "at time (" + _interval + ")");
|
||||
} else {
|
||||
if (_interval.toInt() == 0) {
|
||||
_type = 2; //тип 2 логгирование по событию
|
||||
SerialPrint("I", "Logging", "by event");
|
||||
} else if (_interval.toInt() > 0) {
|
||||
_type = 1; //тип 1 логгирование через период
|
||||
_intervalSec = _interval.toInt() * 1000;
|
||||
SerialPrint("I", "Logging", "periodically (" + _interval + " sec)");
|
||||
}
|
||||
}
|
||||
|
||||
if (_type == 0) SerialPrint("E", "Logging", "type undetermine");
|
||||
}
|
||||
|
||||
LoggingClass::~LoggingClass() {}
|
||||
|
||||
void LoggingClass::loop() {
|
||||
if (_intervalSec > 0) {
|
||||
currentMillis = millis();
|
||||
difference = currentMillis - prevMillis;
|
||||
if (difference >= _intervalSec) {
|
||||
prevMillis = millis();
|
||||
if (_type == 1) {
|
||||
execute("");
|
||||
} else if (_type == 2) {
|
||||
} else if (_type == 3) {
|
||||
String timenow = timeNow->getTimeWOsec();
|
||||
static String prevTime;
|
||||
if (prevTime != timenow) {
|
||||
prevTime = timenow;
|
||||
if (_interval == timenow) execute("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LoggingClass::execute(String keyOrValue) {
|
||||
String loggingValue = "";
|
||||
if (_type == 1) { //тип 1 логгирование через период
|
||||
if (getValue(_loggingValueKey) != "no value") {
|
||||
loggingValue = getValue(_loggingValueKey);
|
||||
} else {
|
||||
SerialPrint("E", "Logging", "'" + _loggingValueKey + "' value not found on this device");
|
||||
}
|
||||
} else if (_type == 2) { //тип 2 логгирование по событию
|
||||
if (isDigitDotCommaStr(keyOrValue)) { //если это число или дробное число
|
||||
loggingValue = keyOrValue;
|
||||
} else { //если это ключ
|
||||
if (getValue(_loggingValueKey) != "no value") {
|
||||
loggingValue = getValue(keyOrValue);
|
||||
} else {
|
||||
SerialPrint("E", "Logging", "This value not found on this device");
|
||||
}
|
||||
}
|
||||
} else if (_type == 3) { //тип 3 логгирование в указанное время
|
||||
if (getValue(_loggingValueKey) != "no value") {
|
||||
float prevValue = readFile("/logs/prv" + _key + ".txt", 100).toFloat();
|
||||
float currentValue = getValue(_loggingValueKey).toFloat();
|
||||
loggingValue = String(currentValue - prevValue);
|
||||
removeFile("/logs/prv" + _key + ".txt");
|
||||
addFile("/logs/prv" + _key + ".txt", String(currentValue));
|
||||
} else {
|
||||
SerialPrint("E", "Logging", "This value not found on this device");
|
||||
}
|
||||
}
|
||||
|
||||
String filename = "/logs/" + _key + ".txt";
|
||||
|
||||
size_t cnt = countLines(filename);
|
||||
size_t sz = getFileSize(filename);
|
||||
|
||||
SerialPrint("I", "Logging", "http://" + WiFi.localIP().toString() + filename + " lines " + String(cnt, DEC) + ", size " + String(sz));
|
||||
|
||||
if ((cnt > _maxPoints + 1)) {
|
||||
removeFile(filename);
|
||||
SerialPrint("E", "Logging", "file been remooved: " + filename + " " + String(cnt) + ">" + String(_maxPoints));
|
||||
cnt = 0;
|
||||
}
|
||||
|
||||
if (loggingValue != "") {
|
||||
if (cnt >= _maxPoints) { //удаляем старую строку и добавляем новую
|
||||
String logData = readFile(filename, 20480); //10240
|
||||
SerialPrint("I", "Logging", "Free heap " + String(ESP.getFreeHeap()));
|
||||
if (logData == "large") {
|
||||
SerialPrint("E", "Logging", "File is very large");
|
||||
}
|
||||
|
||||
logData = deleteBeforeDelimiter(logData, "\r\n");
|
||||
|
||||
if (timeNow->hasTimeSynced()) {
|
||||
logData += timeNow->getTimeUnix() + " " + loggingValue + "\r\n";
|
||||
|
||||
writeFile(filename, logData);
|
||||
}
|
||||
} else { //просто добавляем новую строку
|
||||
if (timeNow->hasTimeSynced()) {
|
||||
addFileLn(filename, timeNow->getTimeUnix() + " " + loggingValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String buf = "{}";
|
||||
jsonWriteInt(buf, "x", timeNow->getTimeUnix().toInt());
|
||||
jsonWriteFloat(buf, "y1", loggingValue.toFloat());
|
||||
buf = "{\"status\":[" + buf + "]}";
|
||||
publishChart(_key, buf);
|
||||
}
|
||||
|
||||
MyLoggingVector* myLogging = nullptr;
|
||||
|
||||
void logging() {
|
||||
myLineParsing.update();
|
||||
String loggingValueKey = myLineParsing.gval();
|
||||
String key = myLineParsing.gkey();
|
||||
String interval = myLineParsing.gint();
|
||||
String maxcnt = myLineParsing.gcnt();
|
||||
String startState = myLineParsing.gstate();
|
||||
myLineParsing.clear();
|
||||
|
||||
logging_KeyList += key + ",";
|
||||
|
||||
logging_EnterCounter++;
|
||||
addKey(key, logging_KeyList, logging_EnterCounter);
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) myLogging = new MyLoggingVector();
|
||||
firstTime = false;
|
||||
myLogging->push_back(LoggingClass(interval, maxcnt.toInt(), loggingValueKey, key, startState, savedFromWeb));
|
||||
|
||||
sCmd.addCommand(key.c_str(), loggingExecute);
|
||||
}
|
||||
|
||||
void loggingExecute() {
|
||||
String key = sCmd.order();
|
||||
String value = sCmd.next();
|
||||
int number = getKeyNum(key, logging_KeyList);
|
||||
if (myLogging != nullptr) {
|
||||
if (number != -1) {
|
||||
myLogging->at(number).execute(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void choose_log_date_and_send() {
|
||||
String all_line = logging_KeyList;
|
||||
while (all_line.length() != 0) {
|
||||
String tmp = selectToMarker(all_line, ",");
|
||||
sendLogData("/logs/" + tmp + ".txt", tmp);
|
||||
all_line = deleteBeforeDelimiter(all_line, ",");
|
||||
}
|
||||
}
|
||||
|
||||
void sendLogData(String file, String topic) {
|
||||
File configFile = FileFS.open(file, "r");
|
||||
if (!configFile) {
|
||||
return;
|
||||
}
|
||||
configFile.seek(0, SeekSet);
|
||||
int i = 0;
|
||||
String buf = "{}";
|
||||
String json_array;
|
||||
String unix_time;
|
||||
String value;
|
||||
unsigned int psn;
|
||||
unsigned int sz = configFile.size();
|
||||
do {
|
||||
i++;
|
||||
psn = configFile.position();
|
||||
String line = configFile.readStringUntil('\n');
|
||||
unix_time = selectToMarker(line, " ");
|
||||
jsonWriteInt(buf, "x", unix_time.toInt());
|
||||
value = deleteBeforeDelimiter(line, " ");
|
||||
jsonWriteFloat(buf, "y1", value.toFloat());
|
||||
if (unix_time != "" || value != "") {
|
||||
json_array += buf + ",";
|
||||
}
|
||||
int grafmax = jsonReadInt(configSetupJson, "grafmax");
|
||||
if (grafmax != 0) {
|
||||
if (i >= grafmax) {
|
||||
json_array = "{\"status\":[" + json_array + "]}";
|
||||
json_array.replace("},]}", "}]}");
|
||||
publishChart(topic, json_array);
|
||||
json_array = "";
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
} while (psn < sz);
|
||||
|
||||
configFile.close();
|
||||
|
||||
json_array = "{\"status\":[" + json_array + "]}";
|
||||
json_array.replace("},]}", "}]}");
|
||||
publishChart(topic, json_array);
|
||||
}
|
||||
|
||||
void cleanLogAndData() {
|
||||
#ifdef ESP8266
|
||||
auto dir = FileFS.openDir("logs");
|
||||
while (dir.next()) {
|
||||
String fname = dir.fileName();
|
||||
SerialPrint("I", "System", fname);
|
||||
removeFile("logs/" + fname);
|
||||
}
|
||||
#endif
|
||||
removeFile("store.json");
|
||||
configStoreJson = "";
|
||||
}
|
||||
#endif
|
||||
@@ -1,58 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableOutput
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Clock.h"
|
||||
#include "Global.h"
|
||||
#include "items/vOutput.h"
|
||||
|
||||
Output::Output(String key) {
|
||||
_key = key;
|
||||
String value = jsonReadStr(configLiveJson, key);
|
||||
this->execute(value);
|
||||
}
|
||||
Output::~Output() {}
|
||||
|
||||
void Output::execute(String value) {
|
||||
eventGen2(_key, value);
|
||||
jsonWriteStr(configLiveJson, _key, value);
|
||||
publishStatus(_key, value);
|
||||
//publishLastUpdateTime(_key, timeNow->getTime());
|
||||
}
|
||||
|
||||
MyOutputVector* myOutput = nullptr;
|
||||
|
||||
void outputValue() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
myLineParsing.clear();
|
||||
|
||||
output_EnterCounter++;
|
||||
addKey(key, output_KeyList, output_EnterCounter);
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) myOutput = new MyOutputVector();
|
||||
firstTime = false;
|
||||
myOutput->push_back(Output(key));
|
||||
|
||||
sCmd.addCommand(key.c_str(), outputExecute);
|
||||
}
|
||||
|
||||
void outputExecute() {
|
||||
String key = sCmd.order();
|
||||
String value = sCmd.next();
|
||||
|
||||
value.replace("#", " ");
|
||||
value.replace("%date%", timeNow->getDateTimeDotFormated());
|
||||
|
||||
int number = getKeyNum(key, output_KeyList);
|
||||
|
||||
if (myOutput != nullptr) {
|
||||
if (number != -1) {
|
||||
myOutput->at(number).execute(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,61 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnablePwmOut
|
||||
#include "items/vPwmOut.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Consts.h"
|
||||
#include "Global.h"
|
||||
|
||||
//this class save data to flash
|
||||
PwmOut::PwmOut(unsigned int pin, String key) {
|
||||
_pin = pin;
|
||||
_key = key;
|
||||
pinMode(_pin, OUTPUT);
|
||||
int state = jsonReadInt(configStoreJson, key);
|
||||
this->execute(String(state));
|
||||
}
|
||||
PwmOut::~PwmOut() {}
|
||||
|
||||
void PwmOut::execute(String state) {
|
||||
analogWrite(_pin, state.toInt());
|
||||
eventGen2(_key, state);
|
||||
jsonWriteInt(configStoreJson, _key, state.toInt());
|
||||
saveStore();
|
||||
publishStatus(_key, state);
|
||||
}
|
||||
|
||||
MyPwmOutVector* myPwmOut = nullptr;
|
||||
|
||||
void pwmOut() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String pin = myLineParsing.gpin();
|
||||
myLineParsing.clear();
|
||||
|
||||
pwmOut_EnterCounter++;
|
||||
addKey(key, pwmOut_KeyList, pwmOut_EnterCounter);
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) myPwmOut = new MyPwmOutVector();
|
||||
firstTime = false;
|
||||
myPwmOut->push_back(PwmOut(pin.toInt(), key));
|
||||
|
||||
sCmd.addCommand(key.c_str(), pwmOutExecute);
|
||||
}
|
||||
|
||||
void pwmOutExecute() {
|
||||
String key = sCmd.order();
|
||||
String state = sCmd.next();
|
||||
|
||||
int number = getKeyNum(key, pwmOut_KeyList);
|
||||
|
||||
if (myPwmOut != nullptr) {
|
||||
if (number != -1) {
|
||||
myPwmOut->at(number).execute(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,72 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorAnalog
|
||||
#include "items/vSensorAnalog.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
#include "BufferExecute.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
SensorAnalog::SensorAnalog(String key, unsigned long interval, unsigned int adcPin, int map1, int map2, int map3, int map4, float c) {
|
||||
_interval = interval * 1000;
|
||||
_key = key;
|
||||
_adcPin = adcPin;
|
||||
|
||||
_map1 = map1;
|
||||
_map2 = map2;
|
||||
_map3 = map3;
|
||||
_map4 = map4;
|
||||
|
||||
_c = c;
|
||||
}
|
||||
|
||||
SensorAnalog::~SensorAnalog() {}
|
||||
|
||||
void SensorAnalog::loop() {
|
||||
currentMillis = millis();
|
||||
difference = currentMillis - prevMillis;
|
||||
if (difference >= _interval) {
|
||||
prevMillis = millis();
|
||||
readAnalog();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorAnalog::readAnalog() {
|
||||
int value;
|
||||
#ifdef ESP32
|
||||
value = analogRead(_adcPin);
|
||||
#endif
|
||||
#ifdef ESP8266
|
||||
value = analogRead(A0);
|
||||
#endif
|
||||
|
||||
value = map(value, _map1, _map2, _map3, _map4);
|
||||
float valueFloat = value * _c;
|
||||
|
||||
eventGen2(_key, String(valueFloat));
|
||||
jsonWriteStr(configLiveJson, _key, String(valueFloat));
|
||||
publishStatus(_key, String(valueFloat));
|
||||
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(valueFloat));
|
||||
}
|
||||
|
||||
MySensorAnalogVector* mySensorAnalog = nullptr;
|
||||
|
||||
void analogAdc() {
|
||||
myLineParsing.update();
|
||||
String interval = myLineParsing.gint();
|
||||
String pin = myLineParsing.gpin();
|
||||
String key = myLineParsing.gkey();
|
||||
String map = myLineParsing.gmap();
|
||||
String c = myLineParsing.gc();
|
||||
myLineParsing.clear();
|
||||
|
||||
int map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
|
||||
int map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
|
||||
int map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
|
||||
int map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorAnalog = new MySensorAnalogVector();
|
||||
firstTime = false;
|
||||
mySensorAnalog->push_back(SensorAnalog(key, interval.toInt(), pin.toInt(), map1, map2, map3, map4, c.toFloat()));
|
||||
}
|
||||
#endif
|
||||
@@ -1,103 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorBme280
|
||||
#include "items/vSensorBme280.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
|
||||
Adafruit_BME280* bme = nullptr;
|
||||
|
||||
SensorBme280::SensorBme280(const paramsBme& paramsTmp, const paramsBme& paramsHum, const paramsBme& paramsPrs) {
|
||||
_paramsTmp = paramsBme(paramsTmp);
|
||||
_paramsHum = paramsBme(paramsHum);
|
||||
_paramsPrs = paramsBme(paramsPrs);
|
||||
|
||||
if (!bme) {
|
||||
bme = new Adafruit_BME280;
|
||||
}
|
||||
|
||||
bme->getTemperatureSensor();
|
||||
bme->getPressureSensor();
|
||||
bme->getHumiditySensor();
|
||||
bme->begin(hexStringToUint8(_paramsPrs.addr));
|
||||
}
|
||||
|
||||
SensorBme280::~SensorBme280() {}
|
||||
|
||||
void SensorBme280::loop() {
|
||||
difference = millis() - prevMillis;
|
||||
if (difference >= _paramsPrs.interval) {
|
||||
prevMillis = millis();
|
||||
read();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorBme280::read() {
|
||||
float tmp = bme->readTemperature();
|
||||
float hum = bme->readHumidity();
|
||||
float prs = bme->readPressure();
|
||||
prs = prs / 1.333224 / 100;
|
||||
|
||||
tmp = tmp * _paramsTmp.c;
|
||||
hum = hum * _paramsHum.c;
|
||||
prs = prs * _paramsPrs.c;
|
||||
|
||||
eventGen2(_paramsTmp.key, String(tmp));
|
||||
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
|
||||
publishStatus(_paramsTmp.key, String(tmp));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
|
||||
|
||||
eventGen2(_paramsHum.key, String(hum));
|
||||
jsonWriteStr(configLiveJson, _paramsHum.key, String(hum));
|
||||
publishStatus(_paramsHum.key, String(hum));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsHum.key + "' data: " + String(hum));
|
||||
|
||||
eventGen2(_paramsPrs.key, String(prs));
|
||||
jsonWriteStr(configLiveJson, _paramsPrs.key, String(prs));
|
||||
publishStatus(_paramsPrs.key, String(prs));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsPrs.key + "' data: " + String(prs));
|
||||
}
|
||||
|
||||
MySensorBme280Vector* mySensorBme280 = nullptr;
|
||||
|
||||
void bme280Sensor() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String addr = myLineParsing.gaddr();
|
||||
String interval = myLineParsing.gint();
|
||||
String c = myLineParsing.gc();
|
||||
myLineParsing.clear();
|
||||
|
||||
static int enterCnt = -1;
|
||||
enterCnt++;
|
||||
|
||||
static paramsBme paramsTmp;
|
||||
static paramsBme paramsHum;
|
||||
static paramsBme paramsPrs;
|
||||
|
||||
if (enterCnt == 0) {
|
||||
paramsTmp.key = key;
|
||||
paramsTmp.c = c.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 1) {
|
||||
paramsHum.key = key;
|
||||
paramsHum.c = c.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 2) {
|
||||
paramsPrs.key = key;
|
||||
paramsPrs.addr = addr;
|
||||
paramsPrs.interval = interval.toInt() * 1000;
|
||||
paramsPrs.c = c.toFloat();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorBme280 = new MySensorBme280Vector();
|
||||
firstTime = false;
|
||||
mySensorBme280->push_back(SensorBme280(paramsTmp, paramsHum, paramsPrs));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,89 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorBmp280
|
||||
#include "items/vSensorBmp280.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
|
||||
Adafruit_BMP280* bmp = nullptr;
|
||||
|
||||
SensorBmp280::SensorBmp280(const paramsBmp& paramsTmp, const paramsBmp& paramsPrs) {
|
||||
_paramsTmp = paramsBmp(paramsTmp);
|
||||
_paramsPrs = paramsBmp(paramsPrs);
|
||||
|
||||
if (!bmp) {
|
||||
bmp = new Adafruit_BMP280;
|
||||
}
|
||||
|
||||
bmp->getTemperatureSensor();
|
||||
bmp->getPressureSensor();
|
||||
bmp->begin(hexStringToUint8(_paramsPrs.addr));
|
||||
}
|
||||
|
||||
SensorBmp280::~SensorBmp280() {}
|
||||
|
||||
void SensorBmp280::loop() {
|
||||
difference = millis() - prevMillis;
|
||||
if (difference >= _paramsPrs.interval) {
|
||||
prevMillis = millis();
|
||||
read();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorBmp280::read() {
|
||||
float tmp = bmp->readTemperature();
|
||||
float prs = bmp->readPressure();
|
||||
prs = prs / 1.333224 / 100;
|
||||
|
||||
tmp = tmp * _paramsTmp.c;
|
||||
prs = prs * _paramsPrs.c;
|
||||
|
||||
eventGen2(_paramsTmp.key, String(tmp));
|
||||
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
|
||||
publishStatus(_paramsTmp.key, String(tmp));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
|
||||
|
||||
eventGen2(_paramsPrs.key, String(prs));
|
||||
jsonWriteStr(configLiveJson, _paramsPrs.key, String(prs));
|
||||
publishStatus(_paramsPrs.key, String(prs));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsPrs.key + "' data: " + String(prs));
|
||||
}
|
||||
|
||||
MySensorBmp280Vector* mySensorBmp280 = nullptr;
|
||||
|
||||
void bmp280Sensor() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String addr = myLineParsing.gaddr();
|
||||
String interval = myLineParsing.gint();
|
||||
String c = myLineParsing.gc();
|
||||
myLineParsing.clear();
|
||||
|
||||
static int enterCnt = -1;
|
||||
enterCnt++;
|
||||
|
||||
static paramsBmp paramsTmp;
|
||||
static paramsBmp paramsHum;
|
||||
static paramsBmp paramsPrs;
|
||||
|
||||
if (enterCnt == 0) {
|
||||
paramsTmp.key = key;
|
||||
paramsTmp.c = c.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 1) {
|
||||
paramsPrs.key = key;
|
||||
paramsPrs.addr = addr;
|
||||
paramsPrs.interval = interval.toInt() * 1000;
|
||||
paramsPrs.c = c.toFloat();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorBmp280 = new MySensorBmp280Vector();
|
||||
firstTime = false;
|
||||
mySensorBmp280->push_back(SensorBmp280(paramsTmp, paramsPrs));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,92 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorCcs811
|
||||
#include "items/vSensorCcs811.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
|
||||
SensorCcs811::SensorCcs811(const paramsCcs811& paramsPpm, const paramsCcs811& paramsPpb) {
|
||||
_paramsPpm = paramsCcs811(paramsPpm);
|
||||
_paramsPpb = paramsCcs811(paramsPpb);
|
||||
|
||||
ccs811 = new Adafruit_CCS811();
|
||||
|
||||
if (!ccs811->begin(hexStringToUint8(_paramsPpb.addr))) SerialPrint("E", "Sensor CCS", "Wire not connected");
|
||||
}
|
||||
|
||||
SensorCcs811::~SensorCcs811() {}
|
||||
|
||||
void SensorCcs811::loop() {
|
||||
difference = millis() - prevMillis;
|
||||
if (difference >= _paramsPpb.interval) {
|
||||
prevMillis = millis();
|
||||
read();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorCcs811::read() {
|
||||
float co2;
|
||||
float ppm;
|
||||
if (ccs811->available()) {
|
||||
if (!ccs811->readData()) {
|
||||
co2 = ccs811->geteCO2();
|
||||
ppm = ccs811->getTVOC();
|
||||
|
||||
co2 = co2 * _paramsPpm.c;
|
||||
ppm = ppm * _paramsPpb.c;
|
||||
|
||||
eventGen2(_paramsPpm.key, String(co2));
|
||||
jsonWriteStr(configLiveJson, _paramsPpm.key, String(co2));
|
||||
publishStatus(_paramsPpm.key, String(co2));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsPpm.key + "' data: " + String(co2));
|
||||
|
||||
eventGen2(_paramsPpb.key, String(ppm));
|
||||
jsonWriteStr(configLiveJson, _paramsPpb.key, String(ppm));
|
||||
publishStatus(_paramsPpb.key, String(ppm));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsPpb.key + "' data: " + String(ppm));
|
||||
} else {
|
||||
SerialPrint("E", "Sensor CCS", "Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MySensorCcs811Vector* mySensorCcs811 = nullptr;
|
||||
|
||||
void ccs811Sensor() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String addr = myLineParsing.gaddr();
|
||||
String interval = myLineParsing.gint();
|
||||
String c = myLineParsing.gc();
|
||||
myLineParsing.clear();
|
||||
|
||||
static int enterCnt = -1;
|
||||
enterCnt++;
|
||||
|
||||
static paramsCcs811 paramsPpm;
|
||||
static paramsCcs811 paramsPpb;
|
||||
|
||||
if (enterCnt == 0) {
|
||||
paramsPpm.key = key;
|
||||
paramsPpm.interval = interval.toInt() * 1000;
|
||||
paramsPpm.c = c.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 1) {
|
||||
paramsPpb.key = key;
|
||||
paramsPpb.addr = addr;
|
||||
paramsPpb.interval = interval.toInt() * 1000;
|
||||
paramsPpb.c = c.toFloat();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorCcs811 = new MySensorCcs811Vector();
|
||||
firstTime = false;
|
||||
mySensorCcs811->push_back(SensorCcs811(paramsPpm, paramsPpb));
|
||||
|
||||
enterCnt = -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,60 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorDallas
|
||||
#include "items/vSensorDallas.h"
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
OneWire* oneWire;
|
||||
DallasTemperature sensors;
|
||||
|
||||
SensorDallas::SensorDallas(unsigned long interval, unsigned int pin, unsigned int index, String key) {
|
||||
_interval = interval * 1000;
|
||||
_key = key;
|
||||
_pin = pin;
|
||||
_index = index;
|
||||
|
||||
oneWire = new OneWire((uint8_t)_pin);
|
||||
sensors.setOneWire(oneWire);
|
||||
sensors.begin();
|
||||
sensors.setResolution(12);
|
||||
}
|
||||
|
||||
SensorDallas::~SensorDallas() {}
|
||||
|
||||
void SensorDallas::loop() {
|
||||
currentMillis = millis();
|
||||
difference = currentMillis - prevMillis;
|
||||
if (difference >= _interval) {
|
||||
prevMillis = millis();
|
||||
readDallas();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorDallas::readDallas() {
|
||||
sensors.requestTemperaturesByIndex(_index);
|
||||
float value = sensors.getTempCByIndex(_index);
|
||||
eventGen2(_key, String(value));
|
||||
jsonWriteStr(configLiveJson, _key, String(value));
|
||||
publishStatus(_key, String(value));
|
||||
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(value));
|
||||
}
|
||||
|
||||
MySensorDallasVector* mySensorDallas2 = nullptr;
|
||||
|
||||
void dallas() {
|
||||
myLineParsing.update();
|
||||
String interval = myLineParsing.gint();
|
||||
String pin = myLineParsing.gpin();
|
||||
String index = myLineParsing.gindex();
|
||||
String key = myLineParsing.gkey();
|
||||
myLineParsing.clear();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorDallas2 = new MySensorDallasVector();
|
||||
firstTime = false;
|
||||
mySensorDallas2->push_back(SensorDallas(interval.toInt(), pin.toInt(), index.toInt(), key));
|
||||
}
|
||||
#endif
|
||||
@@ -1,97 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorDht
|
||||
#include "items/vSensorDht.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
|
||||
SensorDht::SensorDht(const paramsDht& paramsTmp, const paramsDht& paramsHum) {
|
||||
_paramsTmp = paramsDht(paramsTmp);
|
||||
_paramsHum = paramsDht(paramsHum);
|
||||
|
||||
dht = new DHTesp();
|
||||
|
||||
if (_paramsHum.type == "dht11") {
|
||||
dht->setup(_paramsHum.pin, DHTesp::DHT11);
|
||||
} else if (_paramsHum.type == "dht22") {
|
||||
dht->setup(_paramsHum.pin, DHTesp::DHT22);
|
||||
}
|
||||
|
||||
_paramsHum.interval = _paramsHum.interval + dht->getMinimumSamplingPeriod();
|
||||
}
|
||||
|
||||
SensorDht::~SensorDht() {}
|
||||
|
||||
void SensorDht::loop() {
|
||||
difference = millis() - prevMillis;
|
||||
if (difference >= _paramsHum.interval) {
|
||||
prevMillis = millis();
|
||||
readTmpHum();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorDht::readTmpHum() {
|
||||
float tmp = dht->getTemperature();
|
||||
float hum = dht->getHumidity();
|
||||
|
||||
if (String(tmp) != "nan" && String(hum) != "nan") {
|
||||
tmp = tmp * _paramsTmp.c;
|
||||
hum = hum * _paramsHum.c;
|
||||
|
||||
eventGen2(_paramsTmp.key, String(tmp));
|
||||
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
|
||||
publishStatus(_paramsTmp.key, String(tmp));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
|
||||
|
||||
eventGen2(_paramsHum.key, String(hum));
|
||||
jsonWriteStr(configLiveJson, _paramsHum.key, String(hum));
|
||||
publishStatus(_paramsHum.key, String(hum));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsHum.key + "' data: " + String(hum));
|
||||
|
||||
} else {
|
||||
SerialPrint("E", "Sensor DHT", "Error");
|
||||
}
|
||||
}
|
||||
|
||||
MySensorDhtVector* mySensorDht = nullptr;
|
||||
|
||||
void dhtSensor() {
|
||||
myLineParsing.update();
|
||||
String type = myLineParsing.gtype();
|
||||
String interval = myLineParsing.gint();
|
||||
String pin = myLineParsing.gpin();
|
||||
String key = myLineParsing.gkey();
|
||||
String c = myLineParsing.gc();
|
||||
myLineParsing.clear();
|
||||
|
||||
static int enterCnt = -1;
|
||||
enterCnt++;
|
||||
|
||||
static paramsDht paramsTmp;
|
||||
static paramsDht paramsHum;
|
||||
|
||||
if (enterCnt == 0) {
|
||||
paramsTmp.key = key;
|
||||
paramsTmp.interval = interval.toInt() * 1000;
|
||||
paramsTmp.c = c.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 1) {
|
||||
paramsHum.type = type;
|
||||
paramsHum.key = key;
|
||||
paramsHum.interval = interval.toInt() * 1000;
|
||||
paramsHum.pin = pin.toInt();
|
||||
paramsHum.c = c.toFloat();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorDht = new MySensorDhtVector();
|
||||
firstTime = false;
|
||||
mySensorDht->push_back(SensorDht(paramsTmp, paramsHum));
|
||||
|
||||
enterCnt = -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,51 +0,0 @@
|
||||
//#include "items/vSensorImpulsIn.h"
|
||||
//
|
||||
//#include "BufferExecute.h"
|
||||
//#include "Class/LineParsing.h"
|
||||
//#include "Global.h"
|
||||
//#include "SoftUART.h"
|
||||
//
|
||||
//SensorImpulsIn::SensorImpulsIn(const paramsImpulsIn& paramsImpuls) {
|
||||
// _paramsImpuls = paramsImpulsIn(paramsImpuls);
|
||||
// pinMode(14, INPUT);
|
||||
// //pinMode(_paramsImpuls.pin, INPUT_PULLUP);
|
||||
// //attachInterrupt(digitalPinToInterrupt(14), MYinterrupt, CHANGE);
|
||||
//}
|
||||
//
|
||||
//SensorImpulsIn::~SensorImpulsIn() {}
|
||||
//
|
||||
////void SensorImpulsIn::read() {
|
||||
// float voltage; //= (impulsIn->values()->voltage * _paramsV.c) + _paramsV.k;
|
||||
//
|
||||
// eventGen2(_paramsImpuls.key, String(voltage));
|
||||
// jsonWriteStr(configLiveJson, _paramsImpuls.key, String(voltage));
|
||||
// publishStatus(_paramsImpuls.key, String(voltage));
|
||||
// SerialPrint("I", "Sensor", "'" + _paramsImpuls.key + "' data: " + String(voltage));
|
||||
//}
|
||||
//
|
||||
//MySensorImpulsInVector* mySensorImpulsIn = nullptr;
|
||||
//
|
||||
//void impulsInSensor() {
|
||||
// myLineParsing.update();
|
||||
// String key = myLineParsing.gkey();
|
||||
// String pin = myLineParsing.gpin();
|
||||
// String c = myLineParsing.gc();
|
||||
// String k = myLineParsing.gk();
|
||||
// myLineParsing.clear();
|
||||
//
|
||||
// static paramsImpulsIn paramsImpuls;
|
||||
//
|
||||
// paramsImpuls.key = key;
|
||||
// paramsImpuls.pin = pin.toInt();
|
||||
// paramsImpuls.c = c.toFloat();
|
||||
// paramsImpuls.k = k.toFloat();
|
||||
//
|
||||
// static bool firstTime = true;
|
||||
// if (firstTime) mySensorImpulsIn = new MySensorImpulsInVector();
|
||||
// firstTime = false;
|
||||
// mySensorImpulsIn->push_back(SensorImpulsIn(paramsImpuls));
|
||||
//}
|
||||
//
|
||||
//void MYinterrupt() {
|
||||
// Serial.println("interrupt!");
|
||||
//}
|
||||
@@ -1,97 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorNode
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Clock.h"
|
||||
#include "Global.h"
|
||||
#include "Utils/TimeUtils.h"
|
||||
#include "items/vSensorNode.h"
|
||||
|
||||
SensorNode::SensorNode(const paramsSensorNode& params) {
|
||||
_params = paramsSensorNode(params);
|
||||
_updateTime = "";
|
||||
_minutesPassed = 0;
|
||||
}
|
||||
|
||||
SensorNode::~SensorNode() {}
|
||||
|
||||
void SensorNode::loop() {
|
||||
difference = millis() - prevMillis;
|
||||
if (difference >= 60000) {
|
||||
prevMillis = millis();
|
||||
_minutesPassed++;
|
||||
this->publish();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorNode::onChange(String newValue, String incommingKey) {
|
||||
if (_params.key == incommingKey) {
|
||||
_minutesPassed = 0;
|
||||
prevMillis = millis();
|
||||
|
||||
newValue = String(newValue.toFloat() * _params.c);
|
||||
newValue = String(newValue.toFloat() + _params.k);
|
||||
|
||||
eventGen2(_params.key, newValue);
|
||||
jsonWriteStr(configLiveJson, _params.key, newValue);
|
||||
publishStatus(_params.key, newValue);
|
||||
|
||||
_updateTime = timeNow->getDateTimeDotFormated();
|
||||
|
||||
this->publish();
|
||||
//SerialPrint("I", "Sensor", "'" + _params.key + "' data: " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
void SensorNode::publish() {
|
||||
if (_minutesPassed < _params.tm1.toInt()) {
|
||||
publishAnyJsonKey(_params.key, "info", String(_minutesPassed) + " min");
|
||||
publishAnyJsonKey(_params.key, "color", "");
|
||||
} else if (_minutesPassed >= _params.tm1.toInt() && _minutesPassed < _params.tm2.toInt()) {
|
||||
publishAnyJsonKey(_params.key, "info", String(_minutesPassed) + " min");
|
||||
publishAnyJsonKey(_params.key, "color", "orange");
|
||||
} else if (_minutesPassed >= _params.tm2.toInt()) {
|
||||
if (_updateTime == "") {
|
||||
publishAnyJsonKey(_params.key, "info", "offline");
|
||||
} else {
|
||||
publishAnyJsonKey(_params.key, "info", _updateTime);
|
||||
}
|
||||
publishAnyJsonKey(_params.key, "color", "red");
|
||||
}
|
||||
}
|
||||
|
||||
MySensorNodeVector* mySensorNode = nullptr;
|
||||
|
||||
void nodeSensor() {
|
||||
myLineParsing.update();
|
||||
String tm1 = myLineParsing.gtm1();
|
||||
String tm2 = myLineParsing.gtm2();
|
||||
String key = myLineParsing.gkey();
|
||||
String c = myLineParsing.gc();
|
||||
String k = myLineParsing.gk();
|
||||
myLineParsing.clear();
|
||||
|
||||
paramsSensorNode params;
|
||||
|
||||
params.tm1 = tm1;
|
||||
params.tm2 = tm2;
|
||||
params.key = key;
|
||||
params.c = c.toFloat();
|
||||
params.k = k.toFloat();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorNode = new MySensorNodeVector();
|
||||
firstTime = false;
|
||||
mySensorNode->push_back(SensorNode(params));
|
||||
}
|
||||
|
||||
void publishTimes() {
|
||||
if (mySensorNode != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorNode->size(); i++) {
|
||||
mySensorNode->at(i).publish();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,130 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorPzem
|
||||
#include "items/vSensorPzem.h"
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
#include "SoftUART.h"
|
||||
|
||||
SensorPzem::SensorPzem(const paramsPzem& paramsV, const paramsPzem& paramsA, const paramsPzem& paramsWatt, const paramsPzem& paramsWattHrs, const paramsPzem& paramsHz) {
|
||||
_paramsV = paramsPzem(paramsV);
|
||||
_paramsA = paramsPzem(paramsA);
|
||||
_paramsWatt = paramsPzem(paramsWatt);
|
||||
_paramsWattHrs = paramsPzem(paramsWattHrs);
|
||||
_paramsHz = paramsPzem(paramsHz);
|
||||
|
||||
pzem = new PZEMSensor(myUART, hexStringToUint8(_paramsHz.addr));
|
||||
}
|
||||
|
||||
SensorPzem::~SensorPzem() {}
|
||||
|
||||
void SensorPzem::loop() {
|
||||
difference = millis() - prevMillis;
|
||||
if (difference >= _paramsHz.interval) {
|
||||
prevMillis = millis();
|
||||
read();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorPzem::read() {
|
||||
if (myUART) {
|
||||
float voltage = (pzem->values()->voltage * _paramsV.c) + _paramsV.k;
|
||||
float current = (pzem->values()->current * _paramsA.c) + _paramsA.k;
|
||||
float power = (pzem->values()->power * _paramsWatt.c) + _paramsWatt.k;
|
||||
float energy = (pzem->values()->energy * _paramsWattHrs.c) + _paramsWattHrs.k;
|
||||
float freq = (pzem->values()->freq * _paramsHz.c) + _paramsHz.k;
|
||||
|
||||
eventGen2(_paramsV.key, String(voltage));
|
||||
jsonWriteStr(configLiveJson, _paramsV.key, String(voltage));
|
||||
publishStatus(_paramsV.key, String(voltage));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsV.key + "' data: " + String(voltage));
|
||||
|
||||
eventGen2(_paramsA.key, String(current));
|
||||
jsonWriteStr(configLiveJson, _paramsA.key, String(current));
|
||||
publishStatus(_paramsA.key, String(current));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsA.key + "' data: " + String(current));
|
||||
|
||||
eventGen2(_paramsWatt.key, String(power));
|
||||
jsonWriteStr(configLiveJson, _paramsWatt.key, String(power));
|
||||
publishStatus(_paramsWatt.key, String(power));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsWatt.key + "' data: " + String(power));
|
||||
|
||||
eventGen2(_paramsWattHrs.key, String(energy));
|
||||
jsonWriteStr(configLiveJson, _paramsWattHrs.key, String(energy));
|
||||
publishStatus(_paramsWattHrs.key, String(energy));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsWattHrs.key + "' data: " + String(energy));
|
||||
|
||||
eventGen2(_paramsHz.key, String(freq));
|
||||
jsonWriteStr(configLiveJson, _paramsHz.key, String(freq));
|
||||
publishStatus(_paramsHz.key, String(freq));
|
||||
SerialPrint("I", "Sensor", "'" + _paramsHz.key + "' data: " + String(freq));
|
||||
} else {
|
||||
SerialPrint("E", "Sensor PZEM", "Error, UART switched off");
|
||||
}
|
||||
}
|
||||
|
||||
MySensorPzemVector* mySensorPzem = nullptr;
|
||||
|
||||
void pzemSensor() {
|
||||
if (myUART) {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String addr = myLineParsing.gaddr();
|
||||
String interval = myLineParsing.gint();
|
||||
String c = myLineParsing.gc();
|
||||
String k = myLineParsing.gk();
|
||||
myLineParsing.clear();
|
||||
|
||||
static int enterCnt = -1;
|
||||
enterCnt++;
|
||||
|
||||
static paramsPzem paramsV;
|
||||
static paramsPzem paramsA;
|
||||
static paramsPzem paramsWatt;
|
||||
static paramsPzem paramsWattHrs;
|
||||
static paramsPzem paramsHz;
|
||||
|
||||
if (enterCnt == 0) {
|
||||
paramsV.key = key;
|
||||
paramsV.c = c.toFloat();
|
||||
paramsV.k = k.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 1) {
|
||||
paramsA.key = key;
|
||||
paramsA.c = c.toFloat();
|
||||
paramsA.k = k.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 2) {
|
||||
paramsWatt.key = key;
|
||||
paramsWatt.c = c.toFloat();
|
||||
paramsWatt.k = k.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 3) {
|
||||
paramsWattHrs.key = key;
|
||||
paramsWattHrs.c = c.toFloat();
|
||||
paramsWattHrs.k = k.toFloat();
|
||||
}
|
||||
|
||||
if (enterCnt == 4) {
|
||||
paramsHz.key = key;
|
||||
paramsHz.c = c.toFloat();
|
||||
paramsHz.k = k.toFloat();
|
||||
paramsHz.addr = addr;
|
||||
paramsHz.interval = interval.toInt() * 1000;
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorPzem = new MySensorPzemVector();
|
||||
firstTime = false;
|
||||
mySensorPzem->push_back(SensorPzem(paramsV, paramsA, paramsWatt, paramsWattHrs, paramsHz));
|
||||
|
||||
enterCnt = -1;
|
||||
}
|
||||
} else {
|
||||
SerialPrint("E", "Sensor PZEM", "Error, UART switched off");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,92 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorUltrasonic
|
||||
#include "items/vSensorUltrasonic.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
|
||||
GMedian<5, int> testFilter;
|
||||
|
||||
SensorUltrasonic::SensorUltrasonic(String key, unsigned long interval, unsigned int trig, unsigned int echo, int map1, int map2, int map3, int map4, float c) {
|
||||
_interval = interval * 1000;
|
||||
_key = key;
|
||||
_trig = trig;
|
||||
_echo = echo;
|
||||
|
||||
_map1 = map1;
|
||||
_map2 = map2;
|
||||
_map3 = map3;
|
||||
_map4 = map4;
|
||||
|
||||
_c = c;
|
||||
|
||||
pinMode(_trig, OUTPUT);
|
||||
pinMode(_echo, INPUT);
|
||||
}
|
||||
|
||||
SensorUltrasonic::~SensorUltrasonic() {}
|
||||
|
||||
void SensorUltrasonic::loop() {
|
||||
currentMillis = millis();
|
||||
difference = currentMillis - prevMillis;
|
||||
if (difference >= _interval) {
|
||||
prevMillis = millis();
|
||||
readUltrasonic();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorUltrasonic::readUltrasonic() {
|
||||
static unsigned int counter;
|
||||
counter++;
|
||||
|
||||
int value;
|
||||
|
||||
digitalWrite(_trig, LOW);
|
||||
delayMicroseconds(2);
|
||||
digitalWrite(_trig, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(_trig, LOW);
|
||||
long duration_ = pulseIn(_echo, HIGH, 30000); // 3000 µs = 50cm // 30000 µs = 5 m
|
||||
value = duration_ / 29 / 2;
|
||||
|
||||
value = testFilter.filtered(value);
|
||||
|
||||
value = map(value, _map1, _map2, _map3, _map4);
|
||||
float valueFloat = value * _c;
|
||||
|
||||
if (counter > 10) {
|
||||
eventGen2(_key, String(valueFloat));
|
||||
jsonWriteStr(configLiveJson, _key, String(valueFloat));
|
||||
publishStatus(_key, String(valueFloat));
|
||||
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(valueFloat));
|
||||
}
|
||||
}
|
||||
|
||||
MySensorUltrasonicVector* mySensorUltrasonic = nullptr;
|
||||
|
||||
void ultrasonic() {
|
||||
myLineParsing.update();
|
||||
String interval = myLineParsing.gint();
|
||||
String pin = myLineParsing.gpin();
|
||||
String key = myLineParsing.gkey();
|
||||
String map = myLineParsing.gmap();
|
||||
String c = myLineParsing.gc();
|
||||
myLineParsing.clear();
|
||||
|
||||
unsigned int trig = selectFromMarkerToMarker(pin, ",", 0).toInt();
|
||||
unsigned int echo = selectFromMarkerToMarker(pin, ",", 1).toInt();
|
||||
|
||||
int map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
|
||||
int map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
|
||||
int map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
|
||||
int map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorUltrasonic = new MySensorUltrasonicVector();
|
||||
firstTime = false;
|
||||
mySensorUltrasonic->push_back(SensorUltrasonic(key, interval.toInt(), trig, echo, map1, map2, map3, map4, c.toFloat()));
|
||||
}
|
||||
#endif
|
||||
@@ -1,52 +0,0 @@
|
||||
#include "Consts.h"
|
||||
#ifdef EnableSensorUptime
|
||||
#include "items/vSensorUptime.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Class/LineParsing.h"
|
||||
#include "Global.h"
|
||||
|
||||
SensorUptime::SensorUptime(const paramsUptime& paramsUpt) {
|
||||
_paramsUpt = paramsUptime(paramsUpt);
|
||||
}
|
||||
|
||||
SensorUptime::~SensorUptime() {}
|
||||
|
||||
void SensorUptime::loop() {
|
||||
difference = millis() - prevMillis;
|
||||
if (difference >= _paramsUpt.interval) {
|
||||
prevMillis = millis();
|
||||
read();
|
||||
}
|
||||
}
|
||||
|
||||
void SensorUptime::read() {
|
||||
String upt = timeNow->getUptime();
|
||||
|
||||
eventGen2(_paramsUpt.key, upt);
|
||||
jsonWriteStr(configLiveJson, _paramsUpt.key, upt);
|
||||
publishStatus(_paramsUpt.key, upt);
|
||||
SerialPrint("I", "Sensor", "'" + _paramsUpt.key + "' data: " + upt);
|
||||
}
|
||||
|
||||
MySensorUptimeVector* mySensorUptime = nullptr;
|
||||
|
||||
void uptimeSensor() {
|
||||
myLineParsing.update();
|
||||
String key = myLineParsing.gkey();
|
||||
String interval = myLineParsing.gint();
|
||||
myLineParsing.clear();
|
||||
|
||||
static paramsUptime paramsUpt;
|
||||
|
||||
paramsUpt.key = key;
|
||||
paramsUpt.interval = interval.toInt() * 1000;
|
||||
|
||||
static bool firstTime = true;
|
||||
if (firstTime) mySensorUptime = new MySensorUptimeVector();
|
||||
firstTime = false;
|
||||
mySensorUptime->push_back(SensorUptime(paramsUpt));
|
||||
}
|
||||
#endif
|
||||
244
src/main.cpp
244
src/main.cpp
@@ -1,244 +0,0 @@
|
||||
|
||||
#include <SSDP.h>
|
||||
|
||||
#include "BufferExecute.h"
|
||||
#include "Bus.h"
|
||||
#include "Class/CallBackTest.h"
|
||||
#include "Class/NotAsync.h"
|
||||
#include "Class/ScenarioClass3.h"
|
||||
#include "Cmd.h"
|
||||
#include "FileSystem.h"
|
||||
#include "Global.h"
|
||||
#include "Init.h"
|
||||
#include "ItemsList.h"
|
||||
#include "MySensorsDataParse.h"
|
||||
#include "RemoteOrdersUdp.h"
|
||||
#include "SoftUART.h"
|
||||
#include "Telegram.h"
|
||||
#include "Tests.h"
|
||||
#include "Utils/StatUtils.h"
|
||||
#include "Utils/Timings.h"
|
||||
#include "Utils/WebUtils.h"
|
||||
#include "WebServer.h"
|
||||
#include "WebSocket.h"
|
||||
#include "items/ButtonInClass.h"
|
||||
#include "items/vCountDown.h"
|
||||
#include "items/vImpulsOut.h"
|
||||
#include "items/vLogging.h"
|
||||
#include "items/vSensorAnalog.h"
|
||||
#include "items/vSensorBme280.h"
|
||||
#include "items/vSensorBmp280.h"
|
||||
#include "items/vSensorCcs811.h"
|
||||
#include "items/vSensorDallas.h"
|
||||
#include "items/vSensorDht.h"
|
||||
#include "items/vSensorNode.h"
|
||||
#include "items/vSensorPzem.h"
|
||||
#include "items/vSensorUltrasonic.h"
|
||||
#include "items/vSensorUptime.h"
|
||||
//=========================================
|
||||
#include "SetupESP.h"
|
||||
#include "WebServer.h"
|
||||
|
||||
void not_async_actions();
|
||||
|
||||
Timings metric;
|
||||
boolean initialized = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.flush();
|
||||
Serial.println();
|
||||
Serial.println(F("--------------started----------------"));
|
||||
|
||||
myNotAsyncActions = new NotAsync(do_LAST);
|
||||
myScenario = new Scenario();
|
||||
|
||||
wsInit();
|
||||
|
||||
//=========================================initialisation==============================================================
|
||||
setChipId();
|
||||
fileSystemInit();
|
||||
loadConfig();
|
||||
#ifdef EnableUart
|
||||
uartInit();
|
||||
#endif
|
||||
clockInit();
|
||||
timeInit();
|
||||
itemsListInit();
|
||||
espInit();
|
||||
routerConnect();
|
||||
#ifdef EnableTelegram
|
||||
telegramInit();
|
||||
#endif
|
||||
uptime_init();
|
||||
upgradeInit();
|
||||
HttpServerinit();
|
||||
HttpServerinitWS();
|
||||
web_init();
|
||||
initSt();
|
||||
busInit();
|
||||
wifiSignalInit();
|
||||
#ifdef UDP_ENABLED
|
||||
asyncUdpInit();
|
||||
#endif
|
||||
#ifdef SSDP_ENABLED
|
||||
SsdpInit();
|
||||
#endif
|
||||
|
||||
getFSInfo();
|
||||
|
||||
testsPerform();
|
||||
|
||||
just_load = false;
|
||||
initialized = true;
|
||||
|
||||
// const int capacity = JSON_OBJECT_SIZE(35);
|
||||
// SerialPrint("I", F("Test"), String(capacity));
|
||||
|
||||
ts.add(
|
||||
MYTEST, 5000, [&](void*) {
|
||||
// ws.textAll("test msg from esp");
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
// setupESP();
|
||||
|
||||
SerialPrint("I", F("System"), F("✔ Initialization completed"));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
testLoop();
|
||||
|
||||
// if (wsSetupFlag) {
|
||||
// wsSetupFlag = false;
|
||||
// wsSendSetup();
|
||||
//}
|
||||
|
||||
// loopWsExecute();
|
||||
|
||||
#ifdef OTA_UPDATES_ENABLED
|
||||
ArduinoOTA.handle();
|
||||
#endif
|
||||
#ifdef WS_enable
|
||||
ws.cleanupClients();
|
||||
#endif
|
||||
timeNow->loop();
|
||||
mqttLoop();
|
||||
|
||||
myScenario->loop2();
|
||||
loopCmdExecute();
|
||||
|
||||
myNotAsyncActions->loop();
|
||||
ts.update();
|
||||
|
||||
#ifdef EnableTelegram
|
||||
handleTelegram();
|
||||
#endif
|
||||
|
||||
#ifdef EnableUart
|
||||
uartHandle();
|
||||
#endif
|
||||
#ifdef MYSENSORS
|
||||
loopMySensorsExecute();
|
||||
#endif
|
||||
|
||||
#ifdef EnableCountDown
|
||||
if (myCountDown != nullptr) {
|
||||
for (unsigned int i = 0; i < myCountDown->size(); i++) {
|
||||
myCountDown->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableImpulsOut
|
||||
if (myImpulsOut != nullptr) {
|
||||
for (unsigned int i = 0; i < myImpulsOut->size(); i++) {
|
||||
myImpulsOut->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableLogging
|
||||
if (myLogging != nullptr) {
|
||||
for (unsigned int i = 0; i < myLogging->size(); i++) {
|
||||
myLogging->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorDallas
|
||||
if (mySensorDallas2 != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorDallas2->size(); i++) {
|
||||
mySensorDallas2->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorUltrasonic
|
||||
if (mySensorUltrasonic != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorUltrasonic->size(); i++) {
|
||||
mySensorUltrasonic->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EnableSensorAnalog
|
||||
if (mySensorAnalog != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorAnalog->size(); i++) {
|
||||
mySensorAnalog->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorDht
|
||||
if (mySensorDht != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorDht->size(); i++) {
|
||||
mySensorDht->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorBme280
|
||||
if (mySensorBme280 != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorBme280->size(); i++) {
|
||||
mySensorBme280->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorBmp280
|
||||
if (mySensorBmp280 != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorBmp280->size(); i++) {
|
||||
mySensorBmp280->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorCcs811
|
||||
if (mySensorCcs811 != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorCcs811->size(); i++) {
|
||||
mySensorCcs811->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorPzem
|
||||
if (mySensorPzem != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorPzem->size(); i++) {
|
||||
mySensorPzem->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorUptime
|
||||
if (mySensorUptime != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorUptime->size(); i++) {
|
||||
mySensorUptime->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableSensorNode
|
||||
if (mySensorNode != nullptr) {
|
||||
for (unsigned int i = 0; i < mySensorNode->size(); i++) {
|
||||
mySensorNode->at(i).loop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef EnableButtonIn
|
||||
myButtonIn.loop();
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user