Revize 474551ae
Přidáno uživatelem Jakub Šilhavý před více než 2 roky(ů)
client/usb_detector/detector.py | ||
---|---|---|
3 | 3 |
from time import sleep |
4 | 4 |
|
5 | 5 |
from .usb_reader import read_connected_devices |
6 |
from config_manager import scan_period_seconds, connected_devices_filename |
|
7 | 6 |
|
8 | 7 |
|
9 | 8 |
_listeners_connected = [] |
10 | 9 |
_listeners_disconnected = [] |
11 | 10 |
_last_connected_devices = [] |
11 |
_config = None |
|
12 | 12 |
|
13 | 13 |
|
14 |
def register_listener(callback, connected: bool = True): |
|
15 |
logging.info(f'Registering callback: {callback}.') |
|
14 |
def usb_detector_set_config(config): |
|
15 |
global _config |
|
16 |
_config = config |
|
17 |
|
|
16 | 18 |
|
19 |
def register_listener(callback, connected: bool = True): |
|
20 |
logging.info(f"Registering callback: {callback}.") |
|
17 | 21 |
if connected is True: |
18 | 22 |
_listeners_connected.append(callback) |
19 | 23 |
else: |
... | ... | |
21 | 25 |
|
22 | 26 |
|
23 | 27 |
def _notify_listeners(listeners: list, devices: list): |
28 |
if listeners is None or devices is None: |
|
29 |
return |
|
24 | 30 |
for callback in listeners: |
25 | 31 |
for device in devices: |
26 | 32 |
callback(device) |
27 | 33 |
|
28 | 34 |
|
29 | 35 |
def _store_connected_devices(devices: list): |
30 |
logging.debug('storing newly connected devices')
|
|
31 |
with open(connected_devices_filename, "w") as file: |
|
36 |
logging.debug("storing newly connected devices")
|
|
37 |
with open(_config.connected_devices_filename, "w") as file:
|
|
32 | 38 |
json.dump(devices, file) |
33 | 39 |
|
34 | 40 |
|
35 | 41 |
def _load_last_connected_devices() -> list: |
36 |
logging.debug('loading last connected devices')
|
|
42 |
logging.debug("loading last connected devices")
|
|
37 | 43 |
try: |
38 |
with open(connected_devices_filename, "r") as file: |
|
44 |
with open(_config.connected_devices_filename, "r") as file:
|
|
39 | 45 |
return json.loads(file.read()) |
40 | 46 |
except IOError: |
41 |
logging.error('loading of last connected devices failed')
|
|
47 |
logging.error("loading of last connected devices failed")
|
|
42 | 48 |
return [] |
43 | 49 |
|
44 | 50 |
|
... | ... | |
73 | 79 |
|
74 | 80 |
while True: |
75 | 81 |
_update() |
76 |
sleep(scan_period_seconds) |
|
82 |
sleep(_config.scan_period_seconds) |
Také k dispozici: Unified diff
re #9419 Moved the config manager into a class (better modularity)