feat(bms): add read-only diagnostics and guarded operating switch commands
This commit is contained in:
@@ -10,6 +10,7 @@ import time
|
||||
import dbus
|
||||
from dbus.mainloop.glib import DBusGMainLoop
|
||||
|
||||
from .operations import Operations
|
||||
from .protocol import (
|
||||
CHARACTERISTIC,
|
||||
SERVICE,
|
||||
@@ -20,6 +21,7 @@ from .protocol import (
|
||||
supported,
|
||||
telemetry,
|
||||
)
|
||||
from .settings import decode_settings, switch_command
|
||||
|
||||
DEVICE = "org.bluez.Device1"
|
||||
ADAPTER = "org.bluez.Adapter1"
|
||||
@@ -30,6 +32,8 @@ class Observer:
|
||||
def __init__(self, service):
|
||||
DBusGMainLoop(set_as_default=True)
|
||||
self.service = service
|
||||
self.operations = Operations(service, self)
|
||||
service.operations = self.operations
|
||||
self.bus = dbus.SystemBus()
|
||||
self.scanning = set()
|
||||
self.seen = {}
|
||||
@@ -91,6 +95,10 @@ class Observer:
|
||||
self.finish("unsupported_firmware", delay=300)
|
||||
return
|
||||
a["verified"] = True
|
||||
elif frame[4] == 1 and a.get("verified"):
|
||||
settings = decode_settings(frame)
|
||||
self.service.settings(a["address"], settings)
|
||||
self.operations.received(a["address"], frame, settings)
|
||||
elif frame[4] == 2 and a.get("verified"):
|
||||
self.service.sample(a["address"], telemetry(frame))
|
||||
a["last_sample"] = time.monotonic()
|
||||
@@ -104,6 +112,7 @@ class Observer:
|
||||
self.scanning.discard(path)
|
||||
|
||||
def finish(self, reason, delay=None):
|
||||
self.operations.finish("Bluetooth-сеанс завершён; команда не повторяется.")
|
||||
a, self.active = self.active, None
|
||||
if not a:
|
||||
return
|
||||
@@ -133,6 +142,13 @@ class Observer:
|
||||
)
|
||||
a["last_query"] = time.monotonic()
|
||||
|
||||
def write_switch(self, name, enabled):
|
||||
self.interface(self.active["characteristic"], GATT).WriteValue(
|
||||
dbus.Array(switch_command(name, enabled), signature="y"),
|
||||
dbus.Dictionary({"type": dbus.String("command")}, signature="sv"),
|
||||
timeout=3,
|
||||
)
|
||||
|
||||
def tick(self):
|
||||
if self.closed:
|
||||
return False
|
||||
@@ -174,11 +190,20 @@ class Observer:
|
||||
if a.get("verified") and not a.get("requested"):
|
||||
self.write_read(0x96)
|
||||
a["requested"] = True
|
||||
self.operations.tick()
|
||||
if (
|
||||
a.get("verified")
|
||||
and a.get("last_sample")
|
||||
and now - a.get("settings_query", a["started"]) > 60
|
||||
and not self.operations.job
|
||||
):
|
||||
self.write_read(0x96)
|
||||
a["settings_query"] = now
|
||||
if a.get("last_query") and now - a.get("last_sample", a["last_query"]) > 12:
|
||||
self.finish("read_timeout")
|
||||
elif not a.get("last_sample") and now - a["started"] > 45:
|
||||
self.finish("connect_timeout")
|
||||
elif now - a["started"] > 120:
|
||||
elif now - a["started"] > 120 and not self.operations.job:
|
||||
# Fairness: refresh discovery so a second BMS can be admitted.
|
||||
self.finish("reconnecting", delay=5)
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user