Merge PR #573: TpLink: Add a timeout timer for jobs

This commit is contained in:
jenkins 2022-07-01 22:48:55 +02:00
commit 7c128d90d2
2 changed files with 20 additions and 1 deletions

View File

@ -104,7 +104,6 @@ IntegrationPluginTPLink::~IntegrationPluginTPLink()
void IntegrationPluginTPLink::init() void IntegrationPluginTPLink::init()
{ {
m_broadcastSocket = new QUdpSocket(this); m_broadcastSocket = new QUdpSocket(this);
} }
void IntegrationPluginTPLink::discoverThings(ThingDiscoveryInfo *info) void IntegrationPluginTPLink::discoverThings(ThingDiscoveryInfo *info)
@ -264,6 +263,20 @@ void IntegrationPluginTPLink::setupThing(ThingSetupInfo *info)
void IntegrationPluginTPLink::postSetupThing(Thing *thing) void IntegrationPluginTPLink::postSetupThing(Thing *thing)
{ {
qCDebug(dcTplink()) << "Post setup thing" << thing->name(); qCDebug(dcTplink()) << "Post setup thing" << thing->name();
QTimer *jobTimer = new QTimer(thing);
jobTimer->setInterval(5000);
connect(jobTimer, &QTimer::timeout, thing, [this, thing](){
if (m_pendingJobs.contains(thing)) {
Job job = m_pendingJobs.take(thing);
qCWarning(dcTplink()) << "A job" << job.id << "timed out";
if (job.actionInfo) {
job.actionInfo->finish(Thing::ThingErrorTimeout);
}
processQueue(thing);
}
});
m_jobTimers[thing] = jobTimer;
connect(thing, &Thing::nameChanged, this, [this, thing](){ connect(thing, &Thing::nameChanged, this, [this, thing](){
QVariantMap map; QVariantMap map;
QVariantMap systemMap; QVariantMap systemMap;
@ -310,6 +323,7 @@ void IntegrationPluginTPLink::thingRemoved(Thing *thing)
m_sockets.remove(thing); m_sockets.remove(thing);
m_pendingJobs.remove(thing); m_pendingJobs.remove(thing);
m_jobQueue.remove(thing); m_jobQueue.remove(thing);
m_jobTimers.remove(thing);
if (myThings().isEmpty() && m_timer) { if (myThings().isEmpty() && m_timer) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_timer); hardwareManager()->pluginTimerManager()->unregisterTimer(m_timer);
@ -436,6 +450,7 @@ void IntegrationPluginTPLink::connectToDevice(Thing *thing, const QHostAddress &
} }
Job job = m_pendingJobs.take(thing); Job job = m_pendingJobs.take(thing);
m_jobTimers[thing]->stop();
QJsonParseError error; QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(decryptPayload(payload), &error); QJsonDocument jsonDoc = QJsonDocument::fromJson(decryptPayload(payload), &error);
@ -604,5 +619,7 @@ void IntegrationPluginTPLink::processQueue(Thing *thing)
socket->disconnectFromHost(); socket->disconnectFromHost();
return; return;
} }
m_jobTimers[thing]->start();
} }

View File

@ -36,6 +36,7 @@
#include <QUdpSocket> #include <QUdpSocket>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QTimer>
class PluginTimer; class PluginTimer;
@ -76,6 +77,7 @@ private:
}; };
QHash<Thing*, Job> m_pendingJobs; QHash<Thing*, Job> m_pendingJobs;
QHash<Thing*, QList<Job>> m_jobQueue; QHash<Thing*, QList<Job>> m_jobQueue;
QHash<Thing*, QTimer*> m_jobTimers;
int m_jobIdx = 0; int m_jobIdx = 0;
QUdpSocket *m_broadcastSocket = nullptr; QUdpSocket *m_broadcastSocket = nullptr;