GNSS szervíz refraktorálás, hibajavítás, PhoneGpsConnection osztály
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'gnss_connection.dart';
|
||||
import 'gnss_device_service.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
class BtSerialGnssConnection implements GnssConnection {
|
||||
@override
|
||||
@@ -11,6 +14,7 @@ class BtSerialGnssConnection implements GnssConnection {
|
||||
|
||||
BluetoothConnection? _connection;
|
||||
String _messageBuffer = '';
|
||||
String _lineBuffer = '';
|
||||
|
||||
final _nmeaController = StreamController<String>.broadcast();
|
||||
final _stateController = StreamController<GnssConnectionState>.broadcast();
|
||||
@@ -48,41 +52,24 @@ class BtSerialGnssConnection implements GnssConnection {
|
||||
|
||||
// A meglévő _onDataReceived logika változatlanul
|
||||
void _onData(Uint8List data) {
|
||||
int backspacesCounter = 0;
|
||||
for (var byte in data) {
|
||||
if (byte == 8 || byte == 127) backspacesCounter++;
|
||||
}
|
||||
_lineBuffer += utf8.decode(data, allowMalformed: true);
|
||||
|
||||
Uint8List buffer = Uint8List(data.length - backspacesCounter);
|
||||
int bufferIndex = buffer.length;
|
||||
backspacesCounter = 0;
|
||||
final lines = const LineSplitter().convert(_lineBuffer);
|
||||
|
||||
for (int i = data.length - 1; i >= 0; i--) {
|
||||
if (data[i] == 8 || data[i] == 127) {
|
||||
backspacesCounter++;
|
||||
} else if (backspacesCounter > 0) {
|
||||
backspacesCounter--;
|
||||
} else {
|
||||
buffer[--bufferIndex] = data[i];
|
||||
for (int i = 0; i < lines.length - 1; i++) {
|
||||
final sentence = lines[i].trim();
|
||||
if (sentence.isNotEmpty) {
|
||||
_nmeaController.add(sentence);
|
||||
}
|
||||
}
|
||||
|
||||
final dataString = String.fromCharCodes(buffer);
|
||||
final index = buffer.indexOf(13); // \r
|
||||
|
||||
String sentence;
|
||||
if (~index != 0) {
|
||||
sentence = _messageBuffer + dataString.substring(0, index);
|
||||
_messageBuffer = dataString.substring(index);
|
||||
if (_lineBuffer.endsWith('\n')) {
|
||||
if (lines.isNotEmpty && lines.last.trim().isNotEmpty) {
|
||||
_nmeaController.add(lines.last.trim());
|
||||
}
|
||||
_lineBuffer = '';
|
||||
} else {
|
||||
_messageBuffer += dataString;
|
||||
return;
|
||||
}
|
||||
|
||||
// Soronként kibocsátjuk
|
||||
for (final line in sentence.split('\n')) {
|
||||
final trimmed = line.trim();
|
||||
if (trimmed.isNotEmpty) _nmeaController.add(trimmed);
|
||||
_lineBuffer = lines.isNotEmpty ? lines.last : _lineBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,4 +83,7 @@ class BtSerialGnssConnection implements GnssConnection {
|
||||
void sendData(Uint8List data) {
|
||||
_connection?.output.add(data);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<Position> get positionStream => const Stream.empty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user