Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the setupVars.conf option TEMPERATUREUNIT, plus slight rearrangement of settings page #2516

Merged
merged 4 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down
35 changes: 27 additions & 8 deletions scripts/pi-hole/js/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
Expand All @@ -200,6 +216,9 @@ function initCPUtemp() {
tempunitSelector.on("change", function () {
tempunit = $(this).val();
setCPUtemp(tempunit);

// store the selected value on setupVars.conf
setSetupvarsTempUnit(tempunit);
});
}
}
Expand Down
20 changes: 18 additions & 2 deletions scripts/pi-hole/php/header_authenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions scripts/pi-hole/php/sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
}
echo '<span id="temperature"><i class="fa fa-w fa-fire '.$tempcolor.'" style="width: 1em !important"></i> ';
echo 'Temp:&nbsp;<span id="rawtemp" hidden>'.$celsius.'</span>';
echo '<span id="tempunit" hidden>'.$temperatureunit.'</span>';
echo '<span id="tempdisplay"></span></span>';
}
?>
Expand Down
Loading