This commit is contained in:
Dmitry Borisenko
2021-10-05 19:21:52 +08:00
parent 74c31e30ea
commit 421f3fcb9a
348 changed files with 22008 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
#pragma once
#include <Arduino.h>
#include <stdint.h>
#include <functional>
// Декларируем тип - сигнатуру метода , который мы готовы принять в данном случае это
// должен быть метод без результата и без параметров.
// Новый тип мы называем AsynсActionCb - хотя можешь назвать вообще как нравиться а что значит callBack
typedef std::function<void()> AsyncActionCb; //метод без результата и параметров
typedef std::function<bool(const String)> AsyncParamActionCb; //метод без результата и параметров
class CallBackTest {
private:
long count;
AsyncActionCb _cb;
AsyncParamActionCb _pcb;
public:
CallBackTest();
void loop();
void setCallback(AsyncActionCb cb);
void setCallback(AsyncParamActionCb pcb);
};
//extern CallBackTest* CB;

291
include/Class/LineParsing.h Normal file
View File

@@ -0,0 +1,291 @@
#pragma once
#include <Arduino.h>
#include "Global.h"
#include "ItemsList.h"
#include "Utils/JsonUtils.h"
class LineParsing {
protected:
String _key;
String _file;
String _page;
String _descr;
String _order;
String _addr;
String _reg;
String _pin;
String _map;
String _c;
String _k;
String _inv;
String _state;
String _db;
String _type;
String _int;
String _cnt;
String _val;
String _index;
String _tm1;
String _tm2;
int pinErrors;
public:
LineParsing() :
_key{""},
_file{""},
_page{""},
_descr{""},
_order{""},
_addr{""},
_reg{""},
_pin{""},
_map{""},
_c{""},
_k{""},
_inv{""},
_state{""},
_db{""},
_type{""},
_int{""},
_cnt{""},
_val{""},
_index{""},
_tm1{""},
_tm2{""},
pinErrors{0}
{};
void update() {
//String order = sCmd.order();
//SerialPrint("I","module","create '" + order + "'");
for (int i = 1; i < 12; i++) {
if (i == 1) _key = sCmd.next();
if (i == 2) _file = sCmd.next();
if (i == 3) _page = sCmd.next();
if (i == 4) _descr = sCmd.next();
if (i == 5) _order = sCmd.next();
}
for (int i = 1; i < 10; i++) {
String arg = sCmd.next();
if (arg != "") {
if (arg.indexOf("pin[") != -1) {
_pin = extractInner(arg);
}
if (arg.indexOf("inv[") != -1) {
_inv = extractInner(arg);
}
if (arg.indexOf("st[") != -1) {
_state = extractInner(arg);
}
if (arg.indexOf("db[") != -1) {
_db = extractInner(arg);
}
if (arg.indexOf("map[") != -1) {
_map = extractInner(arg);
}
if (arg.indexOf("c[") != -1) {
_c = extractInner(arg);
}
if (arg.indexOf("k[") != -1) {
_k = extractInner(arg);
}
if (arg.indexOf("type[") != -1) {
_type = extractInner(arg);
}
if (arg.indexOf("addr[") != -1) {
_addr = extractInner(arg);
}
if (arg.indexOf("reg[") != -1) {
_reg = extractInner(arg);
}
if (arg.indexOf("int[") != -1) {
_int = extractInner(arg);
}
if (arg.indexOf("cnt[") != -1) {
_cnt = extractInner(arg);
}
if (arg.indexOf("val[") != -1) {
_val = extractInner(arg);
}
if (arg.indexOf("index[") != -1) {
_index = extractInner(arg);
}
if (arg.indexOf("tm1[") != -1) {
_tm1 = extractInner(arg);
}
if (arg.indexOf("tm2[") != -1) {
_tm2 = extractInner(arg);
}
}
}
if (_pin != "") {
if (_pin.indexOf(",") == -1) {
if (!isPinExist(_pin.toInt()) || !isDigitStr(_pin)) {
pinErrors++;
_pin = "";
}
} else {
String pin1 = selectToMarker(_pin, ",");
String pin2 = deleteBeforeDelimiter(_pin, ",");
if (!isPinExist(pin1.toInt()) || !isDigitStr(pin1) || !isPinExist(pin2.toInt()) || !isDigitStr(pin2)) {
pinErrors++;
_pin = "";
}
}
}
_page.replace("#", " ");
_descr.replace("#", " ");
_descr.replace("%ver%", String(FIRMWARE_VERSION));
_descr.replace("%name%", jsonReadStr(configSetupJson, F("name")));
createWidget(_descr, _page, _order, _file, _key);
}
String gkey() {
return _key;
}
String gfile() {
return _file;
}
String gpage() {
return _page;
}
String gdescr() {
return _descr;
}
String gorder() {
return _order;
}
String gpin() {
return _pin;
}
String ginv() {
return _inv;
}
String gstate() {
return _state;
}
String gmap() {
return _map;
}
String gc() {
return _c;
}
String gk() {
return _k;
}
String gtype() {
return _type;
}
String gaddr() {
return _addr;
}
String gregaddr() {
return _reg;
}
String gint() {
return _int;
}
String gcnt() {
return _cnt;
}
String gval() {
return _val;
}
String gindex() {
return _index;
}
String gtm1() {
return _tm1;
}
String gtm2() {
return _tm2;
}
int getPinErrors() {
return pinErrors;
}
void clearErrors() {
pinErrors = 0;
}
void clear() {
_key = "";
_file = "";
_page = "";
_descr = "";
_order = "";
_addr = "";
_reg = "";
_pin = "";
_map = "";
_c = "";
_k = "";
_inv = "";
_state = "";
_db = "";
_type = "";
_int = "";
_cnt = "";
_val = "";
_index = "";
}
String extractInnerDigit(String str) {
int p1 = str.indexOf("[");
int p2 = str.indexOf("]");
return str.substring(p1 + 1, p2);
}
void createWidget(String descr, String page, String order, String filename, String topic) {
if (filename != "na") {
String buf = "{}";
if (!loadWidget(filename, buf)) {
return;
}
if (_cnt != "") {
if (filename.indexOf("chart") != -1) jsonWriteStr(buf, "maxCount", _cnt);
}
#ifdef GATE_MODE
jsonWriteStr(buf, "info", " ");
#endif
jsonWriteStr(buf, "page", page);
jsonWriteStr(buf, "order", order);
jsonWriteStr(buf, "descr", descr);
jsonWriteStr(buf, "topic", prex + "/" + topic);
#ifdef LAYOUT_IN_RAM
all_widgets += widget + "\r\n";
#else
addFileLn("layout.txt", buf);
#endif
}
}
bool loadWidget(const String& filename, String& buf) {
buf = readFile(getWidgetFile(filename), 2048);
bool res = !(buf == "Failed" || buf == "Large");
if (!res) {
SerialPrint("E","module","on load" + filename);
}
return res;
}
const String getWidgetFile(const String& name) {
return "/widgets/" + name + ".json";
}
};
extern LineParsing myLineParsing;

32
include/Class/NotAsync.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <Arduino.h>
#include <stdint.h>
#include <functional>
typedef std::function<void(void*)> NotAsyncCb;
struct NotAsyncItem {
bool test;
NotAsyncCb cb;
void* cb_arg;
volatile bool is_used = false;
};
class NotAsync {
private:
uint8_t size;
uint8_t task = 0;
NotAsyncItem* items = NULL;
void handle(NotAsyncCb f, void* arg);
public:
NotAsync(uint8_t size);
~NotAsync();
void add(uint8_t i, NotAsyncCb, void* arg);
void make(uint8_t task);
void loop();
};
extern NotAsync* myNotAsyncActions;

View File

@@ -0,0 +1,257 @@
#pragma once
#include <Arduino.h>
#include "Cmd.h"
#include "Global.h"
class Scenario {
public:
void loop2() {
if (!jsonReadBool(configSetupJson, "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 = getValue(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 = getValue(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 = getValue(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;
extern void streamEventUDP(String event);