Модуль ExternalMQTT

This commit is contained in:
avaksru
2022-12-27 17:54:56 +03:00
parent 987461ed28
commit 3e24bf61e0
3 changed files with 193 additions and 2 deletions

View File

@@ -110,8 +110,8 @@ void mqttSubscribe() {
mqtt.subscribe((mqttRootDevice + "/update").c_str());
if (jsonReadBool(settingsFlashJson, "mqttin")) {
mqtt.subscribe((mqttPrefix + "/+/+/event").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/order").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/event/#").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/order/#").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
}
}

View File

@@ -0,0 +1,133 @@
#include "Global.h"
#include "classes/IoTItem.h"
class ExternalMQTT : public IoTItem
{
private:
String _MAC;
String _sensor;
IoTItem *tmp;
int _minutesPassed = 0;
String json = "{}";
int orange = 0;
int red = 0;
int offline = 0;
bool dataFromNode = false;
public:
ExternalMQTT(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);
dataFromNode = false;
}
char *TimeToString(unsigned long t)
{
static char str[12];
long h = t / 3600;
t = t % 3600;
int m = t / 60;
int s = t % 60;
sprintf(str, "%02ld:%02d:%02d", h, m, s);
return str;
}
void onMqttRecive(String &topic, String &msg)
{
if (msg.indexOf("HELLO") == -1)
{
// SerialPrint("i", "onMqttRecive", "Прилетело " + topic);
// SerialPrint("i", "onMqttRecive", "Прилетело " + msg);
String dev = selectToMarkerLast(topic, "/");
dev.toUpperCase();
dev.replace(":", "");
if (_MAC == "")
{
SerialPrint("i", "onMqttRecive", dev + " --> " + msg);
}
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, msg);
if (error)
{
SerialPrint("E", F("onMqttRecive"), error.f_str());
}
JsonObject jsonObject = doc.as<JsonObject>();
for (JsonPair kv : jsonObject)
{
String key = kv.key().c_str();
String val = kv.value();
if (_MAC == dev && _sensor == key)
{
dataFromNode = true;
_minutesPassed = 0;
setValue(val);
// setNewWidgetAttributes();
}
// Serial.println("Key: " + key);
// Serial.println("Value: " + val);
}
}
}
void doByInterval()
{
_minutesPassed++;
setNewWidgetAttributes();
}
void onMqttWsAppConnectEvent()
{
setNewWidgetAttributes();
}
void setNewWidgetAttributes()
{
jsonWriteStr(json, F("info"), prettyMinutsTimeout(_minutesPassed));
if (dataFromNode)
{
if (orange != 0 && red != 0 && offline != 0)
{
if (_minutesPassed < orange)
{
jsonWriteStr(json, F("color"), "");
}
if (_minutesPassed >= orange && _minutesPassed < red)
{
jsonWriteStr(json, F("color"), F("orange")); // сделаем виджет оранжевым
}
if (_minutesPassed >= red && _minutesPassed < offline)
{
jsonWriteStr(json, F("color"), F("red")); // сделаем виджет красным
}
if (_minutesPassed >= offline)
{
jsonWriteStr(json, F("info"), F("offline"));
}
}
}
else
{
jsonWriteStr(json, F("info"), F("awaiting"));
}
sendSubWidgetsValues(_id, json);
}
~ExternalMQTT(){};
};
void *getAPI_ExternalMQTT(String subtype, String param)
{
if (subtype == F("ExternalMQTT"))
{
return new ExternalMQTT(param);
}
else
{
return nullptr;
}
}

View File

@@ -0,0 +1,58 @@
{
"menuSection": "Сенсоры",
"configItem": [
{
"global": 0,
"name": "MQTT парсер",
"type": "Reading",
"subtype": "ExternalMQTT",
"id": "MQTTin",
"widget": "",
"page": "",
"descr": "",
"MAC": "",
"sensor": "",
"round": "",
"orange": 60,
"red": 120,
"offline": 180,
"int": 60
}
],
"about": {
"authorName": "AVAKS",
"authorContact": "https://t.me/@avaks_dev",
"authorGit": "https://github.com/avaksru",
"specialThanks": "",
"moduleName": "ExternalMQTT",
"moduleVersion": "1",
"usedRam": {
"esp32_4mb": 15,
"esp8266_4mb": 15
},
"title": "ExternalMQTT",
"moduleDesc": "Модуль получения данных из OpenMQTTGateway, Zigbee2MQTT, SLS, Tasmota, NodeRead, HA, openHAB, Fhem, domotiz, EEdom",
"propInfo": {
"round": "Округление после запятой.",
"int": "Интервал для изменения цвета",
"orange": "количество минут после которого окрасить виджет в оранжевый цвет",
"red": "количество минут после которого окрасить виджет в красный цвет",
"offline": "количество минут после которого отобразить что устройство offline, если все три orange red и offline поставить в ноль - то функция окраски выключится",
"MAC": "MAC адрес беспроводного датчика",
"sensor": "Тип сенсора: температура / влажность / время / ... "
}
},
"defActive": false,
"usedLibs": {
"esp32_4mb": [],
"esp8266_4mb": [],
"esp8266_1mb": [],
"esp8266_1mb_ota": [],
"esp8285_1mb": [],
"esp8285_1mb_ota": []
}
}