fix some param handling issues
This commit is contained in:
parent
f32abc8a2e
commit
824464e09b
@ -172,20 +172,28 @@ QList<DeviceClass> DeviceManager::supportedDevices(const VendorId &vendorId) con
|
||||
return ret;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DeviceManager::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const
|
||||
QPair<DeviceManager::DeviceError, QString> 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<DeviceError, QString>(DeviceManager::DeviceErrorDeviceClassNotFound, deviceClass.id().toString());
|
||||
}
|
||||
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());
|
||||
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.
|
||||
@ -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::DeviceError, QString> DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> ¶ms, const DeviceId id)
|
||||
QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id)
|
||||
{
|
||||
DeviceClass deviceClass = findDeviceClass(deviceClassId);
|
||||
if (!deviceClass.isValid()) {
|
||||
@ -330,8 +338,9 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::confirmPairing(const Q
|
||||
return report(DeviceErrorPairingTransactionIdNotFound, pairingTransactionId.toString());
|
||||
}
|
||||
|
||||
QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const QList<Param> ¶ms, const DeviceId id)
|
||||
QPair<DeviceManager::DeviceError, QString> 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::DeviceError, QString> DeviceManager::addConfiguredDeviceInt
|
||||
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) {
|
||||
return result;
|
||||
}
|
||||
@ -362,7 +371,7 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::addConfiguredDeviceInt
|
||||
|
||||
Device *device = new Device(plugin->pluginId(), id, deviceClassId, this);
|
||||
device->setName(deviceClass.name());
|
||||
device->setParams(params);
|
||||
device->setParams(effectiveParams);
|
||||
|
||||
QPair<DeviceSetupStatus, QString> 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::DeviceError, QString> 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::DeviceError, QString> DeviceManager::executeAction(const Ac
|
||||
foreach (const ActionType &actionType, deviceClass.actionTypes()) {
|
||||
qDebug() << "checking" << 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) {
|
||||
return paramCheck;
|
||||
}
|
||||
finalAction.setParams(finalParams);
|
||||
|
||||
found = true;
|
||||
continue;
|
||||
@ -479,7 +491,7 @@ QPair<DeviceManager::DeviceError, QString> DeviceManager::executeAction(const Ac
|
||||
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());
|
||||
@ -858,7 +870,7 @@ QPair<DeviceManager::DeviceSetupStatus,QString> DeviceManager::setupDevice(Devic
|
||||
return status;
|
||||
}
|
||||
|
||||
QPair<DeviceManager::DeviceError, QString> DeviceManager::verifyParams(const QList<ParamType> paramTypes, const QList<Param> ¶ms, bool requireAll)
|
||||
QPair<DeviceManager::DeviceError, QString> DeviceManager::verifyParams(const QList<ParamType> paramTypes, ParamList ¶ms, bool requireAll)
|
||||
{
|
||||
foreach (const Param ¶m, params) {
|
||||
qDebug() << "verifying param" << param.name() << paramTypes;
|
||||
@ -877,6 +889,13 @@ QPair<DeviceManager::DeviceError, QString> 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::DeviceError, QString> 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")
|
||||
|
||||
@ -78,10 +78,10 @@ public:
|
||||
|
||||
QList<Vendor> supportedVendors() const;
|
||||
QList<DeviceClass> supportedDevices(const VendorId &vendorId = VendorId()) const;
|
||||
DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const;
|
||||
QPair<DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms);
|
||||
|
||||
QList<Device*> configuredDevices() const;
|
||||
QPair<DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> ¶ms, const DeviceId id = DeviceId::createDeviceId());
|
||||
QPair<DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, 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> ¶ms);
|
||||
QPair<DeviceError, QString> pairDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId);
|
||||
@ -121,9 +121,9 @@ private slots:
|
||||
void timerEvent();
|
||||
|
||||
private:
|
||||
QPair<DeviceError, QString> addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const QList<Param> ¶ms, const DeviceId id = DeviceId::createDeviceId());
|
||||
QPair<DeviceError, QString> addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id = DeviceId::createDeviceId());
|
||||
QPair<DeviceSetupStatus, QString> setupDevice(Device *device);
|
||||
QPair<DeviceError, QString> verifyParams(const QList<ParamType> paramTypes, const QList<Param> ¶ms, bool requireAll = true);
|
||||
QPair<DeviceError, QString> verifyParams(const QList<ParamType> paramTypes, ParamList ¶ms, bool requireAll = true);
|
||||
QPair<DeviceError, QString> verifyParam(const QList<ParamType> paramTypes, const Param ¶m);
|
||||
QPair<DeviceError, QString> verifyParam(const ParamType ¶mType, const Param ¶m);
|
||||
|
||||
|
||||
@ -76,12 +76,12 @@ void DeviceDescriptor::setDescription(const QString &description)
|
||||
m_description = description;
|
||||
}
|
||||
|
||||
QList<Param> DeviceDescriptor::params() const
|
||||
ParamList DeviceDescriptor::params() const
|
||||
{
|
||||
return m_params;
|
||||
}
|
||||
|
||||
void DeviceDescriptor::setParams(const QList<Param> ¶ms)
|
||||
void DeviceDescriptor::setParams(const ParamList ¶ms)
|
||||
{
|
||||
m_params = params;
|
||||
}
|
||||
|
||||
@ -42,15 +42,15 @@ public:
|
||||
QString description() const;
|
||||
void setDescription(const QString &description);
|
||||
|
||||
QList<Param> params() const;
|
||||
void setParams(const QList<Param> ¶ms);
|
||||
ParamList params() const;
|
||||
void setParams(const ParamList ¶ms);
|
||||
|
||||
private:
|
||||
DeviceDescriptorId m_id;
|
||||
DeviceClassId m_deviceClassId;
|
||||
QString m_title;
|
||||
QString m_description;
|
||||
QList<Param> m_params;
|
||||
ParamList m_params;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(DeviceDescriptor)
|
||||
|
||||
@ -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<Param> ¶ms) const
|
||||
QPair<DeviceManager::DeviceError, QString> 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.
|
||||
|
||||
@ -50,7 +50,7 @@ public:
|
||||
virtual DeviceManager::HardwareResources requiredHardware() const = 0;
|
||||
|
||||
virtual void startMonitoringAutoDevices();
|
||||
virtual DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const;
|
||||
virtual QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms);
|
||||
|
||||
virtual QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device);
|
||||
virtual void deviceRemoved(Device *device);
|
||||
|
||||
@ -64,13 +64,13 @@ DeviceId Action::deviceId() const
|
||||
}
|
||||
|
||||
/*! Returns the parameters for this Action.*/
|
||||
QList<Param> 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<Param> ¶ms)
|
||||
void Action::setParams(const ParamList ¶ms)
|
||||
{
|
||||
m_params = params;
|
||||
}
|
||||
|
||||
@ -36,15 +36,15 @@ public:
|
||||
ActionTypeId actionTypeId() const;
|
||||
DeviceId deviceId() const;
|
||||
|
||||
QList<Param> params() const;
|
||||
void setParams(const QList<Param> ¶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<Param> m_params;
|
||||
ParamList m_params;
|
||||
};
|
||||
|
||||
#endif // ACTION_H
|
||||
|
||||
@ -67,3 +67,34 @@ QDebug operator<<(QDebug dbg, const QList<Param> ¶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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,4 +44,12 @@ Q_DECLARE_METATYPE(Param)
|
||||
QDebug operator<<(QDebug dbg, const Param ¶m);
|
||||
QDebug operator<<(QDebug dbg, const QList<Param> ¶ms);
|
||||
|
||||
class ParamList: public QList<Param>
|
||||
{
|
||||
public:
|
||||
bool hasParam(const QString ¶mName) const;
|
||||
QVariant paramValue(const QString ¶mName) const;
|
||||
void setParamValue(const QString ¶mName, const QVariant &value);
|
||||
};
|
||||
|
||||
#endif // PARAM_H
|
||||
|
||||
@ -153,6 +153,11 @@ QList<DeviceClass> DevicePluginMock::supportedDevices() const
|
||||
DeviceClass deviceClassMockDiscovery(pluginId(), guhVendorId, mockDeviceDiscoveryClassId);
|
||||
deviceClassMockDiscovery.setName("Mock Device (Discovery created)");
|
||||
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.append(portParam);
|
||||
@ -207,12 +212,13 @@ DeviceManager::HardwareResources DevicePluginMock::requiredHardware() const
|
||||
return DeviceManager::HardwareResourceTimer;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const
|
||||
QPair<DeviceManager::DeviceError, QString> 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<Param> params;
|
||||
ParamList params;
|
||||
Param param("httpport", 4242);
|
||||
params.append(param);
|
||||
mockDescriptor.setParams(params);
|
||||
@ -337,19 +343,23 @@ void DevicePluginMock::emitDevicesDiscovered()
|
||||
{
|
||||
QList<DeviceDescriptor> deviceDescriptors;
|
||||
|
||||
DeviceDescriptor d1(mockDeviceDiscoveryClassId, "Mock Device (Discovered)");
|
||||
QList<Param> 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);
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ public:
|
||||
QList<Vendor> supportedVendors() const override;
|
||||
QList<DeviceClass> supportedDevices() const override;
|
||||
DeviceManager::HardwareResources requiredHardware() const override;
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const override;
|
||||
QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override;
|
||||
|
||||
QString pluginName() const override;
|
||||
PluginId pluginId() const override;
|
||||
@ -66,6 +66,8 @@ private:
|
||||
QHash<Device*, HttpDaemon*> m_daemons;
|
||||
QList<Device*> m_asyncSetupDevices;
|
||||
QList<QPair<Action, Device*> > m_asyncActions;
|
||||
|
||||
int m_discoveredDeviceCount;
|
||||
};
|
||||
|
||||
#endif // DEVICEPLUGINMOCK_H
|
||||
|
||||
@ -423,10 +423,12 @@ QList<DeviceClass> DevicePluginOpenweathermap::supportedDevices() const
|
||||
return ret;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const
|
||||
QPair<DeviceManager::DeviceError, QString> 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<DeviceManager::DeviceSetupStatus, QString> DevicePluginOpenweathermap::setupDevice(Device *device)
|
||||
@ -482,7 +484,7 @@ void DevicePluginOpenweathermap::searchResultsReady(const QList<QVariantMap> &ci
|
||||
QList<DeviceDescriptor> retList;
|
||||
foreach (QVariantMap elemant, cityList) {
|
||||
DeviceDescriptor descriptor(openweathermapDeviceClassId, elemant.value("name").toString(),elemant.value("country").toString());
|
||||
QList<Param> params;
|
||||
ParamList params;
|
||||
Param locationParam("location", elemant.value("name"));
|
||||
params.append(locationParam);
|
||||
Param countryParam("country", elemant.value("country"));
|
||||
|
||||
@ -38,7 +38,7 @@ public:
|
||||
QList<Vendor> supportedVendors() const override;
|
||||
QList<DeviceClass> supportedDevices() const override;
|
||||
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const override;
|
||||
QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override;
|
||||
QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device) override;
|
||||
DeviceManager::HardwareResources requiredHardware() const override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
@ -185,10 +185,12 @@ QList<ParamType> DevicePluginPhilipsHue::configurationDescription() const
|
||||
return params;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginPhilipsHue::discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const
|
||||
QPair<DeviceManager::DeviceError, QString> 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<DeviceManager::DeviceSetupStatus, QString> DevicePluginPhilipsHue::setupDevice(Device *device)
|
||||
@ -222,7 +224,7 @@ QPair<DeviceManager::DeviceSetupStatus, QString> DevicePluginPhilipsHue::setupDe
|
||||
while (!m_unconfiguredLights.isEmpty()) {
|
||||
Light *light = m_unconfiguredLights.takeFirst();
|
||||
DeviceDescriptor descriptor(hueDeviceClassAutoId, light->name());
|
||||
QList<Param> 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<QHostAddress> &bridges)
|
||||
QList<DeviceDescriptor> deviceDescriptors;
|
||||
foreach (const QHostAddress &bridge, bridges) {
|
||||
DeviceDescriptor descriptor(hueDeviceClassId, "Philips Hue bridge", bridge.toString());
|
||||
QList<Param> 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\\-]*")));
|
||||
|
||||
@ -46,7 +46,7 @@ public:
|
||||
PluginId pluginId() const override;
|
||||
|
||||
QList<ParamType> configurationDescription() const override;
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms) const override;
|
||||
QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override;
|
||||
|
||||
QPair<DeviceManager::DeviceSetupStatus, QString> setupDevice(Device *device) override;
|
||||
|
||||
|
||||
@ -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<Param> ¶ms)
|
||||
QPair<DeviceManager::DeviceError, QString> GuhCore::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms)
|
||||
{
|
||||
return m_deviceManager->discoverDevices(deviceClassId, params);
|
||||
}
|
||||
|
||||
QPair<DeviceManager::DeviceError, QString> GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> ¶ms, const DeviceId &newId)
|
||||
QPair<DeviceManager::DeviceError, QString> GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId &newId)
|
||||
{
|
||||
return m_deviceManager->addConfiguredDevice(deviceClassId, params, newId);
|
||||
}
|
||||
|
||||
@ -49,8 +49,8 @@ public:
|
||||
QList<Vendor> supportedVendors() const;
|
||||
QList<DeviceClass> supportedDevices(const VendorId &vendorId = VendorId()) const;
|
||||
DeviceClass findDeviceClass(const DeviceClassId &deviceClassId) const;
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QList<Param> ¶ms);
|
||||
QPair<DeviceManager::DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const QList<Param> ¶ms, const DeviceId &newId);
|
||||
QPair<DeviceManager::DeviceError, QString> discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms);
|
||||
QPair<DeviceManager::DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId &newId);
|
||||
QPair<DeviceManager::DeviceError, QString> addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &newId);
|
||||
QList<Device*> configuredDevices() const;
|
||||
Device *findConfiguredDevice(const DeviceId &deviceId) const;
|
||||
|
||||
@ -59,7 +59,7 @@ JsonReply* ActionHandler::ExecuteAction(const QVariantMap ¶ms)
|
||||
|
||||
DeviceId deviceId(params.value("deviceId").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.setParams(actionParams);
|
||||
|
||||
@ -249,14 +249,10 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap ¶ms) const
|
||||
|
||||
DeviceClassId deviceClassId = DeviceClassId(params.value("deviceClassId").toString());
|
||||
|
||||
QList<Param> 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<DeviceManager::DeviceError, QString> 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<Param> 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<DeviceManager::DeviceError, QString> 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<Param> 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<DeviceManager::DeviceError, QString> 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<Param> 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);
|
||||
}
|
||||
|
||||
|
||||
@ -42,7 +42,7 @@
|
||||
#include <QJsonDocument>
|
||||
#include <QStringList>
|
||||
|
||||
#define JSON_PROTOCOL_VERSION 3
|
||||
#define JSON_PROTOCOL_VERSION 4
|
||||
|
||||
JsonRPCServer::JsonRPCServer(QObject *parent):
|
||||
JsonHandler(parent),
|
||||
|
||||
@ -444,9 +444,9 @@ Param JsonTypes::unpackParam(const QVariantMap ¶mMap)
|
||||
return Param(name, value);
|
||||
}
|
||||
|
||||
QList<Param> JsonTypes::unpackParams(const QVariantList ¶mList)
|
||||
ParamList JsonTypes::unpackParams(const QVariantList ¶mList)
|
||||
{
|
||||
QList<Param> params;
|
||||
ParamList params;
|
||||
foreach (const QVariant ¶mVariant, paramList) {
|
||||
qDebug() << "unpacking param" << paramVariant;
|
||||
params.append(unpackParam(paramVariant.toMap()));
|
||||
|
||||
@ -108,7 +108,7 @@ public:
|
||||
static QVariantMap packRule(const Rule &rule);
|
||||
|
||||
static Param unpackParam(const QVariantMap ¶mMap);
|
||||
static QList<Param> unpackParams(const QVariantList ¶mList);
|
||||
static ParamList unpackParams(const QVariantList ¶mList);
|
||||
static ParamDescriptor unpackParamDescriptor(const QVariantMap ¶mDescriptorMap);
|
||||
static QList<ParamDescriptor> unpackParamDescriptors(const QVariantList ¶mDescriptorList);
|
||||
static EventDescriptor unpackEventDescriptor(const QVariantMap &eventDescriptorMap);
|
||||
|
||||
@ -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<Param> params;
|
||||
ParamList params;
|
||||
foreach (QString paramNameString, settings.childGroups()) {
|
||||
if (paramNameString.startsWith("Param-")) {
|
||||
settings.beginGroup(paramNameString);
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -328,10 +328,18 @@ void TestDevices::discoverDevices_data()
|
||||
QTest::addColumn<DeviceClassId>("deviceClassId");
|
||||
QTest::addColumn<int>("resultCount");
|
||||
QTest::addColumn<bool>("success");
|
||||
QTest::addColumn<QVariantList>("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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user