from __future__ import annotations
from spvirit import codec
print("── encode_pv_request ────────────────────────────────────────────────")
full = codec.encode_pv_request()
subset = codec.encode_pv_request(["value", "alarm.severity", "timeStamp"])
print(f"all-fields request ({len(full)} B): {full.hex()}")
print(f"subset request ({len(subset)} B): {subset.hex()}")
print("\n── decode_packet (SET_BYTE_ORDER control frame) ─────────────────────")
frame = bytes([0xCA, 0x02, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00])
meta = codec.decode_packet(frame)
print(f"command = {meta['command']} ({meta['command_name']})")
print(f"version = {meta['version']}")
print(f"flags.raw = {meta['flags']['raw']:#04x}")
print(f"is_control = {meta['flags']['is_control']}")
print(f"payload len = {meta['payload_length']}")
print("\n── StructureDesc API shape ─────────────────────────────────────────")
req_bytes = codec.encode_pv_request(["value"])
print("StructureDesc attributes: struct_id, fields; methods: field(name),")
print("dump(), __len__, __contains__. See demo_channel.py for a live one.")
print("\n── encode_put_payload ──────────────────────────────────────────────")
print("codec.encode_put_payload(structure_desc, value, is_be=False) -> bytes")
print("See demo_channel.py for a live call using Channel.introspect().")
sample = {
"value": 42.5,
"alarm": {"severity": 0, "status": 0, "message": ""},
"timeStamp": {"secondsPastEpoch": 1_700_000_000, "nanoseconds": 0, "userTag": 0},
"display": {"units": "mA", "description": "sample current"},
}
print("\n── format_value / extract_nt_value ─────────────────────────────────")
print(f"format_value -> {codec.format_value(sample)!r}")
print(f"extract_nt_value -> {codec.extract_nt_value(sample)!r}")