Add new parameter "useKmh" to weather module

Add new parameter "useKmh" to weather module for displaying wind speed as km/h instead of m/s when windUnits is set to metric.
This commit is contained in:
Aurélien Veillard 2020-11-08 14:01:02 +01:00
parent 0d6f736c2c
commit 1460f002ab
8 changed files with 40 additions and 29 deletions

View File

@ -12,6 +12,7 @@ _This release is scheduled to be released on 2021-01-01._
### Added ### Added
- Added new log level "debug" to the logger. - Added new log level "debug" to the logger.
- Added new parameter "useKmh" to weather module for displaying wind speed as kmh.
### Updated ### Updated

View File

@ -62,7 +62,7 @@ WeatherProvider.register("darksky", {
// Implement WeatherDay generator. // Implement WeatherDay generator.
generateWeatherDayFromCurrentWeather(currentWeatherData) { generateWeatherDayFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
currentWeather.date = moment(); currentWeather.date = moment();
currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity); currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity);
@ -80,7 +80,7 @@ WeatherProvider.register("darksky", {
const days = []; const days = [];
for (const forecast of forecasts) { for (const forecast of forecasts) {
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
weather.date = moment(forecast.time, "X"); weather.date = moment(forecast.time, "X");
weather.minTemperature = forecast.temperatureMin; weather.minTemperature = forecast.temperatureMin;

View File

@ -89,11 +89,15 @@ WeatherProvider.register("openweathermap", {
* Generate a WeatherObject based on currentWeatherInformation * Generate a WeatherObject based on currentWeatherInformation
*/ */
generateWeatherObjectFromCurrentWeather(currentWeatherData) { generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
currentWeather.humidity = currentWeatherData.main.humidity; currentWeather.humidity = currentWeatherData.main.humidity;
currentWeather.temperature = currentWeatherData.main.temp; currentWeather.temperature = currentWeatherData.main.temp;
currentWeather.windSpeed = currentWeatherData.wind.speed; if(this.config.windUnits === "metric") {
currentWeather.windSpeed = this.config.useKmh ? currentWeatherData.wind.speed * 3.6 : currentWeatherData.wind.speed;
} else {
currentWeather.windSpeed = currentWeatherData.wind.speed;
}
currentWeather.windDirection = currentWeatherData.wind.deg; currentWeather.windDirection = currentWeatherData.wind.deg;
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon); currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon);
currentWeather.sunrise = moment(currentWeatherData.sys.sunrise, "X"); currentWeather.sunrise = moment(currentWeatherData.sys.sunrise, "X");
@ -112,7 +116,7 @@ WeatherProvider.register("openweathermap", {
return this.fetchForecastDaily(forecasts); return this.fetchForecastDaily(forecasts);
} }
// if weatherEndpoint does not match forecast or forecast/daily, what should be returned? // if weatherEndpoint does not match forecast or forecast/daily, what should be returned?
const days = [new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits)]; const days = [new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh)];
return days; return days;
}, },
@ -124,7 +128,7 @@ WeatherProvider.register("openweathermap", {
return this.fetchOnecall(data); return this.fetchOnecall(data);
} }
// if weatherEndpoint does not match onecall, what should be returned? // if weatherEndpoint does not match onecall, what should be returned?
const weatherData = { current: new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits), hours: [], days: [] }; const weatherData = { current: new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh), hours: [], days: [] };
return weatherData; return weatherData;
}, },
@ -141,7 +145,7 @@ WeatherProvider.register("openweathermap", {
let snow = 0; let snow = 0;
// variable for date // variable for date
let date = ""; let date = "";
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
for (const forecast of forecasts) { for (const forecast of forecasts) {
if (date !== moment(forecast.dt, "X").format("YYYY-MM-DD")) { if (date !== moment(forecast.dt, "X").format("YYYY-MM-DD")) {
@ -154,7 +158,7 @@ WeatherProvider.register("openweathermap", {
// push weather information to days array // push weather information to days array
days.push(weather); days.push(weather);
// create new weather-object // create new weather-object
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
minTemp = []; minTemp = [];
maxTemp = []; maxTemp = [];
@ -217,7 +221,7 @@ WeatherProvider.register("openweathermap", {
const days = []; const days = [];
for (const forecast of forecasts) { for (const forecast of forecasts) {
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
weather.date = moment(forecast.dt, "X"); weather.date = moment(forecast.dt, "X");
weather.minTemperature = forecast.temp.min; weather.minTemperature = forecast.temp.min;
@ -263,7 +267,7 @@ WeatherProvider.register("openweathermap", {
let precip = false; let precip = false;
// get current weather, if requested // get current weather, if requested
const current = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const current = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
if (data.hasOwnProperty("current")) { if (data.hasOwnProperty("current")) {
current.date = moment(data.current.dt, "X").utcOffset(data.timezone_offset / 60); current.date = moment(data.current.dt, "X").utcOffset(data.timezone_offset / 60);
current.windSpeed = data.current.wind_speed; current.windSpeed = data.current.wind_speed;
@ -295,7 +299,7 @@ WeatherProvider.register("openweathermap", {
current.feelsLikeTemp = data.current.feels_like; current.feelsLikeTemp = data.current.feels_like;
} }
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
// get hourly weather, if requested // get hourly weather, if requested
const hours = []; const hours = [];
@ -331,7 +335,7 @@ WeatherProvider.register("openweathermap", {
} }
hours.push(weather); hours.push(weather);
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
} }
} }
@ -370,7 +374,7 @@ WeatherProvider.register("openweathermap", {
} }
days.push(weather); days.push(weather);
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
} }
} }

View File

@ -73,7 +73,7 @@ WeatherProvider.register("ukmetoffice", {
* Generate a WeatherObject based on currentWeatherInformation * Generate a WeatherObject based on currentWeatherInformation
*/ */
generateWeatherObjectFromCurrentWeather(currentWeatherData) { generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
// data times are always UTC // data times are always UTC
let nowUtc = moment.utc(); let nowUtc = moment.utc();
@ -124,7 +124,7 @@ WeatherProvider.register("ukmetoffice", {
// loop round the (5) periods getting the data // loop round the (5) periods getting the data
// for each period array, Day is [0], Night is [1] // for each period array, Day is [0], Night is [1]
for (var j in forecasts.SiteRep.DV.Location.Period) { for (var j in forecasts.SiteRep.DV.Location.Period) {
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
// data times are always UTC // data times are always UTC
const dateStr = forecasts.SiteRep.DV.Location.Period[j].value; const dateStr = forecasts.SiteRep.DV.Location.Period[j].value;
@ -208,10 +208,10 @@ WeatherProvider.register("ukmetoffice", {
}, },
/* /*
* Convert wind speed (from mph to m/s) if required * Convert wind speed (from mph to m/s or km/h) if required
*/ */
convertWindSpeed(windInMph) { convertWindSpeed(windInMph) {
return this.windUnits === "metric" ? windInMph / 2.23694 : windInMph; return this.windUnits === "metric" ? (this.useKmh ? windInMph * 1.60934 : windInMph / 2.23694) : windInMph;
}, },
/* /*

View File

@ -108,7 +108,7 @@ WeatherProvider.register("ukmetofficedatahub", {
// Create a WeatherObject using current weather data (data for the current hour) // Create a WeatherObject using current weather data (data for the current hour)
generateWeatherObjectFromCurrentWeather(currentWeatherData) { generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
// Extract the actual forecasts // Extract the actual forecasts
let forecastDataHours = currentWeatherData.features[0].properties.timeSeries; let forecastDataHours = currentWeatherData.features[0].properties.timeSeries;
@ -189,7 +189,7 @@ WeatherProvider.register("ukmetofficedatahub", {
// Go through each day in the forecasts // Go through each day in the forecasts
for (day in forecastDataDays) { for (day in forecastDataDays) {
const forecastWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const forecastWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
// Get date of forecast // Get date of forecast
let forecastDate = moment.utc(forecastDataDays[day].time); let forecastDate = moment.utc(forecastDataDays[day].time);
@ -254,7 +254,7 @@ WeatherProvider.register("ukmetofficedatahub", {
return windInMpS; return windInMpS;
} }
if (this.config.windUnits == "kph" || this.config.windUnits == "metric") { if (this.config.windUnits == "kph" || this.config.windUnits == "metric" || this.config.useKmh ) {
return windInMpS * 3.6; return windInMpS * 3.6;
} }

View File

@ -131,11 +131,11 @@ WeatherProvider.register("weathergov", {
* ... object needs data in units based on config! * ... object needs data in units based on config!
*/ */
generateWeatherObjectFromCurrentWeather(currentWeatherData) { generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
currentWeather.date = moment(currentWeatherData.timestamp); currentWeather.date = moment(currentWeatherData.timestamp);
currentWeather.temperature = this.convertTemp(currentWeatherData.temperature.value); currentWeather.temperature = this.convertTemp(currentWeatherData.temperature.value);
currentWeather.windSpeed = this.covertSpeed(currentWeatherData.windSpeed.value); currentWeather.windSpeed = this.convertSpeed(currentWeatherData.windSpeed.value);
currentWeather.windDirection = currentWeatherData.windDirection.value; currentWeather.windDirection = currentWeatherData.windDirection.value;
currentWeather.minTemperature = this.convertTemp(currentWeatherData.minTemperatureLast24Hours.value); currentWeather.minTemperature = this.convertTemp(currentWeatherData.minTemperatureLast24Hours.value);
currentWeather.maxTemperature = this.convertTemp(currentWeatherData.maxTemperatureLast24Hours.value); currentWeather.maxTemperature = this.convertTemp(currentWeatherData.maxTemperatureLast24Hours.value);
@ -179,7 +179,7 @@ WeatherProvider.register("weathergov", {
let maxTemp = []; let maxTemp = [];
// variable for date // variable for date
let date = ""; let date = "";
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
weather.precipitation = 0; weather.precipitation = 0;
for (const forecast of forecasts) { for (const forecast of forecasts) {
@ -191,7 +191,7 @@ WeatherProvider.register("weathergov", {
// push weather information to days array // push weather information to days array
days.push(weather); days.push(weather);
// create new weather-object // create new weather-object
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits); weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
minTemp = []; minTemp = [];
maxTemp = []; maxTemp = [];
@ -238,12 +238,16 @@ WeatherProvider.register("weathergov", {
return temp; return temp;
} }
}, },
// conversion to mph // conversion to mph or kmh
covertSpeed(metSec) { convertSpeed(metSec) {
if (this.config.windUnits === "imperial") { if (this.config.windUnits === "imperial") {
return metSec * 2.23694; return metSec * 2.23694;
} else { } else {
return metSec; if(this.config.useKmh) {
return metSec * 3.6;
} else {
return metSec;
}
} }
}, },
// conversion to inches // conversion to inches

View File

@ -30,6 +30,7 @@ Module.register("weather", {
showWindDirection: true, showWindDirection: true,
showWindDirectionAsArrow: false, showWindDirectionAsArrow: false,
useBeaufort: true, useBeaufort: true,
useKmh: false,
lang: config.language, lang: config.language,
showHumidity: false, showHumidity: false,
showSun: true, showSun: true,

View File

@ -10,10 +10,11 @@
* As soon as we start implementing the forecast, mode properties will be added. * As soon as we start implementing the forecast, mode properties will be added.
*/ */
class WeatherObject { class WeatherObject {
constructor(units, tempUnits, windUnits) { constructor(units, tempUnits, windUnits, useKmh) {
this.units = units; this.units = units;
this.tempUnits = tempUnits; this.tempUnits = tempUnits;
this.windUnits = windUnits; this.windUnits = windUnits;
this.useKmh = useKmh;
this.date = null; this.date = null;
this.windSpeed = null; this.windSpeed = null;
this.windDirection = null; this.windDirection = null;
@ -67,7 +68,7 @@ class WeatherObject {
} }
beaufortWindSpeed() { beaufortWindSpeed() {
const windInKmh = this.windUnits === "imperial" ? this.windSpeed * 1.609344 : (this.windSpeed * 60 * 60) / 1000; const windInKmh = this.windUnits === "imperial" ? this.windSpeed * 1.609344 : (this.useKmh ? this.windSpeed : (this.windSpeed * 60 * 60) / 1000);
const speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000]; const speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000];
for (const [index, speed] of speeds.entries()) { for (const [index, speed] of speeds.entries()) {
if (speed > windInKmh) { if (speed > windInKmh) {