diff --git a/include/Class/ScenarioClass.h b/include/Class/ScenarioClass.h new file mode 100644 index 00000000..754ddd96 --- /dev/null +++ b/include/Class/ScenarioClass.h @@ -0,0 +1,111 @@ +#pragma once +#include + +#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; \ No newline at end of file diff --git a/include/Global.h b/include/Global.h index d93a5be9..4911fdb2 100644 --- a/include/Global.h +++ b/include/Global.h @@ -80,9 +80,14 @@ extern String configOptionJson; //для трансфера // Mqtt extern String chipId; extern String prex; + extern String all_widgets; + extern String scenario; -extern String order_loop; + +//orders and events +extern String orderBuf; +extern String eventBuf; extern String itemsFile; extern String itemsLine; diff --git a/src/Class/ScenarioClass.cpp b/src/Class/ScenarioClass.cpp new file mode 100644 index 00000000..d066cc62 --- /dev/null +++ b/src/Class/ScenarioClass.cpp @@ -0,0 +1,2 @@ +#include "Class/ScenarioClass.h" +Scenario* myScenario; \ No newline at end of file diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 528403ac..f82b5b95 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -478,9 +478,9 @@ void firmwareVersion() { } void addCommandLoop(const String &cmdStr) { - order_loop += cmdStr; + orderBuf += cmdStr; if (!cmdStr.endsWith(",")) { - order_loop += ","; + orderBuf += ","; } } @@ -515,11 +515,11 @@ void spaceExecute(String &cmdStr) { } void loopCmd() { - if (order_loop.length()) { - String tmp = selectToMarker(order_loop, ","); //выделяем первую команду rel 5 1, + if (orderBuf.length()) { + String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1, pm.info("do: " + tmp); sCmd.readStr(tmp); //выполняем - order_loop = deleteBeforeDelimiter(order_loop, ","); //осекаем + orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем } } diff --git a/src/Global.cpp b/src/Global.cpp index aa11740f..5298c961 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -35,9 +35,14 @@ String configOptionJson = "{}"; // Mqtt String chipId = ""; String prex = ""; + String all_widgets = ""; + String scenario = ""; -String order_loop = ""; + +//orders and events +String orderBuf = ""; +String eventBuf = ""; String itemsFile = ""; String itemsLine = ""; diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 9e6c1b32..e9258605 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -148,15 +148,15 @@ void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) { String key = selectFromMarkerToMarker(topicStr, "/", 3); - order_loop += key; - order_loop += " "; - order_loop += payloadStr; - order_loop += ","; + orderBuf += key; + orderBuf += " "; + orderBuf += payloadStr; + orderBuf += ","; } else if (topicStr.indexOf("order")) { payloadStr.replace("_", " "); - order_loop += payloadStr; - order_loop += ","; + orderBuf += payloadStr; + orderBuf += ","; } else if (topicStr.indexOf("update")) { if (payloadStr == "1") { diff --git a/src/Scenario.cpp b/src/Scenario.cpp index 51ff5875..06bf5fa8 100644 --- a/src/Scenario.cpp +++ b/src/Scenario.cpp @@ -1,88 +1,81 @@ -#include "Global.h" #include "Cmd.h" +#include "Global.h" +#include "Class/ScenarioClass.h" static const char* MODULE = "Scen"; boolean isScenarioEnabled() { - return jsonReadBool(configSetupJson, "scen") && jsonReadStr(configOptionJson, "scenario_status") != ""; + return jsonReadBool(configSetupJson, "scen") && eventBuf != ""; } void loopScenario() { if (!isScenarioEnabled()) { return; } - String str = scenario; - str += "\n"; - str.replace("\r\n", "\n"); - str.replace("\r", "\n"); + String scenarioTmp = scenario; + scenarioTmp += "\n"; + scenarioTmp.replace("\r\n", "\n"); + scenarioTmp.replace("\r", "\n"); - size_t i = 0; - while (str.length()) { - String block = selectToMarker(str, "end"); - if (!block.length()) { + while (scenarioTmp.length()) { + String scenBlok = selectToMarker(scenarioTmp, "end"); //выделяем первый сценарий + if (!scenBlok.length()) { return; } + + size_t i = 0; i++; if (scenario_line_status[i] == 1) { - //выделяем первую строку самого сценария button1 = 1 (условие) - String condition = selectToMarker(block, "\n"); - String param_name = selectFromMarkerToMarker(condition, " ", 0); - String order = jsonReadStr(configOptionJson, "scenario_status"); //читаем весь файл событий - String param = selectToMarker(order, ","); //читаем первое событие из файла событий - if (param_name == param) { //если поступившее событие равно событию заданному buttonSet1 в файле начинаем его обработку - String sign = selectFromMarkerToMarker(condition, " ", 1); //читаем знак (=) - String value = selectFromMarkerToMarker(condition, " ", 2); //читаем значение (1) - if (value.indexOf("digit") != -1) { - // value = add_set(value); - value = jsonReadStr(configLiveJson, value); - } - if (value.indexOf("time") != -1) { - // value = add_set(value); - value = jsonReadStr(configLiveJson, value); - } - // если условие выполнилось, тогда начинаем выполнять комнады + String condition = selectToMarker(scenBlok, "\n"); //выделяем условие + + String conditionParam = selectFromMarkerToMarker(condition, " ", 0); //выделяем параметр условия + String order = eventBuf; + String eventParam = selectToMarker(order, ","); //выделяем параметр события + + if (conditionParam == eventParam) { //если поступившее событие равно событию заданному buttonSet1 в файле начинаем его обработку + + String conditionSign = selectFromMarkerToMarker(condition, " ", 1); //выделяем знак (=) + + String conditionValue = selectFromMarkerToMarker(condition, " ", 2); //выделяем значение (1) + boolean flag = false; - String param = jsonReadStr(configLiveJson, param_name); - if (sign == "=") { - flag = param == value; - } else if (sign == "!=") { - flag = param != value; - } else if (sign == "<") { - flag = param.toInt() < value.toInt(); - } else if (sign == ">") { - flag = param.toInt() > value.toInt(); - } else if (sign == ">=") { - flag = param.toInt() >= value.toInt(); - } else if (sign == "<=") { - flag = param.toInt() <= value.toInt(); + + String eventParam = jsonReadStr(configLiveJson, conditionParam); //получаем значение этого параметра события из json + + 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(); } if (flag) { - // удаляем строку самого сценария оставляя только команды - block = deleteBeforeDelimiter(block, "\n"); - pm.info("do: " + block); - // выполняем все команды - spaceExecute(block); + scenBlok = deleteBeforeDelimiter(scenBlok, "\n"); // удаляем строку самого сценария оставляя только команды + pm.info("do: " + scenBlok); + spaceExecute(scenBlok); // выполняем все команды } } } - str = deleteBeforeDelimiter(str, "end\n"); //удаляем первый сценарий - //----------------------------------------------------------------------------------------------------------------------- + scenarioTmp = deleteBeforeDelimiter(scenarioTmp, "end\n"); //удаляем первый сценарий } - String tmp2 = jsonReadStr(configOptionJson, "scenario_status"); //читаем файл событий - tmp2 = deleteBeforeDelimiter(tmp2, ","); //удаляем выполненное событие - jsonWriteStr(configOptionJson, "scenario_status", tmp2); //записываем обновленный файл событий + + String eventBufTmp = eventBuf; //читаем файл событий + eventBufTmp = deleteBeforeDelimiter(eventBufTmp, ","); //удаляем выполненное событие + eventBuf = eventBufTmp; //записываем обновленный файл событий } -// событие: имя + Set + номер -// button+Set+1 void eventGen(String event_name, String number) { if (!jsonReadBool(configSetupJson, "scen")) { return; } - // генерирование события - String tmp = jsonReadStr(configOptionJson, "scenario_status"); - jsonWriteStr(configOptionJson, "scenario_status", tmp + event_name + number + ","); + eventBuf = event_name + number + ","; } String add_set(String str) { @@ -96,4 +89,9 @@ String add_set(String str) { } } return str; -} \ No newline at end of file +} + +//button-out1 = 1 +//button-out2 1 +//button-out3 1 +//end \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 97f15ff5..a555c761 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include "Class/CallBackTest.h" #include "Class/NotAsinc.h" #include "Class/Switch.h" +#include "Class/ScenarioClass.h" #include "Cmd.h" #include "ItemsList.h" #include "Global.h" @@ -29,6 +30,7 @@ void setup() { setChipId(); myNotAsincActions = new NotAsinc(do_LAST); + myScenario = new Scenario(); pm.info("FS"); fileSystemInit(); @@ -100,7 +102,8 @@ void loop() { timeNow->loop(); MqttClient::loop(); mySwitch->loop(); - loopScenario(); + myScenario->loop(); + //loopScenario(); loopCmd(); loopSerial();