добавил сценарии и класс сенсора

This commit is contained in:
Dmitry Borisenko
2022-01-14 21:48:43 +01:00
parent a18552c4f4
commit e6e3497c40
20 changed files with 857 additions and 33 deletions

7
include/Buffers.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#include "Global.h"
#include "MqttClient.h"
void eventGen2(String eventName, String eventValue);
extern void spaceCmdExecute(String &cmdStr);
extern String getValueJson(String &key);

View File

@@ -7,6 +7,7 @@
#include <ArduinoJson.h>
#include <TickerScheduler.h>
#include <PubSubClient.h>
#include <StringCommand.h>
#ifdef ESP32
#include "WiFi.h"
@@ -46,6 +47,7 @@
#include "Utils/FileUtils.h"
#include "Utils/JsonUtils.h"
#include "Utils/SerialPrint.h"
#include "Utils/StringUtils.h"
/*********************************************************************************************************************
*****************************************глобальные объекты классов***************************************************
@@ -54,6 +56,7 @@
extern TickerScheduler ts;
extern WiFiClient espClient;
extern PubSubClient mqtt;
extern StringCommand sCmd;
#ifdef ASYNC_WEB_SERVER
extern AsyncWebServer server;
#endif
@@ -79,6 +82,10 @@ extern String settingsFlashJson;
extern String paramsFlashJson;
extern String paramsHeapJson;
// buf
extern String orderBuf;
extern String eventBuf;
// Mqtt
extern String mqttServer;
extern int mqttPort;

View File

@@ -2,7 +2,6 @@
//
#include "EspFileSystem.h"
#include "Global.h"
#include "Utils/Pretty.h"
#include "Utils/WiFiUtils.h"
#include "AsyncWebServer.h"
#include "StandWebServer.h"

View File

@@ -0,0 +1,26 @@
#pragma once
#include <WString.h>
class IoTSensor {
public:
IoTSensor();
~IoTSensor();
void loop();
virtual void doByInterval();
void init(String key, String id, unsigned long interval);
void regEvent(String value, String consoleInfo);
String getKey();
String getID();
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long difference;
protected:
String _key;
String _id;
unsigned long _interval;
};

View File

@@ -0,0 +1,255 @@
#pragma once
#include "Global.h"
#include "MqttClient.h"
#include "Buffers.h"
class Scenario {
public:
void loop2() {
if (!jsonReadBool(settingsFlashJson, "scen")) {
return;
}
String allBlocks = scenario;
allBlocks += "\n";
String incommingEvent = selectToMarker(eventBuf, ",");
String incommingEventKey = selectToMarker(incommingEvent, " ");
String incommingEventValue = selectToMarkerLast(incommingEvent, " ");
while (allBlocks.length()) {
String oneBlock = selectToMarker(allBlocks, "end\n");
String condition = selectToMarker(oneBlock, "\n");
//логическое и
if (condition.indexOf("&&") != -1) {
condition = condition += " && ";
//посчитаем количество условий
int conditionCnt = itemsCount2(condition, "&&") - 1;
//создадим и заполним динамический массив
bool *arr = new bool[conditionCnt];
for (int i = 0; i < conditionCnt; i++) {
arr[i] = false;
}
//есть ли входящее событие хотя бы в одном из условий и удавлетварено ли оно?
int evenInConditionNum = -1;
for (int i = 0; i < conditionCnt; i++) {
String buf = selectFromMarkerToMarker(condition, " && ", i);
if (isScenarioNeedToDo(buf, incommingEventKey, incommingEventValue, 1)) {
arr[i] = true;
evenInConditionNum = i;
}
}
//если да то проверяем остальные условия по json
if (evenInConditionNum >= 0) {
for (int i = 0; i < conditionCnt; i++) {
String buf = selectFromMarkerToMarker(condition, " && ", i);
if (i != evenInConditionNum) {
if (isScenarioNeedToDoJson(buf)) {
arr[i] = true;
}
}
}
}
//все элементы массива должны быть true
bool result = true;
for (int i = 0; i < conditionCnt; i++) {
if (!arr[i]) {
result = false;
}
}
delete[] arr;
if (result) {
oneBlock = deleteBeforeDelimiter(oneBlock, "\n");
oneBlock.replace("end", "");
// SerialPrint("I", "Event done", incommingEvent);
SerialPrint("I", F("Scenario"), F("All conditions are matched"));
spaceCmdExecute(oneBlock);
}
//логическое или
} else if (condition.indexOf("||") != -1) {
condition = condition += " || ";
//посчитаем количество условий
int conditionCnt = itemsCount2(condition, "||") - 1;
//создадим и заполним динамический массив
bool *arr = new bool[conditionCnt];
for (int i = 0; i < conditionCnt; i++) {
arr[i] = false;
}
//есть ли входящее событие хотя бы в одном из условий и удавлетварено ли оно?
int evenInConditionNum = -1;
for (int i = 0; i < conditionCnt; i++) {
String buf = selectFromMarkerToMarker(condition, " || ", i);
if (isScenarioNeedToDo(buf, incommingEventKey, incommingEventValue, 1)) {
arr[i] = true;
evenInConditionNum = i;
}
}
//если да то проверяем остальные условия по json
if (evenInConditionNum >= 0) {
for (int i = 0; i < conditionCnt; i++) {
String buf = selectFromMarkerToMarker(condition, " || ", i);
if (i != evenInConditionNum) {
if (isScenarioNeedToDoJson(buf)) {
arr[i] = true;
}
}
}
}
//хотя бы один элемент массива должн быть true
bool result = false;
for (int i = 0; i < conditionCnt; i++) {
if (arr[i]) {
result = true;
}
}
delete[] arr;
if (result) {
oneBlock = deleteBeforeDelimiter(oneBlock, "\n");
oneBlock.replace("end", "");
// SerialPrint("I", "Event done", incommingEvent);
SerialPrint("I", F("Scenario"), F("One of all condition are matched"));
spaceCmdExecute(oneBlock);
}
//если гистерезис
} else if (condition.indexOf("+-") != -1) {
if (isScenarioNeedToDo(condition, incommingEventKey, incommingEventValue, 2)) {
oneBlock = deleteBeforeDelimiter(oneBlock, "\n");
oneBlock.replace("end", "");
// SerialPrint("I", "Event done", incommingEvent);
SerialPrint("I", F("Scenario"), "Condition are matched: " + condition);
spaceCmdExecute(oneBlock);
}
//остальные случаи
} else {
if (isScenarioNeedToDo(condition, incommingEventKey, incommingEventValue, 1)) {
oneBlock = deleteBeforeDelimiter(oneBlock, "\n");
oneBlock.replace("end", "");
// SerialPrint("I", "Event done", incommingEvent);
SerialPrint("I", F("Scenario"), "Condition are matched: " + condition);
spaceCmdExecute(oneBlock);
}
}
allBlocks = deleteBeforeDelimiter(allBlocks, "end\n");
}
eventBuf = deleteBeforeDelimiter(eventBuf, ",");
}
private:
bool isScenarioNeedToDo(String &condition, String &incommingEventKey, String &incommingEventValue, int type) {
bool res = false;
String setEventKey = selectFromMarkerToMarker(condition, " ", 0);
if (isEventExist(incommingEventKey, setEventKey)) {
String setEventSign;
String setEventValue;
if (type == 1) preCalculation(condition, setEventSign, setEventValue);
if (type == 2) preCalculationGisteresis(condition, setEventSign, setEventValue);
if (isConditionMatch(setEventSign, incommingEventValue, setEventValue)) {
res = true;
}
}
return res;
}
bool isScenarioNeedToDoJson(String &condition) {
bool res = false;
String setEventKey = selectFromMarkerToMarker(condition, " ", 0);
String setEventSign;
String setEventValue;
preCalculation(condition, setEventSign, setEventValue);
String jsonValue = getValueJson(setEventKey);
if (isConditionMatch(setEventSign, jsonValue, setEventValue)) {
res = true;
}
return res;
}
// bool isScenarioNeedToDoJson2(String &condition, String &incommingEventKey, String &incommingEventValue) {
// bool res = false;
// String setEventKey = selectFromMarkerToMarker(condition, " ", 0);
// if (isEventExist(incommingEventKey, setEventKey)) {
// String setEventSign;
// String setEventValue;
// preCalculation(condition, setEventSign, setEventValue);
// if (isConditionMatch(setEventSign, incommingEventValue, setEventValue)) {
// res = true;
// }
// }
// return res;
// }
void preCalculation(String &condition, String &setEventSign, String &setEventValue) {
setEventSign = selectFromMarkerToMarker(condition, " ", 1);
setEventValue = selectFromMarkerToMarker(condition, " ", 2);
if (!isDigitDotCommaStr(setEventValue)) {
setEventValue = getValueJson(setEventValue);
}
}
void preCalculationGisteresis(String &condition, String &setEventSign, String &setEventValue) {
setEventSign = selectFromMarkerToMarker(condition, " ", 1);
setEventValue = selectFromMarkerToMarker(condition, " ", 2);
if (!isDigitDotCommaStr(setEventValue)) {
String setEventValueName = selectToMarker(setEventValue, "+-");
String gisteresisValue = selectToMarkerLast(setEventValue, "+-");
gisteresisValue.replace("+-", "");
String value = getValueJson(setEventValueName);
String upValue = String(value.toFloat() + gisteresisValue.toFloat());
String lowValue = String(value.toFloat() - gisteresisValue.toFloat());
if (setEventSign == ">") {
setEventValue = upValue;
} else if (setEventSign == "<") {
setEventValue = lowValue;
}
}
}
bool isEventExist(String &incommingEventKey, String &setEventKey) {
bool res = false;
if (incommingEventKey == setEventKey) {
res = true;
}
return res;
}
bool isConditionMatch(String &setEventSign, String &incommingEventValue, String &setEventValue) {
boolean flag = false;
if (setEventSign == "=") {
flag = incommingEventValue == setEventValue;
} else if (setEventSign == "!=") {
flag = incommingEventValue != setEventValue;
} else if (setEventSign == "<") {
flag = incommingEventValue.toFloat() < setEventValue.toFloat();
} else if (setEventSign == ">") {
flag = incommingEventValue.toFloat() > setEventValue.toFloat();
} else if (setEventSign == ">=") {
flag = incommingEventValue.toFloat() >= setEventValue.toFloat();
} else if (setEventSign == "<=") {
flag = incommingEventValue.toFloat() <= setEventValue.toFloat();
}
return flag;
}
};
extern Scenario *myScenario;

View File

@@ -1,4 +0,0 @@
#pragma once
#include "Global.h"
String prettyBytes(size_t size);

View File

@@ -0,0 +1,39 @@
#pragma once
#include "Global.h"
void hex2string(byte array[], unsigned int len, char buffer[]);
int string2hex(const char* str, unsigned char* bytes);
uint8_t hexStringToUint8(String hex);
uint16_t hexStringToUint16(String hex);
String selectToMarkerLast(String str, String found);
String selectToMarker(String str, String found);
String extractInner(String str);
String deleteAfterDelimiter(String str, String found);
String deleteBeforeDelimiter(String str, String found);
String deleteBeforeDelimiterTo(String str, String found);
String deleteToMarkerLast(String str, String found);
String selectFromMarkerToMarker(String str, String found, int number);
size_t itemsCount2(String str, const String& separator);
char* stringToChar(String& str);
size_t itemsCount(String& str, const char* delim);
boolean isDigitStr(const String& str);
boolean isDigitDotCommaStr(const String& str);
String prettyBytes(size_t size);