mqtt en option de complilation

This commit is contained in:
2026-04-12 21:16:24 +02:00
parent 60327e11de
commit b3fe788121
4 changed files with 90 additions and 69 deletions

View File

@@ -26,9 +26,9 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
HEADERS += \
src/barometertrend.h \
src/httpserver.h \
#src/httpserver.h \
# src/mqtt.h \
src/pws2mqtt.h \
src/utcicalculator.h \
src/utcicalculator.h \
src/version.h

View File

@@ -1,11 +1,9 @@
#include <QCoreApplication>
#include <QString>
#include <unistd.h>
//#include <unistd.h>
#include <QtGlobal>
#include <iostream>
#include <QDebug>
#include <sys/socket.h>
#include <netinet/in.h>
//#include <iostream>
//#include <QDebug>
#include <qhttpserver.h>
#include <QHttpServerResponse>
#include "pws2mqtt.h"
@@ -14,9 +12,8 @@
#endif
#include "src/barometertrend.h"
#include "version.h"
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
//#include <sys/types.h>
//#include <sys/select.h>
#ifdef WITH_MQTT
#include <QtMqtt>
#include <QtMqtt/QMqttClient>
@@ -55,8 +52,8 @@ int main(int argc, char *argv[])
// Enable logging to journald
qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0"));
debug(DEBUGMACRO, "declaration of mqttClient", INFO);
#ifdef WITH_MQTT
debug(DEBUGMACRO, "declaration of mqttClient", INFO);
mqttClient = new MqttClient;
#endif
httpServer = new QHttpServer;

View File

@@ -1,16 +1,15 @@
#include "pws2mqtt.h"
#include "barometertrend.h"
#include <cstring>
//#include <cstring>
#include <QtGlobal>
#include <iostream>
//#include <iostream>
#include <QList>
#include <QtHttpServer/QHttpServer>
#include <QtHttpServer/QHttpServerRequest>
#include <curl/curl.h>
#include <QtMath>
#include <QImage>
#include <QTransform>
#include <sys/stat.h>
//#include <sys/stat.h>
#include "utcicalculator.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
@@ -59,7 +58,7 @@ QMap <QByteArray, QPair<QString, QByteArray>> propertyName
// {"indoorhumidity", {"Humidité intérieure", "%"}},
{"baromin", {"Pression atmosphérique", " hPa"}},
// {"lowbatt", {"Alerte batterie faible", ""}},
{"UV", {"Alerte UV", ""}}
{"UV", {"UV", ""}}
};
QList <QList <QVariant>> forceVent
@@ -119,9 +118,10 @@ void Pws2mqtt::init()
{
debug(DEBUGMACRO, "Request query :" + request.query().toString() , INFO
);
QMetaObject::invokeMethod(this, [this, query = request.query()]() {
/* QMetaObject::invokeMethod(this, [this, query = request.query()]() {
this->parseData(query.queryItems());
}, Qt::QueuedConnection);
}, Qt::QueuedConnection);*/
parseData(request.query().queryItems());
debug(DEBUGMACRO, "Returning 'success'", DEBUG);
}
//mqttClient.send_message(jsonString);
@@ -132,7 +132,7 @@ void Pws2mqtt::init()
for (auto [name, pair]: propertyName.asKeyValueRange())
{
propertyList[name].append(formatNotifString(pair.first, pair.second, "0"));
propertyList[name].append(formatNotifString(pair, "0"));
debug (DEBUGMACRO, "Init " + name + " => " + propertyList[name], DEBUG);
}
}
@@ -140,7 +140,7 @@ void Pws2mqtt::init()
void notify(QString notif, QString priority, QString inputPath, QString tag)
{
(void) inputPath;
QNetworkAccessManager *manager = new QNetworkAccessManager(pws2mqtt);
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest request(QUrl("http://localhost:81/Meteo"));
request.setRawHeader("Content-Type", "charset=UTF-8; text/markdown");
if (!tag.isEmpty())
@@ -153,17 +153,19 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
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) {
QObject::connect(reply, &QNetworkReply::finished, [reply]()
{
if (reply->error() != QNetworkReply::NoError)
{
debug(DEBUGMACRO, "Erreur réseau : " + reply->errorString(), ERROR);
} else {
} else
{
debug(DEBUGMACRO, "Notification envoyée", DEBUG);
}
reply->deleteLater();
manager->deleteLater();
});
delete(manager);
}
@@ -196,7 +198,6 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
bool deviceFlag = false;
static QDateTime timer = QDateTime::currentDateTime().addSecs(-2000);
static QHash <QByteArray, quint8> priority = {};
static QHash <QByteArray, quint8> announced = {};
static bool start = false;
debug(DEBUGMACRO, "looping list of query", DEBUG);
@@ -247,7 +248,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
//notif += formatNotifString (propertyName[name].first, propertyName[name].second, QByteArray::number(qPow(propertyValue, 1.0)));
//debug (DEBUGMACRO, "", DEBUG);
timeTemp = timeTemp.currentDateTime().addSecs(300);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(propertyName[name], QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
if (propertyValue > 35)
{
@@ -265,7 +266,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
if (compare (propertiesValue[name], propertyValue, 3))
{
//notif += formatNotifString (propertyName[name].first, propertyName[name].second , value);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(propertyName[name], QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
}else if (name == "winddir")
@@ -273,7 +274,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
propertyValue = value.toFloat();
rotateAndSaveImage(this->inputPath, this->outputPath, (qreal)propertyValue);
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertyValue), DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(propertyName[name], QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}else if (name == "windspeedmph")
{
@@ -296,7 +297,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
}
}
propertyList[name] = formatNotifString ("Vent - " + msg, propertyName[name].second , QByteArray::number(propertiesValue[name]));
propertyList[name] = "- Vent - " + msg + " : " + QByteArray::number(propertiesValue[name]) + "Km/h" + "\n";
propertiesValue["forcevent"] = i;
if (i >= 5)
{
@@ -317,7 +318,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertiesValue[name]), DEBUG);
propertyList[name] = formatNotifString (propertyName[name].first, propertyName[name].second , QByteArray::number(propertiesValue[name]));
propertyList[name] = formatNotifString (propertyName[name], QByteArray::number(propertiesValue[name]));
if (propertiesValue[name] > 50)
{
@@ -342,7 +343,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
QString pluviosite = getPluviosite(propertyValue, raininPriority);
priority[name] = setPriority (priority[name], raininPriority);
//debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(pluviosite, propertyName[name].second, QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(propertyName[name], QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
@@ -362,7 +363,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
{
//priority[name] = setPriority (priority[name], 3);
//debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(propertyName[name], QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
@@ -372,7 +373,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
propertyValue = round(tohPa(pair.second.toFloat()) * 100) / 100;
debug (DEBUGMACRO, "Barometre en hPa : " + QByteArray::number(propertyValue), DEBUG);
baro->addPressure(propertyValue);
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(propertyName[name], QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
static QString prevision;
@@ -388,6 +389,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "UV")
{
QString msg;
static QDateTime timeUV = QDateTime::currentDateTime().addSecs(-600);
propertyValue = pair.second.toUInt();
@@ -402,20 +404,20 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
timeUV = timeUV.currentDateTime().addSecs(300);
propertiesValue[name] = propertyValue;
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
propertyList[name] = "- UV - " + QByteArray::number(propertyValue) + "\n";
}
debug (DEBUGMACRO, "priority = " + QString::number(priority[name]), DEBUG);
}else if (name == "solarradiation")
{
propertyValue = pair.second.toFloat()*100/100;
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(propertyName[name], QByteArray::number(propertyValue));
propertiesValue[name] = round(pair.second.toFloat()*100/100);
debug (DEBUGMACRO, "SolarRadiations = " + QByteArray::number(propertyValue), DEBUG);
}
}
}
propertiesValue["windchill"] = calc.computeUTCI(propertiesValue["tempf"], propertiesValue["vent"], propertiesValue["solarradiation"], propertiesValue["humidity"]);
propertyList["windchill"] = formatNotifString(propertyName["windchill"].first, propertyName["windchill"].second, QByteArray::number(propertiesValue["windchill"]));
propertyList["windchill"] = formatNotifString(propertyName["windchill"], QByteArray::number(propertiesValue["windchill"]));
//windChill(propertiesValue["tempf"], propertiesValue["vent"]);
//calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]);
@@ -439,46 +441,67 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug(DEBUGMACRO, "looping to fill notif", DEBUG);
for (auto [name, value]: propertyList.asKeyValueRange())
{
if (precPropertyList.contains(name))
{
if (precPropertyList[name] != propertyList[name])
{
if (announced.contains(name) and priority.contains(name))
{
if (announced[name] != priority[name])
{
debug(DEBUGMACRO, "changed = true", DEBUG);
announced[name] = priority[name];
//changed = true;
aPriority = qMax(aPriority, priority[name]);
}
}
}
}
debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG);
if (name != "prevision")
{
notif += propertyList[name];
}
}
QTime currentTime = QTime::currentTime();
if (currentTime.minute() == 0 or start == false or aPriority >= 3)
if (currentTime.minute() == 0 or start == false)
{
timer = QDateTime::currentDateTime();
start = true;
notif = listProperties2notify (propertyList, priority, 0);
notify (notif, priorityList[aPriority], attachment, tag);
precPropertyList = propertyList;
debug(DEBUGMACRO, "calling notify with notif = #" + notif + "#", INFO);
notif = "**Prévisions à 12/24h**\n" + propertyList["prevision"];
notify (notif, priorityList[aPriority], "", tag);
debug(DEBUGMACRO, "calling notify with notif = #" + notif + "#", INFO);
}
if (aPriority >= 3)
{
notif = "**ALERTES**\n" + listProperties2notify (propertyList, priority, 3);
precPropertyList = propertyList;
notify (notif, priorityList[aPriority], attachment, tag);
}
debug(DEBUGMACRO, "parseData: Returning", DEBUG);
}
QString listProperties2notify (QMap <QByteArray, QString> propertyList, QHash <QByteArray, quint8> priority, quint8 minAlert)
{
static QHash <QByteArray, quint8> announced = {};
QString notif = "";
for (auto [name, value]: propertyList.asKeyValueRange())
{
if (name != "prevision")
{
//if (precPropertyList.contains(name))
//{
//if (precPropertyList[name] != propertyList[name])
//{
if (minAlert == 0)
{
notif += propertyList[name];
}else if (priority[name] >= minAlert)
{
if (announced.contains(name) and priority.contains(name))
{
if (announced[name] != priority[name])
{
notif += propertyList[name];
announced[name] = priority[name];
}else
{
debug (DEBUGMACRO, name + " : déjà annoncé = " + announced[name] + " nouvelle valeur = " + priority[name], INFO);
}
}
}
//}
//}
debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG);
}
}
return notif;
}
double calculerUTCI(double temperature, double humiditeRelative, double vitesseVent, double rayonnement)
{
double e = (humiditeRelative / 100.0) * 6.105 * exp((17.27 * temperature) / (237.7 + temperature));
@@ -565,12 +588,12 @@ bool compare (double value, double currentValue, double ecart)
}
}
QString formatNotifString (QString name, QString unit ,QByteArray value)
QString formatNotifString (QPair<QString, QByteArray> property ,QByteArray value)
{
QString text = "- " + name + " : " + value;
if (!unit.isEmpty())
QString text = "- " + property.first + " : " + value;
if (!property.second.isEmpty())
{
text += unit;
text += property.second;
//"- " + propertyName[name].first + " : " + value + " " + propertyName[name].second + " ";
}
return text += "\n";

View File

@@ -5,7 +5,7 @@
#include <QObject>
#include <QtMqtt>
#include <QtMqtt/QMqttClient>
#include <cstdint>
//#include <cstdint>
#define RED "\e[31m"
#define GREEN "\e[32m"
@@ -54,7 +54,7 @@ class Pws2mqtt : public QObject
double fahrenheitToCelsius(double fahrenheit);
double tohPa(double value);
bool compare (double value=0, double testValue=0, double ecart = 0.5);
QString formatNotifString (QString name, QString unit, QByteArray value="");
QString formatNotifString (QPair<QString, QByteArray> property ,QByteArray value);
double mphTokmh (double value);
quint8 setPriority (quint8 currentPriority, quint8 newPriority);
QString previsionMeteo(double currentPressure, double variation3h, quint8 &priority);
@@ -64,4 +64,5 @@ void rotateAndSaveImage(const QString &inputPath, const QString &outputPath, qre
double windChill(double airT, double vent);
double calculerHumidex(double temperature, double humiditeRelative);
void notify (QString notif, QString priority = "low", QString inputPath = "", QString tag = "");
QString listProperties2notify (QMap <QByteArray, QString> propertyList, QHash <QByteArray, quint8> priority, quint8 minAlert = 0);