- ameliorations diverses

This commit is contained in:
DTux committed 2025-09-28 21:25:04 +02:00
1 parent c850b24704
commit 0d359ee997
3 files changed
+88 -65

No files matched your search

+10 -6
View File
@@ -15,7 +15,7 @@
#include <QHttpServerResponse> #include <QHttpServerResponse>
#include "pws2mqtt.h" #include "pws2mqtt.h"
#include "mqtt.h" #include "mqtt.h"
//#include "version.h" #include "version.h"
#include <curl/curl.h> #include <curl/curl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/select.h> #include <sys/select.h>
@@ -43,6 +43,12 @@ int main(int argc, char *argv[])
debug(DEBUGMACRO, "QcoreApplication", DEBUG); debug(DEBUGMACRO, "QcoreApplication", DEBUG);
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
if (strcmp(argv[1], "v"))
{
//debug(DEBUGMACRO, "version : " + version, DEBUG);
printf ("Version : %s", version.c_str());
exit(0);
}
notify ("Météo" "Program starting"); notify ("Météo" "Program starting");
// Enable logging to journald // Enable logging to journald
qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0")); qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0"));
@@ -62,13 +68,11 @@ int main(int argc, char *argv[])
mqttClient->qmqttClient->unsubscribe(mqttClient->topic); mqttClient->qmqttClient->unsubscribe(mqttClient->topic);
mqttClient->qmqttClient->disconnectFromHost(); mqttClient->qmqttClient->disconnectFromHost();
debug(DEBUGMACRO, "exiting", DEBUG); debug(DEBUGMACRO, "exiting", DEBUG);
notify ("Météo" "Program exiting"); notify ("Program exiting", "low");
//Closing http server //Closing http server
} }
/*QString addValue(QByteArray value)
QString addValue(QByteArray value)
{ {
bool ok; bool ok;
QString str; QString str;
@@ -82,7 +86,7 @@ QString addValue(QByteArray value)
str = ("\"" + value + "\"" ); str = ("\"" + value + "\"" );
} }
return str; return str;
} }*/
void debug(QString debugHeader, QString msg, uint8_t level, QByteArray property) void debug(QString debugHeader, QString msg, uint8_t level, QByteArray property)
{ {
+76 -57
View File
@@ -84,6 +84,7 @@ void Pws2mqtt::init()
{ {
QByteArray data; QByteArray data;
QList<std::pair<QString, QString>> queryList = request.query().queryItems(); QList<std::pair<QString, QString>> queryList = request.query().queryItems();
//QTextStream result(&data); //QTextStream result(&data);
if (queryList.isEmpty()) if (queryList.isEmpty())
@@ -122,21 +123,23 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
QString jsonString = "{"; QString jsonString = "{";
bool propertyFlag = false; bool propertyFlag = false;
bool deviceFlag = false; bool deviceFlag = false;
QString spaceFlag = "";
// QString topic; // QString topic;
QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\""; QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\"";
QString notif = ""; QString notif = "";
double propertyValue; double propertyValue = 0;
QStringList priorityList {"", "min", "low", "default", "High", "urgent"}; QStringList priorityList {"", "min", "low", "default", "High", "urgent"};
quint8 priority = 2; quint8 priority = 2;
QPair <QByteArray, QByteArray> winddir = {"", 0};
debug(DEBUGMACRO, "looping list of query", DEBUG); debug(DEBUGMACRO, "looping list of query", DEBUG);
for (QPair<QString, QString> pair : queryList) for (QPair<QString, QString> pair : queryList)
{ {
debug(DEBUGMACRO, pair.first + " = " + pair.second, DEBUG); debug(DEBUGMACRO, pair.first + " = " + pair.second, DEBUG);
QByteArray name = pair.first.toLatin1();
QByteArray value = pair.second.toLatin1();
if (this->deviceProperties.contains(pair.first)) if (this->deviceProperties.contains(name))
{ {
if(deviceFlag == false) if(deviceFlag == false)
{ {
@@ -145,7 +148,7 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
{ {
deviceString.append(", "); deviceString.append(", ");
} }
deviceString += "\"" + pair.first + "\": "; deviceString += "\"" + name + "\": ";
deviceString += pair.second; deviceString += pair.second;
}else }else
{ {
@@ -156,111 +159,127 @@ void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
{ {
jsonString.append(", "); jsonString.append(", ");
} }
jsonString.append("\"" + pair.first + "\": "); jsonString.append("\"" + name + "\": ");
jsonString.append(pair.second); jsonString.append(pair.second);
//mqttClient->end_message(topic, jsonString);
if (pair.first == "tempf") if (name == "tempf")
{ {
propertyValue = fahrenheitToCelsius(pair.second.toFloat()); static QTime timeTemp;
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 0.5)) propertyValue = round(fahrenheitToCelsius(pair.second.toFloat()));
if (compare (propertyList[name], propertyValue, 1) and timeTemp > QTime::currentTime().addSecs(300))
{ {
notif += spaceFlag + formatNotifString (pair, QByteArray::number(qPow(propertyValue, 1.0))); notif += formatNotifString (name, QByteArray::number(qPow(propertyValue, 1.0)));
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
propertyList[pair.first.toLatin1()] = propertyValue; propertyList[name] = propertyValue;
timeTemp = timeTemp.currentTime();
} }
}else if (pair.first == "humidity") }else if (name == "humidity")
{ {
propertyValue = pair.second.toFloat(); propertyValue = pair.second.toFloat();
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 3)) if (compare (propertyList[name], propertyValue, 3))
{ {
notif += spaceFlag + formatNotifString (pair, pair.second.toLatin1()); notif += formatNotifString (name, value);
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
} }
}else if (pair.first == "windchillf") }else if (name == "windchillf")
{ {
propertyValue = fahrenheitToCelsius(pair.second.toFloat()); static QTime timeWindchill;
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 0.2)) propertyValue = round(fahrenheitToCelsius(pair.second.toFloat()));
if (compare (propertyList[name], propertyValue, 1) and timeWindchill > QTime::currentTime().addSecs(300))
{ {
notif += spaceFlag + formatNotifString (pair, QByteArray::number(qPow(propertyValue, 1.0))); notif += formatNotifString (name, QByteArray::number(qPow(propertyValue, 1.0)));
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
timeWindchill = timeWindchill.currentTime();
} }
}else if (pair.first == "winddir") }else if (name == "winddir")
{ {
propertyValue = pair.second.toFloat(); winddir.first = name;
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 90)) winddir.second = value;
}else if (name == "windspeedmph")
{ {
notif += spaceFlag + formatNotifString (pair, pair.second.toLatin1()); propertyValue = round(mphTokmh(pair.second.toFloat()));
if (compare (propertyList[name], propertyValue, 5))
{
if (propertyValue > 10)
{
priority = setPriority (priority, 2);
notif += formatNotifString (name, QByteArray::number(propertyValue));
notif += formatNotifString (winddir.first, winddir.second);
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
}
}else if (pair.first == "windspeedmph")
{
propertyValue = mphTokmh(pair.second.toFloat());
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 10))
{
notif += spaceFlag + formatNotifString (pair, QByteArray::number(round(propertyValue)));
if (propertyValue > 30) if (propertyValue > 30)
priority = setPriority (priority, 4); priority = setPriority (priority, 4);
if (propertyValue > 50) if (propertyValue > 50)
priority = setPriority (priority, 5); priority = setPriority (priority, 5);
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
} }
}else if (pair.first == "windgustmph") }
}else if (name == "windgustmph")
{ {
propertyValue = mphTokmh(pair.second.toFloat()); propertyValue = round(mphTokmh(pair.second.toFloat()));
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 5) and propertyValue > 20) if (compare (propertyList[name], propertyValue, 5) and propertyValue > 20)
{ {
notif += spaceFlag + formatNotifString (pair, QByteArray::number(round(propertyValue))); notif += formatNotifString (name, QByteArray::number(propertyValue));
if (propertyValue > 40) if (propertyValue > 40)
priority = setPriority (priority, 4); priority = setPriority (priority, 4);
if (propertyValue > 60) if (propertyValue > 60)
priority = setPriority (priority, 5); priority = setPriority (priority, 5);
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
} }
}else if (pair.first == "rainin") }else if (name == "rainin")
{ {
static qfloat16 ecart;
propertyValue = pair.second.toFloat(); propertyValue = pair.second.toFloat();
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 0.01)) if (propertyValue == 0)
{ {
notif += spaceFlag + formatNotifString (pair, pair.second.toLatin1()); ecart = 0;
}else
{
ecart = 0.01;
}
if (compare (propertyList[name], propertyValue, ecart))
{
notif += formatNotifString (name, value);
priority = setPriority (priority, 4); priority = setPriority (priority, 4);
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
} }
}else if (pair.first == "baromin") }else if (name == "baromin")
{ {
propertyValue = tohPa(pair.second.toFloat()); propertyValue = tohPa(pair.second.toFloat());
pressureVariation(propertyValue); pressureVariation(propertyValue);
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 100)) /*if (compare (propertyList[name], propertyValue, 100))
{ {
notif += spaceFlag + formatNotifString (pair, QByteArray::number(propertyValue)); notif += formatNotifString (name, QByteArray::number(propertyValue));
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
} }*/
}else if (pair.first == "UV") }else if (name == "UV")
{ {
static QTime timeUV;
propertyValue = pair.second.toFloat(); propertyValue = pair.second.toFloat();
if (compare (propertyList[pair.first.toLatin1()], propertyValue, 1)) if (compare (propertyList[name], propertyValue, 1) and timeUV > QTime::currentTime().addSecs(300))
{ {
notif += spaceFlag + formatNotifString (pair, pair.second.toLatin1()); notif += formatNotifString (name, value);
if (propertyValue > 3) if (propertyValue == 5 )
priority = setPriority (priority, 5); priority = setPriority (priority, 3);
if (propertyValue >= 6 )
priority = setPriority (priority, 4);
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
timeUV = timeUV.currentTime();
} }
}else if (pair.first == "lowbat") }else if (name == "lowbat")
{ {
static QTime time; static QTime timeLowBat;
if (pair.second.toInt() == 1 and time > QTime::currentTime().addSecs(3600)) if (pair.second.toInt() == 0 and timeLowBat > QTime::currentTime().addSecs(3600))
{ {
notif += spaceFlag + formatNotifString (pair, ""); notif += formatNotifString (name, "");
debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG);
priority = setPriority (priority, 4); priority = setPriority (priority, 4);
time = time.currentTime(); timeLowBat = timeLowBat.currentTime();
} }
} }
} }
propertyList[pair.first.toLatin1()] = propertyValue; propertyList[name] = propertyValue;
if (!notif.isEmpty())
{
spaceFlag = " ";
}
} }
if (!jsonString.isEmpty()) if (!jsonString.isEmpty())
{ {
@@ -388,9 +407,9 @@ bool compare (qfloat16 value, qfloat16 currentValue, qfloat16 ecart)
} }
} }
QString formatNotifString (QPair <QString, QString> pair, QByteArray value) QString formatNotifString (QByteArray name, QByteArray value)
{ {
return "- " + propertyName[pair.first.toLatin1()].first + " : " + value + propertyName[pair.first.toLatin1()].second; return "- " + propertyName[name].first + " : " + value + propertyName[name].second + " \n";
} }
void notify(QString notif, QString priority) void notify(QString notif, QString priority)
+2 -2
View File
@@ -23,7 +23,7 @@
#define DEBUGMACRO QString(__FILE__) + ": " + QString::number(__LINE__) + " -> " + QString(Q_FUNC_INFO) #define DEBUGMACRO QString(__FILE__) + ": " + QString::number(__LINE__) + " -> " + QString(Q_FUNC_INFO)
void debug(QString debugHeader, QString msg, uint8_t level=ALL, QByteArray value=""); void debug(QString debugHeader, QString msg, uint8_t level=ALL, QByteArray value="");
QString addValue(QByteArray value); //QString addValue(QByteArray value);
class Pws2mqtt : public QObject class Pws2mqtt : public QObject
{ {
@@ -51,7 +51,7 @@ class Pws2mqtt : public QObject
double fahrenheitToCelsius(double fahrenheit); double fahrenheitToCelsius(double fahrenheit);
qfloat16 tohPa(qfloat16 value); qfloat16 tohPa(qfloat16 value);
bool compare (qfloat16 value, qfloat16 testValue, qfloat16 ecart = 0.5); bool compare (qfloat16 value, qfloat16 testValue, qfloat16 ecart = 0.5);
QString formatNotifString (QPair<QString, QString> pair, QByteArray value); QString formatNotifString (QByteArray name, QByteArray value);
qfloat16 mphTokmh (qfloat16 value); qfloat16 mphTokmh (qfloat16 value);
void notify (QString notif, QString priority = "low"); void notify (QString notif, QString priority = "low");
quint8 setPriority (quint8 currentPriority, quint8 newPriority); quint8 setPriority (quint8 currentPriority, quint8 newPriority);