tcpcommander: Add Qt6 support

This commit is contained in:
Simon Stürz 2025-08-08 16:46:14 +02:00
parent 09b0855045
commit 4609b79a07
4 changed files with 30 additions and 24 deletions

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2020, nymea GmbH * Copyright 2013 - 2025, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
@ -30,6 +30,7 @@
#include "integrationplugintcpcommander.h" #include "integrationplugintcpcommander.h"
#include "plugininfo.h" #include "plugininfo.h"
#include "tcpserver.h"
#include <QTimer> #include <QTimer>
@ -58,14 +59,14 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info)
connect(tcpSocket, &QTcpSocket::stateChanged, thing, [=](QAbstractSocket::SocketState state){ connect(tcpSocket, &QTcpSocket::stateChanged, thing, [=](QAbstractSocket::SocketState state){
thing->setStateValue(tcpClientConnectedStateTypeId, state == QAbstractSocket::ConnectedState); thing->setStateValue(tcpClientConnectedStateTypeId, state == QAbstractSocket::ConnectedState);
if (state == QAbstractSocket::UnconnectedState) { if (state == QAbstractSocket::UnconnectedState) {
QTimer::singleShot(10000, tcpSocket, [=](){ QTimer::singleShot(10000, tcpSocket, [tcpSocket, address, port](){
qCDebug(dcTCPCommander()) << "Reconnecting to server" << address << port; qCDebug(dcTCPCommander()) << "Reconnecting to server" << address << port;
tcpSocket->connectToHost(address, port); tcpSocket->connectToHost(address, port);
}); });
} }
}); });
connect(tcpSocket, &QTcpSocket::readyRead, thing, [=](){ connect(tcpSocket, &QTcpSocket::readyRead, thing, [this, thing, tcpSocket](){
QByteArray data = tcpSocket->readAll(); QByteArray data = tcpSocket->readAll();
ParamList params; ParamList params;
params << Param(tcpClientTriggeredEventDataParamTypeId, data); params << Param(tcpClientTriggeredEventDataParamTypeId, data);
@ -91,7 +92,7 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info)
if (tcpServer->isValid()) { if (tcpServer->isValid()) {
m_tcpServers.insert(thing, tcpServer); m_tcpServers.insert(thing, tcpServer);
connect(thing, &Thing::settingChanged, tcpServer, [=](const ParamTypeId &paramTypeId, const QVariant &value){ connect(thing, &Thing::settingChanged, tcpServer, [tcpServer](const ParamTypeId &paramTypeId, const QVariant &value){
if (paramTypeId == tcpServerSettingsConfirmCommandParamTypeId) { if (paramTypeId == tcpServerSettingsConfirmCommandParamTypeId) {
tcpServer->setConfirmCommands(value.toBool()); tcpServer->setConfirmCommands(value.toBool());
} }
@ -106,7 +107,7 @@ void IntegrationPluginTcpCommander::setupThing(ThingSetupInfo *info)
return; return;
} else { } else {
tcpServer->deleteLater(); 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.")); info->finish(Thing::ThingErrorSetupFailed, QT_TR_NOOP("Error opening TCP port."));
return; return;
} }
@ -172,7 +173,7 @@ void IntegrationPluginTcpCommander::onTcpServerConnectionCountChanged(int connec
TcpServer *tcpServer = static_cast<TcpServer *>(sender()); TcpServer *tcpServer = static_cast<TcpServer *>(sender());
Thing *thing = m_tcpServers.key(tcpServer); Thing *thing = m_tcpServers.key(tcpServer);
if (thing && thing->thingClassId() == tcpServerThingClassId) { 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); thing->setStateValue(tcpServerConnectionCountStateTypeId, connections);
} }
} }
@ -181,7 +182,7 @@ void IntegrationPluginTcpCommander::onTcpServerCommandReceived(const QString &cl
{ {
TcpServer *tcpServer = static_cast<TcpServer *>(sender()); TcpServer *tcpServer = static_cast<TcpServer *>(sender());
Thing *thing = m_tcpServers.key(tcpServer); Thing *thing = m_tcpServers.key(tcpServer);
qDebug(dcTCPCommander()) << thing->name() << "Message received" << data; qCDebug(dcTCPCommander()) << thing->name() << "Message received" << data;
ParamList params; ParamList params;
params.append(Param(tcpServerTriggeredEventDataParamTypeId, data)); params.append(Param(tcpServerTriggeredEventDataParamTypeId, data));

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2020, nymea GmbH * Copyright 2013 - 2025, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
@ -31,8 +31,11 @@
#ifndef INTEGRATIONPLUGINDEVTCPCOMMANDER_H #ifndef INTEGRATIONPLUGINDEVTCPCOMMANDER_H
#define INTEGRATIONPLUGINDEVTCPCOMMANDER_H #define INTEGRATIONPLUGINDEVTCPCOMMANDER_H
#include "integrations/integrationplugin.h" #include <integrations/integrationplugin.h>
#include "tcpserver.h"
#include <QTcpServer>
class TcpServer;
class IntegrationPluginTcpCommander : public IntegrationPlugin class IntegrationPluginTcpCommander : public IntegrationPlugin
{ {
@ -51,8 +54,8 @@ public:
void executeAction(ThingActionInfo *info) override; void executeAction(ThingActionInfo *info) override;
private: private:
QHash<Thing*, QTcpSocket*> m_tcpSockets; QHash<Thing *, QTcpSocket *> m_tcpSockets;
QHash<Thing*, TcpServer*> m_tcpServers; QHash<Thing *, TcpServer *> m_tcpServers;
private slots: private slots:
void onTcpSocketConnectionChanged(bool connected); void onTcpSocketConnectionChanged(bool connected);

View File

@ -2,8 +2,6 @@ include(../plugins.pri)
QT += network QT += network
TARGET = $$qtLibraryTarget(nymea_integrationplugintcpcommander)
SOURCES += \ SOURCES += \
integrationplugintcpcommander.cpp \ integrationplugintcpcommander.cpp \
tcpserver.cpp tcpserver.cpp

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2020, nymea GmbH * Copyright 2013 - 2025, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
@ -30,17 +30,17 @@
#include "tcpserver.h" #include "tcpserver.h"
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
#include <QNetworkInterface>
#include <QNetworkInterface>
TcpServer::TcpServer(const QHostAddress address, const quint16 &port, QObject *parent) : TcpServer::TcpServer(const QHostAddress address, const quint16 &port, QObject *parent) :
QObject(parent) QObject(parent)
{ {
m_tcpServer = new QTcpServer(this); m_tcpServer = new QTcpServer(this);
connect(m_tcpServer, &QTcpServer::newConnection, this, &TcpServer::newConnection); 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)) { 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; return;
} }
} }
@ -50,9 +50,9 @@ TcpServer::TcpServer(const quint16 &port, QObject *parent) :
{ {
m_tcpServer = new QTcpServer(this); m_tcpServer = new QTcpServer(this);
connect(m_tcpServer, &QTcpServer::newConnection, this, &TcpServer::newConnection); 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)) { 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; return;
} }
} }
@ -113,7 +113,7 @@ bool TcpServer::sendCommand(const QString &clientIp, const QByteArray &data)
void TcpServer::newConnection() void TcpServer::newConnection()
{ {
qDebug(dcTCPCommander()) << "TCP Server new Connection request"; qCDebug(dcTCPCommander()) << "TCP Server new Connection request";
QTcpSocket *socket = m_tcpServer->nextPendingConnection(); QTcpSocket *socket = m_tcpServer->nextPendingConnection();
socket->flush(); socket->flush();
@ -121,14 +121,18 @@ void TcpServer::newConnection()
emit connectionCountChanged(m_clients.count()); emit connectionCountChanged(m_clients.count());
connect(socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected); connect(socket, &QTcpSocket::disconnected, this, &TcpServer::onDisconnected);
connect(socket, &QTcpSocket::readyRead, this, &TcpServer::readData); 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 // 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))); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
#endif
} }
void TcpServer::onDisconnected() void TcpServer::onDisconnected()
{ {
QTcpSocket *client = qobject_cast<QTcpSocket*>(sender()); QTcpSocket *client = qobject_cast<QTcpSocket*>(sender());
qDebug(dcTCPCommander()) << "TCP client disconnected"; qCDebug(dcTCPCommander()) << "TCP client disconnected";
m_clients.removeAll(client); m_clients.removeAll(client);
emit connectionCountChanged(m_clients.count()); emit connectionCountChanged(m_clients.count());
} }
@ -137,7 +141,7 @@ void TcpServer::readData()
{ {
QTcpSocket *socket = static_cast<QTcpSocket *>(sender()); QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
QByteArray data = socket->readAll(); QByteArray data = socket->readAll();
qDebug(dcTCPCommander()) << "TCP Server data received: " << data; qCDebug(dcTCPCommander()) << "TCP Server data received: " << data;
if (m_confirmCommands) { if (m_confirmCommands) {
socket->write("OK\n"); socket->write("OK\n");
} }
@ -148,5 +152,5 @@ void TcpServer::readData()
void TcpServer::onError(QAbstractSocket::SocketError error) void TcpServer::onError(QAbstractSocket::SocketError error)
{ {
QTcpSocket *socket = static_cast<QTcpSocket *>(sender()); QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
qWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error; qCWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error;
} }