-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_port.dart
76 lines (66 loc) · 1.9 KB
/
check_port.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
73
74
75
76
import 'dart:async';
import 'dart:io';
import 'package:logging/logging.dart';
import 'package:zwave/port/rpi_zw_port.dart';
import 'package:zwave/zw_manager.dart';
Future<void> main(List<String> args) async {
setupLogger();
String zwaveDevicePath = getZWaveDevicePath(args);
Logger.root.fine('starting...');
final port = RpiZwPort();
final manager = ZwManager(port.driver);
Logger.root.config('opening Z-Wave port: ${args[0]}');
await port.open(zwaveDevicePath);
// Display ZWave controller version
final version = await manager.apiLibraryVersion();
Logger.root.info(version.toString());
await Future.delayed(const Duration(seconds: 3));
Logger.root.config('closing Z-Wave port');
await port.close();
Logger.root.config('exiting');
}
String getZWaveDevicePath(List<String> args) {
if (args.isNotEmpty) {
return args[0];
}
print('missing path to Z-Wave device (e.g. /dev/ttyACM0)');
var deviceFiles = Directory('/dev')
.listSync()
.where((f) =>
f is File &&
(f.uri.pathSegments.last.startsWith('ttyA') ||
f.uri.pathSegments.last.startsWith('ttyU')))
.map((f) => f.path)
.toList()
..sort();
if (deviceFiles.isNotEmpty) {
print(' perhaps one of ${deviceFiles.join(', ')} ?');
}
exit(1);
}
void setupLogger() {
Logger.root.level = Level.FINEST;
Logger.root.onRecord.listen((LogRecord rec) {
var buffer = StringBuffer();
buffer
..write(rec.time.toString().padRight(26, '0'))
..write(' ')
..write(rec.level.name.padRight(7))
..write(' ')
..write(rec.loggerName.padRight(23))
..write(': ')
..write(rec.message);
if (rec.error != null) {
buffer
..writeln()
..write(' ')
..write(rec.error);
}
if (rec.stackTrace != null) {
buffer
..writeln()
..write(rec.stackTrace);
}
print(buffer.toString());
});
}