Update udp plugin

This commit is contained in:
Simon Stürz 2018-03-13 18:59:20 +01:00 committed by Michael Zanetti
parent 142fe864b8
commit 8244dd439d
3 changed files with 69 additions and 68 deletions

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io> * * Copyright (C) 2015-2018 Simon Stürz <simon.stuerz@guh.io> *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> * * Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
* * * *
* This file is part of nymea. * * This file is part of nymea. *
@ -70,42 +70,39 @@ DevicePluginUdpCommander::DevicePluginUdpCommander()
DeviceManager::DeviceSetupStatus DevicePluginUdpCommander::setupDevice(Device *device) DeviceManager::DeviceSetupStatus DevicePluginUdpCommander::setupDevice(Device *device)
{ {
// check port qCDebug(dcUdpCommander()) << "Setup device" << device->name() << device->params();
// TODO ports used by an input should be available for outputs and vice versa
if ((device->deviceClassId() == udpInputDeviceClassId) || (device->deviceClassId() == udpOutputDeviceClassId)) {
bool portOk = false;
int port = device->paramValue(portParamTypeId).toInt(&portOk);
if (!portOk || port <= 0 || port > 65535) {
qCWarning(dcUdpCommander) << device->name() << ": invalid port:" << device->paramValue(portParamTypeId).toString() << ".";
return DeviceManager::DeviceSetupStatusFailure;
}
if (device->deviceClassId() == udpReceiverDeviceClassId) {
QUdpSocket *udpSocket = new QUdpSocket(this); QUdpSocket *udpSocket = new QUdpSocket(this);
int port = device->paramValue(udpReceiverPortParamTypeId).toInt();
if (!udpSocket->bind(QHostAddress::Any, port)) { if (!udpSocket->bind(QHostAddress::Any, port, QUdpSocket::ShareAddress)) {
qCWarning(dcUdpCommander) << device->name() << "can't bind to port" << port << "."; qCWarning(dcUdpCommander()) << device->name() << "cannot bind to port" << port;
delete udpSocket; delete udpSocket;
return DeviceManager::DeviceSetupStatusFailure; return DeviceManager::DeviceSetupStatusFailure;
} }
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
m_commanderList.insert(udpSocket, device); m_receiverList.insert(udpSocket, device);
return DeviceManager::DeviceSetupStatusSuccess;
} else if (device->deviceClassId() == udpCommanderDeviceClassId) {
QUdpSocket *udpSocket = new QUdpSocket(this);
m_commanderList.insert(udpSocket, device);
return DeviceManager::DeviceSetupStatusSuccess; return DeviceManager::DeviceSetupStatusSuccess;
} }
return DeviceManager::DeviceSetupStatusFailure; return DeviceManager::DeviceSetupStatusFailure;
} }
DeviceManager::DeviceError DevicePluginUdpCommander::executeAction(Device *device, const Action &action) { DeviceManager::DeviceError DevicePluginUdpCommander::executeAction(Device *device, const Action &action) {
if (device->deviceClassId() == udpOutputDeviceClassId) { if (device->deviceClassId() == udpCommanderDeviceClassId) {
if (action.actionTypeId() == udpCommanderOutputDataActionTypeId) {
if (action.actionTypeId() == outputDataActionTypeId) {
QUdpSocket *udpSocket = m_commanderList.key(device); QUdpSocket *udpSocket = m_commanderList.key(device);
int port = device->paramValue(portParamTypeId).toInt(); int port = device->paramValue(udpCommanderPortParamTypeId).toInt();
QHostAddress address = QHostAddress(device->paramValue(ipv4AddressParamTypeId).toString()); QHostAddress address = QHostAddress(device->paramValue(udpCommanderAddressParamTypeId).toString());
QByteArray data = action.param(outputDataAreaParamTypeId).value().toByteArray(); QByteArray data = action.param(udpCommanderDataParamTypeId).value().toByteArray();
qDebug(dcUdpCommander()) << "Send UDP datagram:" << data << "address:" << address.toIPv4Address() << "port:" << port; qDebug(dcUdpCommander()) << "Send UDP datagram:" << data << "address:" << address.toIPv4Address() << "port:" << port;
udpSocket->writeDatagram(data, address, port); udpSocket->writeDatagram(data, address, port);
@ -118,11 +115,15 @@ DeviceManager::DeviceError DevicePluginUdpCommander::executeAction(Device *devic
void DevicePluginUdpCommander::deviceRemoved(Device *device) void DevicePluginUdpCommander::deviceRemoved(Device *device)
{ {
if ((device->deviceClassId() == udpInputDeviceClassId) || (device->deviceClassId() == udpOutputDeviceClassId)){ if (device->deviceClassId() == udpReceiverDeviceClassId) {
QUdpSocket *socket = m_receiverList.key(device);
m_receiverList.remove(socket);
socket->close();
socket->deleteLater();
} else if (device->deviceClassId() == udpCommanderDeviceClassId) {
QUdpSocket *socket = m_commanderList.key(device); QUdpSocket *socket = m_commanderList.key(device);
m_commanderList.remove(socket); m_commanderList.remove(socket);
socket->close(); socket->close();
socket->deleteLater(); socket->deleteLater();
} }
@ -130,8 +131,11 @@ void DevicePluginUdpCommander::deviceRemoved(Device *device)
void DevicePluginUdpCommander::readPendingDatagrams() void DevicePluginUdpCommander::readPendingDatagrams()
{ {
QUdpSocket *socket= qobject_cast<QUdpSocket*>(sender()); QUdpSocket *socket= static_cast<QUdpSocket *>(sender());
Device *device = m_commanderList.value(socket); Device *device = m_receiverList.value(socket);
if (!device)
return;
QByteArray datagram; QByteArray datagram;
QHostAddress sender; QHostAddress sender;
@ -142,13 +146,14 @@ void DevicePluginUdpCommander::readPendingDatagrams()
socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
} }
device->setStateValue(inputDataStateTypeId, datagram); device->setStateValue(udpReceiverInputDataStateTypeId, datagram);
//TO CHECK remove command param and event, comparison may be done in the rule engine if (datagram == device->paramValue(udpReceiverCommandParamTypeId).toByteArray() ||
if (datagram == device->paramValue(commandParamTypeId).toByteArray() || datagram == device->paramValue(udpReceiverCommandParamTypeId).toByteArray() + "\n") {
datagram == device->paramValue(commandParamTypeId).toByteArray() + "\n") { qCDebug(dcUdpCommander()) << device->name() << "got command from" << sender.toString() << senderPort;
qCDebug(dcUdpCommander) << device->name() << " got command from" << sender.toString() << senderPort; emit emitEvent(Event(udpReceiverCommandReceivedEventTypeId, device->id()));
emit emitEvent(Event(commanderCommandReceivedEventTypeId, device->id()));
// Send response for verification
socket->writeDatagram("OK\n", sender, senderPort); socket->writeDatagram("OK\n", sender, senderPort);
} }
} }

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io> * * Copyright (C) 2015-2018 Simon Stürz <simon.stuerz@guh.io> *
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> * * Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
* * * *
* This file is part of nymea. * * This file is part of nymea. *
@ -43,7 +43,9 @@ public:
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override; DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
private: private:
QHash<QUdpSocket *, Device *> m_receiverList;
QHash<QUdpSocket *, Device *> m_commanderList; QHash<QUdpSocket *, Device *> m_commanderList;
private slots: private slots:

View File

@ -10,72 +10,69 @@
"deviceClasses": [ "deviceClasses": [
{ {
"id": "6ecd5a8d-595a-4520-85e3-dcc9679edf66", "id": "6ecd5a8d-595a-4520-85e3-dcc9679edf66",
"displayName": "UDP Commander", "displayName": "UDP Receiver",
"name": "commander", "name": "udpReceiver",
"deviceIcon": "Network", "deviceIcon": "Network",
"basicTags": [ "basicTags": ["Service", "Sensor"],
"Service",
"Sensor"
],
"createMethods": ["user"], "createMethods": ["user"],
"paramTypes": [ "paramTypes": [
{ {
"id": "1843adcb-e377-44d1-8d70-ab4f9eeb32ec", "id": "1843adcb-e377-44d1-8d70-ab4f9eeb32ec",
"name": "port", "name": "port",
"displayName": "port", "displayName": "Port",
"type": "int" "type": "int",
"minValue": 0,
"maxValue": 65535,
"defaultValue": 4242
}, },
{ {
"id": "d0f29961-1624-4b91-a0e8-9b1cc86c44c7", "id": "d0f29961-1624-4b91-a0e8-9b1cc86c44c7",
"name": "command", "name": "command",
"displayName": "command", "displayName": "Command",
"type": "QString", "type": "QString",
"inputType": "TextLine" "defaultValue": "Hello"
} }
], ],
"stateTypes":[ "stateTypes":[
{ {
"id": "065a1a0a-d324-4ae6-a461-bef8143e8795", "id": "065a1a0a-d324-4ae6-a461-bef8143e8795",
"idName": "inputData", "name": "inputData",
"name": "Input Data", "displayName": "Received data",
"displayNameEvent": "Received data changed",
"type": "QString", "type": "QString",
"defaultValue": "", "defaultValue": ""
"eventTypeName": "input data changed",
"index": 0
} }
], ],
"eventTypes": [ "eventTypes": [
{ {
"id": "5fecbba3-ffbb-456b-872c-a2f571c681cb", "id": "5fecbba3-ffbb-456b-872c-a2f571c681cb",
"name": "commandReceived", "name": "commandReceived",
"displayName": "command received" "displayName": "Command received"
} }
] ]
}, },
{ {
"id": "31b00639-8904-4522-84ed-54c46a54c63c", "id": "31b00639-8904-4522-84ed-54c46a54c63c",
"name": "UDP Output", "name": "udpCommander",
"idName": "udpOutput", "displayName": "UDP Commander",
"deviceIcon": "Network", "deviceIcon": "Network",
"basicTags": [ "basicTags": ["Service", "Sensor"],
"Service",
"Sensor"
],
"createMethods": ["user"], "createMethods": ["user"],
"paramTypes": [ "paramTypes": [
{ {
"id": "1843adcb-e377-44d1-8d70-ab4f9eeb32ec", "id": "eb204dfe-11f8-413f-8b68-8bffef3f9a7f",
"idName": "port",
"name": "port", "name": "port",
"index": 0, "displayName": "Port",
"type": "int" "type": "int",
"minValue": 0,
"maxValue": 65535,
"defaultValue": 4242
}, },
{ {
"id": "ea1be9fc-9a9b-44ba-a28d-e021aef4f046", "id": "ea1be9fc-9a9b-44ba-a28d-e021aef4f046",
"idName": "ipv4Address", "name": "address",
"name": "IPv4 Address", "displayName": "Address",
"type": "QString", "type": "QString",
"index": 1,
"inputType": "IPv4Address", "inputType": "IPv4Address",
"defaultValue": "127.0.0.1" "defaultValue": "127.0.0.1"
} }
@ -83,17 +80,14 @@
"actionTypes": [ "actionTypes": [
{ {
"id": "6bc52462-b192-46a4-a6df-92cc5a479c89", "id": "6bc52462-b192-46a4-a6df-92cc5a479c89",
"idName": "outputData", "name": "outputData",
"name": "Send Data", "displayName": "Send data",
"index": 0,
"paramTypes": [ "paramTypes": [
{ {
"id": "6604c852-6b24-4707-b8e5-1ddd8032efcc", "id": "6604c852-6b24-4707-b8e5-1ddd8032efcc",
"idName": "outputDataArea", "name": "data",
"name": "Data", "displayName": "Data",
"type": "QString", "type": "QString"
"index": 0,
"inputType": "TextArea"
} }
] ]
} }