diff --git a/debian/guh-plugins.install b/debian/guh-plugins.install index e321b7ee..b8576439 100644 --- a/debian/guh-plugins.install +++ b/debian/guh-plugins.install @@ -16,3 +16,4 @@ usr/lib/guh/plugins/libguh_deviceplugincommandlauncher.so usr/lib/guh/plugins/libguh_devicepluginunitec.so usr/lib/guh/plugins/libguh_devicepluginleynew.so usr/lib/guh/plugins/libguh_deviceplugintune.so +usr/lib/guh/plugins/libguh_devicepluginudpcommander.so diff --git a/plugins/deviceplugins/deviceplugins.pro b/plugins/deviceplugins/deviceplugins.pro index 94dae701..c9704ca9 100644 --- a/plugins/deviceplugins/deviceplugins.pro +++ b/plugins/deviceplugins/deviceplugins.pro @@ -16,6 +16,7 @@ SUBDIRS += elro \ unitec \ leynew \ tune \ + udpcommander \ diff --git a/plugins/deviceplugins/udpcommander/devicepluginudpcommander.cpp b/plugins/deviceplugins/udpcommander/devicepluginudpcommander.cpp new file mode 100644 index 00000000..f2dd05d6 --- /dev/null +++ b/plugins/deviceplugins/udpcommander/devicepluginudpcommander.cpp @@ -0,0 +1,113 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2015 Simon Stuerz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/*! + \page udpcommander.html + \title UDP Commander + + \ingroup plugins + \ingroup network + + This plugin allows to receive UDP packages over a certain UDP port and generates an \l{Event} if the command matches the \l{Param} command. + This plugin is ment to be cominend with a \l{Rule}. + + \chapter Plugin properties + Following JSON file contains the definition and the description of all available \l{DeviceClass}{DeviceClasses} + and \l{Vendor}{Vendors} of this \l{DevicePlugin}. + + Each \l{DeviceClass} has a list of \l{ParamType}{paramTypes}, \l{ActionType}{actionTypes}, \l{StateType}{stateTypes} + and \l{EventType}{eventTypes}. The \l{DeviceClass::CreateMethod}{createMethods} parameter describes how the \l{Device} + will be created in the system. A device can have more than one \l{DeviceClass::CreateMethod}{CreateMethod}. + The \l{DeviceClass::SetupMethod}{setupMethod} describes the setup method of the \l{Device}. + The detailed implementation of each \l{DeviceClass} can be found in the source code. + + \note If a \l{StateType} has the parameter \tt{"writable": true}, an \l{ActionType} with the same uuid and \l{ParamType}{ParamTypes} + will be created automatically. + + \quotefile plugins/deviceplugins/udpcommander/devicepluginudpcommander.json +*/ + +#include "devicepluginudpcommander.h" +#include "plugin/device.h" +#include "plugininfo.h" + +DevicePluginUdpCommander::DevicePluginUdpCommander() +{ + +} + +DeviceManager::HardwareResources DevicePluginUdpCommander::requiredHardware() const +{ + return DeviceManager::HardwareResourceNone; +} + +DeviceManager::DeviceSetupStatus DevicePluginUdpCommander::setupDevice(Device *device) +{ + // check port + bool portOk = false; + int port = device->paramValue("port").toInt(&portOk); + if (!portOk) { + qWarning() << "ERROR: UDP commander" << device->paramValue("name") << ": invalid port:" << device->paramValue("port").toString() << "."; + return DeviceManager::DeviceSetupStatusFailure; + } + + QUdpSocket *udpSocket = new QUdpSocket(this); + + if (!udpSocket->bind(QHostAddress::Any, port)) { + qWarning() << "ERROR: UDP commander" << device->paramValue("name") << "can't bind to port" << port << "."; + delete udpSocket; + return DeviceManager::DeviceSetupStatusFailure; + } + + connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); + m_commanderList.insert(udpSocket, device); + + return DeviceManager::DeviceSetupStatusSuccess; +} + +void DevicePluginUdpCommander::deviceRemoved(Device *device) +{ + QUdpSocket *socket = m_commanderList.key(device); + m_commanderList.remove(socket); + + socket->close(); + socket->deleteLater(); +} + +void DevicePluginUdpCommander::readPendingDatagrams() +{ + QUdpSocket *socket= qobject_cast(sender()); + Device *device = m_commanderList.value(socket); + + QByteArray datagram; + QHostAddress sender; + quint16 senderPort; + + while (socket->hasPendingDatagrams()) { + datagram.resize(socket->pendingDatagramSize()); + socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); + } + + if (datagram == device->paramValue("command").toByteArray() || + datagram == device->paramValue("command").toByteArray() + "\n") { + qDebug() << device->paramValue("name").toString() << " got command from" << sender.toString() << senderPort; + emit emitEvent(Event(commandReceivedEventTypeId, device->id())); + } +} diff --git a/plugins/deviceplugins/udpcommander/devicepluginudpcommander.h b/plugins/deviceplugins/udpcommander/devicepluginudpcommander.h new file mode 100644 index 00000000..3d057ede --- /dev/null +++ b/plugins/deviceplugins/udpcommander/devicepluginudpcommander.h @@ -0,0 +1,51 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2015 Simon Stuerz * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef DEVICEPLUGINUDPCOMMANDER_H +#define DEVICEPLUGINUDPCOMMANDER_H + +#include "plugin/deviceplugin.h" + +#include +#include +#include + +class DevicePluginUdpCommander : public DevicePlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "guru.guh.DevicePlugin" FILE "devicepluginudpcommander.json") + Q_INTERFACES(DevicePlugin) + +public: + explicit DevicePluginUdpCommander(); + + DeviceManager::HardwareResources requiredHardware() const override; + DeviceManager::DeviceSetupStatus setupDevice(Device *device) override; + void deviceRemoved(Device *device) override; + +private: + QHash m_commanderList; + +private slots: + void readPendingDatagrams(); + +}; + +#endif // DEVICEPLUGINUDPCOMMANDER_H diff --git a/plugins/deviceplugins/udpcommander/devicepluginudpcommander.json b/plugins/deviceplugins/udpcommander/devicepluginudpcommander.json new file mode 100644 index 00000000..79458a6a --- /dev/null +++ b/plugins/deviceplugins/udpcommander/devicepluginudpcommander.json @@ -0,0 +1,40 @@ +{ + "name": "UDP Commander", + "id": "24a8474c-1d86-499e-a76e-9cbfbf48dd72", + "vendors": [ + { + "name": "guh", + "id": "2062d64d-3232-433c-88bc-0d33c0ba2ba6", + "deviceClasses": [ + { + "deviceClassId": "6ecd5a8d-595a-4520-85e3-dcc9679edf66", + "name": "UDP Commander", + "createMethods": ["user"], + "paramTypes": [ + { + "name": "name", + "type": "QString", + "inputType": "TextLine" + }, + { + "name": "port", + "type": "int" + }, + { + "name": "command", + "type": "QString", + "inputType": "TextLine" + } + ], + "eventTypes": [ + { + "id": "5fecbba3-ffbb-456b-872c-a2f571c681cb", + "idName": "commandReceived", + "name": "command received" + } + ] + } + ] + } + ] +} diff --git a/plugins/deviceplugins/udpcommander/udpcommander.pro b/plugins/deviceplugins/udpcommander/udpcommander.pro new file mode 100644 index 00000000..da402bd7 --- /dev/null +++ b/plugins/deviceplugins/udpcommander/udpcommander.pro @@ -0,0 +1,11 @@ +include(../../plugins.pri) + +TARGET = $$qtLibraryTarget(guh_devicepluginudpcommander) + +SOURCES += \ + devicepluginudpcommander.cpp + +HEADERS += \ + devicepluginudpcommander.h + +