feat: wireview clear fault status/log

- WireViewSerialDevice.clear_faults() sends CMD_CLEAR_FAULTS (0x0E)
  with 16-bit keep masks (0x0000 clears, 0xFFFF keeps); all port
  access is now serialized with an RLock so the poller, the
  clear-fault endpoint and the watchdog cannot interleave on the
  port mid-transaction.
- POST /api/wireview/clear-fault (serial transport only — in hwmon
  mode wireviewd owns the port and the masks are read-only); the
  sample is re-read immediately so clients see the cleared masks
  without waiting for the next poll tick.
- WireView tab: Clear buttons on the fault status/log badges.
- tests: protocol-level clear-fault tests via pty (byte-exact
  command framing, keep-mask semantics).
This commit is contained in:
ARIA committed 2026-10-05 20:08:26 +02:00
1 parent 1e11a2c18b
commit d9f5e34c80
5 files changed
+241 -7

No files matched your search

+99
View File
@@ -297,6 +297,10 @@ def test_hwmon_device():
dev = wv.WireViewHwmonDevice(p)
check("hwmon connect", dev.connect())
check("hwmon node exists", dev.node_exists())
check(
"hwmon clear unsupported",
dev.clear_faults(0x0000, 0x0000) is False,
)
s = dev.read_sample()
check("hwmon sample", s is not None)
if s:
@@ -456,6 +460,100 @@ def test_serial_device_protocol():
t.join(timeout=2)
def test_serial_clear_faults():
"""CMD_CLEAR_FAULTS sends 0x0E + two little-endian uint16 keep masks
(0x0000 clears, 0xFFFF keeps), matching the protocol table:
clear fault and log -> 0x0E 00 00 00 00
clear fault only, keep log -> 0x0E 00 00 FF FF
clear log only, keep fault -> 0x0E FF FF 00 00
"""
import pty
import queue
import threading
master, slave = pty.openpty()
slave_path = os.ttyname(slave)
received: queue.Queue = queue.Queue()
def run():
while True:
try:
cmd = os.read(master, 1)
except OSError:
return
if not cmd:
return
if cmd[0] == wv.CMD_CLEAR_FAULTS:
rest = b""
while len(rest) < 4:
try:
chunk = os.read(master, 4 - len(rest))
except OSError:
return
if not chunk:
return
rest += chunk
received.put(cmd + rest)
continue
resp = {
wv.CMD_READ_VENDOR_DATA: bytes([0xEF, 0x05, 1]),
wv.CMD_READ_CONFIG: bytes([0, 0, 1, 0]),
wv.CMD_READ_UID: bytes(12),
wv.CMD_READ_BUILD_INFO: _build_info_struct(
"WireView Pro II", "test"
),
}.get(cmd[0])
if resp:
try:
os.write(master, resp)
except OSError:
return
t = threading.Thread(target=run, daemon=True)
t.start()
try:
dev = wv.WireViewSerialDevice(slave_path)
check("clear: connect", dev.connect())
cases = [
("clear fault and log", 0x0000, 0x0000, bytes([0x0E, 0, 0, 0, 0])),
(
"clear fault only, keep log",
0x0000,
0xFFFF,
bytes([0x0E, 0, 0, 0xFF, 0xFF]),
),
(
"clear log only, keep fault",
0xFFFF,
0x0000,
bytes([0x0E, 0xFF, 0xFF, 0, 0]),
),
]
for name, keep_status, keep_log, expected in cases:
check(f"clear: {name} sent", dev.clear_faults(keep_status, keep_log))
try:
got = received.get(timeout=2)
except queue.Empty:
check(f"clear: {name} bytes", False)
continue
check(f"clear: {name} bytes", got == expected)
# Out-of-range masks are clamped to 16 bits, not sent verbatim.
check("clear: out-of-range sent", dev.clear_faults(0x1FFFF, 0x1FFFF))
try:
got = received.get(timeout=2)
check("clear: out-of-range clamped", got == bytes([0x0E, 0xFF, 0xFF, 0xFF, 0xFF]))
except queue.Empty:
check("clear: out-of-range clamped", False)
dev.close()
check("clear: rejected when disconnected", not dev.clear_faults(0, 0))
finally:
os.close(master)
t.join(timeout=2)
def test_serial_device_rejected_product():
"""An unsupported product id is rejected and memoized via .rejected."""
responses = {wv.CMD_READ_VENDOR_DATA: bytes([0xEF, 0x07, 1])}
@@ -530,6 +628,7 @@ def main() -> int:
test_hwmon_device()
test_hwmon_legacy_attrs()
test_serial_device_protocol()
test_serial_clear_faults()
test_serial_device_rejected_product()
test_serial_device_no_response()
test_serial_transport_missing_pyserial()