Initial weather support
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
var xhrRequest = function (url, type, callback) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onload = function () {
|
||||
callback(this.responseText);
|
||||
};
|
||||
xhr.open(type, url);
|
||||
xhr.send();
|
||||
};
|
||||
|
||||
function locationSuccess(pos) {
|
||||
var url = 'https://api.open-meteo.com/v1/forecast?' +
|
||||
'latitude=' + pos.coords.latitude +
|
||||
'&longitude=' + pos.coords.longitude +
|
||||
'¤t=temperature_2m';
|
||||
|
||||
xhrRequest(url, 'GET',
|
||||
function (responseText) {
|
||||
var json = JSON.parse(responseText);
|
||||
|
||||
// clamp into a range of 80 (technically 81)C (-32,48), -32C=-25.6F, 48C=118.4F
|
||||
var temperature = Math.min(Math.max(json.current.temperature_2m, -32), 48);
|
||||
// set set range minimum to 0 (add 32) and convert to bars (0-20)
|
||||
temperature = Math.round((temperature + 32) / 4);
|
||||
|
||||
Pebble.sendAppMessage({ PKJS_TEMP_BAR_COUNT: temperature });
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function getWeather() {
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
locationSuccess,
|
||||
null,
|
||||
{ timeout: 15000, maximumAge: 60000 }
|
||||
);
|
||||
}
|
||||
|
||||
Pebble.addEventListener("appmessage", function (dict) {
|
||||
if (dict.payload["PKJS_TEMP_BAR_COUNT"]) {
|
||||
getWeather();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user