diff --git a/mqtt.cpp b/mqtt.cpp index d7b5932..f1bedc9 100644 --- a/mqtt.cpp +++ b/mqtt.cpp @@ -61,7 +61,7 @@ qint32 MqttClient::send_message(QString message) mid = this->qmqttClient->publish(this->topic, message.toUtf8(), this->qos, true); 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); return (mid); @@ -88,7 +88,7 @@ void MqttClient::on_messageSentStatusChanged(qint32 id, QMqtt::MessageStatus s, 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) @@ -125,7 +125,7 @@ void MqttClient::updateStatus(QMqttSubscription::SubscriptionState state) debug(DEBUGMACRO, "Subscribed", INFO); break; case QMqttSubscription::Error: - debug(DEBUGMACRO, "Subscription error" + this->subscription->reason(), ERROR); + debug(DEBUGMACRO, "Subscription error" + this->subscription->reason(), WARNING); break; case QMqttSubscription::UnsubscriptionPending: debug(DEBUGMACRO, "Pending Unsubscription", DEBUG); diff --git a/pws2mqtt.cpp b/pws2mqtt.cpp index 76cab7c..9f5e6c1 100644 --- a/pws2mqtt.cpp +++ b/pws2mqtt.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,28 @@ extern MqttClient *mqttClient; extern Pws2mqtt *pws2mqtt; extern QHttpServer *httpServer; +QMap propertyList; +QMap > 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() { @@ -21,31 +44,40 @@ Pws2mqtt::Pws2mqtt() Pws2mqtt::~Pws2mqtt() { - } void Pws2mqtt::init() { 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> 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) { - //QString data; + QByteArray data; + QList> queryList = request.query().queryItems(); //QTextStream result(&data); - debug(DEBUGMACRO, "Request body :" + request.body() , DEBUG); - /* debug(DEBUGMACRO, "Request value :" + request.query().toString() , DEBUG); - debug(DEBUGMACRO, "Request value :" + request.url().toString() , DEBUG); - for (auto pair : request.query().queryItems()) + + if (queryList.isEmpty()) { - if (!data.isEmpty()) - { - result << "\n"; - } - debug(DEBUGMACRO, pair.first + "=" + pair.second, DEBUG); - }*/ - - this->parseData(request.body()); - debug(DEBUGMACRO, "Returning 'success'", DEBUG); - + debug(DEBUGMACRO, "Request query is empty", WARNING); + }else + { + debug(DEBUGMACRO, "Request query :" + request.query().toString() , DEBUG); + this->parseData(queryList); + debug(DEBUGMACRO, "Returning 'success'", DEBUG); + } //mqttClient.send_message(jsonString); return QHttpServerResponse("text/plain", "Success\n"); @@ -66,41 +98,34 @@ void Pws2mqtt::listeningHttp() } -void Pws2mqtt::parseData(QByteArray data) +void Pws2mqtt::parseData(QList> queryList) { - QList dataList; + debug(DEBUGMACRO, "Parsing Datas", DEBUG); + QString jsonString = "{"; - QString deviceString = "\"device\": {\"ieeeAddress\": \"" + mqttClient->macAddress + "\", \"type\": \"" + mqttClient->type + "\", " + "\"powerSource\": \"Battery\", "; - QList dataSplitted; - QString str; - uint j; - uint listSize; bool propertyFlag = false; bool deviceFlag = false; 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('&'); - listSize = dataList.size() - 1; - for (j=2;j pair : queryList) { - debug(DEBUGMACRO, dataList[j], DEBUG); + debug(DEBUGMACRO, pair.first + " = " + pair.second, DEBUG); - dataSplitted = dataList[j].split('='); - str = dataSplitted[0]; - - if (this->deviceProperties.contains(str)) + if (this->deviceProperties.contains(pair.first)) { if(deviceFlag == false) { - deviceFlag = true; + deviceFlag = true; }else { deviceString.append(", "); } - deviceString.append("\"" + str + "\": "); - deviceString.append(addValue(dataSplitted[1])); + deviceString += "\"" + pair.first + "\": "; + deviceString += pair.second; }else { if(propertyFlag == false) @@ -110,15 +135,61 @@ void Pws2mqtt::parseData(QByteArray data) { jsonString.append(", "); } - jsonString.append("\"" + str + "\": "); - jsonString.append(addValue(dataSplitted[1])); - //mqttClient.send_message(topic, jsonString); + jsonString.append("\"" + pair.first + "\": "); + jsonString.append(pair.second); + //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 + "}}"; - mqttClient->send_message(jsonString); - - debug(DEBUGMACRO, "sent => " + jsonString, DEBUG); + if (!jsonString.isEmpty()) + { + jsonString = jsonString +", " + deviceString + "}}"; + mqttClient->send_message(jsonString); + debug(DEBUGMACRO, "sent => " + jsonString, DEBUG); + }else + { + debug(DEBUGMACRO, "No values to send", DEBUG); + } debug(DEBUGMACRO, "parseData: Returning", DEBUG); //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 pair, QByteArray value) +{ + return propertyName[pair.first.toLatin1()].first + " : " + value + propertyName[pair.first.toLatin1()].second; + +} + diff --git a/pws2mqtt.h b/pws2mqtt.h index 16141b6..de26147 100644 --- a/pws2mqtt.h +++ b/pws2mqtt.h @@ -46,7 +46,11 @@ class Pws2mqtt : public QObject void init(); void listeningHttp(); - void parseData(QByteArray data); + void parseData(QList> queryList); }; +double fahrenheitToCelsius(double fahrenheit); +qfloat16 tohPa(qfloat16 value); +bool compare (QByteArray value, QByteArray testValue, qfloat16 ecart = 0.5); +QString formatNotifString (QPair pair, QByteArray value); #endif // PWS2MQTT_H