diff --git a/main.cpp b/main.cpp index a183523..d155fef 100644 --- a/main.cpp +++ b/main.cpp @@ -15,7 +15,7 @@ #include #include "pws2mqtt.h" #include "mqtt.h" -//#include "version.h" +#include "version.h" #include #include #include @@ -43,6 +43,12 @@ int main(int argc, char *argv[]) debug(DEBUGMACRO, "QcoreApplication", DEBUG); 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"); // Enable logging to journald qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0")); @@ -62,13 +68,11 @@ int main(int argc, char *argv[]) mqttClient->qmqttClient->unsubscribe(mqttClient->topic); mqttClient->qmqttClient->disconnectFromHost(); debug(DEBUGMACRO, "exiting", DEBUG); - notify ("Météo" "Program exiting"); + notify ("Program exiting", "low"); //Closing http server } - - -QString addValue(QByteArray value) +/*QString addValue(QByteArray value) { bool ok; QString str; @@ -76,13 +80,13 @@ QString addValue(QByteArray value) value.toFloat(&ok); if (ok) { - str = (value); + str = (value); }else { - str = ("\"" + value + "\"" ); + str = ("\"" + value + "\"" ); } return str; -} +}*/ void debug(QString debugHeader, QString msg, uint8_t level, QByteArray property) { diff --git a/pws2mqtt.cpp b/pws2mqtt.cpp index 385a2d8..5ca6d27 100644 --- a/pws2mqtt.cpp +++ b/pws2mqtt.cpp @@ -83,7 +83,8 @@ void Pws2mqtt::init() httpServer->route("/query", [this](const QHttpServerRequest &request) { QByteArray data; - QList> queryList = request.query().queryItems(); + QList> queryList = request.query().queryItems(); + //QTextStream result(&data); if (queryList.isEmpty()) @@ -122,21 +123,23 @@ void Pws2mqtt::parseData(QList> queryList) QString jsonString = "{"; bool propertyFlag = false; bool deviceFlag = false; - QString spaceFlag = ""; // QString topic; QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\""; QString notif = ""; - double propertyValue; + double propertyValue = 0; QStringList priorityList {"", "min", "low", "default", "High", "urgent"}; quint8 priority = 2; + QPair winddir = {"", 0}; debug(DEBUGMACRO, "looping list of query", DEBUG); for (QPair pair : queryList) { 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) { @@ -145,7 +148,7 @@ void Pws2mqtt::parseData(QList> queryList) { deviceString.append(", "); } - deviceString += "\"" + pair.first + "\": "; + deviceString += "\"" + name + "\": "; deviceString += pair.second; }else { @@ -156,111 +159,127 @@ void Pws2mqtt::parseData(QList> queryList) { jsonString.append(", "); } - jsonString.append("\"" + pair.first + "\": "); + jsonString.append("\"" + name + "\": "); jsonString.append(pair.second); - //mqttClient->end_message(topic, jsonString); - if (pair.first == "tempf") + + if (name == "tempf") { - propertyValue = fahrenheitToCelsius(pair.second.toFloat()); - if (compare (propertyList[pair.first.toLatin1()], propertyValue, 0.5)) + static QTime timeTemp; + 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); - propertyList[pair.first.toLatin1()] = propertyValue; + propertyList[name] = propertyValue; + timeTemp = timeTemp.currentTime(); } - }else if (pair.first == "humidity") + }else if (name == "humidity") { 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); } - }else if (pair.first == "windchillf") + }else if (name == "windchillf") { - propertyValue = fahrenheitToCelsius(pair.second.toFloat()); - if (compare (propertyList[pair.first.toLatin1()], propertyValue, 0.2)) + static QTime timeWindchill; + 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); + timeWindchill = timeWindchill.currentTime(); } - }else if (pair.first == "winddir") + }else if (name == "winddir") { - propertyValue = pair.second.toFloat(); - if (compare (propertyList[pair.first.toLatin1()], propertyValue, 90)) + winddir.first = name; + winddir.second = value; + }else if (name == "windspeedmph") + { + propertyValue = round(mphTokmh(pair.second.toFloat())); + if (compare (propertyList[name], propertyValue, 5)) { - notif += spaceFlag + formatNotifString (pair, pair.second.toLatin1()); - debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); + if (propertyValue > 10) + { + priority = setPriority (priority, 2); + notif += formatNotifString (name, QByteArray::number(propertyValue)); + notif += formatNotifString (winddir.first, winddir.second); + debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); + + if (propertyValue > 30) + priority = setPriority (priority, 4); + if (propertyValue > 50) + priority = setPriority (priority, 5); + + debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); + } } - }else if (pair.first == "windspeedmph") + }else if (name == "windgustmph") { - propertyValue = mphTokmh(pair.second.toFloat()); - if (compare (propertyList[pair.first.toLatin1()], propertyValue, 10)) + propertyValue = round(mphTokmh(pair.second.toFloat())); + if (compare (propertyList[name], propertyValue, 5) and propertyValue > 20) { - notif += spaceFlag + formatNotifString (pair, QByteArray::number(round(propertyValue))); - if (propertyValue > 30) - priority = setPriority (priority, 4); - if (propertyValue > 50) - priority = setPriority (priority, 5); - debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); - } - }else if (pair.first == "windgustmph") - { - propertyValue = mphTokmh(pair.second.toFloat()); - if (compare (propertyList[pair.first.toLatin1()], propertyValue, 5) and propertyValue > 20) - { - notif += spaceFlag + formatNotifString (pair, QByteArray::number(round(propertyValue))); + notif += formatNotifString (name, QByteArray::number(propertyValue)); if (propertyValue > 40) priority = setPriority (priority, 4); if (propertyValue > 60) priority = setPriority (priority, 5); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); } - }else if (pair.first == "rainin") + }else if (name == "rainin") { + static qfloat16 ecart; 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); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); } - }else if (pair.first == "baromin") + }else if (name == "baromin") { propertyValue = tohPa(pair.second.toFloat()); 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); - } - }else if (pair.first == "UV") + }*/ + }else if (name == "UV") { + static QTime timeUV; 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()); - if (propertyValue > 3) - priority = setPriority (priority, 5); + notif += formatNotifString (name, value); + if (propertyValue == 5 ) + priority = setPriority (priority, 3); + if (propertyValue >= 6 ) + priority = setPriority (priority, 4); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); + timeUV = timeUV.currentTime(); } - }else if (pair.first == "lowbat") + }else if (name == "lowbat") { - static QTime time; - if (pair.second.toInt() == 1 and time > QTime::currentTime().addSecs(3600)) + static QTime timeLowBat; + if (pair.second.toInt() == 0 and timeLowBat > QTime::currentTime().addSecs(3600)) { - notif += spaceFlag + formatNotifString (pair, ""); + notif += formatNotifString (name, ""); debug (DEBUGMACRO, "Notif = #" + notif + "#", DEBUG); priority = setPriority (priority, 4); - time = time.currentTime(); + timeLowBat = timeLowBat.currentTime(); } } } - propertyList[pair.first.toLatin1()] = propertyValue; - if (!notif.isEmpty()) - { - spaceFlag = " "; - } + propertyList[name] = propertyValue; } if (!jsonString.isEmpty()) { @@ -388,9 +407,9 @@ bool compare (qfloat16 value, qfloat16 currentValue, qfloat16 ecart) } } -QString formatNotifString (QPair 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) diff --git a/pws2mqtt.h b/pws2mqtt.h index 83eec45..4097d61 100644 --- a/pws2mqtt.h +++ b/pws2mqtt.h @@ -23,7 +23,7 @@ #define DEBUGMACRO QString(__FILE__) + ": " + QString::number(__LINE__) + " -> " + QString(Q_FUNC_INFO) void debug(QString debugHeader, QString msg, uint8_t level=ALL, QByteArray value=""); -QString addValue(QByteArray value); +//QString addValue(QByteArray value); class Pws2mqtt : public QObject { @@ -51,7 +51,7 @@ class Pws2mqtt : public QObject double fahrenheitToCelsius(double fahrenheit); qfloat16 tohPa(qfloat16 value); bool compare (qfloat16 value, qfloat16 testValue, qfloat16 ecart = 0.5); -QString formatNotifString (QPair pair, QByteArray value); +QString formatNotifString (QByteArray name, QByteArray value); qfloat16 mphTokmh (qfloat16 value); void notify (QString notif, QString priority = "low"); quint8 setPriority (quint8 currentPriority, quint8 newPriority);