-
Notifications
You must be signed in to change notification settings - Fork 1
/
weatherForecast.js
executable file
·114 lines (97 loc) · 5.14 KB
/
weatherForecast.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Written by Joshua Fabian, [email protected]
// NWS alerts (updates once every 600 seconds, or 10 minutes)
function toCelcius(fTemp) {
return Math.floor((fTemp - 32) * 5 / 9);
}
function toFahrenheit(cTemp) {
return Math.floor((cTemp * 9 / 5) + 32);
}
var currentTempF = 0;
var currentTempC = 0;
var currentIsDaytime = 'day';
var currentWeather = '';
var currentWeatherIcon = 'wi-train';
var weatherForecastUpdate = function nextWeatherForecastUpdate() {
// Fetch current conditions
jQuery(document).ready(function($) {
$.ajax({
url : "https://api.weather.gov/stations/" + weatherStation + "/observations/latest",
dataType : "json",
success : function(parsed_json) {
var currentWeatherOutput = parsed_json['properties'];
var htmlForForecasts = '';
if (currentWeatherOutput['temperature']['unitCode'].includes('degF')) {
currentTempF = Math.round(currentWeatherOutput['temperature']['value']);
currentTempC = toCelcius(currentWeatherOutput['temperature']['value']);
} else if (currentWeatherOutput['temperature']['unitCode'].includes('degC')) {
currentTempC = Math.round(currentWeatherOutput['temperature']['value']);
currentTempF = toFahrenheit(currentWeatherOutput['temperature']['value']);
}
currentWeather = currentWeatherOutput['textDescription'];
}
});
});
// Fetch upcoming forecast
jQuery(document).ready(function($) {
$.ajax({
url : "https://api.weather.gov/gridpoints/" + weatherOffice + "/" + weatherGridPoint['x'] + "," + weatherGridPoint['y'] + "/forecast/hourly",
dataType : "json",
success : function(parsed_json) {
var allForecasts = parsed_json['properties']['periods'];
var htmlForForecasts = '';
for (i = 0; i < allForecasts.length && i <= 12; i = i + 3) {
var description = allForecasts[i]['shortForecast'];
var timestamp = new Date(allForecasts[i]['startTime']);
var hourDisplay = timestamp.getHours().toString().length == 1 ? '0' + timestamp.getHours() : timestamp.getHours()
var timeDisplay = hourDisplay + ':00';
var isDaytime = allForecasts[i]['isDaytime'];
var isDaytimeDisplay = 'night';
if (isDaytime == true) {
isDaytimeDisplay = 'day';
}
var fTemp = 0;
var cTemp = 0;
if (allForecasts[i]['temperatureUnit'] == 'F') {
fTemp = allForecasts[i]['temperature'];
cTemp = toCelcius(fTemp);
} else if (allForecasts[i]['temperatureUnit'] == 'C') {
cTemp = allForecasts[i]['temperature'];
fTemp = toFahrenheit(cTemp);
}
if (i == 0) {
currentIsDaytime = isDaytimeDisplay;
currentWeatherIcon = weatherIconClass(currentWeather,currentIsDaytime);
}
htmlForForecasts += '<span class="weather-forecast">'
htmlForForecasts += '<span class="weather-forecast-time">' + timeDisplay + '</span>';
htmlForForecasts += '<i class="wi ' + weatherIconClass(description,isDaytimeDisplay) + ' weather-forecast-icon"></i>';
htmlForForecasts += '<span class="weather-forecast-text">' + description + '</span>';
htmlForForecasts += '<span class="weather-forecast-temp-c"><span class="weather-forecast-temp-data">' + cTemp + '°</span><span class="weather-forecast-temp-label">C</span></span>'
htmlForForecasts += '<span class="weather-forecast-temp-f"><span class="weather-forecast-temp-data">' + fTemp + '°</span><span class="weather-forecast-temp-label">F</span></span>';
htmlForForecasts += '</span>';
}
if (document.getElementById('weather-forecast') == null) {
$('#main').append('<div id="weather-forecast" style="display: none; margin: 10px;" class="normal-colors"></div>');
document.getElementById('weather-forecast').innerHTML = htmlForForecasts;
} else {
document.getElementById('weather-forecast').innerHTML = htmlForForecasts;
}
}
});
});
};
var showWeatherForecast = function showWeatherForecast() {
var weatherForecastDiv = document.getElementById('weather-forecast');
if (weatherForecastDiv.style.display == 'none') {
weatherForecastDiv.style.display = 'inline-block';
statsMode = 'weather-forecast-open';
currentStatsUpdate();
} else {
weatherForecastDiv.style.display = 'none';
console.log('You closed it up!')
statsMode = '';
currentStatsUpdate();
}
};
weatherForecastUpdate();
setInterval(weatherForecastUpdate,600000);