-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathweather_openmeteo.js
More file actions
126 lines (107 loc) · 3.63 KB
/
weather_openmeteo.js
File metadata and controls
126 lines (107 loc) · 3.63 KB
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
var weatherCommon = require('./weather');
module.exports.getWeatherFromCoords = getWeatherFromCoords;
function getWeatherFromCoords(pos) {
console.log('getting that weather');
var lat = pos.coords.latitude;
var lon = pos.coords.longitude;
var url = 'https://api.open-meteo.com/v1/forecast?latitude=' +
lat + '&longitude=' + lon +
'¤t_weather=true' +
'&daily=temperature_2m_max,temperature_2m_min,uv_index_max,weathercode' +
'&hourly=uv_index' +
'&minutely_15=apparent_temperature' +
'&timezone=auto';
console.log(url);
getAndSendWeather(url);
}
function getAndSendWeather(url) {
weatherCommon.xhrRequest(url, 'GET', function(responseText) {
var json = JSON.parse(responseText);
// Handle current weather
var temperature = Math.round(json.current_weather.temperature);
var conditionCode = json.current_weather.weathercode;
var isNight = json.current_weather.is_day !== 1;
console.log('Current temperature is ' + temperature);
console.log('Current condition code is ' + conditionCode);
var iconToLoad = getIconForWeatherCode(conditionCode, isNight);
// Handle forecast
var forecastHigh = Math.round(json.daily.temperature_2m_max[0]);
var forecastLow = Math.round(json.daily.temperature_2m_min[0]);
var forecastCode = json.daily.weathercode[0];
function getValueFromArrays(times, values) {
var now = new Date();
var closestIndex = 0;
var smallestDiff = Infinity;
for (var i = 0; i < times.length; i++) {
var t = new Date(times[i]);
var diff = Math.abs(t - now);
if (diff < smallestDiff) {
smallestDiff = diff;
closestIndex = i;
}
}
return Math.round(values[closestIndex]);
}
var uvIndex = getValueFromArrays(json.hourly.time, json.hourly.uv_index);
var apparentTemperature = getValueFromArrays(json.minutely_15.time, json.minutely_15.apparent_temperature);
console.log('Forecast high/low: ' + forecastHigh + '/' + forecastLow);
console.log('Forecast condition code: ' + forecastCode);
console.log('UV Index: ' + uvIndex);
console.log('Is Night ' + isNight);
var forecastIcon = getIconForWeatherCode(forecastCode);
var dictionary = {
'WeatherTemperature': temperature,
'WeatherCondition': iconToLoad,
'WeatherForecastHighTemp': forecastHigh,
'WeatherForecastLowTemp': forecastLow,
'WeatherForecastCondition': forecastIcon,
'WeatherUVIndex': uvIndex,
'WeatherApparentTemperature': apparentTemperature
};
console.log(JSON.stringify(dictionary));
weatherCommon.sendWeatherToPebble(dictionary);
});
}
function getIconForWeatherCode(code, isNight) {
switch (code) {
case 0:
return isNight ? weatherCommon.icons.CLEAR_NIGHT : weatherCommon.icons.CLEAR_DAY;
case 1:
case 2:
return isNight ? weatherCommon.icons.PARTLY_CLOUDY_NIGHT : weatherCommon.icons.PARTLY_CLOUDY;
case 3:
case 45:
case 48:
return weatherCommon.icons.CLOUDY_DAY;
case 51:
case 53:
case 55:
case 61:
case 80:
return weatherCommon.icons.LIGHT_RAIN;
case 63:
case 65:
case 81:
case 82:
return weatherCommon.icons.HEAVY_RAIN;
case 56:
case 57:
case 66:
case 67:
return weatherCommon.icons.RAINING_AND_SNOWING;
case 71:
case 77:
case 85:
return weatherCommon.icons.LIGHT_SNOW;
case 73:
case 75:
case 86:
return weatherCommon.icons.HEAVY_SNOW;
case 95:
case 96:
case 99:
return weatherCommon.icons.THUNDERSTORM;
default:
return weatherCommon.icons.WEATHER_GENERIC;
}
}