41 lines
1.2 KiB
Dart
41 lines
1.2 KiB
Dart
import 'package:flutter/services.dart';
|
||
|
||
import '../../../models/home_config.dart';
|
||
import '../../../services/settings_service.dart';
|
||
|
||
class GeofenceAutomationService {
|
||
GeofenceAutomationService({SettingsService? settingsService})
|
||
: _settingsService = settingsService ?? SettingsService();
|
||
|
||
static const _channel = MethodChannel('ignis/geofence_automation');
|
||
|
||
final SettingsService _settingsService;
|
||
|
||
Future<void> syncActiveHome(HomeConfig? home) async {
|
||
if (home == null || !home.geofenceReady) {
|
||
await _invoke('disarmGeofence');
|
||
return;
|
||
}
|
||
|
||
final apiKey = await _settingsService.requireHomeApiKey(home.id);
|
||
await _invoke('armGeofence', {
|
||
'homeId': home.id,
|
||
'baseUrl': home.url,
|
||
'apiKey': apiKey,
|
||
'latitude': home.latitude,
|
||
'longitude': home.longitude,
|
||
'radiusMeters': home.geofenceRadiusMeters,
|
||
});
|
||
}
|
||
|
||
Future<void> _invoke(String method, [Map<String, Object?>? arguments]) async {
|
||
try {
|
||
await _channel.invokeMethod<void>(method, arguments);
|
||
} on MissingPluginException {
|
||
// В тестах и на не-Android средах platform channel может отсутствовать.
|
||
} on PlatformException {
|
||
rethrow;
|
||
}
|
||
}
|
||
}
|