mirror of
https://github.com/opnsense/plugins.git
synced 2026-05-28 04:34:15 -04:00
dec-hw: move to new widget implementation
This commit is contained in:
parent
e3cebacad7
commit
6639e29302
5 changed files with 116 additions and 107 deletions
|
|
@ -35,20 +35,11 @@ class InfoController extends ApiControllerBase
|
|||
{
|
||||
public function powerStatusAction()
|
||||
{
|
||||
$result = [
|
||||
"status" => "failed",
|
||||
"status_translated" => gettext("Power status could not be fetched.
|
||||
This widget is only applicable to Deciso hardware with dual power supplies.")
|
||||
];
|
||||
$result = ["status" => "failed"];
|
||||
$status = parse_ini_string((new Backend())->configdRun('dechw power'));
|
||||
|
||||
if (!empty($status)) {
|
||||
$result["status"] = "OK";
|
||||
unset($result["status_translated"]);
|
||||
|
||||
foreach (['pwr1', 'pwr2'] as $key) {
|
||||
$result[$key . '_translated'] = $status[$key] === '1' ? gettext('On') : gettext('Off');
|
||||
}
|
||||
$result = array_merge($result, $status);
|
||||
}
|
||||
|
||||
|
|
|
|||
100
sysutils/dec-hw/src/opnsense/www/js/widgets/DecHW.js
Normal file
100
sysutils/dec-hw/src/opnsense/www/js/widgets/DecHW.js
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (C) 2024 Deciso B.V.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
import BaseWidget from "./BaseWidget.js";
|
||||
|
||||
export default class DecHW extends BaseWidget {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
getMarkup() {
|
||||
const styles = `
|
||||
#status {
|
||||
margin: 10px;
|
||||
}
|
||||
.power {
|
||||
margin: 5px;
|
||||
float: right;
|
||||
}
|
||||
.power:hover {
|
||||
opacity: 0.5;
|
||||
}
|
||||
.pwr-container {
|
||||
margin: 5px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
.data-item {
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
margin: 5px;
|
||||
width: 50%;
|
||||
display: inline-block;
|
||||
}
|
||||
`;
|
||||
|
||||
const styleSheet = document.createElement("style");
|
||||
styleSheet.innerText = styles;
|
||||
document.head.appendChild(styleSheet);
|
||||
|
||||
return $(`
|
||||
<div id="status"></div>
|
||||
<div class="pwr-container">
|
||||
<div id="pwr1" class="data-item">
|
||||
<strong>${this.translations.powersupply} 1</strong>
|
||||
</div>
|
||||
<div id="pwr2" class="data-item">
|
||||
<strong>${this.translations.powersupply} 2</strong>
|
||||
</div>
|
||||
</div>
|
||||
`);
|
||||
}
|
||||
|
||||
async onWidgetTick() {
|
||||
$('.power').tooltip('hide');
|
||||
let data = await this.ajaxCall('/api/dechw/info/powerStatus');
|
||||
|
||||
if (!data || data.status === 'failed') {
|
||||
$('#status').html(`<div class="error-message" style="margin: 10px;">${this.translations.nopower}</div>`);
|
||||
$('.pwr-container').hide();
|
||||
return;
|
||||
}
|
||||
|
||||
$('.power').remove();
|
||||
['pwr1', 'pwr2'].forEach((key) => {
|
||||
let status = data[key];
|
||||
|
||||
let $power = $(`<span class="power fa fa-power-off fa-lg" data-toggle="tooltip" title=""></span>`);
|
||||
$power.css('color', status === '1' ? 'blue' : 'red');
|
||||
$power.attr('title', status === '1' ? this.translations.poweron : this.translations.poweroff);
|
||||
$(`#${key}`).append($power);
|
||||
});
|
||||
|
||||
$('.power').tooltip({container: 'body'});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<metadata>
|
||||
<dechw>
|
||||
<filename>DecHW.js</filename>
|
||||
<endpoints>
|
||||
<endpoint>/api/dechw/info/powerStatus</endpoint>
|
||||
</endpoints>
|
||||
<translations>
|
||||
<title>Deciso Hardware Information</title>
|
||||
<nopower>Power status could not be fetched. This widget is only applicable to Deciso hardware with dual power supplies.</nopower>
|
||||
<poweron>Power is on</poweron>
|
||||
<poweroff>Power is off</poweroff>
|
||||
<powersupply>Power Supply</powersupply>
|
||||
</translations>
|
||||
</dechw>
|
||||
</metadata>
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
<?php
|
||||
|
||||
$dechw_title = gettext('Deciso Hardware Information');
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Copyright (C) 2023 Deciso B.V.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
?>
|
||||
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
ajaxGet('/api/dechw/info/powerStatus', {}, function(data, status) {
|
||||
if (data['status'] === 'failed') {
|
||||
$('#status').html(data['status_translated']);
|
||||
$('.pwr-container').hide();
|
||||
return;
|
||||
}
|
||||
|
||||
['pwr1', 'pwr2'].forEach(function(key) {
|
||||
let status = data[key];
|
||||
let status_translated = data[key + '_translated'];
|
||||
let $power = $('<span data-toggle="tooltip" title=""></span>').addClass('power fa fa-power-off fa-lg');
|
||||
$power.css('color', status === '1' ? 'blue' : 'red');
|
||||
$power.attr('title', status_translated);
|
||||
$('#'+key).append($power);
|
||||
})
|
||||
|
||||
$(".circle").tooltip({container: 'body', trigger: 'hover'});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
#status {
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
.power {
|
||||
margin: 5px;
|
||||
float: right;
|
||||
}
|
||||
|
||||
.power:hover {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.pwr-container {
|
||||
margin: 5px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.data-item {
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
margin: 5px;
|
||||
width: 50%;
|
||||
display: inline-block;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div id="status"></div>
|
||||
<div class="pwr-container">
|
||||
<div id="pwr1" class="data-item">
|
||||
<strong><?=gettext("Power Supply 1");?></strong>
|
||||
</div>
|
||||
<div id="pwr2" class="data-item">
|
||||
<strong><?=gettext("Power Supply 2");?></strong>
|
||||
</div>
|
||||
</div>
|
||||
Loading…
Reference in a new issue