From a152974228b8bbb69768bda635bfdd6b289ac77a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Wed, 13 Apr 2022 15:53:53 +0200 Subject: [PATCH 1/3] Fix connected state for tcp server while set up --- tcpcommander/integrationplugintcpcommander.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tcpcommander/integrationplugintcpcommander.cpp b/tcpcommander/integrationplugintcpcommander.cpp index bce407b7..7ddc4979 100644 --- a/tcpcommander/integrationplugintcpcommander.cpp +++ b/tcpcommander/integrationplugintcpcommander.cpp @@ -91,7 +91,9 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) m_tcpServers.insert(thing, tcpServer); connect(tcpServer, &TcpServer::connectionCountChanged, this, &IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged); connect(tcpServer, &TcpServer::commandReceived, this, &IntegrationPluginTcpCommander::onTcpServerCommandReceived); - return info->finish(Thing::ThingErrorNoError); + info->finish(Thing::ThingErrorNoError); + thing->setStateValue("connected", true); + return; } else { tcpServer->deleteLater(); qDebug(dcTCPCommander()) << "Could not open TCP Server"; From 007dfedc205468593849aeb47717b8266af92908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 14 Apr 2022 07:52:10 +0200 Subject: [PATCH 2/3] Fix broadcast sending and make command confirm optional for backwards compatibility --- .../integrationplugintcpcommander.cpp | 13 +++++++++- .../integrationplugintcpcommander.json | 9 +++++++ tcpcommander/tcpserver.cpp | 26 +++++++++++++++---- tcpcommander/tcpserver.h | 12 ++++++--- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/tcpcommander/integrationplugintcpcommander.cpp b/tcpcommander/integrationplugintcpcommander.cpp index 7ddc4979..1827ff49 100644 --- a/tcpcommander/integrationplugintcpcommander.cpp +++ b/tcpcommander/integrationplugintcpcommander.cpp @@ -85,19 +85,30 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) // In case of reconfigure, make sure to re-setup the server delete tcpServer; } + tcpServer = new TcpServer(port, this); + tcpServer->setConfirmCommands(thing->setting(tcpServerSettingsConfirmCommandParamTypeId).toBool()); if (tcpServer->isValid()) { m_tcpServers.insert(thing, tcpServer); + connect(thing, &Thing::settingChanged, tcpServer, [=](const ParamTypeId ¶mTypeId, const QVariant &value){ + if (paramTypeId == tcpServerSettingsConfirmCommandParamTypeId) { + tcpServer->setConfirmCommands(value.toBool()); + } + }); + connect(tcpServer, &TcpServer::connectionCountChanged, this, &IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged); connect(tcpServer, &TcpServer::commandReceived, this, &IntegrationPluginTcpCommander::onTcpServerCommandReceived); info->finish(Thing::ThingErrorNoError); + + // Set the initial connected state since the server is running thing->setStateValue("connected", true); return; } else { tcpServer->deleteLater(); qDebug(dcTCPCommander()) << "Could not open TCP Server"; - return info->finish(Thing::ThingErrorSetupFailed, QT_TR_NOOP("Error opening TCP port.")); + info->finish(Thing::ThingErrorSetupFailed, QT_TR_NOOP("Error opening TCP port.")); + return; } } } diff --git a/tcpcommander/integrationplugintcpcommander.json b/tcpcommander/integrationplugintcpcommander.json index bfaa766b..29244a33 100644 --- a/tcpcommander/integrationplugintcpcommander.json +++ b/tcpcommander/integrationplugintcpcommander.json @@ -89,6 +89,15 @@ "defaultValue": "22" } ], + "settingsTypes": [ + { + "id": "27b8a43a-186e-46e6-b2e5-b6fa1dfaaaa9", + "name": "confirmCommand", + "displayName": "Autoconfirm commands", + "type": "bool", + "defaultValue": false + } + ], "stateTypes": [ { "id": "a2eb1619-261c-45ee-9587-6b5994633ad0", diff --git a/tcpcommander/tcpserver.cpp b/tcpcommander/tcpserver.cpp index 42272237..f735f9be 100644 --- a/tcpcommander/tcpserver.cpp +++ b/tcpcommander/tcpserver.cpp @@ -61,11 +61,21 @@ TcpServer::~TcpServer() { } -bool TcpServer::isValid() +bool TcpServer::isValid() const { return m_tcpServer->isListening(); } +bool TcpServer::confirmCommands() const +{ + return m_confirmCommands; +} + +void TcpServer::setConfirmCommands(bool confirmCommands) +{ + m_confirmCommands = confirmCommands; +} + QHostAddress TcpServer::serverAddress() { return m_tcpServer->serverAddress(); @@ -76,7 +86,7 @@ int TcpServer::serverPort() return m_tcpServer->serverPort(); } -int TcpServer::connectionCount() +int TcpServer::connectionCount() const { return m_clients.count(); } @@ -85,16 +95,19 @@ bool TcpServer::sendCommand(const QString &clientIp, const QByteArray &data) { bool success = false; QHostAddress address(clientIp); + bool broadcast = false; + if (address == QHostAddress(QHostAddress::AnyIPv4) || address == QHostAddress(QHostAddress::Broadcast)) + broadcast = true; foreach (QTcpSocket *client, m_clients) { - if (address == QHostAddress(QHostAddress::AnyIPv4) || client->peerAddress() == address) { + if (broadcast || client->peerAddress() == address) { qint64 len = client->write(data); if (len == data.length()) { success = true; } } } - qCWarning(dcTCPCommander()) << "No client matching the destination IP" << address.toString(); + return success; } @@ -125,7 +138,10 @@ void TcpServer::readData() QTcpSocket *socket = static_cast(sender()); QByteArray data = socket->readAll(); qDebug(dcTCPCommander()) << "TCP Server data received: " << data; - socket->write("OK\n"); + if (m_confirmCommands) { + socket->write("OK\n"); + } + emit commandReceived(socket->peerAddress().toString(), data); } diff --git a/tcpcommander/tcpserver.h b/tcpcommander/tcpserver.h index e99cf1f6..bf384e79 100644 --- a/tcpcommander/tcpserver.h +++ b/tcpcommander/tcpserver.h @@ -43,12 +43,16 @@ public: explicit TcpServer(const quint16 &port, QObject *parent = nullptr); ~TcpServer(); - bool isValid(); - QHostAddress serverAddress(); + QHostAddress serverAddress(); int serverPort(); - int connectionCount(); + bool isValid() const; + + bool confirmCommands() const; + void setConfirmCommands(bool confirmCommands); + + int connectionCount() const; bool sendCommand(const QString &clientIp, const QByteArray &data); @@ -65,7 +69,7 @@ private slots: private: QTcpServer *m_tcpServer = nullptr; - + bool m_confirmCommands = false; QList m_clients; }; From 31e5b0c8a5ee6621f1a80c69230176664e431476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 25 Apr 2022 08:33:09 +0200 Subject: [PATCH 3/3] Fix connected state based on client count and clean up code --- .../integrationplugintcpcommander.cpp | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/tcpcommander/integrationplugintcpcommander.cpp b/tcpcommander/integrationplugintcpcommander.cpp index 1827ff49..a30f17dd 100644 --- a/tcpcommander/integrationplugintcpcommander.cpp +++ b/tcpcommander/integrationplugintcpcommander.cpp @@ -54,9 +54,9 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) // In case of a reconfigure, make sure we reconnect tcpSocket->disconnectFromHost(); } + connect(tcpSocket, &QTcpSocket::stateChanged, thing, [=](QAbstractSocket::SocketState state){ thing->setStateValue(tcpClientConnectedStateTypeId, state == QAbstractSocket::ConnectedState); - if (state == QAbstractSocket::UnconnectedState) { QTimer::singleShot(10000, tcpSocket, [=](){ qCDebug(dcTCPCommander()) << "Reconnecting to server" << address << port; @@ -64,12 +64,12 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) }); } }); + connect(tcpSocket, &QTcpSocket::readyRead, thing, [=](){ QByteArray data = tcpSocket->readAll(); ParamList params; params << Param(tcpClientTriggeredEventDataParamTypeId, data); - Event event(tcpClientTriggeredEventTypeId, thing->id(), params); - emitEvent(event); + emit emitEvent(Event(tcpClientTriggeredEventTypeId, thing->id(), params)); }); tcpSocket->connectToHost(address, port); @@ -171,15 +171,8 @@ void IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged(int connec { TcpServer *tcpServer = static_cast(sender()); Thing *thing = m_tcpServers.key(tcpServer); - if (!thing) - return; - qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected"; - if (thing->thingClassId() == tcpServerThingClassId) { - if (connections > 0) { - thing->setStateValue(tcpServerConnectedStateTypeId, true); - } else { - thing->setStateValue(tcpServerConnectedStateTypeId, false); - } + if (thing && thing->thingClassId() == tcpServerThingClassId) { + qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected"; thing->setStateValue(tcpServerConnectionCountStateTypeId, connections); } } @@ -190,10 +183,8 @@ void IntegrationPluginTcpCommander::onTcpServerCommandReceived(const QString &cl Thing *thing = m_tcpServers.key(tcpServer); qDebug(dcTCPCommander()) << thing->name() << "Message received" << data; - Event event = Event(tcpServerTriggeredEventTypeId, thing->id()); ParamList params; params.append(Param(tcpServerTriggeredEventDataParamTypeId, data)); params.append(Param(tcpServerTriggeredEventClientIpParamTypeId, clientIp)); - event.setParams(params); - emitEvent(event); + emit emitEvent(Event(tcpServerTriggeredEventTypeId, thing->id(), params)); }