Merge PR #107: OpenWeatherMap: Add daylightsensor and allow loading custom API keys

This commit is contained in:
Jenkins 2019-06-19 23:52:34 +02:00
commit 2d486eeb09
2 changed files with 35 additions and 11 deletions

View File

@ -58,6 +58,9 @@
#include <QUrl> #include <QUrl>
#include <QUrlQuery> #include <QUrlQuery>
#include <QDateTime> #include <QDateTime>
#include <QTimeZone>
#include <QSettings>
#include "nymeasettings.h"
DevicePluginOpenweathermap::DevicePluginOpenweathermap() DevicePluginOpenweathermap::DevicePluginOpenweathermap()
{ {
@ -65,6 +68,14 @@ DevicePluginOpenweathermap::DevicePluginOpenweathermap()
// max 50000 calls/day // max 50000 calls/day
m_apiKey = "c1b9d5584bb740804871583f6c62744f"; m_apiKey = "c1b9d5584bb740804871583f6c62744f";
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, "**********");
}
settings.endGroup();
} }
DevicePluginOpenweathermap::~DevicePluginOpenweathermap() DevicePluginOpenweathermap::~DevicePluginOpenweathermap()
@ -372,11 +383,16 @@ void DevicePluginOpenweathermap::processWeatherData(const QByteArray &data, Devi
} }
if (dataMap.contains("sys")) { if (dataMap.contains("sys")) {
uint sunrise = dataMap.value("sys").toMap().value("sunrise").toUInt(); qint64 sunrise = dataMap.value("sys").toMap().value("sunrise").toLongLong();
uint sunset = dataMap.value("sys").toMap().value("sunset").toUInt(); qint64 sunset = dataMap.value("sys").toMap().value("sunset").toLongLong();
device->setStateValue(openweathermapSunriseStateTypeId, sunrise); device->setStateValue(openweathermapSunriseTimeStateTypeId, sunrise);
device->setStateValue(openweathermapSunsetStateTypeId, sunset); device->setStateValue(openweathermapSunsetTimeStateTypeId, sunset);
QTimeZone tz = QTimeZone(QTimeZone::systemTimeZoneId());
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);
} }
if (dataMap.contains("visibility")) { 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) { if (dataMap.contains("weather") && dataMap.value("weather").toList().count() > 0) {
int conditionId = dataMap.value("weather").toList().first().toMap().value("id").toInt(); int conditionId = dataMap.value("weather").toList().first().toMap().value("id").toInt();
if (conditionId == 800) { if (conditionId == 800) {
if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseStateTypeId).toInt() && if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseTimeStateTypeId).toInt() &&
device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetStateTypeId).toInt()) { device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetTimeStateTypeId).toInt()) {
device->setStateValue(openweathermapWeatherConditionStateTypeId, "clear-day"); device->setStateValue(openweathermapWeatherConditionStateTypeId, "clear-day");
} else { } else {
device->setStateValue(openweathermapWeatherConditionStateTypeId, "clear-night"); device->setStateValue(openweathermapWeatherConditionStateTypeId, "clear-night");
} }
} else if (conditionId == 801) { } else if (conditionId == 801) {
if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseStateTypeId).toInt() && if (device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() > device->stateValue(openweathermapSunriseTimeStateTypeId).toInt() &&
device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetStateTypeId).toInt()) { device->stateValue(openweathermapUpdateTimeStateTypeId).toInt() < device->stateValue(openweathermapSunsetTimeStateTypeId).toInt()) {
device->setStateValue(openweathermapWeatherConditionStateTypeId, "few-clouds-day"); device->setStateValue(openweathermapWeatherConditionStateTypeId, "few-clouds-day");
} else { } else {
device->setStateValue(openweathermapWeatherConditionStateTypeId, "few-clouds-night"); device->setStateValue(openweathermapWeatherConditionStateTypeId, "few-clouds-night");

View File

@ -12,7 +12,7 @@
"id": "985195aa-17ad-4530-88a4-cdd753d747d7", "id": "985195aa-17ad-4530-88a4-cdd753d747d7",
"name": "openweathermap", "name": "openweathermap",
"displayName": "Weather", "displayName": "Weather",
"interfaces": ["weather"], "interfaces": ["weather", "daylightsensor"],
"createMethods": ["discovery"], "createMethods": ["discovery"],
"discoveryParamTypes": [ "discoveryParamTypes": [
{ {
@ -164,7 +164,7 @@
}, },
{ {
"id": "af155e94-9492-44e1-8608-7d0ee8b5d50d", "id": "af155e94-9492-44e1-8608-7d0ee8b5d50d",
"name": "sunset", "name": "sunsetTime",
"displayName": "sunset", "displayName": "sunset",
"displayNameEvent": "sunset changed", "displayNameEvent": "sunset changed",
"unit": "UnixTime", "unit": "UnixTime",
@ -173,12 +173,20 @@
}, },
{ {
"id": "a1dddc3d-549f-4f20-b78b-be850548f286", "id": "a1dddc3d-549f-4f20-b78b-be850548f286",
"name": "sunrise", "name": "sunriseTime",
"displayName": "sunrise", "displayName": "sunrise",
"displayNameEvent": "sunrise changed", "displayNameEvent": "sunrise changed",
"unit": "UnixTime", "unit": "UnixTime",
"type": "int", "type": "int",
"defaultValue": 0 "defaultValue": 0
},
{
"id": "e0a14b66-c8e1-49fb-8eff-b48e5dce80de",
"name": "daylight",
"displayName": "daylight",
"displayNameEvent": "daylightChanged",
"type": "bool",
"defaultValue": false
} }
] ]
} }