Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sunrise/sunset to weathergov #1706

Merged
merged 2 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- use Feels Like temp from feed if present
- optionally display probability of precipitation (PoP) in current weather (UK Met Office data)
- automatically try to fix eslint errors by passing `--fix` option to it
- Added sunrise and sunset times to weathergov weather provider [#1705](https://github.com/MichMich/MagicMirror/issues/1705)

### Updated
- English translation for "Feels" to "Feels like"
Expand Down
78 changes: 42 additions & 36 deletions modules/default/weather/providers/weathergov.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ WeatherProvider.register("weathergov", {

currentWeather.temperature = currentWeatherData.temperature;
currentWeather.windSpeed = currentWeatherData.windSpeed.split(" ", 1);
currentWeather.windDirection = this.convertDirectiontoDegrees(currentWeatherData.windDirection);
currentWeather.windDirection = this.convertWindDirection(currentWeatherData.windDirection);
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.shortForecast, currentWeatherData.isDaytime);

// determine the sunrise/sunset times - not supplied in weather.gov data
let times = this.calcAstroData(this.config.lat, this.config.lon)
currentWeather.sunrise = times[0];
currentWeather.sunset = times[1];

return currentWeather;
},

Expand Down Expand Up @@ -136,7 +141,7 @@ WeatherProvider.register("weathergov", {
}

// last day
// calculate minimum/maximum temperature, specify rain amount
// calculate minimum/maximum temperature
weather.minTemperature = Math.min.apply(null, minTemp);
weather.maxTemperature = Math.max.apply(null, maxTemp);

Expand All @@ -145,6 +150,20 @@ WeatherProvider.register("weathergov", {
return days.slice(1);
},

/*
* Calculate the astronomical data
*/
calcAstroData(lat, lon) {
const sunTimes = [];

// determine the sunrise/sunset times
let times = SunCalc.getTimes(new Date(), lat, lon);
sunTimes.push(moment(times.sunrise, "X"));
sunTimes.push(moment(times.sunset, "X"));

return sunTimes;
},

/*
* Convert the icons to a more usable name.
*/
Expand Down Expand Up @@ -218,39 +237,26 @@ WeatherProvider.register("weathergov", {
/*
Convert the direction into Degrees
*/
convertDirectiontoDegrees(direction) {
if (direction === "NNE"){
return 33.75;
} else if (direction === "NE") {
return 56.25;
} else if (direction === "ENE") {
return 78.75;
} else if (direction === "E") {
return 101.25;
} else if (direction === "ESE") {
return 123.75;
} else if (direction === "SE") {
return 146.25;
} else if (direction === "SSE") {
return 168.75;
} else if (direction === "S") {
return 191.25;
} else if (direction === "SSW") {
return 213.75;
} else if (direction === "SW") {
return 236.25;
} else if (direction === "WSW") {
return 258.75;
} else if (direction === "W") {
return 281.25;
} else if (direction === "WNW") {
return 303.75;
} else if (direction === "NW") {
return 326.25;
} else if (direction === "NNW") {
return 348.75;
} else {
return 0;
}
convertWindDirection(windDirection) {
const windCardinals = {
"N": 0,
"NNE": 22,
"NE": 45,
"ENE": 67,
"E": 90,
"ESE": 112,
"SE": 135,
"SSE": 157,
"S": 180,
"SSW": 202,
"SW": 225,
"WSW": 247,
"W": 270,
"WNW": 292,
"NW": 315,
"NNW": 337
};

return windCardinals.hasOwnProperty(windDirection) ? windCardinals[windDirection] : null;
}
});