This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
av_control.py
executable file
·72 lines (53 loc) · 2.03 KB
/
av_control.py
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
#!/usr/bin/env python
"""
Manage I/O between a collection of A/V devices.
The A/V devices (and some not-so-A/V devices) are each represented by a class
instance derived from the AV_Device API.
The A/V devices are controlled by an AV_Loop instance, which mediates
communication and runtime between the instantiated devices.
Devices may submit commands (strings) to the AV_Loop, which then dispatches the
command to the command handler registered for that command (or falls back to
the empty ('') catch-all command handler if no handler is registered for the
given command).
Additionally, AV_Devices may register I/O handlers with the AV_Loop, which will
then listen for I/O events on the given file descriptors.
"""
import sys
import argparse
from tornado.ioloop import IOLoop
from hdmi_switch import HDMI_Switch
from avr_device import AVR_Device
from http_server import AV_HTTPServer
from av_loop import AV_Loop
Devices = (
("hdmi", HDMI_Switch),
("avr", AVR_Device),
("http", AV_HTTPServer),
)
AVR_Device.DefaultTTY = "/dev/ttyUSB1"
def main(args):
parser = argparse.ArgumentParser(
description="Controller daemon for A/V devices")
for name, cls in Devices:
cls.register_args(name, parser)
IOLoop.configure(AV_Loop, parsed_args=vars(parser.parse_args(args)))
mainloop = IOLoop.instance()
for name, cls in Devices:
try:
print("*** Initializing %s..." % (cls.Description), end=' ')
mainloop.add_device(name, cls(mainloop, name))
print("done")
except Exception as e:
print(e)
if not mainloop.cmd_handlers:
print("No A/V commands registered. Aborting...")
return 1
def cmd_catch_all(empty, cmd):
"""Handle commands that are not handled elsewhere."""
assert empty == ""
print("*** Unknown A/V command: '%s'" % (cmd))
mainloop.add_cmd_handler("", cmd_catch_all)
print("Starting A/V controller main loop.")
return mainloop.run()
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))