From 8cd131bda2a2e2ec15fac4a194dad700a984faaf Mon Sep 17 00:00:00 2001 From: Mit4el Date: Tue, 31 Oct 2023 22:03:33 +0300 Subject: [PATCH] bugfix Ble --- src/modules/sensors/Ble/Ble.cpp | 80 ++++++++++++++++++++++++---- src/modules/sensors/Ble/modinfo.json | 26 +++++---- 2 files changed, 87 insertions(+), 19 deletions(-) diff --git a/src/modules/sensors/Ble/Ble.cpp b/src/modules/sensors/Ble/Ble.cpp index 1e57957a..45b3658d 100644 --- a/src/modules/sensors/Ble/Ble.cpp +++ b/src/modules/sensors/Ble/Ble.cpp @@ -18,7 +18,15 @@ private: // описание параметров передаваемых из настроек датчика из веба String _MAC; String _sensor; - int timeRecv; + int timeRecv = 0; + int _minutesPassed = 0; + String json = "{}"; + int orange = 0; + int red = 0; + int offline = 0; + int _int; + bool dataFromNode = false; + public: String whoIAm(/*String &mac, String &sens*/) { @@ -38,6 +46,9 @@ public: if (timeRecv > 0) { value.valS = s; + dataFromNode = true; + _minutesPassed = 0; + setNewWidgetAttributes(); } else { @@ -55,12 +66,18 @@ public: value.isDecimal = 1; value.valD = valStr.toFloat(); regEvent(value.valD, _id); + dataFromNode = true; + _minutesPassed = 0; + setNewWidgetAttributes(); } else { value.isDecimal = 0; value.valS = valStr; regEvent(value.valS, _id); + dataFromNode = true; + _minutesPassed = 0; + setNewWidgetAttributes(); } } else @@ -99,12 +116,56 @@ public: } regEvent(value.valS, _id); } + _minutesPassed++; + setNewWidgetAttributes(); + } + void onMqttWsAppConnectEvent() + { + setNewWidgetAttributes(); + } + void setNewWidgetAttributes() + { + + int minutes_ = _minutesPassed * _int / 60; + jsonWriteStr(json, F("info"), prettyMinutsTimeout(minutes_)); + if (dataFromNode) + { + if (orange != 0 && red != 0 && offline != 0) + { + if (minutes_ < orange) + { + jsonWriteStr(json, F("color"), ""); + } + if (minutes_ >= orange && minutes_ < red) + { + jsonWriteStr(json, F("color"), F("orange")); // сделаем виджет оранжевым + } + if (minutes_ >= red && minutes_ < offline) + { + jsonWriteStr(json, F("color"), F("red")); // сделаем виджет красным + } + if (minutes_ >= offline) + { + jsonWriteStr(json, F("info"), F("offline")); + } + } + } + else + { + jsonWriteStr(json, F("info"), F("awaiting")); + } + sendSubWidgetsValues(_id, json); } BleSens(String parameters) : IoTItem(parameters) { _MAC = jsonReadStr(parameters, "MAC"); _sensor = jsonReadStr(parameters, "sensor"); + jsonRead(parameters, F("orange"), orange); + jsonRead(parameters, F("red"), red); + jsonRead(parameters, F("offline"), offline); + jsonRead(parameters, F("int"), _int); + dataFromNode = false; BleSensArray.push_back(this); } @@ -186,8 +247,9 @@ public: BLEdata.remove("acts"); BLEdata.remove("cont"); BLEdata.remove("track"); + BLEdata.remove("id"); - String mac_address = BLEdata["id"].as(); + String mac_address = BLEdata["MAC"].as(); mac_address.replace(":", ""); // дописываем время прихода пакета данных BLEdata["last"] = millis(); @@ -199,13 +261,15 @@ public: // { // String val = BLEdata.as(); String output; + BLEdata.remove("servicedatauuid"); serializeJson(BLEdata, output); - SerialPrint("i", F("BLE"), _id + " " + output); + SerialPrint("i", F("BLE"), mac_address + " " + output); //} } + + SerialPrint("i", F("BLE"), "found: " + mac_address); } - BLEdata.remove("servicedatauuid"); - SerialPrint("i", F("BLE"), "found: " + mac_address); + // Перебираем все зарегистрированные сенсоры BleSens for (std::vector::iterator it = BleSensArray.begin(); it != BleSensArray.end(); ++it) @@ -246,9 +310,7 @@ public: } } - ~BleScan(){ - BleSensArray.clear(); - }; + ~BleScan() { BleSensArray.clear(); }; }; //======================================================================================================= @@ -267,4 +329,4 @@ void *getAPI_Ble(String subtype, String param) { return nullptr; } -} +} \ No newline at end of file diff --git a/src/modules/sensors/Ble/modinfo.json b/src/modules/sensors/Ble/modinfo.json index b46fe322..a3ef4e58 100644 --- a/src/modules/sensors/Ble/modinfo.json +++ b/src/modules/sensors/Ble/modinfo.json @@ -12,7 +12,7 @@ "int": 135, "scanDuration": 10, "filter": "servicedatauuid", - "debug":1 + "debug": 1 }, { "name": "bluetooth датчик", @@ -26,6 +26,9 @@ "int": 30, "global": 0, "round": 1, + "orange": 60, + "red": 120, + "offline": 180, "MAC": "", "sensor": "" } @@ -49,6 +52,9 @@ "moduleDesc": "Позволяет получить данные с Bluetooth часов и термометров Mijia, Xiaomi, Cleargrass, ...", "propInfo": { "round": "Округление после запятой.", + "orange": "количество минут после которого окрасить виджет в оранжевый цвет", + "red": "количество минут после которого окрасить виджет в красный цвет", + "offline": "количество минут после которого отобразить что устройство offline, если все три orange red и offline поставить в ноль - то функция окраски выключится", "int": "Интервал сканирования BLE окружения (BleScan) / В BleSens темп обновления времнени поступления данных, сами даные обновляются по мене сканирования/поступления", "scanDuration": "Длительность сканирования ", "filter": "Позволяет установить фильтр по параметру передаваемому датчиком. Влияет только на вывод лога при debug=1, что бы было легче найти датчики, если много устройств в эфире", @@ -59,27 +65,27 @@ "defActive": false, "usedLibs": { "esp32_4mb": [ - "https://github.com/h2zero/NimBLE-Arduino.git", + "https://github.com/Mit4el/NimBLE-Arduino.git", "https://github.com/Mit4el/decoder.git" ], "esp32_16mb": [ - "https://github.com/h2zero/NimBLE-Arduino.git", + "https://github.com/Mit4el/NimBLE-Arduino.git", "https://github.com/Mit4el/decoder.git" ], "esp32_4mb3f": [ - "https://github.com/h2zero/NimBLE-Arduino.git", + "https://github.com/Mit4el/NimBLE-Arduino.git", "https://github.com/Mit4el/decoder.git" - ], + ], "esp32cam_4mb": [ - "https://github.com/h2zero/NimBLE-Arduino.git", + "https://github.com/Mit4el/NimBLE-Arduino.git", "https://github.com/Mit4el/decoder.git" - ], + ], "esp32s3_16mb": [ - "https://github.com/h2zero/NimBLE-Arduino.git", + "https://github.com/Mit4el/NimBLE-Arduino.git", "https://github.com/Mit4el/decoder.git" - ], + ], "esp32c3m_4mb": [ - "https://github.com/h2zero/NimBLE-Arduino.git", + "https://github.com/Mit4el/NimBLE-Arduino.git", "https://github.com/Mit4el/decoder.git" ] }