Tado: Sync actions delayed in order to not exceed rate limits
This commit is contained in:
parent
41c13f2503
commit
5bcdf131d3
@ -32,10 +32,82 @@
|
|||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
#include <QtMath>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
void finishPendingActions(const QList<ThingActionInfo *> &actions, Thing::ThingError error)
|
||||||
|
{
|
||||||
|
for (ThingActionInfo *info : actions) {
|
||||||
|
if (info) {
|
||||||
|
info->finish(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
IntegrationPluginTado::IntegrationPluginTado()
|
IntegrationPluginTado::IntegrationPluginTado()
|
||||||
{
|
{
|
||||||
|
m_stateSyncTimer.setInterval(5000);
|
||||||
|
connect(&m_stateSyncTimer, &QTimer::timeout, this, &IntegrationPluginTado::syncPendingOverlays);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString IntegrationPluginTado::buildZoneKey(const ThingId &accountThingId, const QString &homeId, const QString &zoneId)
|
||||||
|
{
|
||||||
|
return accountThingId.toString() + ":" + homeId + ":" + zoneId;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IntegrationPluginTado::overlayStatesEqual(const OverlayState &first, const OverlayState &second)
|
||||||
|
{
|
||||||
|
if (first.deleteOverlay != second.deleteOverlay) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (first.deleteOverlay) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (first.power != second.power) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return qAbs(first.temperature - second.temperature) < 0.01;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginTado::queueOverlayChange(ThingActionInfo *info, const QString &homeId, const QString &zoneId, const OverlayState &desired)
|
||||||
|
{
|
||||||
|
if (!info) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thing *thing = info->thing();
|
||||||
|
if (!thing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ThingId accountThingId = thing->parentId();
|
||||||
|
QString zoneKey = buildZoneKey(accountThingId, homeId, zoneId);
|
||||||
|
PendingOverlayChange &pending = m_pendingOverlayChanges[zoneKey];
|
||||||
|
pending.accountThingId = accountThingId;
|
||||||
|
pending.homeId = homeId;
|
||||||
|
pending.zoneId = zoneId;
|
||||||
|
pending.desired = desired;
|
||||||
|
pending.dirty = true;
|
||||||
|
pending.pendingActions.append(info);
|
||||||
|
|
||||||
|
connect(info, &ThingActionInfo::aborted, this, [this, info]() {
|
||||||
|
removePendingAction(info);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!m_stateSyncTimer.isActive()) {
|
||||||
|
m_stateSyncTimer.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginTado::removePendingAction(ThingActionInfo *info)
|
||||||
|
{
|
||||||
|
for (auto it = m_pendingOverlayChanges.begin(); it != m_pendingOverlayChanges.end(); ++it) {
|
||||||
|
it->pendingActions.removeAll(info);
|
||||||
|
}
|
||||||
|
for (auto it = m_pendingRequests.begin(); it != m_pendingRequests.end(); ++it) {
|
||||||
|
it->actions.removeAll(info);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntegrationPluginTado::init()
|
void IntegrationPluginTado::init()
|
||||||
@ -222,15 +294,57 @@ void IntegrationPluginTado::thingRemoved(Thing *thing)
|
|||||||
{
|
{
|
||||||
if (thing->thingClassId() == tadoAccountThingClassId) {
|
if (thing->thingClassId() == tadoAccountThingClassId) {
|
||||||
Tado *tado = m_tadoAccounts.take(thing->id());
|
Tado *tado = m_tadoAccounts.take(thing->id());
|
||||||
tado->deleteLater();
|
if (tado) {
|
||||||
|
tado->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it = m_pendingOverlayChanges.begin(); it != m_pendingOverlayChanges.end(); ) {
|
||||||
|
if (it->accountThingId == thing->id()) {
|
||||||
|
finishPendingActions(it->pendingActions, Thing::ThingErrorThingNotFound);
|
||||||
|
it = m_pendingOverlayChanges.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString accountPrefix = thing->id().toString() + ":";
|
||||||
|
for (auto it = m_pendingRequests.begin(); it != m_pendingRequests.end(); ) {
|
||||||
|
if (it->zoneKey.startsWith(accountPrefix)) {
|
||||||
|
finishPendingActions(it->actions, Thing::ThingErrorThingNotFound);
|
||||||
|
it = m_pendingRequests.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
} else if (thing->thingClassId() == zoneThingClassId) {
|
||||||
|
QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString();
|
||||||
|
QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString();
|
||||||
|
QString zoneKey = buildZoneKey(thing->parentId(), homeId, zoneId);
|
||||||
|
if (m_pendingOverlayChanges.contains(zoneKey)) {
|
||||||
|
PendingOverlayChange pending = m_pendingOverlayChanges.take(zoneKey);
|
||||||
|
finishPendingActions(pending.pendingActions, Thing::ThingErrorThingNotFound);
|
||||||
|
}
|
||||||
|
for (auto it = m_pendingRequests.begin(); it != m_pendingRequests.end(); ) {
|
||||||
|
if (it->zoneKey == zoneKey) {
|
||||||
|
finishPendingActions(it->actions, Thing::ThingErrorThingNotFound);
|
||||||
|
it = m_pendingRequests.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++it;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up storage
|
// Clean up storage
|
||||||
pluginStorage()->remove(thing->id().toString());
|
pluginStorage()->remove(thing->id().toString());
|
||||||
|
|
||||||
if (myThings().isEmpty() && m_pluginTimer) {
|
if (myThings().isEmpty()) {
|
||||||
m_pluginTimer->deleteLater();
|
if (m_pluginTimer) {
|
||||||
m_pluginTimer = nullptr;
|
m_pluginTimer->deleteLater();
|
||||||
|
m_pluginTimer = nullptr;
|
||||||
|
}
|
||||||
|
if (m_stateSyncTimer.isActive()) {
|
||||||
|
m_stateSyncTimer.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,6 +354,9 @@ void IntegrationPluginTado::postSetupThing(Thing *thing)
|
|||||||
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
|
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
|
||||||
connect(m_pluginTimer, &PluginTimer::timeout, this, &IntegrationPluginTado::onPluginTimer);
|
connect(m_pluginTimer, &PluginTimer::timeout, this, &IntegrationPluginTado::onPluginTimer);
|
||||||
}
|
}
|
||||||
|
if (!m_stateSyncTimer.isActive()) {
|
||||||
|
m_stateSyncTimer.start();
|
||||||
|
}
|
||||||
|
|
||||||
if (thing->thingClassId() == tadoAccountThingClassId) {
|
if (thing->thingClassId() == tadoAccountThingClassId) {
|
||||||
Tado *tado = m_tadoAccounts.value(thing->id());
|
Tado *tado = m_tadoAccounts.value(thing->id());
|
||||||
@ -262,51 +379,46 @@ void IntegrationPluginTado::executeAction(ThingActionInfo *info)
|
|||||||
Action action = info->action();
|
Action action = info->action();
|
||||||
|
|
||||||
if (thing->thingClassId() == zoneThingClassId) {
|
if (thing->thingClassId() == zoneThingClassId) {
|
||||||
Tado *tado = m_tadoAccounts.value(thing->parentId());
|
if (!m_tadoAccounts.contains(thing->parentId())) {
|
||||||
if (!tado) {
|
|
||||||
info->finish(Thing::ThingErrorThingNotFound);
|
info->finish(Thing::ThingErrorThingNotFound);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString();
|
QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString();
|
||||||
QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString();
|
QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString();
|
||||||
if (action.actionTypeId() == zoneModeActionTypeId) {
|
if (action.actionTypeId() == zoneModeActionTypeId) {
|
||||||
QUuid requestId;
|
OverlayState desired;
|
||||||
if (action.param(zoneModeActionModeParamTypeId).value().toString() == "Tado") {
|
QString mode = action.param(zoneModeActionModeParamTypeId).value().toString();
|
||||||
requestId = tado->deleteOverlay(homeId, zoneId);
|
if (mode == "Tado") {
|
||||||
} else if (action.param(zoneModeActionModeParamTypeId).value().toString() == "Off") {
|
desired.deleteOverlay = true;
|
||||||
requestId = tado->setOverlay(homeId, zoneId, false, thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble());
|
} else if (mode == "Off") {
|
||||||
|
desired.power = false;
|
||||||
|
desired.temperature = thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble();
|
||||||
} else {
|
} else {
|
||||||
if(thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble() <= 5.0) {
|
desired.power = true;
|
||||||
requestId = tado->setOverlay(homeId, zoneId, true, 5);
|
double targetTemperature = thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble();
|
||||||
} else {
|
desired.temperature = targetTemperature <= 5.0 ? 5.0 : targetTemperature;
|
||||||
requestId = tado->setOverlay(homeId, zoneId, true, thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
m_asyncActions.insert(requestId, info);
|
queueOverlayChange(info, homeId, zoneId, desired);
|
||||||
connect(info, &ThingActionInfo::aborted, thing, [requestId, this] {m_asyncActions.remove(requestId);});
|
|
||||||
} else if (action.actionTypeId() == zoneTargetTemperatureActionTypeId) {
|
} else if (action.actionTypeId() == zoneTargetTemperatureActionTypeId) {
|
||||||
|
|
||||||
double temperature = action.param(zoneTargetTemperatureActionTargetTemperatureParamTypeId).value().toDouble();
|
double temperature = action.param(zoneTargetTemperatureActionTargetTemperatureParamTypeId).value().toDouble();
|
||||||
QUuid requestId;
|
OverlayState desired;
|
||||||
if (temperature <= 0) {
|
if (temperature <= 0) {
|
||||||
requestId = tado->setOverlay(homeId, zoneId, false, 0);
|
desired.power = false;
|
||||||
|
desired.temperature = 0;
|
||||||
} else {
|
} else {
|
||||||
requestId = tado->setOverlay(homeId, zoneId, true, temperature);
|
desired.power = true;
|
||||||
|
desired.temperature = temperature;
|
||||||
}
|
}
|
||||||
m_asyncActions.insert(requestId, info);
|
queueOverlayChange(info, homeId, zoneId, desired);
|
||||||
connect(info, &ThingActionInfo::aborted, thing, [requestId, this] {m_asyncActions.remove(requestId);});
|
|
||||||
} else if (action.actionTypeId() == zonePowerActionTypeId) {
|
} else if (action.actionTypeId() == zonePowerActionTypeId) {
|
||||||
bool power = action.param(zonePowerActionPowerParamTypeId).value().toBool();
|
bool power = action.param(zonePowerActionPowerParamTypeId).value().toBool();
|
||||||
thing->setStateValue(zonePowerStateTypeId, power); // the actual power set response might be slow
|
thing->setStateValue(zonePowerStateTypeId, power); // the actual power set response might be slow
|
||||||
QUuid requestId;
|
OverlayState desired;
|
||||||
double temperature = thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble();
|
double temperature = thing->stateValue(zoneTargetTemperatureStateTypeId).toDouble();
|
||||||
if (!power) {
|
desired.power = power;
|
||||||
requestId = tado->setOverlay(homeId, zoneId, false, 0);
|
desired.temperature = power ? temperature : 0;
|
||||||
} else {
|
queueOverlayChange(info, homeId, zoneId, desired);
|
||||||
requestId = tado->setOverlay(homeId, zoneId, true, temperature);
|
|
||||||
}
|
|
||||||
m_asyncActions.insert(requestId, info);
|
|
||||||
connect(info, &ThingActionInfo::aborted, thing, [requestId, this] {m_asyncActions.remove(requestId);});
|
|
||||||
} else {
|
} else {
|
||||||
qCWarning(dcTado()) << "Execute action, unhandled actionTypeId" << action.actionTypeId();
|
qCWarning(dcTado()) << "Execute action, unhandled actionTypeId" << action.actionTypeId();
|
||||||
info->finish(Thing::ThingErrorActionTypeNotFound);
|
info->finish(Thing::ThingErrorActionTypeNotFound);
|
||||||
@ -317,6 +429,66 @@ void IntegrationPluginTado::executeAction(ThingActionInfo *info)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginTado::syncPendingOverlays()
|
||||||
|
{
|
||||||
|
if (m_pendingOverlayChanges.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto it = m_pendingOverlayChanges.begin(); it != m_pendingOverlayChanges.end(); ++it) {
|
||||||
|
PendingOverlayChange &pending = it.value();
|
||||||
|
if (!pending.dirty) {
|
||||||
|
if (!pending.pendingActions.isEmpty()) {
|
||||||
|
finishPendingActions(pending.pendingActions, Thing::ThingErrorNoError);
|
||||||
|
pending.pendingActions.clear();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending.inFlightValid) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending.lastSyncedValid && overlayStatesEqual(pending.desired, pending.lastSynced)) {
|
||||||
|
pending.dirty = false;
|
||||||
|
if (!pending.pendingActions.isEmpty()) {
|
||||||
|
finishPendingActions(pending.pendingActions, Thing::ThingErrorNoError);
|
||||||
|
pending.pendingActions.clear();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tado *tado = m_tadoAccounts.value(pending.accountThingId);
|
||||||
|
if (!tado) {
|
||||||
|
if (!pending.pendingActions.isEmpty()) {
|
||||||
|
finishPendingActions(pending.pendingActions, Thing::ThingErrorThingNotFound);
|
||||||
|
pending.pendingActions.clear();
|
||||||
|
}
|
||||||
|
pending.dirty = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
QUuid requestId;
|
||||||
|
if (pending.desired.deleteOverlay) {
|
||||||
|
requestId = tado->deleteOverlay(pending.homeId, pending.zoneId);
|
||||||
|
} else {
|
||||||
|
requestId = tado->setOverlay(pending.homeId, pending.zoneId, pending.desired.power, pending.desired.temperature);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requestId.isNull()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
PendingRequest request;
|
||||||
|
request.zoneKey = it.key();
|
||||||
|
request.actions = pending.pendingActions;
|
||||||
|
request.sentState = pending.desired;
|
||||||
|
pending.pendingActions.clear();
|
||||||
|
pending.inFlightValid = true;
|
||||||
|
m_pendingRequests.insert(requestId, request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void IntegrationPluginTado::onPluginTimer()
|
void IntegrationPluginTado::onPluginTimer()
|
||||||
{
|
{
|
||||||
Q_FOREACH(Tado *tado, m_tadoAccounts){
|
Q_FOREACH(Tado *tado, m_tadoAccounts){
|
||||||
@ -388,13 +560,33 @@ void IntegrationPluginTado::onUsernameChanged(const QString &username)
|
|||||||
|
|
||||||
void IntegrationPluginTado::onRequestExecuted(QUuid requestId, bool success)
|
void IntegrationPluginTado::onRequestExecuted(QUuid requestId, bool success)
|
||||||
{
|
{
|
||||||
if (m_asyncActions.contains(requestId)) {
|
if (!m_pendingRequests.contains(requestId)) {
|
||||||
ThingActionInfo *info = m_asyncActions.take(requestId);
|
return;
|
||||||
if (success) {
|
}
|
||||||
info->finish(Thing::ThingErrorNoError);
|
|
||||||
} else {
|
PendingRequest request = m_pendingRequests.take(requestId);
|
||||||
info->finish(Thing::ThingErrorHardwareNotAvailable);
|
finishPendingActions(request.actions,
|
||||||
|
success ? Thing::ThingErrorNoError : Thing::ThingErrorHardwareNotAvailable);
|
||||||
|
|
||||||
|
if (!m_pendingOverlayChanges.contains(request.zoneKey)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PendingOverlayChange &pending = m_pendingOverlayChanges[request.zoneKey];
|
||||||
|
pending.inFlightValid = false;
|
||||||
|
if (success) {
|
||||||
|
pending.lastSynced = request.sentState;
|
||||||
|
pending.lastSyncedValid = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending.lastSyncedValid && overlayStatesEqual(pending.desired, pending.lastSynced)) {
|
||||||
|
pending.dirty = false;
|
||||||
|
if (!pending.pendingActions.isEmpty()) {
|
||||||
|
finishPendingActions(pending.pendingActions, Thing::ThingErrorNoError);
|
||||||
|
pending.pendingActions.clear();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
pending.dirty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -30,6 +30,7 @@
|
|||||||
#include <network/oauth2.h>
|
#include <network/oauth2.h>
|
||||||
|
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
|
#include <QList>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
#include "extern-plugininfo.h"
|
#include "extern-plugininfo.h"
|
||||||
@ -55,14 +56,46 @@ public:
|
|||||||
void executeAction(ThingActionInfo *info) override;
|
void executeAction(ThingActionInfo *info) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct OverlayState {
|
||||||
|
bool deleteOverlay = false;
|
||||||
|
bool power = false;
|
||||||
|
double temperature = 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PendingOverlayChange {
|
||||||
|
ThingId accountThingId;
|
||||||
|
QString homeId;
|
||||||
|
QString zoneId;
|
||||||
|
OverlayState desired;
|
||||||
|
OverlayState lastSynced;
|
||||||
|
bool dirty = false;
|
||||||
|
bool lastSyncedValid = false;
|
||||||
|
bool inFlightValid = false;
|
||||||
|
QList<ThingActionInfo *> pendingActions;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PendingRequest {
|
||||||
|
QString zoneKey;
|
||||||
|
QList<ThingActionInfo *> actions;
|
||||||
|
OverlayState sentState;
|
||||||
|
};
|
||||||
|
|
||||||
PluginTimer *m_pluginTimer = nullptr;
|
PluginTimer *m_pluginTimer = nullptr;
|
||||||
QHash<ThingId, Tado *> m_unfinishedTadoAccounts;
|
QHash<ThingId, Tado *> m_unfinishedTadoAccounts;
|
||||||
|
|
||||||
QHash<ThingId, Tado *> m_tadoAccounts;
|
QHash<ThingId, Tado *> m_tadoAccounts;
|
||||||
QHash<QUuid, ThingActionInfo *> m_asyncActions;
|
QTimer m_stateSyncTimer;
|
||||||
|
QHash<QString, PendingOverlayChange> m_pendingOverlayChanges;
|
||||||
|
QHash<QUuid, PendingRequest> m_pendingRequests;
|
||||||
|
|
||||||
|
static QString buildZoneKey(const ThingId &accountThingId, const QString &homeId, const QString &zoneId);
|
||||||
|
static bool overlayStatesEqual(const OverlayState &first, const OverlayState &second);
|
||||||
|
void queueOverlayChange(ThingActionInfo *info, const QString &homeId, const QString &zoneId, const OverlayState &desired);
|
||||||
|
void removePendingAction(ThingActionInfo *info);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onPluginTimer();
|
void onPluginTimer();
|
||||||
|
void syncPendingOverlays();
|
||||||
|
|
||||||
void onConnectionChanged(bool connected);
|
void onConnectionChanged(bool connected);
|
||||||
void onAuthenticationStatusChanged(bool authenticated);
|
void onAuthenticationStatusChanged(bool authenticated);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user