diff --git a/main.cpp b/main.cpp index ede206e..32730f9 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,7 @@ #include #include #include +#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); diff --git a/pws2mqtt.cpp b/pws2mqtt.cpp index 1dc875a..0493035 100644 --- a/pws2mqtt.cpp +++ b/pws2mqtt.cpp @@ -15,10 +15,13 @@ #include #include #include +#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 propertyList; +QMap precPropertyList; QMap propertiesValue; QMap > 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 > 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> 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); @@ -212,8 +215,8 @@ void Pws2mqtt::parseData(QList> 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 == "windchillf") + } + /*}else if (name == "windchillf") { static QDateTime timeWindchill = QDateTime::currentDateTime().addSecs(-600); propertyValue = round(fahrenheitToCelsius(value.toFloat())); @@ -225,16 +228,15 @@ void Pws2mqtt::parseData(QList> 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(); 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" or name == "windgustmph") + propertiesValue[name] = propertyValue; + }else if (name == "windspeedmph" or name == "windgustmph") { QString msg = ""; static QDateTime timeWind = QDateTime::currentDateTime().addSecs(-600); @@ -242,10 +244,11 @@ void Pws2mqtt::parseData(QList> 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> 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> 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> 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)) { - 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; + } for (auto [name, value]: propertyList.asKeyValueRange()) { - debug(DEBUGMACRO, "Name = " + name + ", value = " + value, DEBUG); - notif += propertyList[name]; + 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); - notify (notif, priorityList[priority], attachment); - } + 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) diff --git a/pws2mqtt.h b/pws2mqtt.h index 0188d80..8f15cbc 100644 --- a/pws2mqtt.h +++ b/pws2mqtt.h @@ -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