forked from MagicMirrorOrg/MagicMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
187 lines (153 loc) · 5.44 KB
/
weather.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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,
orientation: config.weather.orientation || 'vertical',
}
/**
* Rounds a float to one decimal place
* @param {float} temperature The temperature to be rounded
* @return {float} The new floating point value
*/
weather.roundValue = function (temperature) {
return parseFloat(temperature).toFixed(1);
}
/**
* Converts the wind speed (km/h) into the values given by the Beaufort Wind Scale
* @see http://www.spc.noaa.gov/faq/tornado/beaufort.html
* @param {int} kmh The wind speed in Kilometers Per Hour
* @return {int} The wind speed converted into its corresponding Beaufort number
*/
weather.ms2Beaufort = function(ms) {
var kmh = ms * 60 * 60 / 1000;
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;
}
/**
* Retrieves the current temperature and weather patter from the OpenWeatherMap API
*/
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().format('HH:mm'),
_sunrise = moment(data.sys.sunrise*1000).format('HH:mm'),
_sunset = moment(data.sys.sunset*1000).format('HH:mm');
var _newWindHtml = '<span class="wind"><span class="wi wi-strong-wind xdimmed"></span> ' + this.ms2Beaufort(_wind) + '</span>',
_newSunHtml = '<span class="sun"><span class="wi wi-sunrise xdimmed"></span> ' + _sunrise + '</span>';
if (_sunrise < _now && _sunset > _now) {
_newSunHtml = '<span class="sun"><span class="wi wi-sunset xdimmed"></span> ' + _sunset + '</span>';
}
$(this.windSunLocation).updateWithText(_newWindHtml + ' ' + _newSunHtml,this.fadeInterval);
}.bind(this),
error: function () {
}
});
}
/**
* Updates the 5 Day Forecast from the OpenWeatherMap API
*/
weather.updateWeatherForecast = function () {
$.ajax({
type: 'GET',
url: weather.apiBase + '/' + weather.apiVersion + '/' + weather.forecastEndpoint,
data: weather.params,
success: function (data) {
var _opacity = 1,
_forecastHtml = '<tr>',
_forecastHtml2 = '<tr>',
_forecastHtml3 = '<tr>',
_forecastHtml4 = '<tr>';
_forecastHtml = '<table class="forecast-table"><tr>';
for (var i = 0, count = data.list.length; i < count; i++) {
var _forecast = data.list[i];
if (this.orientation == 'vertical') {
_forecastHtml2 = '';
_forecastHtml3 = '';
_forecastHtml4 = '';
}
_forecastHtml += '<td style="opacity:' + _opacity + '" class="day">' + moment(_forecast.dt, 'X').format('ddd') + '</td>';
_forecastHtml2 += '<td style="opacity:' + _opacity + '" class="icon-small ' + this.iconTable[_forecast.weather[0].icon] + '"></td>';
_forecastHtml3 += '<td style="opacity:' + _opacity + '" class="temp-max">' + this.roundValue(_forecast.temp.max) + '</td>';
_forecastHtml4 += '<td style="opacity:' + _opacity + '" class="temp-min">' + this.roundValue(_forecast.temp.min) + '</td>';
_opacity -= 0.155;
if (this.orientation == 'vertical') {
_forecastHtml += _forecastHtml2 + _forecastHtml3 + _forecastHtml4 + '</tr>';
}
}
_forecastHtml += '</tr>',
_forecastHtml2 += '</tr>',
_forecastHtml3 += '</tr>',
_forecastHtml4 += '</tr>';
if (this.orientation == 'vertical') {
_forecastHtml += '</table>';
} else {
_forecastHtml += _forecastHtml2 + _forecastHtml3 + _forecastHtml4 +'</table>';
}
$(this.forecastLocation).updateWithText(_forecastHtml, this.fadeInterval);
}.bind(this),
error: function () {
}
});
}
weather.init = function () {
if (this.params.lang === undefined) {
this.params.lang = this.lang;
}
if (this.params.cnt === undefined) {
this.params.cnt = 6;
}
this.intervalId = setInterval(function () {
this.updateCurrentWeather();
this.updateWeatherForecast();
}.bind(this), this.updateInterval);
this.updateCurrentWeather();
this.updateWeatherForecast();
}