|
| 1 | +"""Wireless interface for communicating with PSLab devices equiped with ESP8266.""" |
| 2 | + |
| 3 | +import socket |
| 4 | + |
| 5 | +from pslab.connection.connection import ConnectionHandler |
| 6 | + |
| 7 | + |
| 8 | +class WLANHandler(ConnectionHandler): |
| 9 | + """Interface for controlling a PSLab over WLAN. |
| 10 | +
|
| 11 | + Paramaters |
| 12 | + ---------- |
| 13 | + host : str, default 192.168.4.1 |
| 14 | + Network address of the PSLab. |
| 15 | + port : int, default 80 |
| 16 | + timeout : float, default 1 s |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + host: str = "192.168.4.1", |
| 22 | + port: int = 80, |
| 23 | + timeout: float = 1.0, |
| 24 | + ) -> None: |
| 25 | + self._host = host |
| 26 | + self._port = port |
| 27 | + self._timeout = timeout |
| 28 | + self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 29 | + self._sock.settimeout(timeout) |
| 30 | + |
| 31 | + @property |
| 32 | + def host(self) -> int: |
| 33 | + """Network address of the PSLab.""" |
| 34 | + return self._host |
| 35 | + |
| 36 | + @property |
| 37 | + def port(self) -> int: |
| 38 | + """TCP port number.""" |
| 39 | + return self._port |
| 40 | + |
| 41 | + @property |
| 42 | + def timeout(self) -> float: |
| 43 | + """Timeout in seconds.""" |
| 44 | + return self._timeout |
| 45 | + |
| 46 | + @timeout.setter |
| 47 | + def timeout(self, value: float) -> None: |
| 48 | + self._sock.settimeout(value) |
| 49 | + |
| 50 | + def connect(self) -> None: |
| 51 | + """Connect to PSLab.""" |
| 52 | + if self._sock.fileno() == -1: |
| 53 | + # Socket has been closed. |
| 54 | + self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 55 | + self._sock.settimeout(self.timeout) |
| 56 | + |
| 57 | + self._sock.connect((self.host, self.port)) |
| 58 | + |
| 59 | + try: |
| 60 | + self.get_version() |
| 61 | + except Exception: |
| 62 | + self._sock.close() |
| 63 | + raise |
| 64 | + |
| 65 | + def disconnect(self) -> None: |
| 66 | + """Disconnect from PSLab.""" |
| 67 | + self._sock.close() |
| 68 | + |
| 69 | + def read(self, numbytes: int) -> bytes: |
| 70 | + """Read data over WLAN. |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + numbytes : int |
| 75 | + Number of bytes to read. |
| 76 | +
|
| 77 | + Returns |
| 78 | + ------- |
| 79 | + data : bytes |
| 80 | + """ |
| 81 | + received = b"" |
| 82 | + buf_size = 4096 |
| 83 | + remaining = numbytes |
| 84 | + |
| 85 | + while remaining > 0: |
| 86 | + chunk = self._sock.recv(min(remaining, buf_size)) |
| 87 | + received += chunk |
| 88 | + remaining -= len(chunk) |
| 89 | + |
| 90 | + return received |
| 91 | + |
| 92 | + def write(self, data: bytes) -> int: |
| 93 | + """Write data over WLAN. |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + data : bytes |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + numbytes : int |
| 102 | + Number of bytes written. |
| 103 | + """ |
| 104 | + return self._sock.sendall(data) |
| 105 | + |
| 106 | + def __repr__(self) -> str: # noqa |
| 107 | + return ( |
| 108 | + f"{self.__class__.__name__}" |
| 109 | + "[" |
| 110 | + f"{self.host}:{self.port}, " |
| 111 | + f"timeout {self.timeout} s" |
| 112 | + "]" |
| 113 | + ) |
0 commit comments