correction de notify()

This commit is contained in:
DTux committed 2026-04-13 16:34:08 +02:00
1 parent f80313eaa5
commit 40c4c7fae3
1 file changed
+159 -160
+159 -160
View File
@@ -5,12 +5,12 @@
#include <cstring>
#include <unistd.h>
#include <QtGlobal>
#include <iostream>
#include <QList>
#include <sys/socket.h>
#include <netinet/in.h>
#include <QtHttpServer/QHttpServer>
#include <QtHttpServer/QHttpServerRequest>
#include <QHttpServerResponse>
#include <curl/curl.h>
#include <QtMath>
#include <QImage>
@@ -47,9 +47,9 @@ QMap <QByteArray, QPair<QString, QByteArray>> propertyName
{"tempf", {"Température extérieure", "°C"}},
{"humidity", {"Humidité extérieure", "%"}},
{"dewptf", {"Point de rosée", "°C"}},
{"windchill", {"Température ressentie", "°C"}},
{"windchill", {"Température ressentie", "°C"}},
{"winddir", {"Direction du vent", "°"}},
{"windspeedmph", {"Vitesse du vent", " km/h"}},
{"windspeedmph", {"Vitesse du vent", " km/h"}},
{"windgustmph", {"Rafales", " km/h"}},
{"rainin", {"Pluie", " mm/h"}},
{"dailyrainin", {"Pluie de la journée", " mm"}},
@@ -58,7 +58,7 @@ QMap <QByteArray, QPair<QString, QByteArray>> propertyName
// {"solarradiation", {"Énergie solaire", " W/m²"}},
// {"indoortempf", {"Température intérieure", "°C"}},
// {"indoorhumidity", {"Humidité intérieure", "%"}},
{"baromin", {"Pression atmosphérique", " hPa"}},
{"baromin", {"Pression atmosphérique", " hPa"}},
// {"lowbatt", {"Alerte batterie faible", ""}},
{"UV", {"Alerte UV", ""}}
};
@@ -74,7 +74,7 @@ QList <QList <QVariant>> forceVent
{38, "Bonne brise", 1},
{49, "Vent frais", 3},
{61, "Grand vent", 4},
{74, "Vent fort", 4},
{74, "Vent fort", 4},
{88, "Vent violent", 5},
{117, "Tempête", 5},
{1000, "Ouragan", 5}
@@ -92,17 +92,15 @@ Pws2mqtt::~Pws2mqtt()
void Pws2mqtt::init()
{
debug(DEBUGMACRO, "init http server", DEBUG);
httpServer->setMissingHandler([](const QHttpServerRequest &request, QHttpServerResponder &&responder)
httpServer->setMissingHandler(httpServer,[](const QHttpServerRequest &request, QHttpServerResponder &responder)
{
(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));
//responder.write(QHttpServerResponse(QString("404 - Page non trouvée. Route par défaut."), QHttpServerResponse::StatusCode::NotFound));
responder.write(QByteArray("404 - Page non trouvée. Route par défaut."), "text/plain", QHttpServerResponse::StatusCode::NotFound);
});
httpServer->route("/", [](const QHttpServerRequest &request)
{
QList<std::pair<QByteArray, QByteArray>> headersList = request.headers().toList();
debug(DEBUGMACRO, "Remote address" + request.remoteAddress().toString(), WARNING);
return QHttpServerResponse("text/plain", "Failed\n");
});
@@ -126,18 +124,19 @@ void Pws2mqtt::init()
return QHttpServerResponse("text/plain", "Success\n");
});
for (auto [name, pair]: propertyName.asKeyValueRange())
{
propertyList[name].append(formatNotifString(pair.first, pair.second, "0"));
debug (DEBUGMACRO, "Init " + name + " => " + propertyList[name], DEBUG);
}
for (auto [name, pair]: propertyName.asKeyValueRange())
{
propertyList[name].append(formatNotifString(pair.first, pair.second, "0"));
debug (DEBUGMACRO, "Init " + name + " => " + propertyList[name], DEBUG);
}
}
void Pws2mqtt::listeningHttp()
{
//QByteArray data;
debug (DEBUGMACRO, "listening http requests", DEBUG);
const auto port = httpServer->listen(QHostAddress::Any, 5000);
QTcpServer *tcpServer = new QTcpServer(this);
const auto port =tcpServer->listen(QHostAddress::Any, 5000);
if (!port)
{
debug(DEBUGMACRO, "Http Server failed to listen on a port.", ERROR);
@@ -154,12 +153,12 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
QString notif = "";
QStringList priorityList {"", "min", "low", "default", "High", "urgent"};
QString attachment = "";
QString tag = "";
quint8 priority = 2;
double propertyValue = 0;
bool propertyFlag = false;
bool deviceFlag = false;
static QDateTime timer = QDateTime::currentDateTime().addSecs(-2000);
QString tag = "";
quint8 priority = 2;
double propertyValue = 0;
bool propertyFlag = false;
bool deviceFlag = false;
static QDateTime timer = QDateTime::currentDateTime().addSecs(-2000);
debug(DEBUGMACRO, "looping list of query", DEBUG);
@@ -217,28 +216,28 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
//notif += formatNotifString (propertyName[name].first, propertyName[name].second , value);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
}
}else if (name == "winddir")
{
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));
propertiesValue[name] = propertyValue;
}else if (name == "windspeedmph")
propertiesValue[name] = propertyValue;
}else if (name == "windspeedmph")
{
QString msg = "";
quint8 i;
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
quint8 i;
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
quint8 windPriority = 1;
propertiesValue["vent"] = round(mphTokmh(value.toFloat()));
propertiesValue["vent"] = round(mphTokmh(value.toFloat()));
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertiesValue["vent"]), DEBUG);
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertiesValue["vent"]), DEBUG);
for (i=0; i<forceVent.size();i++)
for (i=0; i<forceVent.size();i++)
{
if (propertiesValue["vent"] <= forceVent[i][0].toDouble())
if (propertiesValue["vent"] <= forceVent[i][0].toDouble())
{
msg = forceVent[i][1].toString();
windPriority = forceVent[i][2].toUInt();
@@ -246,35 +245,35 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
}
}
propertyList[name] = formatNotifString ("Vent - " + msg, propertyName[name].second , QByteArray::number(propertiesValue["vent"]));
propertiesValue["forcevent"] = i;
if (i >= 5)
{
attachment = this->outputPath;
}
if (propertiesValue["forcevent"] != i)
{
priority = qMax(priority, windPriority);
propertyList[name] = formatNotifString ("Vent - " + msg, propertyName[name].second , QByteArray::number(propertiesValue["vent"]));
propertiesValue["forcevent"] = i;
if (i >= 5)
{
attachment = this->outputPath;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "windgustmph")
{
QString msg = "";
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
propertiesValue[name] = round(mphTokmh(value.toFloat()));
if (propertiesValue["forcevent"] != i)
{
priority = qMax(priority, windPriority);
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "windgustmph")
{
QString msg = "";
static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600);
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertiesValue["rafale"]), DEBUG);
propertiesValue[name] = round(mphTokmh(value.toFloat()));
propertyList[name] = formatNotifString (propertyName[name].first, propertyName[name].second , QByteArray::number(propertiesValue[name]));
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertiesValue["rafale"]), DEBUG);
if (propertiesValue["rafales"] > 50)
{
priority = qMax(priority, (quint8)4);
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "rainin")
propertyList[name] = formatNotifString (propertyName[name].first, propertyName[name].second , QByteArray::number(propertiesValue[name]));
if (propertiesValue["rafales"] > 50)
{
priority = qMax(priority, (quint8)4);
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "rainin")
{
static double ecart;
quint8 raininPriority = 1;
@@ -292,11 +291,11 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
QString pluviosite = getPluviosite(propertyValue, raininPriority);
priority = setPriority (priority, raininPriority);
//debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(pluviosite, propertyName[name].second, QByteArray::number(propertyValue));
propertyList[name] = formatNotifString(pluviosite, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "dailyrainin")
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "dailyrainin")
{
static double ecart;
propertyValue = round(pair.second.toFloat()*100)/100;
@@ -310,37 +309,37 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertyValue), DEBUG);
if (compare (propertiesValue[name], propertyValue, ecart))
{
//priority = setPriority (priority, 3);
//priority = setPriority (priority, 3);
//debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "baromin")
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "baromin")
{
//static QDateTime timePrevision = QDateTime::currentDateTime().addSecs(-1000);
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));
propertiesValue[name] = propertyValue;
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));
propertiesValue[name] = propertyValue;
static QString prevision;
auto ret = baro->forecast(propertiesValue["tempf"], propertiesValue["humidity"]);
static QString prevision;
auto ret = baro->forecast(propertiesValue["tempf"], propertiesValue["humidity"]);
prevision = ret.message;
//if (prevision != newPrevision)
//{
priority = setPriority(priority, ret.urgency);
tag += ret.icons;
prevision = ret.message;
//if (prevision != newPrevision)
//{
priority = setPriority(priority, ret.urgency);
tag += ret.icons;
propertyList["prevision"] = prevision;
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "UV")
propertyList["prevision"] = prevision;
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "UV")
{
static QDateTime timeUV = QDateTime::currentDateTime().addSecs(-600);
propertyValue = pair.second.toUInt();
propertyValue = pair.second.toUInt();
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertyValue), DEBUG);
if (compare (propertiesValue[name], propertyValue, 1) and timeUV < QDateTime::currentDateTime())
@@ -353,20 +352,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));
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
propertyList[name] = formatNotifString(propertyName[name].first, "", QByteArray::number(propertyValue));
}
debug (DEBUGMACRO, "priority = " + QString::number(priority), DEBUG);
}else if (name == "solarradiation")
{
propertiesValue[name] = round(pair.second.toFloat()*100/100);
}
}else if (name == "solarradiation")
{
propertiesValue[name] = round(pair.second.toFloat()*100/100);
}
}
}
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"]));
//windChill(propertiesValue["tempf"], propertiesValue["vent"]);
//calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]);
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"]));
//windChill(propertiesValue["tempf"], propertiesValue["vent"]);
//calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]);
if (!jsonString.isEmpty())
{
@@ -380,66 +379,66 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug(DEBUGMACRO, "current datetime : " + QDateTime::currentDateTime().toString() + ", timer = " + timer.toString(), DEBUG);
if (priority > 3 or QDateTime::currentDateTime() > timer.addSecs(1800))
if (priority > 3 or QDateTime::currentDateTime() > timer.addSecs(1800))
{
bool changed = false;
timer = QDateTime::currentDateTime();
debug(DEBUGMACRO, "looping to fill notif, priority = " + QString::number(priority), DEBUG);
bool changed = false;
timer = QDateTime::currentDateTime();
debug(DEBUGMACRO, "looping to fill notif, priority = " + QString::number(priority), DEBUG);
if (precPropertyList.size() == 0)
{
changed = true;
}
if (precPropertyList.size() == 0)
{
changed = true;
}
for (auto [name, value]: propertyList.asKeyValueRange())
{
if (precPropertyList.contains(name))
{
if (precPropertyList[name] != propertyList[name])
{
debug(DEBUGMACRO, "changed = true", DEBUG);
changed = true;
}
}
if (precPropertyList.contains(name))
{
if (precPropertyList[name] != propertyList[name])
{
debug(DEBUGMACRO, "changed = true", DEBUG);
changed = true;
}
}
debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG);
if (! (name == "prevision"))
{
notif += propertyList[name];
}
}
if (changed)
{
notify (notif, priorityList[priority], attachment, tag);
precPropertyList = propertyList;
}
debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG);
if (! (name == "prevision"))
{
notif += propertyList[name];
}
}
if (changed)
{
notify (notif, priorityList[priority], attachment, tag);
precPropertyList = propertyList;
}
notif = "**Prévisions à 12/24h**\n" + propertyList["prevision"];
notify (notif, priorityList[priority], attachment, tag);
debug(DEBUGMACRO, "calling notify with notif = #" + notif + "#", DEBUG);
notif = "**Prévisions à 12/24h**\n" + propertyList["prevision"];
notify (notif, priorityList[priority], attachment, tag);
debug(DEBUGMACRO, "calling notify with notif = #" + notif + "#", DEBUG);
}
}
debug(DEBUGMACRO, "parseData: Returning", DEBUG);
}
double calculerUTCI(double temperature, double humiditeRelative, double vitesseVent, double rayonnement)
{
double e = (humiditeRelative / 100.0) * 6.105 * exp((17.27 * temperature) / (237.7 + temperature));
debug(DEBUGMACRO, "UTCI => e = " + QString::number(e), DEBUG);
double V = vitesseVent * 5.0 / 18.0; // Vitesse du vent en m/s
debug(DEBUGMACRO, "UTCI => V = " + QString::number(V), DEBUG);
double e = (humiditeRelative / 100.0) * 6.105 * exp((17.27 * temperature) / (237.7 + temperature));
debug(DEBUGMACRO, "UTCI => e = " + QString::number(e), DEBUG);
double V = vitesseVent * 5.0 / 18.0; // Vitesse du vent en m/s
debug(DEBUGMACRO, "UTCI => V = " + QString::number(V), DEBUG);
double utci = temperature +
(0.607562 + 0.022437 * e + 7.3869e-4 * temperature * e - 3.5582e-6 * temperature * temperature * e) +
(-11.4 + 0.11 * temperature + 0.55 * V) +
(0.0023 * temperature * temperature - 0.118 * V - 0.0014 * temperature * V);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
double utci = temperature +
(0.607562 + 0.022437 * e + 7.3869e-4 * temperature * e - 3.5582e-6 * temperature * temperature * e) +
(-11.4 + 0.11 * temperature + 0.55 * V) +
(0.0023 * temperature * temperature - 0.118 * V - 0.0014 * temperature * V);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
// Ajustement simplifié pour le rayonnement
double ref = 200.0; // Rayonnement de référence (W/m²)
double k = 0.02; // Coefficient empirique
utci = utci + k * (rayonnement - ref);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
return round(utci * 100) / 100;
// Ajustement simplifié pour le rayonnement
double ref = 200.0; // Rayonnement de référence (W/m²)
double k = 0.02; // Coefficient empirique
utci = utci + k * (rayonnement - ref);
debug(DEBUGMACRO, "UTCI = " + QString::number(utci), DEBUG);
return round(utci * 100) / 100;
}
QString getPluviosite(double value, quint8 &priority)
@@ -509,13 +508,13 @@ bool compare (double value, double currentValue, double ecart)
QString formatNotifString (QString name, QString unit ,QByteArray value)
{
QString text = "- " + name + " : " + value;
QString text = "- " + name + " : " + value;
if (!unit.isEmpty())
{
text += unit;
//"- " + propertyName[name].first + " : " + value + " " + propertyName[name].second + " ";
//"- " + propertyName[name].first + " : " + value + " " + propertyName[name].second + " ";
}
return text += "\n";
return text += "\n";
}
static size_t ReadFile(void *ptr, size_t size, size_t nmemb, void *stream) {
@@ -527,9 +526,9 @@ static size_t ReadFile(void *ptr, size_t size, size_t nmemb, void *stream) {
// Callback for curl library
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *output)
{
size_t total_size = size * nmemb;
output->append((char*)contents, total_size);
return total_size;
size_t total_size = size * nmemb;
output->append((char*)contents, total_size);
return total_size;
}
void notify(QString notif, QString priority, QString inputPath, QString tag)
@@ -539,35 +538,35 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
std::string readBuffer;
priority = "Priority: " + priority;
debug (DEBUGMACRO, "notifying at priority " + priority + "with tag : " + tag + " - message : " + notif, DEBUG);
debug (DEBUGMACRO, "notifying at priority " + priority + "with tag : " + tag + " - message : " + notif, DEBUG);
// Initialise libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
if (curl)
{
// Définis les en-têtes pour le titre et les priorités
struct curl_slist *headers = NULL;
notif.replace("\\", "\\\\\\'"); // Échappe les apostrophes
notif.replace("\\", "\\\\\\'"); // Échappe les apostrophes
if (!tag.isEmpty())
{
headers = curl_slist_append(headers, ("Tags: " + tag).toStdString().c_str());
}
headers = curl_slist_append(headers, "Title: Météo");
if (!tag.isEmpty())
{
headers = curl_slist_append(headers, ("Tags: " + tag).toStdString().c_str());
}
headers = curl_slist_append(headers, "Title: Météo");
headers = curl_slist_append(headers, priority.toStdString().c_str());
headers = curl_slist_append(headers, "Markdown: yes");
headers = curl_slist_append(headers, "Markdown: yes");
// headers = curl_slist_append(headers, "Config: /etc/ntfy.client.yml");
headers = curl_slist_append(headers, "Firebase: no");
// headers = curl_slist_append(headers, "Content-Type: charset=UTF-8; text/markdown"); // Ajout de l'encodage
struct curl_slist *h = headers;
while (h) {
debug(DEBUGMACRO, "Header: " + QString(h->data), DEBUG);
h = h->next;
}
debug(DEBUGMACRO, "Notif: " + notif, DEBUG);
struct curl_slist *h = headers;
while (h) {
debug(DEBUGMACRO, "Header: " + QString(h->data), DEBUG);
h = h->next;
}
debug(DEBUGMACRO, "Notif: " + notif, DEBUG);
// Configure la requête POST
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:81/Meteo");
@@ -575,11 +574,11 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, notif.toUtf8().constData());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// Exécute la requête
res = curl_easy_perform(curl);
res = curl_easy_perform(curl);
// Vérifie les erreurs
if (res != CURLE_OK)
@@ -587,7 +586,7 @@ void notify(QString notif, QString priority, QString inputPath, QString tag)
debug(DEBUGMACRO, "Erreur libcurl :" + QString(curl_easy_strerror(res)), DEBUG);
} else
{
debug(DEBUGMACRO, "Réponse du serveur: " + QString::fromStdString(readBuffer), DEBUG);
debug(DEBUGMACRO, "Réponse du serveur: " + QString::fromStdString(readBuffer), DEBUG);
}
// Nettoie les ressources