Szinkronizáció javítása,

This commit is contained in:
2026-07-12 02:14:49 +02:00
parent c73adaf9a1
commit 4bf73c6cc7
3 changed files with 86 additions and 20 deletions
+33 -4
View File
@@ -13,6 +13,10 @@ class PhoneGpsConnection implements GnssConnection {
final _positionController = StreamController<Position>.broadcast();
StreamSubscription<Position>? _positionSub;
int _retryCount = 0;
static const _maxRetries = 5;
Timer? _retryTimer;
@override
Stream<String> get nmeaLines => const Stream.empty(); // Nincs NMEA
@@ -42,20 +46,44 @@ class PhoneGpsConnection implements GnssConnection {
}
_stateController.add(GnssConnectionState.connected);
_retryCount = 0;
await _startPositionStream();
}
// Belső GPS folyamatos olvasása
Future<void> _startPositionStream() async {
await _positionSub?.cancel();
_positionSub = Geolocator.getPositionStream(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 0, // Folyamatos frissítés
),
).listen((Position pos) {
_positionController.add(pos);
});
).listen(
(Position pos) {
_retryCount = 0;
_positionController.add(pos);
},
onError: _handleStreamError,
onDone: () =>
_handleStreamError(Exception('A GPS-stream váratlanul lezárult.')),
);
}
/// A Geolocator stream NEM öngyógyuló — lásd a PhoneGpsSource-nál már
/// javított, ugyanilyen hibát. Ez a kapcsolat idáig SEMMILYEN hiba
/// esetén nem próbálkozott újra, csendben, véglegesen elhallgatott.
void _handleStreamError(Object e) {
_retryCount++;
if (_retryCount > _maxRetries) {
_stateController.add(GnssConnectionState.error);
return;
}
_retryTimer?.cancel();
_retryTimer = Timer(const Duration(seconds: 1), _startPositionStream);
}
@override
Future<void> disconnect() async {
_retryTimer?.cancel();
await _positionSub?.cancel();
_stateController.add(GnssConnectionState.disconnected);
}
@@ -67,6 +95,7 @@ class PhoneGpsConnection implements GnssConnection {
@override
void dispose() {
_retryTimer?.cancel();
_positionSub?.cancel();
_positionController.close();
_stateController.close();