Skip to content

Commit e8da4a7

Browse files
authored
Merge branch 'main' into rfid
2 parents 3c2f31b + 442f018 commit e8da4a7

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

client/lib/utils/rfid_scanner.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'dart:async';
2+
import 'package:flutter/services.dart';
3+
import 'package:logger/logger.dart';
4+
5+
final _log = Logger();
6+
7+
class RfidScanBuffer {
8+
final void Function(String scannedInput) onScan;
9+
10+
String _buffer = '';
11+
Timer? _timeout;
12+
13+
static const _timeoutDuration = Duration(milliseconds: 500);
14+
15+
RfidScanBuffer({required this.onScan});
16+
17+
bool handleKeyEvent(KeyEvent event) {
18+
if (event is! KeyDownEvent) return false;
19+
20+
final key = event.logicalKey;
21+
22+
// Enter key = end of scan
23+
if (key == LogicalKeyboardKey.enter ||
24+
key == LogicalKeyboardKey.numpadEnter) {
25+
if (_buffer.isNotEmpty) {
26+
final input = _buffer.trim();
27+
_clear();
28+
_log.d('RFID scan received: $input');
29+
onScan(input);
30+
}
31+
return false;
32+
}
33+
34+
// Accumulate character input
35+
final char = event.character;
36+
if (char != null && char.isNotEmpty) {
37+
_buffer += char;
38+
_resetTimeout();
39+
return false;
40+
}
41+
42+
return false;
43+
}
44+
45+
void _resetTimeout() {
46+
_timeout?.cancel();
47+
_timeout = Timer(_timeoutDuration, () {
48+
if (_buffer.isNotEmpty) {
49+
_log.t('RFID buffer timeout, clearing: $_buffer');
50+
_buffer = '';
51+
}
52+
});
53+
}
54+
55+
void _clear() {
56+
_buffer = '';
57+
_timeout?.cancel();
58+
_timeout = null;
59+
}
60+
61+
void dispose() {
62+
_clear();
63+
}
64+
}

0 commit comments

Comments
 (0)