Scenario class

This commit is contained in:
Dmitry Borisenko
2020-08-29 20:17:39 +03:00
parent b1ca2ad511
commit 25d58d3471
8 changed files with 194 additions and 70 deletions

View File

@@ -0,0 +1,111 @@
#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(String& scenario) {
_scenarioTmp = scenario;
}
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);
_eventValue = jsonReadStr(configLiveJson, _conditionParam);
}
void delOneScenBlock() {
_scenarioTmp = deleteBeforeDelimiter(_scenarioTmp, "end\n");
//Serial.println(_scenarioTmp);
}
void delOneEvent() {
eventBuf = deleteBeforeDelimiter(eventBuf, ",");
}
bool checkIncommingEvent() {
bool ret = false;
if (_conditionParam == _eventParam) ret = true;
return ret;
}
bool compare() {
boolean flag = false;
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();
}
return flag;
}
void loop() {
this->load(scenario); //после этого мы получили все сценарии
//Serial.println("loaded");
while (_scenarioTmp.length()) {
this->calculate(); //получаем данные для первого блока
if (this->checkIncommingEvent()) {
if (this->compare()) {
_scenBlok = deleteBeforeDelimiter(_scenBlok, "\n");
spaceExecute(_scenBlok);
Serial.println(_scenBlok);
}
}
this->delOneScenBlock(); //удалим использованный блок
}
this->delOneEvent();
}
};
extern Scenario* myScenario;

View File

@@ -80,9 +80,14 @@ extern String configOptionJson; //для трансфера
// Mqtt // Mqtt
extern String chipId; extern String chipId;
extern String prex; extern String prex;
extern String all_widgets; extern String all_widgets;
extern String scenario; extern String scenario;
extern String order_loop;
//orders and events
extern String orderBuf;
extern String eventBuf;
extern String itemsFile; extern String itemsFile;
extern String itemsLine; extern String itemsLine;

View File

@@ -0,0 +1,2 @@
#include "Class/ScenarioClass.h"
Scenario* myScenario;

View File

@@ -478,9 +478,9 @@ void firmwareVersion() {
} }
void addCommandLoop(const String &cmdStr) { void addCommandLoop(const String &cmdStr) {
order_loop += cmdStr; orderBuf += cmdStr;
if (!cmdStr.endsWith(",")) { if (!cmdStr.endsWith(",")) {
order_loop += ","; orderBuf += ",";
} }
} }
@@ -515,11 +515,11 @@ void spaceExecute(String &cmdStr) {
} }
void loopCmd() { void loopCmd() {
if (order_loop.length()) { if (orderBuf.length()) {
String tmp = selectToMarker(order_loop, ","); //выделяем первую команду rel 5 1, String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1,
pm.info("do: " + tmp); pm.info("do: " + tmp);
sCmd.readStr(tmp); //выполняем sCmd.readStr(tmp); //выполняем
order_loop = deleteBeforeDelimiter(order_loop, ","); //осекаем orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем
} }
} }

View File

@@ -35,9 +35,14 @@ String configOptionJson = "{}";
// Mqtt // Mqtt
String chipId = ""; String chipId = "";
String prex = ""; String prex = "";
String all_widgets = ""; String all_widgets = "";
String scenario = ""; String scenario = "";
String order_loop = "";
//orders and events
String orderBuf = "";
String eventBuf = "";
String itemsFile = ""; String itemsFile = "";
String itemsLine = ""; String itemsLine = "";

View File

@@ -148,15 +148,15 @@ void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
String key = selectFromMarkerToMarker(topicStr, "/", 3); String key = selectFromMarkerToMarker(topicStr, "/", 3);
order_loop += key; orderBuf += key;
order_loop += " "; orderBuf += " ";
order_loop += payloadStr; orderBuf += payloadStr;
order_loop += ","; orderBuf += ",";
} else if (topicStr.indexOf("order")) { } else if (topicStr.indexOf("order")) {
payloadStr.replace("_", " "); payloadStr.replace("_", " ");
order_loop += payloadStr; orderBuf += payloadStr;
order_loop += ","; orderBuf += ",";
} else if (topicStr.indexOf("update")) { } else if (topicStr.indexOf("update")) {
if (payloadStr == "1") { if (payloadStr == "1") {

View File

@@ -1,88 +1,81 @@
#include "Global.h"
#include "Cmd.h" #include "Cmd.h"
#include "Global.h"
#include "Class/ScenarioClass.h"
static const char* MODULE = "Scen"; static const char* MODULE = "Scen";
boolean isScenarioEnabled() { boolean isScenarioEnabled() {
return jsonReadBool(configSetupJson, "scen") && jsonReadStr(configOptionJson, "scenario_status") != ""; return jsonReadBool(configSetupJson, "scen") && eventBuf != "";
} }
void loopScenario() { void loopScenario() {
if (!isScenarioEnabled()) { if (!isScenarioEnabled()) {
return; return;
} }
String str = scenario; String scenarioTmp = scenario;
str += "\n"; scenarioTmp += "\n";
str.replace("\r\n", "\n"); scenarioTmp.replace("\r\n", "\n");
str.replace("\r", "\n"); scenarioTmp.replace("\r", "\n");
size_t i = 0; while (scenarioTmp.length()) {
while (str.length()) { String scenBlok = selectToMarker(scenarioTmp, "end"); //выделяем первый сценарий
String block = selectToMarker(str, "end"); if (!scenBlok.length()) {
if (!block.length()) {
return; return;
} }
size_t i = 0;
i++; i++;
if (scenario_line_status[i] == 1) { if (scenario_line_status[i] == 1) {
//выделяем первую строку самого сценария button1 = 1 (условие) String condition = selectToMarker(scenBlok, "\n"); //выделяем условие
String condition = selectToMarker(block, "\n");
String param_name = selectFromMarkerToMarker(condition, " ", 0); String conditionParam = selectFromMarkerToMarker(condition, " ", 0); //выделяем параметр условия
String order = jsonReadStr(configOptionJson, "scenario_status"); //читаем весь файл событий String order = eventBuf;
String param = selectToMarker(order, ","); //читаем первое событие из файла событий String eventParam = selectToMarker(order, ","); //выделяем параметр события
if (param_name == param) { //если поступившее событие равно событию заданному buttonSet1 в файле начинаем его обработку
String sign = selectFromMarkerToMarker(condition, " ", 1); //читаем знак (=) if (conditionParam == eventParam) { //если поступившее событие равно событию заданному buttonSet1 в файле начинаем его обработку
String value = selectFromMarkerToMarker(condition, " ", 2); //читаем значение (1)
if (value.indexOf("digit") != -1) { String conditionSign = selectFromMarkerToMarker(condition, " ", 1); //выделяем знак (=)
// value = add_set(value);
value = jsonReadStr(configLiveJson, value); String conditionValue = selectFromMarkerToMarker(condition, " ", 2); //выделяем значение (1)
}
if (value.indexOf("time") != -1) {
// value = add_set(value);
value = jsonReadStr(configLiveJson, value);
}
// если условие выполнилось, тогда начинаем выполнять комнады
boolean flag = false; boolean flag = false;
String param = jsonReadStr(configLiveJson, param_name);
if (sign == "=") { String eventParam = jsonReadStr(configLiveJson, conditionParam); //получаем значение этого параметра события из json
flag = param == value;
} else if (sign == "!=") { if (conditionSign == "=") {
flag = param != value; flag = eventParam == conditionValue;
} else if (sign == "<") { } else if (conditionSign == "!=") {
flag = param.toInt() < value.toInt(); flag = eventParam != conditionValue;
} else if (sign == ">") { } else if (conditionSign == "<") {
flag = param.toInt() > value.toInt(); flag = eventParam.toInt() < conditionValue.toInt();
} else if (sign == ">=") { } else if (conditionSign == ">") {
flag = param.toInt() >= value.toInt(); flag = eventParam.toInt() > conditionValue.toInt();
} else if (sign == "<=") { } else if (conditionSign == ">=") {
flag = param.toInt() <= value.toInt(); flag = eventParam.toInt() >= conditionValue.toInt();
} else if (conditionSign == "<=") {
flag = eventParam.toInt() <= conditionValue.toInt();
} }
if (flag) { if (flag) {
// удаляем строку самого сценария оставляя только команды scenBlok = deleteBeforeDelimiter(scenBlok, "\n"); // удаляем строку самого сценария оставляя только команды
block = deleteBeforeDelimiter(block, "\n"); pm.info("do: " + scenBlok);
pm.info("do: " + block); spaceExecute(scenBlok); // выполняем все команды
// выполняем все команды
spaceExecute(block);
} }
} }
} }
str = deleteBeforeDelimiter(str, "end\n"); //удаляем первый сценарий scenarioTmp = deleteBeforeDelimiter(scenarioTmp, "end\n"); //удаляем первый сценарий
//-----------------------------------------------------------------------------------------------------------------------
} }
String tmp2 = jsonReadStr(configOptionJson, "scenario_status"); //читаем файл событий
tmp2 = deleteBeforeDelimiter(tmp2, ","); //удаляем выполненное событие String eventBufTmp = eventBuf; //читаем файл событий
jsonWriteStr(configOptionJson, "scenario_status", tmp2); //записываем обновленный файл событий eventBufTmp = deleteBeforeDelimiter(eventBufTmp, ","); //удаляем выполненное событие
eventBuf = eventBufTmp; //записываем обновленный файл событий
} }
// событие: имя + Set + номер
// button+Set+1
void eventGen(String event_name, String number) { void eventGen(String event_name, String number) {
if (!jsonReadBool(configSetupJson, "scen")) { if (!jsonReadBool(configSetupJson, "scen")) {
return; return;
} }
// генерирование события eventBuf = event_name + number + ",";
String tmp = jsonReadStr(configOptionJson, "scenario_status");
jsonWriteStr(configOptionJson, "scenario_status", tmp + event_name + number + ",");
} }
String add_set(String str) { String add_set(String str) {
@@ -97,3 +90,8 @@ String add_set(String str) {
} }
return str; return str;
} }
//button-out1 = 1
//button-out2 1
//button-out3 1
//end

View File

@@ -2,6 +2,7 @@
#include "Class/CallBackTest.h" #include "Class/CallBackTest.h"
#include "Class/NotAsinc.h" #include "Class/NotAsinc.h"
#include "Class/Switch.h" #include "Class/Switch.h"
#include "Class/ScenarioClass.h"
#include "Cmd.h" #include "Cmd.h"
#include "ItemsList.h" #include "ItemsList.h"
#include "Global.h" #include "Global.h"
@@ -29,6 +30,7 @@ void setup() {
setChipId(); setChipId();
myNotAsincActions = new NotAsinc(do_LAST); myNotAsincActions = new NotAsinc(do_LAST);
myScenario = new Scenario();
pm.info("FS"); pm.info("FS");
fileSystemInit(); fileSystemInit();
@@ -100,7 +102,8 @@ void loop() {
timeNow->loop(); timeNow->loop();
MqttClient::loop(); MqttClient::loop();
mySwitch->loop(); mySwitch->loop();
loopScenario(); myScenario->loop();
//loopScenario();
loopCmd(); loopCmd();
loopSerial(); loopSerial();