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));
|
||||
|
||||
Reference in New Issue
Block a user