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

63 lines
2.2 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() {
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: {
latitude: userPosition.coords.latitude,
longitude: userPosition.coords.longitude
},
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");
$("#weathericon").attr("src", "assets/images/weather-" + resp.icon + ".svg");
} 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
setTimeout(function () {
if (!requestfinished) {
app.dialog.preloader("Checking Weather...");
weatherdialogopen = true;
}
}, 1000);
}