From dd495719b19b5ed261c28a193d5825b0617c1c31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 24 Nov 2016 12:16:06 +0100 Subject: [PATCH] fix hue plugin and improve network detector shutdown mechanism --- .../devicepluginnetworkdetector.cpp | 25 ++++++++++++++++++- .../devicepluginnetworkdetector.h | 3 +++ .../philipshue/devicepluginphilipshue.cpp | 8 +++--- .../philipshue/devicepluginphilipshue.json | 8 +++--- .../deviceplugins/philipshue/huebridge.cpp | 5 +++- plugins/deviceplugins/philipshue/huelight.cpp | 16 ++++++++++-- 6 files changed, 53 insertions(+), 12 deletions(-) diff --git a/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.cpp b/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.cpp index 18db8303..bc8d4480 100644 --- a/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.cpp +++ b/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.cpp @@ -54,11 +54,30 @@ DevicePluginNetworkDetector::DevicePluginNetworkDetector(): m_discoveryProcess(0), - m_scanProcess(0) + m_scanProcess(0), + m_aboutToQuit(false) { } +DevicePluginNetworkDetector::~DevicePluginNetworkDetector() +{ + // Stop running processes + m_aboutToQuit = true; + + if (m_scanProcess && m_scanProcess->state() == QProcess::Running) { + qCDebug(dcNetworkDetector()) << "Kill running scan process"; + m_scanProcess->kill(); + m_scanProcess->waitForFinished(5000); + } + + if (m_discoveryProcess && m_discoveryProcess->state() == QProcess::Running) { + qCDebug(dcNetworkDetector()) << "Kill running discovery process"; + m_discoveryProcess->terminate(); + m_discoveryProcess->waitForFinished(5000); + } +} + DeviceManager::DeviceSetupStatus DevicePluginNetworkDetector::setupDevice(Device *device) { qCDebug(dcNetworkDetector()) << "Setup" << device->name() << device->params(); @@ -183,6 +202,10 @@ void DevicePluginNetworkDetector::processFinished(int exitCode, QProcess::ExitSt { QProcess *process = static_cast(sender()); + // If the process was killed because guhd is shutting down...we dont't care any more about the result + if (m_aboutToQuit) + return; + // Discovery if (process == m_discoveryProcess) { diff --git a/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.h b/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.h index 04b56817..a46843cb 100644 --- a/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.h +++ b/plugins/deviceplugins/networkdetector/devicepluginnetworkdetector.h @@ -37,6 +37,7 @@ class DevicePluginNetworkDetector : public DevicePlugin public: explicit DevicePluginNetworkDetector(); + ~DevicePluginNetworkDetector(); DeviceManager::DeviceSetupStatus setupDevice(Device *device) override; DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; @@ -50,6 +51,8 @@ private: QXmlStreamReader m_reader; + bool m_aboutToQuit; + QStringList getDefaultTargets(); QProcess *startScanProcesses(); diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp b/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp index bc52b3ba..48fe7af1 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp @@ -70,7 +70,7 @@ DevicePluginPhilipsHue::DevicePluginPhilipsHue() { m_timer = new QTimer(this); m_timer->setSingleShot(false); - m_timer->setInterval(2000); + m_timer->setInterval(5000); connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimeout())); } @@ -490,11 +490,11 @@ DeviceManager::DeviceError DevicePluginPhilipsHue::executeAction(Device *device, m_asyncActions.insert(networkManagerPut(request.first, request.second),QPair(device, action.id())); return DeviceManager::DeviceErrorAsync; } else if (action.actionTypeId() == hueAlertActionTypeId) { - QPair request = light->createFlashRequest(action.param(hueEffectStateParamTypeId).value().toString()); + QPair request = light->createFlashRequest(action.param(alertParamTypeId).value().toString()); m_asyncActions.insert(networkManagerPut(request.first, request.second),QPair(device, action.id())); return DeviceManager::DeviceErrorAsync; } else if (action.actionTypeId() == hueTemperatureActionTypeId) { - QPair request = light->createSetTemperatureRequest(action.param(hueColorStateParamTypeId).value().toInt()); + QPair request = light->createSetTemperatureRequest(action.param(hueTemperatureStateParamTypeId).value().toInt()); m_asyncActions.insert(networkManagerPut(request.first, request.second),QPair(device, action.id())); return DeviceManager::DeviceErrorAsync; } @@ -519,7 +519,7 @@ DeviceManager::DeviceError DevicePluginPhilipsHue::executeAction(Device *device, m_asyncActions.insert(networkManagerPut(request.first, request.second),QPair(device, action.id())); return DeviceManager::DeviceErrorAsync; } else if (action.actionTypeId() == hueAlertActionTypeId) { - QPair request = light->createFlashRequest(action.param(hueEffectStateParamTypeId).value().toString()); + QPair request = light->createFlashRequest(action.param(alertParamTypeId).value().toString()); m_asyncActions.insert(networkManagerPut(request.first, request.second),QPair(device, action.id())); return DeviceManager::DeviceErrorAsync; } diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.json b/plugins/deviceplugins/philipshue/devicepluginphilipshue.json index 1bf68d31..30b1a55c 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.json +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.json @@ -334,8 +334,8 @@ "type": "QString", "index": 0, "allowedValues": [ - "flash once", - "flash 30 seconds" + "flash", + "flash 15 [s]" ] } ] @@ -476,8 +476,8 @@ "type": "QString", "index": 0, "allowedValues": [ - "flash once", - "flash 30 seconds" + "flash", + "flash 15 [s]" ] } ] diff --git a/plugins/deviceplugins/philipshue/huebridge.cpp b/plugins/deviceplugins/philipshue/huebridge.cpp index 0d24277a..120855b6 100644 --- a/plugins/deviceplugins/philipshue/huebridge.cpp +++ b/plugins/deviceplugins/philipshue/huebridge.cpp @@ -144,7 +144,10 @@ QPair HueBridge::createCheckUpdatesRequest() updateMap.insert("checkforupdate", true); QVariantMap requestMap; - requestMap.insert("portalservices", true); + + // TODO: check if portalservice is true, cannot be done in one step + //requestMap.insert("portalservices", true); + requestMap.insert("swupdate", updateMap); QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap); diff --git a/plugins/deviceplugins/philipshue/huelight.cpp b/plugins/deviceplugins/philipshue/huelight.cpp index 222c9a12..4c7f286a 100644 --- a/plugins/deviceplugins/philipshue/huelight.cpp +++ b/plugins/deviceplugins/philipshue/huelight.cpp @@ -201,6 +201,8 @@ void HueLight::processActionResponse(const QVariantList &responseList) QPair HueLight::createSetPowerRequest(const bool &power) { + qCDebug(dcPhilipsHue()) << "Create power request" << power; + QVariantMap requestMap; requestMap.insert("on", power); @@ -214,6 +216,8 @@ QPair HueLight::createSetPowerRequest(const bool &p QPair HueLight::createSetColorRequest(const QColor &color) { + qCDebug(dcPhilipsHue()) << "Create color request" << color.toRgb(); + QVariantMap requestMap; requestMap.insert("hue", color.hue() * 65535 / 360); requestMap.insert("sat", color.saturation()); @@ -229,6 +233,8 @@ QPair HueLight::createSetColorRequest(const QColor QPair HueLight::createSetBrightnessRequest(const int &brightness) { + qCDebug(dcPhilipsHue()) << "Create brightness request" << brightness; + QVariantMap requestMap; requestMap.insert("bri", brightness); if (brightness == 0) { @@ -247,6 +253,8 @@ QPair HueLight::createSetBrightnessRequest(const in QPair HueLight::createSetEffectRequest(const QString &effect) { + qCDebug(dcPhilipsHue()) << "Create effect request" << effect; + QVariantMap requestMap; if (effect == "none") { requestMap.insert("effect", "none"); @@ -264,6 +272,8 @@ QPair HueLight::createSetEffectRequest(const QStrin QPair HueLight::createSetTemperatureRequest(const int &colorTemp) { + qCDebug(dcPhilipsHue()) << "Create color temperature request" << colorTemp; + QVariantMap requestMap; requestMap.insert("ct", colorTemp); requestMap.insert("on", true); @@ -278,10 +288,12 @@ QPair HueLight::createSetTemperatureRequest(const i QPair HueLight::createFlashRequest(const QString &alert) { + qCDebug(dcPhilipsHue()) << "Create flash request" << alert; + QVariantMap requestMap; - if (alert == "flash once") { + if (alert == "flash") { requestMap.insert("alert", "select"); - } else if (alert == "flash 30 seconds") { + } else if (alert == "flash 15 [s]") { requestMap.insert("alert", "lselect"); } QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);