Files
IoTManager/include/Class/ScenarioClass.h

112 lines
3.6 KiB
C
Raw Normal View History

2020-08-29 20:17:39 +03:00
#pragma once
#include <Arduino.h>
#include "Cmd.h"
#include "Global.h"
class Scenario {
protected:
String _scenarioTmp;
String _condition;
String _conditionParam;
String _conditionSign;
String _conditionValue;
String _scenBlok;
String _event;
String _eventParam;
String _eventValue;
public:
Scenario() : _scenarioTmp{""},
_condition{""},
_conditionParam{""},
_conditionSign{""},
_conditionValue{""},
_scenBlok{""},
_event{""},
_eventParam{""},
2020-08-29 23:30:04 +03:00
_eventValue{""} {};
2020-08-29 20:17:39 +03:00
2020-08-29 22:14:35 +03:00
void load() {
2020-08-29 20:17:39 +03:00
_scenarioTmp = scenario;
}
2020-08-29 23:02:19 +03:00
void calculate1() {
_scenBlok = selectToMarker(_scenarioTmp, "end\n");
_condition = selectToMarker(_scenBlok, "\n");
_conditionParam = selectFromMarkerToMarker(_condition, " ", 0);
_eventParam = selectToMarker(eventBuf, ",");
}
2020-08-29 22:14:35 +03:00
2020-08-29 23:02:19 +03:00
bool isIncommingEventInScenario() {
bool ret = false;
if (_conditionParam == _eventParam) {
ret = true;
}
return ret;
}
void calculate2() {
2020-08-29 20:17:39 +03:00
_scenarioTmp += "\n";
_scenarioTmp.replace("\r\n", "\n");
_scenarioTmp.replace("\r", "\n");
2020-08-29 23:02:19 +03:00
_conditionSign = selectFromMarkerToMarker(_condition, " ", 1);
_conditionValue = selectFromMarkerToMarker(_condition, " ", 2);
2020-08-29 22:14:35 +03:00
if (!isDigitStr(_conditionValue)) _conditionValue = jsonReadStr(configLiveJson, _conditionValue);
2020-08-29 20:17:39 +03:00
_eventValue = jsonReadStr(configLiveJson, _conditionParam);
}
void delOneScenBlock() {
_scenarioTmp = deleteBeforeDelimiter(_scenarioTmp, "end\n");
}
void delOneEvent() {
eventBuf = deleteBeforeDelimiter(eventBuf, ",");
}
2020-08-29 22:14:35 +03:00
bool isConditionSatisfied() {
2020-08-29 20:17:39 +03:00
boolean flag = false;
if (_conditionSign == "=") {
2020-08-29 22:14:35 +03:00
flag = _eventValue == _conditionValue;
2020-08-29 20:17:39 +03:00
} else if (_conditionSign == "!=") {
2020-08-29 22:14:35 +03:00
flag = _eventValue != _conditionValue;
2020-08-29 20:17:39 +03:00
} else if (_conditionSign == "<") {
2020-08-29 22:14:35 +03:00
flag = _eventValue.toInt() < _conditionValue.toInt();
2020-08-29 20:17:39 +03:00
} else if (_conditionSign == ">") {
2020-08-29 22:14:35 +03:00
flag = _eventValue.toInt() > _conditionValue.toInt();
2020-08-29 20:17:39 +03:00
} else if (_conditionSign == ">=") {
2020-08-29 22:14:35 +03:00
flag = _eventValue.toInt() >= _conditionValue.toInt();
2020-08-29 20:17:39 +03:00
} else if (_conditionSign == "<=") {
2020-08-29 22:14:35 +03:00
flag = _eventValue.toInt() <= _conditionValue.toInt();
2020-08-29 20:17:39 +03:00
}
2020-08-29 22:14:35 +03:00
if (flag) Serial.println("[I] Scenario event: " + _condition);
2020-08-29 20:17:39 +03:00
return flag;
}
void loop() {
2020-08-29 23:30:04 +03:00
if (!this->isScenarioEnabled()) {
return;
}
2020-08-29 22:14:35 +03:00
this->load(); //после этого мы получили все сценарии
while (_scenarioTmp.length() > 1) {
2020-08-29 23:30:04 +03:00
this->calculate1();
2020-08-29 22:14:35 +03:00
if (this->isIncommingEventInScenario()) { //если вошедшее событие есть в сценарии
2020-08-29 23:02:19 +03:00
this->calculate2();
if (this->isConditionSatisfied()) { //если вошедшее событие выполняет условие сценария
2020-08-29 20:17:39 +03:00
_scenBlok = deleteBeforeDelimiter(_scenBlok, "\n");
2020-08-29 23:02:19 +03:00
//Serial.println(" [>] Making: " + _scenBlok);
2020-08-29 20:17:39 +03:00
spaceExecute(_scenBlok);
}
}
this->delOneScenBlock(); //удалим использованный блок
}
this->delOneEvent();
}
2020-08-29 23:30:04 +03:00
bool isScenarioEnabled() {
return jsonReadBool(configSetupJson, "scen") && eventBuf != "";
}
2020-08-29 20:17:39 +03:00
};
extern Scenario* myScenario;