diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp index 3466b3f0..f0abd643 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp @@ -171,7 +171,7 @@ QList DevicePluginOpenweathermap::supportedDevices() const DeviceClass deviceClassOpenweathermap(pluginId(), openweathermapVendorId, DeviceClassId("985195aa-17ad-4530-88a4-cdd753d747d7")); deviceClassOpenweathermap.setName("Weather from openweathermap"); - deviceClassOpenweathermap.setCreateMethod(DeviceClass::CreateMethodDiscovery); + //deviceClassOpenweathermap.setCreateMethod(DeviceClass::CreateMethodDiscovery); // Params QVariantList params; @@ -311,7 +311,7 @@ PluginId DevicePluginOpenweathermap::pluginId() const void DevicePluginOpenweathermap::guhTimer() { - m_openweaher->update(); + m_openweaher->search("Vie"); } diff --git a/plugins/deviceplugins/openweathermap/openweathermap.cpp b/plugins/deviceplugins/openweathermap/openweathermap.cpp index 0a859bb6..0021578c 100644 --- a/plugins/deviceplugins/openweathermap/openweathermap.cpp +++ b/plugins/deviceplugins/openweathermap/openweathermap.cpp @@ -42,7 +42,7 @@ void OpenWeatherMap::updateSearchData() QNetworkRequest searchRequest; searchRequest.setUrl(QUrl(urlString)); - m_searchReply = m_manager->get(searchRequest); + m_searchLocationReply = m_manager->get(searchRequest); } void OpenWeatherMap::updateWeatherData() @@ -89,6 +89,42 @@ void OpenWeatherMap::processSearchResponse(QByteArray data) } //qDebug() << jsonDoc.toJson(); + QVariantMap dataMap = jsonDoc.toVariant().toMap(); + qDebug() << "----------------------------------------"; + qDebug() << "openweathermap search results"; + qDebug() << "----------------------------------------"; + QList cityList; + if(dataMap.contains("list")){ + QVariantList list = dataMap.value("list").toList(); + foreach (QVariant key, list) { + QVariantMap elemant = key.toMap(); + qDebug() << elemant.value("name").toString(); + qDebug() << elemant.value("sys").toMap().value("country").toString(); + qDebug() << elemant.value("id").toString(); + qDebug() << "--------------------------------------"; + + QVariantMap city; + city.insert("name",elemant.value("name").toString()); + city.insert("country", elemant.value("sys").toMap().value("country").toString()); + city.insert("id",elemant.value("id").toString()); + cityList.append(city); + } + } + qDebug() << "----------------------------------------"; + emit searchResultReady(cityList); + +} + +void OpenWeatherMap::processSearchLocationResponse(QByteArray data) +{ + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + + if(error.error != QJsonParseError::NoError) { + qWarning() << "failed to parse data" << data << ":" << error.errorString(); + } + //qDebug() << jsonDoc.toJson(); + QVariantMap dataMap = jsonDoc.toVariant().toMap(); if(dataMap.contains("list")){ QVariantList list = dataMap.value("list").toList(); @@ -217,6 +253,15 @@ void OpenWeatherMap::update() updateLocationData(); } +void OpenWeatherMap::search(const QString &searchString) +{ + QString urlString = "http://api.openweathermap.org/data/2.5/find?q=" + searchString + "&type=like&units=metric&mode=json"; + QNetworkRequest searchRequest; + searchRequest.setUrl(QUrl(urlString)); + + m_searchReply = m_manager->get(searchRequest); +} + void OpenWeatherMap::replyFinished(QNetworkReply *reply) { int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -234,6 +279,12 @@ void OpenWeatherMap::replyFinished(QNetworkReply *reply) return; } + if(reply == m_searchLocationReply && status == 200){ + data = reply->readAll(); + processSearchLocationResponse(data); + return; + } + if(reply == m_weatherReply && status == 200){ data = reply->readAll(); processWeatherResponse(data); diff --git a/plugins/deviceplugins/openweathermap/openweathermap.h b/plugins/deviceplugins/openweathermap/openweathermap.h index 9d402843..3fee5ed2 100644 --- a/plugins/deviceplugins/openweathermap/openweathermap.h +++ b/plugins/deviceplugins/openweathermap/openweathermap.h @@ -24,6 +24,7 @@ #include #include #include +#include #include class OpenWeatherMap : public QObject @@ -39,8 +40,9 @@ private: QString m_cityId; QNetworkReply *m_locationReply; - QNetworkReply *m_searchReply; + QNetworkReply *m_searchLocationReply; QNetworkReply *m_weatherReply; + QNetworkReply *m_searchReply; QString m_country; QString m_weatherDescription; @@ -62,6 +64,7 @@ private: void processLocationResponse(QByteArray data); void processSearchResponse(QByteArray data); + void processSearchLocationResponse(QByteArray data); void processWeatherResponse(QByteArray data); signals: @@ -69,6 +72,7 @@ signals: public slots: void update(); + void search(const QString &searchString); private slots: void replyFinished(QNetworkReply *reply); diff --git a/tests/scripts/addconfigureddevice.sh b/tests/scripts/addconfigureddevice.sh index ed24e9dc..345ccf51 100755 --- a/tests/scripts/addconfigureddevice.sh +++ b/tests/scripts/addconfigureddevice.sh @@ -34,7 +34,7 @@ else # (echo '{"id":1, "method":"Devices.AddConfiguredDevice", "params":{"deviceClassId": "{af2e15f0-650e-4452-b379-fa76a2dc46c6}","deviceParams":{"autodetect":"true"}}}'; sleep 1) | nc $1 1234 elif [ $2 == "openweathermap" ]; then # Adds a openweathermap device - (echo '{"id":1, "method":"Devices.AddConfiguredDevice", "params":{"deviceClassId": "{985195aa-17ad-4530-88a4-cdd753d747d7}","deviceParams":{"autodetect":"true"}}}'; sleep 1) | nc $1 1234 + (echo '{"id":1, "method":"Devices.AddConfiguredDevice", "params":{"deviceClassId": "{985195aa-17ad-4530-88a4-cdd753d747d7}","deviceParams":{"location":""}}}'; sleep 1) | nc $1 1234 else echo "unknown type $2. Possible values are: elroremote, elroswitch, intertechnoremote, wifidetector, mock1, mock2, openweathermap" fi