Files
pws2mqtt-qt/main.cpp

129 lines
3.1 KiB
C++

#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 <curl/curl.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include "utcicalculator.h"
#define CLIENT_ID "Client_ID"
#define BROKER_ADDRESS "localhost"
#define MQTT_PORT 1883;
uint debugLevel = DEBUG | INFO | NOTICE | INFO | WARNING | ERROR | ALERT;
QByteArray toFollow;
//class MqttClient;
using namespace std;
//Configuration config;
Pws2mqtt *pws2mqtt;
MqttClient *mqttClient;
QHttpServer *httpServer;
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"));
debug(DEBUGMACRO, "declaration of mqttClient", DEBUG);
mqttClient = new MqttClient;
httpServer = new QHttpServer;
//declaration of debug level
pws2mqtt = new Pws2mqtt;
pws2mqtt->listeningHttp();
//notify (QString("Program started"), "default");
a.exec();
mqttClient->qmqttClient->unsubscribe(mqttClient->topic);
mqttClient->qmqttClient->disconnectFromHost();
debug(DEBUGMACRO, "exiting", DEBUG);
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)
{
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);
}
}