Update udpcommander plugin

This commit is contained in:
Michael Zanetti 2019-09-19 23:13:34 +02:00
parent 3dcc5a038d
commit ddedc1ebc2
2 changed files with 21 additions and 22 deletions

View File

@ -30,8 +30,10 @@ DevicePluginUdpCommander::DevicePluginUdpCommander()
} }
Device::DeviceSetupStatus DevicePluginUdpCommander::setupDevice(Device *device) void DevicePluginUdpCommander::setupDevice(DeviceSetupInfo *info)
{ {
Device *device = info->device();
qCDebug(dcUdpCommander()) << "Setup device" << device->name() << device->params(); qCDebug(dcUdpCommander()) << "Setup device" << device->name() << device->params();
if (device->deviceClassId() == udpReceiverDeviceClassId) { if (device->deviceClassId() == udpReceiverDeviceClassId) {
@ -40,40 +42,37 @@ Device::DeviceSetupStatus DevicePluginUdpCommander::setupDevice(Device *device)
if (!udpSocket->bind(QHostAddress::Any, port, QUdpSocket::ShareAddress)) { if (!udpSocket->bind(QHostAddress::Any, port, QUdpSocket::ShareAddress)) {
qCWarning(dcUdpCommander()) << device->name() << "cannot bind to port" << port; qCWarning(dcUdpCommander()) << device->name() << "cannot bind to port" << port;
delete udpSocket; delete udpSocket;
return Device::DeviceSetupStatusFailure; return info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("Error opening UDP port."));
} }
qCDebug(dcUdpCommander()) << "Listening on port" << port; qCDebug(dcUdpCommander()) << "Listening on port" << port;
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
m_receiverList.insert(udpSocket, device); m_receiverList.insert(udpSocket, device);
return Device::DeviceSetupStatusSuccess; return info->finish(Device::DeviceErrorNoError);
} else if (device->deviceClassId() == udpCommanderDeviceClassId) { } else if (device->deviceClassId() == udpCommanderDeviceClassId) {
QUdpSocket *udpSocket = new QUdpSocket(this); QUdpSocket *udpSocket = new QUdpSocket(this);
m_commanderList.insert(udpSocket, device); m_commanderList.insert(udpSocket, device);
return Device::DeviceSetupStatusSuccess; return info->finish(Device::DeviceErrorNoError);
} }
return Device::DeviceSetupStatusFailure;
} }
Device::DeviceError DevicePluginUdpCommander::executeAction(Device *device, const Action &action) { void DevicePluginUdpCommander::executeAction(DeviceActionInfo *info)
{
Device *device = info->device();
Action action = info->action();
if (device->deviceClassId() == udpCommanderDeviceClassId) { Q_ASSERT_X(action.actionTypeId() == udpCommanderTriggerActionTypeId, "UdpCommander", "Unhandled action type in UDP commander.");
if (action.actionTypeId() == udpCommanderTriggerActionTypeId) {
QUdpSocket *udpSocket = m_commanderList.key(device);
int port = device->paramValue(udpCommanderDevicePortParamTypeId).toInt();
QHostAddress address = QHostAddress(device->paramValue(udpCommanderDeviceAddressParamTypeId).toString());
QByteArray data = action.param(udpCommanderTriggerActionDataParamTypeId).value().toByteArray();
qDebug(dcUdpCommander()) << "Send UDP datagram:" << data << "address:" << address.toIPv4Address() << "port:" << port;
udpSocket->writeDatagram(data, address, port);
return Device::DeviceErrorNoError; QUdpSocket *udpSocket = m_commanderList.key(device);
} int port = device->paramValue(udpCommanderDevicePortParamTypeId).toInt();
return Device::DeviceErrorActionTypeNotFound; QHostAddress address = QHostAddress(device->paramValue(udpCommanderDeviceAddressParamTypeId).toString());
} QByteArray data = action.param(udpCommanderTriggerActionDataParamTypeId).value().toByteArray();
return Device::DeviceErrorDeviceClassNotFound; qDebug(dcUdpCommander()) << "Send UDP datagram:" << data << "address:" << address.toIPv4Address() << "port:" << port;
udpSocket->writeDatagram(data, address, port);
return info->finish(Device::DeviceErrorNoError);
} }
void DevicePluginUdpCommander::deviceRemoved(Device *device) void DevicePluginUdpCommander::deviceRemoved(Device *device)

View File

@ -39,10 +39,10 @@ class DevicePluginUdpCommander : public DevicePlugin
public: public:
explicit DevicePluginUdpCommander(); explicit DevicePluginUdpCommander();
Device::DeviceSetupStatus setupDevice(Device *device) override; void setupDevice(DeviceSetupInfo *info) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
Device::DeviceError executeAction(Device *device, const Action &action) override; void executeAction(DeviceActionInfo *info) override;
private: private:
QHash<QUdpSocket *, Device *> m_receiverList; QHash<QUdpSocket *, Device *> m_receiverList;