-
Notifications
You must be signed in to change notification settings - Fork 0
/
owon-console.vala
54 lines (46 loc) · 1.21 KB
/
owon-console.vala
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
void print_measurement(MeasurementResult res)
{
Posix.timeval tv = Posix.timeval();
tv.get_time_of_day();
stdout.printf("%ld measure: %s\n", tv.tv_sec, res.print_value());
}
OwonDevice owondev;
bool autoconnect = false;
bool try_to_connect() {
OwonManager manager = new OwonManager();
try {
manager.discover();
owondev = manager.get_known();
if (owondev != null) {
owondev.got_measurement.connect(print_measurement);
owondev.start_measure();
} else if (autoconnect) {
manager.search_and_connect.begin((obj, res) => {
bool found = manager.search_and_connect.end(res);
Timeout.add(2000, try_to_connect);
});
return false;
}
} catch(IOError e) {
stderr.printf("error getting owon device: %s\n", e.message);
}
return owondev == null;
}
public int main(string [] args) {
DBusConnection conn = Bus.get_sync(BusType.SYSTEM);
register_profile(conn, OwonDevice.uuid);
if (args.length > 1) {
if (args[1] == "--autoconnect") {
autoconnect = true;
} else {
stdout.printf("Usage: %s [--autoconnect]\n", args[0]);
return 1;
}
}
if (try_to_connect()) {
stderr.printf("no devices connected, waiting\n");
Timeout.add(2000, try_to_connect);
}
new MainLoop().run();
return 0;
}