From 74ad4228cfa950702a8524d57eecffbe4b5896e1 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 14 May 2019 22:47:18 +0200 Subject: [PATCH 1/3] OpenWeatherMap: Add daylightsensor and allow loading custom API keys --- openweathermap/devicepluginopenweathermap.cpp | 28 +++++++++++++++---- .../devicepluginopenweathermap.json | 14 ++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/openweathermap/devicepluginopenweathermap.cpp b/openweathermap/devicepluginopenweathermap.cpp index d313f513..a39690d8 100644 --- a/openweathermap/devicepluginopenweathermap.cpp +++ b/openweathermap/devicepluginopenweathermap.cpp @@ -58,6 +58,9 @@ #include #include #include +#include +#include +#include "nymeasettings.h" DevicePluginOpenweathermap::DevicePluginOpenweathermap() { @@ -65,6 +68,14 @@ DevicePluginOpenweathermap::DevicePluginOpenweathermap() // max 50000 calls/day m_apiKey = "c1b9d5584bb740804871583f6c62744f"; + QSettings settings(NymeaSettings::settingsPath()); + settings.beginGroup("openweathermap"); + if (settings.contains("apiKey")) { + m_apiKey = settings.value("apiKey").toString(); + qCDebug(dcOpenWeatherMap()) << "Using custom API key:" << m_apiKey.replace(m_apiKey.length() - 10, 10, "**********"); + } + + settings.endGroup(); } DevicePluginOpenweathermap::~DevicePluginOpenweathermap() @@ -375,8 +386,13 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi uint sunrise = dataMap.value("sys").toMap().value("sunrise").toUInt(); uint sunset = dataMap.value("sys").toMap().value("sunset").toUInt(); - device->setStateValue(openweathermapSunriseStateTypeId, sunrise); - device->setStateValue(openweathermapSunsetStateTypeId, sunset); + device->setStateValue(openweathermapSunriseTimeStateTypeId, sunrise); + device->setStateValue(openweathermapSunsetTimeStateTypeId, sunset); + QTimeZone tz = QTimeZone(QTimeZone::systemTimeZoneId()); + QDateTime up = QDateTime::fromSecsSinceEpoch(sunrise); + QDateTime down = QDateTime::fromSecsSinceEpoch(sunset); + QDateTime now = QDateTime::currentDateTime().toTimeZone(tz); + device->setStateValue(openweathermapDaylightStateTypeId, up < now && down > now); } if (dataMap.contains("visibility")) { @@ -388,15 +404,15 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi if (dataMap.contains("weather") && dataMap.value("weather").toList().count() > 0) { int conditionId = dataMap.value("weather").toList().first().toMap().value("id").toInt(); if (conditionId == 800) { - if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseStateTypeId).toInt() && - device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetStateTypeId).toInt()) { + if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseTimeStateTypeId).toInt() && + device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetTimeStateTypeId).toInt()) { device->setStateValue(openweathermapWeatherConditionStateTypeId, "clear-day"); } else { device->setStateValue(openweathermapWeatherConditionStateTypeId, "clear-night"); } } else if (conditionId == 801) { - if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseStateTypeId).toInt() && - device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetStateTypeId).toInt()) { + if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseTimeStateTypeId).toInt() && + device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetTimeStateTypeId).toInt()) { device->setStateValue(openweathermapWeatherConditionStateTypeId, "few-clouds-day"); } else { device->setStateValue(openweathermapWeatherConditionStateTypeId, "few-clouds-night"); diff --git a/openweathermap/devicepluginopenweathermap.json b/openweathermap/devicepluginopenweathermap.json index 6cd305cd..acd164c0 100644 --- a/openweathermap/devicepluginopenweathermap.json +++ b/openweathermap/devicepluginopenweathermap.json @@ -12,7 +12,7 @@ "id": "985195aa-17ad-4530-88a4-cdd753d747d7", "name": "openweathermap", "displayName": "Weather", - "interfaces": ["weather"], + "interfaces": ["weather", "daylightsensor"], "createMethods": ["discovery"], "discoveryParamTypes": [ { @@ -164,7 +164,7 @@ }, { "id": "af155e94-9492-44e1-8608-7d0ee8b5d50d", - "name": "sunset", + "name": "sunsetTime", "displayName": "sunset", "displayNameEvent": "sunset changed", "unit": "UnixTime", @@ -173,12 +173,20 @@ }, { "id": "a1dddc3d-549f-4f20-b78b-be850548f286", - "name": "sunrise", + "name": "sunriseTime", "displayName": "sunrise", "displayNameEvent": "sunrise changed", "unit": "UnixTime", "type": "int", "defaultValue": 0 + }, + { + "id": "e0a14b66-c8e1-49fb-8eff-b48e5dce80de", + "name": "daylight", + "displayName": "daylight", + "displayNameEvent": "daylightChanged", + "type": "bool", + "defaultValue": false } ] } From d6990d3e9171dd9363391f34b79a257cc4e9c7b7 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 16 May 2019 14:59:12 +0200 Subject: [PATCH 2/3] Compile with older qt versions --- openweathermap/devicepluginopenweathermap.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openweathermap/devicepluginopenweathermap.cpp b/openweathermap/devicepluginopenweathermap.cpp index a39690d8..3f36cbd8 100644 --- a/openweathermap/devicepluginopenweathermap.cpp +++ b/openweathermap/devicepluginopenweathermap.cpp @@ -383,14 +383,14 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi } if (dataMap.contains("sys")) { - uint sunrise = dataMap.value("sys").toMap().value("sunrise").toUInt(); - uint sunset = dataMap.value("sys").toMap().value("sunset").toUInt(); + qint64 sunrise = dataMap.value("sys").toMap().value("sunrise").toLongLong(); + qint64 sunset = dataMap.value("sys").toMap().value("sunset").toLongLong(); device->setStateValue(openweathermapSunriseTimeStateTypeId, sunrise); device->setStateValue(openweathermapSunsetTimeStateTypeId, sunset); QTimeZone tz = QTimeZone(QTimeZone::systemTimeZoneId()); - QDateTime up = QDateTime::fromSecsSinceEpoch(sunrise); - QDateTime down = QDateTime::fromSecsSinceEpoch(sunset); + QDateTime up = QDateTime::fromMSecsSinceEpoch(sunrise * 1000); + QDateTime down = QDateTime::fromMSecsSinceEpoch(sunset * 1000); QDateTime now = QDateTime::currentDateTime().toTimeZone(tz); device->setStateValue(openweathermapDaylightStateTypeId, up < now && down > now); } From cf40525e22dec96b065f1a590fccd9bc722be28a Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 21 May 2019 17:31:09 +0200 Subject: [PATCH 3/3] fix settings loading --- openweathermap/devicepluginopenweathermap.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openweathermap/devicepluginopenweathermap.cpp b/openweathermap/devicepluginopenweathermap.cpp index 3f36cbd8..e41f44ee 100644 --- a/openweathermap/devicepluginopenweathermap.cpp +++ b/openweathermap/devicepluginopenweathermap.cpp @@ -68,8 +68,8 @@ DevicePluginOpenweathermap::DevicePluginOpenweathermap() // max 50000 calls/day m_apiKey = "c1b9d5584bb740804871583f6c62744f"; - QSettings settings(NymeaSettings::settingsPath()); - settings.beginGroup("openweathermap"); + QSettings settings(NymeaSettings::settingsPath() + "/nymead.conf", QSettings::IniFormat); + settings.beginGroup("OpenWeatherMap"); if (settings.contains("apiKey")) { m_apiKey = settings.value("apiKey").toString(); qCDebug(dcOpenWeatherMap()) << "Using custom API key:" << m_apiKey.replace(m_apiKey.length() - 10, 10, "**********");