From 09dc7bf638e775e705824be9bb5ad08dbb53c5a7 Mon Sep 17 00:00:00 2001 From: Clarth Date: Thu, 3 Dec 2026 20:40:16 -0500 Subject: [PATCH] Auto-scan on page load; prune/rotate devices.json --- assets/app.js | 2 +- lib/adb.php | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/assets/app.js b/assets/app.js index 0288b21..b921983 100644 --- a/assets/app.js +++ b/assets/app.js @@ -600,5 +600,5 @@ initCommandPicker(); bindEvents(); - loadStatus(); + loadStatus().then(() => scan()); })(); diff --git a/lib/adb.php b/lib/adb.php index f3877d0..45d7db6 100644 --- a/lib/adb.php +++ b/lib/adb.php @@ -355,23 +355,50 @@ function load_devices(): array return is_array($data) ? $data : []; } +function prune_devices(array $devices, int $maxAgeDays = 60, int $maxEntries = 100): array +{ + $cutoff = time() - ($maxAgeDays * 86400); + $out = []; + foreach ($devices as $d) { + if (!is_array($d)) continue; + $ip = (string) ($d['ip'] ?? ''); + if (!is_allowed_ip($ip)) continue; + $seen = strtotime((string) ($d['last_seen'] ?? '')) ?: 0; + $conn = strtotime((string) ($d['last_connected'] ?? '')) ?: 0; + $latest = max($seen, $conn); + // Keep if recently seen/connected, or if never timestamped but present + if ($latest > 0 && $latest < $cutoff) continue; + $out[] = $d; + } + usort($out, function ($a, $b) { + $aT = max(strtotime((string) ($a['last_seen'] ?? '')) ?: 0, strtotime((string) ($a['last_connected'] ?? '')) ?: 0); + $bT = max(strtotime((string) ($b['last_seen'] ?? '')) ?: 0, strtotime((string) ($b['last_connected'] ?? '')) ?: 0); + return $bT <=> $aT; + }); + if (count($out) > $maxEntries) { + $out = array_slice($out, 0, $maxEntries); + } + return $out; +} + function save_devices(array $devices): bool { + $devices = prune_devices($devices); $path = devices_path(); $dir = dirname($path); if (!is_dir($dir)) { - mkdir($dir, 0775, true); + @mkdir($dir, 0775, true); } $tmp = $path . '.tmp'; $json = json_encode(array_values($devices), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); if ($json === false) { return false; } - $ok = file_put_contents($tmp, $json . "\n", LOCK_EX) !== false; + $ok = @file_put_contents($tmp, $json . "\n", LOCK_EX) !== false; if (!$ok) { return false; } - return rename($tmp, $path); + return @rename($tmp, $path); } function upsert_device(string $ip, array $fields): array