curl code replaced by qt code

This commit is contained in:
2026-04-04 22:43:59 +02:00
parent 5260442aec
commit 0d353b4d61
4 changed files with 79 additions and 40 deletions
+8 -8
View File
@@ -14,10 +14,10 @@ CONFIG -= app_bundle
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
src/main.cpp \ src/main.cpp \
src/mqtt.cpp \ # src/mqtt.cpp \
src/pws2mqtt.cpp \ src/pws2mqtt.cpp \
src/utcicalculator.cpp src/utcicalculator.cpp
# Default rules for deployment. # Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin
@@ -25,10 +25,10 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
HEADERS += \ HEADERS += \
src/barometertrend.h \ src/barometertrend.h \
src/httpserver.h \ src/httpserver.h \
src/mqtt.h \ # src/mqtt.h \
src/pws2mqtt.h \ src/pws2mqtt.h \
src/utcicalculator.h \ src/utcicalculator.h \
src/version.h src/version.h
+12 -12
View File
@@ -7,14 +7,14 @@
#include <QDebug> #include <QDebug>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <QtMqtt> //#include <QtMqtt>
#include <QtMqtt/QMqttClient> //#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttMessage> //#include <QtMqtt/QMqttMessage>
#include <QtMqtt/QMqttSubscription> //#include <QtMqtt/QMqttSubscription>
#include <qhttpserver.h> #include <qhttpserver.h>
#include <QHttpServerResponse> #include <QHttpServerResponse>
#include "pws2mqtt.h" #include "pws2mqtt.h"
#include "mqtt.h" //#include "mqtt.h"
#include "src/barometertrend.h" #include "src/barometertrend.h"
#include "version.h" #include "version.h"
#include <curl/curl.h> #include <curl/curl.h>
@@ -23,8 +23,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#define CLIENT_ID "Client_ID" #define CLIENT_ID "Client_ID"
#define BROKER_ADDRESS "localhost" //#define BROKER_ADDRESS "localhost"
#define MQTT_PORT 1883; //#define MQTT_PORT 1883;
uint debugLevel = ERROR | ALERT; uint debugLevel = ERROR | ALERT;
QByteArray toFollow; QByteArray toFollow;
@@ -35,7 +35,7 @@ using namespace std;
//Configuration config; //Configuration config;
Pws2mqtt *pws2mqtt; Pws2mqtt *pws2mqtt;
MqttClient *mqttClient; //MqttClient *mqttClient;
QHttpServer *httpServer; QHttpServer *httpServer;
BarometerTrend *baro; BarometerTrend *baro;
@@ -51,11 +51,11 @@ int main(int argc, char *argv[])
exit(0); exit(0);
} }
} }
// Enable logging to journald // Enable logging to ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••journald
qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0")); qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0"));
debug(DEBUGMACRO, "declaration of mqttClient", DEBUG); debug(DEBUGMACRO, "declaration of mqttClient", DEBUG);
mqttClient = new MqttClient; //mqttClient = new MqttClient;
httpServer = new QHttpServer; httpServer = new QHttpServer;
@@ -70,8 +70,8 @@ int main(int argc, char *argv[])
//notify (QString("Program started"), "default"); //notify (QString("Program started"), "default");
a.exec(); a.exec();
mqttClient->qmqttClient->unsubscribe(mqttClient->topic); //mqttClient->qmqttClient->unsubscribe(mqttClient->topic);
mqttClient->qmqttClient->disconnectFromHost(); //mqttClient->qmqttClient->disconnectFromHost();
debug(DEBUGMACRO, "exiting", DEBUG); debug(DEBUGMACRO, "exiting", DEBUG);
notify ("Program exiting \n", "high"); notify ("Program exiting \n", "high");
//Closing http server //Closing http server
+56 -17
View File
@@ -1,25 +1,27 @@
#include "pws2mqtt.h" #include "pws2mqtt.h"
#include "barometertrend.h" #include "barometertrend.h"
#include "mqtt.h" //#include "mqtt.h"
//#include "httpserver.h" //#include "httpserver.h"
#include <cstring> #include <cstring>
#include <unistd.h> //#include <unistd.h>
#include <QtGlobal> #include <QtGlobal>
#include <iostream> #include <iostream>
#include <QList> #include <QList>
#include <sys/socket.h> //#include <sys/socket.h>
#include <netinet/in.h> //#include <netinet/in.h>
#include <QtHttpServer/QHttpServer> #include <QtHttpServer/QHttpServer>
#include <QtHttpServer/QHttpServerRequest> #include <QtHttpServer/QHttpServerRequest>
#include <curl/curl.h> #include <curl/curl.h>
#include <QtMath> #include <QtMath>
#include <QImage> #include <QImage>
#include <QTransform> #include <QTransform>
#include <stdio.h> //#include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "utcicalculator.h" #include "utcicalculator.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
extern MqttClient *mqttClient; //extern MqttClient *mqttClient;
extern Pws2mqtt *pws2mqtt; extern Pws2mqtt *pws2mqtt;
extern QHttpServer *httpServer; extern QHttpServer *httpServer;
extern BarometerTrend *baro; extern BarometerTrend *baro;
@@ -119,13 +121,21 @@ void Pws2mqtt::init()
}else }else
{ {
debug(DEBUGMACRO, "Request query :" + request.query().toString() , DEBUG); debug(DEBUGMACRO, "Request query :" + request.query().toString() , DEBUG);
this->parseData(queryList); QMetaObject::invokeMethod(this, [this, query = request.query()]() {
this->parseData(query.queryItems());
}, Qt::QueuedConnection);
//QThreadPool::globalInstance()->start([this, query = request.query()]() {
// this->parseData(query.queryItems());
//});
//this->parseData(queryList);
debug(DEBUGMACRO, "Returning 'success'", DEBUG); debug(DEBUGMACRO, "Returning 'success'", DEBUG);
} }
//mqttClient.send_message(jsonString); //mqttClient.send_message(jsonString);
return QHttpServerResponse("text/plain", "Requête traitée en arrière-plan\n");
return QHttpServerResponse("text/plain", "Success\n"); //return QHttpServerResponse("text/plain", "Success\n");
}); });
for (auto [name, pair]: propertyName.asKeyValueRange()) for (auto [name, pair]: propertyName.asKeyValueRange())
{ {
propertyList[name].append(formatNotifString(pair.first, pair.second, "0")); propertyList[name].append(formatNotifString(pair.first, pair.second, "0"));
@@ -133,6 +143,35 @@ void Pws2mqtt::init()
} }
} }
void notify(QString notif, QString priority, QString inputPath, QString tag)
{
QNetworkAccessManager *manager = new QNetworkAccessManager(pws2mqtt);
QNetworkRequest request(QUrl("http://localhost:81/Meteo"));
request.setRawHeader("Content-Type", "charset=UTF-8; text/markdown");
if (!tag.isEmpty())
{
request.setRawHeader("Tags", tag.toUtf8());
}
request.setRawHeader("Title", "Vidéo");
request.setRawHeader("Priority", priority.toUtf8());
request.setRawHeader("Markdown", "yes");
request.setRawHeader("Config", "/etc/ntfy.client.yml");
request.setRawHeader("Firebase", "no");
QNetworkReply *reply = manager->post(request, notif.toUtf8());
QObject::connect(reply, &QNetworkReply::finished, [reply, &manager]() {
if (reply->error() != QNetworkReply::NoError) {
debug(DEBUGMACRO, "Erreur réseau : " + reply->errorString(), ERROR);
} else {
debug(DEBUGMACRO, "Notification envoyée", DEBUG);
}
reply->deleteLater();
manager->deleteLater();
});
}
void Pws2mqtt::listeningHttp() void Pws2mqtt::listeningHttp()
{ {
//QByteArray data; //QByteArray data;
@@ -150,7 +189,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug(DEBUGMACRO, "Parsing Datas", DEBUG); debug(DEBUGMACRO, "Parsing Datas", DEBUG);
QString jsonString = "{"; QString jsonString = "{";
QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\""; //QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\"";
QString notif = ""; QString notif = "";
QStringList priorityList {"", "min", "low", "default", "High", "urgent"}; QStringList priorityList {"", "min", "low", "default", "High", "urgent"};
QString attachment = ""; QString attachment = "";
@@ -177,12 +216,12 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
if(deviceFlag == false) if(deviceFlag == false)
{ {
deviceFlag = true; deviceFlag = true;
}else }/*else
{ {
deviceString.append(", "); deviceString.append(", ");
} }
deviceString += "\"" + name + "\": "; deviceString += "\"" + name + "\": ";
deviceString += pair.second; deviceString += pair.second;*/
}else }else
{ {
if(propertyFlag == false) if(propertyFlag == false)
@@ -377,7 +416,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
//windChill(propertiesValue["tempf"], propertiesValue["vent"]); //windChill(propertiesValue["tempf"], propertiesValue["vent"]);
//calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]); //calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]);
if (!jsonString.isEmpty()) /*if (!jsonString.isEmpty())
{ {
debug(DEBUGMACRO, "json string : " + jsonString, DEBUG); debug(DEBUGMACRO, "json string : " + jsonString, DEBUG);
jsonString = jsonString +", " + deviceString + "}}"; jsonString = jsonString +", " + deviceString + "}}";
@@ -385,7 +424,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
}else }else
{ {
debug(DEBUGMACRO, "No values to send", DEBUG); debug(DEBUGMACRO, "No values to send", DEBUG);
} }*/
debug(DEBUGMACRO, "current datetime : " + QDateTime::currentDateTime().toString() + ", timer = " + timer.toString(), DEBUG); debug(DEBUGMACRO, "current datetime : " + QDateTime::currentDateTime().toString() + ", timer = " + timer.toString(), DEBUG);
@@ -531,7 +570,7 @@ QString formatNotifString (QString name, QString unit ,QByteArray value)
return text += "\n"; return text += "\n";
} }
static size_t ReadFile(void *ptr, size_t size, size_t nmemb, void *stream) { /*static size_t ReadFile(void *ptr, size_t size, size_t nmemb, void *stream) {
FILE *f = (FILE *)stream; FILE *f = (FILE *)stream;
size_t n = fread(ptr, size, nmemb, f); size_t n = fread(ptr, size, nmemb, f);
return n; return n;
@@ -544,8 +583,8 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::stri
output->append((char*)contents, total_size); output->append((char*)contents, total_size);
return total_size; return total_size;
} }
*/
void notify(QString notif, QString priority, QString inputPath, QString tag) /*void notify(QString notif, QString priority, QString inputPath, QString tag)
{ {
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
@@ -667,7 +706,7 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
curl_global_cleanup(); curl_global_cleanup();
} }
} }
*/
void rotateAndSaveImage(const QString &inputPath, const QString &outputPath, qreal angle) void rotateAndSaveImage(const QString &inputPath, const QString &outputPath, qreal angle)
{ {
QImage image(inputPath); QImage image(inputPath);
+1 -1
View File
@@ -56,7 +56,6 @@ double tohPa(double value);
bool compare (double value=0, double testValue=0, double ecart = 0.5); bool compare (double value=0, double testValue=0, double ecart = 0.5);
QString formatNotifString (QString name, QString unit, QByteArray value=""); QString formatNotifString (QString name, QString unit, QByteArray value="");
double mphTokmh (double value); double mphTokmh (double value);
void notify (QString notif, QString priority = "low", QString inputPath = "", QString tag = "");
quint8 setPriority (quint8 currentPriority, quint8 newPriority); quint8 setPriority (quint8 currentPriority, quint8 newPriority);
QString previsionMeteo(double currentPressure, double variation3h, quint8 &priority); QString previsionMeteo(double currentPressure, double variation3h, quint8 &priority);
QString pressureVariation(double currentPressure, quint8 &priority); QString pressureVariation(double currentPressure, quint8 &priority);
@@ -64,4 +63,5 @@ QString getPluviosite(double value, quint8 &priority);
void rotateAndSaveImage(const QString &inputPath, const QString &outputPath, qreal angle=0); void rotateAndSaveImage(const QString &inputPath, const QString &outputPath, qreal angle=0);
double windChill(double airT, double vent); double windChill(double airT, double vent);
double calculerHumidex(double temperature, double humiditeRelative); double calculerHumidex(double temperature, double humiditeRelative);
void notify (QString notif, QString priority = "low", QString inputPath = "", QString tag = "");