feat(bms): add read-only diagnostics and guarded operating switch commands
This commit is contained in:
@@ -24,6 +24,7 @@ class Service:
|
||||
self.instance = "jkbms_" + uuid.uuid4().hex
|
||||
self.devices = {}
|
||||
self.revision = 0
|
||||
self.operations = None
|
||||
self.day = datetime.now(UTC).date()
|
||||
for path in sorted(self.root.glob("jkbms_*.json"))[:8]:
|
||||
try:
|
||||
@@ -52,6 +53,9 @@ class Service:
|
||||
adapter=None,
|
||||
rssi_dbm=None,
|
||||
link_generation=0,
|
||||
settings=None,
|
||||
settings_at=None,
|
||||
settings_received=0,
|
||||
)
|
||||
d = self.devices[address]
|
||||
d.update(adapter=adapter, rssi_dbm=rssi)
|
||||
@@ -78,9 +82,22 @@ class Service:
|
||||
os.replace(temporary, destination)
|
||||
self.revision += 1
|
||||
|
||||
def settings(self, address, settings):
|
||||
with self.lock:
|
||||
self.devices[address].update(
|
||||
settings=copy.deepcopy(settings), settings_at=utc(), settings_received=self.clock()
|
||||
)
|
||||
self.revision += 1
|
||||
|
||||
def sample(self, address, sample):
|
||||
with self.lock:
|
||||
self.expire()
|
||||
settings = self.devices[address].get("settings")
|
||||
if settings:
|
||||
sample = {
|
||||
**sample,
|
||||
"configured_capacity_ah": settings["values"]["nominal_capacity_ah"],
|
||||
}
|
||||
self.devices[address].update(
|
||||
sample=copy.deepcopy(sample),
|
||||
sample_at=utc(),
|
||||
@@ -98,6 +115,8 @@ class Service:
|
||||
for d in self.devices.values():
|
||||
if today != self.day or self.clock() - d["sampled"] >= 300:
|
||||
d.update(sample=None, sample_at=None)
|
||||
if today != self.day or self.clock() - d["settings_received"] >= 300:
|
||||
d.update(settings=None, settings_at=None)
|
||||
self.day = today
|
||||
|
||||
def inventory(self, node):
|
||||
@@ -118,6 +137,8 @@ class Service:
|
||||
"adapter",
|
||||
"rssi_dbm",
|
||||
"link_generation",
|
||||
"settings",
|
||||
"settings_at",
|
||||
)
|
||||
}
|
||||
status.update(
|
||||
@@ -125,6 +146,12 @@ class Service:
|
||||
address=address,
|
||||
fresh=online,
|
||||
compatible=supported(info),
|
||||
control_available=online
|
||||
and supported(info)
|
||||
and info.get("hardware") == "11.XA"
|
||||
and bool(d["settings"])
|
||||
and address not in (self.operations.blocked if self.operations else set()),
|
||||
control_locked=bool(self.operations and address in self.operations.blocked),
|
||||
sample_ttl_s=300,
|
||||
)
|
||||
items.append(
|
||||
|
||||
Reference in New Issue
Block a user