자동 위치 갱신 명령 반복 제한
This commit is contained in:
+64
-2
@@ -87,6 +87,60 @@ 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();
|
||||
@@ -252,6 +306,9 @@ function findmydevice_bool_result_exists(array $results): bool
|
||||
if ($value === true) {
|
||||
return true;
|
||||
}
|
||||
if ($value === 'throttled') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -261,6 +318,7 @@ 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') {
|
||||
@@ -278,6 +336,7 @@ 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'];
|
||||
@@ -308,8 +367,11 @@ function findmydevice_location_refresh(string $name): array
|
||||
$results['quietZone'] = null;
|
||||
$results['charging'] = $policy['charging'];
|
||||
$results['requiredBluetoothConnected'] = $policy['required_bluetooth_connected'];
|
||||
$results['request_location'] = findmydevice_mobile_command('request_location_update');
|
||||
$results['update_sensors'] = findmydevice_mobile_command('command_update_sensors');
|
||||
$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';
|
||||
} elseif ($name === 'force_update') {
|
||||
$results['request_location'] = findmydevice_mobile_command('request_location_update');
|
||||
$results['update_sensors'] = findmydevice_mobile_command('command_update_sensors');
|
||||
|
||||
Reference in New Issue
Block a user