-
Notifications
You must be signed in to change notification settings - Fork 0
/
galvanize.py
188 lines (160 loc) · 5.55 KB
/
galvanize.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
import concurrent.futures
import logging
import os
import subprocess
import sys
import threading
import time
import hid
from scapy.automaton import ATMT, Automaton
from scapy.config import conf
from scapy.layers.inet import UDP
logging.basicConfig(
format="%(levelname)s:%(name)s:%(threadName)s:%(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
DEVICE = None
IN_MEETING = False
def discovery() -> None:
global DEVICE
threading.current_thread().name = (
sys._getframe().f_code.co_name # pylint: disable=protected-access
)
logger.info("Waiting for device")
while True:
if DEVICE:
try:
DEVICE.get_product_string()
except OSError:
logger.debug("Device is probably disconnected")
DEVICE = None
else:
if hid.enumerate(vendor_id=0x04D8, product_id=0xF372):
logger.debug("Found device, trying to open")
try:
DEVICE = hid.device()
DEVICE.open(vendor_id=0x04D8, product_id=0xF372)
logger.info("Device found: %r", DEVICE)
break
except OSError:
logger.debug("Failed to open device")
logger.debug("Device not found: %r", DEVICE)
DEVICE = None
time.sleep(5)
def worker() -> None:
threading.current_thread().name = (
sys._getframe().f_code.co_name # pylint: disable=protected-access
)
try:
is_muted = get_muted()
logger.debug(is_muted)
set_led(is_muted, [6, 1, 2])
while True:
data = DEVICE.read(0x8, 500)
if data:
logger.debug("%r", data)
if data == [131, 1, 0, 0, 0, 0, 0, 0]:
is_muted = get_muted()
should_mute = not is_muted
set_led(should_mute, [6, 1, 2])
set_muted(should_mute)
except OSError:
logger.info("Lost device: %r", DEVICE)
def get_muted():
pactl = subprocess.run(
["pactl", "get-source-mute", "@DEFAULT_SOURCE@"],
capture_output=True,
check=True,
)
logger.debug(pactl)
return pactl.stdout == b"Mute: yes\n"
def set_muted(should_mute):
pactl = subprocess.run(
["pactl", "set-source-mute", "@DEFAULT_SOURCE@", str(should_mute)], check=True
)
logger.info(pactl)
def set_led(should_shine, leds):
if DEVICE:
if should_shine:
for led in leds:
DEVICE.write([1, led, 5, 0, 0, 255, 255, 255])
else:
for led in leds:
DEVICE.write([1, led, 0, 0, 0, 255, 255, 255])
def meeting_detector():
global IN_MEETING
logger.info("MeetingDetector started")
threading.current_thread().name = (
sys._getframe().f_code.co_name # pylint: disable=protected-access
)
class MeetingDetector(Automaton):
# pylint: disable=raising-bad-type
def master_filter(self, pkt):
should_accept = UDP in pkt and pkt[UDP].dport >= 8801 and pkt[UDP].dport <= 8810
pkt = None
return should_accept
@ATMT.state(initial=1)
def begin(self):
logger.debug("State=BEGIN")
raise self.waiting()
@ATMT.state()
def waiting(self):
logger.debug("in_meeting? %r", IN_MEETING)
@ATMT.receive_condition(waiting)
def chk_receive_data(self, pkt):
raiser = self.receiving(pkt)
pkt = None
raise raiser
# RECEIVED
@ATMT.state()
def receiving(self, pkt):
global IN_MEETING
IN_MEETING = True
try:
set_led(IN_MEETING, [3, 4, 5])
except (OSError, AttributeError):
logger.exception("Lost device: %r", DEVICE)
MeetingDetector().destroy()
MeetingDetector().run()
pkg = None
raise self.waiting()
@ATMT.timeout(waiting, 5)
def timeout_waiting(self):
global IN_MEETING
if DEVICE and IN_MEETING:
IN_MEETING = False
try:
set_led(IN_MEETING, [3, 4, 5])
except (OSError, AttributeError):
logger.exception("Lost device: %r", DEVICE)
MeetingDetector().destroy()
MeetingDetector().run()
raise self.waiting()
@ATMT.action(timeout_waiting)
def retransmit_last_packet(self):
logger.debug("hit timeout?")
logger.debug(IN_MEETING)
@ATMT.state(final=1)
def end(self):
logger.debug("State=END")
MeetingDetector().run()
def main() -> None:
logger.info("Starting up")
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
executor.submit(meeting_detector)
while True:
try:
futures = []
futures.append(executor.submit(discovery))
futures.append(executor.submit(worker))
for future in concurrent.futures.as_completed(futures):
try:
logger.debug("Future result: %r", future.result())
except Exception: # pylint: disable=broad-except
logger.exception("Exception in thread")
except KeyboardInterrupt:
logger.info("Shutting down")
os._exit(0) # pylint: disable=protected-access
if __name__ == "__main__":
main()