diff --git a/src/main.cpp b/src/main.cpp index 43e6adf..9c52c04 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,41 +1,42 @@ #include #include -//#include #include #include #include #include #include #include -//#include -//#include -//#include -//#include #include #include #include "pws2mqtt.h" -//#include "mqtt.h" +#ifdef WITH_MQTT + #include "mqtt.h" +#endif #include "src/barometertrend.h" #include "version.h" -#include #include #include #include - +#ifdef WITH_MQTT + #include + #include + #include + #include +#endif #define CLIENT_ID "Client_ID" -//#define BROKER_ADDRESS "localhost" -//#define MQTT_PORT 1883; -uint debugLevel = ERROR | ALERT; +uint debugLevel = INFO | ERROR | ALERT; QByteArray toFollow; -//class MqttClient; +#ifdef WITH_MQTT + class MqttClient; + MqttClient *mqttClient; +#endif using namespace std; //Configuration config; Pws2mqtt *pws2mqtt; -//MqttClient *mqttClient; QHttpServer *httpServer; BarometerTrend *baro; @@ -51,12 +52,13 @@ int main(int argc, char *argv[]) exit(0); } } - // Enable logging to ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••journald + // Enable logging to journald qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0")); - debug(DEBUGMACRO, "declaration of mqttClient", DEBUG); - //mqttClient = new MqttClient; - + debug(DEBUGMACRO, "declaration of mqttClient", INFO); +#ifdef WITH_MQTT + mqttClient = new MqttClient; +#endif httpServer = new QHttpServer; //declaration of debug level @@ -65,34 +67,16 @@ int main(int argc, char *argv[]) pws2mqtt->listeningHttp(); - - - //notify (QString("Program started"), "default"); a.exec(); - //mqttClient->qmqttClient->unsubscribe(mqttClient->topic); - //mqttClient->qmqttClient->disconnectFromHost(); - debug(DEBUGMACRO, "exiting", DEBUG); +#ifdef WITH_MQTT + mqttClient->qmqttClient->unsubscribe(mqttClient->topic); + mqttClient->qmqttClient->disconnectFromHost(); +#endif + debug(DEBUGMACRO, "exiting", INFO); notify ("Program exiting \n", "high"); - //Closing http server } -/*QString addValue(QByteArray value) -{ - bool ok; - QString str; - - value.toFloat(&ok); - if (ok) - { - str = (value); - }else - { - str = ("\"" + value + "\"" ); - } - return str; -}*/ - void debug(QString debugHeader, QString msg, uint8_t level, QByteArray property) { diff --git a/src/pws2mqtt.cpp b/src/pws2mqtt.cpp index 5988fb3..5c69c8e 100644 --- a/src/pws2mqtt.cpp +++ b/src/pws2mqtt.cpp @@ -1,27 +1,24 @@ #include "pws2mqtt.h" #include "barometertrend.h" -//#include "mqtt.h" -//#include "httpserver.h" #include -//#include #include #include #include -//#include -//#include #include #include #include #include #include #include -//#include #include #include "utcicalculator.h" #include #include +#ifdef WITH_MQTT + #include "mqtt.h" + /extern MqttClient *mqttClient; +#endif -//extern MqttClient *mqttClient; extern Pws2mqtt *pws2mqtt; extern QHttpServer *httpServer; extern BarometerTrend *baro; @@ -57,7 +54,7 @@ QMap > propertyName {"dailyrainin", {"Pluie de la journée", " mm"}}, // {"monthlyrainin", {"Pluie du mois", " mm"}}, // {"yearlyrainin", {"Pluie de l'année", " mm"}}, -// {"solarradiation", {"Énergie solaire", " W/m²"}}, + {"solarradiation", {"Énergie solaire", " W/m²"}}, // {"indoortempf", {"Température intérieure", "°C"}}, // {"indoorhumidity", {"Humidité intérieure", "%"}}, {"baromin", {"Pression atmosphérique", " hPa"}}, @@ -93,7 +90,7 @@ Pws2mqtt::~Pws2mqtt() void Pws2mqtt::init() { - debug(DEBUGMACRO, "init http server", DEBUG); + debug(DEBUGMACRO, "init http server", INFO); httpServer->setMissingHandler([](const QHttpServerRequest &request, QHttpServerResponder &&responder) { (void) responder; @@ -141,6 +138,7 @@ void Pws2mqtt::init() void notify(QString notif, QString priority, QString inputPath, QString tag) { + (void) inputPath; QNetworkAccessManager *manager = new QNetworkAccessManager(pws2mqtt); QNetworkRequest request(QUrl("http://localhost:81/Meteo")); request.setRawHeader("Content-Type", "charset=UTF-8; text/markdown"); @@ -148,7 +146,7 @@ void notify(QString notif, QString priority, QString inputPath, QString tag) { request.setRawHeader("Tags", tag.toUtf8()); } - request.setRawHeader("Title", "Vidéo"); + request.setRawHeader("Title", "Météo"); request.setRawHeader("Priority", priority.toUtf8()); request.setRawHeader("Markdown", "yes"); request.setRawHeader("Config", "/etc/ntfy.client.yml"); @@ -177,15 +175,17 @@ void Pws2mqtt::listeningHttp() { debug(DEBUGMACRO, "Http Server failed to listen on a port.", ERROR); } - debug(DEBUGMACRO, "Listening on port " + QString::number(port)); + debug(DEBUGMACRO, "Listening on port " + QString::number(port), INFO); } void Pws2mqtt::parseData(QList> queryList) { debug(DEBUGMACRO, "Parsing Datas", DEBUG); +#ifdef WITH_MQTT QString jsonString = "{"; - //QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\""; + QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\""; +#endif QString notif = ""; QStringList priorityList {"", "min", "low", "default", "High", "urgent"}; QString attachment = ""; @@ -212,24 +212,29 @@ void Pws2mqtt::parseData(QList> queryList) if(deviceFlag == false) { deviceFlag = true; - }/*else + } +#ifdef WITH_MQTT + else { deviceString.append(", "); } deviceString += "\"" + name + "\": "; - deviceString += pair.second;*/ + deviceString += pair.second; +#endif }else { if(propertyFlag == false) { propertyFlag = true; - }else + } +#ifdef WITH_MQTT + else { jsonString.append(", "); } jsonString.append("\"" + name + "\": "); jsonString.append(pair.second); - +#endif if (name == "tempf") { static QDateTime timeTemp = QDateTime::currentDateTime().addSecs(-600); @@ -412,7 +417,8 @@ void Pws2mqtt::parseData(QList> queryList) //windChill(propertiesValue["tempf"], propertiesValue["vent"]); //calculerHumidex(propertiesValue["tempf"], propertiesValue["humidity"]); - /*if (!jsonString.isEmpty()) +#ifdef WITH_MQTT + if (!jsonString.isEmpty()) { debug(DEBUGMACRO, "json string : " + jsonString, DEBUG); jsonString = jsonString +", " + deviceString + "}}"; @@ -420,7 +426,8 @@ void Pws2mqtt::parseData(QList> queryList) }else { debug(DEBUGMACRO, "No values to send", DEBUG); - }*/ + } +#endif debug(DEBUGMACRO, "current datetime : " + QDateTime::currentDateTime().toString() + ", timer = " + timer.toString(), DEBUG); diff --git a/src/utcicalculator.cpp b/src/utcicalculator.cpp index ec2c923..aad1ff7 100644 --- a/src/utcicalculator.cpp +++ b/src/utcicalculator.cpp @@ -12,33 +12,6 @@ UtciCalculator::UtciCalculator(QObject *parent) /*// Loads coefficients from a simple text file containing one coefficient per line or tab-separated. // The expected file is the ESM_3 coefficients table exported to a plain text file where the coefficients // appear in the same order as in the official table (210 coefficients). -bool UtciCalculator::loadCoefficientsFromFile(const QString &path) -{ - QFile f(path); - if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) - return false; - - QTextStream in(&f); - QVector coeffs; - coeffs.reserve(300); - - while (!in.atEnd()) { - QString line = in.readLine().trimmed(); - if (line.isEmpty()) continue; - // Allow lines with comments or 'term coefficient' headings: - // We try to extract numbers from the line. - QStringList parts = line.split(QRegularExpression("[\\s,\\t]+"), Qt::SkipEmptyParts); - for (const QString &p : parts) { - bool ok = false; - double val = p.toDouble(&ok); - if (ok) coeffs.append(val); - } - } - if (coeffs.isEmpty()) return false; - - m_coeffs = coeffs; - return true; -} */ // Magnus formula to estimate saturation vapour pressure (hPa) then return pa in kPa double UtciCalculator::vapourPressureFromRH(double Ta, double rh)