openweathermap added search(String)
This commit is contained in:
parent
e4c27dfa88
commit
961079c9d9
@ -171,7 +171,7 @@ QList<DeviceClass> DevicePluginOpenweathermap::supportedDevices() const
|
|||||||
|
|
||||||
DeviceClass deviceClassOpenweathermap(pluginId(), openweathermapVendorId, DeviceClassId("985195aa-17ad-4530-88a4-cdd753d747d7"));
|
DeviceClass deviceClassOpenweathermap(pluginId(), openweathermapVendorId, DeviceClassId("985195aa-17ad-4530-88a4-cdd753d747d7"));
|
||||||
deviceClassOpenweathermap.setName("Weather from openweathermap");
|
deviceClassOpenweathermap.setName("Weather from openweathermap");
|
||||||
deviceClassOpenweathermap.setCreateMethod(DeviceClass::CreateMethodDiscovery);
|
//deviceClassOpenweathermap.setCreateMethod(DeviceClass::CreateMethodDiscovery);
|
||||||
|
|
||||||
// Params
|
// Params
|
||||||
QVariantList params;
|
QVariantList params;
|
||||||
@ -311,7 +311,7 @@ PluginId DevicePluginOpenweathermap::pluginId() const
|
|||||||
|
|
||||||
void DevicePluginOpenweathermap::guhTimer()
|
void DevicePluginOpenweathermap::guhTimer()
|
||||||
{
|
{
|
||||||
m_openweaher->update();
|
m_openweaher->search("Vie");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,7 @@ void OpenWeatherMap::updateSearchData()
|
|||||||
QNetworkRequest searchRequest;
|
QNetworkRequest searchRequest;
|
||||||
searchRequest.setUrl(QUrl(urlString));
|
searchRequest.setUrl(QUrl(urlString));
|
||||||
|
|
||||||
m_searchReply = m_manager->get(searchRequest);
|
m_searchLocationReply = m_manager->get(searchRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenWeatherMap::updateWeatherData()
|
void OpenWeatherMap::updateWeatherData()
|
||||||
@ -89,6 +89,42 @@ void OpenWeatherMap::processSearchResponse(QByteArray data)
|
|||||||
}
|
}
|
||||||
//qDebug() << jsonDoc.toJson();
|
//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();
|
QVariantMap dataMap = jsonDoc.toVariant().toMap();
|
||||||
if(dataMap.contains("list")){
|
if(dataMap.contains("list")){
|
||||||
QVariantList list = dataMap.value("list").toList();
|
QVariantList list = dataMap.value("list").toList();
|
||||||
@ -217,6 +253,15 @@ void OpenWeatherMap::update()
|
|||||||
updateLocationData();
|
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)
|
void OpenWeatherMap::replyFinished(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
@ -234,6 +279,12 @@ void OpenWeatherMap::replyFinished(QNetworkReply *reply)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(reply == m_searchLocationReply && status == 200){
|
||||||
|
data = reply->readAll();
|
||||||
|
processSearchLocationResponse(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(reply == m_weatherReply && status == 200){
|
if(reply == m_weatherReply && status == 200){
|
||||||
data = reply->readAll();
|
data = reply->readAll();
|
||||||
processWeatherResponse(data);
|
processWeatherResponse(data);
|
||||||
|
|||||||
@ -24,6 +24,7 @@
|
|||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
#include <QVariantMap>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
class OpenWeatherMap : public QObject
|
class OpenWeatherMap : public QObject
|
||||||
@ -39,8 +40,9 @@ private:
|
|||||||
QString m_cityId;
|
QString m_cityId;
|
||||||
|
|
||||||
QNetworkReply *m_locationReply;
|
QNetworkReply *m_locationReply;
|
||||||
QNetworkReply *m_searchReply;
|
QNetworkReply *m_searchLocationReply;
|
||||||
QNetworkReply *m_weatherReply;
|
QNetworkReply *m_weatherReply;
|
||||||
|
QNetworkReply *m_searchReply;
|
||||||
|
|
||||||
QString m_country;
|
QString m_country;
|
||||||
QString m_weatherDescription;
|
QString m_weatherDescription;
|
||||||
@ -62,6 +64,7 @@ private:
|
|||||||
|
|
||||||
void processLocationResponse(QByteArray data);
|
void processLocationResponse(QByteArray data);
|
||||||
void processSearchResponse(QByteArray data);
|
void processSearchResponse(QByteArray data);
|
||||||
|
void processSearchLocationResponse(QByteArray data);
|
||||||
void processWeatherResponse(QByteArray data);
|
void processWeatherResponse(QByteArray data);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
@ -69,6 +72,7 @@ signals:
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void update();
|
void update();
|
||||||
|
void search(const QString &searchString);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void replyFinished(QNetworkReply *reply);
|
void replyFinished(QNetworkReply *reply);
|
||||||
|
|||||||
@ -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
|
# (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
|
elif [ $2 == "openweathermap" ]; then
|
||||||
# Adds a openweathermap device
|
# 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
|
else
|
||||||
echo "unknown type $2. Possible values are: elroremote, elroswitch, intertechnoremote, wifidetector, mock1, mock2, openweathermap"
|
echo "unknown type $2. Possible values are: elroremote, elroswitch, intertechnoremote, wifidetector, mock1, mock2, openweathermap"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user