From 4609b79a070dfe2ee75e412f8a882328b728a5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 8 Aug 2025 16:46:14 +0200 Subject: [PATCH] tcpcommander: Add Qt6 support --- .../integrationplugintcpcommander.cpp | 15 ++++++------ tcpcommander/integrationplugintcpcommander.h | 13 ++++++---- tcpcommander/tcpcommander.pro | 2 -- tcpcommander/tcpserver.cpp | 24 +++++++++++-------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/tcpcommander/integrationplugintcpcommander.cpp b/tcpcommander/integrationplugintcpcommander.cpp index a30f17dd..5317afdf 100644 --- a/tcpcommander/integrationplugintcpcommander.cpp +++ b/tcpcommander/integrationplugintcpcommander.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2020, nymea GmbH +* Copyright 2013 - 2025, nymea GmbH * Contact: contact@nymea.io * * This file is part of nymea. @@ -30,6 +30,7 @@ #include "integrationplugintcpcommander.h" #include "plugininfo.h" +#include "tcpserver.h" #include @@ -58,14 +59,14 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) connect(tcpSocket, &QTcpSocket::stateChanged, thing, [=](QAbstractSocket::SocketState state){ thing->setStateValue(tcpClientConnectedStateTypeId, state == QAbstractSocket::ConnectedState); if (state == QAbstractSocket::UnconnectedState) { - QTimer::singleShot(10000, tcpSocket, [=](){ + QTimer::singleShot(10000, tcpSocket, [tcpSocket, address, port](){ qCDebug(dcTCPCommander()) << "Reconnecting to server" << address << port; tcpSocket->connectToHost(address, port); }); } }); - connect(tcpSocket, &QTcpSocket::readyRead, thing, [=](){ + connect(tcpSocket, &QTcpSocket::readyRead, thing, [this, thing, tcpSocket](){ QByteArray data = tcpSocket->readAll(); ParamList params; params << Param(tcpClientTriggeredEventDataParamTypeId, data); @@ -91,7 +92,7 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) if (tcpServer->isValid()) { m_tcpServers.insert(thing, tcpServer); - connect(thing, &Thing::settingChanged, tcpServer, [=](const ParamTypeId ¶mTypeId, const QVariant &value){ + connect(thing, &Thing::settingChanged, tcpServer, [tcpServer](const ParamTypeId ¶mTypeId, const QVariant &value){ if (paramTypeId == tcpServerSettingsConfirmCommandParamTypeId) { tcpServer->setConfirmCommands(value.toBool()); } @@ -106,7 +107,7 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info) return; } else { tcpServer->deleteLater(); - qDebug(dcTCPCommander()) << "Could not open TCP Server"; + qCDebug(dcTCPCommander()) << "Could not open TCP Server"; info->finish(Thing::ThingErrorSetupFailed, QT_TR_NOOP("Error opening TCP port.")); return; } @@ -172,7 +173,7 @@ void IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged(int connec TcpServer *tcpServer = static_cast(sender()); Thing *thing = m_tcpServers.key(tcpServer); if (thing && thing->thingClassId() == tcpServerThingClassId) { - qDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected"; + qCDebug(dcTCPCommander()) << thing->name() << "Tcp Server Client connected"; thing->setStateValue(tcpServerConnectionCountStateTypeId, connections); } } @@ -181,7 +182,7 @@ void IntegrationPluginTcpCommander::onTcpServerCommandReceived(const QString &cl { TcpServer *tcpServer = static_cast(sender()); Thing *thing = m_tcpServers.key(tcpServer); - qDebug(dcTCPCommander()) << thing->name() << "Message received" << data; + qCDebug(dcTCPCommander()) << thing->name() << "Message received" << data; ParamList params; params.append(Param(tcpServerTriggeredEventDataParamTypeId, data)); diff --git a/tcpcommander/integrationplugintcpcommander.h b/tcpcommander/integrationplugintcpcommander.h index d3bae82e..26c7303e 100644 --- a/tcpcommander/integrationplugintcpcommander.h +++ b/tcpcommander/integrationplugintcpcommander.h @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2020, nymea GmbH +* Copyright 2013 - 2025, nymea GmbH * Contact: contact@nymea.io * * This file is part of nymea. @@ -31,8 +31,11 @@ #ifndef INTEGRATIONPLUGINDEVTCPCOMMANDER_H #define INTEGRATIONPLUGINDEVTCPCOMMANDER_H -#include "integrations/integrationplugin.h" -#include "tcpserver.h" +#include + +#include + +class TcpServer; class IntegrationPluginTcpCommander : public IntegrationPlugin { @@ -51,8 +54,8 @@ public: void executeAction(ThingActionInfo *info) override; private: - QHash m_tcpSockets; - QHash m_tcpServers; + QHash m_tcpSockets; + QHash m_tcpServers; private slots: void onTcpSocketConnectionChanged(bool connected); diff --git a/tcpcommander/tcpcommander.pro b/tcpcommander/tcpcommander.pro index 3e0a4d50..71e4cf34 100644 --- a/tcpcommander/tcpcommander.pro +++ b/tcpcommander/tcpcommander.pro @@ -2,8 +2,6 @@ include(../plugins.pri) QT += network -TARGET = $$qtLibraryTarget(nymea_integrationplugintcpcommander) - SOURCES += \ integrationplugintcpcommander.cpp \ tcpserver.cpp diff --git a/tcpcommander/tcpserver.cpp b/tcpcommander/tcpserver.cpp index f735f9be..9b080378 100644 --- a/tcpcommander/tcpserver.cpp +++ b/tcpcommander/tcpserver.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* Copyright 2013 - 2020, nymea GmbH +* Copyright 2013 - 2025, nymea GmbH * Contact: contact@nymea.io * * This file is part of nymea. @@ -30,17 +30,17 @@ #include "tcpserver.h" #include "extern-plugininfo.h" -#include +#include TcpServer::TcpServer(const QHostAddress address, const quint16 &port, QObject *parent) : QObject(parent) { m_tcpServer = new QTcpServer(this); connect(m_tcpServer, &QTcpServer::newConnection, this, &TcpServer::newConnection); - qDebug(dcTCPCommander()) << "TCP Server on Port: " << port << "Address: " << address.toString(); + qCDebug(dcTCPCommander()) << "TCP Server on Port: " << port << "Address: " << address.toString(); if (!m_tcpServer->listen(address, port)) { - qWarning(dcTCPCommander()) << "Unable to start the server: " << m_tcpServer->errorString(); + qCWarning(dcTCPCommander()) << "Unable to start the server: " << m_tcpServer->errorString(); return; } } @@ -50,9 +50,9 @@ TcpServer::TcpServer(const quint16 &port, QObject *parent) : { m_tcpServer = new QTcpServer(this); connect(m_tcpServer, &QTcpServer::newConnection, this, &TcpServer::newConnection); - qDebug(dcTCPCommander()) << "TCP Server on Port: " << port; + qCDebug(dcTCPCommander()) << "TCP Server on Port: " << port; if (!m_tcpServer->listen(QHostAddress::Any, port)) { - qWarning(dcTCPCommander()) << "Unable to start the server: " << m_tcpServer->errorString(); + qCWarning(dcTCPCommander()) << "Unable to start the server: " << m_tcpServer->errorString(); return; } } @@ -113,7 +113,7 @@ bool TcpServer::sendCommand(const QString &clientIp, const QByteArray &data) void TcpServer::newConnection() { - qDebug(dcTCPCommander()) << "TCP Server new Connection request"; + qCDebug(dcTCPCommander()) << "TCP Server new Connection request"; QTcpSocket *socket = m_tcpServer->nextPendingConnection(); socket->flush(); @@ -121,14 +121,18 @@ void TcpServer::newConnection() emit connectionCountChanged(m_clients.count()); connect(socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected); connect(socket, &QTcpSocket::readyRead, this, &TcpServer::readData); +#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) + connect(socket, &QTcpSocket::errorOccurred, this, &TcpServer::onError); +#else // Note: error signal will be interpreted as function, not as signal in C++11 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError))); +#endif } void TcpServer::onDisconnected() { QTcpSocket *client = qobject_cast(sender()); - qDebug(dcTCPCommander()) << "TCP client disconnected"; + qCDebug(dcTCPCommander()) << "TCP client disconnected"; m_clients.removeAll(client); emit connectionCountChanged(m_clients.count()); } @@ -137,7 +141,7 @@ void TcpServer::readData() { QTcpSocket *socket = static_cast(sender()); QByteArray data = socket->readAll(); - qDebug(dcTCPCommander()) << "TCP Server data received: " << data; + qCDebug(dcTCPCommander()) << "TCP Server data received: " << data; if (m_confirmCommands) { socket->write("OK\n"); } @@ -148,5 +152,5 @@ void TcpServer::readData() void TcpServer::onError(QAbstractSocket::SocketError error) { QTcpSocket *socket = static_cast(sender()); - qWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error; + qCWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error; }