From 7f26df1662ec39a01624fd89dd7e160e52ae7f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 5 Aug 2014 13:04:56 +0200 Subject: [PATCH] wemo plugin: switch finished. --- .../deviceplugins/wemo/devicepluginwemo.cpp | 38 +++++++++++++++---- plugins/deviceplugins/wemo/devicepluginwemo.h | 3 ++ plugins/deviceplugins/wemo/wemoswitch.cpp | 27 +++++++++++-- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/plugins/deviceplugins/wemo/devicepluginwemo.cpp b/plugins/deviceplugins/wemo/devicepluginwemo.cpp index 615a3446..313eece0 100644 --- a/plugins/deviceplugins/wemo/devicepluginwemo.cpp +++ b/plugins/deviceplugins/wemo/devicepluginwemo.cpp @@ -140,9 +140,10 @@ QPair DevicePluginWemo::setupDevice(D wemoSwitch->setDeviceType(device->paramValue("device type").toString()); connect(wemoSwitch,SIGNAL(stateChanged()),this,SLOT(wemoSwitchStateChanged())); - + connect(wemoSwitch,SIGNAL(setPowerFinished(bool,ActionId)),this,SLOT(setPowerFinished(bool,ActionId))); m_wemoSwitches.insert(wemoSwitch,device); + wemoSwitch->refresh(); return reportDeviceSetup(); } return reportDeviceSetup(DeviceManager::DeviceSetupStatusSuccess); @@ -155,7 +156,18 @@ DeviceManager::HardwareResources DevicePluginWemo::requiredHardware() const QPair DevicePluginWemo::executeAction(Device *device, const Action &action) { - return report(); + if(device->deviceClassId() == wemoSwitchDeviceClassId){ + if(action.actionTypeId() == powerActionTypeId){ + WemoSwitch *wemoSwitch = m_wemoSwitches.key(device); + wemoSwitch->setPower(action.param("power").value().toBool(),action.id()); + + return report(DeviceManager::DeviceErrorAsync); + }else{ + return report(DeviceManager::DeviceErrorActionTypeNotFound); + } + } + + return report(DeviceManager::DeviceErrorDeviceClassNotFound); } void DevicePluginWemo::deviceRemoved(Device *device) @@ -181,8 +193,8 @@ PluginId DevicePluginWemo::pluginId() const void DevicePluginWemo::guhTimer() { - foreach (WemoSwitch* device, m_wemoSwitches.keys()) { - device->refresh(); + foreach (WemoSwitch* wemoSwitch, m_wemoSwitches.keys()) { + wemoSwitch->refresh(); } } @@ -210,9 +222,21 @@ void DevicePluginWemo::discoveryDone(QList deviceList) void DevicePluginWemo::wemoSwitchStateChanged() { + WemoSwitch *wemoSwitch = static_cast(sender()); - - - + if(m_wemoSwitches.contains(wemoSwitch)){ + Device * device = m_wemoSwitches.value(wemoSwitch); + device->setStateValue(powerStateTypeId, wemoSwitch->powerState()); + device->setStateValue(reachableStateTypeId, wemoSwitch->reachabel()); + } +} + +void DevicePluginWemo::setPowerFinished(const bool &succeeded, const ActionId &actionId) +{ + if(succeeded){ + emit actionExecutionFinished(actionId,DeviceManager::DeviceErrorNoError,QString()); + }else{ + emit actionExecutionFinished(actionId,DeviceManager::DeviceErrorDeviceNotFound,QString("Action could not be executed.")); + } } diff --git a/plugins/deviceplugins/wemo/devicepluginwemo.h b/plugins/deviceplugins/wemo/devicepluginwemo.h index 741d2610..0faf284f 100644 --- a/plugins/deviceplugins/wemo/devicepluginwemo.h +++ b/plugins/deviceplugins/wemo/devicepluginwemo.h @@ -53,6 +53,9 @@ public: private slots: void discoveryDone(QList deviceList); void wemoSwitchStateChanged(); + void setPowerFinished(const bool &succeeded, const ActionId &actionId); + + public slots: diff --git a/plugins/deviceplugins/wemo/wemoswitch.cpp b/plugins/deviceplugins/wemo/wemoswitch.cpp index c11665ac..0c1d98bd 100644 --- a/plugins/deviceplugins/wemo/wemoswitch.cpp +++ b/plugins/deviceplugins/wemo/wemoswitch.cpp @@ -131,6 +131,11 @@ bool WemoSwitch::powerState() return m_powerState; } +bool WemoSwitch::reachabel() +{ + return m_reachabel; +} + void WemoSwitch::replyFinished(QNetworkReply *reply) { if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200){ @@ -141,17 +146,27 @@ void WemoSwitch::replyFinished(QNetworkReply *reply) m_reachabel = true; } + // if this is the answerer to a refresh request if(reply == m_refrashReplay){ QByteArray data = reply->readAll(); if(data.contains("0")){ - qDebug() << "switch is off"; m_powerState = false; } if(data.contains("1")){ - qDebug() << "switch is on"; m_powerState = true; } } + // if this is the answerer to a "set power" request + if(reply == m_setPowerReplay){ + QByteArray data = reply->readAll(); + if(data.contains("1") || data.contains("0")){ + emit setPowerFinished(true,m_actionId); + }else{ + emit setPowerFinished(false,m_actionId); + } + refresh(); + } + emit stateChanged(); } @@ -172,13 +187,17 @@ void WemoSwitch::setPower(const bool &power, const ActionId &actionId) { m_actionId = actionId; + if(m_powerState == power){ + emit setPowerFinished(true,actionId); + } + QByteArray setPowerMessage("" + QByteArray::number((int)power) + ""); QNetworkRequest request; request.setUrl(QUrl("http://" + m_hostAddress.toString() + ":" + QString::number(m_port) + "/upnp/control/basicevent1")); request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml; charset=\"utf-8\"")); request.setHeader(QNetworkRequest::UserAgentHeader,QVariant("guh")); - request.setRawHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#GetBinaryState\""); + request.setRawHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\""); - m_refrashReplay = m_manager->post(request,setPowerMessage); + m_setPowerReplay = m_manager->post(request,setPowerMessage); }