fix some param handling issues

This commit is contained in:
Michael Zanetti 2014-07-06 21:11:55 +02:00
parent f32abc8a2e
commit 824464e09b
26 changed files with 178 additions and 96 deletions

View File

@ -172,20 +172,28 @@ QList<DeviceClass> DeviceManager::supportedDevices(const VendorId &vendorId) con
return ret; return ret;
} }
DeviceManager::DeviceError DeviceManager::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const QPair<DeviceManager::DeviceError, QString> DeviceManager::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{ {
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); DeviceClass deviceClass = findDeviceClass(deviceClassId);
if (!deviceClass.isValid()) { if (!deviceClass.isValid()) {
return DeviceManager::DeviceErrorDeviceClassNotFound; return qMakePair<DeviceError, QString>(DeviceManager::DeviceErrorDeviceClassNotFound, deviceClass.id().toString());
} }
if (deviceClass.createMethod() != DeviceClass::CreateMethodDiscovery) { if (deviceClass.createMethod() != DeviceClass::CreateMethodDiscovery) {
return DeviceManager::DeviceErrorCreationMethodNotSupported; return qMakePair<DeviceError, QString>(DeviceManager::DeviceErrorCreationMethodNotSupported, "");
}
QPair<DeviceError, QString> 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()); DevicePlugin *plugin = m_devicePlugins.value(deviceClass.pluginId());
if (!plugin) { if (!plugin) {
return DeviceManager::DeviceErrorPluginNotFound; return qMakePair<DeviceError, QString>(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. /*! 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 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. needs to do in order to set up the device.
*/ */
QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> &params, const DeviceId id) QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList &params, const DeviceId id)
{ {
DeviceClass deviceClass = findDeviceClass(deviceClassId); DeviceClass deviceClass = findDeviceClass(deviceClassId);
if (!deviceClass.isValid()) { if (!deviceClass.isValid()) {
@ -330,8 +338,9 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::confirmPairing(const Q
return report(DeviceErrorPairingTransactionIdNotFound, pairingTransactionId.toString()); return report(DeviceErrorPairingTransactionIdNotFound, pairingTransactionId.toString());
} }
QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const QList<Param> &params, const DeviceId id) QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList &params, const DeviceId id)
{ {
ParamList effectiveParams = params;
DeviceClass deviceClass = findDeviceClass(deviceClassId); DeviceClass deviceClass = findDeviceClass(deviceClassId);
if (deviceClass.id().isNull()) { if (deviceClass.id().isNull()) {
qWarning() << "cannot find a device class with id" << deviceClassId; qWarning() << "cannot find a device class with id" << deviceClassId;
@ -343,7 +352,7 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDeviceInt
return qMakePair<DeviceError, QString>(DeviceErrorCreationMethodNotSupported, "You need to pair this device."); return qMakePair<DeviceError, QString>(DeviceErrorCreationMethodNotSupported, "You need to pair this device.");
} }
QPair<DeviceError, QString> result = verifyParams(deviceClass.paramTypes(), params); QPair<DeviceError, QString> result = verifyParams(deviceClass.paramTypes(), effectiveParams);
if (result.first != DeviceErrorNoError) { if (result.first != DeviceErrorNoError) {
return result; return result;
} }
@ -362,7 +371,7 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDeviceInt
Device *device = new Device(plugin->pluginId(), id, deviceClassId, this); Device *device = new Device(plugin->pluginId(), id, deviceClassId, this);
device->setName(deviceClass.name()); device->setName(deviceClass.name());
device->setParams(params); device->setParams(effectiveParams);
QPair<DeviceSetupStatus, QString> status = setupDevice(device); QPair<DeviceSetupStatus, QString> status = setupDevice(device);
switch (status.first) { 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}.*/ its \l{DevicePlugin}. Then will dispatch the execution to the \l{DevicePlugin}.*/
QPair<DeviceManager::DeviceError, QString> DeviceManager::executeAction(const Action &action) QPair<DeviceManager::DeviceError, QString> DeviceManager::executeAction(const Action &action)
{ {
Action finalAction = action;
qDebug() << "should execute action"; qDebug() << "should execute action";
foreach (Device *device, m_configuredDevices) { foreach (Device *device, m_configuredDevices) {
if (action.deviceId() == device->id()) { if (action.deviceId() == device->id()) {
@ -466,10 +476,12 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::executeAction(const Ac
foreach (const ActionType &actionType, deviceClass.actionTypes()) { foreach (const ActionType &actionType, deviceClass.actionTypes()) {
qDebug() << "checking" << actionType.id() << action.actionTypeId(); qDebug() << "checking" << actionType.id() << action.actionTypeId();
if (actionType.id() == action.actionTypeId()) { if (actionType.id() == action.actionTypeId()) {
QPair<DeviceError, QString> paramCheck = verifyParams(actionType.parameters(), action.params()); ParamList finalParams = action.params();
QPair<DeviceError, QString> paramCheck = verifyParams(actionType.parameters(), finalParams);
if (paramCheck.first != DeviceErrorNoError) { if (paramCheck.first != DeviceErrorNoError) {
return paramCheck; return paramCheck;
} }
finalAction.setParams(finalParams);
found = true; found = true;
continue; continue;
@ -479,7 +491,7 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::executeAction(const Ac
return qMakePair<DeviceError, QString>(DeviceErrorActionTypeNotFound, action.actionTypeId().toString()); return qMakePair<DeviceError, QString>(DeviceErrorActionTypeNotFound, action.actionTypeId().toString());
} }
return m_devicePlugins.value(device->pluginId())->executeAction(device, action); return m_devicePlugins.value(device->pluginId())->executeAction(device, finalAction);
} }
} }
return qMakePair<DeviceError, QString>(DeviceErrorDeviceNotFound, action.deviceId().toString()); return qMakePair<DeviceError, QString>(DeviceErrorDeviceNotFound, action.deviceId().toString());
@ -858,7 +870,7 @@ QPair<DeviceManager::DeviceSetupStatus,QString> DeviceManager::setupDevice(Devic
return status; return status;
} }
QPair<DeviceManager::DeviceError, QString> DeviceManager::verifyParams(const QList<ParamType> paramTypes, const QList<Param> &params, bool requireAll) QPair<DeviceManager::DeviceError, QString> DeviceManager::verifyParams(const QList<ParamType> paramTypes, ParamList &params, bool requireAll)
{ {
foreach (const Param &param, params) { foreach (const Param &param, params) {
qDebug() << "verifying param" << param.name() << paramTypes; qDebug() << "verifying param" << param.name() << paramTypes;
@ -877,6 +889,13 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::verifyParams(const QLi
found = true; 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) { if (!found) {
return report(DeviceErrorMissingParameter, QString("Missing parameter: %1").arg(paramType.name())); return report(DeviceErrorMissingParameter, QString("Missing parameter: %1").arg(paramType.name()));
} }
@ -910,6 +929,15 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::verifyParam(const Para
return report(DeviceManager::DeviceErrorInvalidParameter, QString("Value out of range for param %1. Got: %2. Min: %3.") 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())); .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();
} }
return report(DeviceErrorInvalidParameter, QString("Parameter name %1 does not match with ParamType name %2") return report(DeviceErrorInvalidParameter, QString("Parameter name %1 does not match with ParamType name %2")

View File

@ -78,10 +78,10 @@ public:
QList<Vendor> supportedVendors() const; QList<Vendor> supportedVendors() const;
QList<DeviceClass> supportedDevices(const VendorId &vendorId = VendorId()) const; QList<DeviceClass> supportedDevices(const VendorId &vendorId = VendorId()) const;
DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const; QPair<DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params);
QList<Device*> configuredDevices() const; QList<Device*> configuredDevices() const;
QPair<DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> &params, const DeviceId id = DeviceId::createDeviceId()); QPair<DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList &params, const DeviceId id = DeviceId::createDeviceId());
QPair<DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &id = DeviceId::createDeviceId()); QPair<DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &id = DeviceId::createDeviceId());
QPair<DeviceError, QString> pairDevice(const DeviceClassId &deviceClassId, const QList<Param> &params); QPair<DeviceError, QString> pairDevice(const DeviceClassId &deviceClassId, const QList<Param> &params);
QPair<DeviceError, QString> pairDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId); QPair<DeviceError, QString> pairDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId);
@ -121,9 +121,9 @@ private slots:
void timerEvent(); void timerEvent();
private: private:
QPair<DeviceError, QString> addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const QList<Param> &params, const DeviceId id = DeviceId::createDeviceId()); QPair<DeviceError, QString> addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList &params, const DeviceId id = DeviceId::createDeviceId());
QPair<DeviceSetupStatus, QString> setupDevice(Device *device); QPair<DeviceSetupStatus, QString> setupDevice(Device *device);
QPair<DeviceError, QString> verifyParams(const QList<ParamType> paramTypes, const QList<Param> &params, bool requireAll = true); QPair<DeviceError, QString> verifyParams(const QList<ParamType> paramTypes, ParamList &params, bool requireAll = true);
QPair<DeviceError, QString> verifyParam(const QList<ParamType> paramTypes, const Param &param); QPair<DeviceError, QString> verifyParam(const QList<ParamType> paramTypes, const Param &param);
QPair<DeviceError, QString> verifyParam(const ParamType &paramType, const Param &param); QPair<DeviceError, QString> verifyParam(const ParamType &paramType, const Param &param);

View File

@ -76,12 +76,12 @@ void DeviceDescriptor::setDescription(const QString &description)
m_description = description; m_description = description;
} }
QList<Param> DeviceDescriptor::params() const ParamList DeviceDescriptor::params() const
{ {
return m_params; return m_params;
} }
void DeviceDescriptor::setParams(const QList<Param> &params) void DeviceDescriptor::setParams(const ParamList &params)
{ {
m_params = params; m_params = params;
} }

View File

@ -42,15 +42,15 @@ public:
QString description() const; QString description() const;
void setDescription(const QString &description); void setDescription(const QString &description);
QList<Param> params() const; ParamList params() const;
void setParams(const QList<Param> &params); void setParams(const ParamList &params);
private: private:
DeviceDescriptorId m_id; DeviceDescriptorId m_id;
DeviceClassId m_deviceClassId; DeviceClassId m_deviceClassId;
QString m_title; QString m_title;
QString m_description; QString m_description;
QList<Param> m_params; ParamList m_params;
}; };
Q_DECLARE_METATYPE(DeviceDescriptor) Q_DECLARE_METATYPE(DeviceDescriptor)

View File

@ -142,11 +142,11 @@ void DevicePlugin::startMonitoringAutoDevices()
be an async operation. Return DeviceErrorAsync or DeviceErrorNoError if the discovery be an async operation. Return DeviceErrorAsync or DeviceErrorNoError if the discovery
has been started successfully. Return an appropriate error otherwise. has been started successfully. Return an appropriate error otherwise.
Once devices are discovered, emit devicesDiscovered() once. */ Once devices are discovered, emit devicesDiscovered() once. */
DeviceManager::DeviceError DevicePlugin::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const QPair<DeviceManager::DeviceError, QString> DevicePlugin::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{ {
Q_UNUSED(deviceClassId) Q_UNUSED(deviceClassId)
Q_UNUSED(params) 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. /*! This will be called when a new device is created. The plugin has the chance to do some setup.

View File

@ -50,7 +50,7 @@ public:
virtual DeviceManager::HardwareResources requiredHardware() const = 0; virtual DeviceManager::HardwareResources requiredHardware() const = 0;
virtual void startMonitoringAutoDevices(); virtual void startMonitoringAutoDevices();
virtual DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const; virtual QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params);
virtual QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device); virtual QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device);
virtual void deviceRemoved(Device *device); virtual void deviceRemoved(Device *device);

View File

@ -64,13 +64,13 @@ DeviceId Action::deviceId() const
} }
/*! Returns the parameters for this Action.*/ /*! Returns the parameters for this Action.*/
QList<Param> Action::params() const ParamList Action::params() const
{ {
return m_params; 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()}*/ /*! 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<Param> &params) void Action::setParams(const ParamList &params)
{ {
m_params = params; m_params = params;
} }

View File

@ -36,15 +36,15 @@ public:
ActionTypeId actionTypeId() const; ActionTypeId actionTypeId() const;
DeviceId deviceId() const; DeviceId deviceId() const;
QList<Param> params() const; ParamList params() const;
void setParams(const QList<Param> &params); void setParams(const ParamList &params);
Param param(const QString &paramName) const; Param param(const QString &paramName) const;
private: private:
ActionId m_id; ActionId m_id;
ActionTypeId m_actionTypeId; ActionTypeId m_actionTypeId;
DeviceId m_deviceId; DeviceId m_deviceId;
QList<Param> m_params; ParamList m_params;
}; };
#endif // ACTION_H #endif // ACTION_H

View File

@ -67,3 +67,34 @@ QDebug operator<<(QDebug dbg, const QList<Param> &params)
return dbg.space(); return dbg.space();
} }
bool ParamList::hasParam(const QString &paramName) const
{
foreach (const Param &param, *this) {
if (param.name() == paramName) {
return true;
}
}
return false;
}
QVariant ParamList::paramValue(const QString &paramName) const
{
foreach (const Param &param, *this) {
if (param.name() == paramName) {
return param.value();
}
}
return QVariant();
}
void ParamList::setParamValue(const QString &paramName, const QVariant &value)
{
for (int i = 0; i < count(); i++) {
if (this->operator [](i).name() == paramName) {
this->operator [](i).setValue(value);
return;
}
}
}

View File

@ -44,4 +44,12 @@ Q_DECLARE_METATYPE(Param)
QDebug operator<<(QDebug dbg, const Param &param); QDebug operator<<(QDebug dbg, const Param &param);
QDebug operator<<(QDebug dbg, const QList<Param> &params); QDebug operator<<(QDebug dbg, const QList<Param> &params);
class ParamList: public QList<Param>
{
public:
bool hasParam(const QString &paramName) const;
QVariant paramValue(const QString &paramName) const;
void setParamValue(const QString &paramName, const QVariant &value);
};
#endif // PARAM_H #endif // PARAM_H

View File

@ -153,6 +153,11 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
DeviceClass deviceClassMockDiscovery(pluginId(), guhVendorId, mockDeviceDiscoveryClassId); DeviceClass deviceClassMockDiscovery(pluginId(), guhVendorId, mockDeviceDiscoveryClassId);
deviceClassMockDiscovery.setName("Mock Device (Discovery created)"); deviceClassMockDiscovery.setName("Mock Device (Discovery created)");
deviceClassMockDiscovery.setCreateMethod(DeviceClass::CreateMethodDiscovery); deviceClassMockDiscovery.setCreateMethod(DeviceClass::CreateMethodDiscovery);
QList<ParamType> paramTypes;
ParamType paramType = ParamType("resultCount", QVariant::Int, 2);
paramType.setAllowedValues(QList<QVariant>() << 1 << 2);
paramTypes.append(paramType);
deviceClassMockDiscovery.setDiscoveryParamTypes(paramTypes);
mockParams.clear(); mockParams.clear();
mockParams.append(portParam); mockParams.append(portParam);
@ -207,12 +212,13 @@ DeviceManager::HardwareResources DevicePluginMock::requiredHardware() const
return DeviceManager::HardwareResourceTimer; return DeviceManager::HardwareResourceTimer;
} }
DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const QPair<DeviceManager::DeviceError, QString> DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{ {
Q_UNUSED(deviceClassId) Q_UNUSED(deviceClassId)
Q_UNUSED(params) Q_UNUSED(params)
m_discoveredDeviceCount = params.paramValue("resultCount").toInt();
QTimer::singleShot(1000, this, SLOT(emitDevicesDiscovered())); QTimer::singleShot(1000, this, SLOT(emitDevicesDiscovered()));
return DeviceManager::DeviceErrorNoError; return report(DeviceManager::DeviceErrorNoError);
} }
QString DevicePluginMock::pluginName() const QString DevicePluginMock::pluginName() const
@ -261,7 +267,7 @@ void DevicePluginMock::startMonitoringAutoDevices()
{ {
DeviceDescriptor mockDescriptor(mockDeviceAutoClassId, "Mock Device (Auto created)"); DeviceDescriptor mockDescriptor(mockDeviceAutoClassId, "Mock Device (Auto created)");
QList<Param> params; ParamList params;
Param param("httpport", 4242); Param param("httpport", 4242);
params.append(param); params.append(param);
mockDescriptor.setParams(params); mockDescriptor.setParams(params);
@ -337,19 +343,23 @@ void DevicePluginMock::emitDevicesDiscovered()
{ {
QList<DeviceDescriptor> deviceDescriptors; QList<DeviceDescriptor> deviceDescriptors;
DeviceDescriptor d1(mockDeviceDiscoveryClassId, "Mock Device (Discovered)"); if (m_discoveredDeviceCount > 0) {
QList<Param> params; DeviceDescriptor d1(mockDeviceDiscoveryClassId, "Mock Device (Discovered)");
Param httpParam("httpport", "7777"); ParamList params;
params.append(httpParam); Param httpParam("httpport", "7777");
d1.setParams(params); params.append(httpParam);
deviceDescriptors.append(d1); d1.setParams(params);
deviceDescriptors.append(d1);
}
DeviceDescriptor d2(mockDeviceDiscoveryClassId, "Mock Device (Discovered)"); if (m_discoveredDeviceCount > 1) {
params.clear(); DeviceDescriptor d2(mockDeviceDiscoveryClassId, "Mock Device (Discovered)");
httpParam.setValue("7778"); ParamList params;
params.append(httpParam); Param httpParam("httpport", "7778");
d2.setParams(params); params.append(httpParam);
deviceDescriptors.append(d2); d2.setParams(params);
deviceDescriptors.append(d2);
}
emit devicesDiscovered(mockDeviceDiscoveryClassId, deviceDescriptors); emit devicesDiscovered(mockDeviceDiscoveryClassId, deviceDescriptors);
} }

View File

@ -39,7 +39,7 @@ public:
QList<Vendor> supportedVendors() const override; QList<Vendor> supportedVendors() const override;
QList<DeviceClass> supportedDevices() const override; QList<DeviceClass> supportedDevices() const override;
DeviceManager::HardwareResources requiredHardware() const override; DeviceManager::HardwareResources requiredHardware() const override;
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const override; QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
QString pluginName() const override; QString pluginName() const override;
PluginId pluginId() const override; PluginId pluginId() const override;
@ -66,6 +66,8 @@ private:
QHash<Device*, HttpDaemon*> m_daemons; QHash<Device*, HttpDaemon*> m_daemons;
QList<Device*> m_asyncSetupDevices; QList<Device*> m_asyncSetupDevices;
QList<QPair<Action, Device*> > m_asyncActions; QList<QPair<Action, Device*> > m_asyncActions;
int m_discoveredDeviceCount;
}; };
#endif // DEVICEPLUGINMOCK_H #endif // DEVICEPLUGINMOCK_H

View File

@ -423,10 +423,12 @@ QList<DeviceClass> DevicePluginOpenweathermap::supportedDevices() const
return ret; return ret;
} }
DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const QPair<DeviceManager::DeviceError, QString> DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{ {
qDebug() << "should discover devices with params:" << params;
QString location; QString location;
foreach (const Param &param, params) { foreach (const Param &param, params) {
qDebug() << "### got param:" << param;
if (param.name() == "location") { if (param.name() == "location") {
location = param.value().toString(); location = param.value().toString();
} }
@ -434,10 +436,10 @@ DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const Dev
if (location.isEmpty()){ if (location.isEmpty()){
m_openweaher->searchAutodetect(); m_openweaher->searchAutodetect();
return DeviceManager::DeviceErrorAsync; return report(DeviceManager::DeviceErrorAsync);
} }
m_openweaher->search(location); m_openweaher->search(location);
return DeviceManager::DeviceErrorAsync; return report(DeviceManager::DeviceErrorAsync);
} }
QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginOpenweathermap::setupDevice(Device *device) QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginOpenweathermap::setupDevice(Device *device)
@ -482,7 +484,7 @@ void DevicePluginOpenweathermap::searchResultsReady(const QList<QVariantMap> &ci
QList<DeviceDescriptor> retList; QList<DeviceDescriptor> retList;
foreach (QVariantMap elemant, cityList) { foreach (QVariantMap elemant, cityList) {
DeviceDescriptor descriptor(openweathermapDeviceClassId, elemant.value("name").toString(),elemant.value("country").toString()); DeviceDescriptor descriptor(openweathermapDeviceClassId, elemant.value("name").toString(),elemant.value("country").toString());
QList<Param> params; ParamList params;
Param locationParam("location", elemant.value("name")); Param locationParam("location", elemant.value("name"));
params.append(locationParam); params.append(locationParam);
Param countryParam("country", elemant.value("country")); Param countryParam("country", elemant.value("country"));

View File

@ -38,7 +38,7 @@ public:
QList<Vendor> supportedVendors() const override; QList<Vendor> supportedVendors() const override;
QList<DeviceClass> supportedDevices() const override; QList<DeviceClass> supportedDevices() const override;
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const override; QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device) override; QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device) override;
DeviceManager::HardwareResources requiredHardware() const override; DeviceManager::HardwareResources requiredHardware() const override;
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override; QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;

View File

@ -185,10 +185,12 @@ QList<ParamType> DevicePluginPhilipsHue::configurationDescription() const
return params; return params;
} }
DeviceManager::DeviceError DevicePluginPhilipsHue::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const QPair<DeviceManager::DeviceError, QString> DevicePluginPhilipsHue::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{ {
Q_UNUSED(deviceClassId)
Q_UNUSED(params)
m_discovery->findBridges(4000); m_discovery->findBridges(4000);
return DeviceManager::DeviceErrorAsync; return report(DeviceManager::DeviceErrorAsync);
} }
QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginPhilipsHue::setupDevice(Device *device) QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginPhilipsHue::setupDevice(Device *device)
@ -222,7 +224,7 @@ QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginPhilipsHue::setupDe
while (!m_unconfiguredLights.isEmpty()) { while (!m_unconfiguredLights.isEmpty()) {
Light *light = m_unconfiguredLights.takeFirst(); Light *light = m_unconfiguredLights.takeFirst();
DeviceDescriptor descriptor(hueDeviceClassAutoId, light->name()); DeviceDescriptor descriptor(hueDeviceClassAutoId, light->name());
QList<Param> params; ParamList params;
params.append(Param("number", light->id())); params.append(Param("number", light->id()));
params.append(Param("ip", light->ip().toString())); params.append(Param("ip", light->ip().toString()));
params.append(Param("username", light->username())); params.append(Param("username", light->username()));
@ -303,7 +305,7 @@ void DevicePluginPhilipsHue::discoveryDone(const QList<QHostAddress> &bridges)
QList<DeviceDescriptor> deviceDescriptors; QList<DeviceDescriptor> deviceDescriptors;
foreach (const QHostAddress &bridge, bridges) { foreach (const QHostAddress &bridge, bridges) {
DeviceDescriptor descriptor(hueDeviceClassId, "Philips Hue bridge", bridge.toString()); DeviceDescriptor descriptor(hueDeviceClassId, "Philips Hue bridge", bridge.toString());
QList<Param> params; ParamList params;
Param param("ip", bridge.toString()); Param param("ip", bridge.toString());
params.append(param); params.append(param);
Param userParam("username", "guh-" + QUuid::createUuid().toString().remove(QRegExp("[\\{\\}]*")).remove(QRegExp("\\-[0-9a-f\\-]*"))); Param userParam("username", "guh-" + QUuid::createUuid().toString().remove(QRegExp("[\\{\\}]*")).remove(QRegExp("\\-[0-9a-f\\-]*")));

View File

@ -46,7 +46,7 @@ public:
PluginId pluginId() const override; PluginId pluginId() const override;
QList<ParamType> configurationDescription() const override; QList<ParamType> configurationDescription() const override;
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) const override; QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device) override; QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device) override;

View File

@ -137,12 +137,12 @@ DeviceClass GuhCore::findDeviceClass(const DeviceClassId &deviceClassId) const
return m_deviceManager->findDeviceClass(deviceClassId); return m_deviceManager->findDeviceClass(deviceClassId);
} }
DeviceManager::DeviceError GuhCore::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params) QPair<DeviceManager::DeviceError, QString> GuhCore::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{ {
return m_deviceManager->discoverDevices(deviceClassId, params); return m_deviceManager->discoverDevices(deviceClassId, params);
} }
QPair<DeviceManager::DeviceError, QString> GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> &params, const DeviceId &newId) QPair<DeviceManager::DeviceError, QString> GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList &params, const DeviceId &newId)
{ {
return m_deviceManager->addConfiguredDevice(deviceClassId, params, newId); return m_deviceManager->addConfiguredDevice(deviceClassId, params, newId);
} }

View File

@ -49,8 +49,8 @@ public:
QList<Vendor> supportedVendors() const; QList<Vendor> supportedVendors() const;
QList<DeviceClass> supportedDevices(const VendorId &vendorId = VendorId()) const; QList<DeviceClass> supportedDevices(const VendorId &vendorId = VendorId()) const;
DeviceClass findDeviceClass(const DeviceClassId &deviceClassId) const; DeviceClass findDeviceClass(const DeviceClassId &deviceClassId) const;
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> &params); QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params);
QPair<DeviceManager::DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> &params, const DeviceId &newId); QPair<DeviceManager::DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList &params, const DeviceId &newId);
QPair<DeviceManager::DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &newId); QPair<DeviceManager::DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &newId);
QList<Device*> configuredDevices() const; QList<Device*> configuredDevices() const;
Device *findConfiguredDevice(const DeviceId &deviceId) const; Device *findConfiguredDevice(const DeviceId &deviceId) const;

View File

@ -59,7 +59,7 @@ JsonReply* ActionHandler::ExecuteAction(const QVariantMap &params)
DeviceId deviceId(params.value("deviceId").toString()); DeviceId deviceId(params.value("deviceId").toString());
ActionTypeId actionTypeId(params.value("actionTypeId").toString()); ActionTypeId actionTypeId(params.value("actionTypeId").toString());
QList<Param> actionParams = JsonTypes::unpackParams(params.value("params").toList()); ParamList actionParams = JsonTypes::unpackParams(params.value("params").toList());
Action action(actionTypeId, deviceId); Action action(actionTypeId, deviceId);
action.setParams(actionParams); action.setParams(actionParams);

View File

@ -249,14 +249,10 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap &params) const
DeviceClassId deviceClassId = DeviceClassId(params.value("deviceClassId").toString()); DeviceClassId deviceClassId = DeviceClassId(params.value("deviceClassId").toString());
QList<Param> discoveryParams; ParamList discoveryParams = JsonTypes::unpackParams(params.value("discoveryParams").toList());
foreach (const QVariant &discoveryParam, params.value("discoveryParams").toList()) {
Param dp(discoveryParam.toMap().keys().first(), discoveryParam.toMap().values().first());
discoveryParams.append(dp);
}
DeviceManager::DeviceError status = GuhCore::instance()->discoverDevices(deviceClassId, discoveryParams); QPair<DeviceManager::DeviceError, QString> status = GuhCore::instance()->discoverDevices(deviceClassId, discoveryParams);
switch (status) { switch (status.first) {
case DeviceManager::DeviceErrorAsync: case DeviceManager::DeviceErrorAsync:
case DeviceManager::DeviceErrorNoError: { case DeviceManager::DeviceErrorNoError: {
JsonReply *reply = createAsyncReply("GetDiscoveredDevices"); JsonReply *reply = createAsyncReply("GetDiscoveredDevices");
@ -264,7 +260,7 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap &params) const
return reply; return reply;
} }
case DeviceManager::DeviceErrorDeviceClassNotFound: 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; break;
case DeviceManager::DeviceErrorPluginNotFound: case DeviceManager::DeviceErrorPluginNotFound:
returns.insert("errorMessage", "Cannot discover devices. Plugin for DeviceClass not found."); returns.insert("errorMessage", "Cannot discover devices. Plugin for DeviceClass not found.");
@ -272,8 +268,11 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap &params) const
case DeviceManager::DeviceErrorCreationMethodNotSupported: case DeviceManager::DeviceErrorCreationMethodNotSupported:
returns.insert("errorMessage", "This device can't be discovered."); returns.insert("errorMessage", "This device can't be discovered.");
break; break;
case DeviceManager::DeviceErrorMissingParameter:
returns.insert("errorMessage", QString("Missing parameter: %1").arg(status.second));
break;
default: 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); returns.insert("success", false);
@ -330,11 +329,7 @@ JsonReply* DeviceHandler::SetPluginConfiguration(const QVariantMap &params)
{ {
QVariantMap returns; QVariantMap returns;
PluginId pluginId = PluginId(params.value("pluginId").toString()); PluginId pluginId = PluginId(params.value("pluginId").toString());
QList<Param> pluginParams; ParamList pluginParams = JsonTypes::unpackParams(params.value("configuration").toList());
foreach (const QVariant &param, params.value("configuration").toList()) {
qDebug() << "got param" << param;
pluginParams.append(JsonTypes::unpackParam(param.toMap()));
}
QPair<DeviceManager::DeviceError, QString> result = GuhCore::instance()->setPluginConfig(pluginId, pluginParams); QPair<DeviceManager::DeviceError, QString> result = GuhCore::instance()->setPluginConfig(pluginId, pluginParams);
returns.insert("success", result.first == DeviceManager::DeviceErrorNoError); returns.insert("success", result.first == DeviceManager::DeviceErrorNoError);
returns.insert("errorMessage", result.second); returns.insert("errorMessage", result.second);
@ -344,13 +339,7 @@ JsonReply* DeviceHandler::SetPluginConfiguration(const QVariantMap &params)
JsonReply* DeviceHandler::AddConfiguredDevice(const QVariantMap &params) JsonReply* DeviceHandler::AddConfiguredDevice(const QVariantMap &params)
{ {
DeviceClassId deviceClass(params.value("deviceClassId").toString()); DeviceClassId deviceClass(params.value("deviceClassId").toString());
QList<Param> deviceParams; ParamList deviceParams = JsonTypes::unpackParams(params.value("deviceParams").toList());
foreach (const QVariant &paramVariant, params.value("deviceParams").toList()) {
Param param(paramVariant.toMap().value("name").toString());
param.setValue(paramVariant.toMap().value("value"));
deviceParams.append(param);
}
DeviceDescriptorId deviceDescriptorId(params.value("deviceDescriptorId").toString()); DeviceDescriptorId deviceDescriptorId(params.value("deviceDescriptorId").toString());
DeviceId newDeviceId = DeviceId::createDeviceId(); DeviceId newDeviceId = DeviceId::createDeviceId();
QPair<DeviceManager::DeviceError, QString> status; QPair<DeviceManager::DeviceError, QString> status;
@ -410,12 +399,7 @@ JsonReply *DeviceHandler::PairDevice(const QVariantMap &params)
DeviceDescriptorId deviceDescriptorId(params.value("deviceDescriptorId").toString()); DeviceDescriptorId deviceDescriptorId(params.value("deviceDescriptorId").toString());
status = GuhCore::instance()->pairDevice(deviceClassId, deviceDescriptorId); status = GuhCore::instance()->pairDevice(deviceClassId, deviceDescriptorId);
} else { } else {
QList<Param> deviceParams; ParamList deviceParams = JsonTypes::unpackParams(params.value("deviceParams").toList());
foreach (const QString &paramName, params.value("deviceParams").toMap().keys()) {
Param param(paramName);
param.setValue(params.value("deviceParams").toMap().value(paramName));
deviceParams.append(param);
}
status = GuhCore::instance()->pairDevice(deviceClassId, deviceParams); status = GuhCore::instance()->pairDevice(deviceClassId, deviceParams);
} }

View File

@ -42,7 +42,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QStringList> #include <QStringList>
#define JSON_PROTOCOL_VERSION 3 #define JSON_PROTOCOL_VERSION 4
JsonRPCServer::JsonRPCServer(QObject *parent): JsonRPCServer::JsonRPCServer(QObject *parent):
JsonHandler(parent), JsonHandler(parent),

View File

@ -444,9 +444,9 @@ Param JsonTypes::unpackParam(const QVariantMap &paramMap)
return Param(name, value); return Param(name, value);
} }
QList<Param> JsonTypes::unpackParams(const QVariantList &paramList) ParamList JsonTypes::unpackParams(const QVariantList &paramList)
{ {
QList<Param> params; ParamList params;
foreach (const QVariant &paramVariant, paramList) { foreach (const QVariant &paramVariant, paramList) {
qDebug() << "unpacking param" << paramVariant; qDebug() << "unpacking param" << paramVariant;
params.append(unpackParam(paramVariant.toMap())); params.append(unpackParam(paramVariant.toMap()));

View File

@ -108,7 +108,7 @@ public:
static QVariantMap packRule(const Rule &rule); static QVariantMap packRule(const Rule &rule);
static Param unpackParam(const QVariantMap &paramMap); static Param unpackParam(const QVariantMap &paramMap);
static QList<Param> unpackParams(const QVariantList &paramList); static ParamList unpackParams(const QVariantList &paramList);
static ParamDescriptor unpackParamDescriptor(const QVariantMap &paramDescriptorMap); static ParamDescriptor unpackParamDescriptor(const QVariantMap &paramDescriptorMap);
static QList<ParamDescriptor> unpackParamDescriptors(const QVariantList &paramDescriptorList); static QList<ParamDescriptor> unpackParamDescriptors(const QVariantList &paramDescriptorList);
static EventDescriptor unpackEventDescriptor(const QVariantMap &eventDescriptorMap); static EventDescriptor unpackEventDescriptor(const QVariantMap &eventDescriptorMap);

View File

@ -114,7 +114,7 @@ RuleEngine::RuleEngine(QObject *parent) :
foreach (const QString &actionIdString, settings.childGroups()) { foreach (const QString &actionIdString, settings.childGroups()) {
settings.beginGroup(actionIdString); settings.beginGroup(actionIdString);
Action action = Action(ActionTypeId(settings.value("actionTypeId").toString()), DeviceId(settings.value("deviceId").toString())); Action action = Action(ActionTypeId(settings.value("actionTypeId").toString()), DeviceId(settings.value("deviceId").toString()));
QList<Param> params; ParamList params;
foreach (QString paramNameString, settings.childGroups()) { foreach (QString paramNameString, settings.childGroups()) {
if (paramNameString.startsWith("Param-")) { if (paramNameString.startsWith("Param-")) {
settings.beginGroup(paramNameString); settings.beginGroup(paramNameString);

View File

@ -1,4 +1,4 @@
3 4
{ {
"methods": { "methods": {
"Actions.ExecuteAction": { "Actions.ExecuteAction": {
@ -419,6 +419,9 @@
}, },
"ParamType": { "ParamType": {
"name": "string", "name": "string",
"o:allowedValues": [
"variant"
],
"o:defaultValue": "variant", "o:defaultValue": "variant",
"o:maxValue": "variant", "o:maxValue": "variant",
"o:minValue": "variant", "o:minValue": "variant",

View File

@ -328,10 +328,18 @@ void TestDevices::discoverDevices_data()
QTest::addColumn<DeviceClassId>("deviceClassId"); QTest::addColumn<DeviceClassId>("deviceClassId");
QTest::addColumn<int>("resultCount"); QTest::addColumn<int>("resultCount");
QTest::addColumn<bool>("success"); QTest::addColumn<bool>("success");
QTest::addColumn<QVariantList>("discoveryParams");
QTest::newRow("valid deviceClassId") << mockDeviceDiscoveryClassId << 2 << true; QVariantList discoveryParams;
QTest::newRow("invalid deviceClassId") << DeviceClassId::createDeviceClassId() << 0 << false; QVariantMap resultCountParam;
QTest::newRow("CreateMethodUser deviceClassId") << mockDeviceClassId << 0 << false; 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() void TestDevices::discoverDevices()
@ -339,9 +347,11 @@ void TestDevices::discoverDevices()
QFETCH(DeviceClassId, deviceClassId); QFETCH(DeviceClassId, deviceClassId);
QFETCH(int, resultCount); QFETCH(int, resultCount);
QFETCH(bool, success); QFETCH(bool, success);
QFETCH(QVariantList, discoveryParams);
QVariantMap params; QVariantMap params;
params.insert("deviceClassId", deviceClassId); params.insert("deviceClassId", deviceClassId);
params.insert("discoveryParams", discoveryParams);
QVariant response = injectAndWait("Devices.GetDiscoveredDevices", params); QVariant response = injectAndWait("Devices.GetDiscoveredDevices", params);
verifySuccess(response, success); verifySuccess(response, success);
@ -353,6 +363,8 @@ void TestDevices::discoverDevices()
if (success) { if (success) {
DeviceDescriptorId descriptorId = DeviceDescriptorId(response.toMap().value("params").toMap().value("deviceDescriptors").toList().first().toMap().value("id").toString()); 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()); params.insert("deviceDescriptorId", descriptorId.toString());
response = injectAndWait("Devices.AddConfiguredDevice", params); response = injectAndWait("Devices.AddConfiguredDevice", params);