Aggressively cache properties

which are not expected to change at run time
This commit is contained in:
Odd Stråbø 2023-12-10 23:54:13 +01:00
parent 3aa6b13615
commit 4dc42ee6f5

View file

@ -232,8 +232,13 @@ class ChargeController:
def __init__(self, device: BaseInterface):
self.device = device
_cached_serial: str | None = None
@property
def serial(self) -> str:
if self._cached_serial is not None:
return self._cached_serial
data = readMemory(self.device, 0x18, 3)
if data is None:
raise IOError # FIXME: Raise specific error in readMemory
@ -241,18 +246,31 @@ class ChargeController:
p1 = data[0]
p2 = data[1]
p3 = (data[2] << 8) + data[3]
return f"{p1}-{p2}-{p3}"
self._cached_serial = f"{p1}-{p2}-{p3}"
return self._cached_serial
_cached_model: str | None = None
@property
def model(self) -> str:
if self._cached_model is not None:
return self._cached_model
data = readMemory(self.device, 0x0C, 8)
if data is None:
raise IOError # FIXME: Raise specific error in readMemory
return data.decode("utf-8").strip()
self._cached_model = data.decode("utf-8").strip()
return self._cached_model
_cached_version: str | None = None
@property
def version(self) -> str:
if self._cached_version is not None:
return self._cached_version
data = readMemory(self.device, 0x14, 4)
if data is None:
raise IOError # FIXME: Raise specific error in readMemory
@ -261,7 +279,8 @@ class ChargeController:
minor = data[2]
patch = data[3]
return f"{major}.{minor}.{patch}"
self._cached_version = f"{major}.{minor}.{patch}"
return self._cached_version
@property
def load_enabled(self) -> bool: