From 55c0cfd718195662f79b2c27c25caa8b05927909 Mon Sep 17 00:00:00 2001 From: biver Date: Sat, 22 Oct 2022 07:59:26 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9A=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D1=83=D0=B5=D0=BC=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D1=82=D0=BC=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D1=81=20=D0=BD=D0=B0=D1=81=D0=BB=D0=B5=D0=B4=D0=BD=D1=8B=D0=BC?= =?UTF-8?q?=D0=B8=20setValue=20=D0=B2=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB?= =?UTF-8?q?=D1=8F=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/display/Ws2812b/Ws2181b.cpp | 2 +- src/modules/exec/ButtonIn/ButtonIn.cpp | 2 +- src/modules/exec/ButtonOut/ButtonOut.cpp | 2 +- src/modules/exec/IoTServo/IoTServo.cpp | 2 +- src/modules/exec/Pwm32/Pwm32.cpp | 2 +- src/modules/exec/Pwm8266/Pwm8266.cpp | 2 +- src/modules/virtual/Loging/Loging.cpp | 2 +- src/modules/virtual/VButton/VButton.cpp | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/modules/display/Ws2812b/Ws2181b.cpp b/src/modules/display/Ws2812b/Ws2181b.cpp index 8cd90a29..9ddac234 100644 --- a/src/modules/display/Ws2812b/Ws2181b.cpp +++ b/src/modules/display/Ws2812b/Ws2181b.cpp @@ -178,7 +178,7 @@ public: int b = map(value.valD, 1,1024,1,255); _strip->setBrightness(b); _strip->show(); - regEvent(value.valD, "Ws2812b"); + if (generateEvent) regEvent(value.valD, "Ws2812b"); } ~Ws2812b(){}; diff --git a/src/modules/exec/ButtonIn/ButtonIn.cpp b/src/modules/exec/ButtonIn/ButtonIn.cpp index 6d2fe354..e968eb18 100644 --- a/src/modules/exec/ButtonIn/ButtonIn.cpp +++ b/src/modules/exec/ButtonIn/ButtonIn.cpp @@ -70,7 +70,7 @@ class ButtonIn : public IoTItem { void setValue(IoTValue Value, bool generateEvent = true) { value = Value; - regEvent((String)(int)value.valD, "ButtonIn"); + if (generateEvent) regEvent((String)(int)value.valD, "ButtonIn"); } String getValue() { diff --git a/src/modules/exec/ButtonOut/ButtonOut.cpp b/src/modules/exec/ButtonOut/ButtonOut.cpp index cce94d06..d041f748 100644 --- a/src/modules/exec/ButtonOut/ButtonOut.cpp +++ b/src/modules/exec/ButtonOut/ButtonOut.cpp @@ -40,7 +40,7 @@ class ButtonOut : public IoTItem { void setValue(IoTValue Value, bool generateEvent = true) { value = Value; IoTgpio.digitalWrite(_pin, _inv?!value.valD:value.valD); - regEvent((String)(int)value.valD, "ButtonOut"); + if (generateEvent) regEvent((String)(int)value.valD, "ButtonOut"); } String getValue() { diff --git a/src/modules/exec/IoTServo/IoTServo.cpp b/src/modules/exec/IoTServo/IoTServo.cpp index 00c68f23..d995918b 100644 --- a/src/modules/exec/IoTServo/IoTServo.cpp +++ b/src/modules/exec/IoTServo/IoTServo.cpp @@ -57,7 +57,7 @@ class IoTServo : public IoTItem { if (value.isDecimal & (_oldValue != value.valD)) { _oldValue = value.valD; servObj.write(_oldValue); - regEvent(value.valD, "IoTServo"); + if (generateEvent) regEvent(value.valD, "IoTServo"); } } diff --git a/src/modules/exec/Pwm32/Pwm32.cpp b/src/modules/exec/Pwm32/Pwm32.cpp index f6351c47..6ad209e8 100644 --- a/src/modules/exec/Pwm32/Pwm32.cpp +++ b/src/modules/exec/Pwm32/Pwm32.cpp @@ -57,7 +57,7 @@ class Pwm32 : public IoTItem { void setValue(IoTValue Value, bool generateEvent = true) { value = Value; ledcWrite(_ledChannel, value.valD); - regEvent(value.valD, "Pwm32"); + if (generateEvent) regEvent(value.valD, "Pwm32"); } //======================================================================================================= diff --git a/src/modules/exec/Pwm8266/Pwm8266.cpp b/src/modules/exec/Pwm8266/Pwm8266.cpp index e52ab9a9..73c4493d 100644 --- a/src/modules/exec/Pwm8266/Pwm8266.cpp +++ b/src/modules/exec/Pwm8266/Pwm8266.cpp @@ -47,7 +47,7 @@ class Pwm8266 : public IoTItem { void setValue(IoTValue Value, bool generateEvent = true) { value = Value; IoTgpio.analogWrite(_pin, value.valD); - regEvent(value.valD, "Pwm8266"); + if (generateEvent) regEvent(value.valD, "Pwm8266"); } //======================================================================================================= diff --git a/src/modules/virtual/Loging/Loging.cpp b/src/modules/virtual/Loging/Loging.cpp index 11193b85..d9ccb976 100644 --- a/src/modules/virtual/Loging/Loging.cpp +++ b/src/modules/virtual/Loging/Loging.cpp @@ -313,7 +313,7 @@ class Date : public IoTItem { void setValue(IoTValue Value, bool generateEvent = true) { value = Value; - regEvent(value.valS, ""); + if (generateEvent) regEvent(value.valS, ""); //отправка данных при изменении даты for (std::list::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { if ((*it)->getSubtype() == "Loging") { diff --git a/src/modules/virtual/VButton/VButton.cpp b/src/modules/virtual/VButton/VButton.cpp index ad0bfcd4..44f846ad 100644 --- a/src/modules/virtual/VButton/VButton.cpp +++ b/src/modules/virtual/VButton/VButton.cpp @@ -8,7 +8,7 @@ class VButton : public IoTItem { void setValue(IoTValue Value, bool generateEvent = true) { value = Value; - regEvent((String)(int)value.valD, "VButton"); + if (generateEvent) regEvent((String)(int)value.valD, "VButton"); } String getValue() { From 538257208d3a5e9f3c7453dad9d7520dcfe8f164 Mon Sep 17 00:00:00 2001 From: biver Date: Sat, 22 Oct 2022 08:00:48 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=92=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC=20=D1=81=D0=B5=D1=82=D0=B5=D0=B2=D1=8B=D0=B5=20=D1=81?= =?UTF-8?q?=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8=D0=B8=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D0=B5=20MQTT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MqttClient.cpp | 50 ++++++++++++++++++++++------------------- src/classes/IoTItem.cpp | 18 +++++++-------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 90691b43..72884352 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -161,29 +161,33 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) { else if (topicStr.indexOf("event") != -1) { //пока не работает сетевой обмен этот код будет закомментирован - // if (!jsonReadBool(settingsFlashJson, "mqttin")) { - // return; - // } - // if (topicStr.indexOf(chipId) == -1) { - // String devId = selectFromMarkerToMarker(topicStr, "/", 2); - // String id = selectFromMarkerToMarker(topicStr, "/", 3); - // IoTItem* itemExist = findIoTItem(id); - // if (itemExist) { - // String valAsStr; - // if (jsonRead(payloadStr, F("val"), valAsStr, false)) { - // itemExist->setValue(valAsStr); - // unsigned long interval; - // jsonRead(payloadStr, F("int"), interval); - // itemExist->setInterval(interval); - // } - // } else { - // //добавим событие в базу - // // IoTItems.push_back((IoTItem*)new externalVariable(payloadStr)); - // } - // //запустим проверку его в сценариях - // generateEvent(id, payloadStr); - // SerialPrint("i", F("=>MQTT"), "Received event from other device: '" + devId + "' " + id + " " + payloadStr); - // } + if (!jsonReadBool(settingsFlashJson, "mqttin")) { + return; + } + if (topicStr.indexOf(chipId) == -1) { + String devId = selectFromMarkerToMarker(topicStr, "/", 2); + String id = selectFromMarkerToMarker(topicStr, "/", 3); + String valAsStr; + jsonRead(payloadStr, F("val"), valAsStr, false); + + IoTItem* itemExist = findIoTItem(id); + if (itemExist) { + unsigned long interval; + jsonRead(payloadStr, F("int"), interval); + itemExist->setInterval(interval); + itemExist->setValue(valAsStr, false); + } else { + //добавим событие в базу + itemExist = (IoTItem*)new externalVariable(payloadStr); + IoTItems.push_back(itemExist); + } + //запустим проверку его в сценариях + generateEvent(id, valAsStr); + publishStatusMqtt(id, valAsStr); + publishStatusWs(id, valAsStr); + //itemExist->regEvent(valAsStr, ""); + SerialPrint("i", F("=>MQTT"), "Received event from other device: '" + devId + "' " + id + " " + valAsStr); + } } //здесь мы получаем прямые команды которые сразу выполнятся на этом устройстве diff --git a/src/classes/IoTItem.cpp b/src/classes/IoTItem.cpp index 02061f9d..0c50e7f0 100644 --- a/src/classes/IoTItem.cpp +++ b/src/classes/IoTItem.cpp @@ -64,7 +64,7 @@ void IoTItem::setValue(String valStr, bool generateEvent) { } else { value.valS = valStr; } - if (generateEvent) setValue(value, generateEvent); + setValue(value, generateEvent); } // @@ -96,14 +96,14 @@ void IoTItem::regEvent(String value, String consoleInfo = "") { // SerialPrint("i", F("=>ALLMQTT"), "Broadcast event: "); // } //отправка события другим устройствам в сети============================== - // if (jsonReadBool(settingsFlashJson, "mqttin")) { - // String json = "{}"; - // jsonWriteStr_(json, "id", _id); - // jsonWriteStr_(json, "val", value); - // jsonWriteInt_(json, "int", _interval + 5000); // 5 секунд про запас - // publishEvent(_id, json); - // SerialPrint("i", F("<=MQTT"), "Broadcast event: " + json); - //} + if (jsonReadBool(settingsFlashJson, "mqttin")) { + String json = "{}"; + jsonWriteStr_(json, "id", _id); + jsonWriteStr_(json, "val", value); + jsonWriteInt_(json, "int", _interval/1000 + 5); // 5 секунд про запас + publishEvent(_id, json); + SerialPrint("i", F("<=MQTT"), "Broadcast event: " + json); + } //======================================================================== } From b89610ae0aeead98873e9ca9422a4b6b361fe927 Mon Sep 17 00:00:00 2001 From: biver Date: Sat, 22 Oct 2022 08:01:33 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA?= =?UTF-8?q?=D0=B8=20=D1=87=D0=B8=D1=81=D0=B5=D0=BB=20=D0=B2=20mqttPub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/IoTScenario.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/classes/IoTScenario.cpp b/src/classes/IoTScenario.cpp index 5d44c832..0779d816 100644 --- a/src/classes/IoTScenario.cpp +++ b/src/classes/IoTScenario.cpp @@ -411,7 +411,9 @@ IoTValue sysExecute(SysOp command, std::vector ¶m) { case sysop_mqttPub: if (param.size() == 2) { // Serial.printf("Call from sysExecute %s %s\n", param[0].valS.c_str(), param[1].valS.c_str()); - value.valD = mqtt.publish(param[0].valS.c_str(), param[1].valS.c_str(), false); + String tmpStr = param[1].valS; + if (param[1].isDecimal) tmpStr = param[1].valD; + value.valD = mqtt.publish(param[0].valS.c_str(), tmpStr.c_str(), false); } break; case sysop_getUptime: