-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.dart
72 lines (55 loc) · 1.97 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// ignore_for_file: avoid_print, unreachable_from_main
import 'dart:convert';
import 'package:nmea/nmea.dart' as nmea;
void main() {
Stream.fromIterable([
r'$--MSG,A,1,0,0,0,0*29',
r"$PACME{'test':true}",
r'$--NOT,REGISTERED,SENTENCE,TEST*1C',
r'$CST,first,second',
])
.transform(
nmea.NmeaDecoder(onlyAllowValid: true)
..registerTalkerSentence(MsgSentence.id, (line) => MsgSentence(raw: line))
..registerProprietarySentence(
AcmeProprietarySentence.id,
(line) => AcmeProprietarySentence(raw: line),
)
..registerCustomChecksumSentence(
MyCustomSentence.id,
(line) => MyCustomSentence(raw: line, validateChecksums: false),
),
)
.listen((sentence) {
print('${sentence.raw} is a valid ${sentence.type.name} sentence');
});
// Output:
// $--MSG,A,1,0,0,0,0*29 is a valid talker sentence
// $PACME{'test':true} is a valid proprietary sentence
// $CST,first,second is a valid custom sentence
}
class MsgSentence extends nmea.TalkerSentence {
MsgSentence({required super.raw});
static const String id = 'MSG';
// You can access the fields in this talker sentence by their index
String get field1 => fields[1];
int get field2 => int.parse(fields[2]);
}
class AcmeProprietarySentence extends nmea.ProprietarySentence {
AcmeProprietarySentence({required super.raw}) : super(manufacturer: id);
static const String id = 'ACME';
// custom data formatting is allowed in proprietary sentences
String get json => rawWithoutFixtures;
// custom validation by overriding [valid]
// remember to call [super.valid]!
@override
bool get valid => super.valid && json.isNotEmpty;
dynamic get data => jsonDecode(json);
}
class MyCustomSentence extends nmea.CustomChecksumSentence {
MyCustomSentence({required super.raw, super.validateChecksums = true})
: super(identifier: id);
static const String id = 'CST';
String get first => fields[0];
String get second => fields[1];
}