add udp commander plugin

pull/135/head
Simon Stürz 2015-06-12 16:31:22 +02:00 committed by Michael Zanetti
parent 4d6ccdfd1c
commit 8bcb535ad9
6 changed files with 217 additions and 0 deletions

View File

@ -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

View File

@ -16,6 +16,7 @@ SUBDIRS += elro \
unitec \
leynew \
tune \
udpcommander \

View File

@ -0,0 +1,113 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*!
\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<QUdpSocket*>(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()));
}
}

View File

@ -0,0 +1,51 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef DEVICEPLUGINUDPCOMMANDER_H
#define DEVICEPLUGINUDPCOMMANDER_H
#include "plugin/deviceplugin.h"
#include <QHash>
#include <QDebug>
#include <QUdpSocket>
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<QUdpSocket *, Device *> m_commanderList;
private slots:
void readPendingDatagrams();
};
#endif // DEVICEPLUGINUDPCOMMANDER_H

View File

@ -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"
}
]
}
]
}
]
}

View File

@ -0,0 +1,11 @@
include(../../plugins.pri)
TARGET = $$qtLibraryTarget(guh_devicepluginudpcommander)
SOURCES += \
devicepluginudpcommander.cpp
HEADERS += \
devicepluginudpcommander.h