GNSS eszközhöz való kapcsolódás és bluetooth kommunikáció refraktorálása külön osztályokba
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
// lib/services/gnss/bt_serial_gnss_connection.dart
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
||||
import 'gnss_connection.dart';
|
||||
|
||||
class BtSerialGnssConnection implements GnssConnection {
|
||||
@override
|
||||
GnssConnectionType get type => GnssConnectionType.btSerial;
|
||||
|
||||
BluetoothConnection? _connection;
|
||||
String _messageBuffer = '';
|
||||
|
||||
final _nmeaController = StreamController<String>.broadcast();
|
||||
final _stateController = StreamController<GnssConnectionState>.broadcast();
|
||||
|
||||
@override
|
||||
Stream<String> get nmeaLines => _nmeaController.stream;
|
||||
|
||||
@override
|
||||
Stream<GnssConnectionState> get connectionState => _stateController.stream;
|
||||
|
||||
@override
|
||||
Future<void> connect(String address) async {
|
||||
_stateController.add(GnssConnectionState.connecting);
|
||||
try {
|
||||
_connection = await BluetoothConnection.toAddress(address);
|
||||
_stateController.add(GnssConnectionState.connected);
|
||||
_connection!.input!.listen(
|
||||
_onData,
|
||||
onDone: () {
|
||||
_stateController.add(GnssConnectionState.disconnected);
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
_stateController.add(GnssConnectionState.error);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> disconnect() async {
|
||||
await _connection?.close();
|
||||
_connection = null;
|
||||
_stateController.add(GnssConnectionState.disconnected);
|
||||
}
|
||||
|
||||
// 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++;
|
||||
}
|
||||
|
||||
Uint8List buffer = Uint8List(data.length - backspacesCounter);
|
||||
int bufferIndex = buffer.length;
|
||||
backspacesCounter = 0;
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_connection?.close();
|
||||
_nmeaController.close();
|
||||
_stateController.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user