mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
Scenario class
This commit is contained in:
2
src/Class/ScenarioClass.cpp
Normal file
2
src/Class/ScenarioClass.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "Class/ScenarioClass.h"
|
||||
Scenario* myScenario;
|
||||
10
src/Cmd.cpp
10
src/Cmd.cpp
@@ -478,9 +478,9 @@ void firmwareVersion() {
|
||||
}
|
||||
|
||||
void addCommandLoop(const String &cmdStr) {
|
||||
order_loop += cmdStr;
|
||||
orderBuf += cmdStr;
|
||||
if (!cmdStr.endsWith(",")) {
|
||||
order_loop += ",";
|
||||
orderBuf += ",";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -515,11 +515,11 @@ void spaceExecute(String &cmdStr) {
|
||||
}
|
||||
|
||||
void loopCmd() {
|
||||
if (order_loop.length()) {
|
||||
String tmp = selectToMarker(order_loop, ","); //выделяем первую команду rel 5 1,
|
||||
if (orderBuf.length()) {
|
||||
String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1,
|
||||
pm.info("do: " + tmp);
|
||||
sCmd.readStr(tmp); //выполняем
|
||||
order_loop = deleteBeforeDelimiter(order_loop, ","); //осекаем
|
||||
orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,9 +35,14 @@ String configOptionJson = "{}";
|
||||
// Mqtt
|
||||
String chipId = "";
|
||||
String prex = "";
|
||||
|
||||
String all_widgets = "";
|
||||
|
||||
String scenario = "";
|
||||
String order_loop = "";
|
||||
|
||||
//orders and events
|
||||
String orderBuf = "";
|
||||
String eventBuf = "";
|
||||
|
||||
String itemsFile = "";
|
||||
String itemsLine = "";
|
||||
|
||||
@@ -148,15 +148,15 @@ void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
|
||||
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
|
||||
order_loop += key;
|
||||
order_loop += " ";
|
||||
order_loop += payloadStr;
|
||||
order_loop += ",";
|
||||
orderBuf += key;
|
||||
orderBuf += " ";
|
||||
orderBuf += payloadStr;
|
||||
orderBuf += ",";
|
||||
|
||||
} else if (topicStr.indexOf("order")) {
|
||||
payloadStr.replace("_", " ");
|
||||
order_loop += payloadStr;
|
||||
order_loop += ",";
|
||||
orderBuf += payloadStr;
|
||||
orderBuf += ",";
|
||||
|
||||
} else if (topicStr.indexOf("update")) {
|
||||
if (payloadStr == "1") {
|
||||
|
||||
110
src/Scenario.cpp
110
src/Scenario.cpp
@@ -1,88 +1,81 @@
|
||||
#include "Global.h"
|
||||
#include "Cmd.h"
|
||||
#include "Global.h"
|
||||
#include "Class/ScenarioClass.h"
|
||||
|
||||
static const char* MODULE = "Scen";
|
||||
|
||||
boolean isScenarioEnabled() {
|
||||
return jsonReadBool(configSetupJson, "scen") && jsonReadStr(configOptionJson, "scenario_status") != "";
|
||||
return jsonReadBool(configSetupJson, "scen") && eventBuf != "";
|
||||
}
|
||||
|
||||
void loopScenario() {
|
||||
if (!isScenarioEnabled()) {
|
||||
return;
|
||||
}
|
||||
String str = scenario;
|
||||
str += "\n";
|
||||
str.replace("\r\n", "\n");
|
||||
str.replace("\r", "\n");
|
||||
String scenarioTmp = scenario;
|
||||
scenarioTmp += "\n";
|
||||
scenarioTmp.replace("\r\n", "\n");
|
||||
scenarioTmp.replace("\r", "\n");
|
||||
|
||||
size_t i = 0;
|
||||
while (str.length()) {
|
||||
String block = selectToMarker(str, "end");
|
||||
if (!block.length()) {
|
||||
while (scenarioTmp.length()) {
|
||||
String scenBlok = selectToMarker(scenarioTmp, "end"); //выделяем первый сценарий
|
||||
if (!scenBlok.length()) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
i++;
|
||||
if (scenario_line_status[i] == 1) {
|
||||
//выделяем первую строку самого сценария button1 = 1 (условие)
|
||||
String condition = selectToMarker(block, "\n");
|
||||
String param_name = selectFromMarkerToMarker(condition, " ", 0);
|
||||
String order = jsonReadStr(configOptionJson, "scenario_status"); //читаем весь файл событий
|
||||
String param = selectToMarker(order, ","); //читаем первое событие из файла событий
|
||||
if (param_name == param) { //если поступившее событие равно событию заданному buttonSet1 в файле начинаем его обработку
|
||||
String sign = selectFromMarkerToMarker(condition, " ", 1); //читаем знак (=)
|
||||
String value = selectFromMarkerToMarker(condition, " ", 2); //читаем значение (1)
|
||||
if (value.indexOf("digit") != -1) {
|
||||
// value = add_set(value);
|
||||
value = jsonReadStr(configLiveJson, value);
|
||||
}
|
||||
if (value.indexOf("time") != -1) {
|
||||
// value = add_set(value);
|
||||
value = jsonReadStr(configLiveJson, value);
|
||||
}
|
||||
// если условие выполнилось, тогда начинаем выполнять комнады
|
||||
String condition = selectToMarker(scenBlok, "\n"); //выделяем условие
|
||||
|
||||
String conditionParam = selectFromMarkerToMarker(condition, " ", 0); //выделяем параметр условия
|
||||
String order = eventBuf;
|
||||
String eventParam = selectToMarker(order, ","); //выделяем параметр события
|
||||
|
||||
if (conditionParam == eventParam) { //если поступившее событие равно событию заданному buttonSet1 в файле начинаем его обработку
|
||||
|
||||
String conditionSign = selectFromMarkerToMarker(condition, " ", 1); //выделяем знак (=)
|
||||
|
||||
String conditionValue = selectFromMarkerToMarker(condition, " ", 2); //выделяем значение (1)
|
||||
|
||||
boolean flag = false;
|
||||
String param = jsonReadStr(configLiveJson, param_name);
|
||||
if (sign == "=") {
|
||||
flag = param == value;
|
||||
} else if (sign == "!=") {
|
||||
flag = param != value;
|
||||
} else if (sign == "<") {
|
||||
flag = param.toInt() < value.toInt();
|
||||
} else if (sign == ">") {
|
||||
flag = param.toInt() > value.toInt();
|
||||
} else if (sign == ">=") {
|
||||
flag = param.toInt() >= value.toInt();
|
||||
} else if (sign == "<=") {
|
||||
flag = param.toInt() <= value.toInt();
|
||||
|
||||
String eventParam = jsonReadStr(configLiveJson, conditionParam); //получаем значение этого параметра события из json
|
||||
|
||||
if (conditionSign == "=") {
|
||||
flag = eventParam == conditionValue;
|
||||
} else if (conditionSign == "!=") {
|
||||
flag = eventParam != conditionValue;
|
||||
} else if (conditionSign == "<") {
|
||||
flag = eventParam.toInt() < conditionValue.toInt();
|
||||
} else if (conditionSign == ">") {
|
||||
flag = eventParam.toInt() > conditionValue.toInt();
|
||||
} else if (conditionSign == ">=") {
|
||||
flag = eventParam.toInt() >= conditionValue.toInt();
|
||||
} else if (conditionSign == "<=") {
|
||||
flag = eventParam.toInt() <= conditionValue.toInt();
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
// удаляем строку самого сценария оставляя только команды
|
||||
block = deleteBeforeDelimiter(block, "\n");
|
||||
pm.info("do: " + block);
|
||||
// выполняем все команды
|
||||
spaceExecute(block);
|
||||
scenBlok = deleteBeforeDelimiter(scenBlok, "\n"); // удаляем строку самого сценария оставляя только команды
|
||||
pm.info("do: " + scenBlok);
|
||||
spaceExecute(scenBlok); // выполняем все команды
|
||||
}
|
||||
}
|
||||
}
|
||||
str = deleteBeforeDelimiter(str, "end\n"); //удаляем первый сценарий
|
||||
//-----------------------------------------------------------------------------------------------------------------------
|
||||
scenarioTmp = deleteBeforeDelimiter(scenarioTmp, "end\n"); //удаляем первый сценарий
|
||||
}
|
||||
String tmp2 = jsonReadStr(configOptionJson, "scenario_status"); //читаем файл событий
|
||||
tmp2 = deleteBeforeDelimiter(tmp2, ","); //удаляем выполненное событие
|
||||
jsonWriteStr(configOptionJson, "scenario_status", tmp2); //записываем обновленный файл событий
|
||||
|
||||
String eventBufTmp = eventBuf; //читаем файл событий
|
||||
eventBufTmp = deleteBeforeDelimiter(eventBufTmp, ","); //удаляем выполненное событие
|
||||
eventBuf = eventBufTmp; //записываем обновленный файл событий
|
||||
}
|
||||
|
||||
// событие: имя + Set + номер
|
||||
// button+Set+1
|
||||
void eventGen(String event_name, String number) {
|
||||
if (!jsonReadBool(configSetupJson, "scen")) {
|
||||
return;
|
||||
}
|
||||
// генерирование события
|
||||
String tmp = jsonReadStr(configOptionJson, "scenario_status");
|
||||
jsonWriteStr(configOptionJson, "scenario_status", tmp + event_name + number + ",");
|
||||
eventBuf = event_name + number + ",";
|
||||
}
|
||||
|
||||
String add_set(String str) {
|
||||
@@ -96,4 +89,9 @@ String add_set(String str) {
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
//button-out1 = 1
|
||||
//button-out2 1
|
||||
//button-out3 1
|
||||
//end
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Class/CallBackTest.h"
|
||||
#include "Class/NotAsinc.h"
|
||||
#include "Class/Switch.h"
|
||||
#include "Class/ScenarioClass.h"
|
||||
#include "Cmd.h"
|
||||
#include "ItemsList.h"
|
||||
#include "Global.h"
|
||||
@@ -29,6 +30,7 @@ void setup() {
|
||||
setChipId();
|
||||
|
||||
myNotAsincActions = new NotAsinc(do_LAST);
|
||||
myScenario = new Scenario();
|
||||
|
||||
pm.info("FS");
|
||||
fileSystemInit();
|
||||
@@ -100,7 +102,8 @@ void loop() {
|
||||
timeNow->loop();
|
||||
MqttClient::loop();
|
||||
mySwitch->loop();
|
||||
loopScenario();
|
||||
myScenario->loop();
|
||||
//loopScenario();
|
||||
loopCmd();
|
||||
loopSerial();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user