Scenario class in working condition

This commit is contained in:
Dmitry Borisenko
2020-08-29 22:14:35 +03:00
parent 25d58d3471
commit 61f1ad64a1
3 changed files with 48 additions and 54 deletions

View File

@@ -35,20 +35,31 @@ class Scenario {
{}; {};
void load(String& scenario) { void load() {
_scenarioTmp = scenario; _scenarioTmp = scenario;
} }
//button-out1 = 1
//button-out2 1
//button-out3 1
//end
void calculate() { void calculate() {
_scenarioTmp += "\n"; _scenarioTmp += "\n";
_scenarioTmp.replace("\r\n", "\n"); _scenarioTmp.replace("\r\n", "\n");
_scenarioTmp.replace("\r", "\n"); _scenarioTmp.replace("\r", "\n");
_scenBlok = selectToMarker(_scenarioTmp, "end"); _scenBlok = selectToMarker(_scenarioTmp, "end\n");
_condition = selectToMarker(_scenBlok, "\n");
_conditionParam = selectFromMarkerToMarker(_condition, " ", 0); _condition = selectToMarker(_scenBlok, "\n"); //button-out1 = 1
_conditionSign = selectFromMarkerToMarker(_condition, " ", 1);
_conditionValue = selectFromMarkerToMarker(_condition, " ", 2); _conditionParam = selectFromMarkerToMarker(_condition, " ", 0); //button-out1
_conditionSign = selectFromMarkerToMarker(_condition, " ", 1); //=
_conditionValue = selectFromMarkerToMarker(_condition, " ", 2); //1
if (!isDigitStr(_conditionValue)) _conditionValue = jsonReadStr(configLiveJson, _conditionValue);
_eventParam = selectToMarker(eventBuf, ",");
_eventValue = jsonReadStr(configLiveJson, _conditionParam); _eventValue = jsonReadStr(configLiveJson, _conditionParam);
} }
@@ -61,48 +72,48 @@ class Scenario {
eventBuf = deleteBeforeDelimiter(eventBuf, ","); eventBuf = deleteBeforeDelimiter(eventBuf, ",");
} }
bool checkIncommingEvent() { bool isIncommingEventInScenario() {
bool ret = false; bool ret = false;
if (_conditionParam == _eventParam) ret = true; if (_conditionParam == _eventParam) {
ret = true;
//Serial.println("true");
}
return ret; return ret;
} }
bool compare() { bool isConditionSatisfied() {
boolean flag = false; boolean flag = false;
if (_conditionSign == "=") { if (_conditionSign == "=") {
flag = _eventParam == _conditionValue; flag = _eventValue == _conditionValue;
} else if (_conditionSign == "!=") { } else if (_conditionSign == "!=") {
flag = _eventParam != _conditionValue; flag = _eventValue != _conditionValue;
} else if (_conditionSign == "<") { } else if (_conditionSign == "<") {
flag = _eventParam.toInt() < _conditionValue.toInt(); flag = _eventValue.toInt() < _conditionValue.toInt();
} else if (_conditionSign == ">") { } else if (_conditionSign == ">") {
flag = _eventParam.toInt() > _conditionValue.toInt(); flag = _eventValue.toInt() > _conditionValue.toInt();
} else if (_conditionSign == ">=") { } else if (_conditionSign == ">=") {
flag = _eventParam.toInt() >= _conditionValue.toInt(); flag = _eventValue.toInt() >= _conditionValue.toInt();
} else if (_conditionSign == "<=") { } else if (_conditionSign == "<=") {
flag = _eventParam.toInt() <= _conditionValue.toInt(); flag = _eventValue.toInt() <= _conditionValue.toInt();
} }
if (flag) Serial.println("[I] Scenario event: " + _condition);
return flag; return flag;
} }
void loop() { void loop() {
this->load(scenario); //после этого мы получили все сценарии this->load(); //после этого мы получили все сценарии
//Serial.println("loaded"); while (_scenarioTmp.length() > 1) {
this->calculate(); //получаем данные для первого блока
while (_scenarioTmp.length()) { if (this->isIncommingEventInScenario()) { //если вошедшее событие есть в сценарии
if (this->isConditionSatisfied()) { //если вошедшее событие выполняет условие сценария
this->calculate(); //получаем данные для первого блока
if (this->checkIncommingEvent()) {
if (this->compare()) {
_scenBlok = deleteBeforeDelimiter(_scenBlok, "\n"); _scenBlok = deleteBeforeDelimiter(_scenBlok, "\n");
Serial.println("[>] Making: " + _scenBlok);
spaceExecute(_scenBlok); spaceExecute(_scenBlok);
Serial.println(_scenBlok);
} }
} }
this->delOneScenBlock(); //удалим использованный блок this->delOneScenBlock(); //удалим использованный блок
} }
this->delOneEvent(); this->delOneEvent();

View File

@@ -131,12 +131,6 @@ void buttonOut() {
void buttonOutSet() { void buttonOutSet() {
String key = sCmd.order(); String key = sCmd.order();
String state = sCmd.next(); String state = sCmd.next();
Serial.println("==");
Serial.println(key);
Serial.println(state);
Serial.println("==");
String pin = jsonReadStr(configOptionJson, key + "_pin"); String pin = jsonReadStr(configOptionJson, key + "_pin");
String inv = jsonReadStr(configOptionJson, key + "_inv"); String inv = jsonReadStr(configOptionJson, key + "_inv");
if (inv == "") { if (inv == "") {
@@ -230,7 +224,7 @@ void handle_time_init() {
TIME, 1000, [&](void *) { TIME, 1000, [&](void *) {
jsonWriteStr(configLiveJson, "time", timeNow->getTime()); jsonWriteStr(configLiveJson, "time", timeNow->getTime());
jsonWriteStr(configLiveJson, "timenow", timeNow->getTimeJson()); jsonWriteStr(configLiveJson, "timenow", timeNow->getTimeJson());
eventGen("timenow", ""); //eventGen("timenow", "");
}, },
nullptr, true); nullptr, true);
} }

View File

@@ -1,15 +1,16 @@
#include <SSDP.h>
#include "Class/CallBackTest.h" #include "Class/CallBackTest.h"
#include "Class/NotAsinc.h" #include "Class/NotAsinc.h"
#include "Class/Switch.h"
#include "Class/ScenarioClass.h" #include "Class/ScenarioClass.h"
#include "Class/Switch.h"
#include "Cmd.h" #include "Cmd.h"
#include "ItemsList.h"
#include "Global.h" #include "Global.h"
#include "Utils\WebUtils.h"
#include "Init.h" #include "Init.h"
#include "ItemsList.h"
#include "Utils/Timings.h" #include "Utils/Timings.h"
#include <SSDP.h> #include "Utils\WebUtils.h"
void not_async_actions(); void not_async_actions();
@@ -77,8 +78,10 @@ void setup() {
SsdpInit(); SsdpInit();
ts.add( ts.add(
TEST, 1000 * 60, [&](void*) { TEST, 30000, [&](void*) {
pm.info(printMemoryStatus()); //pm.info(printMemoryStatus());
//myScenario->loop();
}, },
nullptr, true); nullptr, true);
@@ -106,23 +109,11 @@ void loop() {
//loopScenario(); //loopScenario();
loopCmd(); loopCmd();
loopSerial(); loopSerial();
myNotAsincActions->loop(); myNotAsincActions->loop();
ts.update(); ts.update();
} }
void clock_init() { void clock_init() {
timeNow = new Clock(); timeNow = new Clock();
timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp")); timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp"));
@@ -134,5 +125,3 @@ void clock_init() {
}, },
nullptr, true); nullptr, true);
} }