From b23d17f43b887518b62a2ba10b64e0de973bee66 Mon Sep 17 00:00:00 2001 From: Leonard Menzel Date: Fri, 2 Sep 2022 20:17:34 +0200 Subject: [PATCH 1/2] change weather to openweathermap --- .../UI/Icons/WeatherIcon/IconMapping.ts | 341 +----------------- .../UI/Icons/WeatherIcon/WeatherMapping.json | 278 ++++++++------ utils/getExternalWeather.js | 26 +- 3 files changed, 177 insertions(+), 468 deletions(-) diff --git a/client/src/components/UI/Icons/WeatherIcon/IconMapping.ts b/client/src/components/UI/Icons/WeatherIcon/IconMapping.ts index c0b62579..1c49b2a2 100644 --- a/client/src/components/UI/Icons/WeatherIcon/IconMapping.ts +++ b/client/src/components/UI/Icons/WeatherIcon/IconMapping.ts @@ -13,345 +13,10 @@ export enum TimeOfDay { night } +const mapFromJson = require('./WeatherMapping.json') + export class IconMapping { - private conditions: WeatherCondition[] = [ - { - code: 1000, - icon: { - day: 'clear-day', - night: 'clear-night' - } - }, - { - code: 1003, - icon: { - day: 'partly-cloudy-day', - night: 'partly-cloudy-night' - } - }, - { - code: 1006, - icon: { - day: 'cloudy', - night: 'cloudy' - } - }, - { - code: 1009, - icon: { - day: 'cloudy', - night: 'cloudy' - } - }, - { - code: 1030, - icon: { - day: 'fog', - night: 'fog' - } - }, - { - code: 1063, - icon: { - day: 'rain-day', - night: 'rain-night' - } - }, - { - code: 1066, - icon: { - day: 'snow-day', - night: 'snow-night' - } - }, - { - code: 1069, - icon: { - day: 'rain-snow-day', - night: 'rain-snow-night' - } - }, - { - code: 1072, - icon: { - day: 'sleet', - night: 'sleet' - } - }, - { - code: 1087, - icon: { - day: 'thunder-day', - night: 'thunder-night' - } - }, - { - code: 1114, - icon: { - day: 'snow', - night: 'snow' - } - }, - { - code: 1117, - icon: { - day: 'snow', - night: 'snow' - } - }, - { - code: 1135, - icon: { - day: 'fog', - night: 'fog' - } - }, - { - code: 1147, - icon: { - day: 'fog', - night: 'fog' - } - }, - { - code: 1150, - icon: { - day: 'rain', - night: 'rain' - } - }, - { - code: 1153, - icon: { - day: 'rain', - night: 'rain' - } - }, - { - code: 1168, - icon: { - day: 'sleet', - night: 'sleet' - } - }, - { - code: 1171, - icon: { - day: 'sleet', - night: 'sleet' - } - }, - { - code: 1180, - icon: { - day: 'rain-day', - night: 'rain-night' - } - }, - { - code: 1183, - icon: { - day: 'rain', - night: 'rain' - } - }, - { - code: 1186, - icon: { - day: 'rain-day', - night: 'rain-night' - } - }, - { - code: 1189, - icon: { - day: 'rain', - night: 'rain' - } - }, - { - code: 1192, - icon: { - day: 'rain-day', - night: 'rain-night' - } - }, - { - code: 1195, - icon: { - day: 'rain', - night: 'rain' - } - }, - { - code: 1198, - icon: { - day: 'sleet', - night: 'sleet' - } - }, - { - code: 1201, - icon: { - day: 'sleet', - night: 'sleet' - } - }, - { - code: 1204, - icon: { - day: 'rain-snow', - night: 'rain-snow' - } - }, - { - code: 1207, - icon: { - day: 'rain-snow', - night: 'rain-snow' - } - }, - { - code: 1210, - icon: { - day: 'snow-day', - night: 'snow-night' - } - }, - { - code: 1213, - icon: { - day: 'snow', - night: 'snow' - } - }, - { - code: 1216, - icon: { - day: 'snow-day', - night: 'snow-night' - } - }, - { - code: 1219, - icon: { - day: 'snow', - night: 'snow' - } - }, - { - code: 1222, - icon: { - day: 'snow-day', - night: 'snow-night' - } - }, - { - code: 1225, - icon: { - day: 'snow', - night: 'snow' - } - }, - { - code: 1237, - icon: { - day: 'hail', - night: 'hail' - } - }, - { - code: 1240, - icon: { - day: 'rain-day', - night: 'rain-night' - } - }, - { - code: 1243, - icon: { - day: 'rain-day', - night: 'rain-night' - } - }, - { - code: 1246, - icon: { - day: 'rain-day', - night: 'rain-night' - } - }, - { - code: 1249, - icon: { - day: 'rain-snow-day', - night: 'rain-snow-night' - } - }, - { - code: 1252, - icon: { - day: 'rain-snow-day', - night: 'rain-snow-night' - } - }, - { - code: 1255, - icon: { - day: 'snow-day', - night: 'snow-night' - } - }, - { - code: 1258, - icon: { - day: 'snow-day', - night: 'snow-night' - } - }, - { - code: 1261, - icon: { - day: 'hail', - night: 'hail' - } - }, - { - code: 1264, - icon: { - day: 'hail', - night: 'hail' - } - }, - { - code: 1273, - icon: { - day: 'thunder-rain-day', - night: 'thunder-rain-night' - } - }, - { - code: 1276, - icon: { - day: 'thunder-rain', - night: 'thunder-rain' - } - }, - { - code: 1279, - icon: { - day: 'thunder-day', - night: 'thunder-night' - } - }, - { - code: 1282, - icon: { - day: 'thunder', - night: 'thunder' - } - } - ]; + private conditions: WeatherCondition[] = mapFromJson.mapping mapIcon(weatherStatusCode: number, timeOfDay: TimeOfDay): IconKey { const mapping = this.conditions.find((condition: WeatherCondition) => condition.code === weatherStatusCode); diff --git a/client/src/components/UI/Icons/WeatherIcon/WeatherMapping.json b/client/src/components/UI/Icons/WeatherIcon/WeatherMapping.json index 6a7bdaa3..8f00b097 100644 --- a/client/src/components/UI/Icons/WeatherIcon/WeatherMapping.json +++ b/client/src/components/UI/Icons/WeatherIcon/WeatherMapping.json @@ -1,340 +1,382 @@ { "mapping": [ { - "code": 1000, + "code": 800, "icon": { "day": "clear-day", "night": "clear-night" } }, { - "code": 1003, + "code": 801, "icon": { "day": "partly-cloudy-day", "night": "partly-cloudy-night" } }, { - "code": 1006, + "code": 802, + "icon": { + "day": "partly-cloudy-day", + "night": "partly-cloudy-night" + } + }, + { + "code": 803, "icon": { "day": "cloudy", "night": "cloudy" } }, { - "code": 1009, + "code": 804, "icon": { "day": "cloudy", "night": "cloudy" } }, { - "code": 1030, + "code": 701, "icon": { "day": "fog", "night": "fog" } }, { - "code": 1063, + "code": 711, "icon": { - "day": "rain-day", - "night": "rain-night" + "day": "fog", + "night": "fog" } }, { - "code": 1066, + "code": 721, "icon": { - "day": "snow-day", - "night": "snow-night" + "day": "fog", + "night": "fog" } }, { - "code": 1069, + "code": 731, "icon": { - "day": "rain-snow-day", - "night": "rain-snow-night" + "day": "fog", + "night": "fog" } }, { - "code": 1072, + "code": 741, "icon": { - "day": "sleet", - "night": "sleet" + "day": "fog", + "night": "fog" } }, { - "code": 1087, + "code": 751, "icon": { - "day": "thunder-day", - "night": "thunder-night" + "day": "fog", + "night": "fog" } }, { - "code": 1114, + "code": 761, "icon": { - "day": "snow", - "night": "snow" + "day": "fog", + "night": "fog" } }, { - "code": 1117, + "code": 771, "icon": { - "day": "snow", - "night": "snow" + "day": "wind", + "night": "wind" } }, { - "code": 1135, + "code": 781, "icon": { - "day": "fog", - "night": "fog" + "day": "wind", + "night": "wind" } }, { - "code": 1147, + "code": 600, "icon": { - "day": "fog", - "night": "fog" + "day": "snow-day", + "night": "snow-night" } }, { - "code": 1150, + "code": 601, "icon": { - "day": "rain", - "night": "rain" + "day": "snow-day", + "night": "snow-night" } }, { - "code": 1153, + "code": 602, "icon": { - "day": "rain", - "night": "rain" + "day": "snow", + "night": "snow" } }, { - "code": 1168, + "code": 611, "icon": { "day": "sleet", "night": "sleet" } }, { - "code": 1171, + "code": 612, "icon": { "day": "sleet", "night": "sleet" } }, { - "code": 1180, + "code": 613, "icon": { - "day": "rain-day", - "night": "rain-night" + "day": "sleet", + "night": "sleet" } }, { - "code": 1183, + "code": 615, "icon": { - "day": "rain", - "night": "rain" + "day": "rain-snow-day", + "night": "rain-snow-night" } }, { - "code": 1186, + "code": 616, "icon": { - "day": "rain-day", - "night": "rain-night" + "day": "rain-snow-day", + "night": "rain-snow-night" } }, { - "code": 1189, + "code": 620, "icon": { - "day": "rain", - "night": "rain" + "day": "rain-snow", + "night": "rain-snow" } }, { - "code": 1192, + "code": 621, "icon": { - "day": "rain-day", - "night": "rain-night" + "day": "rain-snow", + "night": "rain-snow" } }, { - "code": 1195, + "code": 622, "icon": { - "day": "rain", - "night": "rain" + "day": "rain-snow", + "night": "rain-snow" } }, { - "code": 1198, + "code": 500, "icon": { - "day": "sleet", - "night": "sleet" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1201, + "code": 501, "icon": { - "day": "sleet", - "night": "sleet" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1204, + "code": 502, "icon": { - "day": "rain-snow", - "night": "rain-snow" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1207, + "code": 503, "icon": { - "day": "rain-snow", - "night": "rain-snow" + "day": "rain", + "night": "rain" } }, { - "code": 1210, + "code": 504, "icon": { - "day": "snow-day", - "night": "snow-night" + "day": "rain", + "night": "rain" } }, { - "code": 1213, + "code": 511, "icon": { - "day": "snow", - "night": "snow" + "day": "hail", + "night": "hail" } }, { - "code": 1216, + "code": 520, "icon": { - "day": "snow-day", - "night": "snow-night" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1219, + "code": 521, "icon": { - "day": "snow", - "night": "snow" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1222, + "code": 522, "icon": { - "day": "snow-day", - "night": "snow-night" + "day": "rain", + "night": "rain" } }, { - "code": 1225, + "code": 531, "icon": { - "day": "snow", - "night": "snow" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1237, + "code": 300, "icon": { - "day": "hail", - "night": "hail" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1240, + "code": 301, "icon": { "day": "rain-day", "night": "rain-night" } }, { - "code": 1243, + "code": 302, "icon": { "day": "rain-day", "night": "rain-night" } }, { - "code": 1246, + "code": 310, "icon": { "day": "rain-day", "night": "rain-night" } }, { - "code": 1249, + "code": 311, "icon": { - "day": "rain-snow-day", - "night": "rain-snow-night" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1252, + "code": 312, "icon": { - "day": "rain-snow-day", - "night": "rain-snow-night" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1255, + "code": 313, "icon": { - "day": "snow-day", - "night": "snow-night" + "day": "rain-day", + "night": "rain-night" } }, { - "code": 1258, + "code": 314, "icon": { - "day": "snow-day", - "night": "snow-night" + "day": "rain", + "night": "rain" } }, { - "code": 1261, + "code": 321, "icon": { - "day": "hail", - "night": "hail" + "day": "rain", + "night": "rain" } }, { - "code": 1264, + "code": 200, "icon": { - "day": "hail", - "night": "hail" + "day": "thunder-rain-day", + "night": "thunder-rain-night" } }, { - "code": 1273, + "code": 201, "icon": { "day": "thunder-rain-day", "night": "thunder-rain-night" } }, { - "code": 1276, + "code": 202, "icon": { - "day": "thunder-rain", - "night": "thunder-rain" + "day": "thunder-rain-day", + "night": "thunder-rain-night" + } + }, + { + "code": 210, + "icon": { + "day": "thunder-day", + "night": "thunder-night" } }, { - "code": 1279, + "code": 211, "icon": { "day": "thunder-day", "night": "thunder-night" } }, { - "code": 1282, + "code": 212, "icon": { "day": "thunder", "night": "thunder" } + }, + { + "code": 221, + "icon": { + "day": "thunder-day", + "night": "thunder-night" + } + }, + { + "code": 230, + "icon": { + "day": "thunder-rain-day", + "night": "thunder-rain-night" + } + }, + { + "code": 231, + "icon": { + "day": "thunder-rain-day", + "night": "thunder-rain-night" + } + }, + { + "code": 232, + "icon": { + "day": "thunder-rain-day", + "night": "thunder-rain-night" + } } ] } \ No newline at end of file diff --git a/utils/getExternalWeather.js b/utils/getExternalWeather.js index 81718074..300c20f9 100644 --- a/utils/getExternalWeather.js +++ b/utils/getExternalWeather.js @@ -5,25 +5,27 @@ const loadConfig = require('./loadConfig'); const getExternalWeather = async () => { const { WEATHER_API_KEY: secret, lat, long } = await loadConfig(); + //units = standard, metric, imperial // Fetch data from external API try { const res = await axios.get( - `http://api.weatherapi.com/v1/current.json?key=${secret}&q=${lat},${long}` + `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${secret}&units=metric` ); // Save weather data - const cursor = res.data.current; + const cursor = res.data; + const isDay = (Math.floor(Date.now()/1000) < cursor.sys.sunset) | 0 const weatherData = await Weather.create({ - externalLastUpdate: cursor.last_updated, - tempC: cursor.temp_c, - tempF: cursor.temp_f, - isDay: cursor.is_day, - cloud: cursor.cloud, - conditionText: cursor.condition.text, - conditionCode: cursor.condition.code, - humidity: cursor.humidity, - windK: cursor.wind_kph, - windM: cursor.wind_mph, + externalLastUpdate: cursor.dt, + tempC: cursor.main.temp, + tempF: 0, + isDay: isDay, + cloud: cursor.clouds.all, + conditionText: cursor.weather[0].main, + conditionCode: cursor.weather[0].id, + humidity: cursor.main.humidity, + windK: cursor.wind.speed, + windM: 0, }); return weatherData; } catch (err) { From 46c2d65f75c339f2903476479fdb7e4c712a123f Mon Sep 17 00:00:00 2001 From: Leonard Menzel Date: Fri, 2 Sep 2022 20:42:28 +0200 Subject: [PATCH 2/2] imperial/metric setting + additional data --- .../Settings/WeatherSettings/WeatherSettings.tsx | 6 +++--- utils/getExternalWeather.js | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/client/src/components/Settings/WeatherSettings/WeatherSettings.tsx b/client/src/components/Settings/WeatherSettings/WeatherSettings.tsx index a6819d8d..f3e22d6e 100644 --- a/client/src/components/Settings/WeatherSettings/WeatherSettings.tsx +++ b/client/src/components/Settings/WeatherSettings/WeatherSettings.tsx @@ -110,10 +110,10 @@ export const WeatherSettings = (): JSX.Element => { onChange={(e) => inputChangeHandler(e)} /> - Using - + Now using + {' '} - Weather API + OpenWeatherMap . Key is required for weather module to work. diff --git a/utils/getExternalWeather.js b/utils/getExternalWeather.js index 300c20f9..660d973c 100644 --- a/utils/getExternalWeather.js +++ b/utils/getExternalWeather.js @@ -3,13 +3,15 @@ const axios = require('axios'); const loadConfig = require('./loadConfig'); const getExternalWeather = async () => { - const { WEATHER_API_KEY: secret, lat, long } = await loadConfig(); - + const { WEATHER_API_KEY: secret, lat, long, isCelsius } = await loadConfig(); + //units = standard, metric, imperial + const units = isCelsius?'metric':'imperial' + // Fetch data from external API try { const res = await axios.get( - `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${secret}&units=metric` + `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${secret}&units=${units}` ); // Save weather data @@ -18,7 +20,7 @@ const getExternalWeather = async () => { const weatherData = await Weather.create({ externalLastUpdate: cursor.dt, tempC: cursor.main.temp, - tempF: 0, + tempF: cursor.main.temp, isDay: isDay, cloud: cursor.clouds.all, conditionText: cursor.weather[0].main,