Merge branch 'feature/2915' of https://github.com/MichaelDeciso/core into MichaelDeciso-feature/2915

This commit is contained in:
Ad Schellevis 2018-12-04 09:28:12 +01:00
commit aad0efbd19

View File

@ -30,28 +30,40 @@ require_once("guiconfig.inc");
config_read_array('widgets', 'thermal_sensors_widget');
function validate_temp_value($value)
function fix_temp_value($value, int $default): int
{
if (is_numeric($value) && (int)$value == $value && $value >= 0 and $value <= 100) {
return (int)$value;
} else {
return $default;
}
}
function fix_checkbox_value($value): bool
{
if ($value !== '') {
return true;
} else {
return false;
}
}
$fieldnames = array('thermal_sensors_widget_zone_warning_threshold', 'thermal_sensors_widget_zone_critical_threshold',
'thermal_sensors_widget_core_warning_threshold', 'thermal_sensors_widget_core_critical_threshold');
$fields = [
['name' => 'thermal_sensors_widget_zone_warning_threshold', 'default' => 70, 'processFunc' => 'fix_temp_value'],
['name' => 'thermal_sensors_widget_zone_critical_threshold', 'default' => 80, 'processFunc' => 'fix_temp_value'],
['name' => 'thermal_sensors_widget_core_warning_threshold', 'default' => 70, 'processFunc' => 'fix_temp_value'],
['name' => 'thermal_sensors_widget_core_critical_threshold', 'default' => 80, 'processFunc' => 'fix_temp_value'],
['name' => 'thermal_sensors_widget_show_one_core_temp', 'default' => false, 'processFunc' => 'fix_checkbox_value'],
];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$pconfig = array();
foreach ($fieldnames as $fieldname) {
$defaultValue = strpos($fieldname, 'critical') !== false ? 80 : 70;
$pconfig[$fieldname] = !empty($config['widgets']['thermal_sensors_widget'][$fieldname]) ? $config['widgets']['thermal_sensors_widget'][$fieldname] : $defaultValue;
$pconfig = [];
foreach ($fields as $field) {
$pconfig[$field['name']] = !empty($config['widgets']['thermal_sensors_widget'][$field['name']]) ? $config['widgets']['thermal_sensors_widget'][$field['name']] : $field['default'];
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
foreach ($fieldnames as $fieldname) {
$defaultValue = strpos($fieldname, 'critical') !== false ? 80 : 70;
$newValue = !empty($_POST[$fieldname]) ? $_POST[$fieldname] : "";
$config['widgets']['thermal_sensors_widget'][$fieldname] = validate_temp_value($newValue) ? $newValue : $defaultValue;
foreach ($fields as $field) {
$newValue = $field['processFunc']($_POST[$field['name']] ?? '', $field['default']);
$config['widgets']['thermal_sensors_widget'][$field['name']] = $newValue;
}
write_config("Thermal sensors widget saved via Dashboard.");
header(url_safe('Location: /index.php'));
@ -60,53 +72,71 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
?>
<script>
function thermal_sensors_widget_update(sender, data)
{
data.map(function(sensor) {
var tempIntValue = parseInt(sensor['temperature']);
var progressbar = $("#thermal_sensors_widget_progress_bar").html();
var tbody = sender.find('tbody');
var tr_id = "thermal_sensors_widget_" + sensor['device'].replace(/\./g, '_');
if (tbody.find("#"+tr_id).length == 0) {
var tr_content = [];
tr_content.push('<tr id="'+tr_id+'">');
tr_content.push('<td>'+progressbar+'</td>');
tr_content.push('</tr>');
tbody.append(tr_content.join(''));
}
let danger_temp, warning_temp;
// probe warning / danger temp
if (sensor['type'] == 'core') {
danger_temp = parseInt($("#thermal_sensors_widget_core_critical_threshold").val());
warning_temp = parseInt($("#thermal_sensors_widget_core_warning_threshold").val());
} else {
danger_temp = parseInt($("#thermal_sensors_widget_zone_critical_threshold").val());
warning_temp = parseInt($("#thermal_sensors_widget_zone_warning_threshold").val());
}
// progress bar style
if (tempIntValue > danger_temp) {
$("#"+tr_id + " .progress-bar").removeClass('progress-bar-success')
.removeClass('progress-bar-warning')
.removeClass('progress-bar-danger')
.addClass('progress-bar-danger');
} else if (tempIntValue > warning_temp) {
$("#"+tr_id + " .progress-bar").removeClass('progress-bar-success')
.removeClass('progress-bar-warning')
.removeClass('progress-bar-danger')
.addClass('progress-bar-warning');
} else {
$("#"+tr_id + " .progress-bar").removeClass('progress-bar-success')
.removeClass('progress-bar-warning')
.removeClass('progress-bar-danger')
.addClass('progress-bar-success');
}
// update bar
$("#"+tr_id + " .progress-bar").html(sensor['temperature'] + ' &deg;C');
$("#"+tr_id + " .progress-bar").css("width", tempIntValue + "%").attr("aria-valuenow", tempIntValue + "%");
// update label
$("#"+tr_id + " .info").html(sensor['type_translated'] + " " + sensor['device_seq'] + " <small>("+sensor['device']+")<small>");
});
}
'use strict';
function thermal_sensors_widget_update(sender, data) {
let firstCore = true;
data.map(function (sensor) {
// Skip if the user only wants one core temperature and we have already showed one.
if (sensor['type'] === 'core' && $('#thermal_sensors_widget_show_one_core_temp').attr('checked') === 'checked')
{
if (!firstCore) {
return;
}
firstCore = false;
}
const tr_id = "thermal_sensors_widget_" + sensor['device'].replace(/\./g, '_');
let tbody = sender.find('tbody');
if (tbody.find('#' + tr_id).length === 0) {
let tr = $('<tr>');
tr.attr('id', tr_id);
let td = $('<td>');
td.html($('#thermal_sensors_widget_progress_bar').html());
tr.append(td);
tbody.append(tr);
}
// probe warning / danger temp
let danger_temp, warning_temp;
if (sensor['type'] === 'core') {
danger_temp = parseInt($('#thermal_sensors_widget_core_critical_threshold').val());
warning_temp = parseInt($('#thermal_sensors_widget_core_warning_threshold').val());
} else {
danger_temp = parseInt($('#thermal_sensors_widget_zone_critical_threshold').val());
warning_temp = parseInt($('#thermal_sensors_widget_zone_warning_threshold').val());
}
// progress bar style
let progressBar = $('#' + tr_id + ' .progress-bar');
const tempIntValue = parseInt(sensor['temperature']);
if (tempIntValue > danger_temp) {
progressBar.removeClass('progress-bar-success')
.removeClass('progress-bar-warning')
.removeClass('progress-bar-danger')
.addClass('progress-bar-danger');
} else if (tempIntValue > warning_temp) {
progressBar.removeClass('progress-bar-success')
.removeClass('progress-bar-warning')
.removeClass('progress-bar-danger')
.addClass('progress-bar-warning');
} else {
progressBar.removeClass('progress-bar-success')
.removeClass('progress-bar-warning')
.removeClass('progress-bar-danger')
.addClass('progress-bar-success');
}
// update bar
progressBar.html(sensor['temperature'] + ' &deg;C');
progressBar.css("width", tempIntValue + "%").attr("aria-valuenow", tempIntValue + "%");
// update label
$('#' + tr_id + ' .info').html(sensor['type_translated'] + ' ' + sensor['device_seq'] + ' <small>(' + sensor['device'] + ')<small>');
});
}
</script>
<div id="thermal_sensors-settings" class="widgetconfigdiv" style="display:none;">
@ -142,6 +172,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
<input type="text" id="thermal_sensors_widget_core_critical_threshold" name="thermal_sensors_widget_core_critical_threshold" value="<?= $pconfig['thermal_sensors_widget_core_critical_threshold']; ?>" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" id="thermal_sensors_widget_show_one_core_temp" name="thermal_sensors_widget_show_one_core_temp" <?=$pconfig['thermal_sensors_widget_show_one_core_temp'] ? 'checked="checked"' : ''; ?>/>
<?= gettext('Show only one CPU core temperature') ?>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" id="thermal_sensors_widget_submit" name="thermal_sensors_widget_submit" class="btn btn-primary formbtn" value="<?= gettext('Save') ?>" />