From 4a6b5ea75b9caef3ea3002ff936a7f22a7a383e8 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 19 Sep 2019 23:03:00 +0200 Subject: [PATCH] Update TcpCommander plugin --- tcpcommander/deviceplugintcpcommander.cpp | 60 ++++++++++------------- tcpcommander/deviceplugintcpcommander.h | 6 +-- 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/tcpcommander/deviceplugintcpcommander.cpp b/tcpcommander/deviceplugintcpcommander.cpp index 5177079a..6b7a2ee6 100644 --- a/tcpcommander/deviceplugintcpcommander.cpp +++ b/tcpcommander/deviceplugintcpcommander.cpp @@ -27,8 +27,10 @@ DevicePluginTcpCommander::DevicePluginTcpCommander() } -Device::DeviceSetupStatus DevicePluginTcpCommander::setupDevice(Device *device) +void DevicePluginTcpCommander::setupDevice(DeviceSetupInfo *info) { + Device *device = info->device(); + if (device->deviceClassId() == tcpOutputDeviceClassId) { quint16 port = device->paramValue(tcpOutputDevicePortParamTypeId).toUInt(); @@ -36,18 +38,18 @@ Device::DeviceSetupStatus DevicePluginTcpCommander::setupDevice(Device *device) TcpSocket *tcpSocket = new TcpSocket(address, port, this); m_tcpSockets.insert(tcpSocket, device); connect(tcpSocket, &TcpSocket::connectionChanged, this, &DevicePluginTcpCommander::onTcpSocketConnectionChanged); - connect(tcpSocket, &TcpSocket::commandSent, this, &DevicePluginTcpCommander::onTcpSocketCommandSent); - connect(tcpSocket, &TcpSocket::connectionTestFinished, this, [this, device] (bool status) { + connect(tcpSocket, &TcpSocket::connectionTestFinished, info, [info] (bool status) { if (status) { - emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess); + info->finish(Device::DeviceErrorNoError); } else { - emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure); + info->finish(Device::DeviceErrorSetupFailed, QT_TR_NOOP("Error connecting to remote server.")); } }); - tcpSocket->connectionTest(); + // Test the socket, if a socket can be established the setup process was successfull - return Device::DeviceSetupStatusAsync; + tcpSocket->connectionTest(); + return; } if (device->deviceClassId() == tcpInputDeviceClassId) { @@ -58,30 +60,34 @@ Device::DeviceSetupStatus DevicePluginTcpCommander::setupDevice(Device *device) m_tcpServer.insert(tcpServer, device); connect(tcpServer, &TcpServer::connectionChanged, this, &DevicePluginTcpCommander::onTcpServerConnectionChanged); connect(tcpServer, &TcpServer::commandReceived, this, &DevicePluginTcpCommander::onTcpServerCommandReceived); - return Device::DeviceSetupStatusSuccess; + return info->finish(Device::DeviceErrorNoError); } else { tcpServer->deleteLater(); qDebug(dcTCPCommander()) << "Could not open TCP Server"; + return info->finish(Device::DeviceErrorSetupFailed, QT_TR_NOOP("Error opening TCP port.")); } } - return Device::DeviceSetupStatusFailure; } -Device::DeviceError DevicePluginTcpCommander::executeAction(Device *device, const Action &action) +void DevicePluginTcpCommander::executeAction(DeviceActionInfo *info) { - if (device->deviceClassId() == tcpOutputDeviceClassId) { + Device *device = info->device(); + Action action = info->action(); - if (action.actionTypeId() == tcpOutputTriggerActionTypeId) { - TcpSocket *tcpSocket = m_tcpSockets.key(device); - QByteArray data = action.param(tcpOutputTriggerActionOutputDataAreaParamTypeId).value().toByteArray(); - tcpSocket->sendCommand(data); - m_pendingActions.insert(action.id(), device->id()); - return Device::DeviceErrorAsync; + Q_ASSERT_X(action.actionTypeId() == tcpOutputTriggerActionTypeId, "TcpCommander", "Invalid action type in executeAction"); + + TcpSocket *tcpSocket = m_tcpSockets.key(device); + QByteArray data = action.param(tcpOutputTriggerActionOutputDataAreaParamTypeId).value().toByteArray(); + tcpSocket->sendCommand(data); + + connect(tcpSocket, &TcpSocket::commandSent, info, [info](bool success){ + if (success) { + info->finish(Device::DeviceErrorNoError); + } else { + info->finish(Device::DeviceErrorHardwareNotAvailable); } - return Device::DeviceErrorActionTypeNotFound; - } - return Device::DeviceErrorDeviceClassNotFound; + }); } @@ -111,20 +117,6 @@ void DevicePluginTcpCommander::onTcpSocketConnectionChanged(bool connected) } } -void DevicePluginTcpCommander::onTcpSocketCommandSent(bool successfull) -{ - TcpSocket *tcpSocket = static_cast(sender()); - Device *device = m_tcpSockets.value(tcpSocket); - - ActionId action = m_pendingActions.key(device->id()); - m_pendingActions.remove(action); - if (successfull) { - emit actionExecutionFinished(action, Device::DeviceErrorNoError); - } else { - emit actionExecutionFinished(action, Device::DeviceErrorHardwareNotAvailable); - } -} - void DevicePluginTcpCommander::onTcpServerConnectionChanged(bool connected) { diff --git a/tcpcommander/deviceplugintcpcommander.h b/tcpcommander/deviceplugintcpcommander.h index addc713c..f019373b 100644 --- a/tcpcommander/deviceplugintcpcommander.h +++ b/tcpcommander/deviceplugintcpcommander.h @@ -35,20 +35,18 @@ class DevicePluginTcpCommander : public DevicePlugin public: explicit DevicePluginTcpCommander(); - Device::DeviceSetupStatus setupDevice(Device *device) override; + void setupDevice(DeviceSetupInfo *info) override; void deviceRemoved(Device *device) override; - Device::DeviceError executeAction(Device *device, const Action &action) override; + void executeAction(DeviceActionInfo *info) override; private: QHash m_tcpSockets; QHash m_tcpServer; - QHash m_pendingActions; private slots: void onTcpSocketConnectionChanged(bool connected); - void onTcpSocketCommandSent(bool successfulle); void onTcpServerConnectionChanged(bool connected); void onTcpServerCommandReceived(QByteArray message);