added windchill calculation

This commit is contained in:
2025-10-15 18:58:46 +02:00
parent 371d1f10db
commit 521e87f011
3 changed files with 108 additions and 29 deletions

View File

@@ -20,6 +20,7 @@
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include "utcicalculator.h"
#define CLIENT_ID "Client_ID"
#define BROKER_ADDRESS "localhost"
@@ -62,7 +63,6 @@ int main(int argc, char *argv[])
pws2mqtt->listeningHttp();
//notify (QString("Program started"), "default");
a.exec();
mqttClient->qmqttClient->unsubscribe(mqttClient->topic);

View File

@@ -15,10 +15,13 @@
#include <QTransform>
#include <stdio.h>
#include <sys/stat.h>
#include "utcicalculator.h"
extern MqttClient *mqttClient;
extern Pws2mqtt *pws2mqtt;
extern QHttpServer *httpServer;
UtciCalculator calc;
QStringList previsionList
{
"Temps variable (incertain).", \
@@ -39,16 +42,18 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use
return size * nmemb; // On ne traite pas les données reçues
}
QMap <QByteArray, QString> propertyList;
QMap <QByteArray, QString> precPropertyList;
QMap <QByteArray, double> propertiesValue;
QMap <QByteArray, QPair<QString, QByteArray>> propertyName
{
{"tempf", {"Température extérieure", "°C"}},
{"humidity", {"Humidité extérieure", "%"}},
{"dewptf", {"Point de rosée", "°C"}},
{"windchillf", {"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"}},
@@ -57,7 +62,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", ""}}
};
@@ -144,17 +149,15 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
debug(DEBUGMACRO, "Parsing Datas", DEBUG);
QString jsonString = "{";
bool propertyFlag = false;
bool deviceFlag = false;
// QString topic;
QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\"";
QString notif = "";
double propertyValue = 0;
QStringList priorityList {"", "min", "low", "default", "High", "urgent"};
quint8 priority = 2;
//static qreal winddir = 0;
QString attachment = "";
static QDateTime timer;
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);
@@ -213,7 +216,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
}else if (name == "windchillf")
/*}else if (name == "windchillf")
{
static QDateTime timeWindchill = QDateTime::currentDateTime().addSecs(-600);
propertyValue = round(fahrenheitToCelsius(value.toFloat()));
@@ -225,7 +228,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
timeWindchill = timeWindchill.currentDateTime().addSecs(300);
propertyList[name] = formatNotifString(propertyName[name].first, propertyName[name].second, QByteArray::number(propertyValue));
propertiesValue[name] = propertyValue;
}
}*/
}else if (name == "winddir")
{
propertyValue = value.toFloat();
@@ -233,7 +236,6 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
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" or name == "windgustmph")
{
QString msg = "";
@@ -242,10 +244,11 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
//static QString msgNotif;
debug (DEBUGMACRO, name + " : " + QByteArray::number(propertyValue), DEBUG);
propertyValue = round(mphTokmh(value.toFloat()));
propertiesValue["vent"] = round(mphTokmh(value.toFloat()));
for (quint8 i=0; i<forceVent.size();i++)
{
if (propertyValue <= forceVent[i][0].toDouble())
if (propertiesValue["vent"] <= forceVent[i][0].toDouble())
{
msg = forceVent[i][1].toString();
windPriority = forceVent[i][2].toUInt();
@@ -258,8 +261,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
if (name == "windspeedmph")
{
//propertyList["forcevent"] = msg;
msg.prepend("Vent - ");
propertyList[name] = formatNotifString (msg, propertyName[name].second , QByteArray::number(propertyValue));
propertyList[name] = formatNotifString ("Vent - " + msg, propertyName[name].second , QByteArray::number(propertyValue));
}else
{
propertyList[name] = formatNotifString (propertyName[name].first, propertyName[name].second , QByteArray::number(propertyValue));
@@ -354,9 +356,17 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
propertiesValue[name] = propertyValue;
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
}
}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"]);
if (!jsonString.isEmpty())
{
debug(DEBUGMACRO, "json string : " + jsonString, DEBUG);
@@ -369,21 +379,88 @@ 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(900))
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);
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;
}
}
debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG);
notif += propertyList[name];
}
debug(DEBUGMACRO, "calling notify with notif = #" + notif + "#", DEBUG);
if (changed)
{
notify (notif, priorityList[priority], attachment);
precPropertyList = propertyList;
}
}
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 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;
}
double windChill(double airT, double vent)
{
double w = 0;
if (airT > 10 or vent < 4.8)
{
return airT;
} else
{
w = 13.12 + 0.6215 * airT - 11.37 * pow(vent, 0.16) + 0.3965 * airT * pow(vent, 0.16);
debug(DEBUGMACRO, "windChill = " + QString::number(w), DEBUG);
return w;
}
}
double calculerHumidex(double temperature, double humiditeRelative)
{
// Calcul de la température du point de rosée (T_rosée)
double T_rosee = (243.04 * (log(humiditeRelative / 100.0) + (17.625 * temperature) / (243.04 + temperature))) / (17.625 - (log(humiditeRelative / 100.0) + (17.625 * temperature) / (243.04 + temperature)));
// Calcul de l'Humidex (H)
double H = temperature + 0.5555 * (6.11 * exp(5417.7530 * (1.0 / 273.16 - 1.0 / (273.15 + T_rosee))) - 10.0);
debug(DEBUGMACRO, "humidex = " + QString::number(H), DEBUG);
return H;
}
QString getPluviosite(double value, quint8 &priority)
{
if (value < 1)

View File

@@ -62,5 +62,7 @@ QString previsionMeteo(double currentPressure, double variation3h, quint8 &prior
QString pressureVariation(double currentPressure, quint8 &priority);
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);
#endif // PWS2MQTT_H