fixed missing requestId issue
parent
1ba818c69f
commit
44407f36a2
|
|
@ -91,7 +91,6 @@ QUuid AirQualityIndex::searchByName(const QString &name)
|
||||||
qCWarning(dcAirQualityIndex()) << "Received invalide JSON object";
|
qCWarning(dcAirQualityIndex()) << "Received invalide JSON object";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emit requestExecuted(requestId, true);
|
|
||||||
|
|
||||||
QList<Station> stations;
|
QList<Station> stations;
|
||||||
QVariantList stationList = doc.toVariant().toMap().value("data").toList();
|
QVariantList stationList = doc.toVariant().toMap().value("data").toList();
|
||||||
|
|
@ -109,6 +108,8 @@ QUuid AirQualityIndex::searchByName(const QString &name)
|
||||||
}
|
}
|
||||||
if (!stations.isEmpty())
|
if (!stations.isEmpty())
|
||||||
emit stationsReceived(requestId, stations);
|
emit stationsReceived(requestId, stations);
|
||||||
|
|
||||||
|
requestExecuted(requestId, true);
|
||||||
});
|
});
|
||||||
return requestId;
|
return requestId;
|
||||||
}
|
}
|
||||||
|
|
@ -143,7 +144,9 @@ QUuid AirQualityIndex::getDataByIp()
|
||||||
qCWarning(dcAirQualityIndex()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcAirQualityIndex()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
parseData(requestId, reply->readAll());
|
if (!parseData(requestId, reply->readAll()))
|
||||||
|
requestExecuted(requestId, false);
|
||||||
|
requestExecuted(requestId, true);
|
||||||
});
|
});
|
||||||
return requestId;
|
return requestId;
|
||||||
}
|
}
|
||||||
|
|
@ -178,24 +181,23 @@ QUuid AirQualityIndex::getDataByGeolocation(const QString &lat, const QString &l
|
||||||
qCWarning(dcAirQualityIndex()) << "Request error:" << status << reply->errorString();
|
qCWarning(dcAirQualityIndex()) << "Request error:" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!parseData(requestId, reply->readAll()))
|
||||||
|
requestExecuted(requestId, false);
|
||||||
requestExecuted(requestId, true);
|
requestExecuted(requestId, true);
|
||||||
parseData(requestId, reply->readAll());
|
|
||||||
});
|
});
|
||||||
return requestId;
|
return requestId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AirQualityIndex::parseData(QUuid requestId, const QByteArray &data)
|
bool AirQualityIndex::parseData(QUuid requestId, const QByteArray &data)
|
||||||
{
|
{
|
||||||
qCDebug(dcAirQualityIndex()) << "Parsing data" << data;
|
qCDebug(dcAirQualityIndex()) << "Parsing data" << data;
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
emit requestExecuted(requestId, false);
|
|
||||||
qCWarning(dcAirQualityIndex()) << "Received invalide JSON object";
|
qCWarning(dcAirQualityIndex()) << "Received invalide JSON object";
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
emit requestExecuted(requestId, true);
|
|
||||||
Station station;
|
Station station;
|
||||||
station.aqi = doc.toVariant().toMap().value("data").toMap().value("aqi").toInt();
|
station.aqi = doc.toVariant().toMap().value("data").toMap().value("aqi").toInt();
|
||||||
station.idx = doc.toVariant().toMap().value("data").toMap().value("idx").toInt();
|
station.idx = doc.toVariant().toMap().value("data").toMap().value("idx").toInt();
|
||||||
|
|
@ -228,4 +230,5 @@ void AirQualityIndex::parseData(QUuid requestId, const QByteArray &data)
|
||||||
aqiData.temperature = iaqi["t"].toMap().value("v").toDouble();
|
aqiData.temperature = iaqi["t"].toMap().value("v").toDouble();
|
||||||
aqiData.windSpeed = iaqi["w"].toMap().value("v").toDouble();
|
aqiData.windSpeed = iaqi["w"].toMap().value("v").toDouble();
|
||||||
emit dataReceived(requestId, aqiData);
|
emit dataReceived(requestId, aqiData);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ private:
|
||||||
QString m_baseUrl = "https://api.waqi.info";
|
QString m_baseUrl = "https://api.waqi.info";
|
||||||
QString m_apiKey;
|
QString m_apiKey;
|
||||||
|
|
||||||
void parseData(QUuid requestId, const QByteArray &data);
|
bool parseData(QUuid requestId, const QByteArray &data);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void stationsReceived(QUuid requestId, QList<Station> stations);
|
void stationsReceived(QUuid requestId, QList<Station> stations);
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,17 @@ IntegrationPluginAqi::IntegrationPluginAqi()
|
||||||
|
|
||||||
void IntegrationPluginAqi::startPairing(ThingPairingInfo *info)
|
void IntegrationPluginAqi::startPairing(ThingPairingInfo *info)
|
||||||
{
|
{
|
||||||
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter your API token for Air Quality Index"));
|
NetworkAccessManager *network = hardwareManager()->networkManager();
|
||||||
|
QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://api.waqi.info")));
|
||||||
|
connect(reply, &QNetworkReply::finished, this, [reply, info] {
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) {
|
||||||
|
info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("Air quality index server is not reachable."));
|
||||||
|
} else {
|
||||||
|
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter your API token for Air Quality Index"));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntegrationPluginAqi::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret)
|
void IntegrationPluginAqi::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret)
|
||||||
|
|
@ -116,6 +126,13 @@ void IntegrationPluginAqi::setupThing(ThingSetupInfo *info)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
// An AQI connection might be setup because of an discovery request
|
||||||
|
// or because there is already another thing using the connection
|
||||||
|
// In any case the API key is being updated to avoid using the discovery key.
|
||||||
|
pluginStorage()->beginGroup(info->thing()->id().toString());
|
||||||
|
QString apiKey = pluginStorage()->value("apiKey").toString();
|
||||||
|
pluginStorage()->endGroup();
|
||||||
|
m_aqiConnection->setApiKey(apiKey);
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -128,8 +145,10 @@ void IntegrationPluginAqi::postSetupThing(Thing *thing)
|
||||||
{
|
{
|
||||||
if (thing->thingClassId() == airQualityIndexThingClassId) {
|
if (thing->thingClassId() == airQualityIndexThingClassId) {
|
||||||
|
|
||||||
if (!m_aqiConnection)
|
if (!m_aqiConnection) {
|
||||||
|
qCWarning(dcAirQualityIndex()) << "Air quality connection not initialized";
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QString longitude = thing->paramValue(airQualityIndexThingLongitudeParamTypeId).toString();
|
QString longitude = thing->paramValue(airQualityIndexThingLongitudeParamTypeId).toString();
|
||||||
QString latitude = thing->paramValue(airQualityIndexThingLatitudeParamTypeId).toString();
|
QString latitude = thing->paramValue(airQualityIndexThingLatitudeParamTypeId).toString();
|
||||||
|
|
@ -147,11 +166,10 @@ void IntegrationPluginAqi::thingRemoved(Thing *thing)
|
||||||
{
|
{
|
||||||
Q_UNUSED(thing)
|
Q_UNUSED(thing)
|
||||||
if (myThings().empty()) {
|
if (myThings().empty()) {
|
||||||
if (!m_pluginTimer) {
|
if (m_pluginTimer) {
|
||||||
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
|
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer);
|
||||||
m_pluginTimer = nullptr;
|
m_pluginTimer = nullptr; }
|
||||||
}
|
if (m_aqiConnection) {
|
||||||
if (!m_aqiConnection) {
|
|
||||||
m_aqiConnection->deleteLater();
|
m_aqiConnection->deleteLater();
|
||||||
m_aqiConnection = nullptr;
|
m_aqiConnection = nullptr;
|
||||||
}
|
}
|
||||||
|
|
@ -160,8 +178,10 @@ void IntegrationPluginAqi::thingRemoved(Thing *thing)
|
||||||
|
|
||||||
void IntegrationPluginAqi::onAirQualityDataReceived(QUuid requestId, AirQualityIndex::AirQualityData data)
|
void IntegrationPluginAqi::onAirQualityDataReceived(QUuid requestId, AirQualityIndex::AirQualityData data)
|
||||||
{
|
{
|
||||||
|
qCDebug(dcAirQualityIndex()) << "Air Quality data received, request id:" << requestId << "is an async request:" << m_asyncRequests.contains(requestId);
|
||||||
|
|
||||||
if (m_asyncSetups.contains(requestId)) {
|
if (m_asyncSetups.contains(requestId)) {
|
||||||
ThingSetupInfo *info = m_asyncSetups.take(requestId);
|
ThingSetupInfo *info = m_asyncSetups.value(requestId);
|
||||||
return info->finish(Thing::ThingErrorNoError);
|
return info->finish(Thing::ThingErrorNoError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -206,6 +226,7 @@ void IntegrationPluginAqi::onAirQualityDataReceived(QUuid requestId, AirQualityI
|
||||||
|
|
||||||
void IntegrationPluginAqi::onAirQualityStationsReceived(QUuid requestId, QList<AirQualityIndex::Station> stations)
|
void IntegrationPluginAqi::onAirQualityStationsReceived(QUuid requestId, QList<AirQualityIndex::Station> stations)
|
||||||
{
|
{
|
||||||
|
qCDebug(dcAirQualityIndex()) << "Air Quality Stations received, request id:" << requestId << "is an async request:" << m_asyncRequests.contains(requestId);
|
||||||
if (m_asyncDiscovery.contains(requestId)) {
|
if (m_asyncDiscovery.contains(requestId)) {
|
||||||
ThingDiscoveryInfo *info = m_asyncDiscovery.take(requestId);
|
ThingDiscoveryInfo *info = m_asyncDiscovery.take(requestId);
|
||||||
foreach(AirQualityIndex::Station station, stations) {
|
foreach(AirQualityIndex::Station station, stations) {
|
||||||
|
|
@ -221,9 +242,11 @@ void IntegrationPluginAqi::onAirQualityStationsReceived(QUuid requestId, QList<A
|
||||||
|
|
||||||
|
|
||||||
if (m_asyncRequests.contains(requestId)) {
|
if (m_asyncRequests.contains(requestId)) {
|
||||||
Thing * thing = myThings().findById(m_asyncRequests.take(requestId));
|
Thing * thing = myThings().findById(m_asyncRequests.value(requestId));
|
||||||
if (!thing)
|
if (!thing) {
|
||||||
|
qCWarning(dcAirQualityIndex()) << "Can't find thing, associated to this async request";
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
if (stations.length() != 0) {
|
if (stations.length() != 0) {
|
||||||
thing->setStateValue(airQualityIndexStationNameStateTypeId, stations.first().name);
|
thing->setStateValue(airQualityIndexStationNameStateTypeId, stations.first().name);
|
||||||
}
|
}
|
||||||
|
|
@ -246,11 +269,14 @@ void IntegrationPluginAqi::onPluginTimer()
|
||||||
|
|
||||||
void IntegrationPluginAqi::onRequestExecuted(QUuid requestId, bool success)
|
void IntegrationPluginAqi::onRequestExecuted(QUuid requestId, bool success)
|
||||||
{
|
{
|
||||||
|
qCDebug(dcAirQualityIndex()) << "Request executd, requestId:" << requestId << "Success:" << success << "is an async request:" << m_asyncRequests.contains(requestId);
|
||||||
if (m_asyncRequests.contains(requestId)) {
|
if (m_asyncRequests.contains(requestId)) {
|
||||||
|
|
||||||
Thing *thing = myThings().findById(m_asyncRequests.value(requestId));
|
Thing *thing = myThings().findById(m_asyncRequests.value(requestId));
|
||||||
thing->setStateValue(airQualityIndexConnectedStateTypeId, success);
|
thing->setStateValue(airQualityIndexConnectedStateTypeId, success);
|
||||||
if (!success)
|
if (!success) {
|
||||||
m_asyncRequests.remove(requestId);
|
qCWarning(dcAirQualityIndex()) << "Request failed, removing request from async request list";
|
||||||
|
}
|
||||||
|
m_asyncRequests.remove(requestId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue