Request frequent mobile location updates
This commit is contained in:
+62
-2
@@ -21,6 +21,58 @@ if (function_exists('set_time_limit')) {
|
||||
custom_require_auth();
|
||||
|
||||
$requestData = custom_read_request_data();
|
||||
|
||||
function findmydevice_mobile_command(string $message, array $data = []): bool
|
||||
{
|
||||
$config = custom_config();
|
||||
$refresh = $config['findmydevice_location_refresh'] ?? [];
|
||||
$profile = (string)($refresh['profile'] ?? 'seoul');
|
||||
$service = (string)($refresh['notify_service'] ?? '');
|
||||
|
||||
if ($service === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$payload = ['message' => $message];
|
||||
if ($data) {
|
||||
$payload['data'] = $data;
|
||||
}
|
||||
|
||||
return custom_ha_service($profile, 'notify', $service, $payload, 5);
|
||||
}
|
||||
|
||||
function findmydevice_location_refresh(string $name): array
|
||||
{
|
||||
$config = custom_config();
|
||||
$refresh = $config['findmydevice_location_refresh'] ?? [];
|
||||
$interval = max(5, (int)($refresh['high_accuracy_interval'] ?? 5));
|
||||
$results = [];
|
||||
|
||||
if ($name === 'auto_on') {
|
||||
$results['high_accuracy_interval'] = findmydevice_mobile_command('command_high_accuracy_mode', [
|
||||
'command' => 'high_accuracy_set_update_interval',
|
||||
'high_accuracy_update_interval' => $interval,
|
||||
]);
|
||||
$results['high_accuracy_on'] = findmydevice_mobile_command('command_high_accuracy_mode', [
|
||||
'command' => 'force_on',
|
||||
]);
|
||||
$results['request_location'] = findmydevice_mobile_command('request_location_update');
|
||||
$results['update_sensors'] = findmydevice_mobile_command('command_update_sensors');
|
||||
} elseif ($name === 'auto_tick') {
|
||||
$results['request_location'] = findmydevice_mobile_command('request_location_update');
|
||||
$results['update_sensors'] = findmydevice_mobile_command('command_update_sensors');
|
||||
} elseif ($name === 'force_update') {
|
||||
$results['request_location'] = findmydevice_mobile_command('request_location_update');
|
||||
$results['update_sensors'] = findmydevice_mobile_command('command_update_sensors');
|
||||
} elseif ($name === 'auto_off') {
|
||||
$results['high_accuracy_off'] = findmydevice_mobile_command('command_high_accuracy_mode', [
|
||||
'command' => 'force_off',
|
||||
]);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
if (isset($_GET['action']) && $_GET['action'] === 'webhook') {
|
||||
$name = (string)($requestData['name'] ?? '');
|
||||
$allowed = [
|
||||
@@ -34,8 +86,16 @@ if (isset($_GET['action']) && $_GET['action'] === 'webhook') {
|
||||
custom_json(['result' => 'invalid_webhook'], 400);
|
||||
}
|
||||
|
||||
$ok = custom_ha_webhook($allowed[$name]);
|
||||
custom_json(['result' => $ok ? 'ok' : 'fail'], $ok ? 200 : 502);
|
||||
$mobileResults = findmydevice_location_refresh($name);
|
||||
$webhookOk = custom_ha_webhook($allowed[$name]);
|
||||
$mobileOk = !$mobileResults || in_array(true, $mobileResults, true);
|
||||
$ok = $webhookOk || $mobileOk;
|
||||
|
||||
custom_json([
|
||||
'result' => $ok ? 'ok' : 'fail',
|
||||
'webhook' => $webhookOk ? 'ok' : 'fail',
|
||||
'mobileCommands' => $mobileResults,
|
||||
], $ok ? 200 : 502);
|
||||
}
|
||||
|
||||
$config = custom_config();
|
||||
|
||||
Reference in New Issue
Block a user