mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 03:39:55 +00:00
move weather into its own folder and file
This commit is contained in:
parent
2e812ac8ae
commit
97bd948200
26
js/config.js
26
js/config.js
@ -1,4 +1,16 @@
|
||||
var config = {
|
||||
lang: 'en',
|
||||
weather: {
|
||||
//change weather params here:
|
||||
//units: metric or imperial
|
||||
params: {
|
||||
q: 'Philadelphia, Pennsylvania',
|
||||
units: 'imperial',
|
||||
// if you want a different lang for the weather that what is set above, change it here
|
||||
lang: 'en',
|
||||
APPID: 'ba8a928b5152be1d4b371b256554abfc'
|
||||
}
|
||||
},
|
||||
compliments: {
|
||||
interval: 2000,
|
||||
fadeInterval: 4000,
|
||||
@ -20,20 +32,6 @@ var config = {
|
||||
}
|
||||
}
|
||||
|
||||
// for navigator language
|
||||
// var lang = window.navigator.language;
|
||||
// you can change the language
|
||||
var lang = 'en';
|
||||
|
||||
//change weather params here:
|
||||
//units: metric or imperial
|
||||
var weatherParams = {
|
||||
'q':'Philadelphia,Pennsylvania',
|
||||
'units':'imperial',
|
||||
'lang':lang,
|
||||
'APPID':'ba8a928b5152be1d4b371b256554abfc'
|
||||
};
|
||||
|
||||
// var feed = 'http://feeds.nos.nl/nosjournaal?format=rss';
|
||||
//var feed = 'http://www.nu.nl/feeds/rss/achterklap.rss';
|
||||
//var feed = 'http://www.nu.nl/feeds/rss/opmerkelijk.rss';
|
||||
|
143
js/main.js
143
js/main.js
@ -24,18 +24,6 @@ function roundVal(temp)
|
||||
return Math.round(temp * 10) / 10;
|
||||
}
|
||||
|
||||
function kmh2beaufort(kmh)
|
||||
{
|
||||
var speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000];
|
||||
for (var beaufort in speeds) {
|
||||
var speed = speeds[beaufort];
|
||||
if (speed > kmh) {
|
||||
return beaufort;
|
||||
}
|
||||
}
|
||||
return 12;
|
||||
}
|
||||
|
||||
jQuery(document).ready(function($) {
|
||||
|
||||
var news = [];
|
||||
@ -46,7 +34,7 @@ jQuery(document).ready(function($) {
|
||||
var lastCompliment;
|
||||
var compliment;
|
||||
|
||||
moment.lang(lang);
|
||||
moment.lang(config.lang);
|
||||
|
||||
//connect do Xbee monitor
|
||||
// var socket = io.connect('http://rpi-alarm.local:8082');
|
||||
@ -202,134 +190,7 @@ jQuery(document).ready(function($) {
|
||||
|
||||
compliments.init();
|
||||
|
||||
(function updateCurrentWeather()
|
||||
{
|
||||
var iconTable = {
|
||||
'01d':'wi-day-sunny',
|
||||
'02d':'wi-day-cloudy',
|
||||
'03d':'wi-cloudy',
|
||||
'04d':'wi-cloudy-windy',
|
||||
'09d':'wi-showers',
|
||||
'10d':'wi-rain',
|
||||
'11d':'wi-thunderstorm',
|
||||
'13d':'wi-snow',
|
||||
'50d':'wi-fog',
|
||||
'01n':'wi-night-clear',
|
||||
'02n':'wi-night-cloudy',
|
||||
'03n':'wi-night-cloudy',
|
||||
'04n':'wi-night-cloudy',
|
||||
'09n':'wi-night-showers',
|
||||
'10n':'wi-night-rain',
|
||||
'11n':'wi-night-thunderstorm',
|
||||
'13n':'wi-night-snow',
|
||||
'50n':'wi-night-alt-cloudy-windy'
|
||||
}
|
||||
|
||||
|
||||
$.getJSON('http://api.openweathermap.org/data/2.5/weather', weatherParams, function(json, textStatus) {
|
||||
|
||||
var temp = roundVal(json.main.temp);
|
||||
var temp_min = roundVal(json.main.temp_min);
|
||||
var temp_max = roundVal(json.main.temp_max);
|
||||
|
||||
var wind = roundVal(json.wind.speed);
|
||||
|
||||
var iconClass = iconTable[json.weather[0].icon];
|
||||
var icon = $('<span/>').addClass('icon').addClass('dimmed').addClass('wi').addClass(iconClass);
|
||||
$('.temp').updateWithText(icon.outerHTML()+temp+'°', 1000);
|
||||
|
||||
// var forecast = 'Min: '+temp_min+'°, Max: '+temp_max+'°';
|
||||
// $('.forecast').updateWithText(forecast, 1000);
|
||||
|
||||
var now = new Date();
|
||||
var sunrise = new Date(json.sys.sunrise*1000).toTimeString().substring(0,5);
|
||||
var sunset = new Date(json.sys.sunset*1000).toTimeString().substring(0,5);
|
||||
|
||||
var windString = '<span class="wi wi-strong-wind xdimmed"></span> ' + kmh2beaufort(wind) ;
|
||||
var sunString = '<span class="wi wi-sunrise xdimmed"></span> ' + sunrise;
|
||||
if (json.sys.sunrise*1000 < now && json.sys.sunset*1000 > now) {
|
||||
sunString = '<span class="wi wi-sunset xdimmed"></span> ' + sunset;
|
||||
}
|
||||
|
||||
$('.windsun').updateWithText(windString+' '+sunString, 1000);
|
||||
});
|
||||
|
||||
setTimeout(function() {
|
||||
updateCurrentWeather();
|
||||
}, 60000);
|
||||
})();
|
||||
|
||||
(function updateWeatherForecast()
|
||||
{
|
||||
var iconTable = {
|
||||
'01d':'wi-day-sunny',
|
||||
'02d':'wi-day-cloudy',
|
||||
'03d':'wi-cloudy',
|
||||
'04d':'wi-cloudy-windy',
|
||||
'09d':'wi-showers',
|
||||
'10d':'wi-rain',
|
||||
'11d':'wi-thunderstorm',
|
||||
'13d':'wi-snow',
|
||||
'50d':'wi-fog',
|
||||
'01n':'wi-night-clear',
|
||||
'02n':'wi-night-cloudy',
|
||||
'03n':'wi-night-cloudy',
|
||||
'04n':'wi-night-cloudy',
|
||||
'09n':'wi-night-showers',
|
||||
'10n':'wi-night-rain',
|
||||
'11n':'wi-night-thunderstorm',
|
||||
'13n':'wi-night-snow',
|
||||
'50n':'wi-night-alt-cloudy-windy'
|
||||
}
|
||||
$.getJSON('http://api.openweathermap.org/data/2.5/forecast', weatherParams, function(json, textStatus) {
|
||||
|
||||
var forecastData = {};
|
||||
|
||||
for (var i in json.list) {
|
||||
var forecast = json.list[i];
|
||||
var dateKey = forecast.dt_txt.substring(0, 10);
|
||||
|
||||
if (forecastData[dateKey] == undefined) {
|
||||
forecastData[dateKey] = {
|
||||
'timestamp':forecast.dt * 1000,
|
||||
'icon':forecast.weather[0].icon,
|
||||
'temp_min':forecast.main.temp,
|
||||
'temp_max':forecast.main.temp
|
||||
};
|
||||
} else {
|
||||
forecastData[dateKey]['icon'] = forecast.weather[0].icon;
|
||||
forecastData[dateKey]['temp_min'] = (forecast.main.temp < forecastData[dateKey]['temp_min']) ? forecast.main.temp : forecastData[dateKey]['temp_min'];
|
||||
forecastData[dateKey]['temp_max'] = (forecast.main.temp > forecastData[dateKey]['temp_max']) ? forecast.main.temp : forecastData[dateKey]['temp_max'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
var forecastTable = $('<table />').addClass('forecast-table');
|
||||
var opacity = 1;
|
||||
for (var i in forecastData) {
|
||||
var forecast = forecastData[i];
|
||||
var iconClass = iconTable[forecast.icon];
|
||||
var dt = new Date(forecast.timestamp);
|
||||
var row = $('<tr />').css('opacity', opacity);
|
||||
|
||||
row.append($('<td/>').addClass('day').html(moment.weekdaysShort(dt.getDay())));
|
||||
row.append($('<td/>').addClass('icon-small').addClass(iconClass));
|
||||
row.append($('<td/>').addClass('temp-max').html(roundVal(forecast.temp_max)));
|
||||
row.append($('<td/>').addClass('temp-min').html(roundVal(forecast.temp_min)));
|
||||
|
||||
forecastTable.append(row);
|
||||
opacity -= 0.155;
|
||||
}
|
||||
|
||||
|
||||
$('.forecast').updateWithText(forecastTable, 1000);
|
||||
});
|
||||
|
||||
setTimeout(function() {
|
||||
updateWeatherForecast();
|
||||
}, 60000);
|
||||
})();
|
||||
weather.init();
|
||||
|
||||
(function fetchNews() {
|
||||
$.feedToJson({
|
||||
|
152
js/weather/weather.js
Normal file
152
js/weather/weather.js
Normal file
@ -0,0 +1,152 @@
|
||||
var weather = {
|
||||
// Default language is Dutch because that is what the original author used
|
||||
lang: config.lang || 'nl',
|
||||
params: config.weather.params || null,
|
||||
iconTable: {
|
||||
'01d':'wi-day-sunny',
|
||||
'02d':'wi-day-cloudy',
|
||||
'03d':'wi-cloudy',
|
||||
'04d':'wi-cloudy-windy',
|
||||
'09d':'wi-showers',
|
||||
'10d':'wi-rain',
|
||||
'11d':'wi-thunderstorm',
|
||||
'13d':'wi-snow',
|
||||
'50d':'wi-fog',
|
||||
'01n':'wi-night-clear',
|
||||
'02n':'wi-night-cloudy',
|
||||
'03n':'wi-night-cloudy',
|
||||
'04n':'wi-night-cloudy',
|
||||
'09n':'wi-night-showers',
|
||||
'10n':'wi-night-rain',
|
||||
'11n':'wi-night-thunderstorm',
|
||||
'13n':'wi-night-snow',
|
||||
'50n':'wi-night-alt-cloudy-windy'
|
||||
},
|
||||
temperatureLocation: '.temp',
|
||||
windSunLocation: '.windsun',
|
||||
forecastLocation: '.forecast',
|
||||
apiVersion: '2.5',
|
||||
apiBase: 'http://api.openweathermap.org/data/',
|
||||
weatherEndpoint: 'weather',
|
||||
forecastEndpoint: 'forecast/daily',
|
||||
updateInterval: config.weather.interval || 6000,
|
||||
fadeInterval: config.weather.fadeInterval || 1000,
|
||||
intervalId: null
|
||||
}
|
||||
|
||||
weather.roundValue = function (temperature) {
|
||||
return Math.round(temperature * 10) / 10;
|
||||
}
|
||||
|
||||
weather.kmh2Beaufort = function(kmh) {
|
||||
var speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000];
|
||||
for (var beaufort in speeds) {
|
||||
var speed = speeds[beaufort];
|
||||
if (speed > kmh) {
|
||||
return beaufort;
|
||||
}
|
||||
}
|
||||
return 12;
|
||||
}
|
||||
|
||||
weather.updateCurrentWeather = function () {
|
||||
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: weather.apiBase + '/' + weather.apiVersion + '/' + weather.weatherEndpoint,
|
||||
dataType: 'json',
|
||||
data: weather.params,
|
||||
success: function (data) {
|
||||
|
||||
var _temperature = this.roundValue(data.main.temp),
|
||||
_temperatureMin = this.roundValue(data.main.temp_min),
|
||||
_temperatureMax = this.roundValue(data.main.temp_max),
|
||||
_wind = this.roundValue(data.wind.speed),
|
||||
_iconClass = this.iconTable[data.weather[0].icon];
|
||||
|
||||
var _icon = '<span class="icon ' + _iconClass + ' dimmed wi"></span>';
|
||||
|
||||
var _newTempHtml = _icon + '' + _temperature + '°';
|
||||
|
||||
$(this.temperatureLocation).updateWithText(_newTempHtml, this.fadeInterval);
|
||||
|
||||
var _now = moment(),
|
||||
_sunrise = moment(data.sys.sunrise*1000).format('HH:mm'),
|
||||
_sunset = moment(data.sys.sunset*1000).format('HH:mm');
|
||||
|
||||
var _newWindHtml = '<span class="wi wi-strong-wind xdimmed"></span> ' + this.kmh2Beaufort(_wind),
|
||||
_newSunHtml = '<span class="wi wi-sunrise xdimmed"></span> ' + _sunrise;
|
||||
|
||||
if (_sunrise < _now && _sunset > now) {
|
||||
_newSunHtml = '<span class="wi wi-sunset xdimmed"></span> ' + _sunset;
|
||||
}
|
||||
|
||||
$(this.windSunLocation).updateWithText(_newWindHtml + ' ' + _newSunHtml, this.fadeInterval);
|
||||
|
||||
}.bind(this),
|
||||
error: function () {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
weather.updateWeatherForecast = function () {
|
||||
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: weather.apiBase + '/' + weather.apiVersion + '/' + weather.forecastEndpoint,
|
||||
data: weather.params,
|
||||
success: function (data) {
|
||||
|
||||
console.log('data', data);
|
||||
|
||||
var _opacity = 1,
|
||||
_forecastHtml = '';
|
||||
|
||||
_forecastHtml += '<table class="forecast-table">';
|
||||
|
||||
for (var i = 0, count = data.list.length; i < count; i++) {
|
||||
|
||||
var _forecast = data.list[i];
|
||||
|
||||
_forecastHtml += '<tr style="opacity:' + _opacity + '">';
|
||||
|
||||
_forecastHtml += '<td class="day">' + moment(_forecast.dt, 'X').format('ddd') + '</td>';
|
||||
_forecastHtml += '<td class="icon-small ' + this.iconTable[_forecast.weather[0].icon] + '"></td>';
|
||||
_forecastHtml += '<td class="temp-max">' + this.roundValue(_forecast.temp.max) + '</td>';
|
||||
_forecastHtml += '<td class="temp-min">' + this.roundValue(_forecast.temp.min) + '</td>';
|
||||
|
||||
_forecastHtml += '</tr>';
|
||||
|
||||
_opacity -= 0.155;
|
||||
|
||||
}
|
||||
|
||||
_forecastHtml += '</table>';
|
||||
|
||||
$(this.forecastLocation).updateWithText(_forecastHtml, this.fadeInterval);
|
||||
|
||||
}.bind(this),
|
||||
error: function () {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
weather.init = function () {
|
||||
|
||||
if (weather.params.lang === undefined) {
|
||||
weather.params.lang = weather.lang;
|
||||
}
|
||||
|
||||
if (weather.params.cnt === undefined) {
|
||||
weather.params.cnt = 5;
|
||||
}
|
||||
|
||||
weather.updateCurrentWeather();
|
||||
|
||||
weather.updateWeatherForecast();
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user