From 0d353b4d61c494312067701061bce6d08c64c8fa Mon Sep 17 00:00:00 2001 From: Daniel TARTAVEL Date: Sat, 4 Apr 2026 22:43:59 +0200 Subject: [PATCH] curl code replaced by qt code --- pws2mqtt-qt.pro | 20 ++++++------- src/main.cpp | 24 ++++++++-------- src/pws2mqtt.cpp | 73 +++++++++++++++++++++++++++++++++++++----------- src/pws2mqtt.h | 2 +- 4 files changed, 79 insertions(+), 40 deletions(-) diff --git a/pws2mqtt-qt.pro b/pws2mqtt-qt.pro index 5f83235..5e5d34b 100644 --- a/pws2mqtt-qt.pro +++ b/pws2mqtt-qt.pro @@ -14,10 +14,10 @@ CONFIG -= app_bundle #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ - src/main.cpp \ - src/mqtt.cpp \ - src/pws2mqtt.cpp \ - src/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,10 +25,10 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ - src/barometertrend.h \ - src/httpserver.h \ - src/mqtt.h \ - src/pws2mqtt.h \ - src/utcicalculator.h \ - src/version.h +src/barometertrend.h \ +src/httpserver.h \ +# src/mqtt.h \ +src/pws2mqtt.h \ + src/utcicalculator.h \ + src/version.h diff --git a/src/main.cpp b/src/main.cpp index c7665c7..43e6adf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,14 +7,14 @@ #include #include #include -#include -#include -#include -#include +//#include +//#include +//#include +//#include #include #include #include "pws2mqtt.h" -#include "mqtt.h" +//#include "mqtt.h" #include "src/barometertrend.h" #include "version.h" #include @@ -23,8 +23,8 @@ #include #define CLIENT_ID "Client_ID" -#define BROKER_ADDRESS "localhost" -#define MQTT_PORT 1883; +//#define BROKER_ADDRESS "localhost" +//#define MQTT_PORT 1883; uint debugLevel = ERROR | ALERT; QByteArray toFollow; @@ -35,7 +35,7 @@ using namespace std; //Configuration config; Pws2mqtt *pws2mqtt; -MqttClient *mqttClient; +//MqttClient *mqttClient; QHttpServer *httpServer; BarometerTrend *baro; @@ -51,11 +51,11 @@ int main(int argc, char *argv[]) exit(0); } } - // Enable logging to journald + // Enable logging to ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••journald qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0")); debug(DEBUGMACRO, "declaration of mqttClient", DEBUG); - mqttClient = new MqttClient; + //mqttClient = new MqttClient; httpServer = new QHttpServer; @@ -70,8 +70,8 @@ int main(int argc, char *argv[]) //notify (QString("Program started"), "default"); a.exec(); - mqttClient->qmqttClient->unsubscribe(mqttClient->topic); - mqttClient->qmqttClient->disconnectFromHost(); + //mqttClient->qmqttClient->unsubscribe(mqttClient->topic); + //mqttClient->qmqttClient->disconnectFromHost(); debug(DEBUGMACRO, "exiting", DEBUG); notify ("Program exiting \n", "high"); //Closing http server diff --git a/src/pws2mqtt.cpp b/src/pws2mqtt.cpp index abd670e..5cbdc99 100644 --- a/src/pws2mqtt.cpp +++ b/src/pws2mqtt.cpp @@ -1,25 +1,27 @@ #include "pws2mqtt.h" #include "barometertrend.h" -#include "mqtt.h" +//#include "mqtt.h" //#include "httpserver.h" #include -#include +//#include #include #include #include -#include -#include +//#include +//#include #include #include #include #include #include #include -#include +//#include #include #include "utcicalculator.h" +#include +#include -extern MqttClient *mqttClient; +//extern MqttClient *mqttClient; extern Pws2mqtt *pws2mqtt; extern QHttpServer *httpServer; extern BarometerTrend *baro; @@ -119,13 +121,21 @@ void Pws2mqtt::init() }else { 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); } //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()) { 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() { //QByteArray data; @@ -150,7 +189,7 @@ void Pws2mqtt::parseData(QList> queryList) debug(DEBUGMACRO, "Parsing Datas", DEBUG); 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 = ""; QStringList priorityList {"", "min", "low", "default", "High", "urgent"}; QString attachment = ""; @@ -177,12 +216,12 @@ void Pws2mqtt::parseData(QList> queryList) if(deviceFlag == false) { deviceFlag = true; - }else + }/*else { deviceString.append(", "); } deviceString += "\"" + name + "\": "; - deviceString += pair.second; + deviceString += pair.second;*/ }else { if(propertyFlag == false) @@ -377,7 +416,7 @@ void Pws2mqtt::parseData(QList> queryList) //windChill(propertiesValue["tempf"], propertiesValue["vent"]); //calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]); - if (!jsonString.isEmpty()) + /*if (!jsonString.isEmpty()) { debug(DEBUGMACRO, "json string : " + jsonString, DEBUG); jsonString = jsonString +", " + deviceString + "}}"; @@ -385,7 +424,7 @@ void Pws2mqtt::parseData(QList> queryList) }else { debug(DEBUGMACRO, "No values to send", 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"; } -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; size_t n = fread(ptr, size, nmemb, f); 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); 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; CURLcode res; @@ -667,7 +706,7 @@ void notify(QString notif, QString priority, QString inputPath, QString tag) curl_global_cleanup(); } } - +*/ void rotateAndSaveImage(const QString &inputPath, const QString &outputPath, qreal angle) { QImage image(inputPath); diff --git a/src/pws2mqtt.h b/src/pws2mqtt.h index dc05356..59bfd12 100644 --- a/src/pws2mqtt.h +++ b/src/pws2mqtt.h @@ -56,7 +56,6 @@ double tohPa(double value); bool compare (double value=0, double testValue=0, double ecart = 0.5); QString formatNotifString (QString name, QString unit, QByteArray value=""); double mphTokmh (double value); -void notify (QString notif, QString priority = "low", QString inputPath = "", QString tag = ""); quint8 setPriority (quint8 currentPriority, quint8 newPriority); QString previsionMeteo(double currentPressure, double variation3h, 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); double windChill(double airT, double vent); double calculerHumidex(double temperature, double humiditeRelative); +void notify (QString notif, QString priority = "low", QString inputPath = "", QString tag = "");