added detection for Garadget disconnect
parent
303f877308
commit
c7cc9b2350
|
|
@ -50,7 +50,7 @@ void IntegrationPluginGaradget::setupThing(ThingSetupInfo *info)
|
||||||
thing->setParamValue(garadgetThingDeviceNameParamTypeId,"garadget/" + device + "/#" );
|
thing->setParamValue(garadgetThingDeviceNameParamTypeId,"garadget/" + device + "/#" );
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(dcGaradget) << "entered setupThing" << thing->paramValue(garadgetThingDeviceNameParamTypeId);
|
qCDebug(dcGaradget) << "entered setupThing" << thing->paramValue(garadgetThingDeviceNameParamTypeId) ;
|
||||||
MqttClient *client = nullptr;
|
MqttClient *client = nullptr;
|
||||||
if (thing->thingClassId() == garadgetThingClassId) {
|
if (thing->thingClassId() == garadgetThingClassId) {
|
||||||
client = hardwareManager()->mqttProvider()->createInternalClient(thing->id().toString());
|
client = hardwareManager()->mqttProvider()->createInternalClient(thing->id().toString());
|
||||||
|
|
@ -69,13 +69,48 @@ void IntegrationPluginGaradget::setupThing(ThingSetupInfo *info)
|
||||||
qCDebug(dcGaradget) << "entered is Connected" << client;
|
qCDebug(dcGaradget) << "entered is Connected" << client;
|
||||||
subscribe(thing);
|
subscribe(thing);
|
||||||
}
|
}
|
||||||
|
m_lastActivityTimeStamps[thing] = QDateTime::currentDateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void IntegrationPluginGaradget::postSetupThing(Thing *thing)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!m_pluginTimer) {
|
||||||
|
QString name = thing->paramValue(garadgetThingDeviceNameParamTypeId).toString();
|
||||||
|
if (name.endsWith("/#")) {
|
||||||
|
name.chop(2);
|
||||||
|
}
|
||||||
|
name = name + "/command";
|
||||||
|
qCDebug(dcGaradget) << "inside m_pluginTimer with" << name ;
|
||||||
|
uint updatetime = 10;
|
||||||
|
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(updatetime);
|
||||||
|
connect(m_pluginTimer, &PluginTimer::timeout, this, [=](){
|
||||||
|
if (m_garadgetconnect == 1) {
|
||||||
|
foreach (Thing *thing, myThings()) {
|
||||||
|
m_mqttClients.value(thing)->publish(name, "get-status");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint timesinceupdate = QDateTime::currentDateTime().toTime_t() - m_lastActivityTimeStamps[thing].toTime_t();
|
||||||
|
if ((timesinceupdate > updatetime) && (m_garadgetconnect == 1)) {
|
||||||
|
qCDebug(dcGaradget) << "disconnect garadget" << m_lastActivityTimeStamps[thing] << timesinceupdate ;
|
||||||
|
thing->setStateValue(garadgetConnectedStateTypeId, false);
|
||||||
|
m_garadgetconnect = 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void IntegrationPluginGaradget::thingRemoved(Thing *thing)
|
void IntegrationPluginGaradget::thingRemoved(Thing *thing)
|
||||||
{
|
{
|
||||||
qCDebug(dcGaradget) << thing << "Removed";
|
qCDebug(dcGaradget) << thing << "Removed";
|
||||||
m_mqttClients.take(thing)->deleteLater();
|
m_mqttClients.take(thing)->deleteLater();
|
||||||
|
if (m_pluginTimer) {
|
||||||
|
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
|
||||||
|
m_pluginTimer = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntegrationPluginGaradget::executeAction(ThingActionInfo *info)
|
void IntegrationPluginGaradget::executeAction(ThingActionInfo *info)
|
||||||
|
|
@ -183,6 +218,8 @@ void IntegrationPluginGaradget::publishReceived(const QString &topic, const QByt
|
||||||
}
|
}
|
||||||
if (topic.endsWith("/status")) {
|
if (topic.endsWith("/status")) {
|
||||||
thing->setStateValue(garadgetConnectedStateTypeId, true);
|
thing->setStateValue(garadgetConnectedStateTypeId, true);
|
||||||
|
m_garadgetconnect = 1;
|
||||||
|
m_lastActivityTimeStamps[thing] = QDateTime::currentDateTime();
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(payload, &error);
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(payload, &error);
|
||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
|
|
@ -190,8 +227,7 @@ void IntegrationPluginGaradget::publishReceived(const QString &topic, const QByt
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QJsonObject jo = jsonDoc.object();
|
QJsonObject jo = jsonDoc.object();
|
||||||
qCDebug(dcGaradget) << "wifi signal" << (100 + jo.value(QString("signal")).toInt()) / 0.5 ;
|
thing->setStateValue(garadgetSignalStrengthStateTypeId, (100 + jo.value(QString("signal")).toInt()) * 2 );
|
||||||
thing->setStateValue(garadgetSignalStrengthStateTypeId, (100 + jo.value(QString("signal")).toInt()) / 0.5 );
|
|
||||||
thing->setStateValue(garadgetSensorlevelStateTypeId, jo.value(QString("sensor")).toInt());
|
thing->setStateValue(garadgetSensorlevelStateTypeId, jo.value(QString("sensor")).toInt());
|
||||||
thing->setStateValue(garadgetBrightlevelStateTypeId, jo.value(QString("bright")).toInt());
|
thing->setStateValue(garadgetBrightlevelStateTypeId, jo.value(QString("bright")).toInt());
|
||||||
if (jo.value(QString("status")).toString().contains(QString("stopped"))) {
|
if (jo.value(QString("status")).toString().contains(QString("stopped"))) {
|
||||||
|
|
@ -217,6 +253,7 @@ void IntegrationPluginGaradget::publishReceived(const QString &topic, const QByt
|
||||||
if (topic.endsWith("/set-config")){
|
if (topic.endsWith("/set-config")){
|
||||||
if ( (payload.contains("mqip")) or (payload.contains("mqpt")) ) {
|
if ( (payload.contains("mqip")) or (payload.contains("mqpt")) ) {
|
||||||
thing->setStateValue(garadgetConnectedStateTypeId, false);
|
thing->setStateValue(garadgetConnectedStateTypeId, false);
|
||||||
|
m_garadgetconnect = 0;
|
||||||
qCDebug(dcGaradget) << "Detected change of Broker msg - set connected to false";
|
qCDebug(dcGaradget) << "Detected change of Broker msg - set connected to false";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,14 @@
|
||||||
#ifndef INTEGRATIONPLUGINGARADGET_H
|
#ifndef INTEGRATIONPLUGINGARADGET_H
|
||||||
#define INTEGRATIONPLUGINGARADGET_H
|
#define INTEGRATIONPLUGINGARADGET_H
|
||||||
|
|
||||||
|
#include "plugintimer.h"
|
||||||
#include "integrations/integrationplugin.h"
|
#include "integrations/integrationplugin.h"
|
||||||
|
#include "network/networkaccessmanager.h"
|
||||||
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QUdpSocket>
|
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
class MqttClient;
|
class MqttClient;
|
||||||
|
|
||||||
|
|
@ -54,14 +57,19 @@ public:
|
||||||
void setupThing(ThingSetupInfo *info) override;
|
void setupThing(ThingSetupInfo *info) override;
|
||||||
void thingRemoved(Thing *thing) override;
|
void thingRemoved(Thing *thing) override;
|
||||||
void executeAction(ThingActionInfo *info) override;
|
void executeAction(ThingActionInfo *info) override;
|
||||||
|
void postSetupThing(Thing *thing) override;
|
||||||
private slots:
|
|
||||||
void subscribe(Thing *thing);
|
|
||||||
|
|
||||||
void publishReceived(const QString &topic, const QByteArray &payload, bool retained);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QHash<Thing*, MqttClient*> m_mqttClients;
|
QHash<Thing*, MqttClient*> m_mqttClients;
|
||||||
|
PluginTimer *m_pluginTimer = nullptr;
|
||||||
|
QHash<Thing*, QDateTime> m_lastActivityTimeStamps;
|
||||||
|
|
||||||
|
int m_garadgetconnect = 0;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void subscribe(Thing *thing);
|
||||||
|
void publishReceived(const QString &topic, const QByteArray &payload, bool retained);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INTEGRATIONPLUGINGARADGET_H
|
#endif // INTEGRATIONPLUGINGARADGET_H
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue