pws2mqtt-qt/main.cpp

119 lines
2.6 KiB
C++
Raw Permalink Normal View History

2024-09-10 20:34:16 +02:00
#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"
//#include "version.h"
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#define CLIENT_ID "Client_ID"
#define BROKER_ADDRESS "localhost"
#define MQTT_PORT 1883;
uint debugLevel = ALL;
QByteArray toFollow;
//class MqttClient;
using namespace std;
//Configuration config;
Pws2mqtt *pws2mqtt;
MqttClient *mqttClient;
QHttpServer *httpServer;
int main(int argc, char *argv[])
{
debug(DEBUGMACRO, "QcoreApplication", DEBUG);
QCoreApplication a(argc, argv);
// Enable logging to journald
qputenv("QT_FORCE_STDERR_LOGGING", QByteArray("0"));
debug(DEBUGMACRO, "declaration of mqttClient", DEBUG);
mqttClient = new MqttClient;
httpServer = new QHttpServer;
// declaration of debug level
pws2mqtt = new Pws2mqtt;
pws2mqtt->listeningHttp();
a.exec();
mqttClient->qmqttClient->unsubscribe(mqttClient->topic);
mqttClient->qmqttClient->disconnectFromHost();
debug(DEBUGMACRO, "exiting", DEBUG);
//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)
{
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);
}
if ((debugLevel & level) == 8)
{
qInfo("%s%s ERROR: %s%s", debugHeader.toStdString().c_str(), RED, msg.toStdString().c_str(), NORMAL);
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);
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);
}
}