Auto-scan on page load; prune/rotate devices.json
This commit is contained in:
parent
09c23f54d0
commit
09dc7bf638
2 changed files with 31 additions and 4 deletions
|
|
@ -600,5 +600,5 @@
|
||||||
|
|
||||||
initCommandPicker();
|
initCommandPicker();
|
||||||
bindEvents();
|
bindEvents();
|
||||||
loadStatus();
|
loadStatus().then(() => scan());
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
33
lib/adb.php
33
lib/adb.php
|
|
@ -355,23 +355,50 @@ function load_devices(): array
|
||||||
return is_array($data) ? $data : [];
|
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
|
function save_devices(array $devices): bool
|
||||||
{
|
{
|
||||||
|
$devices = prune_devices($devices);
|
||||||
$path = devices_path();
|
$path = devices_path();
|
||||||
$dir = dirname($path);
|
$dir = dirname($path);
|
||||||
if (!is_dir($dir)) {
|
if (!is_dir($dir)) {
|
||||||
mkdir($dir, 0775, true);
|
@mkdir($dir, 0775, true);
|
||||||
}
|
}
|
||||||
$tmp = $path . '.tmp';
|
$tmp = $path . '.tmp';
|
||||||
$json = json_encode(array_values($devices), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
$json = json_encode(array_values($devices), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||||
if ($json === false) {
|
if ($json === false) {
|
||||||
return 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) {
|
if (!$ok) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return rename($tmp, $path);
|
return @rename($tmp, $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
function upsert_device(string $ip, array $fields): array
|
function upsert_device(string $ip, array $fields): array
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue