From 454922b8b48c41aec8f72b9c8e8981be06708116 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Sat, 26 Dec 2020 23:10:07 +0100 Subject: [PATCH] get mqtt remote --- include/Class/ScenarioClass3.h | 28 ++++++----------- src/Class/ScenarioClass3.cpp | 5 ++- src/Init.cpp | 2 -- src/MqttClient.cpp | 23 +++++++++----- src/Web.cpp | 37 ++++++++++++---------- src/items/vLogging.cpp | 56 +++++++++++++++++++--------------- 6 files changed, 81 insertions(+), 70 deletions(-) diff --git a/include/Class/ScenarioClass3.h b/include/Class/ScenarioClass3.h index cc8b0b5b..13937370 100644 --- a/include/Class/ScenarioClass3.h +++ b/include/Class/ScenarioClass3.h @@ -1,12 +1,11 @@ #pragma once #include + #include "Cmd.h" #include "Global.h" class Scenario { - -public: - + public: void loop() { if (!jsonReadBool(configSetupJson, "scen")) { return; @@ -27,7 +26,6 @@ public: String setEventKey = selectFromMarkerToMarker(condition, " ", 0); if (incommingEventKey == setEventKey) { - String setEventSign = selectFromMarkerToMarker(condition, " ", 1); String setEventValue = selectFromMarkerToMarker(condition, " ", 2); @@ -42,13 +40,10 @@ public: if (setEventSign == ">") { setEventValue = upValue; - } - else if (setEventSign == "<") { + } else if (setEventSign == "<") { setEventValue = lowValue; - } - } - else { + } else { setEventValue = getValue(setEventValue); } } @@ -57,20 +52,15 @@ public: if (setEventSign == "=") { flag = incommingEventValue == setEventValue; - } - else if (setEventSign == "!=") { + } else if (setEventSign == "!=") { flag = incommingEventValue != setEventValue; - } - else if (setEventSign == "<") { + } else if (setEventSign == "<") { flag = incommingEventValue.toFloat() < setEventValue.toFloat(); - } - else if (setEventSign == ">") { + } else if (setEventSign == ">") { flag = incommingEventValue.toFloat() > setEventValue.toFloat(); - } - else if (setEventSign == ">=") { + } else if (setEventSign == ">=") { flag = incommingEventValue.toFloat() >= setEventValue.toFloat(); - } - else if (setEventSign == "<=") { + } else if (setEventSign == "<=") { flag = incommingEventValue.toFloat() <= setEventValue.toFloat(); } diff --git a/src/Class/ScenarioClass3.cpp b/src/Class/ScenarioClass3.cpp index b50f3f83..09350105 100644 --- a/src/Class/ScenarioClass3.cpp +++ b/src/Class/ScenarioClass3.cpp @@ -1,4 +1,5 @@ #include "Class/ScenarioClass3.h" + #include "MqttClient.h" #include "RemoteOrdersUdp.h" Scenario* myScenario; @@ -11,7 +12,9 @@ void eventGen2(String eventName, String eventValue) { eventBuf += event; if (jsonReadBool(configSetupJson, "MqttOut")) { - publishEvent(eventName, eventValue); + if (eventName != "timenow") { + publishEvent(eventName, eventValue); + } } } diff --git a/src/Init.cpp b/src/Init.cpp index 32fb4737..074211c2 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -43,7 +43,6 @@ void espInit() { } void deviceInit() { - //======clear dallas params====== if (mySensorDallas2 != nullptr) { mySensorDallas2->clear(); @@ -111,7 +110,6 @@ void deviceInit() { } else { jsonWriteStr(configSetupJson, F("warning3"), ""); } - //outcoming_date(); } //-------------------------------сценарии----------------------------------------------------- diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 610957f1..f5147292 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -1,14 +1,15 @@ #include "MqttClient.h" + #include "BufferExecute.h" -#include "items/vLogging.h" #include "Class/NotAsync.h" #include "Global.h" #include "Init.h" +#include "items/vLogging.h" enum MqttBroker { MQTT_PRIMARY, MQTT_RESERVE }; -MqttBroker activeBroker = MQTT_PRIMARY; +MqttBroker activeBroker = MQTT_PRIMARY; String mqttPrefix; String mqttRootDevice; @@ -110,6 +111,7 @@ void mqttSubscribe() { if (jsonReadBool(configSetupJson, "MqttIn")) { mqtt.subscribe((mqttPrefix + "/+/+/event").c_str()); + mqtt.subscribe((mqttPrefix + "/+/+/order").c_str()); mqtt.subscribe((mqttPrefix + "/+/+/info").c_str()); } } @@ -201,6 +203,17 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) { } } + else if (topicStr.indexOf("order") != -1) { + if (!jsonReadBool(configSetupJson, "MqttIn")) { + return; + } + String devId = selectFromMarkerToMarker(topicStr, "/", 2); + String key = selectFromMarkerToMarker(topicStr, "/", 3); + SerialPrint("I", "=>MQTT", "Received direct order " + key + " " + payloadStr); + String order = key + " " + payloadStr + ","; + orderBuf += order; + } + else if (topicStr.indexOf("info") != -1) { if (topicStr.indexOf("scen") != -1) { writeFile(String(DEVICE_SCENARIO_FILE), payloadStr); @@ -208,12 +221,6 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) { SerialPrint("I", "=>MQTT", "Scenario received"); } } - - //else if (topicStr.indexOf("update")) { - // if (payloadStr == "1") { - // myNotAsyncActions->make(do_UPGRADE); - // } - //} } boolean publish(const String& topic, const String& data) { diff --git a/src/Web.cpp b/src/Web.cpp index b01713ce..ef30e82a 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -1,12 +1,13 @@ #include "Web.h" + #include "Class/NotAsync.h" #include "Global.h" #include "Init.h" #include "ItemsList.h" -#include "items/vLogging.h" -#include "Telegram.h" #include "RemoteOrdersUdp.h" #include "SoftUART.h" +#include "Telegram.h" +#include "items/vLogging.h" bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) { if (request->hasArg("preset")) { @@ -84,7 +85,6 @@ void web_init() { request->send(200); } - if (request->hasArg(F("cleanlog"))) { cleanLogAndData(); request->send(200); @@ -325,8 +325,18 @@ void web_init() { serverIP = jsonReadStr(configSetupJson, "serverip"); request->send(200); } - }); + //set?order=button_1 + if (request->hasArg("order")) { + String order = request->getParam("order")->value(); + order.replace("_"," "); + orderBuf += order + ","; + request->send(200); + } + }); + server.on("/order", HTTP_GET, [](AsyncWebServerRequest* request) { + + }); server.on("/check", HTTP_GET, [](AsyncWebServerRequest* request) { myNotAsyncActions->make(do_GETLASTVERSION); @@ -336,21 +346,16 @@ void web_init() { if (ESP8266_FLASH_SIZE_1MB) { msg = F("Обновление невозможно, память устройства 1 мб"); - } - else { + } else { if (lastVersion == FIRMWARE_VERSION) { msg = F("Актуальная версия прошивки уже установлена."); - } - else if (lastVersion > FIRMWARE_VERSION) { + } else if (lastVersion > FIRMWARE_VERSION) { msg = F("Новая версия прошивкиИдет обновление прошивки, после обновления страница перезагрузится автоматически...')\">Установить"); - } - else if (lastVersion == -1) { + } else if (lastVersion == -1) { msg = F("Cервер не найден. Попробуйте повторить позже..."); - } - else if (lastVersion == -2) { + } else if (lastVersion == -2) { msg = F("Устройство не подключено к роутеру!"); - } - else if (lastVersion < FIRMWARE_VERSION) { + } else if (lastVersion < FIRMWARE_VERSION) { msg = F("Ошибка версии. Попробуйте повторить позже..."); } } @@ -359,7 +364,7 @@ void web_init() { jsonWriteStr(tmp, "title", "" + msg); jsonWriteStr(tmp, "class", "pop-up"); request->send(200, "text/html", tmp); - }); + }); /* * Upgrade @@ -367,7 +372,7 @@ void web_init() { server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest* request) { myNotAsyncActions->make(do_UPGRADE); request->send(200, "text/html"); - }); + }); SerialPrint("I", F("Web"), F("WebAdmin Init")); } diff --git a/src/items/vLogging.cpp b/src/items/vLogging.cpp index 66f71094..3593afa4 100644 --- a/src/items/vLogging.cpp +++ b/src/items/vLogging.cpp @@ -2,11 +2,10 @@ #include -#include "FileSystem.h" - -#include "Class/LineParsing.h" -#include "Global.h" #include "BufferExecute.h" +#include "Class/LineParsing.h" +#include "FileSystem.h" +#include "Global.h" LoggingClass::LoggingClass(unsigned long period, unsigned int maxPoints, String loggingValueKey, String key) { _period = period * 1000; @@ -28,10 +27,25 @@ void LoggingClass::loop() { } } -void LoggingClass::execute(String payload) { +void LoggingClass::execute(String keyOrValue) { + String loggingValue = ""; - if (_period > 0) { - payload = getValue(_loggingValueKey); + if (keyOrValue == "") { //прилетело из лупа + if (getValue(_loggingValueKey) != "no value") { + loggingValue = getValue(_loggingValueKey); + } else { + SerialPrint("E", "Logging", "This value not found on this device"); + } + } else { //прилетело из события + if (isDigitStr(keyOrValue)) { //если это число + loggingValue = keyOrValue; + } else { //если это ключ + if (getValue(_loggingValueKey) != "no value") { + loggingValue = getValue(keyOrValue); + } else { + SerialPrint("E", "Logging", "This value not found on this device"); + } + } } String filename = "logs/" + _key + ".txt"; @@ -46,20 +60,24 @@ void LoggingClass::execute(String payload) { lines_cnt = 0; } - if (payload != "") { - if (lines_cnt > _maxPoints) { + if (loggingValue != "") { + if (lines_cnt > _maxPoints) { //удаляем старую строку и добавляем новую logData = deleteBeforeDelimiter(logData, "\r\n"); if (timeNow->hasTimeSynced()) { - logData += timeNow->getTimeUnix() + " " + payload + "\r\n"; + logData += timeNow->getTimeUnix() + " " + loggingValue + "\r\n"; writeFile(filename, logData); } - } - else { + } else { //просто добавляем новую строку if (timeNow->hasTimeSynced()) { - addFileLn(filename, timeNow->getTimeUnix() + " " + payload); + addFileLn(filename, timeNow->getTimeUnix() + " " + loggingValue); } } } + String buf = "{}"; + jsonWriteInt(buf, "x", timeNow->getTimeUnix().toInt()); + jsonWriteFloat(buf, "y1", loggingValue.toFloat()); + buf = "{\"status\":[" + buf + "]}"; + publishChart(_key, buf); } MyLoggingVector* myLogging = nullptr; @@ -88,13 +106,7 @@ void logging() { void loggingExecute() { String key = sCmd.order(); String value = sCmd.next(); - - if (!isDigitStr(value)) { //если значение - текст - value = getValue(value); - } - int number = getKeyNum(key, logging_KeyList); - if (myLogging != nullptr) { if (number != -1) { myLogging->at(number).execute(value); @@ -102,8 +114,6 @@ void loggingExecute() { } } - - void choose_log_date_and_send() { String all_line = logging_KeyList; while (all_line.length() != 0) { @@ -131,8 +141,7 @@ void sendLogData(String file, String topic) { jsonWriteFloat(buf, "y1", value.toFloat()); if (log_date.length() < 3) { json_array += buf; - } - else { + } else { json_array += buf + ","; } buf = "{}"; @@ -148,7 +157,6 @@ void sendLogData(String file, String topic) { } void cleanLogAndData() { - #ifdef ESP8266 auto dir = FileFS.openDir("logs"); while (dir.next()) {