(function() { var DAYS, LAT, LONG, MONTHS, altitude, get_day_night, get_season, last_day_night, last_seconds, last_sep, pad_time, update_05_secs, update_10_min; LAT = 43.62; LONG = 10.63; DAYS = ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab']; MONTHS = ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic']; /* add a zero in front of numbers < 10 */ pad_time = function(i) { if (i < 10) { return "0" + i; } else { return i; } }; /* add methods to Date to check if DST is in effect */ /* from http://stackoverflow.com/questions/11887934/check-if-daylight-saving-time-is-in-effect-and-if-it-is-for-how-many-hours */ Date.prototype.stdTimezoneOffset = function() { var jan, jul; jan = new Date(this.getFullYear(), 0, 1); jul = new Date(this.getFullYear(), 6, 1); return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); }; Date.prototype.dst = function() { return this.getTimezoneOffset() < this.stdTimezoneOffset(); }; /* SEASONS */ get_season = function(now) { var seasons; seasons = Date.getSeasons(now.getFullYear()); if (now < seasons[1]) { return 'winter'; } else if (now < seasons[2]) { return 'spring'; } else if (now < seasons[3]) { return 'summer'; } else if (now < seasons[4]) { return 'fall'; } else { return 'winter'; } }; /* define a scale for SUN and MOON altitude */ altitude = d3.scale.linear().domain([-1, 1]).range([600, 0]); /* DAY/NIGHT */ get_day_night = function(now) { var times; times = SunCalc.getTimes(now, LAT, LONG); if (now < times.sunriseEnd) { return 'night'; } else if (now < times.dusk) { return 'day'; } else { return 'night'; } }; /* update every 0.5 seconds */ last_seconds = -1; last_sep = false; last_day_night = null; update_05_secs = function() { /* CLOCK */ var clock_string, day_night, h, m, moon_alt, moon_phase, now, s, sun_alt; now = new Date(); h = now.getHours(); m = now.getMinutes(); s = now.getSeconds(); h = pad_time(h); m = pad_time(m); s = pad_time(s); if (s !== last_seconds) { last_sep = !last_sep; last_seconds = s; } clock_string = h; if (last_sep) { clock_string += ':'; } else { clock_string += ' '; } clock_string += m; d3.select('#clock').text(clock_string); /* DAYLIGHT SAVINGS TIME */ d3.select('#dst').text(now.dst() ? "LEG" : "SOL"); /* CALENDAR */ d3.select('#calendar').text("" + DAYS[now.getDay()] + " " + (now.getDate()) + " " + MONTHS[now.getMonth()] + " " + (now.getFullYear())); /* SEASONS */ d3.select('#season').text((function() { switch (get_season(now)) { case 'winter': return 'inverno'; case 'spring': return 'primavera'; case 'summer': return 'estate'; case 'fall': return 'autunno'; } })()); /* NETWORK status */ d3.select('#router').classed('ok', navigator.onLine); d3.select('#wifirouter').classed('ok', true); /* SUN and MOON simulation */ sun_alt = Math.sin(SunCalc.getPosition(now, LAT, LONG).altitude); moon_alt = Math.sin(SunCalc.getMoonPosition(now, LAT, LONG).altitude); moon_phase = SunCalc.getMoonIllumination(now); d3.select('#sun').attr('y', altitude(sun_alt)).attr('fill', '#FFF268'); d3.select('#moon').attr('y', altitude(moon_alt)).attr('fill', '#BDDBFD'); d3.select('#moon_symbol > path').attr('d', moon_phase.angle > 0 ? "M0 30 A30 30 0 1 1 0 -30 A" + (60 * Math.abs(moon_phase.fraction - 0.5)) + " 30 0 1 " + (moon_phase.fraction > 0.5 ? 1 : 0) + " 0 30" : "M0 30 A" + (60 * Math.abs(moon_phase.fraction - 0.5)) + " 30 0 1 " + (moon_phase.fraction > 0.5 ? 1 : 0) + " 0 -30 A30 30 0 1 1 0 30"); /* NIGHT and DAY */ day_night = get_day_night(now); if (day_night !== last_day_night) { if (day_night === 'day') { d3.select('body').transition().duration(3000).style('background', '#00BCD1'); d3.select('#land').transition().duration(3000).attr('fill', '#ABEC72'); } else if (day_night === 'night') { d3.select('body').transition().duration(3000).style('background', '#434367'); d3.select('#land').transition().duration(3000).attr('fill', '#0A8690'); } last_day_night = day_night; } return setTimeout((function() { return update_05_secs(); }), 500); }; /* update every 10 minutes */ update_10_min = function() { /* WHEATER from OpenWeatherMap */ d3.json('http://api.openweathermap.org/data/2.5/weather?q=Ponsacco,it&APPID=eaecf85a68297fd2c57d209840cb81dc&lang=it', function(data) { var status; if (!(data != null)) return; d3.select('#temperature').text("" + (Math.round(data.main.temp - 273.15)) + "°"); d3.select('#humidity').text("" + (Math.round(data.main.humidity)) + "%"); if ((data.weather != null) && (data.weather[0] != null)) { status = +data.weather[0].icon.slice(0, 2); /* set haze visibility */ if (status < 4) { d3.select('#haze').transition().duration(3000).attr('fill-opacity', 0); } else { d3.select('#haze').transition().duration(3000).attr('fill-opacity', 0.5); } /* set clouds visibility */ if (status < 2 || status === 50) { d3.select('#clouds').transition().duration(3000).attr('opacity', 0); } else { d3.select('#clouds').transition().duration(3000).attr('opacity', 1); } /* set more clouds visibility */ if (status < 3 || status === 50) { return d3.select('#more_clouds').transition().duration(3000).attr('opacity', 0); } else { return d3.select('#more_clouds').transition().duration(3000).attr('opacity', 1); } } }); return setTimeout((function() { return update_10_min(); }), 600000); }; update_05_secs(); update_10_min(); }).call(this);