1
0
Files
BasaltMeter/BasaltTrainer/cmd_server.py

127 lines
4.4 KiB
Python
Raw Normal View History

2026-01-06 19:50:45 +08:00
import struct
from enum import IntEnum, Enum, auto
from dataclasses import dataclass
2026-01-06 21:24:47 +08:00
from typing import Callable
2026-01-06 19:50:45 +08:00
from pipe_operator import PipeOperator
PIPE_NAME: str = "ed0e3f1f-d214-4880-9562-640bce15e72e"
2026-01-06 21:24:47 +08:00
2026-01-06 19:50:45 +08:00
class ProtocolCode(IntEnum):
HANDSHAKE_REQUEST = 0x61 # Trainer -> Presenter
HANDSHAKE_RESPONSE = 0x62 # Presenter -> Trainer
DATA_READY = 0x01 # Presenter -> Trainer
DATA_RECEIVED = 0x02 # Trainer -> Presenter
2026-01-08 19:48:56 +08:00
ACTIVELY_STOP = 0x21 # Presenter -> Trainer
STOP_REQUEST = 0x71 # Trainer -> Presenter
STOP_RESPONSE = 0x72 # Trainer -> Presenter
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
2026-01-06 19:50:45 +08:00
class PixelKind(IntEnum):
GRAY_FLOAT32 = 0x01 # Grayscale represented by one float32
GRAY_U8 = 0x02 # Grayscale represented by one u8
RGB_FLOAT32 = 0x03 # RGB represented by three float32
RGB_U8 = 0x04 # RGB represented by three u8
2026-01-06 21:24:47 +08:00
2026-01-06 19:50:45 +08:00
@dataclass
2026-01-06 21:24:47 +08:00
class HandshakePayload:
2026-01-06 19:50:45 +08:00
pixel_kind: PixelKind
width: int
height: int
2026-01-06 21:24:47 +08:00
2026-01-06 19:50:45 +08:00
class ServerStatus(Enum):
Ready = auto()
Running = auto()
Stop = auto()
CODE_PACKER: struct.Struct = struct.Struct("=B")
2026-01-06 21:24:47 +08:00
HANDSHAKE_REQUEST_PACKER: struct.Struct = struct.Struct("=BII")
2026-01-06 19:50:45 +08:00
2026-01-08 19:37:25 +08:00
class CmdServer:
2026-01-06 19:50:45 +08:00
"""
Command server implementation for the Trainer side according to the protocol.
"""
2026-01-08 19:48:56 +08:00
__pipe_operator: PipeOperator
__status: ServerStatus
2026-01-06 19:50:45 +08:00
def __init__(self):
2026-01-08 19:48:56 +08:00
self.__pipe_operator = PipeOperator(PIPE_NAME)
self.__status = ServerStatus.Ready
2026-01-06 19:50:45 +08:00
def __del__(self):
"""Cleanup resources when object is destroyed."""
2026-01-08 19:48:56 +08:00
self.__pipe_operator.close()
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
def wait_handshake(self, payload: HandshakePayload) -> None:
2026-01-06 19:50:45 +08:00
"""
Wait for handshake from Presenter, send request first and wait for response with data properties.
Returns a tuple of (pixel_kind, width, height) from the Presenter.
"""
2026-01-08 19:48:56 +08:00
if self.__status != ServerStatus.Ready:
2026-01-06 19:50:45 +08:00
raise RuntimeError("unexpected server status")
2026-01-08 19:48:56 +08:00
# Send handshake request to Presenter
self.__pipe_operator.write(CODE_PACKER.pack(ProtocolCode.HANDSHAKE_REQUEST))
2026-01-06 21:24:47 +08:00
# And the payload data
2026-01-08 20:04:24 +08:00
self.__pipe_operator.write(
HANDSHAKE_REQUEST_PACKER.pack(
payload.pixel_kind, payload.width, payload.height
)
)
2026-01-06 19:50:45 +08:00
# Wait for handshake response from Presenter (code 0x62)
2026-01-08 19:48:56 +08:00
code_bytes = self.__pipe_operator.read(CODE_PACKER.size)
2026-01-08 20:04:24 +08:00
(code,) = CODE_PACKER.unpack(code_bytes)
2026-01-06 19:50:45 +08:00
if ProtocolCode(code) != ProtocolCode.HANDSHAKE_RESPONSE:
2026-01-08 19:48:56 +08:00
raise RuntimeError("expect handshake response code, but got another")
2026-01-06 19:50:45 +08:00
# Set status and return
2026-01-08 19:48:56 +08:00
self.__status = ServerStatus.Running
2026-01-06 21:24:47 +08:00
return
2026-01-06 19:50:45 +08:00
2026-01-08 20:04:24 +08:00
def tick(self, data_receiver: Callable[[], None], request_stop: bool) -> bool:
2026-01-06 19:50:45 +08:00
"""
Tick function called every frame to wait for data ready from Presenter and send response.
Returns True if a stop code was received (meaning the process should stop), False otherwise.
"""
2026-01-08 19:48:56 +08:00
if self.__status != ServerStatus.Running:
2026-01-06 19:50:45 +08:00
raise RuntimeError("unexpected server status")
2026-01-08 20:25:33 +08:00
# If there is stop requested from us,
# we order Presenter exit and enter next step.
2026-01-06 19:50:45 +08:00
if request_stop:
2026-01-08 20:25:33 +08:00
self.__pipe_operator.write(CODE_PACKER.pack(ProtocolCode.STOP_REQUEST))
2026-01-06 19:50:45 +08:00
2026-01-08 19:48:56 +08:00
while True:
# Wait for code from Presenter
code_bytes = self.__pipe_operator.read(CODE_PACKER.size)
2026-01-08 20:04:24 +08:00
(code,) = CODE_PACKER.unpack(code_bytes)
2026-01-08 19:48:56 +08:00
# Analyse code
match ProtocolCode(code):
case ProtocolCode.DATA_READY:
# Receive data
data_receiver()
# Send data received symbol
2026-01-08 20:04:24 +08:00
self.__pipe_operator.write(
CODE_PACKER.pack(ProtocolCode.DATA_RECEIVED)
)
2026-01-08 19:48:56 +08:00
return False
case ProtocolCode.ACTIVELY_STOP:
# Presenter requested stop.
# Agree with it, send code and wait response
2026-01-08 20:25:33 +08:00
self.__pipe_operator.write(
CODE_PACKER.pack(ProtocolCode.STOP_REQUEST)
)
2026-01-08 19:48:56 +08:00
case ProtocolCode.STOP_RESPONSE:
# Set self status and return
self.__status = ServerStatus.Stop
2026-01-08 20:25:33 +08:00
return True
2026-01-08 19:48:56 +08:00
case _:
2026-01-08 20:25:33 +08:00
raise RuntimeError("unexpected protocol code when running")