|
| 1 | +"""Mock connection handler for PSLab. |
| 2 | +
|
| 3 | +This module provides a minimal in-memory `ConnectionHandler` implementation for |
| 4 | +use in tests and development without physical PSLab hardware. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | +from collections import deque |
| 9 | +import pslab.protocol as CP |
| 10 | +from pslab.connection.connection import ConnectionHandler |
| 11 | + |
| 12 | + |
| 13 | +class MockHandler(ConnectionHandler): |
| 14 | + """In-memory mock implementation of `ConnectionHandler`. |
| 15 | +
|
| 16 | + The handler queues deterministic responses based on bytes written via |
| 17 | + `write()` so higher-level code can be exercised without an actual device. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, version: str = "PSLab V6 ", fw=(3, 0, 0)) -> None: |
| 21 | + self._rx = deque() # bytes to be read |
| 22 | + self._tx = bytearray() # bytes written by client |
| 23 | + self.version = version # convenient attribute for callers |
| 24 | + self._fw = fw |
| 25 | + self._connected = False |
| 26 | + |
| 27 | + def connect(self) -> None: |
| 28 | + """Mark the handler as connected.""" |
| 29 | + self._connected = True |
| 30 | + # Optional: validate the mock “device” by answering get_version |
| 31 | + # self.version = self.get_version() |
| 32 | + |
| 33 | + def disconnect(self) -> None: |
| 34 | + """Mark the handler as disconnected.""" |
| 35 | + self._connected = False |
| 36 | + |
| 37 | + def read(self, numbytes: int) -> bytes: |
| 38 | + """Read bytes from the internal receive buffer. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + numbytes : int |
| 43 | + Number of bytes to read. |
| 44 | +
|
| 45 | + Returns |
| 46 | + ------- |
| 47 | + bytes |
| 48 | + Bytes read from the receive buffer (may be shorter if insufficient data |
| 49 | + is available). |
| 50 | + """ |
| 51 | + out = bytearray() |
| 52 | + while len(out) < numbytes and self._rx: |
| 53 | + out.append(self._rx.popleft()) |
| 54 | + return bytes(out) |
| 55 | + |
| 56 | + def write(self, data: bytes) -> int: |
| 57 | + """Write bytes to the handler and queue any corresponding responses. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + data : bytes |
| 62 | + Bytes written by the caller. |
| 63 | +
|
| 64 | + Returns |
| 65 | + ------- |
| 66 | + int |
| 67 | + Number of bytes written. |
| 68 | + """ |
| 69 | + self._tx.extend(data) |
| 70 | + self._maybe_respond() |
| 71 | + return len(data) |
| 72 | + |
| 73 | + def _queue(self, payload: bytes) -> None: |
| 74 | + """Append bytes to the internal receive buffer. |
| 75 | +
|
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + payload : bytes |
| 79 | + Bytes to enqueue so they can be returned by `read()`. |
| 80 | + """ |
| 81 | + self._rx.extend(payload) |
| 82 | + |
| 83 | + def _maybe_respond(self) -> None: |
| 84 | + """Inspect written bytes and enqueue protocol responses. |
| 85 | +
|
| 86 | + This method implements minimal protocol handling for mock mode. |
| 87 | + When known command patterns are detected in the transmit buffer, |
| 88 | + corresponding response bytes are queued for later reads. |
| 89 | + """ |
| 90 | + # Detect “CP.COMMON, <cmd>” patterns |
| 91 | + while len(self._tx) >= 2: |
| 92 | + if self._tx[0] != CP.COMMON: |
| 93 | + # Drop unknown leading bytes |
| 94 | + self._tx.pop(0) |
| 95 | + continue |
| 96 | + |
| 97 | + cmd = self._tx[1] |
| 98 | + |
| 99 | + # GET_VERSION: ConnectionHandler.get_version reads 9 bytes |
| 100 | + # and checks b"PSLab" |
| 101 | + if cmd == CP.GET_VERSION: |
| 102 | + self._tx = self._tx[2:] |
| 103 | + self._queue(self.version.encode("utf-8")[:9].ljust(9, b" ")) |
| 104 | + continue |
| 105 | + |
| 106 | + # GET_FW_VERSION: reads 3 bytes (major, minor, patch) |
| 107 | + if cmd == CP.GET_FW_VERSION: |
| 108 | + self._tx = self._tx[2:] |
| 109 | + major, minor, patch = self._fw |
| 110 | + self._queue(bytes([major, minor, patch])) |
| 111 | + continue |
| 112 | + |
| 113 | + # Unknown command under CP.COMMON: drop and stop |
| 114 | + break |
0 commit comments