QT6 adaptation
This commit is contained in:
@@ -14,10 +14,10 @@ CONFIG -= app_bundle
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
mqtt.cpp \
|
||||
pws2mqtt.cpp \
|
||||
utcicalculator.cpp
|
||||
src/main.cpp \
|
||||
src/mqtt.cpp \
|
||||
src/pws2mqtt.cpp \
|
||||
src/utcicalculator.cpp
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
@@ -25,9 +25,9 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
||||
|
||||
HEADERS += \
|
||||
httpserver.h \
|
||||
mqtt.h \
|
||||
pws2mqtt.h \
|
||||
utcicalculator.h \
|
||||
version.h
|
||||
src/httpserver.h \
|
||||
src/mqtt.h \
|
||||
src/pws2mqtt.h \
|
||||
src/utcicalculator.h \
|
||||
src/version.h
|
||||
|
||||
|
||||
@@ -3,24 +3,36 @@
|
||||
//#include "httpserver.h"
|
||||
#include <unistd.h>
|
||||
#include <QtGlobal>
|
||||
#include <iostream>
|
||||
//#include <iostream>
|
||||
#include <QList>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <QtHttpServer/QHttpServer>
|
||||
/*#include "qhostaddress.h"
|
||||
#include "qhttpserverrequest.h"
|
||||
#include "qhttpserverresponse.h"
|
||||
#include "qhttpserverrouterrule.h"*/
|
||||
#include <QtHttpServer/QHttpServerRequest>
|
||||
#include <curl/curl.h>
|
||||
#include <QtHttpServer/QHttpServerResponder>
|
||||
#include <QtHttpServer/QHttpServerRouter>
|
||||
#include <QtHttpServer/QHttpServerRouterRule>
|
||||
#include <QtMath>
|
||||
#include <QNetworkInterface>
|
||||
#include <QTcpServer>
|
||||
#include <QImage>
|
||||
#include <QTransform>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include "utcicalculator.h"
|
||||
#include <curl/curl.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern MqttClient *mqttClient;
|
||||
extern Pws2mqtt *pws2mqtt;
|
||||
extern QHttpServer *httpServer;
|
||||
UtciCalculator calc;
|
||||
QTcpServer * tcpServer;
|
||||
|
||||
QStringList previsionList
|
||||
{
|
||||
@@ -34,15 +46,6 @@ QStringList previsionList
|
||||
"Temps stable"
|
||||
};
|
||||
|
||||
// Callback for curl library
|
||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
(void)contents; // Évite les warnings de compilation
|
||||
(void)userp;
|
||||
return size * nmemb; // On ne traite pas les données reçues
|
||||
}
|
||||
|
||||
|
||||
QMap <QByteArray, QString> propertyList;
|
||||
QMap <QByteArray, QString> precPropertyList;
|
||||
QMap <QByteArray, double> propertiesValue;
|
||||
@@ -93,20 +96,46 @@ Pws2mqtt::~Pws2mqtt()
|
||||
{
|
||||
}
|
||||
|
||||
// Callback for curl library
|
||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
(void)contents; // Évite les warnings de compilation
|
||||
(void)userp;
|
||||
return size * nmemb; // On ne traite pas les données reçues
|
||||
}
|
||||
|
||||
void missingHandler(const QHttpServerRequest &request, QHttpServerResponder &responder)
|
||||
{
|
||||
QString message = QString("404 Not Found: %1 n'existe pas").arg(request.url().path());
|
||||
responder.sendResponse(QHttpServerResponse("text/plain; charset=utf-8", message.toUtf8(), QHttpServerResponder::StatusCode::NotFound));
|
||||
}
|
||||
|
||||
void Pws2mqtt::init()
|
||||
{
|
||||
QHostInfo info;
|
||||
// QHostAddress address;
|
||||
QList <QHostAddress> addresses;
|
||||
// QHttpServerRouterRule * status;
|
||||
|
||||
debug(DEBUGMACRO, "init http server", DEBUG);
|
||||
httpServer->setMissingHandler([](const QHttpServerRequest &request, QHttpServerResponder &&responder)
|
||||
addresses = QNetworkInterface::allAddresses();
|
||||
for( const auto &i: as_const(addresses))
|
||||
{
|
||||
(void) responder;
|
||||
debug(DEBUGMACRO, "body " + request.url().toString(), WARNING);
|
||||
return QHttpServerResponse("404 - Page non trouvée. Route par défaut.", QHttpServerResponse::StatusCode::NotFound);
|
||||
//responder.write(QHttpServerResponse("404 - Page non trouvée. Route par défaut.", QHttpServerResponse::StatusCode::NotFound));
|
||||
});
|
||||
if (!i.isLoopback() and !i.isNull())
|
||||
{
|
||||
this->localAddress = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
debug(DEBUGMACRO, "Locale address : " + this->localAddress.toString (), INFO);
|
||||
|
||||
// server's routes
|
||||
|
||||
httpServer->setMissingHandler(httpServer, missingHandler);
|
||||
|
||||
httpServer->route("/", [](const QHttpServerRequest &request)
|
||||
{
|
||||
QList<std::pair<QByteArray, QByteArray>> headersList = request.headers().toList();
|
||||
QList<std::pair<QByteArray, QByteArray>> headersList = request.headers().toListOfPairs();
|
||||
debug(DEBUGMACRO, "Remote address" + request.remoteAddress().toString(), WARNING);
|
||||
return QHttpServerResponse("text/plain", "Failed\n");
|
||||
});
|
||||
@@ -134,14 +163,23 @@ void Pws2mqtt::init()
|
||||
|
||||
void Pws2mqtt::listeningHttp()
|
||||
{
|
||||
//QByteArray data;
|
||||
debug (DEBUGMACRO, "listening http requests", DEBUG);
|
||||
const auto port = httpServer->listen(QHostAddress::Any, 5000);
|
||||
if (!port)
|
||||
tcpServer = new QTcpServer(); //httpServer.server.listen(QHostAddress::Any, this->port);
|
||||
if (!tcpServer->listen(QHostAddress::LocalHost, 8001))
|
||||
{
|
||||
debug(DEBUGMACRO, "Http Server failed to listen on a port.", ERROR);
|
||||
delete tcpServer;
|
||||
debug(DEBUGMACRO, "TCPServer failed to listen on a port.", ERROR);
|
||||
//return 0;
|
||||
}else
|
||||
{
|
||||
if (!httpServer->bind(tcpServer))
|
||||
{
|
||||
debug(DEBUGMACRO, "Binding tcpserver to httpserver failed ", ERROR);
|
||||
}else
|
||||
{
|
||||
debug(DEBUGMACRO, "Listening on port " + QString::number(tcpServer->serverPort ()), INFO);
|
||||
}
|
||||
}
|
||||
debug(DEBUGMACRO, "Listening on port " + QString::number(port));
|
||||
}
|
||||
|
||||
void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#include <QObject>
|
||||
#include <QtMqtt>
|
||||
#include <QtMqtt/QMqttClient>
|
||||
#include <QTcpServer>
|
||||
#include "qhostaddress.h"
|
||||
|
||||
|
||||
#define RED "\e[31m"
|
||||
#define GREEN "\e[32m"
|
||||
@@ -36,6 +39,7 @@ class Pws2mqtt : public QObject
|
||||
QString ProcName = "pws2mqtt"; // name of the proceesus in ps, top, pstree, ...;
|
||||
FILE * logFh = nullptr;
|
||||
QString listenHost = "0.0.0.0";
|
||||
QHostAddress localAddress;
|
||||
uint listenPort = 5000;
|
||||
int sockfd = 0;
|
||||
int newsockfd = 0;
|
||||
|
||||
Reference in New Issue
Block a user