diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 0ac39161..83e157de 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -172,20 +172,28 @@ QList DeviceManager::supportedDevices(const VendorId &vendorId) con return ret; } -DeviceManager::DeviceError DeviceManager::discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const +QPair DeviceManager::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { + qDebug() << "DeviceManager discoverdevices" << params; + // Create a copy of the parameter list because we might modify it (fillig in default values etc) + ParamList effectiveParams = params; DeviceClass deviceClass = findDeviceClass(deviceClassId); if (!deviceClass.isValid()) { - return DeviceManager::DeviceErrorDeviceClassNotFound; + return qMakePair(DeviceManager::DeviceErrorDeviceClassNotFound, deviceClass.id().toString()); } if (deviceClass.createMethod() != DeviceClass::CreateMethodDiscovery) { - return DeviceManager::DeviceErrorCreationMethodNotSupported; + return qMakePair(DeviceManager::DeviceErrorCreationMethodNotSupported, ""); + } + QPair result = verifyParams(deviceClass.discoveryParamTypes(), effectiveParams); + if (result.first != DeviceErrorNoError) { + qDebug() << "got erorr" << result.first << result.second; + return result; } DevicePlugin *plugin = m_devicePlugins.value(deviceClass.pluginId()); if (!plugin) { - return DeviceManager::DeviceErrorPluginNotFound; + return qMakePair(DeviceManager::DeviceErrorPluginNotFound, deviceClass.pluginId().toString()); } - return plugin->discoverDevices(deviceClassId, params); + return plugin->discoverDevices(deviceClassId, effectiveParams); } /*! Add a new configured device for the given \l{DeviceClass} and the given parameters. @@ -207,7 +215,7 @@ DeviceManager::DeviceError DeviceManager::discoverDevices(const DeviceClassId &d went wrong during setup. Reasons may be a hardware/network failure, wrong username/password or similar, depending on what the device plugin needs to do in order to set up the device. */ -QPair DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const QList ¶ms, const DeviceId id) +QPair DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id) { DeviceClass deviceClass = findDeviceClass(deviceClassId); if (!deviceClass.isValid()) { @@ -330,8 +338,9 @@ QPair DeviceManager::confirmPairing(const Q return report(DeviceErrorPairingTransactionIdNotFound, pairingTransactionId.toString()); } -QPair DeviceManager::addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const QList ¶ms, const DeviceId id) +QPair DeviceManager::addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id) { + ParamList effectiveParams = params; DeviceClass deviceClass = findDeviceClass(deviceClassId); if (deviceClass.id().isNull()) { qWarning() << "cannot find a device class with id" << deviceClassId; @@ -343,7 +352,7 @@ QPair DeviceManager::addConfiguredDeviceInt return qMakePair(DeviceErrorCreationMethodNotSupported, "You need to pair this device."); } - QPair result = verifyParams(deviceClass.paramTypes(), params); + QPair result = verifyParams(deviceClass.paramTypes(), effectiveParams); if (result.first != DeviceErrorNoError) { return result; } @@ -362,7 +371,7 @@ QPair DeviceManager::addConfiguredDeviceInt Device *device = new Device(plugin->pluginId(), id, deviceClassId, this); device->setName(deviceClass.name()); - device->setParams(params); + device->setParams(effectiveParams); QPair status = setupDevice(device); switch (status.first) { @@ -455,6 +464,7 @@ DeviceClass DeviceManager::findDeviceClass(const DeviceClassId &deviceClassId) c its \l{DevicePlugin}. Then will dispatch the execution to the \l{DevicePlugin}.*/ QPair DeviceManager::executeAction(const Action &action) { + Action finalAction = action; qDebug() << "should execute action"; foreach (Device *device, m_configuredDevices) { if (action.deviceId() == device->id()) { @@ -466,10 +476,12 @@ QPair DeviceManager::executeAction(const Ac foreach (const ActionType &actionType, deviceClass.actionTypes()) { qDebug() << "checking" << actionType.id() << action.actionTypeId(); if (actionType.id() == action.actionTypeId()) { - QPair paramCheck = verifyParams(actionType.parameters(), action.params()); + ParamList finalParams = action.params(); + QPair paramCheck = verifyParams(actionType.parameters(), finalParams); if (paramCheck.first != DeviceErrorNoError) { return paramCheck; } + finalAction.setParams(finalParams); found = true; continue; @@ -479,7 +491,7 @@ QPair DeviceManager::executeAction(const Ac return qMakePair(DeviceErrorActionTypeNotFound, action.actionTypeId().toString()); } - return m_devicePlugins.value(device->pluginId())->executeAction(device, action); + return m_devicePlugins.value(device->pluginId())->executeAction(device, finalAction); } } return qMakePair(DeviceErrorDeviceNotFound, action.deviceId().toString()); @@ -858,7 +870,7 @@ QPair DeviceManager::setupDevice(Devic return status; } -QPair DeviceManager::verifyParams(const QList paramTypes, const QList ¶ms, bool requireAll) +QPair DeviceManager::verifyParams(const QList paramTypes, ParamList ¶ms, bool requireAll) { foreach (const Param ¶m, params) { qDebug() << "verifying param" << param.name() << paramTypes; @@ -877,6 +889,13 @@ QPair DeviceManager::verifyParams(const QLi found = true; } } + + // This paramType has a default value... lets fill in that one. + if (!paramType.defaultValue().isNull()) { + found = true; + params.append(Param(paramType.name(), paramType.defaultValue())); + } + if (!found) { return report(DeviceErrorMissingParameter, QString("Missing parameter: %1").arg(paramType.name())); } @@ -910,6 +929,15 @@ QPair DeviceManager::verifyParam(const Para return report(DeviceManager::DeviceErrorInvalidParameter, QString("Value out of range for param %1. Got: %2. Min: %3.") .arg(param.name()).arg(param.value().toString()).arg(paramType.minValue().toString())); } + if (!paramType.allowedValues().isEmpty() && !paramType.allowedValues().contains(param.value())) { + QStringList allowedValues; + foreach (const QVariant &value, paramType.allowedValues()) { + allowedValues.append(value.toString()); + } + + return report(DeviceManager::DeviceErrorInvalidParameter, QString("Value not in allowed values for param %1. Got: %2. Allowed: %3.") + .arg(param.name()).arg(param.value().toString()).arg(allowedValues.join(","))); + } return report(); } return report(DeviceErrorInvalidParameter, QString("Parameter name %1 does not match with ParamType name %2") diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index 78e00f42..bba5ea98 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -78,10 +78,10 @@ public: QList supportedVendors() const; QList supportedDevices(const VendorId &vendorId = VendorId()) const; - DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const; + QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); QList configuredDevices() const; - QPair addConfiguredDevice(const DeviceClassId &deviceClassId, const QList ¶ms, const DeviceId id = DeviceId::createDeviceId()); + QPair addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id = DeviceId::createDeviceId()); QPair addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &id = DeviceId::createDeviceId()); QPair pairDevice(const DeviceClassId &deviceClassId, const QList ¶ms); QPair pairDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId); @@ -121,9 +121,9 @@ private slots: void timerEvent(); private: - QPair addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const QList ¶ms, const DeviceId id = DeviceId::createDeviceId()); + QPair addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id = DeviceId::createDeviceId()); QPair setupDevice(Device *device); - QPair verifyParams(const QList paramTypes, const QList ¶ms, bool requireAll = true); + QPair verifyParams(const QList paramTypes, ParamList ¶ms, bool requireAll = true); QPair verifyParam(const QList paramTypes, const Param ¶m); QPair verifyParam(const ParamType ¶mType, const Param ¶m); diff --git a/libguh/plugin/devicedescriptor.cpp b/libguh/plugin/devicedescriptor.cpp index 45365596..ebe0efe1 100644 --- a/libguh/plugin/devicedescriptor.cpp +++ b/libguh/plugin/devicedescriptor.cpp @@ -76,12 +76,12 @@ void DeviceDescriptor::setDescription(const QString &description) m_description = description; } -QList DeviceDescriptor::params() const +ParamList DeviceDescriptor::params() const { return m_params; } -void DeviceDescriptor::setParams(const QList ¶ms) +void DeviceDescriptor::setParams(const ParamList ¶ms) { m_params = params; } diff --git a/libguh/plugin/devicedescriptor.h b/libguh/plugin/devicedescriptor.h index 590c6f4a..19d37c58 100644 --- a/libguh/plugin/devicedescriptor.h +++ b/libguh/plugin/devicedescriptor.h @@ -42,15 +42,15 @@ public: QString description() const; void setDescription(const QString &description); - QList params() const; - void setParams(const QList ¶ms); + ParamList params() const; + void setParams(const ParamList ¶ms); private: DeviceDescriptorId m_id; DeviceClassId m_deviceClassId; QString m_title; QString m_description; - QList m_params; + ParamList m_params; }; Q_DECLARE_METATYPE(DeviceDescriptor) diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index f749bc60..7d059946 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -142,11 +142,11 @@ void DevicePlugin::startMonitoringAutoDevices() be an async operation. Return DeviceErrorAsync or DeviceErrorNoError if the discovery has been started successfully. Return an appropriate error otherwise. Once devices are discovered, emit devicesDiscovered() once. */ -DeviceManager::DeviceError DevicePlugin::discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const +QPair DevicePlugin::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { Q_UNUSED(deviceClassId) Q_UNUSED(params) - return DeviceManager::DeviceErrorCreationMethodNotSupported; + return report(DeviceManager::DeviceErrorCreationMethodNotSupported); } /*! This will be called when a new device is created. The plugin has the chance to do some setup. diff --git a/libguh/plugin/deviceplugin.h b/libguh/plugin/deviceplugin.h index a5860dc0..e3bac96a 100644 --- a/libguh/plugin/deviceplugin.h +++ b/libguh/plugin/deviceplugin.h @@ -50,7 +50,7 @@ public: virtual DeviceManager::HardwareResources requiredHardware() const = 0; virtual void startMonitoringAutoDevices(); - virtual DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const; + virtual QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); virtual QPair setupDevice(Device *device); virtual void deviceRemoved(Device *device); diff --git a/libguh/types/action.cpp b/libguh/types/action.cpp index 638e1d7b..f872b3e1 100644 --- a/libguh/types/action.cpp +++ b/libguh/types/action.cpp @@ -64,13 +64,13 @@ DeviceId Action::deviceId() const } /*! Returns the parameters for this Action.*/ -QList Action::params() const +ParamList Action::params() const { return m_params; } /*! Set the the parameters for this Action. \a params must match the template in the \l{ActionType} referred by \l{Action::actionTypeId()}*/ -void Action::setParams(const QList ¶ms) +void Action::setParams(const ParamList ¶ms) { m_params = params; } diff --git a/libguh/types/action.h b/libguh/types/action.h index 9b4eccac..8c4a297c 100644 --- a/libguh/types/action.h +++ b/libguh/types/action.h @@ -36,15 +36,15 @@ public: ActionTypeId actionTypeId() const; DeviceId deviceId() const; - QList params() const; - void setParams(const QList ¶ms); + ParamList params() const; + void setParams(const ParamList ¶ms); Param param(const QString ¶mName) const; private: ActionId m_id; ActionTypeId m_actionTypeId; DeviceId m_deviceId; - QList m_params; + ParamList m_params; }; #endif // ACTION_H diff --git a/libguh/types/param.cpp b/libguh/types/param.cpp index 98f8d0ac..d4d472fe 100644 --- a/libguh/types/param.cpp +++ b/libguh/types/param.cpp @@ -67,3 +67,34 @@ QDebug operator<<(QDebug dbg, const QList ¶ms) return dbg.space(); } + + +bool ParamList::hasParam(const QString ¶mName) const +{ + foreach (const Param ¶m, *this) { + if (param.name() == paramName) { + return true; + } + } + return false; +} + +QVariant ParamList::paramValue(const QString ¶mName) const +{ + foreach (const Param ¶m, *this) { + if (param.name() == paramName) { + return param.value(); + } + } + return QVariant(); +} + +void ParamList::setParamValue(const QString ¶mName, const QVariant &value) +{ + for (int i = 0; i < count(); i++) { + if (this->operator [](i).name() == paramName) { + this->operator [](i).setValue(value); + return; + } + } +} diff --git a/libguh/types/param.h b/libguh/types/param.h index 760ba8ef..37f1d542 100644 --- a/libguh/types/param.h +++ b/libguh/types/param.h @@ -44,4 +44,12 @@ Q_DECLARE_METATYPE(Param) QDebug operator<<(QDebug dbg, const Param ¶m); QDebug operator<<(QDebug dbg, const QList ¶ms); +class ParamList: public QList +{ +public: + bool hasParam(const QString ¶mName) const; + QVariant paramValue(const QString ¶mName) const; + void setParamValue(const QString ¶mName, const QVariant &value); +}; + #endif // PARAM_H diff --git a/plugins/deviceplugins/mock/devicepluginmock.cpp b/plugins/deviceplugins/mock/devicepluginmock.cpp index d41a10e5..8d4484db 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.cpp +++ b/plugins/deviceplugins/mock/devicepluginmock.cpp @@ -153,6 +153,11 @@ QList DevicePluginMock::supportedDevices() const DeviceClass deviceClassMockDiscovery(pluginId(), guhVendorId, mockDeviceDiscoveryClassId); deviceClassMockDiscovery.setName("Mock Device (Discovery created)"); deviceClassMockDiscovery.setCreateMethod(DeviceClass::CreateMethodDiscovery); + QList paramTypes; + ParamType paramType = ParamType("resultCount", QVariant::Int, 2); + paramType.setAllowedValues(QList() << 1 << 2); + paramTypes.append(paramType); + deviceClassMockDiscovery.setDiscoveryParamTypes(paramTypes); mockParams.clear(); mockParams.append(portParam); @@ -207,12 +212,13 @@ DeviceManager::HardwareResources DevicePluginMock::requiredHardware() const return DeviceManager::HardwareResourceTimer; } -DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const +QPair DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { Q_UNUSED(deviceClassId) Q_UNUSED(params) + m_discoveredDeviceCount = params.paramValue("resultCount").toInt(); QTimer::singleShot(1000, this, SLOT(emitDevicesDiscovered())); - return DeviceManager::DeviceErrorNoError; + return report(DeviceManager::DeviceErrorNoError); } QString DevicePluginMock::pluginName() const @@ -261,7 +267,7 @@ void DevicePluginMock::startMonitoringAutoDevices() { DeviceDescriptor mockDescriptor(mockDeviceAutoClassId, "Mock Device (Auto created)"); - QList params; + ParamList params; Param param("httpport", 4242); params.append(param); mockDescriptor.setParams(params); @@ -337,19 +343,23 @@ void DevicePluginMock::emitDevicesDiscovered() { QList deviceDescriptors; - DeviceDescriptor d1(mockDeviceDiscoveryClassId, "Mock Device (Discovered)"); - QList params; - Param httpParam("httpport", "7777"); - params.append(httpParam); - d1.setParams(params); - deviceDescriptors.append(d1); + if (m_discoveredDeviceCount > 0) { + DeviceDescriptor d1(mockDeviceDiscoveryClassId, "Mock Device (Discovered)"); + ParamList params; + Param httpParam("httpport", "7777"); + params.append(httpParam); + d1.setParams(params); + deviceDescriptors.append(d1); + } - DeviceDescriptor d2(mockDeviceDiscoveryClassId, "Mock Device (Discovered)"); - params.clear(); - httpParam.setValue("7778"); - params.append(httpParam); - d2.setParams(params); - deviceDescriptors.append(d2); + if (m_discoveredDeviceCount > 1) { + DeviceDescriptor d2(mockDeviceDiscoveryClassId, "Mock Device (Discovered)"); + ParamList params; + Param httpParam("httpport", "7778"); + params.append(httpParam); + d2.setParams(params); + deviceDescriptors.append(d2); + } emit devicesDiscovered(mockDeviceDiscoveryClassId, deviceDescriptors); } diff --git a/plugins/deviceplugins/mock/devicepluginmock.h b/plugins/deviceplugins/mock/devicepluginmock.h index e7fe37c5..ac35be73 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.h +++ b/plugins/deviceplugins/mock/devicepluginmock.h @@ -39,7 +39,7 @@ public: QList supportedVendors() const override; QList supportedDevices() const override; DeviceManager::HardwareResources requiredHardware() const override; - DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const override; + QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; QString pluginName() const override; PluginId pluginId() const override; @@ -66,6 +66,8 @@ private: QHash m_daemons; QList m_asyncSetupDevices; QList > m_asyncActions; + + int m_discoveredDeviceCount; }; #endif // DEVICEPLUGINMOCK_H diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp index c009b38f..9ccf2000 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp @@ -423,10 +423,12 @@ QList DevicePluginOpenweathermap::supportedDevices() const return ret; } -DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const +QPair DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { + qDebug() << "should discover devices with params:" << params; QString location; foreach (const Param ¶m, params) { + qDebug() << "### got param:" << param; if (param.name() == "location") { location = param.value().toString(); } @@ -434,10 +436,10 @@ DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const Dev if (location.isEmpty()){ m_openweaher->searchAutodetect(); - return DeviceManager::DeviceErrorAsync; + return report(DeviceManager::DeviceErrorAsync); } m_openweaher->search(location); - return DeviceManager::DeviceErrorAsync; + return report(DeviceManager::DeviceErrorAsync); } QPair DevicePluginOpenweathermap::setupDevice(Device *device) @@ -482,7 +484,7 @@ void DevicePluginOpenweathermap::searchResultsReady(const QList &ci QList retList; foreach (QVariantMap elemant, cityList) { DeviceDescriptor descriptor(openweathermapDeviceClassId, elemant.value("name").toString(),elemant.value("country").toString()); - QList params; + ParamList params; Param locationParam("location", elemant.value("name")); params.append(locationParam); Param countryParam("country", elemant.value("country")); diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h index 771f58ea..71d4083e 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h @@ -38,7 +38,7 @@ public: QList supportedVendors() const override; QList supportedDevices() const override; - DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const override; + QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; QPair setupDevice(Device *device) override; DeviceManager::HardwareResources requiredHardware() const override; QPair executeAction(Device *device, const Action &action) override; diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp b/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp index 038f9799..b8813fc2 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp @@ -185,10 +185,12 @@ QList DevicePluginPhilipsHue::configurationDescription() const return params; } -DeviceManager::DeviceError DevicePluginPhilipsHue::discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const +QPair DevicePluginPhilipsHue::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { + Q_UNUSED(deviceClassId) + Q_UNUSED(params) m_discovery->findBridges(4000); - return DeviceManager::DeviceErrorAsync; + return report(DeviceManager::DeviceErrorAsync); } QPair DevicePluginPhilipsHue::setupDevice(Device *device) @@ -222,7 +224,7 @@ QPair DevicePluginPhilipsHue::setupDe while (!m_unconfiguredLights.isEmpty()) { Light *light = m_unconfiguredLights.takeFirst(); DeviceDescriptor descriptor(hueDeviceClassAutoId, light->name()); - QList params; + ParamList params; params.append(Param("number", light->id())); params.append(Param("ip", light->ip().toString())); params.append(Param("username", light->username())); @@ -303,7 +305,7 @@ void DevicePluginPhilipsHue::discoveryDone(const QList &bridges) QList deviceDescriptors; foreach (const QHostAddress &bridge, bridges) { DeviceDescriptor descriptor(hueDeviceClassId, "Philips Hue bridge", bridge.toString()); - QList params; + ParamList params; Param param("ip", bridge.toString()); params.append(param); Param userParam("username", "guh-" + QUuid::createUuid().toString().remove(QRegExp("[\\{\\}]*")).remove(QRegExp("\\-[0-9a-f\\-]*"))); diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.h b/plugins/deviceplugins/philipshue/devicepluginphilipshue.h index 49c6c4a8..4da91943 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.h +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.h @@ -46,7 +46,7 @@ public: PluginId pluginId() const override; QList configurationDescription() const override; - DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) const override; + QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; QPair setupDevice(Device *device) override; diff --git a/server/guhcore.cpp b/server/guhcore.cpp index 26809643..5b168c8b 100644 --- a/server/guhcore.cpp +++ b/server/guhcore.cpp @@ -137,12 +137,12 @@ DeviceClass GuhCore::findDeviceClass(const DeviceClassId &deviceClassId) const return m_deviceManager->findDeviceClass(deviceClassId); } -DeviceManager::DeviceError GuhCore::discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms) +QPair GuhCore::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { return m_deviceManager->discoverDevices(deviceClassId, params); } -QPair GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const QList ¶ms, const DeviceId &newId) +QPair GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId &newId) { return m_deviceManager->addConfiguredDevice(deviceClassId, params, newId); } diff --git a/server/guhcore.h b/server/guhcore.h index 24146dcf..a4433ed3 100644 --- a/server/guhcore.h +++ b/server/guhcore.h @@ -49,8 +49,8 @@ public: QList supportedVendors() const; QList supportedDevices(const VendorId &vendorId = VendorId()) const; DeviceClass findDeviceClass(const DeviceClassId &deviceClassId) const; - DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList ¶ms); - QPair addConfiguredDevice(const DeviceClassId &deviceClassId, const QList ¶ms, const DeviceId &newId); + QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); + QPair addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId &newId); QPair addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &newId); QList configuredDevices() const; Device *findConfiguredDevice(const DeviceId &deviceId) const; diff --git a/server/jsonrpc/actionhandler.cpp b/server/jsonrpc/actionhandler.cpp index 7d448240..bb8c76d2 100644 --- a/server/jsonrpc/actionhandler.cpp +++ b/server/jsonrpc/actionhandler.cpp @@ -59,7 +59,7 @@ JsonReply* ActionHandler::ExecuteAction(const QVariantMap ¶ms) DeviceId deviceId(params.value("deviceId").toString()); ActionTypeId actionTypeId(params.value("actionTypeId").toString()); - QList actionParams = JsonTypes::unpackParams(params.value("params").toList()); + ParamList actionParams = JsonTypes::unpackParams(params.value("params").toList()); Action action(actionTypeId, deviceId); action.setParams(actionParams); diff --git a/server/jsonrpc/devicehandler.cpp b/server/jsonrpc/devicehandler.cpp index 50625dd2..4258de61 100644 --- a/server/jsonrpc/devicehandler.cpp +++ b/server/jsonrpc/devicehandler.cpp @@ -249,14 +249,10 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap ¶ms) const DeviceClassId deviceClassId = DeviceClassId(params.value("deviceClassId").toString()); - QList discoveryParams; - foreach (const QVariant &discoveryParam, params.value("discoveryParams").toList()) { - Param dp(discoveryParam.toMap().keys().first(), discoveryParam.toMap().values().first()); - discoveryParams.append(dp); - } + ParamList discoveryParams = JsonTypes::unpackParams(params.value("discoveryParams").toList()); - DeviceManager::DeviceError status = GuhCore::instance()->discoverDevices(deviceClassId, discoveryParams); - switch (status) { + QPair status = GuhCore::instance()->discoverDevices(deviceClassId, discoveryParams); + switch (status.first) { case DeviceManager::DeviceErrorAsync: case DeviceManager::DeviceErrorNoError: { JsonReply *reply = createAsyncReply("GetDiscoveredDevices"); @@ -264,7 +260,7 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap ¶ms) const return reply; } case DeviceManager::DeviceErrorDeviceClassNotFound: - returns.insert("errorMessage", "Cannot discover devices. Unknown DeviceClassId."); + returns.insert("errorMessage", QString("Cannot discover devices. Unknown DeviceClassId: %1").arg(status.second)); break; case DeviceManager::DeviceErrorPluginNotFound: returns.insert("errorMessage", "Cannot discover devices. Plugin for DeviceClass not found."); @@ -272,8 +268,11 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap ¶ms) const case DeviceManager::DeviceErrorCreationMethodNotSupported: returns.insert("errorMessage", "This device can't be discovered."); break; + case DeviceManager::DeviceErrorMissingParameter: + returns.insert("errorMessage", QString("Missing parameter: %1").arg(status.second)); + break; default: - returns.insert("errorMessage", QString("Unknown error %1").arg(status)); + returns.insert("errorMessage", QString("Unknown error %1 %2").arg(status.first).arg(status.second)); } returns.insert("success", false); @@ -330,11 +329,7 @@ JsonReply* DeviceHandler::SetPluginConfiguration(const QVariantMap ¶ms) { QVariantMap returns; PluginId pluginId = PluginId(params.value("pluginId").toString()); - QList pluginParams; - foreach (const QVariant ¶m, params.value("configuration").toList()) { - qDebug() << "got param" << param; - pluginParams.append(JsonTypes::unpackParam(param.toMap())); - } + ParamList pluginParams = JsonTypes::unpackParams(params.value("configuration").toList()); QPair result = GuhCore::instance()->setPluginConfig(pluginId, pluginParams); returns.insert("success", result.first == DeviceManager::DeviceErrorNoError); returns.insert("errorMessage", result.second); @@ -344,13 +339,7 @@ JsonReply* DeviceHandler::SetPluginConfiguration(const QVariantMap ¶ms) JsonReply* DeviceHandler::AddConfiguredDevice(const QVariantMap ¶ms) { DeviceClassId deviceClass(params.value("deviceClassId").toString()); - QList deviceParams; - foreach (const QVariant ¶mVariant, params.value("deviceParams").toList()) { - Param param(paramVariant.toMap().value("name").toString()); - param.setValue(paramVariant.toMap().value("value")); - deviceParams.append(param); - } - + ParamList deviceParams = JsonTypes::unpackParams(params.value("deviceParams").toList()); DeviceDescriptorId deviceDescriptorId(params.value("deviceDescriptorId").toString()); DeviceId newDeviceId = DeviceId::createDeviceId(); QPair status; @@ -410,12 +399,7 @@ JsonReply *DeviceHandler::PairDevice(const QVariantMap ¶ms) DeviceDescriptorId deviceDescriptorId(params.value("deviceDescriptorId").toString()); status = GuhCore::instance()->pairDevice(deviceClassId, deviceDescriptorId); } else { - QList deviceParams; - foreach (const QString ¶mName, params.value("deviceParams").toMap().keys()) { - Param param(paramName); - param.setValue(params.value("deviceParams").toMap().value(paramName)); - deviceParams.append(param); - } + ParamList deviceParams = JsonTypes::unpackParams(params.value("deviceParams").toList()); status = GuhCore::instance()->pairDevice(deviceClassId, deviceParams); } diff --git a/server/jsonrpc/jsonrpcserver.cpp b/server/jsonrpc/jsonrpcserver.cpp index 883c0627..4d3d5c57 100644 --- a/server/jsonrpc/jsonrpcserver.cpp +++ b/server/jsonrpc/jsonrpcserver.cpp @@ -42,7 +42,7 @@ #include #include -#define JSON_PROTOCOL_VERSION 3 +#define JSON_PROTOCOL_VERSION 4 JsonRPCServer::JsonRPCServer(QObject *parent): JsonHandler(parent), diff --git a/server/jsonrpc/jsontypes.cpp b/server/jsonrpc/jsontypes.cpp index 9a95bb40..589bcc8e 100644 --- a/server/jsonrpc/jsontypes.cpp +++ b/server/jsonrpc/jsontypes.cpp @@ -444,9 +444,9 @@ Param JsonTypes::unpackParam(const QVariantMap ¶mMap) return Param(name, value); } -QList JsonTypes::unpackParams(const QVariantList ¶mList) +ParamList JsonTypes::unpackParams(const QVariantList ¶mList) { - QList params; + ParamList params; foreach (const QVariant ¶mVariant, paramList) { qDebug() << "unpacking param" << paramVariant; params.append(unpackParam(paramVariant.toMap())); diff --git a/server/jsonrpc/jsontypes.h b/server/jsonrpc/jsontypes.h index 6b04e69e..9cce0109 100644 --- a/server/jsonrpc/jsontypes.h +++ b/server/jsonrpc/jsontypes.h @@ -108,7 +108,7 @@ public: static QVariantMap packRule(const Rule &rule); static Param unpackParam(const QVariantMap ¶mMap); - static QList unpackParams(const QVariantList ¶mList); + static ParamList unpackParams(const QVariantList ¶mList); static ParamDescriptor unpackParamDescriptor(const QVariantMap ¶mDescriptorMap); static QList unpackParamDescriptors(const QVariantList ¶mDescriptorList); static EventDescriptor unpackEventDescriptor(const QVariantMap &eventDescriptorMap); diff --git a/server/ruleengine.cpp b/server/ruleengine.cpp index 570a2d23..4790945a 100644 --- a/server/ruleengine.cpp +++ b/server/ruleengine.cpp @@ -114,7 +114,7 @@ RuleEngine::RuleEngine(QObject *parent) : foreach (const QString &actionIdString, settings.childGroups()) { settings.beginGroup(actionIdString); Action action = Action(ActionTypeId(settings.value("actionTypeId").toString()), DeviceId(settings.value("deviceId").toString())); - QList params; + ParamList params; foreach (QString paramNameString, settings.childGroups()) { if (paramNameString.startsWith("Param-")) { settings.beginGroup(paramNameString); diff --git a/tests/auto/api.json b/tests/auto/api.json index 774e6966..aff12c91 100644 --- a/tests/auto/api.json +++ b/tests/auto/api.json @@ -1,4 +1,4 @@ -3 +4 { "methods": { "Actions.ExecuteAction": { @@ -419,6 +419,9 @@ }, "ParamType": { "name": "string", + "o:allowedValues": [ + "variant" + ], "o:defaultValue": "variant", "o:maxValue": "variant", "o:minValue": "variant", diff --git a/tests/auto/devices/testdevices.cpp b/tests/auto/devices/testdevices.cpp index b82624c0..ac625d6c 100644 --- a/tests/auto/devices/testdevices.cpp +++ b/tests/auto/devices/testdevices.cpp @@ -328,10 +328,18 @@ void TestDevices::discoverDevices_data() QTest::addColumn("deviceClassId"); QTest::addColumn("resultCount"); QTest::addColumn("success"); + QTest::addColumn("discoveryParams"); - QTest::newRow("valid deviceClassId") << mockDeviceDiscoveryClassId << 2 << true; - QTest::newRow("invalid deviceClassId") << DeviceClassId::createDeviceClassId() << 0 << false; - QTest::newRow("CreateMethodUser deviceClassId") << mockDeviceClassId << 0 << false; + QVariantList discoveryParams; + QVariantMap resultCountParam; + resultCountParam.insert("name", "resultCount"); + resultCountParam.insert("value", 1); + discoveryParams.append(resultCountParam); + + QTest::newRow("valid deviceClassId") << mockDeviceDiscoveryClassId << 2 << true << QVariantList(); + QTest::newRow("valid deviceClassId with params") << mockDeviceDiscoveryClassId << 1 << true << discoveryParams; + QTest::newRow("invalid deviceClassId") << DeviceClassId::createDeviceClassId() << 0 << false << QVariantList(); + QTest::newRow("CreateMethodUser deviceClassId") << mockDeviceClassId << 0 << false << QVariantList(); } void TestDevices::discoverDevices() @@ -339,9 +347,11 @@ void TestDevices::discoverDevices() QFETCH(DeviceClassId, deviceClassId); QFETCH(int, resultCount); QFETCH(bool, success); + QFETCH(QVariantList, discoveryParams); QVariantMap params; params.insert("deviceClassId", deviceClassId); + params.insert("discoveryParams", discoveryParams); QVariant response = injectAndWait("Devices.GetDiscoveredDevices", params); verifySuccess(response, success); @@ -353,6 +363,8 @@ void TestDevices::discoverDevices() if (success) { DeviceDescriptorId descriptorId = DeviceDescriptorId(response.toMap().value("params").toMap().value("deviceDescriptors").toList().first().toMap().value("id").toString()); + params.clear(); + params.insert("deviceClassId", deviceClassId); params.insert("deviceDescriptorId", descriptorId.toString()); response = injectAndWait("Devices.AddConfiguredDevice", params);