From 64dfb1beb7061e1c76a0d8a835f331797b57f222 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 25 Jan 2021 23:54:39 +0100 Subject: [PATCH] Tuya: Poll the cloud even less frequently to avoid rate limits Fixes: https://github.com/nymea/nymea/issues/384 --- tuya/integrationplugintuya.cpp | 18 +++++++++++++----- tuya/integrationplugintuya.h | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tuya/integrationplugintuya.cpp b/tuya/integrationplugintuya.cpp index a2c3061b..35a8cc78 100644 --- a/tuya/integrationplugintuya.cpp +++ b/tuya/integrationplugintuya.cpp @@ -126,8 +126,11 @@ void IntegrationPluginTuya::postSetupThing(Thing *thing) m_pluginTimerQuery = hardwareManager()->pluginTimerManager()->registerTimer(queryInterval); connect(m_pluginTimerQuery, &PluginTimer::timeout, this, [this](){ foreach (Thing *d, myThings().filterByThingClassId(tuyaCloudThingClassId)) { - foreach (Thing *child, myThings().filterByParentId(d->id())) { - queryDevice(child); + if (m_pollQueue.value(d).isEmpty()) { + m_pollQueue[d] = myThings().filterByParentId(d->id()); + } + if (m_pollQueue[d].count() > 0) { + queryDevice(m_pollQueue[d].takeFirst()); } } }); @@ -146,6 +149,7 @@ void IntegrationPluginTuya::postSetupThing(Thing *thing) void IntegrationPluginTuya::thingRemoved(Thing *thing) { if (thing->thingClassId() == tuyaCloudThingClassId) { + m_pollQueue.remove(thing); m_tokenExpiryTimers.take(thing->id())->deleteLater(); } @@ -445,11 +449,10 @@ void IntegrationPluginTuya::updateChildDevices(Thing *thing) void IntegrationPluginTuya::queryDevice(Thing *thing) { - qCDebug(dcTuya()) << "QueryDevice" << thing; + qCDebug(dcTuya()) << "Updating thing:" << thing; QString devId = thing->paramValue(m_devIdParamTypeIdsMap.value(thing->thingClassId())).toString(); - qCDebug(dcTuya()) << thing->name() << "Updating child devices"; pluginStorage()->beginGroup(thing->parentId().toString()); QString accesToken = pluginStorage()->value("accessToken").toString(); pluginStorage()->endGroup(); @@ -495,7 +498,12 @@ void IntegrationPluginTuya::queryDevice(Thing *thing) QVariantMap result = jsonDoc.toVariant().toMap(); if (result.value("header").toMap().value("code").toString() != "SUCCESS") { qCWarning(dcTuya()) << "Error quering tuya device" << thing->name() << qUtf8Printable(jsonDoc.toJson()); - // Fall through to mark thing as offline + if (result.value("header").toMap().value("code").toString() == "FrequentlyInvoke") { + // Poll rate limit exceeded... As switching is not affected by this, all we lose is an update + // from remote... So just do nothing here and hope next polling gets going again... + return; + } + // Fall through to mark thing as offline on any other error. } bool connected = result.value("payload").toMap().value("data").toMap().value("online").toBool(); diff --git a/tuya/integrationplugintuya.h b/tuya/integrationplugintuya.h index a29de699..a10bcf96 100644 --- a/tuya/integrationplugintuya.h +++ b/tuya/integrationplugintuya.h @@ -73,6 +73,8 @@ private: QHash m_devIdParamTypeIdsMap; QHash m_connectedStateTypeIdsMap; QHash m_powerStateTypeIdsMap; + + QHash> m_pollQueue; }; #endif // INTEGRATIONPLUGINTUYA_H