Skip to content

Commit

Permalink
Merge pull request #1573 from vincep5/develop
Browse files Browse the repository at this point in the history
 weather module adjustments for rain and snow
  • Loading branch information
MichMich authored Feb 15, 2019
2 parents 954253c + cbe4d2c commit b7b5517
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Weather forecast now works with openweathermap for both, `/forecast` and `/forecast/daily`, in new weather module. If you use the `/forecast`-weatherEndpoint, the hourly data are converted to daily data, see issues [#1504](https://github.com/MichMich/MagicMirror/issues/1504), [#1513](https://github.com/MichMich/MagicMirror/issues/1513).
- Added fade, fadePoint and maxNumberOfDays properties to the forecast mode [#1516](https://github.com/MichMich/MagicMirror/issues/1516)
- Fixed Loading string and decimalSymbol string replace [#1538](https://github.com/MichMich/MagicMirror/issues/1538)
- Show Snow amounts in new weather module [#1545](https://github.com/MichMich/MagicMirror/issues/1545)

## [2.6.0] - 2019-01-01

Expand Down
2 changes: 1 addition & 1 deletion modules/default/weather/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The following properties can be configured:
| ---------------------------- | -----------
| `tableClass` | The class for the forecast table. <br><br> **Default value:** `'small'`
| `colored` | If set to `true`, the min and max temperature are color coded. <br><br> **Default value:** `false`
| `showRainAmount` | Show the amount of rain in the forecast <br><br> **Possible values:** `true` or `false` <br> **Default value:** `true`
| `showPrecipitationAmount` | Show the amount of rain/snow in the forecast <br><br> **Possible values:** `true` or `false` <br> **Default value:** `false`
| `fade` | Fade the future events to black. (Gradient) <br><br> **Possible values:** `true` or `false` <br> **Default value:** `true`
| `fadePoint` | Where to start fade? <br><br> **Possible values:** `0` (top of the list) - `1` (bottom of list) <br> **Default value:** `0.25`
| `maxNumberOfDays` | How many days of forecast to return. Specified by config.js <br><br> **Possible values:** `1` - `16` <br> **Default value:** `5` (5 days) <br> This value is optional. By default the weatherforecast module will return 5 days.
Expand Down
6 changes: 3 additions & 3 deletions modules/default/weather/forecast.njk
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<td class="align-right min-temp">
{{ f.minTemperature | roundValue | unit("temperature") }}
</td>
{% if config.showRainAmount %}
<td class="align-right bright rain">
{{ f.rain | unit("rain") }}
{% if config.showPrecipitationAmount %}
<td class="align-right bright precipitation">
{{ f.precipitation | unit("precip") }}
</td>
{% endif %}
</tr>
Expand Down
2 changes: 2 additions & 0 deletions modules/default/weather/providers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ A convinience function to make requests. It returns a promise.
| weatherType | `string` | Icon name of the weather type. <br> Possible values: [WeatherIcons](https://www.npmjs.com/package/weathericons) |
| 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` |

#### Current weather

Expand Down
17 changes: 13 additions & 4 deletions modules/default/weather/providers/darksky.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* MIT Licensed
*
* This class is a provider for Dark Sky.
* Note that the Dark Sky API does not provide rainfall. Instead it provides snowfall and precipitation probability
*/
WeatherProvider.register("darksky", {
// Set the name of the provider.
Expand Down Expand Up @@ -81,12 +82,20 @@ WeatherProvider.register("darksky", {
weather.minTemperature = forecast.temperatureMin;
weather.maxTemperature = forecast.temperatureMax;
weather.weatherType = this.convertWeatherType(forecast.icon);
if (this.config.units === "metric" && !isNaN(forecast.precipAccumulation)) {
weather.rain = forecast.precipAccumulation * 10;
} else {
weather.rain = forecast.precipAccumulation;
weather.snow = 0;

// The API will return centimeters if units is 'si' and will return inches for 'us'
// Note that the Dark Sky API does not provide rainfall. Instead it provides snowfall and precipitation probability
if (forecast.hasOwnProperty("precipAccumulation")) {
if (this.config.units === "imperial" && !isNaN(forecast.precipAccumulation)) {
weather.snow = forecast.precipAccumulation;
} else if (!isNaN(forecast.precipAccumulation)) {
weather.snow = forecast.precipAccumulation * 10;
}
}

weather.precipitation = weather.snow;

days.push(weather);
}

Expand Down
42 changes: 32 additions & 10 deletions modules/default/weather/providers/openweathermap.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ WeatherProvider.register("openweathermap", {
var minTemp = [];
var maxTemp = [];
var rain = 0;
var snow = 0;
// variable for date
let date = "";
var weather = new WeatherObject(this.config.units);
Expand All @@ -117,6 +118,8 @@ WeatherProvider.register("openweathermap", {
weather.minTemperature = Math.min.apply(null, minTemp);
weather.maxTemperature = Math.max.apply(null, maxTemp);
weather.rain = rain;
weather.snow = snow;
weather.precipitation = weather.rain + weather.snow;
// push weather information to days array
days.push(weather);
// create new weather-object
Expand All @@ -125,6 +128,7 @@ WeatherProvider.register("openweathermap", {
minTemp = [];
maxTemp = [];
rain = 0;
snow = 0;

// set new date
date = moment(forecast.dt, "X").format("YYYY-MM-DD");
Expand All @@ -149,20 +153,27 @@ WeatherProvider.register("openweathermap", {
if (forecast.hasOwnProperty("rain")) {
if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) {
rain += forecast.rain["3h"] / 25.4;
} else if (!isNaN(forecast.rain["3h"])){
} else if (!isNaN(forecast.rain["3h"])) {
rain += forecast.rain["3h"];
} else {
rain += 0;
}
} else {
rain += 0;
}

if (forecast.hasOwnProperty("snow")) {
if (this.config.units === "imperial" && !isNaN(forecast.snow["3h"])) {
snow += forecast.snow["3h"] / 25.4;
} else if (!isNaN(forecast.snow["3h"])) {
snow += forecast.snow["3h"];
}
}
}

// last day
// calculate minimum/maximum temperature, specify rain amount
weather.minTemperature = Math.min.apply(null, minTemp);
weather.maxTemperature = Math.max.apply(null, maxTemp);
weather.rain = rain;
weather.snow = snow;
weather.precipitation = weather.rain + weather.snow;
// push weather information to days array
days.push(weather);
return days.slice(1);
Expand All @@ -182,20 +193,31 @@ WeatherProvider.register("openweathermap", {
weather.minTemperature = forecast.temp.min;
weather.maxTemperature = forecast.temp.max;
weather.weatherType = this.convertWeatherType(forecast.weather[0].icon);
weather.rain = 0;
weather.snow = 0;

// forecast.rain not available if amount is zero
// The API always returns in millimeters
if (forecast.hasOwnProperty("rain")) {
if (this.config.units === "imperial" && !isNaN(forecast.rain)) {
weather.rain = forecast.rain / 25.4;
} else if (!isNaN(forecast.rain)){
} else if (!isNaN(forecast.rain)) {
weather.rain = forecast.rain;
} else {
weather.rain = 0;
}
} else {
weather.rain = 0;
}

// forecast.snow not available if amount is zero
// The API always returns in millimeters
if (forecast.hasOwnProperty("snow")) {
if (this.config.units === "imperial" && !isNaN(forecast.snow)) {
weather.snow = forecast.snow / 25.4;
} else if (!isNaN(forecast.snow)) {
weather.snow = forecast.snow;
}
}

weather.precipitation = weather.rain + weather.snow;

days.push(weather);
}

Expand Down
2 changes: 1 addition & 1 deletion modules/default/weather/weather.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
padding-right: 0;
}

.weather .rain {
.weather .precipitation {
padding-left: 20px;
padding-right: 0;
}
Expand Down
6 changes: 3 additions & 3 deletions modules/default/weather/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Module.register("weather",{
tableClass: "small",

onlyTemp: false,
showRainAmount: true,
showPrecipitationAmount: false,
colored: false,
showFeelsLike: true
},
Expand Down Expand Up @@ -200,8 +200,8 @@ Module.register("weather",{
value += "K";
}
}
} else if (type === "rain") {
if (isNaN(value) || value === 0) {
} else if (type === "precip") {
if (isNaN(value) || value === 0 || value.toFixed(2) === "0.00") {
value = "";
} else {
value = `${value.toFixed(2)} ${this.config.units === "imperial" ? "in" : "mm"}`;
Expand Down
2 changes: 2 additions & 0 deletions modules/default/weather/weatherobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class WeatherObject {
this.weatherType = null;
this.humidity = null;
this.rain = null;
this.snow = null;
this.precipitation = null;
}

cardinalWindDirection() {
Expand Down

0 comments on commit b7b5517

Please sign in to comment.