httpcommander: Add Qt6 support

This commit is contained in:
Simon Stürz 2025-08-08 13:53:44 +02:00
parent 233b89d65e
commit 6091d24574
5 changed files with 30 additions and 37 deletions

View File

@ -1,8 +1,6 @@
include(../plugins.pri) include(../plugins.pri)
QT += network QT *= network
TARGET = $$qtLibraryTarget(nymea_integrationpluginhttpcommander)
SOURCES += \ SOURCES += \
integrationpluginhttpcommander.cpp \ integrationpluginhttpcommander.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,14 +30,13 @@
#include "httpsimpleserver.h" #include "httpsimpleserver.h"
#include "types/statetype.h"
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
#include <QTcpSocket> #include <QTcpSocket>
#include <QDebug> #include <QDebug>
#include <QDateTime> #include <QDateTime>
#include <QUrlQuery> #include <QUrlQuery>
#include <QRegExp> #include <QRegularExpression>
#include <QStringList> #include <QStringList>
HttpSimpleServer::HttpSimpleServer(quint16 port, QObject *parent): HttpSimpleServer::HttpSimpleServer(quint16 port, QObject *parent):
@ -58,8 +57,8 @@ void HttpSimpleServer::incomingConnection(qintptr socket)
// works asynchronously, this means that all the communication is done // works asynchronously, this means that all the communication is done
// in the two slots readClient() and discardClient(). // in the two slots readClient() and discardClient().
QTcpSocket* tcpSocket = new QTcpSocket(this); QTcpSocket* tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readClient())); connect(tcpSocket, &QTcpSocket::readyRead, this, &HttpSimpleServer::readClient);
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(discardClient())); connect(tcpSocket, &QTcpSocket::disconnected, this, &HttpSimpleServer::discardClient);
tcpSocket->setSocketDescriptor(socket); tcpSocket->setSocketDescriptor(socket);
} }
@ -69,17 +68,16 @@ void HttpSimpleServer::readClient()
// This slot is called when the client sent data to the server. The // This slot is called when the client sent data to the server. The
// server looks if it was a get request and sends a very simple HTML // server looks if it was a get request and sends a very simple HTML
// document back. // document back.
QTcpSocket* tcpSocket = static_cast<QTcpSocket*>(sender()); QTcpSocket *tcpSocket = static_cast<QTcpSocket*>(sender());
if (tcpSocket->canReadLine()) { if (tcpSocket->canReadLine()) {
QByteArray data = tcpSocket->readAll(); QByteArray data = tcpSocket->readAll();
QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*")); QStringList tokens = QString(data).split(QRegularExpression("[ \r\n][ \r\n]*"));
qCDebug(dcHttpCommander()) << "Http Request, type" << tokens[0] << "path" << tokens[1] << "body" << tokens.last(); qCDebug(dcHttpCommander()) << "Http Request, type" << tokens[0] << "path" << tokens[1] << "body" << tokens.last();
if ((tokens[0] == "GET") || if ((tokens[0] == "GET") ||
(tokens[0] == "PUT") || (tokens[0] == "PUT") ||
(tokens[0] == "POST") || (tokens[0] == "POST") ||
(tokens[0] == "DELETE")) { (tokens[0] == "DELETE")) {
QTextStream os(tcpSocket); QTextStream os(tcpSocket);
os.setAutoDetectUnicode(true); os.setAutoDetectUnicode(true);
@ -96,16 +94,16 @@ void HttpSimpleServer::readClient()
void HttpSimpleServer::discardClient() void HttpSimpleServer::discardClient()
{ {
QTcpSocket* socket = static_cast<QTcpSocket*>(sender()); QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
socket->deleteLater(); socket->deleteLater();
} }
QString HttpSimpleServer::generateHeader() QString HttpSimpleServer::generateHeader()
{ {
QString contentHeader( QString contentHeader(
"HTTP/1.0 200 Ok\r\n" "HTTP/1.0 200 Ok\r\n"
"Content-Type: text/html; charset=\"utf-8\"\r\n" "Content-Type: text/html; charset=\"utf-8\"\r\n"
"\r\n" "\r\n"
); );
return contentHeader; return contentHeader;
} }

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,15 +31,12 @@
#ifndef HTTPSIMPLESERVER1_H #ifndef HTTPSIMPLESERVER1_H
#define HTTPSIMPLESERVER1_H #define HTTPSIMPLESERVER1_H
#include "typeutils.h"
#include <QTcpServer> #include <QTcpServer>
#include <QUuid> #include <QUuid>
#include <QDateTime> #include <QDateTime>
#include <QUrl> #include <QUrl>
class Device; #include <typeutils.h>
class DevicePlugin;
class HttpSimpleServer : public QTcpServer class HttpSimpleServer : public QTcpServer
{ {
@ -48,6 +45,7 @@ public:
HttpSimpleServer(quint16 port, QObject* parent = nullptr); HttpSimpleServer(quint16 port, QObject* parent = nullptr);
~HttpSimpleServer() override; ~HttpSimpleServer() override;
void incomingConnection(qintptr socket) override; void incomingConnection(qintptr socket) override;
signals: signals:

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.
@ -29,8 +29,12 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "integrationpluginhttpcommander.h" #include "integrationpluginhttpcommander.h"
#include "network/networkaccessmanager.h"
#include "plugininfo.h" #include "plugininfo.h"
#include <network/networkaccessmanager.h>
#include <QHostInfo>
#include <QNetworkReply>
#include <QNetworkInterface> #include <QNetworkInterface>
IntegrationPluginHttpCommander::IntegrationPluginHttpCommander() IntegrationPluginHttpCommander::IntegrationPluginHttpCommander()
@ -40,14 +44,14 @@ IntegrationPluginHttpCommander::IntegrationPluginHttpCommander()
void IntegrationPluginHttpCommander::setupThing(ThingSetupInfo *info) void IntegrationPluginHttpCommander::setupThing(ThingSetupInfo *info)
{ {
Thing *thing = info->thing(); Thing *thing = info->thing();
qDebug(dcHttpCommander()) << "Setup thing" << thing->name() << thing->params(); qCDebug(dcHttpCommander()) << "Setup thing" << thing->name() << thing->params();
if (thing->thingClassId() == httpRequestThingClassId) { if (thing->thingClassId() == httpRequestThingClassId) {
QUrl url = thing->paramValue(httpRequestThingUrlParamTypeId).toUrl(); QUrl url = thing->paramValue(httpRequestThingUrlParamTypeId).toUrl();
if (!url.isValid()) { if (!url.isValid()) {
qDebug(dcHttpCommander()) << "Given URL is not valid"; qCDebug(dcHttpCommander()) << "Given URL is not valid";
//: Error setting up thing //: Error setting up thing
return info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The given url is not valid.")); return info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The given url is not valid."));
} }
@ -92,9 +96,9 @@ void IntegrationPluginHttpCommander::executeAction(ThingActionInfo *info)
info->finish(Thing::ThingErrorInvalidParameter); info->finish(Thing::ThingErrorInvalidParameter);
return; return;
} }
connect(reply, &QNetworkReply::finished, this, [thing, reply, this](){
qDebug(dcHttpCommander()) << "POST reply finished"; connect(reply, &QNetworkReply::finished, this, [thing, reply](){
qCDebug(dcHttpCommander()) << "POST reply finished";
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

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,13 +31,9 @@
#ifndef INTEGRATIONPLUGINHTTPCOMMANDER_H #ifndef INTEGRATIONPLUGINHTTPCOMMANDER_H
#define INTEGRATIONPLUGINHTTPCOMMANDER_H #define INTEGRATIONPLUGINHTTPCOMMANDER_H
#include "integrations/integrationplugin.h" #include <integrations/integrationplugin.h>
#include "plugintimer.h"
#include "httpsimpleserver.h" #include "httpsimpleserver.h"
#include <QNetworkReply>
#include <QHostInfo>
class IntegrationPluginHttpCommander : public IntegrationPlugin class IntegrationPluginHttpCommander : public IntegrationPlugin
{ {
Q_OBJECT Q_OBJECT
@ -46,7 +42,6 @@ class IntegrationPluginHttpCommander : public IntegrationPlugin
Q_INTERFACES(IntegrationPlugin) Q_INTERFACES(IntegrationPlugin)
public: public:
explicit IntegrationPluginHttpCommander(); explicit IntegrationPluginHttpCommander();
void setupThing(ThingSetupInfo *info) override; void setupThing(ThingSetupInfo *info) override;