You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PackageHelper/www/assets/js/toolbox_weather.js

86 lines
3.4 KiB
JavaScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
function ftoc(f) {
return (f - 32) * 5 / 9;
}
function loadWeather(reload) {
if (typeof reload == "undefined") {
reload = false;
} else {
reload = reload == true;
}
if (userPosition.coords.accuracy > 99999) {
app.dialog.alert("Couldn't find your location. Wait for a GPS signal and try again.", "Error");
return;
}
var requestfinished = false;
var weatherdialogopen = false;
$.ajax({
url: SETTINGS.weatherapi,
dataType: 'json',
data: {
// Round the numbers off to increase user privacy
// Accuracy with two decimal places is ~1.1km/0.6mi
latitude: userPosition.coords.latitude.toFixed(2),
longitude: userPosition.coords.longitude.toFixed(2)
},
timeout: 15 * 1000,
success: function (resp) {
if (weatherdialogopen) {
app.dialog.close();
weatherdialogopen = false;
}
requestfinished = true;
if (resp.status == "OK") {
var mintemp = (localStorage.getItem("units") == "metric" ? Math.round(ftoc(resp.temp.min)) + " °C" : Math.round(resp.temp.min) + " °F");
var maxtemp = (localStorage.getItem("units") == "metric" ? Math.round(ftoc(resp.temp.max)) + " °C" : Math.round(resp.temp.max) + " °F");
$("#lowtemp").html(mintemp);
$("#hightemp").html(maxtemp);
$("#precipchance").text(Math.round(resp.precipitation.chance * 100.0) + "% chance");
if (localStorage.getItem("units") == "metric") {
$("#windspeed").text(Math.round(resp.windspeed * 1.609344) + " km/h");
} else {
$("#windspeed").text(Math.round(resp.windspeed) + " mph");
}
if (SETTINGS.weathericons.includes(resp.icon)) {
$("#weathericon").attr("src", "assets/images/weather-" + resp.icon + ".svg");
} else {
$("#weathericon").attr("src", "assets/images/weather-none.svg");
}
$("#forecast-location").text(resp.location_name);
$("#forecast-info").text("Forecast covers the next " + resp.forecast_hours + " hours (until " + timestampToTimeString(resp.forecast_until) + ").");
} else {
app.dialog.alert(resp.message, "Error");
}
},
error: function (jqXHR, status, errorThrown) {
if (weatherdialogopen) {
app.dialog.close();
weatherdialogopen = false;
}
requestfinished = true;
app.dialog.alert("There was a network issue while checking the weather. Please try again.", "Error");
}
});
// Open a loading message if there's a delay or we're reloading
if (reload) {
app.dialog.preloader("Checking Weather...");
weatherdialogopen = true;
} else {
setTimeout(function () {
if (!requestfinished) {
app.dialog.preloader("Checking Weather...");
weatherdialogopen = true;
}
}, 1000);
}
}