스캔 캐시를 백그라운드로 갱신

This commit is contained in:
seo
2026-06-27 03:08:28 +09:00
parent 9936ae6985
commit 3be4d8838f
4 changed files with 167 additions and 51 deletions
+146 -46
View File
@@ -2652,6 +2652,66 @@ function wifi_scan_cache_path(): string
return sys_get_temp_dir() . '/control_wifi_scan.json';
}
function scan_refresh_lock_path(string $type): string
{
return sys_get_temp_dir() . '/control_' . preg_replace('/[^a-z0-9_]/i', '', $type) . '_scan_refresh.lock';
}
function scan_start_background_refresh(string $type): void
{
$lockPath = scan_refresh_lock_path($type);
$script = dirname(__DIR__) . '/bin/control_scan_refresh.php';
if (!is_file($script)) {
return;
}
$cmd = '/usr/bin/flock -n '
. escapeshellarg($lockPath)
. ' /usr/bin/php '
. escapeshellarg($script)
. ' '
. escapeshellarg($type)
. ' >/dev/null 2>&1 &';
exec($cmd);
}
function scan_cached_payload(string $cachePath, int $cacheSeconds): ?array
{
if (!is_file($cachePath)) {
return null;
}
$cached = json_decode((string)@file_get_contents($cachePath), true);
if (!is_array($cached)) {
return null;
}
$cached['cached'] = true;
$cached['stale'] = (time() - filemtime($cachePath)) >= $cacheSeconds;
return $cached;
}
function scan_write_cache(string $cachePath, array $payload): void
{
$tmp = $cachePath . '.' . getmypid() . '.tmp';
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if ($json === false) {
return;
}
if (@file_put_contents($tmp, $json) === false) {
return;
}
@chmod($tmp, 0666);
if (!@rename($tmp, $cachePath)) {
@unlink($tmp);
return;
}
@chmod($cachePath, 0666);
}
function wifi_channel_from_freq(int $freq): ?int
{
if ($freq >= 2412 && $freq <= 2472) {
@@ -3031,31 +3091,9 @@ function merge_wifi_scan_networks(array $freshNetworks, array $observedNetworks)
return $rows;
}
function wifi_scan_data(): array
function wifi_scan_refresh_payload(): array
{
if (!(bool)setting_value('wifi.scan_enabled')) {
return [
'enabled' => false,
'cached' => false,
'generated_at' => null,
'interfaces' => [],
'networks' => wifi_scan_observed_networks((int)setting_value('wifi.scan_max_networks')),
'observed_hours' => null,
'observed_mode' => 'all',
'errors' => [],
];
}
$cacheSeconds = (int)setting_value('wifi.scan_cache_seconds');
$cachePath = wifi_scan_cache_path();
if (is_file($cachePath) && (time() - filemtime($cachePath)) < $cacheSeconds) {
$cached = json_decode((string)@file_get_contents($cachePath), true);
if (is_array($cached)) {
$cached['cached'] = true;
return $cached;
}
}
$interfaces = wifi_interfaces_from_iw();
$networks = [];
$errors = [];
@@ -3098,11 +3136,54 @@ function wifi_scan_data(): array
'errors' => $errors,
];
@file_put_contents($cachePath, json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
scan_write_cache($cachePath, $payload);
return $payload;
}
function wifi_scan_data(): array
{
$max = (int)setting_value('wifi.scan_max_networks');
if (!(bool)setting_value('wifi.scan_enabled')) {
return [
'enabled' => false,
'cached' => false,
'stale' => false,
'generated_at' => null,
'interfaces' => [],
'networks' => wifi_scan_observed_networks($max),
'observed_hours' => null,
'observed_mode' => 'all',
'errors' => [],
];
}
$cacheSeconds = (int)setting_value('wifi.scan_cache_seconds');
$cachePath = wifi_scan_cache_path();
$cached = scan_cached_payload($cachePath, $cacheSeconds);
if ($cached !== null && empty($cached['stale'])) {
return $cached;
}
scan_start_background_refresh('wifi');
$observedNetworks = wifi_scan_observed_networks($max);
return [
'enabled' => true,
'cached' => $cached !== null,
'stale' => true,
'refresh_pending' => true,
'generated_at' => is_array($cached) ? (string)($cached['generated_at'] ?? '') : date('Y-m-d H:i:s'),
'interfaces' => is_array($cached) ? (array)($cached['interfaces'] ?? []) : [],
'networks' => $observedNetworks,
'live_found' => is_array($cached) ? (int)($cached['live_found'] ?? 0) : 0,
'total_found' => count($observedNetworks),
'observed_hours' => null,
'observed_mode' => 'all',
'errors' => is_array($cached) ? (array)($cached['errors'] ?? []) : [],
];
}
function bluetooth_scan_cache_path(): string
{
return sys_get_temp_dir() . '/control_bluetooth_scan.json';
@@ -3444,30 +3525,10 @@ function merge_bluetooth_scan_devices(array $freshDevices, array $observedDevice
return $rows;
}
function bluetooth_scan_data(): array
function bluetooth_scan_refresh_payload(): array
{
$max = (int)setting_value('bluetooth.scan_max_devices');
if (!(bool)setting_value('bluetooth.scan_enabled')) {
return [
'enabled' => false,
'cached' => false,
'generated_at' => null,
'devices' => bluetooth_scan_observed_devices($max),
'observed_mode' => 'all',
'errors' => [],
];
}
$cacheSeconds = (int)setting_value('bluetooth.scan_cache_seconds');
$cachePath = bluetooth_scan_cache_path();
if (is_file($cachePath) && (time() - filemtime($cachePath)) < $cacheSeconds) {
$cached = json_decode((string)@file_get_contents($cachePath), true);
if (is_array($cached)) {
$cached['cached'] = true;
return $cached;
}
}
$timeout = (int)setting_value('bluetooth.scan_timeout_seconds');
$includeRaw = (bool)setting_value('bluetooth.scan_include_raw');
$ctl = bluetoothctl_path();
@@ -3512,11 +3573,50 @@ function bluetooth_scan_data(): array
'errors' => $errors,
];
@file_put_contents($cachePath, json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
scan_write_cache($cachePath, $payload);
return $payload;
}
function bluetooth_scan_data(): array
{
$max = (int)setting_value('bluetooth.scan_max_devices');
if (!(bool)setting_value('bluetooth.scan_enabled')) {
return [
'enabled' => false,
'cached' => false,
'stale' => false,
'generated_at' => null,
'devices' => bluetooth_scan_observed_devices($max),
'observed_mode' => 'all',
'errors' => [],
];
}
$cacheSeconds = (int)setting_value('bluetooth.scan_cache_seconds');
$cachePath = bluetooth_scan_cache_path();
$cached = scan_cached_payload($cachePath, $cacheSeconds);
if ($cached !== null && empty($cached['stale'])) {
return $cached;
}
scan_start_background_refresh('bluetooth');
$observedDevices = bluetooth_scan_observed_devices($max);
return [
'enabled' => true,
'cached' => $cached !== null,
'stale' => true,
'refresh_pending' => true,
'generated_at' => is_array($cached) ? (string)($cached['generated_at'] ?? '') : date('Y-m-d H:i:s'),
'devices' => $observedDevices,
'live_found' => is_array($cached) ? (int)($cached['live_found'] ?? 0) : 0,
'total_found' => count($observedDevices),
'observed_mode' => 'all',
'errors' => is_array($cached) ? (array)($cached['errors'] ?? []) : [],
];
}
function parse_live_wifi_rows(string $iface, string $band, string $text, array $leases, array $aliases): array
{
$rows = [];