Add low voltage status to control dashboard
This commit is contained in:
+102
@@ -117,6 +117,107 @@ function dmesg_log(): array
|
||||
];
|
||||
}
|
||||
|
||||
function low_voltage_status(): array
|
||||
{
|
||||
$result = sh(['/usr/bin/vcgencmd', 'get_throttled'], false, 3);
|
||||
$raw = trim((string)$result['out']);
|
||||
$flags = null;
|
||||
|
||||
if ((int)$result['code'] === 0 && preg_match('/throttled=0x([0-9a-f]+)/i', $raw, $m)) {
|
||||
$flags = hexdec($m[1]);
|
||||
}
|
||||
|
||||
$now = time();
|
||||
$active = $flags !== null && (bool)($flags & 0x1);
|
||||
$seen = $flags !== null && (bool)($flags & 0x10000);
|
||||
$freqCapped = $flags !== null && (bool)($flags & 0x2);
|
||||
$throttled = $flags !== null && (bool)($flags & 0x4);
|
||||
$softTempLimit = $flags !== null && (bool)($flags & 0x8);
|
||||
$statePath = '/tmp/control-low-voltage-state.json';
|
||||
if (is_file($statePath) && !is_writable($statePath)) {
|
||||
@chmod($statePath, 0666);
|
||||
}
|
||||
|
||||
$state = [
|
||||
'active' => false,
|
||||
'active_since' => null,
|
||||
'last_detected_at' => null,
|
||||
'last_cleared_at' => null,
|
||||
'last_duration_seconds' => 0,
|
||||
'updated_at' => null,
|
||||
];
|
||||
|
||||
$fp = @fopen($statePath, 'c+');
|
||||
if ($fp !== false) {
|
||||
@chmod($statePath, 0666);
|
||||
@flock($fp, LOCK_EX);
|
||||
$saved = stream_get_contents($fp);
|
||||
$decoded = json_decode((string)$saved, true);
|
||||
if (is_array($decoded)) {
|
||||
$state = array_merge($state, array_intersect_key($decoded, $state));
|
||||
}
|
||||
|
||||
$wasActive = !empty($state['active']);
|
||||
$activeSince = is_numeric($state['active_since'] ?? null) ? (int)$state['active_since'] : null;
|
||||
|
||||
if ($flags !== null) {
|
||||
if ($active) {
|
||||
if (!$wasActive || $activeSince === null) {
|
||||
$activeSince = $now;
|
||||
$state['last_detected_at'] = $now;
|
||||
}
|
||||
$state['active'] = true;
|
||||
$state['active_since'] = $activeSince;
|
||||
} else {
|
||||
if ($wasActive && $activeSince !== null) {
|
||||
$state['last_cleared_at'] = $now;
|
||||
$state['last_duration_seconds'] = max(0, $now - $activeSince);
|
||||
}
|
||||
$state['active'] = false;
|
||||
$state['active_since'] = null;
|
||||
}
|
||||
|
||||
$state['updated_at'] = $now;
|
||||
ftruncate($fp, 0);
|
||||
rewind($fp);
|
||||
fwrite($fp, json_encode($state, JSON_UNESCAPED_SLASHES));
|
||||
fflush($fp);
|
||||
@chmod($statePath, 0666);
|
||||
}
|
||||
|
||||
@flock($fp, LOCK_UN);
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
$duration = 0;
|
||||
if ($active && is_numeric($state['active_since'] ?? null)) {
|
||||
$duration = max(0, $now - (int)$state['active_since']);
|
||||
} elseif (is_numeric($state['last_duration_seconds'] ?? null)) {
|
||||
$duration = max(0, (int)$state['last_duration_seconds']);
|
||||
}
|
||||
|
||||
$dateValue = static fn($ts): ?string => is_numeric($ts) && (int)$ts > 0
|
||||
? date('Y-m-d H:i:s', (int)$ts)
|
||||
: null;
|
||||
|
||||
return [
|
||||
'available' => $flags !== null,
|
||||
'raw' => $raw,
|
||||
'flags' => $flags,
|
||||
'active' => $active,
|
||||
'seen_since_boot' => $seen,
|
||||
'freq_capped' => $freqCapped,
|
||||
'throttled' => $throttled,
|
||||
'soft_temp_limit' => $softTempLimit,
|
||||
'active_since' => $dateValue($state['active_since'] ?? null),
|
||||
'last_detected_at' => $dateValue($state['last_detected_at'] ?? null),
|
||||
'last_cleared_at' => $dateValue($state['last_cleared_at'] ?? null),
|
||||
'duration_seconds' => $duration,
|
||||
'last_duration_seconds' => (int)($state['last_duration_seconds'] ?? 0),
|
||||
'updated_at' => $dateValue($state['updated_at'] ?? null),
|
||||
];
|
||||
}
|
||||
|
||||
function fan_paths(): array
|
||||
{
|
||||
$candidates = glob('/sys/devices/platform/cooling_fan/hwmon/hwmon*/pwm1') ?: [];
|
||||
@@ -1748,6 +1849,7 @@ function collect_snapshot(bool $applyFan = true): array
|
||||
'active_users' => active_user_info(),
|
||||
'memory' => $mem,
|
||||
'disk' => $disk,
|
||||
'low_voltage' => low_voltage_status(),
|
||||
]);
|
||||
|
||||
$history = add_battery_remaining_history(sensor_history(240));
|
||||
|
||||
@@ -21,6 +21,9 @@
|
||||
statusDisk: $('#statusDisk'),
|
||||
statusMemory: $('#statusMemory'),
|
||||
statusUptime: $('#statusUptime'),
|
||||
statusLowVoltage: $('#statusLowVoltage'),
|
||||
statusLowVoltageLast: $('#statusLowVoltageLast'),
|
||||
statusLowVoltageDuration: $('#statusLowVoltageDuration'),
|
||||
statusBatteryVoltage: $('#statusBatteryVoltage'),
|
||||
statusBatterySoc: $('#statusBatterySoc'),
|
||||
statusBatteryRemaining: $('#statusBatteryRemaining'),
|
||||
@@ -88,6 +91,15 @@
|
||||
diskRoot: 'Disk',
|
||||
memory: 'Memory',
|
||||
uptime: 'Uptime',
|
||||
lowVoltage: 'Low Voltage',
|
||||
lowVoltageLast: 'Last Low Voltage',
|
||||
lowVoltageDuration: 'LV Duration',
|
||||
lowVoltageActive: 'Active',
|
||||
lowVoltageNormal: 'Normal',
|
||||
lowVoltageSeen: 'seen since boot',
|
||||
lowVoltageNotSeen: 'not seen since boot',
|
||||
currentEpisode: 'current',
|
||||
lastEpisode: 'last',
|
||||
batteryV: 'Battery V',
|
||||
batterySoc: 'Battery SOC',
|
||||
sensorHistory: 'Sensor History',
|
||||
@@ -221,6 +233,15 @@
|
||||
diskRoot: '디스크',
|
||||
memory: '메모리',
|
||||
uptime: '가동 시간',
|
||||
lowVoltage: '저전압',
|
||||
lowVoltageLast: '최근 저전압',
|
||||
lowVoltageDuration: '저전압 지속',
|
||||
lowVoltageActive: '감지 중',
|
||||
lowVoltageNormal: '정상',
|
||||
lowVoltageSeen: '부팅 후 이력 있음',
|
||||
lowVoltageNotSeen: '부팅 후 이력 없음',
|
||||
currentEpisode: '현재',
|
||||
lastEpisode: '최근',
|
||||
batteryV: '배터리 전압',
|
||||
batterySoc: '배터리 SOC',
|
||||
sensorHistory: '센서 이력',
|
||||
@@ -756,6 +777,27 @@
|
||||
return formatDhms(parseWifiDurationSeconds(value)) || value;
|
||||
}
|
||||
|
||||
function lowVoltageStateText(lowVoltage) {
|
||||
if (!lowVoltage || lowVoltage.available === false) {
|
||||
return t('na');
|
||||
}
|
||||
|
||||
const stateText = lowVoltage.active ? t('lowVoltageActive') : t('lowVoltageNormal');
|
||||
const seenText = lowVoltage.seen_since_boot ? t('lowVoltageSeen') : t('lowVoltageNotSeen');
|
||||
return `${stateText} (${seenText})`;
|
||||
}
|
||||
|
||||
function lowVoltageDurationText(lowVoltage) {
|
||||
if (!lowVoltage || lowVoltage.available === false) {
|
||||
return t('na');
|
||||
}
|
||||
|
||||
const seconds = Number(lowVoltage.duration_seconds ?? 0);
|
||||
const label = lowVoltage.active ? t('currentEpisode') : t('lastEpisode');
|
||||
const formatted = formatDhms(seconds) || '-';
|
||||
return `${label}: ${formatted}`;
|
||||
}
|
||||
|
||||
function timeAgo(seconds) {
|
||||
const formatted = formatDhms(seconds);
|
||||
return formatted ? `${formatted} ${t('ago')}` : '-';
|
||||
@@ -786,6 +828,7 @@
|
||||
const disk = system.disk || {};
|
||||
const memory = system.memory || {};
|
||||
const battery = data.battery || {};
|
||||
const lowVoltage = system.low_voltage || {};
|
||||
const load = Array.isArray(system.load) ? system.load : [];
|
||||
const activeUsers = system.active_users || {};
|
||||
|
||||
@@ -795,6 +838,9 @@
|
||||
setText(els.statusDisk, `${Number(disk.used_kb || 0).toLocaleString()} / ${Number(disk.total_kb || 0).toLocaleString()} KB (${disk.percent ?? '-'}%)`);
|
||||
setText(els.statusMemory, `${memory.used_mb ?? '-'} / ${memory.total_mb ?? '-'} MB (${memory.percent ?? '-'}%)`);
|
||||
setText(els.statusUptime, system.uptime || '-');
|
||||
setText(els.statusLowVoltage, lowVoltageStateText(lowVoltage));
|
||||
setText(els.statusLowVoltageLast, lowVoltage.last_detected_at || '-');
|
||||
setText(els.statusLowVoltageDuration, lowVoltageDurationText(lowVoltage));
|
||||
setText(els.statusBatteryVoltage, battery.voltage === null || battery.voltage === undefined ? '-' : `${Number(battery.voltage).toFixed(3)} V`);
|
||||
setText(els.statusBatterySoc, battery.percent === null || battery.percent === undefined ? '-' : `${Number(battery.percent).toFixed(2)}%`);
|
||||
setText(els.statusBatteryRemaining, battery.remaining?.display || '-');
|
||||
|
||||
@@ -200,6 +200,9 @@ html[data-theme="light"] .btn.secondary{background:#d9e2ef;color:#172033}html[da
|
||||
<div class="status-row"><div class="status-key" data-i18n="diskRoot">Disk /</div><div class="status-value" id="statusDisk">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="memory">Memory</div><div class="status-value" id="statusMemory">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="uptime">Uptime</div><div class="status-value" id="statusUptime">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="lowVoltage">Low Voltage</div><div class="status-value" id="statusLowVoltage">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="lowVoltageLast">Last Low Voltage</div><div class="status-value" id="statusLowVoltageLast">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="lowVoltageDuration">LV Duration</div><div class="status-value" id="statusLowVoltageDuration">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="batteryV">Battery V</div><div class="status-value" id="statusBatteryVoltage">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="batterySoc">Battery SOC</div><div class="status-value" id="statusBatterySoc">-</div></div>
|
||||
<div class="status-row"><div class="status-key" data-i18n="remaining">Remaining</div><div class="status-value" id="statusBatteryRemaining">-</div></div>
|
||||
|
||||
Reference in New Issue
Block a user