Files
Civic-Segments/src/pkjs/weather.js
T
2026-06-01 23:11:57 -04:00

44 lines
1.3 KiB
JavaScript

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 +
'&current=temperature_2m';
xhrRequest(url, 'GET',
function (responseText) {
const current_temp = JSON.parse(responseText).current.temperature_2m;
const min_temp = JSON.parse(localStorage.getItem('clay-settings')).CLAY_TEMP_BAR_0;
const max_temp = min_temp + 80;
const clamped_temp = Math.min(Math.max(current_temp, min_temp), max_temp);
const normalized_temp = clamped_temp - min_temp;
const temp_bars = Math.round(normalized_temp / 4);
Pebble.sendAppMessage({ PKJS_TEMP_BAR_COUNT: temp_bars });
}
);
}
function getWeather() {
navigator.geolocation.getCurrentPosition(
locationSuccess,
null,
{ timeout: 15000, maximumAge: 60000 }
);
}
Pebble.addEventListener("appmessage", function (dict) {
if (dict.payload["PKJS_TEMP_BAR_COUNT"]) {
getWeather();
}
});