-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.py
66 lines (51 loc) · 2.11 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
#!/usr/bin/python3
from fw_victron.victron.mappings import *
from fw_victron.base.device_serial import DeviceSerial
class Device(DeviceSerial):
"""
Device class for Victron devices communicating via Serial port
"""
DELIMITER = "PID"
FIELD_PID = "PID"
FIELD_TYPE = "--"
def __init__(self, device: str = '/dev/ttyUSB0', speed: int = 19200, auto_refresh=True):
super().__init__(device, speed, self.DELIMITER, self.FIELD_PID, self.FIELD_TYPE, auto_refresh)
self.cached_pid = None
self.cached_model = None
self.cached_type = None
self.cached_serial = None
def _parse_pdu(self, frames):
for frame in frames:
if frame.startswith(b'Checksum'):
# This entry is useless
continue
key, value = frame.strip().decode('utf-8').split('\t')
self._data[key] = value
@property
def device_serial(self) -> "str | None":
""" Returns the device serial number """
if self.cached_serial is None:
self.cached_serial = self._data['SER#']
return self.cached_serial
@property
def device_model(self) -> "str | None":
""" Returns the device model """
if self.cached_model is None:
if self.device_pid is not None:
self.cached_model = PID[self.device_pid]['model']
return self.cached_model
@property
def device_type(self) -> str:
""" Returns the device type """
if self.cached_type is None:
model = self.device_model
if model is not None:
try:
self.cached_type = PID[self.device_pid]['type']
except KeyError as err:
raise SystemError("Unknown PID '{}' read from device".format(self.device_pid)) from err
return self.cached_type if self.cached_type is not None else DEV_TYPE_UNKNOWN
if __name__ == '__main__':
v = Device()
print("{}/{}# [{}CONNECTED]: {}".format(v.device_model, v.device_serial,
"" if v.is_connected else "NOT ", v.latest_data["V"]))