Replace geofence polling with native Android geofence
This commit is contained in:
40
lib/features/homes/services/geofence_automation_service.dart
Normal file
40
lib/features/homes/services/geofence_automation_service.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user