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

View File

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

View File

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