diff --git a/tcpcommander/integrationplugintcpcommander.cpp b/tcpcommander/integrationplugintcpcommander.cpp index b99da97e..ebd7c7ab 100644 --- a/tcpcommander/integrationplugintcpcommander.cpp +++ b/tcpcommander/integrationplugintcpcommander.cpp @@ -68,7 +68,7 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) if (tcpServer->isValid()) { m_tcpServer.insert(tcpServer, thing); - connect(tcpServer, &TcpServer::connectionChanged, this, &IntegrationPluginTcpCommander::onTcpServerConnectionChanged); + connect(tcpServer, &TcpServer::connectionCountChanged, this, &IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged); connect(tcpServer, &TcpServer::commandReceived, this, &IntegrationPluginTcpCommander::onTcpServerCommandReceived); return info->finish(Thing::ThingErrorNoError); } else { @@ -128,13 +128,20 @@ void IntegrationPluginTcpCommander::onTcpSocketConnectionChanged(bool connected) } -void IntegrationPluginTcpCommander::onTcpServerConnectionChanged(bool connected) +void IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged(int connections) { TcpServer *tcpServer = static_cast(sender()); Thing *thing = m_tcpServer.value(tcpServer); - qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected" ; + if (!thing) + return; + qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected"; if (thing->thingClassId() == tcpInputThingClassId) { - thing->setStateValue(tcpInputConnectedStateTypeId, connected); + if (connections > 0) { + thing->setStateValue(tcpInputConnectedStateTypeId, true); + } else { + thing->setStateValue(tcpInputConnectedStateTypeId, false); + } + thing->setStateValue(tcpInputConnectionCountStateTypeId, connections); } } diff --git a/tcpcommander/integrationplugintcpcommander.h b/tcpcommander/integrationplugintcpcommander.h index 0c30a2a2..36220cce 100644 --- a/tcpcommander/integrationplugintcpcommander.h +++ b/tcpcommander/integrationplugintcpcommander.h @@ -58,7 +58,7 @@ private: private slots: void onTcpSocketConnectionChanged(bool connected); - void onTcpServerConnectionChanged(bool connected); + void onTcpServerConnectionCountChanged(int connections); void onTcpServerCommandReceived(QByteArray message); }; diff --git a/tcpcommander/integrationplugintcpcommander.json b/tcpcommander/integrationplugintcpcommander.json index adb8d1b1..6934e247 100644 --- a/tcpcommander/integrationplugintcpcommander.json +++ b/tcpcommander/integrationplugintcpcommander.json @@ -1,6 +1,6 @@ { "name": "TCPCommander", - "displayName": "tcp commander", + "displayName": "Tcp Commander", "id": "741b7b0a-0c9c-4c93-be99-0d0bcf5a4643", "vendors": [ { @@ -80,14 +80,24 @@ "displayName": "Connected", "type": "bool", "defaultValue": false, + "cached": false, "displayNameEvent": "Connection status changed" + }, + { + "id": "e959417f-55d4-4316-88f4-d3fa29976841", + "name": "connectionCount", + "displayName": "Connection count", + "type": "int", + "defaultValue": 0, + "cached": false, + "displayNameEvent": "Connection count changed" } ], "eventTypes": [ { "id": "6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b", "name": "triggered", - "displayName": "Data recieved", + "displayName": "Data received", "paramTypes": [ { "id": "97d7ee8c-d9db-40b4-9855-4ceecd64c411", diff --git a/tcpcommander/tcpserver.cpp b/tcpcommander/tcpserver.cpp index 4580b353..9bc27b0a 100644 --- a/tcpcommander/tcpserver.cpp +++ b/tcpcommander/tcpserver.cpp @@ -45,7 +45,6 @@ TcpServer::TcpServer(const QHostAddress address, const quint16 &port, QObject *p } } - TcpServer::TcpServer(const quint16 &port, QObject *parent) : QObject(parent) { @@ -77,38 +76,46 @@ int TcpServer::serverPort() return m_tcpServer->serverPort(); } +int TcpServer::connectionCount() +{ + return m_connectionCount; +} + void TcpServer::newConnection() { qDebug(dcTCPCommander()) << "TCP Server new Connection request"; - m_socket = m_tcpServer->nextPendingConnection(); - m_socket->flush(); + QTcpSocket *socket = m_tcpServer->nextPendingConnection(); + socket->flush(); - emit connectionChanged(true); - connect(m_socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected); - connect(m_socket, &QTcpSocket::readyRead, this, &TcpServer::readData); + m_connectionCount++; + emit connectionCountChanged(m_connectionCount); + connect(socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected); + connect(socket, &QTcpSocket::readyRead, this, &TcpServer::readData); // Note: error signal will be interpreted as function, not as signal in C++11 - connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError))); + connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError))); } - void TcpServer::onDisconnected() { qDebug(dcTCPCommander()) << "TCP Server connection aborted"; - disconnect(m_socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected); - disconnect(m_socket, &QTcpSocket::readyRead, this, &TcpServer::readData); - m_socket->deleteLater(); - emit connectionChanged(false); + m_connectionCount--; + if (m_connectionCount < 0) + m_connectionCount = 0; + + emit connectionCountChanged(m_connectionCount); } void TcpServer::readData() { - QByteArray data = m_socket->readAll(); + QTcpSocket *socket = static_cast(sender()); + QByteArray data = socket->readAll(); qDebug(dcTCPCommander()) << "TCP Server data received: " << data; - m_socket->write("OK\n"); + socket->write("OK\n"); emit commandReceived(data); } void TcpServer::onError(QAbstractSocket::SocketError error) { - qWarning(dcTCPCommander()) << "Socket Error" << m_socket->errorString() << error; + QTcpSocket *socket = static_cast(sender()); + qWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error; } diff --git a/tcpcommander/tcpserver.h b/tcpcommander/tcpserver.h index 1bb5a8f6..06d4d1c2 100644 --- a/tcpcommander/tcpserver.h +++ b/tcpcommander/tcpserver.h @@ -50,15 +50,16 @@ public: void setPort(int port); void setServerAddress(const QHostAddress &address); - + int connectionCount(); private: QTcpServer *m_tcpServer = nullptr; - QTcpSocket *m_socket = nullptr; + + int m_connectionCount = 0; signals: void newPendingConnection(); void commandReceived(QByteArray message); - void connectionChanged(bool connected); + void connectionCountChanged(int connections); private slots: void newConnection(); diff --git a/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-de_DE.ts b/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-de_DE.ts index e0bd008b..7fee0b8f 100644 --- a/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-de_DE.ts +++ b/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-de_DE.ts @@ -6,39 +6,33 @@ Error connecting to remote server. - + Fehler beim Herstellen einer Verbindung zum Remote-Server. Error opening TCP port. - + Fehler beim Öffnen des TCP-Ports. TCPCommander - - tcp commander - The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643}) - TCP Commander - - - + TCP Output The name of the ThingClass ({c67d059f-694f-47cb-8e1d-9e3e6d014c1a}) TCP Ausgang - + IPv4 Address The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {2a3fcb23-931b-4ba1-b134-c49b656c76f7}) IPv4 Adresse - + Connected The name of the ParamType (ThingClass: tcpInput, EventType: connected, ID: {a2eb1619-261c-45ee-9587-6b5994633ad0}) ---------- @@ -47,26 +41,41 @@ The name of the StateType ({a2eb1619-261c-45ee-9587-6b5994633ad0}) of ThingClass The name of the ParamType (ThingClass: tcpOutput, EventType: connected, ID: {725b541a-9e0c-4634-81eb-e415c0b8f025}) ---------- The name of the StateType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass tcpOutput - + Verbunden - + + Connection count + The name of the ParamType (ThingClass: tcpInput, EventType: connectionCount, ID: {e959417f-55d4-4316-88f4-d3fa29976841}) +---------- +The name of the StateType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput + Verbindungsanzahl + + + + Connection count changed + The name of the EventType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput + Verbindungsanzahl geändert + + + + Connection status changed The name of the EventType ({a2eb1619-261c-45ee-9587-6b5994633ad0}) of ThingClass tcpInput ---------- The name of the EventType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass tcpOutput - + Verbindungsstatus geändert - + Data recieved The name of the EventType ({6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b}) of ThingClass tcpInput - + Daten empfangen - - + + Port The name of the ParamType (ThingClass: tcpInput, Type: thing, ID: {88eb227d-13f7-451c-babf-1b141c219fd4}) ---------- @@ -74,20 +83,26 @@ The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {bee8b151-815 Port - - nymea - The name of the vendor ({2062d64d-3232-433c-88bc-0d33c0ba2ba6}) - + + Tcp Commander + The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643}) + TCP Commander - + + nymea + The name of the vendor ({2062d64d-3232-433c-88bc-0d33c0ba2ba6}) + nymea + + + Send Data The name of the ActionType ({6bc52462-b192-46a4-a6df-92cc5a479c89}) of ThingClass tcpOutput Sende Daten - - + + Data The name of the ParamType (ThingClass: tcpInput, EventType: triggered, ID: {97d7ee8c-d9db-40b4-9855-4ceecd64c411}) ---------- @@ -95,7 +110,7 @@ The name of the ParamType (ThingClass: tcpOutput, ActionType: trigger, ID: {6604 Daten - + TCP Input The name of the ThingClass ({bc40e84a-69c4-4fd2-beb0-bd65f19aa8ff}) TCP Eingang diff --git a/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-en_US.ts b/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-en_US.ts index 8bef8d39..f02a6d8c 100644 --- a/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-en_US.ts +++ b/tcpcommander/translations/741b7b0a-0c9c-4c93-be99-0d0bcf5a4643-en_US.ts @@ -17,28 +17,22 @@ TCPCommander - - tcp commander - The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643}) - - - - + TCP Output The name of the ThingClass ({c67d059f-694f-47cb-8e1d-9e3e6d014c1a}) - + IPv4 Address The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {2a3fcb23-931b-4ba1-b134-c49b656c76f7}) - + Connected The name of the ParamType (ThingClass: tcpInput, EventType: connected, ID: {a2eb1619-261c-45ee-9587-6b5994633ad0}) ---------- @@ -50,8 +44,23 @@ The name of the StateType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass - + + Connection count + The name of the ParamType (ThingClass: tcpInput, EventType: connectionCount, ID: {e959417f-55d4-4316-88f4-d3fa29976841}) +---------- +The name of the StateType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput + + + + + Connection count changed + The name of the EventType ({e959417f-55d4-4316-88f4-d3fa29976841}) of ThingClass tcpInput + + + + + Connection status changed The name of the EventType ({a2eb1619-261c-45ee-9587-6b5994633ad0}) of ThingClass tcpInput ---------- @@ -59,14 +68,14 @@ The name of the EventType ({725b541a-9e0c-4634-81eb-e415c0b8f025}) of ThingClass - + Data recieved The name of the EventType ({6d7c6df6-cb61-4d9e-b0d7-37c43911ca4b}) of ThingClass tcpInput - - + + Port The name of the ParamType (ThingClass: tcpInput, Type: thing, ID: {88eb227d-13f7-451c-babf-1b141c219fd4}) ---------- @@ -74,20 +83,26 @@ The name of the ParamType (ThingClass: tcpOutput, Type: thing, ID: {bee8b151-815 - + + Tcp Commander + The name of the plugin TCPCommander ({741b7b0a-0c9c-4c93-be99-0d0bcf5a4643}) + + + + nymea The name of the vendor ({2062d64d-3232-433c-88bc-0d33c0ba2ba6}) - + Send Data The name of the ActionType ({6bc52462-b192-46a4-a6df-92cc5a479c89}) of ThingClass tcpOutput - - + + Data The name of the ParamType (ThingClass: tcpInput, EventType: triggered, ID: {97d7ee8c-d9db-40b4-9855-4ceecd64c411}) ---------- @@ -95,7 +110,7 @@ The name of the ParamType (ThingClass: tcpOutput, ActionType: trigger, ID: {6604 - + TCP Input The name of the ThingClass ({bc40e84a-69c4-4fd2-beb0-bd65f19aa8ff})