자동 위치 갱신 5초 명령 복원

This commit is contained in:
seo
2026-07-13 22:15:18 +09:00
parent 5e8b037d99
commit bb070e82c2
3 changed files with 7 additions and 65 deletions
+5 -63
View File
@@ -87,60 +87,6 @@ function findmydevice_mobile_command(string $message, array $data = []): bool
return $ok;
}
function findmydevice_command_state_file(): string
{
return rtrim(sys_get_temp_dir(), '/\\') . '/findmydevice_location_refresh_state.json';
}
function findmydevice_throttled_mobile_command(string $key, int $seconds, string $message, array $data = []): ?bool
{
if ($seconds <= 0) {
return findmydevice_mobile_command($message, $data);
}
$file = findmydevice_command_state_file();
$now = time();
$handle = fopen($file, 'c+');
if (!$handle) {
return findmydevice_mobile_command($message, $data);
}
try {
if (!flock($handle, LOCK_EX)) {
fclose($handle);
return findmydevice_mobile_command($message, $data);
}
$raw = stream_get_contents($handle);
$state = json_decode($raw ?: '{}', true);
if (!is_array($state)) {
$state = [];
}
$last = (int)($state[$key] ?? 0);
if ($last > 0 && ($now - $last) < $seconds) {
flock($handle, LOCK_UN);
fclose($handle);
return null;
}
$state[$key] = $now;
ftruncate($handle, 0);
rewind($handle);
fwrite($handle, json_encode($state, JSON_UNESCAPED_UNICODE));
fflush($handle);
flock($handle, LOCK_UN);
fclose($handle);
} catch (Throwable $e) {
if (is_resource($handle)) {
flock($handle, LOCK_UN);
fclose($handle);
}
}
return findmydevice_mobile_command($message, $data);
}
function findmydevice_ha_profiles(): array
{
$config = custom_config();
@@ -306,9 +252,6 @@ function findmydevice_bool_result_exists(array $results): bool
if ($value === true) {
return true;
}
if ($value === 'throttled') {
return true;
}
}
return false;
}
@@ -318,7 +261,6 @@ function findmydevice_location_refresh(string $name): array
$config = custom_config();
$refresh = $config['findmydevice_location_refresh'] ?? [];
$interval = max(1, (int)($refresh['high_accuracy_interval'] ?? 1));
$autoTickInterval = max(5, (int)($refresh['auto_tick_command_interval'] ?? 30));
$results = [];
if ($name === 'auto_on') {
@@ -364,18 +306,18 @@ function findmydevice_location_refresh(string $name): array
}
$results['policy'] = 'allowed';
$results['allowWebhook'] = false;
$results['quietZone'] = null;
$results['charging'] = $policy['charging'];
$results['requiredBluetoothConnected'] = $policy['required_bluetooth_connected'];
$requestLocation = findmydevice_throttled_mobile_command('auto_tick_request_location', $autoTickInterval, 'request_location_update');
$updateSensors = findmydevice_throttled_mobile_command('auto_tick_update_sensors', $autoTickInterval, 'command_update_sensors');
$results['throttleSeconds'] = $autoTickInterval;
$results['request_location'] = $requestLocation ?? 'throttled';
$results['update_sensors'] = $updateSensors ?? 'throttled';
$results['request_location'] = findmydevice_mobile_command('request_location_update');
$results['update_sensors'] = findmydevice_mobile_command('command_update_sensors');
} elseif ($name === 'force_update') {
$results['allowWebhook'] = false;
$results['request_location'] = findmydevice_mobile_command('request_location_update');
$results['update_sensors'] = findmydevice_mobile_command('command_update_sensors');
} elseif ($name === 'auto_off') {
$results['allowWebhook'] = false;
$results['high_accuracy_off'] = findmydevice_mobile_command('command_high_accuracy_mode', [
'command' => 'force_off',
]);