mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
fixed scenario bug
This commit is contained in:
@@ -1,128 +0,0 @@
|
||||
#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{ "" },
|
||||
_eventValue{ "" } {};
|
||||
|
||||
void load() {
|
||||
_scenarioTmp = scenario;
|
||||
}
|
||||
|
||||
void calculate1() {
|
||||
_scenBlok = selectToMarker(_scenarioTmp, "end\n");
|
||||
_condition = selectToMarker(_scenBlok, "\n");
|
||||
_eventParam = selectToMarker(eventBuf, ",");
|
||||
}
|
||||
|
||||
bool isIncommingEventInScenario() {
|
||||
bool ret = false;
|
||||
if (_condition.indexOf(_eventParam) != -1) {
|
||||
ret = true;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void calculate2() {
|
||||
_scenarioTmp += "\n";
|
||||
_scenarioTmp.replace("\r\n", "\n");
|
||||
_scenarioTmp.replace("\r", "\n");
|
||||
|
||||
_conditionParam = selectFromMarkerToMarker(_condition, " ", 0);
|
||||
_conditionSign = selectFromMarkerToMarker(_condition, " ", 1);
|
||||
_conditionValue = selectFromMarkerToMarker(_condition, " ", 2);
|
||||
if (!isDigitStr(_conditionValue)) _conditionValue = jsonReadStr(configLiveJson, _conditionValue);
|
||||
_eventValue = jsonReadStr(configLiveJson, _conditionParam);
|
||||
}
|
||||
|
||||
void delOneScenBlock() {
|
||||
_scenarioTmp = deleteBeforeDelimiter(_scenarioTmp, "end\n");
|
||||
}
|
||||
|
||||
void delOneEvent() {
|
||||
eventBuf = deleteBeforeDelimiter(eventBuf, ",");
|
||||
}
|
||||
|
||||
bool isConditionSatisfied() {
|
||||
boolean flag = false;
|
||||
|
||||
if (_conditionSign == "=") {
|
||||
flag = _eventValue == _conditionValue;
|
||||
}
|
||||
else if (_conditionSign == "!=") {
|
||||
flag = _eventValue != _conditionValue;
|
||||
}
|
||||
else if (_conditionSign == "<") {
|
||||
flag = _eventValue.toFloat() < _conditionValue.toFloat();
|
||||
}
|
||||
else if (_conditionSign == ">") {
|
||||
flag = _eventValue.toFloat() > _conditionValue.toFloat();
|
||||
}
|
||||
else if (_conditionSign == ">=") {
|
||||
flag = _eventValue.toFloat() >= _conditionValue.toFloat();
|
||||
}
|
||||
else if (_conditionSign == "<=") {
|
||||
flag = _eventValue.toFloat() <= _conditionValue.toFloat();
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
//SerialPrint("I", "Scenario", "event value: " + _eventValue);
|
||||
//SerialPrint("I", "Scenario", "condition value: " + _conditionValue);
|
||||
SerialPrint("I", "Scenario", "event: " + _condition);
|
||||
}
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!this->isScenarioEnabled()) {
|
||||
return;
|
||||
}
|
||||
this->load(); //после этого мы получили все сценарии
|
||||
while (_scenarioTmp.length() > 1) {
|
||||
this->calculate1(); //расчет необходимый для ответа на следующий вопрос
|
||||
if (this->isIncommingEventInScenario()) { //если вошедшее событие есть в сценарии
|
||||
this->calculate2();
|
||||
if (this->isConditionSatisfied()) { //если вошедшее событие выполняет условие сценария
|
||||
_scenBlok = deleteBeforeDelimiter(_scenBlok, "\n");
|
||||
String forPrint = _scenBlok;
|
||||
forPrint.replace("end", "");
|
||||
forPrint.replace("\r\n", "");
|
||||
forPrint.replace("\r", "");
|
||||
forPrint.replace("\n", "");
|
||||
SerialPrint("I", "Scenario", "making: " + forPrint);
|
||||
spaceCmdExecute(_scenBlok);
|
||||
}
|
||||
}
|
||||
this->delOneScenBlock(); //удалим использованный блок
|
||||
}
|
||||
this->delOneEvent();
|
||||
}
|
||||
|
||||
bool isScenarioEnabled() {
|
||||
return jsonReadBool(configSetupJson, "scen") && eventBuf != "";
|
||||
}
|
||||
};
|
||||
|
||||
extern Scenario* myScenario;
|
||||
68
include/Class/ScenarioClass2.h
Normal file
68
include/Class/ScenarioClass2.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include "Cmd.h"
|
||||
#include "Global.h"
|
||||
|
||||
class Scenario {
|
||||
|
||||
public:
|
||||
|
||||
void loop() {
|
||||
|
||||
String allBlocks = scenario;
|
||||
allBlocks.replace("\r\n", "\n");
|
||||
allBlocks.replace("\r", "\n");
|
||||
allBlocks += "\n";
|
||||
|
||||
String incommingEvent = selectToMarker(eventBuf, ",");
|
||||
|
||||
while (allBlocks.length() > 1) {
|
||||
String oneBlock = selectToMarker(allBlocks, "end\n");
|
||||
String condition = selectToMarker(oneBlock, "\n");
|
||||
|
||||
String setEvent = selectFromMarkerToMarker(condition, " ", 0);
|
||||
String setEventSign = selectFromMarkerToMarker(condition, " ", 1);
|
||||
String setEventValue = selectFromMarkerToMarker(condition, " ", 2);
|
||||
if (!isDigitStr(setEventValue)) setEventValue = jsonReadStr(configLiveJson, setEventValue);
|
||||
String incommingEventValue = jsonReadStr(configLiveJson, incommingEvent);
|
||||
|
||||
if (incommingEvent == setEvent) {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
//SerialPrint("I", "Scenario", "incomming Event Value: " + incommingEventValue);
|
||||
//SerialPrint("I", "Scenario", "set Event Value: " + setEventValue);
|
||||
|
||||
oneBlock = deleteBeforeDelimiter(oneBlock, "\n");
|
||||
oneBlock.replace("end", "");
|
||||
SerialPrint("I", "Scenario", condition + " set:\n" + oneBlock);
|
||||
spaceCmdExecute(oneBlock);
|
||||
}
|
||||
}
|
||||
allBlocks = deleteBeforeDelimiter(allBlocks, "end\n");
|
||||
}
|
||||
eventBuf = deleteBeforeDelimiter(eventBuf, ",");
|
||||
}
|
||||
};
|
||||
|
||||
extern Scenario* myScenario;
|
||||
Reference in New Issue
Block a user