debug + adding notification

This commit is contained in:
2025-09-24 23:26:30 +02:00
parent c04446c5e9
commit 2816e0a70b
3 changed files with 121 additions and 46 deletions

View File

@@ -61,7 +61,7 @@ qint32 MqttClient::send_message(QString message)
mid = this->qmqttClient->publish(this->topic, message.toUtf8(), this->qos, true); mid = this->qmqttClient->publish(this->topic, message.toUtf8(), this->qos, true);
if (mid == -1) if (mid == -1)
{ {
debug(DEBUGMACRO, "Error, can't publish message : " + this->qmqttErrors[this->qmqttClient->error()], ERROR); debug(DEBUGMACRO, "Error, can't publish message : " + this->qmqttErrors[this->qmqttClient->error()], WARNING);
} }
messagesList.insert(mid, message); messagesList.insert(mid, message);
return (mid); return (mid);
@@ -88,7 +88,7 @@ void MqttClient::on_messageSentStatusChanged(qint32 id, QMqtt::MessageStatus s,
void MqttClient::on_error(QMqttClient::ClientError error) void MqttClient::on_error(QMqttClient::ClientError error)
{ {
debug(DEBUGMACRO, "Error " + qmqttErrors[error], ERROR); debug(DEBUGMACRO, "Error " + qmqttErrors[error], WARNING);
} }
void MqttClient::on_message(const QByteArray &message, const QMqttTopicName &topic) void MqttClient::on_message(const QByteArray &message, const QMqttTopicName &topic)
@@ -125,7 +125,7 @@ void MqttClient::updateStatus(QMqttSubscription::SubscriptionState state)
debug(DEBUGMACRO, "Subscribed", INFO); debug(DEBUGMACRO, "Subscribed", INFO);
break; break;
case QMqttSubscription::Error: case QMqttSubscription::Error:
debug(DEBUGMACRO, "Subscription error" + this->subscription->reason(), ERROR); debug(DEBUGMACRO, "Subscription error" + this->subscription->reason(), WARNING);
break; break;
case QMqttSubscription::UnsubscriptionPending: case QMqttSubscription::UnsubscriptionPending:
debug(DEBUGMACRO, "Pending Unsubscription", DEBUG); debug(DEBUGMACRO, "Pending Unsubscription", DEBUG);

View File

@@ -4,6 +4,7 @@
#include <unistd.h> #include <unistd.h>
#include <QtGlobal> #include <QtGlobal>
#include <iostream> #include <iostream>
#include <QList>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <QtHttpServer/QHttpServer> #include <QtHttpServer/QHttpServer>
@@ -13,6 +14,28 @@ extern MqttClient *mqttClient;
extern Pws2mqtt *pws2mqtt; extern Pws2mqtt *pws2mqtt;
extern QHttpServer *httpServer; extern QHttpServer *httpServer;
QMap <QByteArray, QByteArray> propertyList;
QMap <QByteArray, QPair<QString, QByteArray>> propertyName
{
{"tempf", {"Température extérieure", "°C"}},
{"humidity", {"Humidité extérieure", "%"}},
{"dewptf", {"Point de rosée", "°C"}},
{"windchillf", {"Température ressentie", "°C"}},
{"windir", {"Direction du vent", "°"}},
{"windspeedmph", {"vitesse du vent", "km/h"}},
{"windgustmph", {"Rafales", "km/h"}},
{"rainin", {"Pluie", "mm/h"}},
{"dailyrainin", {"Pluie de la journée", "mm"}},
{"monthlyrainin", {"Pluie du mois", "mm"}},
{"yearlyrainin", {"Pluie de l'année", "mm"}},
{"solarradiation", {"Énergie solaire", "W/m²"}},
{"indoortempf", {"Température intérieure", "°C"}},
{"indoorhumidity", {"Humidité intérieure", "%"}},
{"baromin", {"pression atmosphérique", "hPa"}},
{"lowbatt", {"Batterie faible", ""}},
{"UV", {"UV", ""}}
};
Pws2mqtt::Pws2mqtt() Pws2mqtt::Pws2mqtt()
{ {
@@ -21,31 +44,40 @@ Pws2mqtt::Pws2mqtt()
Pws2mqtt::~Pws2mqtt() Pws2mqtt::~Pws2mqtt()
{ {
} }
void Pws2mqtt::init() void Pws2mqtt::init()
{ {
debug(DEBUGMACRO, "init http server", DEBUG); debug(DEBUGMACRO, "init http server", DEBUG);
httpServer->setMissingHandler([](const QHttpServerRequest &request, QHttpServerResponder &&responder)
{
(void) responder;
debug(DEBUGMACRO, "body " + request.url().toString(), WARNING);
return QHttpServerResponse("404 - Page non trouvée. Route par défaut.", QHttpServerResponse::StatusCode::NotFound);
//responder.write(QHttpServerResponse("404 - Page non trouvée. Route par défaut.", QHttpServerResponse::StatusCode::NotFound));
});
httpServer->route("/", [](const QHttpServerRequest &request)
{
QList<std::pair<QByteArray, QByteArray>> headersList = request.headers().toList();
debug(DEBUGMACRO, "Remote address" + request.remoteAddress().toString(), WARNING);
return QHttpServerResponse("text/plain", "Failed\n");
});
httpServer->route("/query", [this](const QHttpServerRequest &request) httpServer->route("/query", [this](const QHttpServerRequest &request)
{ {
//QString data; QByteArray data;
QList<std::pair<QString, QString>> queryList = request.query().queryItems();
//QTextStream result(&data); //QTextStream result(&data);
debug(DEBUGMACRO, "Request body :" + request.body() , DEBUG);
/* debug(DEBUGMACRO, "Request value :" + request.query().toString() , DEBUG); if (queryList.isEmpty())
debug(DEBUGMACRO, "Request value :" + request.url().toString() , DEBUG);
for (auto pair : request.query().queryItems())
{ {
if (!data.isEmpty()) debug(DEBUGMACRO, "Request query is empty", WARNING);
{ }else
result << "\n"; {
} debug(DEBUGMACRO, "Request query :" + request.query().toString() , DEBUG);
debug(DEBUGMACRO, pair.first + "=" + pair.second, DEBUG); this->parseData(queryList);
}*/ debug(DEBUGMACRO, "Returning 'success'", DEBUG);
}
this->parseData(request.body());
debug(DEBUGMACRO, "Returning 'success'", DEBUG);
//mqttClient.send_message(jsonString); //mqttClient.send_message(jsonString);
return QHttpServerResponse("text/plain", "Success\n"); return QHttpServerResponse("text/plain", "Success\n");
@@ -66,41 +98,34 @@ void Pws2mqtt::listeningHttp()
} }
void Pws2mqtt::parseData(QByteArray data) void Pws2mqtt::parseData(QList<std::pair<QString, QString>> queryList)
{ {
QList<QByteArray> dataList; debug(DEBUGMACRO, "Parsing Datas", DEBUG);
QString jsonString = "{"; QString jsonString = "{";
QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", " + "\"powerSource\": \"Battery\", ";
QList<QByteArray> dataSplitted;
QString str;
uint j;
uint listSize;
bool propertyFlag = false; bool propertyFlag = false;
bool deviceFlag = false; bool deviceFlag = false;
QString topic; QString topic;
QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", \"powerSource\": \"Battery\"";
QString notif;
debug(DEBUGMACRO, "Parsing Datas", DEBUG); debug(DEBUGMACRO, "looping list of query", DEBUG);
dataList = data.split('&'); for (QPair<QString, QString> pair : queryList)
listSize = dataList.size() - 1;
for (j=2;j<listSize;j++)
{ {
debug(DEBUGMACRO, dataList[j], DEBUG); debug(DEBUGMACRO, pair.first + " = " + pair.second, DEBUG);
dataSplitted = dataList[j].split('='); if (this->deviceProperties.contains(pair.first))
str = dataSplitted[0];
if (this->deviceProperties.contains(str))
{ {
if(deviceFlag == false) if(deviceFlag == false)
{ {
deviceFlag = true; deviceFlag = true;
}else }else
{ {
deviceString.append(", "); deviceString.append(", ");
} }
deviceString.append("\"" + str + "\": "); deviceString += "\"" + pair.first + "\": ";
deviceString.append(addValue(dataSplitted[1])); deviceString += pair.second;
}else }else
{ {
if(propertyFlag == false) if(propertyFlag == false)
@@ -110,15 +135,61 @@ void Pws2mqtt::parseData(QByteArray data)
{ {
jsonString.append(", "); jsonString.append(", ");
} }
jsonString.append("\"" + str + "\": "); jsonString.append("\"" + pair.first + "\": ");
jsonString.append(addValue(dataSplitted[1])); jsonString.append(pair.second);
//mqttClient.send_message(topic, jsonString); //mqttClient->end_message(topic, jsonString);
if (pair.first == "tempf")
{
QByteArray tmp = pair.second.toLatin1();
if (compare (propertyList[pair.first.toLatin1()], tmp, 0.2))
{
notif += formatNotifString (pair, QByteArray::number(fahrenheitToCelsius(pair.second.toFloat())));
}
}else if (pair.first == "humidity")
//TODO
propertyList[pair.first.toLatin1()] = pair.second.toLatin1();
} }
} }
jsonString = jsonString +", " + deviceString + "}}"; if (!jsonString.isEmpty())
mqttClient->send_message(jsonString); {
jsonString = jsonString +", " + deviceString + "}}";
debug(DEBUGMACRO, "sent => " + jsonString, DEBUG); mqttClient->send_message(jsonString);
debug(DEBUGMACRO, "sent => " + jsonString, DEBUG);
}else
{
debug(DEBUGMACRO, "No values to send", DEBUG);
}
debug(DEBUGMACRO, "parseData: Returning", DEBUG); debug(DEBUGMACRO, "parseData: Returning", DEBUG);
//return jsonString; //return jsonString;
} }
double fahrenheitToCelsius(double fahrenheit)
{
return (fahrenheit - 32.0) * 5.0 / 9.0;
}
qfloat16 tohPa(qfloat16 value)
{
return value * 33.8639;
}
bool compare (QByteArray valueBA, QByteArray testValueBA, qfloat16 ecart)
{
qfloat16 value = valueBA.toFloat();
qfloat16 testValue = testValueBA.toFloat();
if (value <= (testValue - ecart) or value >= (testValue + ecart))
{
return true;
}else
{
return false;
}
}
QString formatNotifString (QPair <QString, QString> pair, QByteArray value)
{
return propertyName[pair.first.toLatin1()].first + " : " + value + propertyName[pair.first.toLatin1()].second;
}

View File

@@ -46,7 +46,11 @@ class Pws2mqtt : public QObject
void init(); void init();
void listeningHttp(); void listeningHttp();
void parseData(QByteArray data); void parseData(QList<std::pair<QString, QString>> queryList);
}; };
double fahrenheitToCelsius(double fahrenheit);
qfloat16 tohPa(qfloat16 value);
bool compare (QByteArray value, QByteArray testValue, qfloat16 ecart = 0.5);
QString formatNotifString (QPair<QString, QString> pair, QByteArray value);
#endif // PWS2MQTT_H #endif // PWS2MQTT_H