feat: Implement poll() method for efficient device communication and update monitoring status retrieval
This commit is contained in:
@@ -727,6 +727,24 @@ class MiniMateClient:
|
|||||||
notes=notes,
|
notes=notes,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def poll(self) -> None:
|
||||||
|
"""
|
||||||
|
Perform just the POLL startup handshake — no config reads.
|
||||||
|
|
||||||
|
Opens the connection if not already open. Used by the monitoring
|
||||||
|
endpoints which need to communicate with the device quickly without
|
||||||
|
spending 10-15 seconds reading compliance config and event index.
|
||||||
|
|
||||||
|
The POLL establishes the DLE-framed session with the device.
|
||||||
|
After poll(), the protocol is ready for any command (read_monitor_status,
|
||||||
|
start_monitoring, stop_monitoring, etc.).
|
||||||
|
"""
|
||||||
|
if not self.is_open:
|
||||||
|
self.open()
|
||||||
|
proto = self._require_proto()
|
||||||
|
log.debug("poll: startup handshake")
|
||||||
|
proto.startup()
|
||||||
|
|
||||||
# ── Monitoring ────────────────────────────────────────────────────────────
|
# ── Monitoring ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
def get_monitor_status(self) -> MonitorStatus:
|
def get_monitor_status(self) -> MonitorStatus:
|
||||||
@@ -1748,6 +1766,14 @@ def _decode_monitor_status(data: bytes) -> MonitorStatus:
|
|||||||
"""
|
"""
|
||||||
# The data section starts at offset 11 (after the S3 section header).
|
# The data section starts at offset 11 (after the S3 section header).
|
||||||
section = data[11:] if len(data) > 11 else data
|
section = data[11:] if len(data) > 11 else data
|
||||||
|
|
||||||
|
# Log the raw payload at WARNING level so we can see it in the server logs
|
||||||
|
# and confirm the field offsets and is_monitoring detection are correct.
|
||||||
|
log.warning(
|
||||||
|
"_decode_monitor_status: total data=%d bytes section=%d bytes hex=%s",
|
||||||
|
len(data), len(section), section.hex(),
|
||||||
|
)
|
||||||
|
|
||||||
# Mode: idle payload is 44 bytes; monitoring is shorter (12 bytes observed)
|
# Mode: idle payload is 44 bytes; monitoring is shorter (12 bytes observed)
|
||||||
is_monitoring = len(section) < 20
|
is_monitoring = len(section) < 20
|
||||||
|
|
||||||
|
|||||||
+16
-11
@@ -626,15 +626,20 @@ def device_monitor_status(
|
|||||||
"""
|
"""
|
||||||
Read monitoring status from the device.
|
Read monitoring status from the device.
|
||||||
|
|
||||||
Returns is_monitoring bool, battery voltage, and memory usage (total + free bytes).
|
Uses poll() (POLL handshake only — no config/compliance reads) so the
|
||||||
Battery and memory are only present when the unit is idle (not monitoring).
|
request completes in ~2 seconds instead of ~15. The full connect() was
|
||||||
|
causing false "idle" readings because the compliance+event sequence was
|
||||||
|
interacting with the device state before the 0x1C read.
|
||||||
|
|
||||||
|
Returns is_monitoring bool, battery voltage, and memory usage (total + free
|
||||||
|
bytes). Battery and memory are only present when the unit is idle.
|
||||||
"""
|
"""
|
||||||
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
||||||
try:
|
try:
|
||||||
client.connect()
|
client.poll()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
log.warning("monitor status connect retry: %s", exc)
|
log.warning("monitor status poll retry: %s", exc)
|
||||||
client.connect()
|
client.poll()
|
||||||
status = client.get_monitor_status()
|
status = client.get_monitor_status()
|
||||||
|
|
||||||
result: dict = {"is_monitoring": status.is_monitoring}
|
result: dict = {"is_monitoring": status.is_monitoring}
|
||||||
@@ -663,10 +668,10 @@ def device_monitor_start(
|
|||||||
"""
|
"""
|
||||||
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
||||||
try:
|
try:
|
||||||
client.connect()
|
client.poll()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
log.warning("start monitoring connect retry: %s", exc)
|
log.warning("start monitoring poll retry: %s", exc)
|
||||||
client.connect()
|
client.poll()
|
||||||
client.start_monitoring()
|
client.start_monitoring()
|
||||||
return {"status": "started"}
|
return {"status": "started"}
|
||||||
|
|
||||||
@@ -685,10 +690,10 @@ def device_monitor_stop(
|
|||||||
"""
|
"""
|
||||||
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
with _build_client(port=port, baud=baud, host=host, tcp_port=tcp_port) as client:
|
||||||
try:
|
try:
|
||||||
client.connect()
|
client.poll()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
log.warning("stop monitoring connect retry: %s", exc)
|
log.warning("stop monitoring poll retry: %s", exc)
|
||||||
client.connect()
|
client.poll()
|
||||||
client.stop_monitoring()
|
client.stop_monitoring()
|
||||||
return {"status": "stopped"}
|
return {"status": "stopped"}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user