Allow temp and wind units to be specified separately if required.

This commit is contained in:
Malcolm Oakes
2019-06-07 15:27:08 +01:00
parent 2970568eab
commit a619fc4fef
9 changed files with 48 additions and 37 deletions

View File

@@ -18,9 +18,9 @@ This is the script in which the weather provider will be defined. In it's most s
````javascript
WeatherProvider.register("yourprovider", {
providerName: "YourProvider",
fetchCurrentWeather() {},
fetchWeatherForecast() {}
});
````
@@ -91,9 +91,11 @@ A convenience function to make requests. It returns a promise.
| Property | Type | Value/Unit |
| --- | --- | --- |
| units | `string` | Gets initialized with the constructor. <br> Possible values: `metric`, `imperial` and `ukunits` |
| units | `string` | Gets initialized with the constructor. <br> Possible values: `metric`, `imperial` |
| tempUnits | `string` | Gets initialized with the constructor. <br> Possible values: `metric`, `imperial` |
| windUnits | `string` | Gets initialized with the constructor. <br> Possible values: `metric`, `imperial` |
| date | `object` | [Moment.js](https://momentjs.com/) object of the time/date. |
| windSpeed |`number` | Metric: `meter/second` <br> Imperial: `miles/hour` <br> UKunits: `miles/hour` |
| windSpeed |`number` | Metric: `meter/second` <br> Imperial: `miles/hour` |
| windDirection |`number` | Direction of the wind in degrees. |
| sunrise |`object` | [Moment.js](https://momentjs.com/) object of sunrise. |
| sunset |`object` | [Moment.js](https://momentjs.com/) object of sunset. |
@@ -104,7 +106,7 @@ A convenience function to make requests. It returns a promise.
| humidity | `number` | Percentage of humidity |
| rain | `number` | Metric: `millimeters` <br> Imperial: `inches` |
| snow | `number` | Metric: `millimeters` <br> Imperial: `inches` |
| precipitation | `number` | Metric: `millimeters` <br> Imperial: `inches` <br> Ukunits: `percent` |
| precipitation | `number` | Metric: `millimeters` <br> Imperial: `inches` <br> UK Met Office provider: `percent` |
#### Current weather

4
modules/default/weather/providers/darksky.js Normal file → Executable file
View File

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

12
modules/default/weather/providers/openweathermap.js Normal file → Executable file
View File

@@ -68,7 +68,7 @@ WeatherProvider.register("openweathermap", {
* Generate a WeatherObject based on currentWeatherInformation
*/
generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units);
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
currentWeather.humidity = currentWeatherData.main.humidity;
currentWeather.temperature = currentWeatherData.main.temp;
@@ -92,7 +92,7 @@ WeatherProvider.register("openweathermap", {
return this.fetchForecastDaily(forecasts);
}
// if weatherEndpoint does not match forecast or forecast/daily, what should be returned?
const days = [new WeatherObject(this.config.units)];
const days = [new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits)];
return days;
},
@@ -109,7 +109,7 @@ WeatherProvider.register("openweathermap", {
let snow = 0;
// variable for date
let date = "";
let weather = new WeatherObject(this.config.units);
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
for (const forecast of forecasts) {
@@ -123,7 +123,7 @@ WeatherProvider.register("openweathermap", {
// push weather information to days array
days.push(weather);
// create new weather-object
weather = new WeatherObject(this.config.units);
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
minTemp = [];
maxTemp = [];
@@ -140,7 +140,7 @@ WeatherProvider.register("openweathermap", {
weather.weatherType = this.convertWeatherType(forecast.weather[0].icon);
}
if (moment(forecast.dt, "X").format("H") >= 8 && moment(forecast.dt, "X").format("H") <= 17) {
weather.weatherType = this.convertWeatherType(forecast.weather[0].icon);
}
@@ -187,7 +187,7 @@ WeatherProvider.register("openweathermap", {
const days = [];
for (const forecast of forecasts) {
const weather = new WeatherObject(this.config.units);
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
weather.date = moment(forecast.dt, "X");
weather.minTemperature = forecast.temp.min;

View File

@@ -19,8 +19,7 @@ WeatherProvider.register("ukmetoffice", {
units: {
imperial: "us",
metric: "si",
ukunits: "uk"
metric: "si"
},
// Overwrite the fetchCurrentWeather method.
@@ -79,7 +78,7 @@ WeatherProvider.register("ukmetoffice", {
* Generate a WeatherObject based on currentWeatherInformation
*/
generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units);
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
// data times are always UTC
let nowUtc = moment.utc()
@@ -132,7 +131,7 @@ WeatherProvider.register("ukmetoffice", {
// loop round the (5) periods getting the data
// for each period array, Day is [0], Night is [1]
for (j in forecasts.SiteRep.DV.Location.Period) {
const weather = new WeatherObject(this.config.units);
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
// data times are always UTC
dateStr = forecasts.SiteRep.DV.Location.Period[j].value
@@ -212,14 +211,14 @@ WeatherProvider.register("ukmetoffice", {
* Convert temp (from degrees C) if required
*/
convertTemp(tempInC) {
return this.units === "imperial" ? tempInC * 9 / 5 + 32 : tempInC;
return this.tempUnits === "imperial" ? tempInC * 9 / 5 + 32 : tempInC;
},
/*
* Convert wind speed (from mph) if required
*/
convertWindSpeed(windInMph) {
return this.units === "metric" ? windInMph * 2.23694 : windInMph;
return this.windUnits === "metric" ? windInMph * 2.23694 : windInMph;
},
/*

10
modules/default/weather/providers/weathergov.js Normal file → Executable file
View File

@@ -67,7 +67,7 @@ WeatherProvider.register("weathergov", {
* Generate a WeatherObject based on currentWeatherInformation
*/
generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units);
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
currentWeather.temperature = currentWeatherData.temperature;
currentWeather.windSpeed = currentWeatherData.windSpeed.split(" ", 1);
@@ -95,7 +95,7 @@ WeatherProvider.register("weathergov", {
let maxTemp = [];
// variable for date
let date = "";
let weather = new WeatherObject(this.config.units);
let weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
weather.precipitation = 0;
for (const forecast of forecasts) {
@@ -109,7 +109,7 @@ WeatherProvider.register("weathergov", {
// push weather information to days array
days.push(weather);
// create new weather-object
weather = new WeatherObject(this.config.units);
weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
minTemp = [];
maxTemp = [];
@@ -134,7 +134,7 @@ WeatherProvider.register("weathergov", {
minTemp.push(forecast.temperature);
maxTemp.push(forecast.temperature);
}
// last day
// calculate minimum/maximum temperature, specify rain amount
weather.minTemperature = Math.min.apply(null, minTemp);
@@ -202,7 +202,7 @@ WeatherProvider.register("weathergov", {
}
return "night-clear";
} else if (weatherType.includes("Dust") || weatherType.includes("Sand")) {
} else if (weatherType.includes("Dust") || weatherType.includes("Sand")) {
return "dust";
} else if (weatherType.includes("Fog")) {
return "fog";