GNSS szervíz refraktorálás, hibajavítás, PhoneGpsConnection osztály
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:nmea/nmea.dart';
|
||||
import 'package:terepi_seged/services/gnss/gnss_device_service.dart';
|
||||
import 'package:terepi_seged/services/gnss/phone_gps_connection.dart';
|
||||
|
||||
import '../../gnss_sentences/gngga.dart';
|
||||
import '../../gnss_sentences/gngst.dart';
|
||||
@@ -50,9 +52,13 @@ class GnssService extends GetxService {
|
||||
final NmeaDecoder _decoder = NmeaDecoder();
|
||||
StreamSubscription? _nmeaSub;
|
||||
StreamSubscription? _stateSub;
|
||||
StreamSubscription<Position>? _positionSub;
|
||||
|
||||
String _utcTime = '';
|
||||
String _utcDate = '';
|
||||
|
||||
bool _intentionalDisconnection = false;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
@@ -86,16 +92,28 @@ class GnssService extends GetxService {
|
||||
}
|
||||
|
||||
/// Eszközváltás — GnssDevicePicker hívja.
|
||||
Future<void> onDeviceChanged(GnssDevice device) async {
|
||||
Future<void> onDeviceChanged(GnssDevice? device) async {
|
||||
if (device == null) {
|
||||
await _disconnect();
|
||||
activeConnectionType.value = GnssConnectionType.none;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (device.type) {
|
||||
case GnssConnectionType.none:
|
||||
await _disconnect();
|
||||
activeConnectionType.value = GnssConnectionType.none;
|
||||
break;
|
||||
case GnssConnectionType.btSerial:
|
||||
await connectBtSerial(device.address);
|
||||
case GnssConnectionType.ble:
|
||||
await connectBle(device.address);
|
||||
case GnssConnectionType.phoneGps:
|
||||
await _disconnect();
|
||||
connectionState.value = GnssConnectionState.disconnected;
|
||||
_connection = PhoneGpsConnection();
|
||||
//connectionState.value = GnssConnectionState.disconnected;
|
||||
activeConnectionType.value = GnssConnectionType.phoneGps;
|
||||
await _doConnect('iternal');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,13 +127,40 @@ class GnssService extends GetxService {
|
||||
Future<void> _doConnect(String address) async {
|
||||
_stateSub = _connection!.connectionState.listen((s) {
|
||||
connectionState.value = s;
|
||||
if (s == GnssConnectionState.disconnected &&
|
||||
_intentionalDisconnection == false) {
|
||||
Future.delayed(const Duration(seconds: 3), () => reconnect());
|
||||
}
|
||||
});
|
||||
|
||||
_nmeaSub = _connection!.nmeaLines.listen(_parseNmea);
|
||||
_positionSub = _connection!.positionStream.listen(_parseDirectPosition);
|
||||
|
||||
_intentionalDisconnection = false;
|
||||
await _connection!.connect(address);
|
||||
}
|
||||
|
||||
void _parseDirectPosition(Position pos) {
|
||||
latitude.value = pos.latitude;
|
||||
longitude.value = pos.longitude;
|
||||
altitude.value = pos.altitude;
|
||||
|
||||
gpsQuality.value = 1; // 1 = Standard (nem-RTK) minőség
|
||||
satelliteCount.value = 0; // A Geolocator nem ad műholdszámot direktben
|
||||
|
||||
// A Geolocator a pontosságot (accuracy) méterben adja vissza
|
||||
latitudeError.value = pos.accuracy;
|
||||
longitudeError.value = pos.accuracy;
|
||||
altitudeError.value = pos.altitudeAccuracy ?? 0.0;
|
||||
|
||||
// Az RMC (idő) adatait is beállítjuk a telefon idejéből
|
||||
gpsDateTime.value = pos.timestamp;
|
||||
}
|
||||
|
||||
Future<void> _disconnect() async {
|
||||
_intentionalDisconnection = true;
|
||||
await _nmeaSub?.cancel();
|
||||
await _positionSub?.cancel();
|
||||
await _stateSub?.cancel();
|
||||
await _connection?.disconnect();
|
||||
_connection?.dispose();
|
||||
@@ -129,7 +174,7 @@ class GnssService extends GetxService {
|
||||
void sendToReceiver(Uint8List data) {
|
||||
if (_connection == null) return;
|
||||
if (connectionState.value != GnssConnectionState.connected) return;
|
||||
(_connection as BtSerialGnssConnection?)?.sendData(data);
|
||||
_connection?.sendData(data);
|
||||
}
|
||||
|
||||
// ── NMEA parsing ──────────────────────────────────────────────────
|
||||
@@ -196,4 +241,36 @@ class GnssService extends GetxService {
|
||||
_disconnect();
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
Future<void> determineInitialPosition() async {
|
||||
// 1. Ha már van élő adatunk (pl. a külső vevő már küldött koordinátát),
|
||||
// akkor nincs szükség extra lekérdezésre.
|
||||
if (latitude.value != 0 && longitude.value != 0) return;
|
||||
|
||||
try {
|
||||
// 2. Gyors lekérdezés: megkérdezzük a telefont, hol voltunk utoljára.
|
||||
// Ez szinte azonnal visszatér, nem pörgeti fel a GPS chipet.
|
||||
final lastPosition = await Geolocator.getLastKnownPosition();
|
||||
|
||||
if (lastPosition != null) {
|
||||
latitude.value = lastPosition.latitude;
|
||||
longitude.value = lastPosition.longitude;
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Ha nincs utolsó ismert pozíció (pl. friss telepítés),
|
||||
// kérünk egy friss pozíciót, de alacsony pontossággal, hogy gyors legyen.
|
||||
final currentPosition = await Geolocator.getCurrentPosition(
|
||||
desiredAccuracy: LocationAccuracy.low,
|
||||
timeLimit: const Duration(seconds: 3), // Ne akassza meg az appot sokáig
|
||||
);
|
||||
|
||||
latitude.value = currentPosition.latitude;
|
||||
longitude.value = currentPosition.longitude;
|
||||
} catch (e) {
|
||||
// Engedélyhiány vagy kikapcsolt helymeghatározás esetén a térkép
|
||||
// marad a (0,0)-n vagy egy alapértelmezett (pl. budapesti) koordinátán.
|
||||
print('Nem sikerült lekérni a kezdőpozíciót: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user