-
Notifications
You must be signed in to change notification settings - Fork 3
/
decode_packet.dart
72 lines (65 loc) · 1.68 KB
/
decode_packet.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
import 'package:logging/logging.dart';
import 'package:zwave/util/packet_to_source.dart';
import 'dart:io';
main(List<String> args) {
Logger.root.onRecord.listen(printLog);
// convert arguments into a list of bytes
final data = <int>[];
try {
for (String arg in args) {
for (String part1 in arg.split('[')) {
for (String part2 in part1.split(',')) {
for (String text in part2.split(']')) {
text = text.trim();
if (text.isNotEmpty) {
data.add(int.parse(text));
}
}
}
}
}
} on FormatException catch (e) {
print('Failed to parse int: ${e.source}');
usage();
return;
}
if (data.isEmpty) {
usage();
return;
}
print(packetToSource(data));
}
void printLog(LogRecord rec) {
var buffer = StringBuffer();
printLogTo(buffer, rec);
print(buffer.toString());
}
final defaultLogLevel = Level.CONFIG;
void printLogTo(StringBuffer buffer, LogRecord rec) {
buffer
..write(rec.time.toString().padRight(26, '0'))
..write(' ')
..write(rec.level.name.padRight(7))
..write(' ')
..write(rec.loggerName.padRight(27))
..write(': ')
..write(rec.message);
if (rec.error != null) {
buffer
..writeln()
..write(' ')
..write(rec.error);
}
if (rec.stackTrace != null) {
buffer
..writeln()
..write(rec.stackTrace);
}
}
void usage() {
var appName = Platform.resolvedExecutable;
appName = appName.substring(appName.lastIndexOf('bin/'));
print('usage: dart $appName <bytes>');
print(' where <bytes> is a list of int values between 0 and 255 inclusive');
print(' all "[", ",", and "]" characters are ignored');
}