fix plugin configuration and move to plugin json file

close #264
close #268
This commit is contained in:
Simon Stürz 2016-01-13 10:34:53 +01:00 committed by Michael Zanetti
parent ffaa50c6c4
commit 9b9a5d6b23
11 changed files with 57 additions and 74 deletions

View File

@ -10,7 +10,6 @@ DEFINES += GUH_VERSION_STRING=\\\"$${GUH_VERSION_STRING}\\\" \
REST_API_VERSION=\\\"$${REST_API_VERSION}\\\"
QT+= network
CONFIG += c++11
QMAKE_CXXFLAGS += -Werror -std=c++11
QMAKE_LFLAGS += -std=c++11

View File

@ -295,21 +295,18 @@ DevicePlugin *DeviceManager::plugin(const PluginId &id) const
DeviceManager::DeviceError DeviceManager::setPluginConfig(const PluginId &pluginId, const ParamList &pluginConfig)
{
DevicePlugin *plugin = m_devicePlugins.value(pluginId);
if (!plugin) {
if (!plugin)
return DeviceErrorPluginNotFound;
}
ParamList params = pluginConfig;
DeviceError verify = verifyParams(plugin->configurationDescription(), params);
if (verify != DeviceErrorNoError) {
if (verify != DeviceErrorNoError)
return verify;
}
DeviceError result = plugin->setConfiguration(pluginConfig);
if (result != DeviceErrorNoError) {
if (result != DeviceErrorNoError)
return result;
}
GuhSettings settings(GuhSettings::SettingsRolePlugins);
settings.beginGroup("PluginConfig");
@ -836,10 +833,12 @@ DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType &paramType
qCWarning(dcDeviceManager) << "Value out of range for param" << param.name() << " Got:" << param.value() << " Max:" << paramType.maxValue();
return DeviceErrorInvalidParameter;
}
if (paramType.minValue().isValid() && param.value() < paramType.minValue()) {
qCWarning(dcDeviceManager) << "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;
foreach (const QVariant &value, paramType.allowedValues()) {
@ -851,6 +850,7 @@ DeviceManager::DeviceError DeviceManager::verifyParam(const ParamType &paramType
}
return DeviceErrorNoError;
}
qCWarning(dcDeviceManager) << "Parameter name" << param.name() << "does not match with ParamType name" << paramType.name();
return DeviceErrorInvalidParameter;
}

View File

@ -137,6 +137,7 @@
#include "loggingcategories.h"
#include "devicemanager.h"
#include "guhsettings.h"
#include "hardware/radio433/radio433.h"
#include "network/upnpdiscovery/upnpdiscovery.h"
@ -412,7 +413,7 @@ DeviceManager::DeviceError DevicePlugin::executeAction(Device *device, const Act
/*! Returns the configuration description of this DevicePlugin as a list of \l{ParamType}{ParamTypes}. */
QList<ParamType> DevicePlugin::configurationDescription() const
{
return QList<ParamType>();
return m_configurationDescription;
}
/*! This will be called when the DeviceManager initializes the plugin and set up the things behind the scenes.
@ -420,6 +421,11 @@ QList<ParamType> DevicePlugin::configurationDescription() const
void DevicePlugin::initPlugin(const QJsonObject &metaData, DeviceManager *deviceManager)
{
m_metaData = metaData;
// parse plugin configuration params
if (m_metaData.contains("paramTypes"))
m_configurationDescription = parseParamTypes(m_metaData.value("paramTypes").toArray());
m_deviceManager = deviceManager;
init();
}
@ -495,7 +501,7 @@ QVariant DevicePlugin::configValue(const QString &paramName) const
DeviceManager::DeviceError DevicePlugin::setConfiguration(const ParamList &configuration)
{
foreach (const Param &param, configuration) {
qCDebug(dcDeviceManager) << "* setting config" << param;
qCDebug(dcDeviceManager) << "* set plugin configuration" << param;
DeviceManager::DeviceError result = setConfigValue(param.name(), param.value());
if (result != DeviceManager::DeviceErrorNoError) {
return result;
@ -504,46 +510,33 @@ DeviceManager::DeviceError DevicePlugin::setConfiguration(const ParamList &confi
return DeviceManager::DeviceErrorNoError;
}
/*! Will be called by the DeviceManager to set a plugin's \l{Param} with the given \a paramName and \a value. */
/*! Can be called in the DevicePlugin to set a plugin's \l{Param} with the given \a paramName and \a value. */
DeviceManager::DeviceError DevicePlugin::setConfigValue(const QString &paramName, const QVariant &value)
{
bool found = false;
foreach (const ParamType &paramType, configurationDescription()) {
if (paramType.name() == paramName) {
if (!value.canConvert(paramType.type())) {
qCWarning(dcDeviceManager) << QString("Wrong parameter type for param %1. Got %2. Expected %3.")
.arg(paramName).arg(value.toString()).arg(QVariant::typeToName(paramType.type()));
return DeviceManager::DeviceErrorInvalidParameter;
}
if (paramType.maxValue().isValid() && value > paramType.maxValue()) {
qCWarning(dcDeviceManager) << QString("Value out of range for param %1. Got %2. Max: %3.")
.arg(paramName).arg(value.toString()).arg(paramType.maxValue().toString());
return DeviceManager::DeviceErrorInvalidParameter;
}
if (paramType.minValue().isValid() && value < paramType.minValue()) {
qCWarning(dcDeviceManager) << QString("Value out of range for param %1. Got: %2. Min: %3.")
.arg(paramName).arg(value.toString()).arg(paramType.minValue().toString());
return DeviceManager::DeviceErrorInvalidParameter;
}
found = true;
DeviceManager::DeviceError result = deviceManager()->verifyParam(paramType, Param(paramName, value));
if (result != DeviceManager::DeviceErrorNoError)
return result;
break;
}
}
if (!found) {
qCWarning(dcDeviceManager) << QString("Invalid parameter %1.").arg(paramName);
qCWarning(dcDeviceManager) << QString("Could not find plugin parameter with the name %1.").arg(paramName);
return DeviceManager::DeviceErrorInvalidParameter;
}
for (int i = 0; i < m_config.count(); i++) {
if (m_config.at(i).name() == paramName) {
m_config[i].setValue(value);
emit configValueChanged(paramName, value);
return DeviceManager::DeviceErrorNoError;
}
if (m_config.hasParam(paramName)) {
m_config.setParamValue(paramName, value);
} else {
Param newParam(paramName, value);
m_config.append(newParam);
}
// Still here? need to create the param
Param newParam(paramName, value);
m_config.append(newParam);
emit configValueChanged(paramName, value);
return DeviceManager::DeviceErrorNoError;
}

View File

@ -84,7 +84,7 @@ public:
#endif
// Configuration
virtual QList<ParamType> configurationDescription() const;
QList<ParamType> configurationDescription() const;
DeviceManager::DeviceError setConfiguration(const ParamList &configuration);
ParamList configuration() const;
QVariant configValue(const QString &paramName) const;
@ -131,6 +131,7 @@ private:
DeviceManager *m_deviceManager;
QList<ParamType> m_configurationDescription;
ParamList m_config;
QJsonObject m_metaData;

View File

@ -118,7 +118,7 @@ DevicePluginDateTime::DevicePluginDateTime() :
m_timer = new QTimer(this);
m_timer->setInterval(1000);
m_timeZone = QTimeZone(configValue("timezone").toByteArray());
m_timeZone = QTimeZone(QTimeZone::systemTimeZoneId());
m_currentDateTime = QDateTime(QDate::currentDate(), QTime::currentTime(), m_timeZone);
connect(m_timer, &QTimer::timeout, this, &DevicePluginDateTime::onSecondChanged);
@ -130,20 +130,20 @@ DeviceManager::HardwareResources DevicePluginDateTime::requiredHardware() const
return DeviceManager::HardwareResourceNetworkManager;
}
QList<ParamType> DevicePluginDateTime::configurationDescription() const
{
QList<ParamType> params;
ParamType timezoneParamType("timezone", QVariant::String, "Europe/Vienna");
//QList<ParamType> DevicePluginDateTime::configurationDescription() const
//{
// QList<ParamType> params;
// ParamType timezoneParamType("timezone", QVariant::String, "Europe/Vienna");
QList<QVariant> allowedValues;
foreach (QByteArray timeZone, QTimeZone::availableTimeZoneIds()) {
allowedValues.append(timeZone);
}
timezoneParamType.setAllowedValues(allowedValues);
// QList<QVariant> allowedValues;
// foreach (QByteArray timeZone, QTimeZone::availableTimeZoneIds()) {
// allowedValues.append(timeZone);
// }
// timezoneParamType.setAllowedValues(allowedValues);
params.append(timezoneParamType);
return params;
}
// params.append(timezoneParamType);
// return params;
//}
DeviceManager::DeviceSetupStatus DevicePluginDateTime::setupDevice(Device *device)
{

View File

@ -41,7 +41,6 @@ public:
explicit DevicePluginDateTime();
DeviceManager::HardwareResources requiredHardware() const override;
QList<ParamType> configurationDescription() const override;
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
void postSetupDevice(Device *device) override;
void deviceRemoved(Device *device) override;

View File

@ -87,12 +87,6 @@ DeviceManager::HardwareResources DevicePluginEQ3::requiredHardware() const
return DeviceManager::HardwareResourceTimer;
}
QList<ParamType> DevicePluginEQ3::configurationDescription() const
{
QList<ParamType> params;
return params;
}
DeviceManager::DeviceError DevicePluginEQ3::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{
Q_UNUSED(params)

View File

@ -39,8 +39,6 @@ public:
explicit DevicePluginEQ3();
DeviceManager::HardwareResources requiredHardware() const override;
QList<ParamType> configurationDescription() const override;
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
void startMonitoringAutoDevices() override;

View File

@ -189,19 +189,6 @@ DeviceManager::DeviceError DevicePluginMock::displayPin(const PairingTransaction
return DeviceManager::DeviceErrorNoError;
}
QList<ParamType> DevicePluginMock::configurationDescription() const
{
QList<ParamType> params;
ParamType mockParam1("configParamInt", QVariant::Int, 42);
mockParam1.setLimits(1, 50);
params.append(mockParam1);
ParamType mockParam2("configParamBool", QVariant::Bool, true);
params.append(mockParam2);
return params;
}
DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const Action &action)
{
if (device->deviceClassId() == mockDeviceClassId || device->deviceClassId() == mockDeviceAutoDeviceClassId) {

View File

@ -51,8 +51,6 @@ public:
DeviceManager::DeviceSetupStatus confirmPairing(const PairingTransactionId &pairingTransactionId, const DeviceClassId &deviceClassId, const ParamList &params, const QString &secret) override;
DeviceManager::DeviceError displayPin(const PairingTransactionId &pairingTransactionId, const DeviceDescriptor &deviceDescriptor) override;
QList<ParamType> configurationDescription() const override;
public slots:
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;

View File

@ -2,6 +2,20 @@
"name": "Mock Devices",
"idName": "MockDevice",
"id": "727a4a9a-c187-446f-aadf-f1b2220607d1",
"paramTypes": [
{
"name": "configParamInt",
"type": "int",
"defaultValue": 42,
"minValue": 1,
"maxValue": 50
},
{
"name": "configParamBool",
"type": "bool",
"defaultValue": true
}
],
"vendors": [
{
"name": "guh",