Files
pws2mqtt-qt/src/main.cpp
T
2026-04-12 21:16:24 +02:00

115 lines
2.8 KiB
C++

#include <QCoreApplication>
#include <QString>
//#include <unistd.h>
#include <QtGlobal>
//#include <iostream>
//#include <QDebug>
#include <qhttpserver.h>
#include <QHttpServerResponse>
#include "pws2mqtt.h"
#ifdef WITH_MQTT
#include "mqtt.h"
#endif
#include "src/barometertrend.h"
#include "version.h"
//#include <sys/types.h>
//#include <sys/select.h>
#ifdef WITH_MQTT
#include <QtMqtt>
#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttMessage>
#include <QtMqtt/QMqttSubscription>
#endif
#define CLIENT_ID "Client_ID"
uint debugLevel = INFO | ERROR | ALERT;
QByteArray toFollow;
#ifdef WITH_MQTT
class MqttClient;
MqttClient *mqttClient;
#endif
using namespace std;
//Configuration config;
Pws2mqtt *pws2mqtt;
QHttpServer *httpServer;
BarometerTrend *baro;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (argc > 1)
{
if (strcmp(argv[1], "v"))
{
printf ("Version : %s\n", version.c_str());
exit(0);
}
}
// Enable logging to journald
qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0"));
#ifdef WITH_MQTT
debug(DEBUGMACRO, "declaration of mqttClient", INFO);
mqttClient = new MqttClient;
#endif
httpServer = new QHttpServer;
//declaration of debug level
pws2mqtt = new Pws2mqtt;
baro = new BarometerTrend;
pws2mqtt->listeningHttp();
a.exec();
#ifdef WITH_MQTT
mqttClient->qmqttClient->unsubscribe(mqttClient->topic);
mqttClient->qmqttClient->disconnectFromHost();
#endif
debug(DEBUGMACRO, "exiting", INFO);
notify ("Program exiting \n", "high");
}
void debug(QString debugHeader, QString msg, uint8_t level, QByteArray property)
{
msg.replace("\n", "\\n");
if ((debugLevel & level) == 1)
{
qInfo("%s%sINFO: %s%s", debugHeader.toStdString().c_str(), GREEN, msg.toStdString().c_str(), NORMAL);
}
if ((debugLevel & level) == 2)
{
qInfo("%s%s NOTICE: %s%s", debugHeader.toStdString().c_str(), GREEN, msg.toStdString().c_str(), NORMAL);
}
if ((debugLevel & level) == 4)
{
qInfo("%s%s WARNING: %s%s", debugHeader.toStdString().c_str(), ORANGE, msg.toStdString().c_str(), NORMAL);
notify (debugHeader + " - " + msg);
}
if ((debugLevel & level) == 8)
{
qInfo("%s%s ERROR: %s%s", debugHeader.toStdString().c_str(), RED, msg.toStdString().c_str(), NORMAL);
notify ("Program error : " + debugHeader + " - " + msg, "high");
exit(1);
}
if ((debugLevel & level) == 16)
{
qInfo("%s%s DEBUG: %s%s", debugHeader.toStdString().c_str(), GREEN, ("'" + msg.toStdString() + "'").c_str(), NORMAL);
}
if ((debugLevel & level) == 32)
{
qInfo("%s%s ALERT: %s%s", debugHeader.toStdString().c_str(), RED, msg.toStdString().c_str(), NORMAL);
notify ("Program exiting " + debugHeader + " - " + msg, "high" );
exit(2);
}
if (toFollow == property and !toFollow.isEmpty())
{
qInfo("%s%s FOLLOWING %s => %s%s", debugHeader.toStdString().c_str(), GREEN, property.toStdString().c_str(), msg.toStdString().c_str(), NORMAL);
}
}