diff --git a/tcpcommander/deviceplugintcpcommander.cpp b/tcpcommander/deviceplugintcpcommander.cpp index e7684d2a..36491c6f 100644 --- a/tcpcommander/deviceplugintcpcommander.cpp +++ b/tcpcommander/deviceplugintcpcommander.cpp @@ -37,19 +37,23 @@ DeviceManager::DeviceSetupStatus DevicePluginTcpCommander::setupDevice(Device *d if (device->deviceClassId() == tcpOutputDeviceClassId) { QTcpSocket *tcpSocket = new QTcpSocket(this); m_tcpSockets.insert(tcpSocket, device); - return DeviceManager::DeviceSetupStatusSuccess; + connect(tcpSocket, &QTcpSocket::connected, this, &DevicePluginTcpCommander::onTcpSocketConnected); + connect(tcpSocket, &QTcpSocket::disconnected, this, &DevicePluginTcpCommander::onTcpSocketDisconnected); + connect(tcpSocket, &QTcpSocket::bytesWritten, this, &DevicePluginTcpCommander::onTcpSocketBytesWritten); + return DeviceManager::DeviceSetupStatusAsync; } if (device->deviceClassId() == tcpInputDeviceClassId) { int port = device->paramValue(portParamTypeId).toInt(); TcpServer *tcpServer = new TcpServer(port, this); - //TODO Connect TCP Server request received + if (tcpServer->isValid()) { m_tcpServer.insert(tcpServer, device); connect(tcpServer, &TcpServer::connected, this, &DevicePluginTcpCommander::onTcpServerConnected); connect(tcpServer, &TcpServer::disconnected, this, &DevicePluginTcpCommander::onTcpServerDisconnected); return DeviceManager::DeviceSetupStatusSuccess; } else { + tcpServer->deleteLater(); qDebug(dcTCPCommander()) << "Could not open TCP Server"; } } @@ -65,10 +69,7 @@ DeviceManager::DeviceError DevicePluginTcpCommander::executeAction(Device *devic int port = device->paramValue(portParamTypeId).toInt(); QHostAddress address= QHostAddress(device->paramValue(ipv4addressParamTypeId).toString()); QTcpSocket *tcpSocket = m_tcpSockets.key(device); - QByteArray data = device->paramValue(outputDataAreaParamTypeId).toByteArray(); tcpSocket->connectToHost(address, port); - tcpSocket->write(data); - tcpSocket->close(); return DeviceManager::DeviceErrorNoError; } return DeviceManager::DeviceErrorActionTypeNotFound; @@ -80,8 +81,13 @@ DeviceManager::DeviceError DevicePluginTcpCommander::executeAction(Device *devic void DevicePluginTcpCommander::deviceRemoved(Device *device) { if(device->deviceClassId() == tcpOutputDeviceClassId){ - m_tcpSockets.remove(m_tcpSockets.key(device)); + + QTcpSocket *tcpSocket = m_tcpSockets.key(device); + m_tcpSockets.remove(tcpSocket); + tcpSocket->deleteLater(); + }else if(device->deviceClassId() == tcpInputDeviceClassId){ + TcpServer *tcpServer = m_tcpServer.key(device); m_tcpServer.remove(tcpServer); tcpServer->deleteLater(); @@ -89,12 +95,42 @@ void DevicePluginTcpCommander::deviceRemoved(Device *device) } +void DevicePluginTcpCommander::onTcpSocketConnected() +{ + QTcpSocket *tcpSocket = static_cast(sender()); + Device *device = m_tcpSockets.value(tcpSocket); + if (!device->setupComplete()) { + qDebug(dcTCPCommander()) << device->name() << "Setup finished" ; + emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess); + } else { + QByteArray data = device->paramValue(outputDataAreaParamTypeId).toByteArray(); + tcpSocket->write(data); + } + device->setStateValue(connectedStateTypeId, true); +} + + +void DevicePluginTcpCommander::onTcpSocketDisconnected() +{ + QTcpSocket *tcpSocket = static_cast(sender()); + Device *device = m_tcpSockets.value(tcpSocket); + device->setStateValue(connectedStateTypeId, false); +} + + +void DevicePluginTcpCommander::onTcpSocketBytesWritten() +{ + QTcpSocket *tcpSocket = static_cast(sender()); + tcpSocket->close(); +} + void DevicePluginTcpCommander::onTcpServerConnected() { TcpServer *tcpServer = static_cast(sender()); Device *device = m_tcpServer.value(tcpServer); qDebug(dcTCPCommander()) << device->name() << "Tcp Server Client connected" ; device->setStateValue(connectedStateTypeId, true); + connect(tcpServer, &TcpServer::textMessageReceived, this, &DevicePluginTcpCommander::onTcpServerTextMessageReceived); //send signal device Setup was successfull } @@ -113,7 +149,7 @@ void DevicePluginTcpCommander::onTcpServerTextMessageReceived(QByteArray data) TcpServer *tcpServer = static_cast(sender()); Device *device = m_tcpServer.value(tcpServer); qDebug(dcTCPCommander()) << device->name() << "Message received" << data; - device->setStateValue(responseStateTypeId, data); //TODO change wording + device->setStateValue(dataReceivedStateTypeId, data); if (device->paramValue(comparisionParamTypeId).toString() == "Is exactly") { qDebug(dcTCPCommander()) << "is exacly"; diff --git a/tcpcommander/deviceplugintcpcommander.h b/tcpcommander/deviceplugintcpcommander.h index 88da8fc4..d000f3a4 100644 --- a/tcpcommander/deviceplugintcpcommander.h +++ b/tcpcommander/deviceplugintcpcommander.h @@ -47,6 +47,10 @@ private: QHash m_tcpServer; private slots: + void onTcpSocketConnected(); + void onTcpSocketDisconnected(); + void onTcpSocketBytesWritten(); + void onTcpServerConnected(); void onTcpServerDisconnected(); void onTcpServerTextMessageReceived(QByteArray message); diff --git a/tcpcommander/deviceplugintcpcommander.json b/tcpcommander/deviceplugintcpcommander.json index 44cbab22..18e3897c 100644 --- a/tcpcommander/deviceplugintcpcommander.json +++ b/tcpcommander/deviceplugintcpcommander.json @@ -105,7 +105,7 @@ { "id": "23051bdf-3f50-41fa-abde-bc4fe0bcc4fc", "idName": "inputData", - "name": "Data", + "name": "Command", "type": "QString", "inputType": "TextArea", "defaultValue": "", @@ -124,12 +124,12 @@ }, { "id": "b98fdacc-59d7-41c4-b790-1fdca50dfb22", - "idName": "response", - "name": "Response", + "idName": "dataReceived", + "name": "Data Received", "type": "QString", "inputType": "TextArea", "defaultValue": "", - "eventTypeName": "response received", + "eventTypeName": "Data received", "index" : 1 } ], diff --git a/tcpcommander/translations/de_DE.ts b/tcpcommander/translations/de_DE.ts index 738c664a..4633dce4 100644 --- a/tcpcommander/translations/de_DE.ts +++ b/tcpcommander/translations/de_DE.ts @@ -53,11 +53,8 @@ - Data - The name of the paramType (6604c852-6b24-4707-b8e5-1ddd8032efcc) of TCP Output ----------- -The name of the paramType (23051bdf-3f50-41fa-abde-bc4fe0bcc4fc) of TCP Input + The name of the paramType (6604c852-6b24-4707-b8e5-1ddd8032efcc) of TCP Output @@ -72,15 +69,21 @@ The name of the paramType (23051bdf-3f50-41fa-abde-bc4fe0bcc4fc) of TCP InputThe name of the paramType (d99f55c7-0e14-45ee-b0f0-33f2d1d2e674) of TCP Input + + + Command + The name of the paramType (23051bdf-3f50-41fa-abde-bc4fe0bcc4fc) of TCP Input + + - response received + Data received The name of the autocreated EventType (b98fdacc-59d7-41c4-b790-1fdca50dfb22) - Response + Data Received The name of the ParamType of StateType (b98fdacc-59d7-41c4-b790-1fdca50dfb22) of DeviceClass TCP Input diff --git a/tcpcommander/translations/en_US.ts b/tcpcommander/translations/en_US.ts index 66df86b8..544bfd83 100644 --- a/tcpcommander/translations/en_US.ts +++ b/tcpcommander/translations/en_US.ts @@ -53,11 +53,8 @@ - Data - The name of the paramType (6604c852-6b24-4707-b8e5-1ddd8032efcc) of TCP Output ----------- -The name of the paramType (23051bdf-3f50-41fa-abde-bc4fe0bcc4fc) of TCP Input + The name of the paramType (6604c852-6b24-4707-b8e5-1ddd8032efcc) of TCP Output @@ -72,15 +69,21 @@ The name of the paramType (23051bdf-3f50-41fa-abde-bc4fe0bcc4fc) of TCP InputThe name of the paramType (d99f55c7-0e14-45ee-b0f0-33f2d1d2e674) of TCP Input + + + Command + The name of the paramType (23051bdf-3f50-41fa-abde-bc4fe0bcc4fc) of TCP Input + + - response received + Data received The name of the autocreated EventType (b98fdacc-59d7-41c4-b790-1fdca50dfb22) - Response + Data Received The name of the ParamType of StateType (b98fdacc-59d7-41c4-b790-1fdca50dfb22) of DeviceClass TCP Input