From 1aba1643f856cd469318b8e0f55d70fe43ac3e44 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sat, 11 Oct 2014 19:00:59 +0200 Subject: [PATCH] first attempt to clean up type system --- libguh/devicemanager.cpp | 104 +++++++++--------- libguh/devicemanager.h | 18 +-- libguh/libguh.pro | 3 +- libguh/plugin/deviceclass.h | 3 + libguh/plugin/deviceplugin.cpp | 4 +- libguh/plugin/deviceplugin.h | 6 +- libguh/types/eventdescriptor.cpp | 12 +- libguh/types/paramdescriptor.cpp | 6 +- libguh/types/paramdescriptor.h | 6 +- libguh/types/statedescriptor.cpp | 18 +-- libguh/types/statedescriptor.h | 6 +- libguh/typeutils.h | 35 +++--- .../conrad/devicepluginconrad.cpp | 6 +- .../deviceplugins/conrad/devicepluginconrad.h | 2 +- .../deviceplugins/elro/devicepluginelro.cpp | 6 +- plugins/deviceplugins/elro/devicepluginelro.h | 2 +- .../deviceplugins/eq-3/deviceplugineq-3.cpp | 42 ++++--- plugins/deviceplugins/eq-3/deviceplugineq-3.h | 4 +- .../intertechno/devicepluginintertechno.cpp | 10 +- .../intertechno/devicepluginintertechno.h | 2 +- .../lgsmarttv/devicepluginlgsmarttv.cpp | 82 ++++---------- .../lgsmarttv/devicepluginlgsmarttv.h | 4 +- .../devicepluginmailnotification.cpp | 4 +- .../devicepluginmailnotification.h | 2 +- .../deviceplugins/mock/devicepluginmock.cpp | 14 +-- plugins/deviceplugins/mock/devicepluginmock.h | 4 +- .../devicepluginopenweathermap.cpp | 16 +-- .../devicepluginopenweathermap.h | 4 +- .../philipshue/devicepluginphilipshue.cpp | 13 ++- .../philipshue/devicepluginphilipshue.h | 4 +- .../wakeonlan/devicepluginwakeonlan.cpp | 4 +- .../wakeonlan/devicepluginwakeonlan.h | 2 +- .../deviceplugins/wemo/devicepluginwemo.cpp | 14 +-- plugins/deviceplugins/wemo/devicepluginwemo.h | 4 +- server/guhcore.cpp | 18 +-- server/guhcore.h | 8 +- server/jsonrpc/actionhandler.cpp | 31 ++---- server/jsonrpc/devicehandler.cpp | 78 +++---------- server/jsonrpc/jsonrpcserver.cpp | 15 ++- server/jsonrpc/jsonrpcserver.h | 2 + server/jsonrpc/jsontypes.cpp | 59 ++++++---- server/jsonrpc/jsontypes.h | 40 +++++-- server/jsonrpc/ruleshandler.cpp | 50 ++------- server/ruleengine.cpp | 2 +- server/ruleengine.h | 7 +- server/server.pri | 1 + server/stateevaluator.cpp | 14 +-- server/stateevaluator.h | 13 +-- tests/auto/actions/testactions.cpp | 34 +++--- tests/auto/devices/testdevices.cpp | 60 +++++----- tests/auto/guhtestbase.cpp | 6 +- tests/auto/guhtestbase.h | 3 +- tests/auto/jsonrpc/testjsonrpc.cpp | 6 +- tests/auto/rules/testrules.cpp | 90 +++++++-------- tests/auto/states/teststates.cpp | 12 +- 55 files changed, 474 insertions(+), 541 deletions(-) diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index e43b5825..332dc6e2 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -180,26 +180,25 @@ QList DeviceManager::supportedDevices(const VendorId &vendorId) con return ret; } -QPair DeviceManager::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError 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 qMakePair(DeviceManager::DeviceErrorDeviceClassNotFound, deviceClass.id().toString()); + return DeviceErrorDeviceClassNotFound; } if (!deviceClass.createMethods().testFlag(DeviceClass::CreateMethodDiscovery)) { - return qMakePair(DeviceManager::DeviceErrorCreationMethodNotSupported, ""); + return DeviceErrorCreationMethodNotSupported; } - QPair result = verifyParams(deviceClass.discoveryParamTypes(), effectiveParams); - if (result.first != DeviceErrorNoError) { - qDebug() << "got erorr" << result.first << result.second; + DeviceError result = verifyParams(deviceClass.discoveryParamTypes(), effectiveParams); + if (result != DeviceErrorNoError) { return result; } DevicePlugin *plugin = m_devicePlugins.value(deviceClass.pluginId()); if (!plugin) { - return qMakePair(DeviceManager::DeviceErrorPluginNotFound, deviceClass.pluginId().toString()); + return DeviceErrorPluginNotFound; } m_discoveringPlugins.append(plugin); QPair ret = plugin->discoverDevices(deviceClassId, effectiveParams); @@ -228,32 +227,31 @@ QPair DeviceManager::discoverDevices(const 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 ParamList ¶ms, const DeviceId id) +DeviceManager::DeviceError DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id) { DeviceClass deviceClass = findDeviceClass(deviceClassId); if (!deviceClass.isValid()) { - qWarning() << "cannot find a device class with id" << deviceClassId; - return qMakePair(DeviceErrorDeviceClassNotFound, deviceClassId.toString()); + return DeviceErrorDeviceClassNotFound; } if (deviceClass.createMethods().testFlag(DeviceClass::CreateMethodUser)) { return addConfiguredDeviceInternal(deviceClassId, params, id); } - return qMakePair(DeviceErrorCreationMethodNotSupported, "CreateMethodUser"); + return DeviceErrorCreationMethodNotSupported; } -QPair DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &deviceId) +DeviceManager::DeviceError DeviceManager::addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &deviceId) { DeviceClass deviceClass = findDeviceClass(deviceClassId); if (!deviceClass.isValid()) { - return qMakePair(DeviceErrorDeviceClassNotFound, deviceClassId.toString()); + return DeviceErrorDeviceClassNotFound; } if (!deviceClass.createMethods().testFlag(DeviceClass::CreateMethodDiscovery)) { - return qMakePair(DeviceErrorCreationMethodNotSupported, "CreateMethodDiscovery"); + return DeviceErrorCreationMethodNotSupported; } DeviceDescriptor descriptor = m_discoveredDevices.take(deviceDescriptorId); if (!descriptor.isValid()) { - return qMakePair(DeviceErrorDeviceDescriptorNotFound, deviceDescriptorId.toString()); + return DeviceErrorDeviceDescriptorNotFound; } return addConfiguredDeviceInternal(deviceClassId, descriptor.params(), deviceId); @@ -351,35 +349,32 @@ QPair DeviceManager::confirmPairing(const Q return report(DeviceErrorPairingTransactionIdNotFound, pairingTransactionId.toString()); } -QPair DeviceManager::addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id) +DeviceManager::DeviceError 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; - return qMakePair(DeviceErrorDeviceClassNotFound, deviceClassId.toString()); + return DeviceErrorDeviceClassNotFound; } if (deviceClass.setupMethod() != DeviceClass::SetupMethodJustAdd) { - qWarning() << "Cannot setup this device this way. You need to pair this device."; - return qMakePair(DeviceErrorCreationMethodNotSupported, "You need to pair this device."); + return DeviceErrorCreationMethodNotSupported; } - QPair result = verifyParams(deviceClass.paramTypes(), effectiveParams); - if (result.first != DeviceErrorNoError) { + DeviceError result = verifyParams(deviceClass.paramTypes(), effectiveParams); + if (result != DeviceErrorNoError) { return result; } foreach(Device *device, m_configuredDevices) { if (device->id() == id) { - return qMakePair(DeviceErrorDuplicateUuid, id.toString()); + return DeviceErrorDuplicateUuid; } } DevicePlugin *plugin = m_devicePlugins.value(deviceClass.pluginId()); if (!plugin) { - qWarning() << "Cannot find a plugin for this device class!"; - return qMakePair(DeviceErrorPluginNotFound, deviceClass.pluginId().toString()); + return DeviceErrorPluginNotFound; } Device *device = new Device(plugin->pluginId(), id, deviceClassId, this); @@ -391,9 +386,9 @@ QPair DeviceManager::addConfiguredDeviceInt case DeviceSetupStatusFailure: qWarning() << "Device setup failed. Not adding device to system."; delete device; - return qMakePair(DeviceErrorSetupFailed, QString("Device setup failed: %1").arg(status.second)); + return DeviceErrorSetupFailed; case DeviceSetupStatusAsync: - return qMakePair(DeviceErrorAsync, ""); + return DeviceErrorAsync; case DeviceSetupStatusSuccess: qDebug() << "Device setup complete."; break; @@ -402,7 +397,7 @@ QPair DeviceManager::addConfiguredDeviceInt m_configuredDevices.append(device); storeConfiguredDevices(); - return qMakePair(DeviceErrorNoError, QString()); + return DeviceErrorNoError; } QPair DeviceManager::removeConfiguredDevice(const DeviceId &deviceId) @@ -475,7 +470,7 @@ DeviceClass DeviceManager::findDeviceClass(const DeviceClassId &deviceClassId) c /*! Execute the given \{Action}. This will find the \l{Device} \a action refers to in \l{Action::deviceId()} and its \l{DevicePlugin}. Then will dispatch the execution to the \l{DevicePlugin}.*/ -QPair DeviceManager::executeAction(const Action &action) +DeviceManager::DeviceError DeviceManager::executeAction(const Action &action) { Action finalAction = action; qDebug() << "should execute action"; @@ -490,8 +485,8 @@ QPair DeviceManager::executeAction(const Ac qDebug() << "checking" << actionType.id() << action.actionTypeId(); if (actionType.id() == action.actionTypeId()) { ParamList finalParams = action.params(); - QPair paramCheck = verifyParams(actionType.paramTypes(), finalParams); - if (paramCheck.first != DeviceErrorNoError) { + DeviceError paramCheck = verifyParams(actionType.paramTypes(), finalParams); + if (paramCheck != DeviceErrorNoError) { return paramCheck; } finalAction.setParams(finalParams); @@ -501,13 +496,13 @@ QPair DeviceManager::executeAction(const Ac } } if (!found) { - return qMakePair(DeviceErrorActionTypeNotFound, action.actionTypeId().toString()); + return DeviceErrorActionTypeNotFound; } return m_devicePlugins.value(device->pluginId())->executeAction(device, finalAction); } } - return qMakePair(DeviceErrorDeviceNotFound, action.deviceId().toString()); + return DeviceErrorDeviceNotFound; } void DeviceManager::loadPlugins() @@ -926,17 +921,16 @@ QPair DeviceManager::setupDevice(Devic return status; } -QPair DeviceManager::verifyParams(const QList paramTypes, ParamList ¶ms, bool requireAll) +DeviceManager::DeviceError DeviceManager::verifyParams(const QList paramTypes, ParamList ¶ms, bool requireAll) { foreach (const Param ¶m, params) { - qDebug() << "verifying param" << param.name() << paramTypes; - QPair result = verifyParam(paramTypes, param); - if (result.first != DeviceErrorNoError) { + DeviceManager::DeviceError result = verifyParam(paramTypes, param); + if (result != DeviceErrorNoError) { return result; } } if (!requireAll) { - return report(); + return DeviceErrorNoError; } foreach (const ParamType ¶mType, paramTypes) { bool found = false; @@ -953,37 +947,39 @@ QPair DeviceManager::verifyParams(const QLi } if (!found) { - return report(DeviceErrorMissingParameter, QString("Missing parameter: %1").arg(paramType.name())); + qWarning() << "Missing parameter:" << paramType.name(); + return DeviceErrorMissingParameter; } } - return report(); + return DeviceErrorNoError; } -QPair DeviceManager::verifyParam(const QList paramTypes, const Param ¶m) +DeviceManager::DeviceError DeviceManager::verifyParam(const QList paramTypes, const Param ¶m) { foreach (const ParamType ¶mType, paramTypes) { if (paramType.name() == param.name()) { return verifyParam(paramType, param); } } - return report(DeviceErrorInvalidParameter, QString("Parameter %1 not in ParamTypes list").arg(param.name())); + qWarning() << "Invalid parameter" << param.name() << "in parameter list"; + return DeviceErrorInvalidParameter; } -QPair DeviceManager::verifyParam(const ParamType ¶mType, const Param ¶m) +DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType ¶mType, const Param ¶m) { if (paramType.name() == param.name()) { if (!param.value().canConvert(paramType.type())) { - return report(DeviceManager::DeviceErrorInvalidParameter, QString("Wrong parameter type for param %1. Got: %2. Expected %3.") - .arg(param.name()).arg(param.value().toString()).arg(QVariant::typeToName(paramType.type()))); + qWarning() << "Wrong parameter type for param" << param.name() << " Got:" << param.value() << " Expected:" << QVariant::typeToName(paramType.type()); + return DeviceErrorInvalidParameter; } if (paramType.maxValue().isValid() && param.value() > paramType.maxValue()) { - return report(DeviceManager::DeviceErrorInvalidParameter, QString("Value out of range for param %1. Got: %2. Max: %3.") - .arg(param.name()).arg(param.value().toString()).arg(paramType.maxValue().toString())); + qWarning() << "Value out of range for param" << param.name() << " Got:" << param.value() << " Max:" << paramType.maxValue(); + return DeviceErrorInvalidParameter; } if (paramType.minValue().isValid() && param.value() < paramType.minValue()) { - 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())); + qWarning() << "Value out of range for param" << param.name() << " Got:" << param.value() << " Min:" << paramType.minValue(); + return DeviceErrorInvalidParameter; } if (!paramType.allowedValues().isEmpty() && !paramType.allowedValues().contains(param.value())) { QStringList allowedValues; @@ -991,13 +987,13 @@ QPair DeviceManager::verifyParam(const Para 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(","))); + qWarning() << "Value not in allowed values for param" << param.name() << " Got:" << param.value() << " Allowed:" << allowedValues.join(","); + return DeviceErrorInvalidParameter; } - return report(); + return DeviceErrorNoError; } - return report(DeviceErrorInvalidParameter, QString("Parameter name %1 does not match with ParamType name %2") - .arg(param.name()).arg(paramType.name())); + qWarning() << "Parameter name" << param.name() << "does not match with ParamType name" << paramType.name(); + return DeviceErrorInvalidParameter; } QPair DeviceManager::report(DeviceManager::DeviceError error, const QString &message) diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index 376e4f9a..259381c4 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -37,6 +37,7 @@ class Radio433; class DeviceManager : public QObject { Q_OBJECT + Q_ENUMS(DeviceError) public: enum HardwareResource { HardwareResourceNone = 0x00, @@ -62,6 +63,7 @@ public: DeviceErrorDeviceDescriptorNotFound, DeviceErrorAsync, DeviceErrorPairingTransactionIdNotFound, + // Don't forget to update JsonTypes! }; enum DeviceSetupStatus { @@ -79,11 +81,11 @@ public: QList supportedVendors() const; QList supportedDevices(const VendorId &vendorId = VendorId()) const; - QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); + DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); QList configuredDevices() const; - 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()); + DeviceError addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id = DeviceId::createDeviceId()); + DeviceError addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &id = DeviceId::createDeviceId()); QPair pairDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms); QPair pairDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId); QPair confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString()); @@ -103,7 +105,7 @@ signals: void actionExecutionFinished(const ActionId, DeviceError status, const QString &errorMessage); public slots: - QPair executeAction(const Action &action); + DeviceError executeAction(const Action &action); private slots: void loadPlugins(); @@ -123,11 +125,11 @@ private slots: private: bool verifyPluginMetadata(const QJsonObject &data); - QPair addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id = DeviceId::createDeviceId()); + DeviceError addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId id = DeviceId::createDeviceId()); QPair setupDevice(Device *device); - 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); + DeviceError verifyParams(const QList paramTypes, ParamList ¶ms, bool requireAll = true); + DeviceError verifyParam(const QList paramTypes, const Param ¶m); + DeviceError verifyParam(const ParamType ¶mType, const Param ¶m); QPair report(DeviceError error = DeviceErrorNoError, const QString &message = QString()); diff --git a/libguh/libguh.pro b/libguh/libguh.pro index 1e5efa0d..cc733a6c 100644 --- a/libguh/libguh.pro +++ b/libguh/libguh.pro @@ -55,5 +55,6 @@ HEADERS += plugin/device.h \ types/paramtype.h \ types/param.h \ types/paramdescriptor.h \ - types/statedescriptor.h + types/statedescriptor.h \ + typeutils.h diff --git a/libguh/plugin/deviceclass.h b/libguh/plugin/deviceclass.h index d46c2f1a..687aa7bf 100644 --- a/libguh/plugin/deviceclass.h +++ b/libguh/plugin/deviceclass.h @@ -31,6 +31,9 @@ class DeviceClass { + Q_GADGET + Q_ENUMS(CreateMethod) + Q_ENUMS(SetupMethod) public: enum CreateMethod { CreateMethodUser = 0x01, diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index 6076c62a..cf193a25 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -235,11 +235,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. */ -QPair DevicePlugin::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError DevicePlugin::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { Q_UNUSED(deviceClassId) Q_UNUSED(params) - return report(DeviceManager::DeviceErrorCreationMethodNotSupported); + return 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 ec80e684..ab0cfefd 100644 --- a/libguh/plugin/deviceplugin.h +++ b/libguh/plugin/deviceplugin.h @@ -51,7 +51,7 @@ public: virtual DeviceManager::HardwareResources requiredHardware() const = 0; virtual void startMonitoringAutoDevices(); - virtual QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); + virtual DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); virtual QPair setupDevice(Device *device); virtual void deviceRemoved(Device *device); @@ -70,9 +70,9 @@ public: QPair setConfigValue(const QString ¶mName, const QVariant &value); public slots: - virtual QPair executeAction(Device *device, const Action &action) { + virtual DeviceManager::DeviceError executeAction(Device *device, const Action &action) { Q_UNUSED(device) Q_UNUSED(action) - return qMakePair(DeviceManager::DeviceErrorNoError, ""); + return DeviceManager::DeviceErrorNoError; } signals: diff --git a/libguh/types/eventdescriptor.cpp b/libguh/types/eventdescriptor.cpp index 2752327c..6ba6943c 100644 --- a/libguh/types/eventdescriptor.cpp +++ b/libguh/types/eventdescriptor.cpp @@ -99,32 +99,32 @@ bool EventDescriptor::operator ==(const Event &event) const foreach (const ParamDescriptor ¶mDescriptor, m_paramDescriptors) { switch (paramDescriptor.operatorType()) { - case ValueOperatorEquals: + case Types::ValueOperatorEquals: if (event.param(paramDescriptor.name()).value() != paramDescriptor.value()) { return false; } break; - case ValueOperatorNotEquals: + case Types::ValueOperatorNotEquals: if (event.param(paramDescriptor.name()).value() == paramDescriptor.value()) { return false; } break; - case ValueOperatorGreater: + case Types::ValueOperatorGreater: if (event.param(paramDescriptor.name()).value() <= paramDescriptor.value()) { return false; } break; - case ValueOperatorGreaterOrEqual: + case Types::ValueOperatorGreaterOrEqual: if (event.param(paramDescriptor.name()).value() < paramDescriptor.value()) { return false; } break; - case ValueOperatorLess: + case Types::ValueOperatorLess: if (event.param(paramDescriptor.name()).value() >= paramDescriptor.value()) { return false; } break; - case ValueOperatorLessOrEqual: + case Types::ValueOperatorLessOrEqual: if (event.param(paramDescriptor.name()).value() < paramDescriptor.value()) { return false; } diff --git a/libguh/types/paramdescriptor.cpp b/libguh/types/paramdescriptor.cpp index a64f69b8..8d94f8c3 100644 --- a/libguh/types/paramdescriptor.cpp +++ b/libguh/types/paramdescriptor.cpp @@ -20,16 +20,16 @@ ParamDescriptor::ParamDescriptor(const QString &name, const QVariant &value): Param(name, value), - m_operatorType(ValueOperatorEquals) + m_operatorType(Types::ValueOperatorEquals) { } -ValueOperator ParamDescriptor::operatorType() const +Types::ValueOperator ParamDescriptor::operatorType() const { return m_operatorType; } -void ParamDescriptor::setOperatorType(ValueOperator operatorType) +void ParamDescriptor::setOperatorType(Types::ValueOperator operatorType) { m_operatorType = operatorType; } diff --git a/libguh/types/paramdescriptor.h b/libguh/types/paramdescriptor.h index b9450449..0a464edc 100644 --- a/libguh/types/paramdescriptor.h +++ b/libguh/types/paramdescriptor.h @@ -27,11 +27,11 @@ class ParamDescriptor : public Param public: ParamDescriptor(const QString &name, const QVariant &value = QVariant()); - ValueOperator operatorType() const; - void setOperatorType(ValueOperator operatorType); + Types::ValueOperator operatorType() const; + void setOperatorType(Types::ValueOperator operatorType); private: - ValueOperator m_operatorType; + Types::ValueOperator m_operatorType; }; #endif // PARAMDESCRIPTOR_H diff --git a/libguh/types/statedescriptor.cpp b/libguh/types/statedescriptor.cpp index 92edc3a7..8ffaa8fe 100644 --- a/libguh/types/statedescriptor.cpp +++ b/libguh/types/statedescriptor.cpp @@ -19,12 +19,12 @@ #include "statedescriptor.h" StateDescriptor::StateDescriptor(): - m_operatorType(ValueOperatorEquals) + m_operatorType(Types::ValueOperatorEquals) { } -StateDescriptor::StateDescriptor(const StateTypeId &stateTypeId, const DeviceId &deviceId, const QVariant &stateValue, ValueOperator operatorType): +StateDescriptor::StateDescriptor(const StateTypeId &stateTypeId, const DeviceId &deviceId, const QVariant &stateValue, Types::ValueOperator operatorType): m_stateTypeId(stateTypeId), m_deviceId(deviceId), m_stateValue(stateValue), @@ -48,7 +48,7 @@ QVariant StateDescriptor::stateValue() const return m_stateValue; } -ValueOperator StateDescriptor::operatorType() const +Types::ValueOperator StateDescriptor::operatorType() const { return m_operatorType; } @@ -67,17 +67,17 @@ bool StateDescriptor::operator ==(const State &state) const return false; } switch (m_operatorType) { - case ValueOperatorEquals: + case Types::ValueOperatorEquals: return m_stateValue == state.value(); - case ValueOperatorGreater: + case Types::ValueOperatorGreater: return state.value() > m_stateValue; - case ValueOperatorGreaterOrEqual: + case Types::ValueOperatorGreaterOrEqual: return state.value() >= m_stateValue; - case ValueOperatorLess: + case Types::ValueOperatorLess: return state.value() < m_stateValue; - case ValueOperatorLessOrEqual: + case Types::ValueOperatorLessOrEqual: return state.value() <= m_stateValue; - case ValueOperatorNotEquals: + case Types::ValueOperatorNotEquals: return m_stateValue != state.value(); } return false; diff --git a/libguh/types/statedescriptor.h b/libguh/types/statedescriptor.h index e44f8871..05246daf 100644 --- a/libguh/types/statedescriptor.h +++ b/libguh/types/statedescriptor.h @@ -31,12 +31,12 @@ class StateDescriptor { public: StateDescriptor(); - StateDescriptor(const StateTypeId &stateTypeId, const DeviceId &deviceId, const QVariant &stateValue, ValueOperator operatorType = ValueOperatorEquals); + StateDescriptor(const StateTypeId &stateTypeId, const DeviceId &deviceId, const QVariant &stateValue, Types::ValueOperator operatorType = Types::ValueOperatorEquals); StateTypeId stateTypeId() const; DeviceId deviceId() const; QVariant stateValue() const; - ValueOperator operatorType() const; + Types::ValueOperator operatorType() const; bool operator ==(const StateDescriptor &other) const; @@ -47,7 +47,7 @@ private: StateTypeId m_stateTypeId; DeviceId m_deviceId; QVariant m_stateValue; - ValueOperator m_operatorType; + Types::ValueOperator m_operatorType; }; QDebug operator<<(QDebug dbg, const StateDescriptor &eventDescriptor); QDebug operator<<(QDebug dbg, const QList &eventDescriptors); diff --git a/libguh/typeutils.h b/libguh/typeutils.h index 86798553..2edfc03c 100644 --- a/libguh/typeutils.h +++ b/libguh/typeutils.h @@ -50,21 +50,28 @@ DECLARE_TYPE_ID(Action) DECLARE_TYPE_ID(Plugin) DECLARE_TYPE_ID(Rule) -enum ValueOperator { - ValueOperatorEquals, - ValueOperatorNotEquals, - ValueOperatorLess, - ValueOperatorGreater, - ValueOperatorLessOrEqual, - ValueOperatorGreaterOrEqual +class Types +{ + Q_GADGET + Q_ENUMS(StateOperator) + Q_ENUMS(ValueOperator) + +public: + enum ValueOperator { + ValueOperatorEquals, + ValueOperatorNotEquals, + ValueOperatorLess, + ValueOperatorGreater, + ValueOperatorLessOrEqual, + ValueOperatorGreaterOrEqual + }; + enum StateOperator { + StateOperatorAnd, + StateOperatorOr + }; }; -enum StateOperator { - StateOperatorAnd, - StateOperatorOr -}; - -Q_DECLARE_METATYPE(ValueOperator) -Q_DECLARE_METATYPE(StateOperator) +Q_DECLARE_METATYPE(Types::ValueOperator) +Q_DECLARE_METATYPE(Types::StateOperator) #endif // TYPEUTILS_H diff --git a/plugins/deviceplugins/conrad/devicepluginconrad.cpp b/plugins/deviceplugins/conrad/devicepluginconrad.cpp index a7d764ba..8effc217 100644 --- a/plugins/deviceplugins/conrad/devicepluginconrad.cpp +++ b/plugins/deviceplugins/conrad/devicepluginconrad.cpp @@ -72,7 +72,7 @@ DeviceManager::HardwareResources DevicePluginConrad::requiredHardware() const return DeviceManager::HardwareResourceRadio433; } -QPair DevicePluginConrad::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginConrad::executeAction(Device *device, const Action &action) { QList rawData; QByteArray binCode; @@ -117,10 +117,10 @@ QPair DevicePluginConrad::executeAction(Dev // send data to driver if(transmitData(delay, rawData)){ qDebug() << "action" << pluginName() << device->name() << "power: " << action.param("power").value().toBool(); - return report(); + return DeviceManager::DeviceErrorNoError; }else{ qDebug() << "could not transmitt" << pluginName() << device->name() << "power: " << action.param("power").value().toBool(); - return report(DeviceManager::DeviceErrorHardwareNotAvailable, "Radio 433 MHz transmitter not available."); + return DeviceManager::DeviceErrorHardwareNotAvailable; } } diff --git a/plugins/deviceplugins/conrad/devicepluginconrad.h b/plugins/deviceplugins/conrad/devicepluginconrad.h index 754e0895..0b7447e2 100644 --- a/plugins/deviceplugins/conrad/devicepluginconrad.h +++ b/plugins/deviceplugins/conrad/devicepluginconrad.h @@ -35,7 +35,7 @@ public: void radioData(const QList &rawData) override; public slots: - QPair executeAction(Device *device, const Action &action) override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; }; diff --git a/plugins/deviceplugins/elro/devicepluginelro.cpp b/plugins/deviceplugins/elro/devicepluginelro.cpp index 824a0710..516749fc 100644 --- a/plugins/deviceplugins/elro/devicepluginelro.cpp +++ b/plugins/deviceplugins/elro/devicepluginelro.cpp @@ -70,7 +70,7 @@ DeviceManager::HardwareResources DevicePluginElro::requiredHardware() const return DeviceManager::HardwareResourceRadio433; } -QPair DevicePluginElro::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginElro::executeAction(Device *device, const Action &action) { QList rawData; @@ -162,10 +162,10 @@ QPair DevicePluginElro::executeAction(Devic // send data to hardware resource if(transmitData(delay, rawData)){ qDebug() << "transmitted" << pluginName() << device->name() << "power: " << action.param("power").value().toBool(); - return report(); + return DeviceManager::DeviceErrorNoError; }else{ qDebug() << "could not transmitt" << pluginName() << device->name() << "power: " << action.param("power").value().toBool(); - return report(DeviceManager::DeviceErrorHardwareNotAvailable,QString("Radio 433 MHz transmitter not available.")); + return DeviceManager::DeviceErrorHardwareNotAvailable; } } diff --git a/plugins/deviceplugins/elro/devicepluginelro.h b/plugins/deviceplugins/elro/devicepluginelro.h index 2254a06b..588cfc3b 100644 --- a/plugins/deviceplugins/elro/devicepluginelro.h +++ b/plugins/deviceplugins/elro/devicepluginelro.h @@ -35,7 +35,7 @@ public: void radioData(const QList &rawData) override; public slots: - QPair executeAction(Device *device, const Action &action) override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; }; diff --git a/plugins/deviceplugins/eq-3/deviceplugineq-3.cpp b/plugins/deviceplugins/eq-3/deviceplugineq-3.cpp index 1be1e82f..1fc7ddfb 100644 --- a/plugins/deviceplugins/eq-3/deviceplugineq-3.cpp +++ b/plugins/deviceplugins/eq-3/deviceplugineq-3.cpp @@ -247,13 +247,13 @@ QList DevicePluginEQ3::configurationDescription() const return params; } -QPair DevicePluginEQ3::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError DevicePluginEQ3::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { if(deviceClassId == cubeDeviceClassId){ m_cubeDiscovery->detectCubes(); - return report(DeviceManager::DeviceErrorAsync); + return DeviceManager::DeviceErrorAsync; } - return report(DeviceManager::DeviceErrorDeviceClassNotFound); + return DeviceManager::DeviceErrorDeviceClassNotFound; } void DevicePluginEQ3::startMonitoringAutoDevices() @@ -317,36 +317,32 @@ void DevicePluginEQ3::guhTimer() } } -QPair DevicePluginEQ3::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginEQ3::executeAction(Device *device, const Action &action) { if(device->deviceClassId() == wallThermostateDeviceClassId || device->deviceClassId() == radiatorThermostateDeviceClassId){ foreach (MaxCube *cube, m_cubes.keys()){ if(cube->serialNumber() == device->paramValue("parent cube").toString()){ + + QByteArray rfAddress = device->paramValue("rf address").toByteArray(); + int roomId = device->paramValue("room id").toInt(); + if (action.actionTypeId() == setSetpointTemperatureActionTypeId){ - cube->setDeviceSetpointTemp(device->paramValue("rf address").toByteArray(), device->paramValue("room id").toInt(), action.param("setpoint temperature").value().toDouble(), action.id()); - return report(DeviceManager::DeviceErrorAsync,QString()); - } - if (action.actionTypeId() == setAutoModeActionTypeId){ - cube->setDeviceAutoMode(device->paramValue("rf address").toByteArray(), device->paramValue("room id").toInt(), action.id()); - return report(DeviceManager::DeviceErrorAsync,QString()); - } - if (action.actionTypeId() == setManuelModeActionTypeId){ - cube->setDeviceManuelMode(device->paramValue("rf address").toByteArray(), device->paramValue("room id").toInt(), action.id()); - return report(DeviceManager::DeviceErrorAsync,QString()); - } - if (action.actionTypeId() == setEcoModeActionTypeId){ - cube->setDeviceEcoMode(device->paramValue("rf address").toByteArray(), device->paramValue("room id").toInt(), action.id()); - return report(DeviceManager::DeviceErrorAsync,QString()); - } - if (action.actionTypeId() == displayCurrentTempActionTypeId){ - cube->displayCurrentTemperature(device->paramValue("rf address").toByteArray(), device->paramValue("room id").toInt(), action.param("display").value().toBool(), action.id()); - return report(DeviceManager::DeviceErrorAsync,QString()); + cube->setDeviceSetpointTemp(rfAddress, roomId, action.param("setpoint temperature").value().toDouble(), action.id()); + } else if (action.actionTypeId() == setAutoModeActionTypeId){ + cube->setDeviceAutoMode(rfAddress, roomId, action.id()); + } else if (action.actionTypeId() == setManuelModeActionTypeId){ + cube->setDeviceManuelMode(rfAddress, roomId, action.id()); + } else if (action.actionTypeId() == setEcoModeActionTypeId){ + cube->setDeviceEcoMode(rfAddress, roomId, action.id()); + } else if (action.actionTypeId() == displayCurrentTempActionTypeId){ + cube->displayCurrentTemperature(rfAddress, roomId, action.param("display").value().toBool(), action.id()); } + return DeviceManager::DeviceErrorAsync; } } } - return report(DeviceManager::DeviceErrorActionTypeNotFound,QString()); + return DeviceManager::DeviceErrorActionTypeNotFound; } void DevicePluginEQ3::cubeConnectionStatusChanged(const bool &connected) diff --git a/plugins/deviceplugins/eq-3/deviceplugineq-3.h b/plugins/deviceplugins/eq-3/deviceplugineq-3.h index 33ab1153..282a26a1 100644 --- a/plugins/deviceplugins/eq-3/deviceplugineq-3.h +++ b/plugins/deviceplugins/eq-3/deviceplugineq-3.h @@ -39,7 +39,7 @@ public: DeviceManager::HardwareResources requiredHardware() const override; QList configurationDescription() const override; - QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; + DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; void startMonitoringAutoDevices() override; @@ -53,7 +53,7 @@ private: QHash m_cubes; public slots: - QPair executeAction(Device *device, const Action &action); + DeviceManager::DeviceError executeAction(Device *device, const Action &action); private slots: void cubeConnectionStatusChanged(const bool &connected); diff --git a/plugins/deviceplugins/intertechno/devicepluginintertechno.cpp b/plugins/deviceplugins/intertechno/devicepluginintertechno.cpp index fc0a3954..6d90f6ea 100644 --- a/plugins/deviceplugins/intertechno/devicepluginintertechno.cpp +++ b/plugins/deviceplugins/intertechno/devicepluginintertechno.cpp @@ -175,7 +175,7 @@ DeviceManager::HardwareResources DevicePluginIntertechno::requiredHardware() con return DeviceManager::HardwareResourceRadio433; } -QPair DevicePluginIntertechno::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginIntertechno::executeAction(Device *device, const Action &action) { QList rawData; @@ -218,7 +218,7 @@ QPair DevicePluginIntertechno::executeActio }else if(familyCode == "P"){ binCode.append("01010101"); }else{ - return report(); + return DeviceManager::DeviceErrorNoError; } QString buttonCode = device->paramValue("buttonCode").toString(); @@ -258,7 +258,7 @@ QPair DevicePluginIntertechno::executeActio }else if(familyCode == "16"){ binCode.append("01010101"); }else{ - return report(); + return DeviceManager::DeviceErrorNoError; } // ======================================= @@ -297,10 +297,10 @@ QPair DevicePluginIntertechno::executeActio // send data to hardware resource if(transmitData(delay, rawData)){ qDebug() << "transmitted" << pluginName() << device->name() << "power: " << action.param("power").value().toBool(); - return report(); + return DeviceManager::DeviceErrorNoError; }else{ qWarning() << "ERROR: could not transmitt" << pluginName() << device->name() << "power: " << action.param("power").value().toBool(); - return report(DeviceManager::DeviceErrorHardwareNotAvailable, QString("Radio 433 MHz transmitter not available.")); + return DeviceManager::DeviceErrorHardwareNotAvailable; } } diff --git a/plugins/deviceplugins/intertechno/devicepluginintertechno.h b/plugins/deviceplugins/intertechno/devicepluginintertechno.h index adaa9ec6..074775c4 100644 --- a/plugins/deviceplugins/intertechno/devicepluginintertechno.h +++ b/plugins/deviceplugins/intertechno/devicepluginintertechno.h @@ -35,7 +35,7 @@ public: void radioData(const QList &rawData) override; public slots: - QPair executeAction(Device *device, const Action &action) override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; }; diff --git a/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.cpp b/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.cpp index bc0e43cd..1af08e52 100644 --- a/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.cpp +++ b/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.cpp @@ -65,17 +65,17 @@ DevicePluginLgSmartTv::DevicePluginLgSmartTv() connect(m_discovery,SIGNAL(discoveryDone(QList)),this,SLOT(discoveryDone(QList))); } -QPair DevicePluginLgSmartTv::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError DevicePluginLgSmartTv::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { qDebug() << "should discover devices with params:" << params; if(deviceClassId != lgSmartTvDeviceClassId){ - return report(DeviceManager::DeviceErrorDeviceClassNotFound); + return DeviceManager::DeviceErrorDeviceClassNotFound; } m_discovery->discover(3000); - return report(DeviceManager::DeviceErrorAsync); + return DeviceManager::DeviceErrorAsync; } QPair DevicePluginLgSmartTv::setupDevice(Device *device) @@ -110,84 +110,50 @@ DeviceManager::HardwareResources DevicePluginLgSmartTv::requiredHardware() const return DeviceManager::HardwareResourceTimer; } -QPair DevicePluginLgSmartTv::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginLgSmartTv::executeAction(Device *device, const Action &action) { TvDevice * tvDevice = m_tvList.key(device); if(action.actionTypeId() == commandVolumeUpActionTypeId){ tvDevice->sendCommand(TvDevice::VolUp, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandVolumeDownActionTypeId){ + } else if(action.actionTypeId() == commandVolumeDownActionTypeId){ tvDevice->sendCommand(TvDevice::VolDown, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandMuteActionTypeId){ + } else if(action.actionTypeId() == commandMuteActionTypeId){ tvDevice->sendCommand(TvDevice::Mute, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandChannelUpActionTypeId){ + } else if(action.actionTypeId() == commandChannelUpActionTypeId){ tvDevice->sendCommand(TvDevice::ChannelUp, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandChannelDownActionTypeId){ + } else if(action.actionTypeId() == commandChannelDownActionTypeId){ tvDevice->sendCommand(TvDevice::ChannelDown, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandPowerOffActionTypeId){ + } else if(action.actionTypeId() == commandPowerOffActionTypeId){ tvDevice->sendCommand(TvDevice::Power, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandArrowUpActionTypeId){ + } else if(action.actionTypeId() == commandArrowUpActionTypeId){ tvDevice->sendCommand(TvDevice::Up, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandArrowDownActionTypeId){ + } else if(action.actionTypeId() == commandArrowDownActionTypeId){ tvDevice->sendCommand(TvDevice::Down, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandArrowLeftActionTypeId){ + } else if(action.actionTypeId() == commandArrowLeftActionTypeId){ tvDevice->sendCommand(TvDevice::Left, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandArrowRightActionTypeId){ + } else if(action.actionTypeId() == commandArrowRightActionTypeId){ tvDevice->sendCommand(TvDevice::Right, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandOkActionTypeId){ + } else if(action.actionTypeId() == commandOkActionTypeId){ tvDevice->sendCommand(TvDevice::Ok, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandBackActionTypeId){ + } else if(action.actionTypeId() == commandBackActionTypeId){ tvDevice->sendCommand(TvDevice::Back, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandHomeActionTypeId){ + } else if(action.actionTypeId() == commandHomeActionTypeId){ tvDevice->sendCommand(TvDevice::Home, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandInputSourceActionTypeId){ + } else if(action.actionTypeId() == commandInputSourceActionTypeId){ tvDevice->sendCommand(TvDevice::ExternalInput, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandExitActionTypeId){ + } else if(action.actionTypeId() == commandExitActionTypeId){ tvDevice->sendCommand(TvDevice::Exit, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandInfoActionTypeId){ + } else if(action.actionTypeId() == commandInfoActionTypeId){ tvDevice->sendCommand(TvDevice::Info, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandMyAppsActionTypeId){ + } else if(action.actionTypeId() == commandMyAppsActionTypeId){ tvDevice->sendCommand(TvDevice::MyApps, action.id()); - return report(DeviceManager::DeviceErrorAsync); - } - if(action.actionTypeId() == commandProgramListActionTypeId){ + } else if(action.actionTypeId() == commandProgramListActionTypeId){ tvDevice->sendCommand(TvDevice::ProgramList, action.id()); - return report(DeviceManager::DeviceErrorAsync); + } else { + return DeviceManager::DeviceErrorActionTypeNotFound; } - - return report(DeviceManager::DeviceErrorActionTypeNotFound); + return DeviceManager::DeviceErrorAsync; } void DevicePluginLgSmartTv::deviceRemoved(Device *device) diff --git a/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.h b/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.h index 1f4bffb9..e1131059 100644 --- a/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.h +++ b/plugins/deviceplugins/lgsmarttv/devicepluginlgsmarttv.h @@ -34,10 +34,10 @@ public: TvDiscovery *m_discovery; - QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; + DeviceManager::DeviceError 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; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; void deviceRemoved(Device *device) override; diff --git a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp index c86a8e7e..6a1b297d 100644 --- a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp +++ b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.cpp @@ -263,7 +263,7 @@ DeviceManager::HardwareResources DevicePluginMailNotification::requiredHardware( return DeviceManager::HardwareResourceNone; } -QPair DevicePluginMailNotification::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginMailNotification::executeAction(Device *device, const Action &action) { qDebug() << "execute action " << sendMailActionTypeId.toString(); if(action.actionTypeId() == sendMailActionTypeId){ @@ -294,5 +294,5 @@ QPair DevicePluginMailNotification::execute m_smtpClient->sendMail(device->paramValue("user").toString(), device->paramValue("recipient").toString(), action.param("subject").value().toString(), action.param("body").value().toString()); } - return report(); + return DeviceManager::DeviceErrorNoError; } diff --git a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.h b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.h index ea0a49c4..d67981ba 100644 --- a/plugins/deviceplugins/mailnotification/devicepluginmailnotification.h +++ b/plugins/deviceplugins/mailnotification/devicepluginmailnotification.h @@ -35,7 +35,7 @@ public: QPair setupDevice(Device *device) override; DeviceManager::HardwareResources requiredHardware() const override; - QPair executeAction(Device *device, const Action &action) override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; private: SmtpClient *m_smtpClient; diff --git a/plugins/deviceplugins/mock/devicepluginmock.cpp b/plugins/deviceplugins/mock/devicepluginmock.cpp index 4bdf3737..7bf3d176 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.cpp +++ b/plugins/deviceplugins/mock/devicepluginmock.cpp @@ -49,13 +49,13 @@ DeviceManager::HardwareResources DevicePluginMock::requiredHardware() const return DeviceManager::HardwareResourceTimer; } -QPair DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError DevicePluginMock::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { Q_UNUSED(deviceClassId) qDebug() << "starting mock discovery:" << params; m_discoveredDeviceCount = params.paramValue("resultCount").toInt(); QTimer::singleShot(1000, this, SLOT(emitDevicesDiscovered())); - return report(DeviceManager::DeviceErrorNoError); + return DeviceManager::DeviceErrorAsync; } QPair DevicePluginMock::setupDevice(Device *device) @@ -126,26 +126,26 @@ QList DevicePluginMock::configurationDescription() const return params; } -QPair DevicePluginMock::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const Action &action) { if (!myDevices().contains(device)) { qWarning() << "Should execute action for a device which doesn't seem to be mine."; - return report(DeviceManager::DeviceErrorDeviceNotFound, "Should execute an action for a device which doesn't seem to be mine."); + return DeviceManager::DeviceErrorDeviceNotFound; } if (action.actionTypeId() == mockActionIdAsync || action.actionTypeId() == mockActionIdAsyncFailing) { m_asyncActions.append(qMakePair(action, device)); QTimer::singleShot(1000, this, SLOT(emitActionExecuted())); - return report(DeviceManager::DeviceErrorAsync); + return DeviceManager::DeviceErrorAsync; } if (action.actionTypeId() == mockActionIdFailing) { - return report(DeviceManager::DeviceErrorSetupFailed); + return DeviceManager::DeviceErrorSetupFailed; } qDebug() << "Should execute action" << action.actionTypeId(); m_daemons.value(device)->actionExecuted(action.actionTypeId()); - return report(); + return DeviceManager::DeviceErrorNoError; } void DevicePluginMock::setState(const StateTypeId &stateTypeId, const QVariant &value) diff --git a/plugins/deviceplugins/mock/devicepluginmock.h b/plugins/deviceplugins/mock/devicepluginmock.h index 251d2845..1897707e 100644 --- a/plugins/deviceplugins/mock/devicepluginmock.h +++ b/plugins/deviceplugins/mock/devicepluginmock.h @@ -37,7 +37,7 @@ public: ~DevicePluginMock(); DeviceManager::HardwareResources requiredHardware() const override; - QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; + DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; QPair setupDevice(Device *device) override; void deviceRemoved(Device *device) override; @@ -47,7 +47,7 @@ public: QList configurationDescription() const override; public slots: - QPair executeAction(Device *device, const Action &action) override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; private slots: void setState(const StateTypeId &stateTypeId, const QVariant &value); diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp index 1ad8b639..63c466c0 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp @@ -303,7 +303,7 @@ DevicePluginOpenweathermap::DevicePluginOpenweathermap() connect(m_openweaher, &OpenWeatherMap::weatherDataReady, this, &DevicePluginOpenweathermap::weatherDataReady); } -QPair DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { if(deviceClassId != openweathermapDeviceClassId){ return report(DeviceManager::DeviceErrorDeviceClassNotFound); @@ -319,14 +319,10 @@ QPair DevicePluginOpenweathermap::discoverD // if we have an empty search string, perform an autodetection of the location with the WAN ip... if (location.isEmpty()){ m_openweaher->searchAutodetect(); - return report(DeviceManager::DeviceErrorAsync); - }else{ - return report(DeviceManager::DeviceErrorDeviceClassNotFound); + } else { + m_openweaher->search(location); } - - // otherwise search the given string - m_openweaher->search(location); - return report(DeviceManager::DeviceErrorAsync); + return DeviceManager::DeviceErrorAsync; } QPair DevicePluginOpenweathermap::setupDevice(Device *device) @@ -348,12 +344,12 @@ DeviceManager::HardwareResources DevicePluginOpenweathermap::requiredHardware() return DeviceManager::HardwareResourceTimer; } -QPair DevicePluginOpenweathermap::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginOpenweathermap::executeAction(Device *device, const Action &action) { if(action.actionTypeId() == updateWeatherActionTypeId){ m_openweaher->update(device->paramValue("id").toString(), device->id()); } - return report(); + return DeviceManager::DeviceErrorNoError; } void DevicePluginOpenweathermap::guhTimer() diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h index 7a3ff0a4..97235d64 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h @@ -35,10 +35,10 @@ public: OpenWeatherMap *m_openweaher; - QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; + DeviceManager::DeviceError 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; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; void guhTimer() override; diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp b/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp index b1eeffd3..5df7a45e 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.cpp @@ -69,12 +69,12 @@ QList DevicePluginPhilipsHue::configurationDescription() const return params; } -QPair DevicePluginPhilipsHue::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError DevicePluginPhilipsHue::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { Q_UNUSED(deviceClassId) Q_UNUSED(params) m_discovery->findBridges(4000); - return report(DeviceManager::DeviceErrorAsync); + return DeviceManager::DeviceErrorAsync; } QPair DevicePluginPhilipsHue::setupDevice(Device *device) @@ -171,17 +171,18 @@ void DevicePluginPhilipsHue::guhTimer() } } -QPair DevicePluginPhilipsHue::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginPhilipsHue::executeAction(Device *device, const Action &action) { qDebug() << "Should execute action in hue plugin"; Light *light = m_lights.key(device); if (!light) { - return report(DeviceManager::DeviceErrorDeviceNotFound, device->id().toString()); + return DeviceManager::DeviceErrorDeviceNotFound; } if (!light->reachable()) { - return report(DeviceManager::DeviceErrorSetupFailed, "This light is currently not reachable."); + qWarning() << "Hue Bulb not reachable"; + return DeviceManager::DeviceErrorSetupFailed; } if (action.actionTypeId() == hueSetColorActionTypeId) { @@ -191,7 +192,7 @@ QPair DevicePluginPhilipsHue::executeAction } else if (action.actionTypeId() == hueSetBrightnessActionTypeId) { light->setBri(action.param("brightness").value().toInt()); } - return report(); + return DeviceManager::DeviceErrorNoError; } void DevicePluginPhilipsHue::discoveryDone(const QList &bridges) diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.h b/plugins/deviceplugins/philipshue/devicepluginphilipshue.h index 56e23035..65c8637b 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.h +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.h @@ -41,7 +41,7 @@ public: void startMonitoringAutoDevices() override; QList configurationDescription() const override; - QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; + DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; QPair setupDevice(Device *device) override; void deviceRemoved(Device *device) override; @@ -51,7 +51,7 @@ public: void guhTimer() override; public slots: - QPair executeAction(Device *device, const Action &action); + DeviceManager::DeviceError executeAction(Device *device, const Action &action); private slots: void discoveryDone(const QList &bridges); diff --git a/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.cpp b/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.cpp index 7c9a5f58..140caa11 100644 --- a/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.cpp +++ b/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.cpp @@ -161,13 +161,13 @@ DeviceManager::HardwareResources DevicePluginWakeOnLan::requiredHardware() const return DeviceManager::HardwareResourceNone; } -QPair DevicePluginWakeOnLan::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginWakeOnLan::executeAction(Device *device, const Action &action) { qDebug() << "execute action " << action.actionTypeId().toString(); if(action.actionTypeId() == wolActionTypeId){ wakeup(device->paramValue("mac").toString()); } - return report(); + return DeviceManager::DeviceErrorNoError; } void DevicePluginWakeOnLan::wakeup(QString mac) diff --git a/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.h b/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.h index 3565203d..c1ba90eb 100644 --- a/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.h +++ b/plugins/deviceplugins/wakeonlan/devicepluginwakeonlan.h @@ -35,7 +35,7 @@ public: DeviceManager::HardwareResources requiredHardware() const override; - QPair executeAction(Device *device, const Action &action) override; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; private slots: diff --git a/plugins/deviceplugins/wemo/devicepluginwemo.cpp b/plugins/deviceplugins/wemo/devicepluginwemo.cpp index 349320b9..be2ce8c4 100644 --- a/plugins/deviceplugins/wemo/devicepluginwemo.cpp +++ b/plugins/deviceplugins/wemo/devicepluginwemo.cpp @@ -146,15 +146,15 @@ DevicePluginWemo::DevicePluginWemo() connect(m_discovery,SIGNAL(discoveryDone(QList)),this,SLOT(discoveryDone(QList))); } -QPair DevicePluginWemo::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError DevicePluginWemo::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { if(deviceClassId != wemoSwitchDeviceClassId){ - return report(DeviceManager::DeviceErrorDeviceClassNotFound); + return DeviceManager::DeviceErrorDeviceClassNotFound; } m_discovery->discover(2000); - return report(DeviceManager::DeviceErrorAsync); + return DeviceManager::DeviceErrorAsync; } QPair DevicePluginWemo::setupDevice(Device *device) @@ -196,20 +196,20 @@ DeviceManager::HardwareResources DevicePluginWemo::requiredHardware() const return DeviceManager::HardwareResourceTimer; } -QPair DevicePluginWemo::executeAction(Device *device, const Action &action) +DeviceManager::DeviceError DevicePluginWemo::executeAction(Device *device, const Action &action) { if(device->deviceClassId() == wemoSwitchDeviceClassId){ if(action.actionTypeId() == powerActionTypeId){ WemoSwitch *wemoSwitch = m_wemoSwitches.key(device); wemoSwitch->setPower(action.param("power").value().toBool(),action.id()); - return report(DeviceManager::DeviceErrorAsync); + return DeviceManager::DeviceErrorAsync; }else{ - return report(DeviceManager::DeviceErrorActionTypeNotFound); + return DeviceManager::DeviceErrorActionTypeNotFound; } } - return report(DeviceManager::DeviceErrorDeviceClassNotFound); + return DeviceManager::DeviceErrorDeviceClassNotFound; } void DevicePluginWemo::deviceRemoved(Device *device) diff --git a/plugins/deviceplugins/wemo/devicepluginwemo.h b/plugins/deviceplugins/wemo/devicepluginwemo.h index 5dc3f431..6605f40c 100644 --- a/plugins/deviceplugins/wemo/devicepluginwemo.h +++ b/plugins/deviceplugins/wemo/devicepluginwemo.h @@ -32,10 +32,10 @@ class DevicePluginWemo : public DevicePlugin public: explicit DevicePluginWemo(); - QPair discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) override; + DeviceManager::DeviceError 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; + DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; void deviceRemoved(Device *device) override; diff --git a/server/guhcore.cpp b/server/guhcore.cpp index a6e8c761..129d831d 100644 --- a/server/guhcore.cpp +++ b/server/guhcore.cpp @@ -127,7 +127,7 @@ QPair GuhCore::confirmPairing(const QUuid & return m_deviceManager->confirmPairing(pairingTransactionId, secret); } -QPair GuhCore::executeAction(const Action &action) +DeviceManager::DeviceError GuhCore::executeAction(const Action &action) { return m_deviceManager->executeAction(action); } @@ -137,17 +137,17 @@ DeviceClass GuhCore::findDeviceClass(const DeviceClassId &deviceClassId) const return m_deviceManager->findDeviceClass(deviceClassId); } -QPair GuhCore::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) +DeviceManager::DeviceError GuhCore::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) { return m_deviceManager->discoverDevices(deviceClassId, params); } -QPair GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId &newId) +DeviceManager::DeviceError GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId &newId) { return m_deviceManager->addConfiguredDevice(deviceClassId, params, newId); } -QPair GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &newId) +DeviceManager::DeviceError GuhCore::addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &newId) { return m_deviceManager->addConfiguredDevice(deviceClassId, deviceDescriptorId, newId); } @@ -252,18 +252,18 @@ void GuhCore::gotEvent(const Event &event) // Now execute all the associated rules foreach (const Action &action, m_ruleEngine->evaluateEvent(event)) { qDebug() << "executing action" << action.actionTypeId(); - QPair status = m_deviceManager->executeAction(action); - switch(status.first) { + DeviceManager::DeviceError status = m_deviceManager->executeAction(action); + switch(status) { case DeviceManager::DeviceErrorNoError: break; case DeviceManager::DeviceErrorSetupFailed: - qDebug() << "Error executing action. Device setup failed:" << status.second; + qDebug() << "Error executing action. Device setup failed."; break; case DeviceManager::DeviceErrorActionParameterError: - qDebug() << "Error executing action. Invalid action parameter:" << status.second; + qDebug() << "Error executing action. Invalid action parameter."; break; default: - qDebug() << "Error executing action:" << status.first << status.second; + qDebug() << "Error executing action:" << status; } } } diff --git a/server/guhcore.h b/server/guhcore.h index cb96eb42..f7e74fc7 100644 --- a/server/guhcore.h +++ b/server/guhcore.h @@ -49,9 +49,9 @@ public: QList supportedVendors() const; QList supportedDevices(const VendorId &vendorId = VendorId()) const; DeviceClass findDeviceClass(const DeviceClassId &deviceClassId) const; - 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); + DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms); + DeviceManager::DeviceError addConfiguredDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms, const DeviceId &newId); + DeviceManager::DeviceError addConfiguredDevice(const DeviceClassId &deviceClassId, const DeviceDescriptorId &deviceDescriptorId, const DeviceId &newId); QList configuredDevices() const; Device *findConfiguredDevice(const DeviceId &deviceId) const; QList findConfiguredDevices(const DeviceClassId &deviceClassId) const; @@ -61,7 +61,7 @@ public: QPair pairDevice(const DeviceClassId &deviceClassId, const ParamList ¶ms); QPair confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString()); - QPair executeAction(const Action &action); + DeviceManager::DeviceError executeAction(const Action &action); QList rules() const; QList ruleIds() const; diff --git a/server/jsonrpc/actionhandler.cpp b/server/jsonrpc/actionhandler.cpp index bb8c76d2..eba91d44 100644 --- a/server/jsonrpc/actionhandler.cpp +++ b/server/jsonrpc/actionhandler.cpp @@ -41,8 +41,7 @@ ActionHandler::ActionHandler(QObject *parent) : setDescription("GetActionType", "Get the ActionType for the given ActionTypeId"); params.insert("actionTypeId", "uuid"); setParams("GetActionType", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "int"); returns.insert("o:actionType", JsonTypes::actionTypeDescription()); setReturns("GetActionType", returns); @@ -67,14 +66,15 @@ JsonReply* ActionHandler::ExecuteAction(const QVariantMap ¶ms) qDebug() << "actions params in json" << action.params() << params; - QPair status = GuhCore::instance()->executeAction(action); - if (status.first == DeviceManager::DeviceErrorAsync) { + DeviceManager::DeviceError status = GuhCore::instance()->executeAction(action); + if (status == DeviceManager::DeviceErrorAsync) { JsonReply *reply = createAsyncReply("ExecuteAction"); m_asyncActionExecutions.insert(action.id(), reply); return reply; } - QVariantMap returns = statusToReply(status.first, status.second); + QVariantMap returns; + returns.insert("deviceError", status); return createReply(returns); } @@ -85,16 +85,14 @@ JsonReply *ActionHandler::GetActionType(const QVariantMap ¶ms) const foreach (const ActionType &actionType, deviceClass.actionTypes()) { if (actionType.id() == actionTypeId) { QVariantMap data; - data.insert("success", true); - data.insert("errorMessage", QString()); + data.insert("deviceError", DeviceManager::DeviceErrorNoError); data.insert("actionType", JsonTypes::packActionType(actionType)); return createReply(data); } } } QVariantMap data; - data.insert("success", false); - data.insert("errorMessage", QString("No ActionType with id %1.").arg(actionTypeId.toString())); + data.insert("deviceError", DeviceManager::DeviceErrorActionTypeNotFound); return createReply(data); } @@ -112,19 +110,6 @@ void ActionHandler::actionExecuted(const ActionId &id, DeviceManager::DeviceErro QVariantMap ActionHandler::statusToReply(DeviceManager::DeviceError status, const QString &errorMessage) { QVariantMap returns; - returns.insert("success", status == DeviceManager::DeviceErrorNoError); - returns.insert("errorMessage", errorMessage); - - switch (status) { - case DeviceManager::DeviceErrorNoError: - break; - case DeviceManager::DeviceErrorDeviceNotFound: - returns.insert("errorMessage", QString("Device not found: %1").arg(errorMessage)); - break; - case DeviceManager::DeviceErrorSetupFailed: - returns.insert("errorMessage", QString("Device setup failed: %1").arg(errorMessage)); - break; - } - + returns.insert("deviceError", status); return returns; } diff --git a/server/jsonrpc/devicehandler.cpp b/server/jsonrpc/devicehandler.cpp index 4258de61..bf6e162a 100644 --- a/server/jsonrpc/devicehandler.cpp +++ b/server/jsonrpc/devicehandler.cpp @@ -62,8 +62,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : setParams("GetPluginConfiguration", params); QVariantList pluginParams; pluginParams.append(JsonTypes::paramRef()); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); returns.insert("o:configuration", pluginParams); setReturns("GetPluginConfiguration", returns); @@ -72,8 +71,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : params.insert("pluginId", "uuid"); params.insert("configuration", pluginParams); setParams("SetPluginConfiguration", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); setReturns("SetPluginConfiguration", returns); params.clear(); returns.clear(); @@ -89,8 +87,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : deviceParams.append(JsonTypes::paramRef()); params.insert("o:deviceParams", deviceParams); setParams("AddConfiguredDevice", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); returns.insert("o:deviceId", "uuid"); setReturns("AddConfiguredDevice", returns); @@ -105,8 +102,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : " or PairDevice." ); setParams("PairDevice", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); returns.insert("o:pairingTransactionId", "uuid"); returns.insert("o:displayMessage", "string"); returns.insert("o:setupMethod", JsonTypes::setupMethodTypesRef()); @@ -117,8 +113,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : params.insert("pairingTransactionId", "uuid"); params.insert("o:secret", "string"); setParams("ConfirmPairing", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); returns.insert("o:deviceId", "uuid"); setReturns("ConfirmPairing", returns); @@ -137,8 +132,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : discoveryParams.append(JsonTypes::paramRef()); params.insert("o:discoveryParams", discoveryParams); setParams("GetDiscoveredDevices", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); QVariantList deviceDescriptors; deviceDescriptors.append(JsonTypes::deviceDescriptorRef()); returns.insert("o:deviceDescriptors", deviceDescriptors); @@ -154,8 +148,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : removePolicyList.append(policy); params.insert("o:removePolicyList", removePolicyList); setParams("RemoveConfiguredDevice", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); setReturns("RemoveConfiguredDevice", returns); params.clear(); returns.clear(); @@ -190,8 +183,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : params.insert("deviceId", "uuid"); params.insert("stateTypeId", "uuid"); setParams("GetStateValue", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("deviceError", "DeviceError"); returns.insert("o:value", "variant"); setReturns("GetStateValue", returns); @@ -251,31 +243,13 @@ JsonReply *DeviceHandler::GetDiscoveredDevices(const QVariantMap ¶ms) const ParamList discoveryParams = JsonTypes::unpackParams(params.value("discoveryParams").toList()); - QPair status = GuhCore::instance()->discoverDevices(deviceClassId, discoveryParams); - switch (status.first) { - case DeviceManager::DeviceErrorAsync: - case DeviceManager::DeviceErrorNoError: { + DeviceManager::DeviceError status = GuhCore::instance()->discoverDevices(deviceClassId, discoveryParams); + if (status == DeviceManager::DeviceErrorAsync ) { JsonReply *reply = createAsyncReply("GetDiscoveredDevices"); m_discoverRequests.insert(deviceClassId, reply); return reply; } - case DeviceManager::DeviceErrorDeviceClassNotFound: - 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."); - break; - 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 %2").arg(status.first).arg(status.second)); - } - - returns.insert("success", false); + returns.insert("deviceError", status); return createReply(returns); } @@ -342,7 +316,7 @@ JsonReply* DeviceHandler::AddConfiguredDevice(const QVariantMap ¶ms) ParamList deviceParams = JsonTypes::unpackParams(params.value("deviceParams").toList()); DeviceDescriptorId deviceDescriptorId(params.value("deviceDescriptorId").toString()); DeviceId newDeviceId = DeviceId::createDeviceId(); - QPair status; + DeviceManager::DeviceError status; if (deviceDescriptorId.isNull()) { qDebug() << "adding a manual device."; status = GuhCore::instance()->addConfiguredDevice(deviceClass, deviceParams, newDeviceId); @@ -351,40 +325,16 @@ JsonReply* DeviceHandler::AddConfiguredDevice(const QVariantMap ¶ms) status = GuhCore::instance()->addConfiguredDevice(deviceClass, deviceDescriptorId, newDeviceId); } QVariantMap returns; - switch(status.first) { + switch (status) { case DeviceManager::DeviceErrorAsync: { JsonReply *asyncReply = createAsyncReply("AddConfiguredDevice"); m_asynDeviceAdditions.insert(newDeviceId, asyncReply); return asyncReply; } case DeviceManager::DeviceErrorNoError: - returns.insert("success", true); - returns.insert("errorMessage", ""); returns.insert("deviceId", newDeviceId); - break; - case DeviceManager::DeviceErrorDeviceClassNotFound: - returns.insert("errorMessage", QString("Error creating device. Device class not found: %1").arg(status.second)); - returns.insert("success", false); - break; - case DeviceManager::DeviceErrorMissingParameter: - returns.insert("errorMessage", QString("Error creating device. Missing parameter: %1").arg(status.second)); - returns.insert("success", false); - break; - case DeviceManager::DeviceErrorSetupFailed: - returns.insert("errorMessage", QString("Error creating device. Device setup failed: %1").arg(status.second)); - returns.insert("success", false); - break; - case DeviceManager::DeviceErrorCreationMethodNotSupported: - returns.insert("errorMessage", QString("Error creating device. This device can't be created this way: %1").arg(status.second)); - returns.insert("success", false); - break; - case DeviceManager::DeviceErrorInvalidParameter: - returns.insert("errorMessage", QString("Error creating device. Invalid device parameter: %1").arg(status.second)); - returns.insert("success", false); - break; default: - returns.insert("errorMessage", "Unknown error. Please report a bug describing what you did."); - returns.insert("success", false); + returns.insert("deviceError", status); } return createReply(returns); } diff --git a/server/jsonrpc/jsonrpcserver.cpp b/server/jsonrpc/jsonrpcserver.cpp index 5915f21c..4aeb37f9 100644 --- a/server/jsonrpc/jsonrpcserver.cpp +++ b/server/jsonrpc/jsonrpcserver.cpp @@ -77,7 +77,6 @@ JsonRPCServer::JsonRPCServer(QObject *parent): setParams("SetNotificationStatus", params); returns.insert("success", "bool"); returns.insert("enabled", "bool"); - returns.insert("errorMessage", "string"); setReturns("SetNotificationStatus", returns); // Now set up the logic @@ -132,7 +131,6 @@ JsonReply* JsonRPCServer::SetNotificationStatus(const QVariantMap ¶ms) m_clients[clientId] = params.value("enabled").toBool(); QVariantMap returns; returns.insert("success", "true"); - returns.insert("errorMessage", "No error"); returns.insert("enabled", m_clients[clientId]); return createReply(returns); } @@ -206,12 +204,23 @@ void JsonRPCServer::processData(const QUuid &clientId, const QByteArray &jsonDat connect(reply, &JsonReply::finished, this, &JsonRPCServer::asyncReplyFinished); reply->startWait(); } else { - Q_ASSERT((targetNamespace == "JSONRPC" && method == "Introspect") || handler->validateReturns(method, reply->data()).first); + Q_ASSERT_X((targetNamespace == "JSONRPC" && method == "Introspect") || handler->validateReturns(method, reply->data()).first + ,"validating return value", formatAssertion(targetNamespace, method, handler, reply->data()).toLatin1().data()); sendResponse(clientId, commandId, reply->data()); reply->deleteLater(); } } +QString JsonRPCServer::formatAssertion(const QString &targetNamespace, const QString &method, JsonHandler *handler, const QVariantMap &data) const +{ + QJsonDocument doc = QJsonDocument::fromVariant(handler->introspect(QMetaMethod::Method).value(targetNamespace + "." + method)); + QJsonDocument doc2 = QJsonDocument::fromVariant(data); + return QString("\nMethod: %1\nTemplate: %2\nValue: %3") + .arg(targetNamespace + "." + method) + .arg(QString(doc.toJson())) + .arg(QString(doc2.toJson())); +} + void JsonRPCServer::sendNotification(const QVariantMap ¶ms) { JsonHandler *handler = qobject_cast(sender()); diff --git a/server/jsonrpc/jsonrpcserver.h b/server/jsonrpc/jsonrpcserver.h index aa000ce0..1254389e 100644 --- a/server/jsonrpc/jsonrpcserver.h +++ b/server/jsonrpc/jsonrpcserver.h @@ -69,6 +69,8 @@ private: void sendResponse(const QUuid &clientId, int commandId, const QVariantMap ¶ms = QVariantMap()); void sendErrorResponse(const QUuid &clientId, int commandId, const QString &error); + QString formatAssertion(const QString &targetNamespace, const QString &method, JsonHandler *handler, const QVariantMap &data) const; + private: #ifdef TESTING_ENABLED MockTcpServer *m_tcpServer; diff --git a/server/jsonrpc/jsontypes.cpp b/server/jsonrpc/jsontypes.cpp index cece93f9..4e12c6e4 100644 --- a/server/jsonrpc/jsontypes.cpp +++ b/server/jsonrpc/jsontypes.cpp @@ -19,10 +19,13 @@ #include "jsontypes.h" #include "plugin/device.h" +#include "devicemanager.h" +#include "ruleengine.h" #include #include #include +#include bool JsonTypes::s_initialized = false; QString JsonTypes::s_lastError; @@ -33,6 +36,8 @@ QVariantList JsonTypes::s_valueOperatorTypes; QVariantList JsonTypes::s_createMethodTypes; QVariantList JsonTypes::s_setupMethodTypes; QVariantList JsonTypes::s_removePolicyTypes; +QVariantList JsonTypes::s_deviceErrorTypes; +QVariantList JsonTypes::s_ruleErrorTypes; QVariantMap JsonTypes::s_paramType; QVariantMap JsonTypes::s_param; @@ -56,12 +61,14 @@ QVariantMap JsonTypes::s_rule; void JsonTypes::init() { // BasicTypes - s_basicTypes << "uuid" << "string" << "integer" << "double" << "bool"; - s_stateOperatorTypes << "StateOperatorAnd" << "StateOperatorOr"; - s_valueOperatorTypes << "OperatorTypeEquals" << "OperatorTypeNotEquals" << "OperatorTypeLess" << "OperatorTypeGreater" << "OperatorTypeLessThan" << "OperatorTypeGreaterThan"; - s_createMethodTypes << "CreateMethodUser" << "CreateMethodAuto" << "CreateMethodDiscovery"; - s_setupMethodTypes << "SetupMethodJustAdd" << "SetupMethodDisplayPin" << "SetupMethodEnterPin" << "SetupMethodPushButton"; - s_removePolicyTypes << "RemovePolicyCascade" << "RemovePolicyUpdate"; + s_basicTypes = enumToStrings(JsonTypes::staticMetaObject, "BasicTypes"); + s_stateOperatorTypes = enumToStrings(Types::staticMetaObject, "StateOperator"); + s_valueOperatorTypes = enumToStrings(Types::staticMetaObject, "ValueOperator"); + s_createMethodTypes = enumToStrings(DeviceClass::staticMetaObject, "CreateMethod"); + s_setupMethodTypes = enumToStrings(DeviceClass::staticMetaObject, "SetupMethod"); + s_removePolicyTypes = enumToStrings(RuleEngine::staticMetaObject, "RemovePolicy"); + s_deviceErrorTypes = enumToStrings(DeviceManager::staticMetaObject, "DeviceError"); + s_ruleErrorTypes = enumToStrings(RuleEngine::staticMetaObject, "RuleError"); // ParamType s_paramType.insert("name", "string"); @@ -174,6 +181,19 @@ QPair JsonTypes::report(bool status, const QString &message) return qMakePair(status, message); } +QVariantList JsonTypes::enumToStrings(const QMetaObject &metaObject, const QString &enumName) +{ + int enumIndex = metaObject.indexOfEnumerator(enumName.toLatin1().data()); + QMetaEnum metaEnum = metaObject.enumerator(enumIndex); + + qDebug() << "*** have enum" << metaEnum.name(); + QVariantList enumStrings; + for (int i = 0; i < metaEnum.keyCount(); i++) { + enumStrings << metaEnum.valueToKey(metaEnum.value(i)); + } + return enumStrings; +} + QVariantMap JsonTypes::allTypes() { QVariantMap allTypes; @@ -184,6 +204,8 @@ QVariantMap JsonTypes::allTypes() allTypes.insert("ValueOperatorType", valueOperatorTypes()); allTypes.insert("StateOperatorType", stateOperatorTypes()); allTypes.insert("RemovePolicyType", removePolicyTypes()); + allTypes.insert("DeviceError", deviceErrorTypes()); + allTypes.insert("RuleError", ruleErrorTypes()); allTypes.insert("StateType", stateTypeDescription()); allTypes.insert("StateDescriptor", stateDescriptorDescription()); allTypes.insert("StateEvaluator", stateEvaluatorDescription()); @@ -474,19 +496,11 @@ ParamDescriptor JsonTypes::unpackParamDescriptor(const QVariantMap ¶mMap) { ParamDescriptor param(paramMap.value("name").toString(), paramMap.value("value")); QString operatorString = paramMap.value("operator").toString(); - if (operatorString == "ValueOperatorEquals") { - param.setOperatorType(ValueOperatorEquals); - } else if (operatorString == "ValueOperatorNotEquals") { - param.setOperatorType(ValueOperatorNotEquals); - } else if (operatorString == "ValueOperatorLess") { - param.setOperatorType(ValueOperatorLess); - } else if (operatorString == "ValueOperatorGreater") { - param.setOperatorType(ValueOperatorGreater); - } else if (operatorString == "ValueOperatorLessOrEqual") { - param.setOperatorType(ValueOperatorLessOrEqual); - } else if (operatorString == "ValueOperatorGreaterOrEqual") { - param.setOperatorType(ValueOperatorGreaterOrEqual); - } + + QMetaObject metaObject = Types::staticMetaObject; + int enumIndex = metaObject.indexOfEnumerator("ValueOperator"); + QMetaEnum metaEnum = metaObject.enumerator(enumIndex); + param.setOperatorType((Types::ValueOperator)metaEnum.keyToValue(operatorString.toLatin1().data())); return param; } @@ -511,6 +525,7 @@ EventDescriptor JsonTypes::unpackEventDescriptor(const QVariantMap &eventDescrip QPair JsonTypes::validateMap(const QVariantMap &templateMap, const QVariantMap &map) { s_lastError.clear(); + qDebug() << "validating Map" << templateMap << map; // Make sure all values defined in the template are around foreach (const QString &key, templateMap.keys()) { @@ -548,7 +563,7 @@ QPair JsonTypes::validateMap(const QVariantMap &templateMap, cons QPair JsonTypes::validateProperty(const QVariant &templateValue, const QVariant &value) { -// qDebug() << "validating property. template:" << templateValue << "got:" << value; + qDebug() << "validating property. template:" << templateValue << "got:" << value; QString strippedTemplateValue = templateValue.toString(); if (strippedTemplateValue == "variant") { @@ -566,6 +581,10 @@ QPair JsonTypes::validateProperty(const QVariant &templateValue, QString errorString = QString("Param %1 is not a bool.").arg(value.toString()); return report(value.canConvert(QVariant::Bool), errorString); } + if (strippedTemplateValue == "int") { + QString errorString = QString("Param %1 is not a int.").arg(value.toString()); + return report(value.canConvert(QVariant::Int), errorString); + } qWarning() << QString("Unhandled property type: %1 (expected: %2)").arg(value.toString()).arg(strippedTemplateValue); QString errorString = QString("Unhandled property type: %1 (expected: %2)").arg(value.toString()).arg(strippedTemplateValue); return report(false, errorString); diff --git a/server/jsonrpc/jsontypes.h b/server/jsonrpc/jsontypes.h index ecbe1ed2..82b42749 100644 --- a/server/jsonrpc/jsontypes.h +++ b/server/jsonrpc/jsontypes.h @@ -22,6 +22,8 @@ #include "plugin/deviceclass.h" #include "plugin/devicedescriptor.h" #include "rule.h" +#include "devicemanager.h" +#include "ruleengine.h" #include "types/event.h" #include "types/action.h" @@ -33,6 +35,7 @@ #include #include +#include class DevicePlugin; class Device; @@ -48,28 +51,47 @@ class Device; static QVariantMap s_##typeName; \ public: -#define DECLARE_TYPE(typeName, jsonName) \ +#define DECLARE_TYPE(typeName, enumString, className, enumName) \ public: \ - static QString typeName##Ref() { return QStringLiteral("$ref:") + QStringLiteral(jsonName); } \ + static QString typeName##Ref() { return QStringLiteral("$ref:") + QStringLiteral(enumString); } \ static QVariantList typeName() { \ if (!s_initialized) { init(); } \ return s_##typeName; \ } \ + static QString typeName##ToString(className::enumName value) { \ + QMetaObject metaObject = className::staticMetaObject; \ + int enumIndex = metaObject.indexOfEnumerator(enumString); \ + QMetaEnum metaEnum = metaObject.enumerator(enumIndex); \ + return metaEnum.valueToKey(metaEnum.value(value)); \ + } \ private: \ static QVariantList s_##typeName; \ public: class JsonTypes { + Q_GADGET + Q_ENUMS(BasicTypes) public: + enum BasicTypes { + Uuid, + String, + Int, + Double, + Bool + }; + + static QVariantMap allTypes(); - DECLARE_TYPE(basicTypes, "BasicType") - DECLARE_TYPE(stateOperatorTypes, "StateOperatorType") - DECLARE_TYPE(valueOperatorTypes, "ValueOperatorType") - DECLARE_TYPE(createMethodTypes, "CreateMethodType") - DECLARE_TYPE(setupMethodTypes, "SetupMethodType") - DECLARE_TYPE(removePolicyTypes, "RemovePolicyType") + DECLARE_TYPE(basicTypes, "BasicType", JsonTypes, BasicTypes) + DECLARE_TYPE(stateOperatorTypes, "StateOperator", Types, StateOperator) + DECLARE_TYPE(valueOperatorTypes, "ValueOperator", Types, ValueOperator) + DECLARE_TYPE(createMethodTypes, "CreateMethod", DeviceClass, CreateMethod) + DECLARE_TYPE(setupMethodTypes, "SetupMethod", DeviceClass, SetupMethod) + DECLARE_TYPE(deviceErrorTypes, "DeviceError", DeviceManager, DeviceError) + DECLARE_TYPE(removePolicyTypes, "RemovePolicy", RuleEngine, RemovePolicy) + DECLARE_TYPE(ruleErrorTypes, "RuleError", RuleEngine, RuleError) DECLARE_OBJECT(paramType, "ParamType") DECLARE_OBJECT(param, "Param") DECLARE_OBJECT(paramDescriptor, "ParamDescriptor") @@ -124,11 +146,13 @@ public: static QPair validateSetupMethodType(const QVariant &variant); static QPair validateValueOperatorType(const QVariant &variant); + private: static bool s_initialized; static void init(); static QPair report(bool status, const QString &message); + static QVariantList enumToStrings(const QMetaObject &metaObject, const QString &enumName); static QString s_lastError; }; diff --git a/server/jsonrpc/ruleshandler.cpp b/server/jsonrpc/ruleshandler.cpp index 4bdb23f4..a80dbb79 100644 --- a/server/jsonrpc/ruleshandler.cpp +++ b/server/jsonrpc/ruleshandler.cpp @@ -43,7 +43,8 @@ RulesHandler::RulesHandler(QObject *parent) : setReturns("GetRuleDetails", returns); params.clear(); returns.clear(); - setDescription("AddRule", "Add a rule."); + setDescription("AddRule", "Add a rule. You can describe rules by one or many EventDesciptors and a StateEvaluator. Note that only" + "one of either eventDescriptor or eventDescriptorList may be passed at a time."); params.insert("o:eventDescriptor", JsonTypes::eventDescriptorRef()); params.insert("o:eventDescriptorList", QVariantList() << JsonTypes::eventDescriptorRef()); params.insert("o:stateEvaluator", JsonTypes::stateEvaluatorRef()); @@ -51,8 +52,7 @@ RulesHandler::RulesHandler(QObject *parent) : actions.append(JsonTypes::actionRef()); params.insert("actions", actions); setParams("AddRule", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("ruleError", "int"); returns.insert("o:ruleId", "uuid"); setReturns("AddRule", returns); @@ -60,8 +60,7 @@ RulesHandler::RulesHandler(QObject *parent) : setDescription("RemoveRule", "Remove a rule"); params.insert("ruleId", "uuid"); setParams("RemoveRule", params); - returns.insert("success", "bool"); - returns.insert("errorMessage", "string"); + returns.insert("ruleError", "int"); setReturns("RemoveRule", returns); params.clear(); returns.clear(); @@ -106,8 +105,8 @@ JsonReply* RulesHandler::AddRule(const QVariantMap ¶ms) { if (params.contains("eventDescriptor") && params.contains("eventDescriptorList")) { QVariantMap returns; - returns.insert("success", false); - returns.insert("errorMessage", "Only one of \"eventDescriptor\" and \"eventDescriptorList\" may be used."); + qWarning() << "Only one of eventDesciptor or eventDescriptorList may be used."; + returns.insert("ruleError", RuleEngine::RuleErrorInvalidParameter); return createReply(returns); } @@ -133,30 +132,16 @@ JsonReply* RulesHandler::AddRule(const QVariantMap ¶ms) QVariantMap returns; if (actions.count() == 0) { - returns.insert("success", false); - returns.insert("errorMessage", "Missing parameter: \"actions\"."); + returns.insert("ruleErorr", RuleEngine::RuleErrorMissingParameter); return createReply(returns); } RuleId newRuleId = RuleId::createRuleId(); - switch(GuhCore::instance()->addRule(newRuleId, eventDescriptorList, actions)) { - case RuleEngine::RuleErrorNoError: - returns.insert("success", true); - returns.insert("errorMessage", ""); + RuleEngine::RuleError status = GuhCore::instance()->addRule(newRuleId, eventDescriptorList, actions); + if (status == RuleEngine::RuleErrorNoError) { returns.insert("ruleId", newRuleId.toString()); - break; - case RuleEngine::RuleErrorDeviceNotFound: - returns.insert("success", false); - returns.insert("errorMessage", "No such device."); - break; - case RuleEngine::RuleErrorEventTypeNotFound: - returns.insert("success", false); - returns.insert("errorMessage", "Device does not have such a event type."); - break; - default: - returns.insert("success", false); - returns.insert("errorMessage", "Unknown error"); } + returns.insert("ruleError", status); return createReply(returns); } @@ -164,19 +149,8 @@ JsonReply* RulesHandler::RemoveRule(const QVariantMap ¶ms) { QVariantMap returns; RuleId ruleId(params.value("ruleId").toString()); - switch (GuhCore::instance()->removeRule(ruleId)) { - case RuleEngine::RuleErrorNoError: - returns.insert("success", true); - returns.insert("errorMessage", ""); - break; - case RuleEngine::RuleErrorRuleNotFound: - returns.insert("success", false); - returns.insert("errorMessage", "No such rule."); - break; - default: - returns.insert("success", false); - returns.insert("errorMessage", "Unknown error"); - } + RuleEngine::RuleError status = GuhCore::instance()->removeRule(ruleId); + returns.insert("ruleError", status); return createReply(returns); } diff --git a/server/ruleengine.cpp b/server/ruleengine.cpp index 4790945a..c90f6605 100644 --- a/server/ruleengine.cpp +++ b/server/ruleengine.cpp @@ -93,7 +93,7 @@ RuleEngine::RuleEngine(QObject *parent) : if (groupName.startsWith("ParamDescriptor-")) { settings.beginGroup(groupName); ParamDescriptor paramDescriptor(groupName.remove(QRegExp("^ParamDescriptor-")), settings.value("value")); - paramDescriptor.setOperatorType((ValueOperator)settings.value("operator").toInt()); + paramDescriptor.setOperatorType((Types::ValueOperator)settings.value("operator").toInt()); params.append(paramDescriptor); settings.endGroup(); } diff --git a/server/ruleengine.h b/server/ruleengine.h index 608d7916..5c35898e 100644 --- a/server/ruleengine.h +++ b/server/ruleengine.h @@ -30,6 +30,8 @@ class RuleEngine : public QObject { Q_OBJECT + Q_ENUMS(RuleError) + Q_ENUMS(RemovePolicy) public: enum RuleError { RuleErrorNoError, @@ -37,7 +39,9 @@ public: RuleErrorRuleNotFound, RuleErrorDeviceNotFound, RuleErrorEventTypeNotFound, - RuleErrorActionTypeNotFound + RuleErrorActionTypeNotFound, + RuleErrorInvalidParameter, + RuleErrorMissingParameter }; enum RemovePolicy { @@ -75,5 +79,6 @@ private: QList m_ruleIds; // Keeping a list of RuleIds to keep sorting order... QHash m_rules; // ...but use a Hash for faster finding }; +Q_DECLARE_METATYPE(RuleEngine::RuleError) #endif // RULEENGINE_H diff --git a/server/server.pri b/server/server.pri index 7a7f84e2..daed4e18 100644 --- a/server/server.pri +++ b/server/server.pri @@ -23,3 +23,4 @@ HEADERS += $$top_srcdir/server/guhcore.h \ $$top_srcdir/server/jsonrpc/actionhandler.h \ $$top_srcdir/server/jsonrpc/eventhandler.h \ $$top_srcdir/server/stateevaluator.h \ + $$top_srcdir/server/jsontypes.h diff --git a/server/stateevaluator.cpp b/server/stateevaluator.cpp index 3678955b..1f92b33d 100644 --- a/server/stateevaluator.cpp +++ b/server/stateevaluator.cpp @@ -22,12 +22,12 @@ StateEvaluator::StateEvaluator(const StateDescriptor &stateDescriptor): m_stateDescriptor(stateDescriptor), - m_operatorType(StateOperatorAnd) + m_operatorType(Types::StateOperatorAnd) { } -StateEvaluator::StateEvaluator(QList childEvaluators, StateOperator stateOperator): +StateEvaluator::StateEvaluator(QList childEvaluators, Types::StateOperator stateOperator): m_stateDescriptor(), m_childEvaluators(childEvaluators), m_operatorType(stateOperator) @@ -54,12 +54,12 @@ void StateEvaluator::appendEvaluator(const StateEvaluator &stateEvaluator) m_childEvaluators.append(stateEvaluator); } -StateOperator StateEvaluator::operatorType() const +Types::StateOperator StateEvaluator::operatorType() const { return m_operatorType; } -void StateEvaluator::setOperatorType(StateOperator operatorType) +void StateEvaluator::setOperatorType(Types::StateOperator operatorType) { m_operatorType = operatorType; } @@ -82,7 +82,7 @@ bool StateEvaluator::evaluate() const } } - if (m_operatorType == StateOperatorOr) { + if (m_operatorType == Types::StateOperatorOr) { foreach (const StateEvaluator &stateEvaluator, m_childEvaluators) { if (stateEvaluator.evaluate()) { return true; @@ -151,13 +151,13 @@ StateEvaluator StateEvaluator::loadFromSettings(QSettings &settings, const QStri StateTypeId stateTypeId(settings.value("stateTypeId").toString()); DeviceId deviceId(settings.value("deviceId").toString()); QVariant stateValue = settings.value("value"); - ValueOperator valueOperator = (ValueOperator)settings.value("operator").toInt(); + Types::ValueOperator valueOperator = (Types::ValueOperator)settings.value("operator").toInt(); StateDescriptor stateDescriptor(stateTypeId, deviceId, stateValue, valueOperator); settings.endGroup(); StateEvaluator ret(stateDescriptor); - ret.setOperatorType((StateOperator)settings.value("operator").toInt()); + ret.setOperatorType((Types::StateOperator)settings.value("operator").toInt()); settings.beginGroup("childEvaluators"); foreach (const QString &evaluatorGroup, settings.childGroups()) { diff --git a/server/stateevaluator.h b/server/stateevaluator.h index 311b489b..63d43011 100644 --- a/server/stateevaluator.h +++ b/server/stateevaluator.h @@ -27,13 +27,8 @@ class StateEvaluator { public: - enum OperatorType { - OperatorTypeAnd, - OperatorTypeOr - }; - StateEvaluator(const StateDescriptor &stateDescriptor); - StateEvaluator(QList childEvaluators = QList(), StateOperator stateOperator = StateOperatorAnd); + StateEvaluator(QList childEvaluators = QList(), Types::StateOperator stateOperator = Types::StateOperatorAnd); StateDescriptor stateDescriptor() const; @@ -41,8 +36,8 @@ public: void setChildEvaluators(const QList &childEvaluators); void appendEvaluator(const StateEvaluator &stateEvaluator); - StateOperator operatorType() const; - void setOperatorType(StateOperator operatorType); + Types::StateOperator operatorType() const; + void setOperatorType(Types::StateOperator operatorType); bool evaluate() const; bool containsDevice(const DeviceId &deviceId) const; @@ -56,7 +51,7 @@ private: StateDescriptor m_stateDescriptor; QList m_childEvaluators; - StateOperator m_operatorType; + Types::StateOperator m_operatorType; }; #endif // STATEEVALUATOR_H diff --git a/tests/auto/actions/testactions.cpp b/tests/auto/actions/testactions.cpp index 753aef9a..6e0f6aaa 100644 --- a/tests/auto/actions/testactions.cpp +++ b/tests/auto/actions/testactions.cpp @@ -47,7 +47,7 @@ void TestActions::executeAction_data() QTest::addColumn("deviceId"); QTest::addColumn("actionTypeId"); QTest::addColumn("actionParams"); - QTest::addColumn("success"); + QTest::addColumn("error"); QVariantList params; QVariantMap param1; @@ -59,13 +59,13 @@ void TestActions::executeAction_data() param2.insert("value", true); params.append(param2); - QTest::newRow("valid action") << m_mockDeviceId << mockActionIdWithParams << params << true; - QTest::newRow("invalid deviceId") << DeviceId::createDeviceId() << mockActionIdWithParams << params << false; - QTest::newRow("invalid actionTypeId") << m_mockDeviceId << ActionTypeId::createActionTypeId() << params << false; - QTest::newRow("missing params") << m_mockDeviceId << mockActionIdWithParams << QVariantList() << false; - QTest::newRow("async action") << m_mockDeviceId << mockActionIdAsync << QVariantList() << true; - QTest::newRow("broken action") << m_mockDeviceId << mockActionIdFailing << QVariantList() << false; - QTest::newRow("async broken action") << m_mockDeviceId << mockActionIdAsyncFailing << QVariantList() << false; + QTest::newRow("valid action") << m_mockDeviceId << mockActionIdWithParams << params << DeviceManager::DeviceErrorNoError; + QTest::newRow("invalid deviceId") << DeviceId::createDeviceId() << mockActionIdWithParams << params << DeviceManager::DeviceErrorDeviceNotFound; + QTest::newRow("invalid actionTypeId") << m_mockDeviceId << ActionTypeId::createActionTypeId() << params << DeviceManager::DeviceErrorActionTypeNotFound; + QTest::newRow("missing params") << m_mockDeviceId << mockActionIdWithParams << QVariantList() << DeviceManager::DeviceErrorMissingParameter; + QTest::newRow("async action") << m_mockDeviceId << mockActionIdAsync << QVariantList() << DeviceManager::DeviceErrorNoError; + QTest::newRow("broken action") << m_mockDeviceId << mockActionIdFailing << QVariantList() << DeviceManager::DeviceErrorActionParameterError; + QTest::newRow("async broken action") << m_mockDeviceId << mockActionIdAsyncFailing << QVariantList() << DeviceManager::DeviceErrorActionParameterError; } void TestActions::executeAction() @@ -73,7 +73,7 @@ void TestActions::executeAction() QFETCH(DeviceId, deviceId); QFETCH(ActionTypeId, actionTypeId); QFETCH(QVariantList, actionParams); - QFETCH(bool, success); + QFETCH(DeviceManager::DeviceError, error); QVariantMap params; params.insert("actionTypeId", actionTypeId); @@ -81,7 +81,7 @@ void TestActions::executeAction() params.insert("params", actionParams); QVariant response = injectAndWait("Actions.ExecuteAction", params); qDebug() << "executeActionresponse" << response; - verifySuccess(response, success); + verifyError(response, "deviceError", error); // Fetch action execution history from mock device QNetworkAccessManager nam; @@ -94,7 +94,7 @@ void TestActions::executeAction() reply->deleteLater(); QByteArray data = reply->readAll(); - if (success) { + if (error == DeviceManager::DeviceErrorNoError) { QVERIFY2(actionTypeId == ActionTypeId(data), QString("ActionTypeId mismatch. Got %1, Expected: %2") .arg(ActionTypeId(data).toString()).arg(actionTypeId.toString()).toLatin1().data()); } else { @@ -123,24 +123,24 @@ void TestActions::executeAction() void TestActions::getActionTypes_data() { QTest::addColumn("actionTypeId"); - QTest::addColumn("success"); + QTest::addColumn("error"); - QTest::newRow("valid actiontypeid") << mockActionIdWithParams << true; - QTest::newRow("invalid actiontypeid") << ActionTypeId::createActionTypeId() << false; + QTest::newRow("valid actiontypeid") << mockActionIdWithParams << DeviceManager::DeviceErrorNoError; + QTest::newRow("invalid actiontypeid") << ActionTypeId::createActionTypeId() << DeviceManager::DeviceErrorActionTypeNotFound; } void TestActions::getActionTypes() { QFETCH(ActionTypeId, actionTypeId); - QFETCH(bool, success); + QFETCH(DeviceManager::DeviceError, error); QVariantMap params; params.insert("actionTypeId", actionTypeId.toString()); QVariant response = injectAndWait("Actions.GetActionType", params); - verifySuccess(response, success); + verifyError(response, "deviceError", error); - if (success) { + if (error == DeviceManager::DeviceErrorNoError) { QVERIFY2(ActionTypeId(response.toMap().value("params").toMap().value("actionType").toMap().value("id").toString()) == actionTypeId, "Didnt get reply for same actionTypeId as requested."); } } diff --git a/tests/auto/devices/testdevices.cpp b/tests/auto/devices/testdevices.cpp index cec90374..98592efb 100644 --- a/tests/auto/devices/testdevices.cpp +++ b/tests/auto/devices/testdevices.cpp @@ -84,41 +84,41 @@ void TestDevices::getPlugins() void TestDevices::getPluginConfig_data() { QTest::addColumn("pluginId"); - QTest::addColumn("success"); + QTest::addColumn("error"); - QTest::newRow("valid plugin") << mockPluginId << true; - QTest::newRow("invalid plugin") << PluginId::createPluginId() << false; + QTest::newRow("valid plugin") << mockPluginId << DeviceManager::DeviceErrorNoError; + QTest::newRow("invalid plugin") << PluginId::createPluginId() << DeviceManager::DeviceErrorPluginNotFound; } void TestDevices::getPluginConfig() { QFETCH(PluginId, pluginId); - QFETCH(bool, success); + QFETCH(DeviceManager::DeviceError, error); QVariantMap params; params.insert("pluginId", pluginId); QVariant response = injectAndWait("Devices.GetPluginConfiguration", params); - verifySuccess(response, success); + verifyError(response, "deviceError", error); } void TestDevices::setPluginConfig_data() { QTest::addColumn("pluginId"); QTest::addColumn("value"); - QTest::addColumn("success"); + QTest::addColumn("error"); - QTest::newRow("valid") << mockPluginId << QVariant(13) << true; - QTest::newRow("invalid plugin") << PluginId::createPluginId() << QVariant(13) << false; - QTest::newRow("too big") << mockPluginId << QVariant(130) << false; - QTest::newRow("too small") << mockPluginId << QVariant(-13) << false; - QTest::newRow("wrong type") << mockPluginId << QVariant("wrontType") << false; + QTest::newRow("valid") << mockPluginId << QVariant(13) << DeviceManager::DeviceErrorNoError; + QTest::newRow("invalid plugin") << PluginId::createPluginId() << QVariant(13) << DeviceManager::DeviceErrorPluginNotFound; + QTest::newRow("too big") << mockPluginId << QVariant(130) << DeviceManager::DeviceErrorInvalidParameter; + QTest::newRow("too small") << mockPluginId << QVariant(-13) << DeviceManager::DeviceErrorInvalidParameter; + QTest::newRow("wrong type") << mockPluginId << QVariant("wrontType") << DeviceManager::DeviceErrorInvalidParameter; } void TestDevices::setPluginConfig() { QFETCH(PluginId, pluginId); QFETCH(QVariant, value); - QFETCH(bool, success); + QFETCH(DeviceManager::DeviceError, error); QVariantMap params; params.insert("pluginId", pluginId); @@ -130,13 +130,13 @@ void TestDevices::setPluginConfig() configuration.append(configParam); params.insert("configuration", configuration); QVariant response = injectAndWait("Devices.SetPluginConfiguration", params); - verifySuccess(response, success); + verifyError(response, "deviceError", error); - if (success) { + if (error == DeviceManager::DeviceErrorNoError) { params.clear(); params.insert("pluginId", pluginId); response = injectAndWait("Devices.GetPluginConfiguration", params); - verifySuccess(response); + verifyError(response, "deviceError"); qDebug() << "222" << response.toMap().value("params").toMap().value("configuration").toList().first(); QVERIFY2(response.toMap().value("params").toMap().value("configuration").toList().first().toMap().value("name") == "configParamInt", "Value not set correctly"); QVERIFY2(response.toMap().value("params").toMap().value("configuration").toList().first().toMap().value("value") == value, "Value not set correctly"); @@ -250,14 +250,14 @@ void TestDevices::addConfiguredDevice() QVariant response = injectAndWait("Devices.AddConfiguredDevice", params); qDebug() << "response is" << response; - verifySuccess(response, success); + verifyError(response, "deviceError", success); if (success) { QUuid deviceId(response.toMap().value("params").toMap().value("deviceId").toString()); params.clear(); params.insert("deviceId", deviceId.toString()); response = injectAndWait("Devices.RemoveConfiguredDevice", params); - verifySuccess(response); + verifyError(response, "deviceError"); } } @@ -296,7 +296,7 @@ void TestDevices::removeDevice() QVariant response = injectAndWait("Devices.RemoveConfiguredDevice", params); - verifySuccess(response, success); + verifyError(response, "deviceError", success); if (success) { // Make sure the device is gone from settings too @@ -323,7 +323,7 @@ void TestDevices::storedDevices() deviceParams.append(httpportParam); params.insert("deviceParams", deviceParams); QVariant response = injectAndWait("Devices.AddConfiguredDevice", params); - verifySuccess(response); + verifyError(response, "deviceError"); DeviceId addedDeviceId = DeviceId(response.toMap().value("params").toMap().value("deviceId").toString()); QVERIFY(!addedDeviceId.isNull()); @@ -352,14 +352,14 @@ void TestDevices::storedDevices() params.clear(); params.insert("deviceId", addedDeviceId); response = injectAndWait("Devices.RemoveConfiguredDevice", params); - verifySuccess(response); + verifyError(response, "deviceError"); } void TestDevices::discoverDevices_data() { QTest::addColumn("deviceClassId"); QTest::addColumn("resultCount"); - QTest::addColumn("success"); + QTest::addColumn("error"); QTest::addColumn("discoveryParams"); QVariantList discoveryParams; @@ -368,16 +368,16 @@ void TestDevices::discoverDevices_data() resultCountParam.insert("value", 1); discoveryParams.append(resultCountParam); - QTest::newRow("valid deviceClassId") << mockDeviceClassId << 2 << true << QVariantList(); - QTest::newRow("valid deviceClassId with params") << mockDeviceClassId << 1 << true << discoveryParams; - QTest::newRow("invalid deviceClassId") << DeviceClassId::createDeviceClassId() << 0 << false << QVariantList(); + QTest::newRow("valid deviceClassId") << mockDeviceClassId << 2 << DeviceManager::DeviceErrorNoError << QVariantList(); + QTest::newRow("valid deviceClassId with params") << mockDeviceClassId << 1 << DeviceManager::DeviceErrorNoError << discoveryParams; + QTest::newRow("invalid deviceClassId") << DeviceClassId::createDeviceClassId() << 0 << DeviceManager::DeviceErrorDeviceClassNotFound << QVariantList(); } void TestDevices::discoverDevices() { QFETCH(DeviceClassId, deviceClassId); QFETCH(int, resultCount); - QFETCH(bool, success); + QFETCH(DeviceManager::DeviceError, error); QFETCH(QVariantList, discoveryParams); QVariantMap params; @@ -385,13 +385,13 @@ void TestDevices::discoverDevices() params.insert("discoveryParams", discoveryParams); QVariant response = injectAndWait("Devices.GetDiscoveredDevices", params); - verifySuccess(response, success); - if (success) { + verifyError(response, "deviceError", error); + if (error == DeviceManager::DeviceErrorNoError) { QCOMPARE(response.toMap().value("params").toMap().value("deviceDescriptors").toList().count(), resultCount); } // If we found something, lets try to add it - if (success) { + if (DeviceManager::DeviceErrorNoError) { DeviceDescriptorId descriptorId = DeviceDescriptorId(response.toMap().value("params").toMap().value("deviceDescriptors").toList().first().toMap().value("id").toString()); params.clear(); @@ -399,13 +399,13 @@ void TestDevices::discoverDevices() params.insert("deviceDescriptorId", descriptorId.toString()); response = injectAndWait("Devices.AddConfiguredDevice", params); - verifySuccess(response); + verifyError(response, "deviceError"); DeviceId deviceId(response.toMap().value("params").toMap().value("deviceId").toString()); params.clear(); params.insert("deviceId", deviceId.toString()); response = injectAndWait("Devices.RemoveConfiguredDevice", params); - verifySuccess(response); + verifyError(response, "deviceError"); } } diff --git a/tests/auto/guhtestbase.cpp b/tests/auto/guhtestbase.cpp index 48496707..487970e7 100644 --- a/tests/auto/guhtestbase.cpp +++ b/tests/auto/guhtestbase.cpp @@ -91,7 +91,7 @@ void GuhTestBase::initTestCase() QVariant response = injectAndWait("Devices.AddConfiguredDevice", params); - verifySuccess(response); + verifyError(response, "deviceError"); m_mockDeviceId = DeviceId(response.toMap().value("params").toMap().value("deviceId").toString()); QVERIFY2(!m_mockDeviceId.isNull(), "Newly created mock device must not be null."); @@ -125,11 +125,11 @@ QVariant GuhTestBase::injectAndWait(const QString &method, const QVariantMap &pa return jsonDoc.toVariant(); } -void GuhTestBase::verifySuccess(const QVariant &response, bool success) +void GuhTestBase::verifyError(const QVariant &response, const QString &fieldName, int error) { QJsonDocument jsonDoc = QJsonDocument::fromVariant(response); QVERIFY2(response.toMap().value("status").toString() == QString("success"), jsonDoc.toJson().data()); - QVERIFY2(response.toMap().value("params").toMap().value("success").toBool() == success, jsonDoc.toJson().data()); + QVERIFY2(response.toMap().value("params").toMap().value(fieldName).toInt() == error, jsonDoc.toJson().data()); } void GuhTestBase::restartServer() diff --git a/tests/auto/guhtestbase.h b/tests/auto/guhtestbase.h index d1be062e..1062b94f 100644 --- a/tests/auto/guhtestbase.h +++ b/tests/auto/guhtestbase.h @@ -21,6 +21,7 @@ #include "typeutils.h" #include "mocktcpserver.h" +#include "devicemanager.h" #include #include @@ -57,7 +58,7 @@ protected slots: protected: QVariant injectAndWait(const QString &method, const QVariantMap ¶ms = QVariantMap()); - void verifySuccess(const QVariant &response, bool success = true); + void verifyError(const QVariant &response, const QString &fieldName, int error = 0); void restartServer(); protected: diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index 6d19dadd..e3229e27 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -181,7 +181,7 @@ void TestJSONRPC::enableDisableNotifications() params.insert("enabled", enabled); QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", params); - verifySuccess(response); + verifyError(response, "error"); QCOMPARE(response.toMap().value("params").toMap().value("enabled").toString(), enabled); } @@ -191,7 +191,7 @@ void TestJSONRPC::stateChangeEmitsNotifications() QVariantMap params; params.insert("enabled", true); QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", params); - verifySuccess(response); + verifyError(response, "serverError"); // Setup connection to mock client QNetworkAccessManager nam; @@ -226,7 +226,7 @@ void TestJSONRPC::stateChangeEmitsNotifications() params.clear(); params.insert("enabled", false); response = injectAndWait("JSONRPC.SetNotificationStatus", params); - verifySuccess(response); + verifyError(response, "serverError"); // Fire the a statechange once again clientSpy.clear(); diff --git a/tests/auto/rules/testrules.cpp b/tests/auto/rules/testrules.cpp index 45744137..a2d83be5 100644 --- a/tests/auto/rules/testrules.cpp +++ b/tests/auto/rules/testrules.cpp @@ -104,14 +104,14 @@ void TestRules::addRemoveRules_data() QTest::addColumn("eventDescriptor"); QTest::addColumn("eventDescriptorList"); QTest::addColumn("stateEvaluator"); - QTest::addColumn("success"); + QTest::addColumn("error"); - QTest::newRow("valid rule. 1 EventDescriptor, StateEvaluator, 1 Action") << validActionNoParams << validEventDescriptor1 << QVariantList() << validStateEvaluator << true; - QTest::newRow("valid rule. 2 EventDescriptors, 1 Action") << validActionNoParams << QVariantMap() << eventDescriptorList << validStateEvaluator << true; - QTest::newRow("invalid rule: eventDescriptor and eventDescriptorList used") << validActionNoParams << validEventDescriptor1 << eventDescriptorList << validStateEvaluator << false; - QTest::newRow("invalid action") << invalidAction << validEventDescriptor1 << QVariantList() << validStateEvaluator << false; - QTest::newRow("invalid event descriptor") << validActionNoParams << invalidEventDescriptor << QVariantList() << validStateEvaluator << false; + QTest::newRow("valid rule. 1 EventDescriptor, StateEvaluator, 1 Action") << validActionNoParams << validEventDescriptor1 << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorNoError; + QTest::newRow("valid rule. 2 EventDescriptors, 1 Action") << validActionNoParams << QVariantMap() << eventDescriptorList << validStateEvaluator << RuleEngine::RuleErrorNoError; + QTest::newRow("invalid rule: eventDescriptor and eventDescriptorList used") << validActionNoParams << validEventDescriptor1 << eventDescriptorList << validStateEvaluator << RuleEngine::RuleErrorInvalidParameter; + QTest::newRow("invalid action") << invalidAction << validEventDescriptor1 << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorActionTypeNotFound; + QTest::newRow("invalid event descriptor") << validActionNoParams << invalidEventDescriptor << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorInvalidParameter; // QTest::newRow("invalid state evaluator") << validActionNoParams << invalidEventDescriptor << QVariantList() << invalidStateEvaluator << false; } @@ -122,7 +122,7 @@ void TestRules::addRemoveRules() QFETCH(QVariantMap, eventDescriptor); QFETCH(QVariantList, eventDescriptorList); QFETCH(QVariantMap, stateEvaluator); - QFETCH(bool, success); + QFETCH(RuleEngine::RuleError, error); QVariantMap params; QVariantList actions; @@ -137,14 +137,14 @@ void TestRules::addRemoveRules() } params.insert("stateEvaluator", stateEvaluator); QVariant response = injectAndWait("Rules.AddRule", params); - verifySuccess(response, success); + verifyError(response, "ruleError", error); RuleId newRuleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString()); response = injectAndWait("Rules.GetRules"); QVariantList rules = response.toMap().value("params").toMap().value("ruleIds").toList(); - if (!success) { + if (error != RuleEngine::RuleErrorNoError) { QVERIFY2(rules.count() == 0, "There should be no rules."); return; } @@ -185,7 +185,7 @@ void TestRules::addRemoveRules() params.clear(); params.insert("ruleId", newRuleId); response = injectAndWait("Rules.RemoveRule", params); - verifySuccess(response, true); + verifyError(response, "ruleError"); response = injectAndWait("Rules.GetRules"); rules = response.toMap().value("params").toMap().value("rules").toList(); @@ -197,7 +197,7 @@ void TestRules::removeInvalidRule() QVariantMap params; params.insert("ruleId", RuleId::createRuleId()); QVariant response = injectAndWait("Rules.RemoveRule", params); - verifySuccess(response, false); + verifyError(response, "ruleError", RuleEngine::RuleErrorInvalidRuleId); } void TestRules::loadStoreConfig() @@ -253,7 +253,7 @@ void TestRules::loadStoreConfig() QVariant response = injectAndWait("Rules.AddRule", params); RuleId newRuleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString()); - verifySuccess(response, true); + verifyError(response, "ruleError"); restartServer(); @@ -302,7 +302,7 @@ void TestRules::loadStoreConfig() params.clear(); params.insert("ruleId", newRuleId); response = injectAndWait("Rules.RemoveRule", params); - verifySuccess(response, true); + verifyError(response, "ruleError"); restartServer(); @@ -329,7 +329,7 @@ void TestRules::evaluateEvent() actions.append(action); addRuleParams.insert("actions", actions); QVariant response = injectAndWait("Rules.AddRule", addRuleParams); - verifySuccess(response, true); + verifyError(response, "ruleError"); // Trigger an event QNetworkAccessManager nam; @@ -360,29 +360,29 @@ void TestRules::testStateEvaluator_data() QTest::addColumn("deviceId"); QTest::addColumn("stateTypeId"); QTest::addColumn("value"); - QTest::addColumn("operatorType"); + QTest::addColumn("operatorType"); QTest::addColumn("shouldMatch"); - QTest::newRow("invalid stateId") << m_mockDeviceId << StateTypeId::createStateTypeId() << QVariant(10) << ValueOperatorEquals << false; - QTest::newRow("invalid deviceId") << DeviceId::createDeviceId() << mockIntStateId << QVariant(10) << ValueOperatorEquals << false; + QTest::newRow("invalid stateId") << m_mockDeviceId << StateTypeId::createStateTypeId() << QVariant(10) << Types::ValueOperatorEquals << false; + QTest::newRow("invalid deviceId") << DeviceId::createDeviceId() << mockIntStateId << QVariant(10) << Types::ValueOperatorEquals << false; - QTest::newRow("equals, not matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << ValueOperatorEquals << false; - QTest::newRow("equals, matching") << m_mockDeviceId << mockIntStateId << QVariant(10) << ValueOperatorEquals << true; + QTest::newRow("equals, not matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << Types::ValueOperatorEquals << false; + QTest::newRow("equals, matching") << m_mockDeviceId << mockIntStateId << QVariant(10) << Types::ValueOperatorEquals << true; - QTest::newRow("not equal, not matching") << m_mockDeviceId << mockIntStateId << QVariant(10) << ValueOperatorNotEquals << false; - QTest::newRow("not equal, matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << ValueOperatorNotEquals << true; + QTest::newRow("not equal, not matching") << m_mockDeviceId << mockIntStateId << QVariant(10) << Types::ValueOperatorNotEquals << false; + QTest::newRow("not equal, matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << Types::ValueOperatorNotEquals << true; - QTest::newRow("Greater, not matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << ValueOperatorGreater << false; - QTest::newRow("Greater, matching") << m_mockDeviceId << mockIntStateId << QVariant(2) << ValueOperatorGreater << true; - QTest::newRow("GreaterOrEqual, not matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << ValueOperatorGreaterOrEqual << false; - QTest::newRow("GreaterOrEqual, matching (greater)") << m_mockDeviceId << mockIntStateId << QVariant(2) << ValueOperatorGreaterOrEqual << true; - QTest::newRow("GreaterOrEqual, matching (equals)") << m_mockDeviceId << mockIntStateId << QVariant(10) << ValueOperatorGreaterOrEqual << true; + QTest::newRow("Greater, not matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << Types::ValueOperatorGreater << false; + QTest::newRow("Greater, matching") << m_mockDeviceId << mockIntStateId << QVariant(2) << Types::ValueOperatorGreater << true; + QTest::newRow("GreaterOrEqual, not matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << Types::ValueOperatorGreaterOrEqual << false; + QTest::newRow("GreaterOrEqual, matching (greater)") << m_mockDeviceId << mockIntStateId << QVariant(2) << Types::ValueOperatorGreaterOrEqual << true; + QTest::newRow("GreaterOrEqual, matching (equals)") << m_mockDeviceId << mockIntStateId << QVariant(10) << Types::ValueOperatorGreaterOrEqual << true; - QTest::newRow("Less, not matching") << m_mockDeviceId << mockIntStateId << QVariant(2) << ValueOperatorLess << false; - QTest::newRow("Less, matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << ValueOperatorLess << true; - QTest::newRow("LessOrEqual, not matching") << m_mockDeviceId << mockIntStateId << QVariant(2) << ValueOperatorLessOrEqual << false; - QTest::newRow("LessOrEqual, matching (less)") << m_mockDeviceId << mockIntStateId << QVariant(777) << ValueOperatorLessOrEqual << true; - QTest::newRow("LessOrEqual, matching (equals)") << m_mockDeviceId << mockIntStateId << QVariant(10) << ValueOperatorLessOrEqual << true; + QTest::newRow("Less, not matching") << m_mockDeviceId << mockIntStateId << QVariant(2) << Types::ValueOperatorLess << false; + QTest::newRow("Less, matching") << m_mockDeviceId << mockIntStateId << QVariant(7777) << Types::ValueOperatorLess << true; + QTest::newRow("LessOrEqual, not matching") << m_mockDeviceId << mockIntStateId << QVariant(2) << Types::ValueOperatorLessOrEqual << false; + QTest::newRow("LessOrEqual, matching (less)") << m_mockDeviceId << mockIntStateId << QVariant(777) << Types::ValueOperatorLessOrEqual << true; + QTest::newRow("LessOrEqual, matching (equals)") << m_mockDeviceId << mockIntStateId << QVariant(10) << Types::ValueOperatorLessOrEqual << true; } void TestRules::testStateEvaluator() @@ -390,7 +390,7 @@ void TestRules::testStateEvaluator() QFETCH(DeviceId, deviceId); QFETCH(StateTypeId, stateTypeId); QFETCH(QVariant, value); - QFETCH(ValueOperator, operatorType); + QFETCH(Types::ValueOperator, operatorType); QFETCH(bool, shouldMatch); StateDescriptor descriptor(stateTypeId, deviceId, value, operatorType); @@ -402,31 +402,31 @@ void TestRules::testStateEvaluator() void TestRules::testStateEvaluator2_data() { QTest::addColumn("intValue"); - QTest::addColumn("intOperator"); + QTest::addColumn("intOperator"); QTest::addColumn("boolValue"); - QTest::addColumn("boolOperator"); + QTest::addColumn("boolOperator"); - QTest::addColumn("stateOperator"); + QTest::addColumn("stateOperator"); QTest::addColumn("shouldMatch"); - QTest::newRow("Y: 10 && false") << 10 << ValueOperatorEquals << false << ValueOperatorEquals << StateOperatorAnd << true; - QTest::newRow("N: 10 && true") << 10 << ValueOperatorEquals << true << ValueOperatorEquals << StateOperatorAnd << false; - QTest::newRow("N: 11 && false") << 11 << ValueOperatorEquals << false << ValueOperatorEquals << StateOperatorAnd << false; - QTest::newRow("Y: 11 || false") << 11 << ValueOperatorEquals << false << ValueOperatorEquals << StateOperatorOr << true; - QTest::newRow("Y: 10 || false") << 10 << ValueOperatorEquals << false << ValueOperatorEquals << StateOperatorOr << true; - QTest::newRow("Y: 10 || true") << 10 << ValueOperatorEquals << true << ValueOperatorEquals << StateOperatorOr << true; - QTest::newRow("N: 11 || true") << 11 << ValueOperatorEquals << true << ValueOperatorEquals << StateOperatorOr << false; + QTest::newRow("Y: 10 && false") << 10 << Types::ValueOperatorEquals << false << Types::ValueOperatorEquals << Types::StateOperatorAnd << true; + QTest::newRow("N: 10 && true") << 10 << Types::ValueOperatorEquals << true << Types::ValueOperatorEquals << Types::StateOperatorAnd << false; + QTest::newRow("N: 11 && false") << 11 << Types::ValueOperatorEquals << false << Types::ValueOperatorEquals << Types::StateOperatorAnd << false; + QTest::newRow("Y: 11 || false") << 11 << Types::ValueOperatorEquals << false << Types::ValueOperatorEquals << Types::StateOperatorOr << true; + QTest::newRow("Y: 10 || false") << 10 << Types::ValueOperatorEquals << false << Types::ValueOperatorEquals << Types::StateOperatorOr << true; + QTest::newRow("Y: 10 || true") << 10 << Types::ValueOperatorEquals << true << Types::ValueOperatorEquals << Types::StateOperatorOr << true; + QTest::newRow("N: 11 || true") << 11 << Types::ValueOperatorEquals << true << Types::ValueOperatorEquals << Types::StateOperatorOr << false; } void TestRules::testStateEvaluator2() { QFETCH(int, intValue); - QFETCH(ValueOperator, intOperator); + QFETCH(Types::ValueOperator, intOperator); QFETCH(bool, boolValue); - QFETCH(ValueOperator, boolOperator); - QFETCH(StateOperator, stateOperator); + QFETCH(Types::ValueOperator, boolOperator); + QFETCH(Types::StateOperator, stateOperator); QFETCH(bool, shouldMatch); StateDescriptor descriptor1(mockIntStateId, m_mockDeviceId, intValue, intOperator); diff --git a/tests/auto/states/teststates.cpp b/tests/auto/states/teststates.cpp index 1de97b91..c607dc2e 100644 --- a/tests/auto/states/teststates.cpp +++ b/tests/auto/states/teststates.cpp @@ -46,18 +46,18 @@ void TestStates::getStateValue_data() QTest::addColumn("deviceId"); QTest::addColumn("stateTypeId"); - QTest::addColumn("success"); + QTest::addColumn("error"); - QTest::newRow("existing state") << device->id() << mockIntStateId << true; - QTest::newRow("invalid device") << DeviceId::createDeviceId() << mockIntStateId << false; - QTest::newRow("invalid statetype") << device->id() << StateTypeId::createStateTypeId() << false; + QTest::newRow("existing state") << device->id() << mockIntStateId << DeviceManager::DeviceErrorNoError; + QTest::newRow("invalid device") << DeviceId::createDeviceId() << mockIntStateId << DeviceManager::DeviceErrorDeviceNotFound; + QTest::newRow("invalid statetype") << device->id() << StateTypeId::createStateTypeId() << DeviceManager::DeviceErrorInvalidParameter; } void TestStates::getStateValue() { QFETCH(DeviceId, deviceId); QFETCH(StateTypeId, stateTypeId); - QFETCH(bool, success); + QFETCH(DeviceManager::DeviceError, error); QVariantMap params; params.insert("deviceId", deviceId.toString()); @@ -65,7 +65,7 @@ void TestStates::getStateValue() QVariant response = injectAndWait("Devices.GetStateValue", params); - verifySuccess(response, success); + verifyError(response, "deviceError", error); } #include "teststates.moc"