added UDP output and changed Vendor to UDP commander
This commit is contained in:
parent
81a313ebbf
commit
a0d26f2635
@ -1,6 +1,7 @@
|
|||||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* *
|
* *
|
||||||
* Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io> *
|
* Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io> *
|
||||||
|
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
|
||||||
* *
|
* *
|
||||||
* This file is part of nymea. *
|
* This file is part of nymea. *
|
||||||
* *
|
* *
|
||||||
@ -70,34 +71,68 @@ DevicePluginUdpCommander::DevicePluginUdpCommander()
|
|||||||
DeviceManager::DeviceSetupStatus DevicePluginUdpCommander::setupDevice(Device *device)
|
DeviceManager::DeviceSetupStatus DevicePluginUdpCommander::setupDevice(Device *device)
|
||||||
{
|
{
|
||||||
// check port
|
// check port
|
||||||
bool portOk = false;
|
if ((device->deviceClassId() == udpInputDeviceClassId) || (device->deviceClassId() == udpOutputDeviceClassId)) {
|
||||||
int port = device->paramValue(commanderPortParamTypeId).toInt(&portOk);
|
bool portOk = false;
|
||||||
if (!portOk || port <= 0 || port > 65535) {
|
int port = device->paramValue(portParamTypeId).toInt(&portOk);
|
||||||
qCWarning(dcUdpCommander) << device->name() << ": invalid port:" << device->paramValue(commanderPortParamTypeId).toString() << ".";
|
if (!portOk || port <= 0 || port > 65535) {
|
||||||
return DeviceManager::DeviceSetupStatusFailure;
|
qCWarning(dcUdpCommander) << device->name() << ": invalid port:" << device->paramValue(portParamTypeId).toString() << ".";
|
||||||
|
return DeviceManager::DeviceSetupStatusFailure;
|
||||||
|
}
|
||||||
|
|
||||||
|
QUdpSocket *udpSocket = new QUdpSocket(this);
|
||||||
|
|
||||||
|
if (!udpSocket->bind(QHostAddress::Any, port)) {
|
||||||
|
qCWarning(dcUdpCommander) << device->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;
|
||||||
}
|
}
|
||||||
|
return DeviceManager::DeviceSetupStatusFailure;
|
||||||
|
}
|
||||||
|
|
||||||
QUdpSocket *udpSocket = new QUdpSocket(this);
|
|
||||||
|
|
||||||
if (!udpSocket->bind(QHostAddress::Any, port)) {
|
DeviceManager::DeviceError DevicePluginUdpCommander::executeAction(Device *device, const Action &action) {
|
||||||
qCWarning(dcUdpCommander) << device->name() << "can't bind to port" << port << ".";
|
|
||||||
delete udpSocket;
|
if (device->deviceClassId() == udpOutputDeviceClassId) {
|
||||||
return DeviceManager::DeviceSetupStatusFailure;
|
|
||||||
|
if (action.actionTypeId() == outputDataActionTypeId) {
|
||||||
|
QUdpSocket *udpSocket = m_commanderList.key(device);
|
||||||
|
int port = device->paramValue(portParamTypeId).toInt();
|
||||||
|
QHostAddress address = QHostAddress(device->paramValue(ipv4AddressParamTypeId).toString());
|
||||||
|
QByteArray data = action.param(outputDataAreaParamTypeId).value().toByteArray();
|
||||||
|
qDebug(dcUdpCommander()) << "Send UDP datagram:" << data << "address:" << address.toIPv4Address() << "port:" << port;
|
||||||
|
udpSocket->writeDatagram(data, address, port);
|
||||||
|
|
||||||
|
return DeviceManager::DeviceErrorNoError;
|
||||||
|
}
|
||||||
|
return DeviceManager::DeviceErrorActionTypeNotFound;
|
||||||
}
|
}
|
||||||
|
return DeviceManager::DeviceErrorDeviceClassNotFound;
|
||||||
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
|
||||||
m_commanderList.insert(udpSocket, device);
|
|
||||||
|
|
||||||
return DeviceManager::DeviceSetupStatusSuccess;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicePluginUdpCommander::deviceRemoved(Device *device)
|
void DevicePluginUdpCommander::deviceRemoved(Device *device)
|
||||||
{
|
{
|
||||||
QUdpSocket *socket = m_commanderList.key(device);
|
if (device->deviceClassId() == udpInputDeviceClassId) {
|
||||||
m_commanderList.remove(socket);
|
|
||||||
|
|
||||||
socket->close();
|
QUdpSocket *socket = m_commanderList.key(device);
|
||||||
socket->deleteLater();
|
m_commanderList.remove(socket);
|
||||||
|
|
||||||
|
socket->close();
|
||||||
|
socket->deleteLater();
|
||||||
|
}
|
||||||
|
if (device->deviceClassId() == udpOutputDeviceClassId) {
|
||||||
|
|
||||||
|
QUdpSocket *socket = m_commanderList.key(device);
|
||||||
|
m_commanderList.remove(socket);
|
||||||
|
|
||||||
|
socket->close();
|
||||||
|
socket->deleteLater();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicePluginUdpCommander::readPendingDatagrams()
|
void DevicePluginUdpCommander::readPendingDatagrams()
|
||||||
@ -114,8 +149,10 @@ void DevicePluginUdpCommander::readPendingDatagrams()
|
|||||||
socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
|
socket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (datagram == device->paramValue(commanderCommandParamTypeId).toByteArray() ||
|
device->setStateValue(inputDataStateTypeId, datagram);
|
||||||
datagram == device->paramValue(commanderCommandParamTypeId).toByteArray() + "\n") {
|
|
||||||
|
if (datagram == device->paramValue(commandParamTypeId).toByteArray() ||
|
||||||
|
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(commanderCommandReceivedEventTypeId, device->id()));
|
emit emitEvent(Event(commanderCommandReceivedEventTypeId, device->id()));
|
||||||
socket->writeDatagram("OK\n", sender, senderPort);
|
socket->writeDatagram("OK\n", sender, senderPort);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* *
|
* *
|
||||||
* Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io> *
|
* Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io> *
|
||||||
|
* Copyright (C) 2017 Bernhard Trinnes <bernhard.trinnes@guh.io> *
|
||||||
* *
|
* *
|
||||||
* This file is part of nymea. *
|
* This file is part of nymea. *
|
||||||
* *
|
* *
|
||||||
@ -41,6 +42,7 @@ public:
|
|||||||
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
|
DeviceManager::DeviceSetupStatus setupDevice(Device *device) override;
|
||||||
void deviceRemoved(Device *device) override;
|
void deviceRemoved(Device *device) override;
|
||||||
|
|
||||||
|
DeviceManager::DeviceError executeAction(Device *device, const Action &action) override;
|
||||||
private:
|
private:
|
||||||
QHash<QUdpSocket *, Device *> m_commanderList;
|
QHash<QUdpSocket *, Device *> m_commanderList;
|
||||||
|
|
||||||
|
|||||||
@ -33,6 +33,17 @@
|
|||||||
"inputType": "TextLine"
|
"inputType": "TextLine"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"stateTypes":[
|
||||||
|
{
|
||||||
|
"id": "065a1a0a-d324-4ae6-a461-bef8143e8795",
|
||||||
|
"idName": "inputData",
|
||||||
|
"name": "Input Data",
|
||||||
|
"type": "QString",
|
||||||
|
"defaultValue": "",
|
||||||
|
"eventTypeName": "input data changed",
|
||||||
|
"index": 0
|
||||||
|
}
|
||||||
|
],
|
||||||
"eventTypes": [
|
"eventTypes": [
|
||||||
{
|
{
|
||||||
"id": "5fecbba3-ffbb-456b-872c-a2f571c681cb",
|
"id": "5fecbba3-ffbb-456b-872c-a2f571c681cb",
|
||||||
@ -40,6 +51,53 @@
|
|||||||
"displayName": "command received"
|
"displayName": "command received"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "31b00639-8904-4522-84ed-54c46a54c63c",
|
||||||
|
"name": "UDP Output",
|
||||||
|
"idName": "udpOutput",
|
||||||
|
"deviceIcon": "Network",
|
||||||
|
"basicTags": [
|
||||||
|
"Service",
|
||||||
|
"Sensor"
|
||||||
|
],
|
||||||
|
"createMethods": ["user"],
|
||||||
|
"paramTypes": [
|
||||||
|
{
|
||||||
|
"id": "1843adcb-e377-44d1-8d70-ab4f9eeb32ec",
|
||||||
|
"idName": "port",
|
||||||
|
"name": "port",
|
||||||
|
"index": 0,
|
||||||
|
"type": "int"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "ea1be9fc-9a9b-44ba-a28d-e021aef4f046",
|
||||||
|
"idName": "ipv4Address",
|
||||||
|
"name": "IPv4 Address",
|
||||||
|
"type": "QString",
|
||||||
|
"index": 1,
|
||||||
|
"inputType": "IPv4Address",
|
||||||
|
"defaultValue": "127.0.0.1"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"actionTypes": [
|
||||||
|
{
|
||||||
|
"id": "6bc52462-b192-46a4-a6df-92cc5a479c89",
|
||||||
|
"idName": "outputData",
|
||||||
|
"name": "Send Data",
|
||||||
|
"index": 0,
|
||||||
|
"paramTypes": [
|
||||||
|
{
|
||||||
|
"id": "6604c852-6b24-4707-b8e5-1ddd8032efcc",
|
||||||
|
"idName": "outputDataArea",
|
||||||
|
"name": "Data",
|
||||||
|
"type": "QString",
|
||||||
|
"index": 0,
|
||||||
|
"inputType": "TextArea"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user