From 6b0025125b2a646f4ae8ab9da0c9eebaabf01899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 29 Sep 2015 12:23:17 +0200 Subject: [PATCH] fix #224 and fix #225 --- .../devicepluginopenweathermap.cpp | 94 ++++++++++++------- .../devicepluginopenweathermap.json | 13 ++- .../philipshue/devicepluginphilipshue.json | 2 +- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp index bfd730f7..8e067aeb 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp @@ -29,7 +29,7 @@ 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 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. \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 #include #include +#include +#include #include @@ -91,11 +93,9 @@ DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const Dev DeviceManager::DeviceSetupStatus DevicePluginOpenweathermap::setupDevice(Device *device) { - if (device->deviceClassId() != openweathermapDeviceClassId) { + if (device->deviceClassId() != openweathermapDeviceClassId) return DeviceManager::DeviceSetupStatusFailure; - } - device->setName("Weather from OpenWeatherMap (" + device->paramValue("location").toString() + ")"); update(device); return DeviceManager::DeviceSetupStatusSuccess; @@ -110,8 +110,9 @@ DeviceManager::DeviceError DevicePluginOpenweathermap::executeAction(Device *dev { if(action.actionTypeId() == updateWeatherActionTypeId){ update(device); + return DeviceManager::DeviceErrorNoError; } - return DeviceManager::DeviceErrorNoError; + return DeviceManager::DeviceErrorActionTypeNotFound; } void DevicePluginOpenweathermap::deviceRemoved(Device *device) @@ -130,6 +131,8 @@ void DevicePluginOpenweathermap::networkManagerReplyReady(QNetworkReply *reply) { if (reply->error()) { qCWarning(dcOpenWeatherMap) << "OpenWeatherMap reply error: " << reply->errorString(); + reply->deleteLater(); + return; } if (m_autodetectionReplies.contains(reply)) { @@ -149,7 +152,6 @@ void DevicePluginOpenweathermap::networkManagerReplyReady(QNetworkReply *reply) Device* device = m_weatherReplies.take(reply); processWeatherData(data, device); } - reply->deleteLater(); } @@ -161,54 +163,64 @@ void DevicePluginOpenweathermap::guhTimer() void DevicePluginOpenweathermap::update() { foreach (Device *device, myDevices()) { - QString cityId = device->paramValue("id").toString(); - QString urlString = "http://api.openweathermap.org/data/2.5/weather?id="+ cityId + "&mode=json&units=metric"; - QNetworkRequest weatherRequest; - weatherRequest.setUrl(QUrl(urlString)); + QUrl url("http://api.openweathermap.org/data/2.5/weather"); + QUrlQuery query; + query.addQueryItem("id", device->paramValue("id").toString()); + 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); } } void DevicePluginOpenweathermap::update(Device *device) { - QString cityId = device->paramValue("id").toString(); - QString urlString = "http://api.openweathermap.org/data/2.5/weather?id="+ cityId + "&mode=json&units=metric"; - QNetworkRequest weatherRequest; - weatherRequest.setUrl(QUrl(urlString)); + QUrl url("http://api.openweathermap.org/data/2.5/weather"); + QUrlQuery query; + query.addQueryItem("id", device->paramValue("id").toString()); + 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); } void DevicePluginOpenweathermap::searchAutodetect() { - QString urlString = "http://ip-api.com/json"; - QNetworkRequest locationRequest; - locationRequest.setUrl(QUrl(urlString)); - - QNetworkReply *reply = networkManagerGet(locationRequest); + QNetworkReply *reply = networkManagerGet(QNetworkRequest(QUrl("http://ip-api.com/json"))); m_autodetectionReplies.append(reply); } void DevicePluginOpenweathermap::search(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)); + QUrl url("http://api.openweathermap.org/data/2.5/find"); + QUrlQuery query; + 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); } 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"; - QNetworkRequest searchRequest; - searchRequest.setUrl(QUrl(urlString)); + QUrl url("http://api.openweathermap.org/data/2.5/find"); + QUrlQuery query; + 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); } @@ -219,6 +231,8 @@ void DevicePluginOpenweathermap::processAutodetectResponse(QByteArray data) if(error.error != QJsonParseError::NoError) { qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString(); + emit devicesDiscovered(openweathermapDeviceClassId, QList()); + return; } // search by geographic coordinates @@ -255,6 +269,8 @@ void DevicePluginOpenweathermap::processSearchResponse(QByteArray data) if(error.error != QJsonParseError::NoError) { qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString(); + emit devicesDiscovered(openweathermapDeviceClassId, QList()); + return; } QVariantMap dataMap = jsonDoc.toVariant().toMap(); @@ -280,6 +296,8 @@ void DevicePluginOpenweathermap::processGeoSearchResponse(QByteArray data) if(error.error != QJsonParseError::NoError) { qCWarning(dcOpenWeatherMap) << "failed to parse data" << data << ":" << error.errorString(); + emit devicesDiscovered(openweathermapDeviceClassId, QList()); + return; } QVariantMap dataMap = jsonDoc.toVariant().toMap(); @@ -306,10 +324,10 @@ void DevicePluginOpenweathermap::processSearchResults(const QList & { QList retList; 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; - Param locationParam("location", elemant.value("name")); - params.append(locationParam); + Param nameParam("name", elemant.value("name")); + params.append(nameParam); Param countryParam("country", elemant.value("country")); params.append(countryParam); Param idParam("id", elemant.value("id")); @@ -325,13 +343,15 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi QJsonParseError error; QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + qCDebug(dcOpenWeatherMap) << jsonDoc.toJson(); + if (error.error != QJsonParseError::NoError) { qCWarning(dcOpenWeatherMap) << "failed to parse weather data for device " << device->name() << ": " << data << ":" << error.errorString(); return; } + // http://openweathermap.org/current QVariantMap dataMap = jsonDoc.toVariant().toMap(); - if (dataMap.contains("clouds")) { int cloudiness = dataMap.value("clouds").toMap().value("all").toInt(); device->setStateValue(cloudinessStateTypeId, cloudiness); @@ -363,8 +383,14 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi 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")) { - QString description = dataMap.value("weather").toMap().value("description").toString(); + QString description = dataMap.value("weather").toList().first().toMap().value("description").toString(); device->setStateValue(weatherDescriptionStateTypeId, description); } diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json index ef08b6f6..cae594da 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json @@ -22,7 +22,7 @@ ], "paramTypes": [ { - "name": "location", + "name": "name", "type": "QString", "inputType": "TextLine" }, @@ -117,11 +117,20 @@ "type": "int", "defaultValue": 0 }, + { + "id": "1e10d129-cb88-48b0-9244-e3e7e7b175d9", + "idName": "visibility", + "name": "visibility", + "unit": "Meter", + "type": "int", + "defaultValue": 0 + }, { "id": "f9539108-0e0e-4736-a306-6408f8e20a26", "idName": "weatherDescription", "name": "weather description", - "type": "QString" + "type": "QString", + "defaultValue": "-" }, { "id": "af155e94-9492-44e1-8608-7d0ee8b5d50d", diff --git a/plugins/deviceplugins/philipshue/devicepluginphilipshue.json b/plugins/deviceplugins/philipshue/devicepluginphilipshue.json index 74bb1302..a9bf76a9 100644 --- a/plugins/deviceplugins/philipshue/devicepluginphilipshue.json +++ b/plugins/deviceplugins/philipshue/devicepluginphilipshue.json @@ -121,7 +121,7 @@ "unit": "Mired", "defaultValue": 170, "writable": { - "minValue": 154, + "minValue": 153, "maxValue": 500 } },