From 46f77cd67af8fa8f5ae779a2f33b9730239c88a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Sun, 25 Oct 2015 10:50:16 +0100 Subject: [PATCH] update openweathermap api changes small changes in the plugin json --- .../devicepluginopenweathermap.cpp | 53 +++++++++---------- .../devicepluginopenweathermap.h | 10 +++- .../devicepluginopenweathermap.json | 6 +-- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp index 8e067aeb..e5db68ea 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.cpp @@ -25,14 +25,15 @@ \ingroup plugins \ingroup services - This plugin alows you to get the current weather data from \l{http://www.openweathermap.org}. - 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. + This plugin allows to get the current weather data from \l{http://www.openweathermap.org}{OpenWeatherMap}. + The weather data will be refreshed every 15 minutes automatically, but can also refreshed manually. + The plugin offers two different search methods for the location: if the user searches for a empty string, + the plugin makes an autodetction with the WAN ip and offers the user the found weather stations. The autodetection function uses the geolocation of your WAN ip and searches all available weather 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. + \underline{NOTE}: If you are using a VPN connection, the autodetection will show the results around your VPN location. \chapter Plugin properties Following JSON file contains the definition and the description of all available \l{DeviceClass}{DeviceClasses} @@ -64,9 +65,18 @@ #include #include - DevicePluginOpenweathermap::DevicePluginOpenweathermap() { + // max 60 calls/minute + // max 50000 calls/day + m_apiKey = "c1b9d5584bb740804871583f6c62744f"; + + // update every 15 minutes + m_timer = new QTimer(this); + m_timer->setSingleShot(false); + m_timer->setInterval(900000); + + connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimeout())); } DeviceManager::DeviceError DevicePluginOpenweathermap::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) @@ -103,12 +113,12 @@ DeviceManager::DeviceSetupStatus DevicePluginOpenweathermap::setupDevice(Device DeviceManager::HardwareResources DevicePluginOpenweathermap::requiredHardware() const { - return DeviceManager::HardwareResourceTimer | DeviceManager::HardwareResourceNetworkManager; + return DeviceManager::HardwareResourceNetworkManager; } DeviceManager::DeviceError DevicePluginOpenweathermap::executeAction(Device *device, const Action &action) { - if(action.actionTypeId() == updateWeatherActionTypeId){ + if(action.actionTypeId() == refreshWeatherActionTypeId){ update(device); return DeviceManager::DeviceErrorNoError; } @@ -155,26 +165,6 @@ void DevicePluginOpenweathermap::networkManagerReplyReady(QNetworkReply *reply) reply->deleteLater(); } -void DevicePluginOpenweathermap::guhTimer() -{ - update(); -} - -void DevicePluginOpenweathermap::update() -{ - foreach (Device *device, myDevices()) { - 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(QNetworkRequest(url)); - m_weatherReplies.insert(reply, device); - } -} - void DevicePluginOpenweathermap::update(Device *device) { QUrl url("http://api.openweathermap.org/data/2.5/weather"); @@ -182,6 +172,7 @@ void DevicePluginOpenweathermap::update(Device *device) query.addQueryItem("id", device->paramValue("id").toString()); query.addQueryItem("mode", "json"); query.addQueryItem("units", "metric"); + query.addQueryItem("appid", m_apiKey); url.setQuery(query); QNetworkReply *reply = networkManagerGet(QNetworkRequest(url)); @@ -202,6 +193,7 @@ void DevicePluginOpenweathermap::search(QString searchString) query.addQueryItem("type", "like"); query.addQueryItem("mode", "json"); query.addQueryItem("units", "metric"); + query.addQueryItem("appid", m_apiKey); url.setQuery(query); QNetworkReply *reply = networkManagerGet(QNetworkRequest(url)); @@ -218,6 +210,7 @@ void DevicePluginOpenweathermap::searchGeoLocation(double lat, double lon) query.addQueryItem("type", "like"); query.addQueryItem("mode", "json"); query.addQueryItem("units", "metric"); + query.addQueryItem("appid", m_apiKey); url.setQuery(query); QNetworkReply *reply = networkManagerGet(QNetworkRequest(url)); @@ -403,4 +396,10 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi } } +void DevicePluginOpenweathermap::onTimeout() +{ + foreach (Device *device, myDevices()) { + update(device); + } +} diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h index 65ebc1b2..7a19eeed 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.h @@ -23,6 +23,8 @@ #include "plugin/deviceplugin.h" +#include + class DevicePluginOpenweathermap : public DevicePlugin { Q_OBJECT @@ -40,7 +42,7 @@ public: void deviceRemoved(Device *device) override; void networkManagerReplyReady(QNetworkReply *reply) override; - void guhTimer() override; + //void guhTimer() override; private: QList m_autodetectionReplies; @@ -55,7 +57,9 @@ private: double m_longitude; double m_latitude; - void update(); + QTimer *m_timer; + QString m_apiKey; + void update(Device *device); void searchAutodetect(); void search(QString searchString); @@ -68,6 +72,8 @@ private: void processSearchResults(const QList &cityList); void processWeatherData(const QByteArray &data, Device *device); +private slots: + void onTimeout(); }; diff --git a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json index c368794f..c339a7df 100644 --- a/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json +++ b/plugins/deviceplugins/openweathermap/devicepluginopenweathermap.json @@ -11,7 +11,7 @@ { "deviceClassId": "985195aa-17ad-4530-88a4-cdd753d747d7", "idName": "openweathermap", - "name": "Weather from OpenWeatherMap", + "name": "Weather", "createMethods": ["discovery"], "discoveryParamTypes": [ { @@ -41,8 +41,8 @@ "actionTypes": [ { "id": "cfbc6504-d86f-4856-8dfa-97b6fbb385e4", - "idName": "updateWeather", - "name": "update" + "idName": "refreshWeather", + "name": "refresh" } ], "stateTypes": [