added api for plugin configs
This commit is contained in:
parent
23c1ba123f
commit
4ce012117c
@ -27,7 +27,17 @@ DeviceManager::DeviceManager(QObject *parent) :
|
|||||||
QMetaObject::invokeMethod(this, "loadConfiguredDevices", Qt::QueuedConnection);
|
QMetaObject::invokeMethod(this, "loadConfiguredDevices", Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<DeviceClass> DeviceManager::supportedDevices()
|
QList<DevicePlugin *> DeviceManager::plugins() const
|
||||||
|
{
|
||||||
|
return m_devicePlugins.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
DevicePlugin *DeviceManager::plugin(const QUuid &id) const
|
||||||
|
{
|
||||||
|
return m_devicePlugins.value(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<DeviceClass> DeviceManager::supportedDevices() const
|
||||||
{
|
{
|
||||||
return m_supportedDevices.values();
|
return m_supportedDevices.values();
|
||||||
}
|
}
|
||||||
@ -78,7 +88,7 @@ QList<Device *> DeviceManager::configuredDevices() const
|
|||||||
return m_configuredDevices;
|
return m_configuredDevices;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<Device *> DeviceManager::findConfiguredDevices(const QUuid &deviceClassId)
|
QList<Device *> DeviceManager::findConfiguredDevices(const QUuid &deviceClassId) const
|
||||||
{
|
{
|
||||||
QList<Device*> ret;
|
QList<Device*> ret;
|
||||||
foreach (Device *device, m_configuredDevices) {
|
foreach (Device *device, m_configuredDevices) {
|
||||||
@ -89,7 +99,7 @@ QList<Device *> DeviceManager::findConfiguredDevices(const QUuid &deviceClassId)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeviceClass DeviceManager::findDeviceClassforTrigger(const QUuid &triggerTypeId)
|
DeviceClass DeviceManager::findDeviceClassforTrigger(const QUuid &triggerTypeId) const
|
||||||
{
|
{
|
||||||
foreach (const DeviceClass &deviceClass, m_supportedDevices) {
|
foreach (const DeviceClass &deviceClass, m_supportedDevices) {
|
||||||
foreach (const TriggerType &triggerType, deviceClass.triggers()) {
|
foreach (const TriggerType &triggerType, deviceClass.triggers()) {
|
||||||
@ -101,7 +111,7 @@ DeviceClass DeviceManager::findDeviceClassforTrigger(const QUuid &triggerTypeId)
|
|||||||
return DeviceClass(QUuid(), QUuid());
|
return DeviceClass(QUuid(), QUuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
DeviceClass DeviceManager::findDeviceClass(const QUuid &deviceClassId)
|
DeviceClass DeviceManager::findDeviceClass(const QUuid &deviceClassId) const
|
||||||
{
|
{
|
||||||
foreach (const DeviceClass &deviceClass, m_supportedDevices) {
|
foreach (const DeviceClass &deviceClass, m_supportedDevices) {
|
||||||
if (deviceClass.id() == deviceClassId) {
|
if (deviceClass.id() == deviceClassId) {
|
||||||
|
|||||||
@ -30,15 +30,17 @@ public:
|
|||||||
|
|
||||||
explicit DeviceManager(QObject *parent = 0);
|
explicit DeviceManager(QObject *parent = 0);
|
||||||
|
|
||||||
QList<DeviceClass> supportedDevices();
|
QList<DevicePlugin*> plugins() const;
|
||||||
|
DevicePlugin* plugin(const QUuid &id) const;
|
||||||
|
QList<DeviceClass> supportedDevices() const;
|
||||||
|
|
||||||
QList<Device*> configuredDevices() const;
|
QList<Device*> configuredDevices() const;
|
||||||
DeviceError addConfiguredDevice(const QUuid &deviceClassId, const QVariantMap ¶ms);
|
DeviceError addConfiguredDevice(const QUuid &deviceClassId, const QVariantMap ¶ms);
|
||||||
|
|
||||||
Device* findConfiguredDevice(const QUuid &id) const;
|
Device* findConfiguredDevice(const QUuid &id) const;
|
||||||
QList<Device*> findConfiguredDevices(const QUuid &deviceClassId);
|
QList<Device*> findConfiguredDevices(const QUuid &deviceClassId) const;
|
||||||
DeviceClass findDeviceClassforTrigger(const QUuid &triggerTypeId);
|
DeviceClass findDeviceClassforTrigger(const QUuid &triggerTypeId) const;
|
||||||
DeviceClass findDeviceClass(const QUuid &deviceClassId);
|
DeviceClass findDeviceClass(const QUuid &deviceClassId) const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void emitTrigger(const Trigger &trigger);
|
void emitTrigger(const Trigger &trigger);
|
||||||
|
|||||||
@ -21,6 +21,17 @@ void DevicePlugin::initPlugin(DeviceManager *deviceManager)
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariantMap DevicePlugin::configuration() const
|
||||||
|
{
|
||||||
|
return QVariantMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DevicePlugin::setConfiguration(const QVariantMap &configuration)
|
||||||
|
{
|
||||||
|
Q_UNUSED(configuration)
|
||||||
|
qWarning() << "Plugin" << pluginName() << pluginId() << "does not support any configuration";
|
||||||
|
}
|
||||||
|
|
||||||
DeviceManager *DevicePlugin::deviceManager() const
|
DeviceManager *DevicePlugin::deviceManager() const
|
||||||
{
|
{
|
||||||
return m_deviceManager;
|
return m_deviceManager;
|
||||||
|
|||||||
@ -31,6 +31,9 @@ public:
|
|||||||
// Hardware input
|
// Hardware input
|
||||||
virtual void receiveData(QList<int> rawData) = 0;
|
virtual void receiveData(QList<int> rawData) = 0;
|
||||||
|
|
||||||
|
virtual QVariantMap configuration() const;
|
||||||
|
virtual void setConfiguration(const QVariantMap &configuration);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
virtual void executeAction(Device *device, const Action &action) = 0;
|
virtual void executeAction(Device *device, const Action &action) = 0;
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "hivecore.h"
|
#include "hivecore.h"
|
||||||
#include "devicemanager.h"
|
#include "devicemanager.h"
|
||||||
|
#include "deviceplugin.h"
|
||||||
#include "deviceclass.h"
|
#include "deviceclass.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
#include "rule.h"
|
#include "rule.h"
|
||||||
@ -53,42 +54,7 @@ void JsonRPCServer::processData(int clientId, const QByteArray &jsonData)
|
|||||||
emit commandReceived(targetNamespace, method, params);
|
emit commandReceived(targetNamespace, method, params);
|
||||||
|
|
||||||
if (targetNamespace == "Devices") {
|
if (targetNamespace == "Devices") {
|
||||||
if (method == "GetSupportedDevices") {
|
handleDevicesMessage(clientId, commandId, method, params);
|
||||||
QVariantMap params;
|
|
||||||
QVariantList supportedDeviceList;
|
|
||||||
foreach (const DeviceClass &deviceClass, HiveCore::instance()->deviceManager()->supportedDevices()) {
|
|
||||||
supportedDeviceList.append(packDeviceClass(deviceClass));
|
|
||||||
}
|
|
||||||
params.insert("deviceClasses", supportedDeviceList);
|
|
||||||
sendResponse(clientId, commandId, params);
|
|
||||||
} else if (method == "AddConfiguredDevice") {
|
|
||||||
QUuid deviceClass = params.value("deviceClass").toUuid();
|
|
||||||
QVariantMap deviceParams = params.value("params").toMap();
|
|
||||||
DeviceManager::DeviceError status = HiveCore::instance()->deviceManager()->addConfiguredDevice(deviceClass, deviceParams);
|
|
||||||
switch(status) {
|
|
||||||
case DeviceManager::DeviceErrorNoError:
|
|
||||||
sendResponse(clientId, commandId);
|
|
||||||
break;
|
|
||||||
case DeviceManager::DeviceErrorDeviceClassNotFound:
|
|
||||||
sendErrorResponse(clientId, commandId, "Error creating device. Device class not found.");
|
|
||||||
break;
|
|
||||||
case DeviceManager::DeviceErrorMissingParameter:
|
|
||||||
sendErrorResponse(clientId, commandId, "Error creating device. Missing parameter.");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
sendErrorResponse(clientId, commandId, "Unknown error.");
|
|
||||||
}
|
|
||||||
} else if (method == "GetConfiguredDevices") {
|
|
||||||
QVariantMap rspParams;
|
|
||||||
QVariantList configuredDeviceList;
|
|
||||||
foreach (Device *device, HiveCore::instance()->deviceManager()->configuredDevices()) {
|
|
||||||
configuredDeviceList.append(packDevice(device));
|
|
||||||
}
|
|
||||||
rspParams.insert("devices", configuredDeviceList);
|
|
||||||
sendResponse(clientId, commandId, rspParams);
|
|
||||||
} else {
|
|
||||||
sendErrorResponse(clientId, commandId, "No such method");
|
|
||||||
}
|
|
||||||
} else if (targetNamespace == "Rules") {
|
} else if (targetNamespace == "Rules") {
|
||||||
handleRulesMessage(clientId, commandId, method, params);
|
handleRulesMessage(clientId, commandId, method, params);
|
||||||
} else if (targetNamespace == "Actions") {
|
} else if (targetNamespace == "Actions") {
|
||||||
@ -98,6 +64,63 @@ void JsonRPCServer::processData(int clientId, const QByteArray &jsonData)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JsonRPCServer::handleDevicesMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms)
|
||||||
|
{
|
||||||
|
if (method == "GetSupportedDevices") {
|
||||||
|
QVariantMap params;
|
||||||
|
QVariantList supportedDeviceList;
|
||||||
|
foreach (const DeviceClass &deviceClass, HiveCore::instance()->deviceManager()->supportedDevices()) {
|
||||||
|
supportedDeviceList.append(packDeviceClass(deviceClass));
|
||||||
|
}
|
||||||
|
params.insert("deviceClasses", supportedDeviceList);
|
||||||
|
sendResponse(clientId, commandId, params);
|
||||||
|
} else if (method == "GetPlugins") {
|
||||||
|
QVariantMap params;
|
||||||
|
QVariantList plugins;
|
||||||
|
foreach (DevicePlugin *plugin, HiveCore::instance()->deviceManager()->plugins()) {
|
||||||
|
QVariantMap pluginMap;
|
||||||
|
pluginMap.insert("id", plugin->pluginId());
|
||||||
|
pluginMap.insert("name", plugin->pluginName());
|
||||||
|
pluginMap.insert("config", plugin->configuration());
|
||||||
|
plugins.append(pluginMap);
|
||||||
|
}
|
||||||
|
params.insert("plugins", plugins);
|
||||||
|
sendResponse(clientId, commandId, params);
|
||||||
|
} else if (method == "SetPluginConfig") {
|
||||||
|
QUuid pluginId = params.value("pluginId").toUuid();
|
||||||
|
QVariantMap pluginParams = params.value("params").toMap();
|
||||||
|
HiveCore::instance()->deviceManager()->plugin(pluginId)->setConfiguration(pluginParams);
|
||||||
|
sendResponse(clientId, commandId);
|
||||||
|
} else if (method == "AddConfiguredDevice") {
|
||||||
|
QUuid deviceClass = params.value("deviceClass").toUuid();
|
||||||
|
QVariantMap deviceParams = params.value("params").toMap();
|
||||||
|
DeviceManager::DeviceError status = HiveCore::instance()->deviceManager()->addConfiguredDevice(deviceClass, deviceParams);
|
||||||
|
switch(status) {
|
||||||
|
case DeviceManager::DeviceErrorNoError:
|
||||||
|
sendResponse(clientId, commandId);
|
||||||
|
break;
|
||||||
|
case DeviceManager::DeviceErrorDeviceClassNotFound:
|
||||||
|
sendErrorResponse(clientId, commandId, "Error creating device. Device class not found.");
|
||||||
|
break;
|
||||||
|
case DeviceManager::DeviceErrorMissingParameter:
|
||||||
|
sendErrorResponse(clientId, commandId, "Error creating device. Missing parameter.");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sendErrorResponse(clientId, commandId, "Unknown error.");
|
||||||
|
}
|
||||||
|
} else if (method == "GetConfiguredDevices") {
|
||||||
|
QVariantMap rspParams;
|
||||||
|
QVariantList configuredDeviceList;
|
||||||
|
foreach (Device *device, HiveCore::instance()->deviceManager()->configuredDevices()) {
|
||||||
|
configuredDeviceList.append(packDevice(device));
|
||||||
|
}
|
||||||
|
rspParams.insert("devices", configuredDeviceList);
|
||||||
|
sendResponse(clientId, commandId, rspParams);
|
||||||
|
} else {
|
||||||
|
sendErrorResponse(clientId, commandId, "No such method");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void JsonRPCServer::handleRulesMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms)
|
void JsonRPCServer::handleRulesMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
if (method == "GetRules") {
|
if (method == "GetRules") {
|
||||||
|
|||||||
@ -25,6 +25,7 @@ private slots:
|
|||||||
void processData(int clientId, const QByteArray &jsonData);
|
void processData(int clientId, const QByteArray &jsonData);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void handleDevicesMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms);
|
||||||
void handleRulesMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms);
|
void handleRulesMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms);
|
||||||
void handleActionMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms);
|
void handleActionMessage(int clientId, int commandId, const QString &method, const QVariantMap ¶ms);
|
||||||
|
|
||||||
|
|||||||
@ -52,6 +52,8 @@ QList<Action> RuleEngine::evaluateTrigger(const Trigger &trigger)
|
|||||||
RuleEngine::RuleError RuleEngine::addRule(const Trigger &trigger, const Action &action)
|
RuleEngine::RuleError RuleEngine::addRule(const Trigger &trigger, const Action &action)
|
||||||
{
|
{
|
||||||
qDebug() << "adding rule: Trigger:" << trigger.triggerTypeId() << "deviceid:" << action.deviceId();
|
qDebug() << "adding rule: Trigger:" << trigger.triggerTypeId() << "deviceid:" << action.deviceId();
|
||||||
|
DeviceClass triggerDeviceClass = HiveCore::instance()->deviceManager()->findDeviceClassforTrigger(trigger.triggerTypeId());
|
||||||
|
|
||||||
Device *device = HiveCore::instance()->deviceManager()->findConfiguredDevice(trigger.deviceId());
|
Device *device = HiveCore::instance()->deviceManager()->findConfiguredDevice(trigger.deviceId());
|
||||||
if (!device) {
|
if (!device) {
|
||||||
qWarning() << "Cannot create rule. No configured device for triggerTypeId" << trigger.triggerTypeId();
|
qWarning() << "Cannot create rule. No configured device for triggerTypeId" << trigger.triggerTypeId();
|
||||||
|
|||||||
7
tests/getplugins.sh
Executable file
7
tests/getplugins.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ -z $1 ]; then
|
||||||
|
echo "usage $0 host"
|
||||||
|
else
|
||||||
|
(echo '{"id":1, "method":"Devices.GetPlugins"}'; sleep 1) | nc $1 1234
|
||||||
|
fi
|
||||||
7
tests/setpluginconfig.sh
Executable file
7
tests/setpluginconfig.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ -z $1 ]; then
|
||||||
|
echo "usage $0 host"
|
||||||
|
else
|
||||||
|
(echo '{"id":1, "method":"Devices.SetPluginConfig", "params":{"pluginId":"'$2'", "params":{"foo":"bar"}}}'; sleep 1) | nc $1 1234
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user