diff --git a/api.php b/api.php index 4caecfecd..213512fd4 100644 --- a/api.php +++ b/api.php @@ -75,6 +75,17 @@ $data = array_merge($data, $current); $data = array_merge($data, $latest); $data = array_merge($data, $branches); +} elseif (isset($_GET['setTempUnit'])) { + $unit = strtolower($_GET['setTempUnit']); + if ($unit == 'c' || $unit == 'f' || $unit == 'k') { + pihole_execute('-a -'.$unit); + $result = 'success'; + } else { + // invalid unit + $result = 'error'; + } + + $data = array_merge($data, array('result' => $result)); } elseif (isset($_GET['list'])) { if (!$auth) { exit('Not authorized!'); diff --git a/scripts/pi-hole/js/footer.js b/scripts/pi-hole/js/footer.js index 73ab2107b..e982c0d69 100644 --- a/scripts/pi-hole/js/footer.js +++ b/scripts/pi-hole/js/footer.js @@ -160,10 +160,6 @@ function initCheckboxRadioStyle() { function initCPUtemp() { function setCPUtemp(unit) { - if (localStorage) { - localStorage.setItem("tempunit", tempunit); - } - var temperature = parseFloat($("#rawtemp").text()); var displaytemp = $("#tempdisplay"); if (!isNaN(temperature)) { @@ -185,10 +181,30 @@ function initCPUtemp() { } } - // Read from local storage, initialize if needed - var tempunit = localStorage ? localStorage.getItem("tempunit") : null; - if (tempunit === null) { - tempunit = "C"; + function setSetupvarsTempUnit(unit, showmsg = true) { + var token = encodeURIComponent($("#token").text()); + $.getJSON("api.php?setTempUnit=" + unit + "&token=" + token, function (data) { + if (showmsg === true) { + if ("result" in data && data.result === "success") { + utils.showAlert("success", "", "Temperature unit set to " + unit, ""); + } else { + utils.showAlert("error", "", "", "Temperature unit not set"); + } + } + }); + } + + // Read the temperature unit from HTML code + var tempunit = $("#tempunit").text(); + if (!tempunit) { + // if no value was set in setupVars.conf, tries to retrieve the old config from localstorage + tempunit = localStorage ? localStorage.getItem("tempunit") : null; + if (tempunit === null) { + tempunit = "C"; + } else { + // if some value was found on localstorage, set the value in setupVars.conf + setSetupvarsTempUnit(tempunit, false); + } } setCPUtemp(tempunit); @@ -200,6 +216,9 @@ function initCPUtemp() { tempunitSelector.on("change", function () { tempunit = $(this).val(); setCPUtemp(tempunit); + + // store the selected value on setupVars.conf + setSetupvarsTempUnit(tempunit); }); } } diff --git a/scripts/pi-hole/php/header_authenticated.php b/scripts/pi-hole/php/header_authenticated.php index f84ee5efc..b022d4a62 100644 --- a/scripts/pi-hole/php/header_authenticated.php +++ b/scripts/pi-hole/php/header_authenticated.php @@ -98,7 +98,23 @@ function getTemperature() $limit = null; } - return array($celsius, $limit); + // Get user-defined temperature limit if set + if (isset($setupVars['TEMPERATUREUNIT'])) { + switch (strtoupper($setupVars['TEMPERATUREUNIT'])) { + case 'F': + case 'K': + $unit = strtoupper($setupVars['TEMPERATUREUNIT']); + break; + + default: + $unit = 'C'; + } + } else { + // no value is set in setupVars.conf + $unit = ''; + } + + return array($celsius, $limit, $unit); } check_cors(); @@ -113,7 +129,7 @@ function getTemperature() $maxlifetime = ini_get('session.gc_maxlifetime'); // Get temperature -list($celsius, $temperaturelimit) = getTemperature(); +list($celsius, $temperaturelimit, $temperatureunit) = getTemperature(); // Get CPU load $loaddata = sys_getloadavg(); diff --git a/scripts/pi-hole/php/sidebar.php b/scripts/pi-hole/php/sidebar.php index b15a67178..1d474901b 100644 --- a/scripts/pi-hole/php/sidebar.php +++ b/scripts/pi-hole/php/sidebar.php @@ -57,6 +57,7 @@ } echo ' '; echo 'Temp: '.$celsius.''; + echo ''.$temperatureunit.''; echo ''; } ?> diff --git a/settings.php b/settings.php index f024b894b..2f21449ae 100644 --- a/settings.php +++ b/settings.php @@ -196,7 +196,7 @@ ?> class="active"> DHCP +
Checkbox and radio buttons
-CPU Temperature Unit
-