This commit is contained in:
Simon Stürz 2015-09-29 12:23:17 +02:00 committed by Michael Zanetti
parent deb268d59b
commit 6b0025125b
3 changed files with 72 additions and 37 deletions

View File

@ -29,7 +29,7 @@
The plugin offers two different search methods: if the user searches for a empty string, The plugin offers two different search methods: if the user searches for a empty string,
the plugin makes an autodetction with the WAN ip and offers the user the found autodetectresult. the plugin makes an autodetction with the WAN ip and offers the user the found autodetectresult.
The autodetection function uses the geolocation of your WAN ip and searches all available weather The autodetection function uses the geolocation of your WAN ip and searches all available weather
stations in a radius of 2.5 km. Otherwise the plugin returns the list of the found search results stations in a radius of 1.5 km. Otherwise the plugin returns the list of the found search results
from the search string. from the search string.
\underline{NOTE}: If you are using a VPN connection, the autodetection will show the results around of your VPN location. \underline{NOTE}: If you are using a VPN connection, the autodetection will show the results around of your VPN location.
@ -60,6 +60,8 @@
#include <QDebug> #include <QDebug>
#include <QJsonDocument> #include <QJsonDocument>
#include <QVariantMap> #include <QVariantMap>
#include <QUrl>
#include <QUrlQuery>
#include <QDateTime> #include <QDateTime>
@ -91,11 +93,9 @@ DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const Dev
DeviceManager::DeviceSetupStatus DevicePluginOpenweathermap::setupDevice(Device *device) DeviceManager::DeviceSetupStatus DevicePluginOpenweathermap::setupDevice(Device *device)
{ {
if (device->deviceClassId() != openweathermapDeviceClassId) { if (device->deviceClassId() != openweathermapDeviceClassId)
return DeviceManager::DeviceSetupStatusFailure; return DeviceManager::DeviceSetupStatusFailure;
}
device->setName("Weather from OpenWeatherMap (" + device->paramValue("location").toString() + ")");
update(device); update(device);
return DeviceManager::DeviceSetupStatusSuccess; return DeviceManager::DeviceSetupStatusSuccess;
@ -110,8 +110,9 @@ DeviceManager::DeviceError DevicePluginOpenweathermap::executeAction(Device *dev
{ {
if(action.actionTypeId() == updateWeatherActionTypeId){ if(action.actionTypeId() == updateWeatherActionTypeId){
update(device); update(device);
return DeviceManager::DeviceErrorNoError;
} }
return DeviceManager::DeviceErrorNoError; return DeviceManager::DeviceErrorActionTypeNotFound;
} }
void DevicePluginOpenweathermap::deviceRemoved(Device *device) void DevicePluginOpenweathermap::deviceRemoved(Device *device)
@ -130,6 +131,8 @@ void DevicePluginOpenweathermap::networkManagerReplyReady(QNetworkReply *reply)
{ {
if (reply->error()) { if (reply->error()) {
qCWarning(dcOpenWeatherMap) << "OpenWeatherMap reply error: " << reply->errorString(); qCWarning(dcOpenWeatherMap) << "OpenWeatherMap reply error: " << reply->errorString();
reply->deleteLater();
return;
} }
if (m_autodetectionReplies.contains(reply)) { if (m_autodetectionReplies.contains(reply)) {
@ -149,7 +152,6 @@ void DevicePluginOpenweathermap::networkManagerReplyReady(QNetworkReply *reply)
Device* device = m_weatherReplies.take(reply); Device* device = m_weatherReplies.take(reply);
processWeatherData(data, device); processWeatherData(data, device);
} }
reply->deleteLater(); reply->deleteLater();
} }
@ -161,54 +163,64 @@ void DevicePluginOpenweathermap::guhTimer()
void DevicePluginOpenweathermap::update() void DevicePluginOpenweathermap::update()
{ {
foreach (Device *device, myDevices()) { foreach (Device *device, myDevices()) {
QString cityId = device->paramValue("id").toString(); QUrl url("http://api.openweathermap.org/data/2.5/weather");
QString urlString = "http://api.openweathermap.org/data/2.5/weather?id="+ cityId + "&mode=json&units=metric"; QUrlQuery query;
QNetworkRequest weatherRequest; query.addQueryItem("id", device->paramValue("id").toString());
weatherRequest.setUrl(QUrl(urlString)); query.addQueryItem("mode", "json");
query.addQueryItem("units", "metric");
url.setQuery(query);
QNetworkReply *reply = networkManagerGet(weatherRequest); QNetworkReply *reply = networkManagerGet(QNetworkRequest(url));
m_weatherReplies.insert(reply, device); m_weatherReplies.insert(reply, device);
} }
} }
void DevicePluginOpenweathermap::update(Device *device) void DevicePluginOpenweathermap::update(Device *device)
{ {
QString cityId = device->paramValue("id").toString(); QUrl url("http://api.openweathermap.org/data/2.5/weather");
QString urlString = "http://api.openweathermap.org/data/2.5/weather?id="+ cityId + "&mode=json&units=metric"; QUrlQuery query;
QNetworkRequest weatherRequest; query.addQueryItem("id", device->paramValue("id").toString());
weatherRequest.setUrl(QUrl(urlString)); query.addQueryItem("mode", "json");
query.addQueryItem("units", "metric");
url.setQuery(query);
QNetworkReply *reply = networkManagerGet(weatherRequest); QNetworkReply *reply = networkManagerGet(QNetworkRequest(url));
m_weatherReplies.insert(reply, device); m_weatherReplies.insert(reply, device);
} }
void DevicePluginOpenweathermap::searchAutodetect() void DevicePluginOpenweathermap::searchAutodetect()
{ {
QString urlString = "http://ip-api.com/json"; QNetworkReply *reply = networkManagerGet(QNetworkRequest(QUrl("http://ip-api.com/json")));
QNetworkRequest locationRequest;
locationRequest.setUrl(QUrl(urlString));
QNetworkReply *reply = networkManagerGet(locationRequest);
m_autodetectionReplies.append(reply); m_autodetectionReplies.append(reply);
} }
void DevicePluginOpenweathermap::search(QString searchString) void DevicePluginOpenweathermap::search(QString searchString)
{ {
QString urlString = "http://api.openweathermap.org/data/2.5/find?q=" + searchString + "&type=like&units=metric&mode=json"; QUrl url("http://api.openweathermap.org/data/2.5/find");
QNetworkRequest searchRequest; QUrlQuery query;
searchRequest.setUrl(QUrl(urlString)); query.addQueryItem("q", searchString);
query.addQueryItem("type", "like");
query.addQueryItem("mode", "json");
query.addQueryItem("units", "metric");
url.setQuery(query);
QNetworkReply *reply = networkManagerGet(searchRequest); QNetworkReply *reply = networkManagerGet(QNetworkRequest(url));
m_searchReplies.append(reply); m_searchReplies.append(reply);
} }
void DevicePluginOpenweathermap::searchGeoLocation(double lat, double lon) void DevicePluginOpenweathermap::searchGeoLocation(double lat, double lon)
{ {
QString urlString = "http://api.openweathermap.org/data/2.5/find?lat=" + QString::number(lat) + "&lon=" + QString::number(lon) + "cnt=5&type=like&units=metric&mode=json"; QUrl url("http://api.openweathermap.org/data/2.5/find");
QNetworkRequest searchRequest; QUrlQuery query;
searchRequest.setUrl(QUrl(urlString)); query.addQueryItem("lat", QString::number(lat));
query.addQueryItem("lon", QString::number(lon));
query.addQueryItem("cnt", QString::number(3)); // 3 km radius
query.addQueryItem("type", "like");
query.addQueryItem("mode", "json");
query.addQueryItem("units", "metric");
url.setQuery(query);
QNetworkReply *reply = networkManagerGet(searchRequest); QNetworkReply *reply = networkManagerGet(QNetworkRequest(url));
m_searchGeoReplies.append(reply); m_searchGeoReplies.append(reply);
} }
@ -219,6 +231,8 @@ void DevicePluginOpenweathermap::processAutodetectResponse(QByteArray data)
if(error.error != QJsonParseError::NoError) { if(error.error != QJsonParseError::NoError) {
qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString(); qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString();
emit devicesDiscovered(openweathermapDeviceClassId, QList<DeviceDescriptor>());
return;
} }
// search by geographic coordinates // search by geographic coordinates
@ -255,6 +269,8 @@ void DevicePluginOpenweathermap::processSearchResponse(QByteArray data)
if(error.error != QJsonParseError::NoError) { if(error.error != QJsonParseError::NoError) {
qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString(); qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString();
emit devicesDiscovered(openweathermapDeviceClassId, QList<DeviceDescriptor>());
return;
} }
QVariantMap dataMap = jsonDoc.toVariant().toMap(); QVariantMap dataMap = jsonDoc.toVariant().toMap();
@ -280,6 +296,8 @@ void DevicePluginOpenweathermap::processGeoSearchResponse(QByteArray data)
if(error.error != QJsonParseError::NoError) { if(error.error != QJsonParseError::NoError) {
qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString(); qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString();
emit devicesDiscovered(openweathermapDeviceClassId, QList<DeviceDescriptor>());
return;
} }
QVariantMap dataMap = jsonDoc.toVariant().toMap(); QVariantMap dataMap = jsonDoc.toVariant().toMap();
@ -306,10 +324,10 @@ void DevicePluginOpenweathermap::processSearchResults(const QList<QVariantMap> &
{ {
QList<DeviceDescriptor> retList; QList<DeviceDescriptor> retList;
foreach (QVariantMap elemant, cityList) { foreach (QVariantMap elemant, cityList) {
DeviceDescriptor descriptor(openweathermapDeviceClassId, elemant.value("name").toString(),elemant.value("country").toString()); DeviceDescriptor descriptor(openweathermapDeviceClassId, elemant.value("name").toString(), elemant.value("country").toString());
ParamList params; ParamList params;
Param locationParam("location", elemant.value("name")); Param nameParam("name", elemant.value("name"));
params.append(locationParam); params.append(nameParam);
Param countryParam("country", elemant.value("country")); Param countryParam("country", elemant.value("country"));
params.append(countryParam); params.append(countryParam);
Param idParam("id", elemant.value("id")); Param idParam("id", elemant.value("id"));
@ -325,13 +343,15 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi
QJsonParseError error; QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
qCDebug(dcOpenWeatherMap) << jsonDoc.toJson();
if (error.error != QJsonParseError::NoError) { if (error.error != QJsonParseError::NoError) {
qCWarning(dcOpenWeatherMap) << "failed to parse weather data for device " << device->name() << ": " << data << ":" << error.errorString(); qCWarning(dcOpenWeatherMap) << "failed to parse weather data for device " << device->name() << ": " << data << ":" << error.errorString();
return; return;
} }
// http://openweathermap.org/current
QVariantMap dataMap = jsonDoc.toVariant().toMap(); QVariantMap dataMap = jsonDoc.toVariant().toMap();
if (dataMap.contains("clouds")) { if (dataMap.contains("clouds")) {
int cloudiness = dataMap.value("clouds").toMap().value("all").toInt(); int cloudiness = dataMap.value("clouds").toMap().value("all").toInt();
device->setStateValue(cloudinessStateTypeId, cloudiness); device->setStateValue(cloudinessStateTypeId, cloudiness);
@ -363,8 +383,14 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi
device->setStateValue(sunsetStateTypeId, sunset); device->setStateValue(sunsetStateTypeId, sunset);
} }
if (dataMap.contains("visibility")) {
int visibility = dataMap.value("visibility").toInt();
device->setStateValue(visibilityStateTypeId, visibility);
}
// http://openweathermap.org/weather-conditions
if (dataMap.contains("weather")) { if (dataMap.contains("weather")) {
QString description = dataMap.value("weather").toMap().value("description").toString(); QString description = dataMap.value("weather").toList().first().toMap().value("description").toString();
device->setStateValue(weatherDescriptionStateTypeId, description); device->setStateValue(weatherDescriptionStateTypeId, description);
} }

View File

@ -22,7 +22,7 @@
], ],
"paramTypes": [ "paramTypes": [
{ {
"name": "location", "name": "name",
"type": "QString", "type": "QString",
"inputType": "TextLine" "inputType": "TextLine"
}, },
@ -117,11 +117,20 @@
"type": "int", "type": "int",
"defaultValue": 0 "defaultValue": 0
}, },
{
"id": "1e10d129-cb88-48b0-9244-e3e7e7b175d9",
"idName": "visibility",
"name": "visibility",
"unit": "Meter",
"type": "int",
"defaultValue": 0
},
{ {
"id": "f9539108-0e0e-4736-a306-6408f8e20a26", "id": "f9539108-0e0e-4736-a306-6408f8e20a26",
"idName": "weatherDescription", "idName": "weatherDescription",
"name": "weather description", "name": "weather description",
"type": "QString" "type": "QString",
"defaultValue": "-"
}, },
{ {
"id": "af155e94-9492-44e1-8608-7d0ee8b5d50d", "id": "af155e94-9492-44e1-8608-7d0ee8b5d50d",

View File

@ -121,7 +121,7 @@
"unit": "Mired", "unit": "Mired",
"defaultValue": 170, "defaultValue": 170,
"writable": { "writable": {
"minValue": 154, "minValue": 153,
"maxValue": 500 "maxValue": 500
} }
}, },