improved logging

This commit is contained in:
Ayzen
2026-06-06 00:52:52 +03:00
parent af6005d68f
commit aea49f6128
65 changed files with 1206 additions and 240 deletions
+10
View File
@@ -36,6 +36,7 @@ class K209RemoteRequestHandler(socketserver.StreamRequestHandler):
"""Handle one persistent K209 remote client connection."""
def setup(self) -> None:
"""Disable Nagle and open the local K209 VISA session for this connection."""
super().setup()
self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.service = CompactMK209Service(
@@ -47,12 +48,14 @@ class K209RemoteRequestHandler(socketserver.StreamRequestHandler):
self.service.open()
def finish(self) -> None:
"""Close the K209 VISA session when the client disconnects."""
try:
self.service.close()
finally:
super().finish()
def handle(self) -> None:
"""Serve command bytes from the client until EOF, reporting errors back inline."""
while True:
command = self.rfile.read(1)
if not command:
@@ -74,12 +77,14 @@ class K209RemoteRequestHandler(socketserver.StreamRequestHandler):
self.wfile.flush()
def _handle_identity(self) -> None:
"""Reply with the device identity string (length-prefixed UTF-8)."""
payload = self.service.query_identity().encode("utf-8")
self.wfile.write(STATUS_OK)
send_u32(self.wfile, len(payload))
self.wfile.write(payload)
def _handle_limits(self) -> None:
"""Reply with the device frequency/IFBW/power/point limits as a packed struct."""
limits = self.service.read_device_limits()
self.wfile.write(STATUS_OK)
self.wfile.write(
@@ -95,6 +100,7 @@ class K209RemoteRequestHandler(socketserver.StreamRequestHandler):
)
def _handle_configure(self) -> None:
"""Apply a sweep config from the client and reply with the frequency axis."""
start_hz, stop_hz, points, ifbw_hz, power_dbm = CONFIG_STRUCT.unpack(
recv_exact(self.rfile, CONFIG_STRUCT.size)
)
@@ -111,6 +117,7 @@ class K209RemoteRequestHandler(socketserver.StreamRequestHandler):
send_float32_array(self.wfile, self.service.frequency_axis())
def _handle_acquire(self) -> None:
"""Acquire one interleaved sweep and reply with the S11 and S21 arrays."""
sweep = self.service.acquire_interleaved()
self.wfile.write(STATUS_OK)
send_u32(self.wfile, int(sweep.frequency_hz.size))
@@ -124,12 +131,14 @@ class K209RemoteServer(socketserver.TCPServer):
allow_reuse_address = True
def __init__(self, server_address: tuple[str, int], resource: str, timeout_ms: int) -> None:
"""Store the VISA resource and timeout used by each accepted connection."""
self.resource = resource
self.timeout_ms = timeout_ms
super().__init__(server_address, K209RemoteRequestHandler)
def _parse_args() -> argparse.Namespace:
"""Parse command-line options for the K209 remote server."""
parser = argparse.ArgumentParser(description="Serve a locally connected Compact-M K209 over TCP.")
parser.add_argument("--host", default="0.0.0.0", help="Server bind address.")
parser.add_argument("--port", type=int, default=DEFAULT_REMOTE_PORT, help="Server TCP port.")
@@ -139,6 +148,7 @@ def _parse_args() -> argparse.Namespace:
def main() -> int:
"""Bind the K209 remote server and serve clients until interrupted."""
args = _parse_args()
with K209RemoteServer((args.host, args.port), resource=args.resource, timeout_ms=args.timeout_ms) as server:
print(f"K209 remote server listening on {args.host}:{args.port}")