tcpcommander: Add Qt6 support
parent
09b0855045
commit
4609b79a07
|
|
@ -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 <QTimer>
|
||||
|
||||
|
|
@ -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<TcpServer *>(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<TcpServer *>(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));
|
||||
|
|
|
|||
|
|
@ -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 <integrations/integrationplugin.h>
|
||||
|
||||
#include <QTcpServer>
|
||||
|
||||
class TcpServer;
|
||||
|
||||
class IntegrationPluginTcpCommander : public IntegrationPlugin
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@ include(../plugins.pri)
|
|||
|
||||
QT += network
|
||||
|
||||
TARGET = $$qtLibraryTarget(nymea_integrationplugintcpcommander)
|
||||
|
||||
SOURCES += \
|
||||
integrationplugintcpcommander.cpp \
|
||||
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 <QNetworkInterface>
|
||||
|
||||
#include <QNetworkInterface>
|
||||
|
||||
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<QTcpSocket*>(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<QTcpSocket *>(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<QTcpSocket *>(sender());
|
||||
qWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error;
|
||||
qCWarning(dcTCPCommander()) << "Socket Error" << socket->errorString() << error;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue