move mqtt in compilation's option

This commit is contained in:
DTux committed 2026-04-05 14:23:08 +02:00
1 parent ada4d7ed99
commit d4a6942ec4
3 files changed
+49 -85

No files matched your search

+24 -40
View File
@@ -1,41 +1,42 @@
#include <QCoreApplication>
#include <QString>
//#include <QObject>
#include <unistd.h>
#include <QtGlobal>
#include <iostream>
#include <QDebug>
#include <sys/socket.h>
#include <netinet/in.h>
//#include <QtMqtt>
//#include <QtMqtt/QMqttClient>
//#include <QtMqtt/QMqttMessage>
//#include <QtMqtt/QMqttSubscription>
#include <qhttpserver.h>
#include <QHttpServerResponse>
#include "pws2mqtt.h"
//#include "mqtt.h"
#ifdef WITH_MQTT
#include "mqtt.h"
#endif
#include "src/barometertrend.h"
#include "version.h"
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#ifdef WITH_MQTT
#include <QtMqtt>
#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttMessage>
#include <QtMqtt/QMqttSubscription>
#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)
{
+25 -18
View File
@@ -1,27 +1,24 @@
#include "pws2mqtt.h"
#include "barometertrend.h"
//#include "mqtt.h"
//#include "httpserver.h"
#include <cstring>
//#include <unistd.h>
#include <QtGlobal>
#include <iostream>
#include <QList>
//#include <sys/socket.h>
//#include <netinet/in.h>
#include <QtHttpServer/QHttpServer>
#include <QtHttpServer/QHttpServerRequest>
#include <curl/curl.h>
#include <QtMath>
#include <QImage>
#include <QTransform>
//#include <stdio.h>
#include <sys/stat.h>
#include "utcicalculator.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#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 <QByteArray, QPair<QString, QByteArray>> 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<std::pair<QString, QString>> 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<std::pair<QString, QString>> 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<std::pair<QString, QString>> 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<std::pair<QString, QString>> queryList)
}else
{
debug(DEBUGMACRO, "No values to send", DEBUG);
}*/
}
#endif
debug(DEBUGMACRO, "current datetime : " + QDateTime::currentDateTime().toString() + ", timer = " + timer.toString(), DEBUG);
-27
View File
@@ -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<double> 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)