Files
IoTManager/src/modules/sensors/ExternalMQTT/ExternalMQTT.cpp

177 lines
5.1 KiB
C++
Raw Normal View History

2022-12-27 17:54:56 +03:00
#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;
String _topic = "";
bool _isJson;
bool _addPrefix;
bool _debug;
2022-12-27 17:54:56 +03:00
public:
ExternalMQTT(String parameters) : IoTItem(parameters)
{
_sensor = jsonReadStr(parameters, "sensor");
jsonRead(parameters, F("orange"), orange);
jsonRead(parameters, F("red"), red);
jsonRead(parameters, F("offline"), offline);
_topic = jsonReadStr(parameters, "topic");
_isJson = jsonReadBool(parameters, "isJson");
_addPrefix = jsonReadBool(parameters, "addPrefix");
_debug = jsonReadBool(parameters, "debug");
2022-12-27 17:54:56 +03:00
dataFromNode = false;
mqttSubscribeExternal(_topic, _addPrefix);
2022-12-27 17:54:56 +03:00
}
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)
{
if (_debug)
{
SerialPrint("i", "onMqttRecive", "Прилетело " + topic + " msg: " + msg);
// SerialPrint("i", "onMqttRecive", "Прилетело " + msg);
}
2022-12-27 17:54:56 +03:00
String dev = selectToMarkerLast(topic, "/");
dev.toUpperCase();
dev.replace(":", "");
if (_topic != topic)
2022-12-27 17:54:56 +03:00
{
return;
2022-12-27 17:54:56 +03:00
}
if (_isJson)
2022-12-27 17:54:56 +03:00
{
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 (_debug)
{
SerialPrint("i", "onMqttRecive", "Прилетело MAC: " + dev + " key=" + key + " val=" + val);
}
if (_sensor == key)
{
dataFromNode = true;
_minutesPassed = 0;
setValue(val);
// setNewWidgetAttributes();
}
2022-12-27 17:54:56 +03:00
// Serial.println("Key: " + key);
// Serial.println("Value: " + val);
}
}
else
2022-12-27 17:54:56 +03:00
{
if (_debug)
2022-12-27 17:54:56 +03:00
{
SerialPrint("i", "onMqttRecive", "Прилетело MAC: " + dev + " val=" + msg);
2022-12-27 17:54:56 +03:00
}
dataFromNode = true;
_minutesPassed = 0;
setValue(msg);
// setNewWidgetAttributes();
2022-12-27 17:54:56 +03:00
}
}
}
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);
}
/*
IoTValue execute(String command, std::vector<IoTValue> &param)
{
if (command == "mqttSubscribe")
{
if (param.size() == 2)
{
if (!param[0].isDecimal && param[1].isDecimal)
{
mqttSubscribeExternal(param[0].valS, (bool)param[0].valD);
}
}
}
return {};
}
*/
2022-12-27 17:54:56 +03:00
~ExternalMQTT(){};
};
void *getAPI_ExternalMQTT(String subtype, String param)
{
if (subtype == F("ExternalMQTT"))
{
return new ExternalMQTT(param);
}
else
{
return nullptr;
}
}