diff --git a/meross/integrationpluginmeross.cpp b/meross/integrationpluginmeross.cpp index bb6ff9d8..3cf12aab 100644 --- a/meross/integrationpluginmeross.cpp +++ b/meross/integrationpluginmeross.cpp @@ -158,44 +158,50 @@ void IntegrationPluginMeross::setupThing(ThingSetupInfo *info) if (monitor) { hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(monitor); } - PluginTimer *timer = m_timers.take(thing); - if (timer) { - hardwareManager()->pluginTimerManager()->unregisterTimer(timer); - } monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(MacAddress(thing->paramValue(plugThingMacAddressParamTypeId).toString())); m_deviceMonitors.insert(thing, monitor); - timer = hardwareManager()->pluginTimerManager()->registerTimer(5); - m_timers.insert(thing, timer); - - connect(monitor, &NetworkDeviceMonitor::reachableChanged, thing, [timer, thing](bool reachable) { - thing->setStateValue("connected", reachable); - if (reachable) { - timer->start(); - } else { - timer->stop(); - } - }); - - connect(timer, &PluginTimer::currentTickChanged, this, [this, thing](qlonglong tick){ - if (tick % 5 == 0) { - pollDevice5s(thing); - } else if (tick == 0) { - pollDevice60s(thing); - } - }); - pollDevice5s(thing); pollDevice60s(thing); info->finish(Thing::ThingErrorNoError); } +void IntegrationPluginMeross::postSetupThing(Thing */*thing*/) +{ + if (!m_timer5s) { + m_timer5s = hardwareManager()->pluginTimerManager()->registerTimer(5); + connect(m_timer5s, &PluginTimer::timeout, this, [=](){ + foreach (Thing *thing, myThings()) { + if (m_deviceMonitors.value(thing)->reachable()) { + pollDevice5s(thing); + } + } + }); + } + if (!m_timer60s) { + m_timer5s = hardwareManager()->pluginTimerManager()->registerTimer(60); + connect(m_timer5s, &PluginTimer::timeout, this, [=](){ + foreach (Thing *thing, myThings()) { + if (m_deviceMonitors.value(thing)->reachable()) { + pollDevice60s(thing); + } + } + }); + } +} + void IntegrationPluginMeross::thingRemoved(Thing *thing) { hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_deviceMonitors.take(thing)); - hardwareManager()->pluginTimerManager()->unregisterTimer(m_timers.take(thing)); + + if (myThings().isEmpty()) { + hardwareManager()->pluginTimerManager()->unregisterTimer(m_timer5s); + m_timer5s = nullptr; + hardwareManager()->pluginTimerManager()->unregisterTimer(m_timer60s); + m_timer60s = nullptr; + } } void IntegrationPluginMeross::executeAction(ThingActionInfo *info) diff --git a/meross/integrationpluginmeross.h b/meross/integrationpluginmeross.h index 9123c5a8..dbc2b526 100644 --- a/meross/integrationpluginmeross.h +++ b/meross/integrationpluginmeross.h @@ -60,6 +60,7 @@ public: void confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret) override; void setupThing(ThingSetupInfo *info) override; + void postSetupThing(Thing *thing) override; void thingRemoved(Thing *thing) override; void executeAction(ThingActionInfo *info) override; @@ -75,7 +76,8 @@ private: QHash m_keys; QHash m_deviceMonitors; - QHash m_timers; + PluginTimer* m_timer5s = nullptr; + PluginTimer* m_timer60s = nullptr; }; #endif // INTEGRATIONPLUGINMEROSS_H