-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.py
73 lines (58 loc) · 2.05 KB
/
device.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
#!/usr/bin/python3
import signal
# noinspection PyPropertyDefinition
class DeviceAbs:
"""
Device base classes.
"""
def __init__(self):
self._must_terminate = False
self._register_kill_signals()
def refresh(self, reset_data=False):
"""
Refresh latest data querying them to the device, if `reset_data` is true,
then default-Zero values are set.
"""
raise NotImplementedError()
@property
def is_connected(self) -> bool:
""" Returns True if at last refresh attempt the serial device was available. """
raise NotImplementedError()
@property
def is_reading(self) -> bool:
""" Returns the local device (eg: '/dev/ttyUSB0') used to connect to the serial device """
raise NotImplementedError()
def terminate(self):
"""
Send the terminate signal to all device process and loops.
"""
self._must_terminate = True
@property
def must_terminate(self) -> bool:
""" Returns True if the device must terminate the current operation and disconnect """
return self._must_terminate
def _register_kill_signals(self):
signal.signal(signal.SIGINT, self.__handle_kill_signals)
signal.signal(signal.SIGTERM, self.__handle_kill_signals)
def __handle_kill_signals(self, signo, _stack_frame):
print("Device received `{}` signal. Shutdown device...".format(signo))
# SIGINT 2 <= Ctrl+C
# SIGTERM 15 <= kill PID
self.terminate()
@property
def device_pid(self) -> "str | None":
"""
Returns the device PID, it can be used as index for the PID dict.
"""
raise NotImplementedError()
@property
def device_type(self) -> str:
""" Returns the device type """
raise NotImplementedError()
@property
def device_type_code(self) -> str:
""" Returns the device type as a code string"""
raise NotImplementedError()
@property
def latest_data(self) -> dict:
raise NotImplementedError()