MagicMirror/js/main.js
Michael Teeuw 5deb6de863 Brighten
2014-02-25 13:56:00 +01:00

226 lines
5.6 KiB
JavaScript

jQuery.fn.updateWithText = function(text, speed)
{
var dummy = $('<div/>').html(text);
if ($(this).html() != dummy.html())
{
$(this).fadeOut(speed/2, function() {
$(this).html(text);
$(this).fadeIn(speed/2, function() {
//done
});
});
}
}
jQuery.fn.outerHTML = function(s) {
return s
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
};
function roundTemp(temp)
{
return Math.round(temp * 10) / 10;
}
jQuery(document).ready(function($) {
var news = [];
var newsIndex = 0;
var lastCompliment;
var compliment;
var weatherParams = {
'q':'Baarn,Netherlands',
'units':'metric',
'lang':'nl'
};
(function checkVersion()
{
$.getJSON('githash.php', {}, function(json, textStatus) {
if (json) {
if (json.gitHash != gitHash) {
window.location.reload();
window.location.href=window.location.href;
}
}
});
setTimeout(function() {
checkVersion();
}, 3000);
})();
(function updateTime()
{
var days = ['zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag'];
var months = ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'];
var now = new Date();
var day = now.getDay();
var date = now.getDate();
var month = now.getMonth();
var year = now.getFullYear();
var date = days[day] + ', ' + date+' ' + months[month] + ' ' + year;
$('.date').html(date);
$('.time').html(now.toTimeString().substring(0,5));
setTimeout(function() {
updateTime();
}, 1000);
})();
(function updateCompliment()
{
var compliments = ['Hey, handsome!','Hi, sexy!','Hello, beauty!', 'You look sexy!', 'Wow, you look hot!'];
//var compliments = ['Testing ...', 'Please wait ...'];
while (compliment == lastCompliment) {
compliment = Math.floor(Math.random()*compliments.length);
}
$('.compliment').updateWithText(compliments[compliment], 4000);
lastCompliment = compliment;
setTimeout(function() {
updateCompliment();
}, 20000);
})();
(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 = roundTemp(json.main.temp);
var temp_min = roundTemp(json.main.temp_min);
var temp_max = roundTemp(json.main.temp_max);
var iconClass = iconTable[json.weather[0].icon];
var icon = $('<span/>').addClass('icon').addClass('dimmed').addClass(iconClass);
$('.temp').updateWithText(icon.outerHTML()+temp+'&deg;', 1000);
// var forecast = 'Min: '+temp_min+'&deg;, Max: '+temp_max+'&deg;';
// $('.forecast').updateWithText(forecast, 1000);
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 sunString = '<span class="wi-sunrise xdimmed"></span> ' + sunrise+' <span class="wi-sunset xdimmed"></span> ' + sunset;
$('.sun').updateWithText(sunString, 1000);
});
setTimeout(function() {
updateCurrentWeather();
}, 60000);
})();
(function updateWeatherForecast()
{
var dayAbbr = ['zo','ma','di','wo','do','vr','za'];
$.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,
'temp_min':forecast.main.temp,
'temp_max':forecast.main.temp
};
} else {
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');
for (var i in forecastData) {
var forecast = forecastData[i];
var dt = new Date(forecast.timestamp);
var row = $('<tr />');
row.append($('<td/>').addClass('day').html(dayAbbr[dt.getDay()]));
row.append($('<td/>').addClass('temp-max').html(roundTemp(forecast.temp_max)));
row.append($('<td/>').addClass('temp-min').html(roundTemp(forecast.temp_min)));
forecastTable.append(row);
}
$('.forecast').updateWithText(forecastTable, 1000);
});
setTimeout(function() {
updateWeatherForecast();
}, 60000);
})();
(function fetchNews() {
$.feedToJson({
feed:'http://feeds.nos.nl/nosjournaal?format=rss',
//feed:'http://www.nu.nl/feeds/rss/achterklap.rss',
//feed:'http://www.nu.nl/feeds/rss/opmerkelijk.rss',
success: function(data){
news = [];
for (var i in data.item) {
var item = data.item[i];
news.push(item.title);
}
}
});
setTimeout(function() {
fetchNews();
}, 60000);
})();
function showNews() {
var newsItem = news[newsIndex];
$('.news').updateWithText(newsItem,2000);
newsIndex--;
if (newsIndex < 0) newsIndex = news.length - 1;
setTimeout(function() {
showNews();
}, 5500);
};
});