openweathermap added search(String)

This commit is contained in:
Simon Stürz 2014-04-13 21:56:21 +02:00
parent e4c27dfa88
commit 961079c9d9
4 changed files with 60 additions and 5 deletions

View File

@ -171,7 +171,7 @@ QList<DeviceClass> 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");
}

View File

@ -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<QVariantMap> 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);

View File

@ -24,6 +24,7 @@
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QVariantMap>
#include <QUrl>
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);

View File

@ -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