From 286c94891517e1878284271a8079a9318c3d4e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 8 Mar 2016 11:54:40 +0100 Subject: [PATCH] add awattar manual/auto mode and corresponding actions --- .../awattar/devicepluginawattar.cpp | 107 ++++++++++++++---- .../awattar/devicepluginawattar.h | 8 ++ .../awattar/devicepluginawattar.json | 34 +++++- plugins/deviceplugins/awattar/heatpump.cpp | 2 +- 4 files changed, 126 insertions(+), 25 deletions(-) diff --git a/plugins/deviceplugins/awattar/devicepluginawattar.cpp b/plugins/deviceplugins/awattar/devicepluginawattar.cpp index cd8f35af..16f2ed08 100644 --- a/plugins/deviceplugins/awattar/devicepluginawattar.cpp +++ b/plugins/deviceplugins/awattar/devicepluginawattar.cpp @@ -237,6 +237,45 @@ void DevicePluginAwattar::guhTimer() searchHeatPumps(); } +DeviceManager::DeviceError DevicePluginAwattar::executeAction(Device *device, const Action &action) +{ + if (!m_device || m_device != device) + return DeviceManager::DeviceErrorHardwareNotAvailable; + + if (action.actionTypeId() == sgSyncModeActionTypeId) { + qCDebug(dcAwattar) << "Set sg sync mode to" << action.param("sync mode").value(); + device->setStateValue(sgSyncModeStateTypeId, action.param("sync mode").value()); + if (action.param("sync mode").value() == "auto") + setSgMode(m_autoSgMode); + + return DeviceManager::DeviceErrorNoError; + } else if (action.actionTypeId() == setSgModeActionTypeId) { + if (!device->stateValue(reachableStateTypeId).toBool()) { + qCWarning(dcAwattar) << "Could not set SG mode. The pump is not reachable"; + return DeviceManager::DeviceErrorHardwareNotAvailable; + } + + device->setStateValue(sgSyncModeStateTypeId, "manual"); + QString sgModeString = action.param("sg-mode").value().toString(); + qCDebug(dcAwattar) << "Set manual SG mode to:" << sgModeString; + + if(sgModeString == "1 - Off") { + m_manualSgMode = 1; + } else if (sgModeString == "2 - Normal") { + m_manualSgMode = 2; + } else if (sgModeString == "3 - High Temperature") { + m_manualSgMode = 3; + } else if (sgModeString == "4 - On") { + m_manualSgMode = 4; + } + + setSgMode(m_manualSgMode); + return DeviceManager::DeviceErrorNoError; + } + + return DeviceManager::DeviceErrorActionTypeNotFound; +} + void DevicePluginAwattar::processPriceData(const QVariantMap &data) { if (!m_device) @@ -324,27 +363,13 @@ void DevicePluginAwattar::processUserData(const QVariantMap &data) } } - switch (sgMode) { - case 1: - m_device->setStateValue(sgModeStateTypeId, "1 - Off"); - break; - case 2: - m_device->setStateValue(sgModeStateTypeId, "2 - Normal"); - break; - case 3: - m_device->setStateValue(sgModeStateTypeId, "3 - High Temperature"); - break; - case 4: - m_device->setStateValue(sgModeStateTypeId, "4 - On"); - break; - default: - m_device->setStateValue(sgModeStateTypeId, "0 - Invalid"); - continue; - } + m_autoSgMode = sgMode; - foreach (HeatPump *pump, m_heatPumps) { - if (pump->reachable()) - pump->setSgMode(sgMode); + // sync the sg mode to each pump available + if (m_device->stateValue(sgSyncModeStateTypeId).toString() == "auto") { + setSgMode(m_autoSgMode); + } else { + setSgMode(m_manualSgMode); } } } @@ -413,14 +438,39 @@ void DevicePluginAwattar::searchHeatPumps() return; } - //qCDebug(dcAwattar) << "Search heat pump" << rplAddress.toString(); - QNetworkRequest request(QUrl(QString("http://[%1]").arg(rplAddress.toString()))); QNetworkReply *reply = networkManagerGet(request); m_searchPumpReplies.append(reply); } +void DevicePluginAwattar::setSgMode(const int &sgMode) +{ + switch (sgMode) { + case 1: + m_device->setStateValue(sgModeStateTypeId, "1 - Off"); + break; + case 2: + m_device->setStateValue(sgModeStateTypeId, "2 - Normal"); + break; + case 3: + m_device->setStateValue(sgModeStateTypeId, "3 - High Temperature"); + break; + case 4: + m_device->setStateValue(sgModeStateTypeId, "4 - On"); + break; + default: + m_device->setStateValue(sgModeStateTypeId, "0 - Invalid"); + return; + } + + foreach (HeatPump *pump, m_heatPumps) { + if (pump->reachable()) { + pump->setSgMode(sgMode); + } + } +} + bool DevicePluginAwattar::heatPumpExists(const QHostAddress &pumpAddress) { foreach (HeatPump *pump, m_heatPumps) { @@ -439,8 +489,19 @@ void DevicePluginAwattar::connectionTest() void DevicePluginAwattar::onHeatPumpReachableChanged() { HeatPump *pump = static_cast(sender()); + qCDebug(dcAwattar) << "Heatpump" << pump->address().toString() << " -> reachable" << pump->reachable(); + + // if there is still one pump reachable, lets keep the state true + // if no pump can be reached, the state will be set to false + bool reachable = false; + foreach (HeatPump *heatPump, m_heatPumps) { + if (heatPump->reachable()){ + reachable = true; + break; + } + } foreach (Device *device, myDevices()) { - device->setStateValue(reachableStateTypeId, pump->reachable()); + device->setStateValue(reachableStateTypeId, reachable); } } diff --git a/plugins/deviceplugins/awattar/devicepluginawattar.h b/plugins/deviceplugins/awattar/devicepluginawattar.h index e8a57a7b..f7dddf64 100644 --- a/plugins/deviceplugins/awattar/devicepluginawattar.h +++ b/plugins/deviceplugins/awattar/devicepluginawattar.h @@ -45,6 +45,8 @@ public: void guhTimer() override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; + private: Device *m_device; QList m_heatPumps; @@ -58,6 +60,9 @@ private: QString m_userUuid; int m_setupRetry; + int m_autoSgMode; + int m_manualSgMode; + void processPriceData(const QVariantMap &data); void processUserData(const QVariantMap &data); void processPumpSearchData(const QByteArray &data); @@ -67,6 +72,9 @@ private: void updateData(); void searchHeatPumps(); + + void setSgMode(const int &sgMode); + bool heatPumpExists(const QHostAddress &pumpAddress); private slots: diff --git a/plugins/deviceplugins/awattar/devicepluginawattar.json b/plugins/deviceplugins/awattar/devicepluginawattar.json index 5bf4618f..f4f22e32 100644 --- a/plugins/deviceplugins/awattar/devicepluginawattar.json +++ b/plugins/deviceplugins/awattar/devicepluginawattar.json @@ -52,7 +52,7 @@ "id": "38b86cee-9588-4269-a585-128907929dc2", "idName": "averageDeviation", "name": "average deviation", - "type": "double", + "type": "int", "unit": "Percentage", "defaultValue": 0 }, @@ -108,6 +108,38 @@ "4 - On" ], "defaultValue": "0 - Invalid" + }, + { + "id": "4c303bcd-152d-45ad-874d-d57fc87a26bb", + "idName": "sgSyncMode", + "name": "sync mode", + "type": "QString", + "possibleValues": [ + "auto", + "manual" + ], + "defaultValue": "auto", + "writable": true + } + ], + "actionTypes": [ + { + "id": "dd82f6c7-5e92-48ca-b0bc-bdc55d3e1482", + "idName": "setSgMode", + "name": "set sg-mode", + "paramTypes": [ + { + "name": "sg-mode", + "type": "QString", + "allowedValues": [ + "1 - Off", + "2 - Normal", + "3 - High Temperature", + "4 - On" + ], + "defaultValue": "2 - Normal" + } + ] } ] } diff --git a/plugins/deviceplugins/awattar/heatpump.cpp b/plugins/deviceplugins/awattar/heatpump.cpp index 060e86d7..10d7a949 100644 --- a/plugins/deviceplugins/awattar/heatpump.cpp +++ b/plugins/deviceplugins/awattar/heatpump.cpp @@ -59,7 +59,7 @@ bool HeatPump::reachable() const } void HeatPump::setSgMode(const int &sgMode) -{ +{ if (m_sgMode != sgMode) { m_sgMode = sgMode; qCDebug(dcAwattar) << "Setting sg-mode to" << sgMode;