make executeAction more verbose
This commit is contained in:
parent
e4c007070f
commit
7c3ffbd5d6
@ -330,7 +330,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}.*/
|
||||
DeviceManager::DeviceError DeviceManager::executeAction(const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DeviceManager::executeAction(const Action &action)
|
||||
{
|
||||
foreach (Device *device, m_configuredDevices) {
|
||||
if (action.deviceId() == device->id()) {
|
||||
@ -345,21 +345,21 @@ DeviceManager::DeviceError DeviceManager::executeAction(const Action &action)
|
||||
|
||||
qDebug() << "checking params" << actionType.parameters().count() << action.params().count();
|
||||
qDebug() << "action params:" << action.params();
|
||||
if (actionType.parameters().count() > action.params().count()) {
|
||||
return DeviceErrorMissingParameter;
|
||||
QPair<bool, QString> paramCheck = verifyParams(actionType.parameters(), action.params());
|
||||
if (!paramCheck.first) {
|
||||
return qMakePair<DeviceError, QString>(DeviceErrorMissingParameter, paramCheck.second);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return DeviceErrorActionTypeNotFound;
|
||||
return qMakePair<DeviceError, QString>(DeviceErrorActionTypeNotFound, action.actionTypeId().toString());
|
||||
}
|
||||
|
||||
return m_devicePlugins.value(device->pluginId())->executeAction(device, action);
|
||||
}
|
||||
}
|
||||
return DeviceErrorDeviceNotFound;
|
||||
return qMakePair<DeviceError, QString>(DeviceErrorDeviceNotFound, action.deviceId().toString());
|
||||
}
|
||||
|
||||
void DeviceManager::loadPlugins()
|
||||
@ -562,3 +562,23 @@ bool DeviceManager::setupDevice(Device *device)
|
||||
connect(device, SIGNAL(stateValueChanged(QUuid,QVariant)), this, SLOT(slotDeviceStateValueChanged(QUuid,QVariant)));
|
||||
return true;
|
||||
}
|
||||
|
||||
QPair<bool, QString> DeviceManager::verifyParams(const QList<ParamType> paramTypes, const QList<Param> params)
|
||||
{
|
||||
foreach (const ParamType ¶mType, paramTypes) {
|
||||
qDebug() << "checking paramType" << paramType.name();
|
||||
bool found = false;
|
||||
foreach (const Param ¶m, params) {
|
||||
qDebug() << "checking param" << param.name();
|
||||
if (param.name() == paramType.name()) {
|
||||
if (param.value().canConvert(paramType.type())) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return qMakePair<bool, QString>(false, paramType.name());
|
||||
}
|
||||
}
|
||||
return qMakePair<bool, QString>(true, QString());
|
||||
}
|
||||
|
||||
@ -87,7 +87,7 @@ signals:
|
||||
void devicesDiscovered(const DeviceClassId &deviceClassId, const QList<DeviceDescriptor> &devices);
|
||||
|
||||
public slots:
|
||||
DeviceError executeAction(const Action &action);
|
||||
QPair<DeviceError, QString> executeAction(const Action &action);
|
||||
|
||||
private slots:
|
||||
void loadPlugins();
|
||||
@ -105,6 +105,9 @@ private slots:
|
||||
private:
|
||||
DeviceError addConfiguredDeviceInternal(const DeviceClassId &deviceClassId, const QList<Param> ¶ms, const DeviceId id = DeviceId::createDeviceId());
|
||||
bool setupDevice(Device *device);
|
||||
QPair<bool, QString> verifyParams(const QList<ParamType> paramTypes, const QList<Param> params);
|
||||
|
||||
private:
|
||||
|
||||
QHash<VendorId, Vendor> m_supportedVendors;
|
||||
QHash<VendorId, QList<DeviceClassId> > m_vendorDeviceMap;
|
||||
|
||||
@ -66,6 +66,15 @@ pure virtual methods: \l{DevicePlugin::pluginName()}, \l{DevicePlugin::pluginId(
|
||||
\fn void DevicePlugin::executeAction(Device *device, const Action &action)
|
||||
This will be called to actually execute actions on the hardware. The \{Device} and
|
||||
the \{Action} are contained in the \a device and \a action parameters.
|
||||
Use \l{DevicePlugin::report()} to report the result. If everything worked out,
|
||||
just return report(). Otherwise fill in the error code and a short message
|
||||
describing the offending part. E.g:
|
||||
If the action couldn't be executed because the device can't be reached (e.g. it is unplugged)
|
||||
then report the appropriate error code and give the device id as message:
|
||||
return report(DeviceManager::DeviceErrorSetupFailed, device->id());
|
||||
Keep the message short, the DeviceManager will format it for you.
|
||||
|
||||
\sa DevicePlugin::report()
|
||||
*/
|
||||
|
||||
/*!
|
||||
@ -251,3 +260,14 @@ void DevicePlugin::transmitData(QList<int> rawData)
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Constructs a status report to be returned. By default (when called without
|
||||
arguments) this will report \l{DeviceManager::DeviceErrorNoError} and an
|
||||
empty message.
|
||||
Keep the message short, the DeviceManager will format it for you.
|
||||
*/
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePlugin::report(DeviceManager::DeviceError error, const QString &message)
|
||||
{
|
||||
return qMakePair<DeviceManager::DeviceError, QString>(error, message);
|
||||
}
|
||||
|
||||
|
||||
@ -62,9 +62,9 @@ public:
|
||||
virtual void setConfiguration(const QVariantMap &configuration);
|
||||
|
||||
public slots:
|
||||
virtual DeviceManager::DeviceError executeAction(Device *device, const Action &action) {
|
||||
virtual QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) {
|
||||
Q_UNUSED(device) Q_UNUSED(action)
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return qMakePair<DeviceManager::DeviceError, QString>(DeviceManager::DeviceErrorNoError, "");
|
||||
}
|
||||
|
||||
signals:
|
||||
@ -78,6 +78,7 @@ protected:
|
||||
|
||||
void transmitData(QList<int> rawData);
|
||||
|
||||
QPair<DeviceManager::DeviceError, QString> report(DeviceManager::DeviceError error = DeviceManager::DeviceErrorNoError, const QString &message = QString());
|
||||
private:
|
||||
void initPlugin(DeviceManager *deviceManager);
|
||||
|
||||
|
||||
@ -133,21 +133,21 @@ void DevicePluginBoblight::setConfiguration(const QVariantMap &configuration)
|
||||
connectToBoblight();
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginBoblight::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginBoblight::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
if (!m_bobClient->connected()) {
|
||||
return DeviceManager::DeviceErrorSetupFailed;
|
||||
return report(DeviceManager::DeviceErrorSetupFailed, device->id().toString());
|
||||
}
|
||||
QColor newColor = action.param("color").value().value<QColor>();
|
||||
if (!newColor.isValid()) {
|
||||
return DeviceManager::DeviceErrorActionParameterError;
|
||||
return report(DeviceManager::DeviceErrorActionParameterError, "color");
|
||||
}
|
||||
qDebug() << "executing boblight action" << newColor;
|
||||
m_bobClient->setColor(device->paramValue("channel").toInt(), newColor);
|
||||
m_bobClient->sync();
|
||||
|
||||
device->setStateValue(colorStateTypeId, newColor);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
}
|
||||
|
||||
void DevicePluginBoblight::connectToBoblight()
|
||||
|
||||
@ -48,7 +48,7 @@ public:
|
||||
void setConfiguration(const QVariantMap &configuration) override;
|
||||
|
||||
public slots:
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action);
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action);
|
||||
|
||||
private slots:
|
||||
void connectToBoblight();
|
||||
|
||||
@ -141,13 +141,13 @@ PluginId DevicePluginConrad::pluginId() const
|
||||
return PluginId("1fd1a076-f229-4ec6-b501-48ddd15935e4");
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginConrad::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginConrad::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
|
||||
QList<int> rawData;
|
||||
QByteArray binCode;
|
||||
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
}
|
||||
|
||||
void DevicePluginConrad::radioData(QList<int> rawData)
|
||||
|
||||
@ -41,7 +41,7 @@ public:
|
||||
void radioData(QList<int> rawData) override;
|
||||
|
||||
public slots:
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -218,7 +218,7 @@ PluginId DevicePluginElro::pluginId() const
|
||||
return PluginId("2b267f81-d9ae-4f4f-89a0-7386b547cfd3");
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginElro::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginElro::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
|
||||
QList<int> rawData;
|
||||
@ -311,7 +311,7 @@ DeviceManager::DeviceError DevicePluginElro::executeAction(Device *device, const
|
||||
//qDebug() << "rawData" << rawData;
|
||||
qDebug() << "transmit" << pluginName() << action.param("power").value().toBool();
|
||||
transmitData(rawData);
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
}
|
||||
|
||||
void DevicePluginElro::radioData(QList<int> rawData)
|
||||
|
||||
@ -41,7 +41,7 @@ public:
|
||||
void radioData(QList<int> rawData) override;
|
||||
|
||||
public slots:
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -218,19 +218,19 @@ DeviceManager::HardwareResources DevicePluginGoogleMail::requiredHardware() cons
|
||||
return DeviceManager::HardwareResourceNone;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginGoogleMail::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginGoogleMail::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
qDebug() << "execute action " << sendMailActionTypeId.toString();
|
||||
if(action.actionTypeId() == sendMailActionTypeId){
|
||||
if(!m_smtpClient->login(device->paramValue("user").toString(), device->paramValue("password").toString())){
|
||||
qDebug() << "ERROR: could nt login for sending mail";
|
||||
return DeviceManager::DeviceErrorDeviceParameterError;
|
||||
return report(DeviceManager::DeviceErrorDeviceParameterError, "user, password");
|
||||
}
|
||||
m_smtpClient->sendMail(device->paramValue("user").toString(), device->paramValue("sendTo").toString(), action.param("subject").value().toString(), action.param("body").value().toString());
|
||||
m_smtpClient->logout();
|
||||
}
|
||||
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
}
|
||||
|
||||
QString DevicePluginGoogleMail::pluginName() const
|
||||
|
||||
@ -38,7 +38,7 @@ public:
|
||||
|
||||
bool deviceCreated(Device *device) override;
|
||||
DeviceManager::HardwareResources requiredHardware() const override;
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
QString pluginName() const override;
|
||||
PluginId pluginId() const override;
|
||||
|
||||
@ -348,7 +348,7 @@ PluginId DevicePluginIntertechno::pluginId() const
|
||||
return PluginId("e998d934-0397-42c1-ad63-9141bcac8563");
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginIntertechno::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginIntertechno::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
|
||||
QList<int> rawData;
|
||||
@ -391,7 +391,7 @@ DeviceManager::DeviceError DevicePluginIntertechno::executeAction(Device *device
|
||||
}else if(familyCode == "P"){
|
||||
binCode.append("01010101");
|
||||
}else{
|
||||
return DeviceManager::DeviceErrorDeviceParameterError;
|
||||
return report();
|
||||
}
|
||||
|
||||
QString buttonCode = device->paramValue("buttonCode").toString();
|
||||
@ -431,7 +431,7 @@ DeviceManager::DeviceError DevicePluginIntertechno::executeAction(Device *device
|
||||
}else if(familyCode == "16"){
|
||||
binCode.append("01010101");
|
||||
}else{
|
||||
return DeviceManager::DeviceErrorDeviceParameterError;
|
||||
return report();
|
||||
}
|
||||
|
||||
// =======================================
|
||||
@ -470,7 +470,7 @@ DeviceManager::DeviceError DevicePluginIntertechno::executeAction(Device *device
|
||||
qDebug() << "transmit" << pluginName() << familyCode << buttonCode << action.param("power").value().toBool();
|
||||
transmitData(rawData);
|
||||
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -41,7 +41,7 @@ public:
|
||||
void radioData(QList<int> rawData) override;
|
||||
|
||||
public slots:
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -180,11 +180,11 @@ bool DevicePluginMock::configureAutoDevice(QList<Device *> loadedDevices, Device
|
||||
return true;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginMock::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
qDebug() << "Should execute action" << action.actionTypeId();
|
||||
m_daemons.value(device)->actionExecuted(action.actionTypeId());
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
}
|
||||
|
||||
void DevicePluginMock::setState(const StateTypeId &stateTypeId, const QVariant &value)
|
||||
|
||||
@ -48,7 +48,7 @@ public:
|
||||
bool configureAutoDevice(QList<Device *> loadedDevices, Device *device) const override;
|
||||
|
||||
public slots:
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
private slots:
|
||||
void setState(const StateTypeId &stateTypeId, const QVariant &value);
|
||||
|
||||
@ -442,13 +442,13 @@ DeviceManager::HardwareResources DevicePluginOpenweathermap::requiredHardware()
|
||||
return DeviceManager::HardwareResourceTimer;
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginOpenweathermap::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginOpenweathermap::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
qDebug() << "execute action " << updateWeatherActionTypeId.toString();
|
||||
if(action.actionTypeId() == updateWeatherActionTypeId){
|
||||
m_openweaher->update(device->paramValue("id").toString());
|
||||
}
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
}
|
||||
|
||||
QString DevicePluginOpenweathermap::pluginName() const
|
||||
|
||||
@ -41,7 +41,7 @@ public:
|
||||
DeviceManager::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const QVariantMap ¶ms) const override;
|
||||
bool deviceCreated(Device *device) override;
|
||||
DeviceManager::HardwareResources requiredHardware() const override;
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
QString pluginName() const override;
|
||||
PluginId pluginId() const override;
|
||||
|
||||
@ -180,13 +180,13 @@ PluginId DevicePluginWakeOnLan::pluginId() const
|
||||
return PluginId("b5a87848-de56-451e-84a6-edd26ad4958f");
|
||||
}
|
||||
|
||||
DeviceManager::DeviceError DevicePluginWakeOnLan::executeAction(Device *device, const Action &action)
|
||||
QPair<DeviceManager::DeviceError, QString> DevicePluginWakeOnLan::executeAction(Device *device, const Action &action)
|
||||
{
|
||||
qDebug() << "execute action " << action.actionTypeId().toString();
|
||||
if(action.actionTypeId() == wolActionTypeId){
|
||||
wakeup(device->paramValue("mac").toString());
|
||||
}
|
||||
return DeviceManager::DeviceErrorNoError;
|
||||
return report();
|
||||
}
|
||||
|
||||
void DevicePluginWakeOnLan::wakeup(QString mac)
|
||||
|
||||
@ -39,7 +39,7 @@ public:
|
||||
|
||||
QString pluginName() const override;
|
||||
PluginId pluginId() const override;
|
||||
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||
QPair<DeviceManager::DeviceError, QString> executeAction(Device *device, const Action &action) override;
|
||||
|
||||
|
||||
private slots:
|
||||
|
||||
@ -95,18 +95,18 @@ void GuhCore::gotEvent(const Event &event)
|
||||
{
|
||||
foreach (const Action &action, m_ruleEngine->evaluateEvent(event)) {
|
||||
qDebug() << "executing action" << action.actionTypeId();
|
||||
DeviceManager::DeviceError status = m_deviceManager->executeAction(action);
|
||||
switch(status) {
|
||||
QPair<DeviceManager::DeviceError, QString> status = m_deviceManager->executeAction(action);
|
||||
switch(status.first) {
|
||||
case DeviceManager::DeviceErrorNoError:
|
||||
break;
|
||||
case DeviceManager::DeviceErrorSetupFailed:
|
||||
qDebug() << "Error executing action. Device setup failed.";
|
||||
qDebug() << "Error executing action. Device setup failed:" << status.second;
|
||||
break;
|
||||
case DeviceManager::DeviceErrorActionParameterError:
|
||||
qDebug() << "Error executing action. Invalid action parameter.";
|
||||
qDebug() << "Error executing action. Invalid action parameter:" << status.second;
|
||||
break;
|
||||
default:
|
||||
qDebug() << "Error executing action:" << status;
|
||||
qDebug() << "Error executing action:" << status.first << status.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,27 +57,27 @@ JsonReply* ActionHandler::ExecuteAction(const QVariantMap ¶ms)
|
||||
|
||||
|
||||
QVariantMap returns;
|
||||
DeviceManager::DeviceError error = GuhCore::instance()->deviceManager()->executeAction(action);
|
||||
QPair<DeviceManager::DeviceError, QString> error = GuhCore::instance()->deviceManager()->executeAction(action);
|
||||
|
||||
switch (error) {
|
||||
switch (error.first) {
|
||||
case DeviceManager::DeviceErrorNoError:
|
||||
returns.insert("success", true);
|
||||
returns.insert("errorMessage", "");
|
||||
break;
|
||||
case DeviceManager::DeviceErrorDeviceNotFound:
|
||||
returns.insert("errorMessage", "No such device");
|
||||
returns.insert("errorMessage", QString("No such device: %1").arg(error.second));
|
||||
returns.insert("success", false);
|
||||
break;
|
||||
case DeviceManager::DeviceErrorActionTypeNotFound:
|
||||
returns.insert("errorMessage", "ActionType not found");
|
||||
returns.insert("errorMessage", QString("ActionType not found: %1").arg(error.second));
|
||||
returns.insert("success", false);
|
||||
break;
|
||||
case DeviceManager::DeviceErrorMissingParameter:
|
||||
returns.insert("errorMessage", "Missing parameter");
|
||||
returns.insert("errorMessage", QString("Missing parameter: %1").arg(error.second));
|
||||
returns.insert("success", false);
|
||||
break;
|
||||
default:
|
||||
returns.insert("errorMessage", QString("Unknown error %1").arg(error));
|
||||
returns.insert("errorMessage", QString("Unknown error %1 %2").arg(error.first).arg(error.second));
|
||||
returns.insert("success", false);
|
||||
}
|
||||
|
||||
|
||||
@ -383,13 +383,15 @@ QVariantMap JsonTypes::packRule(const Rule &rule)
|
||||
|
||||
Param JsonTypes::unpackParam(const QVariantMap ¶mMap)
|
||||
{
|
||||
return Param(paramMap.value("name").toString(), paramMap.value("value"));
|
||||
QString key = paramMap.keys().first();
|
||||
return Param(key, paramMap.value(key));
|
||||
}
|
||||
|
||||
QList<Param> JsonTypes::unpackParams(const QVariantList ¶mList)
|
||||
{
|
||||
QList<Param> params;
|
||||
foreach (const QVariant ¶mVariant, paramList) {
|
||||
qDebug() << "unpacking param" << paramVariant;
|
||||
params.append(unpackParam(paramVariant.toMap()));
|
||||
}
|
||||
return params;
|
||||
|
||||
@ -7,6 +7,7 @@ elif [ -z $4 ]; then
|
||||
elif [ -z $5 ]; then
|
||||
echo "usage: $0 host actionTypeId deviceId [paramname paramvalue] [paramname paramvalue]"
|
||||
elif [ -z $6 ]; then
|
||||
echo calling.. $4 $5
|
||||
(echo '{"id":1, "method":"Actions.ExecuteAction","params":{"actionTypeId": "{'$2'}", "deviceId":"{'$3'}","params":[{"'$4'":"'$5'"}]}}'; sleep 1) | nc $1 1234
|
||||
elif [ -z $7 ]; then
|
||||
echo "usage: $0 host actionTypeId deviceId [paramname paramvalue] [paramname paramvalue]"
|
||||
|
||||
Reference in New Issue
Block a user