-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_session.py
More file actions
189 lines (174 loc) · 6.33 KB
/
Copy pathserial_session.py
File metadata and controls
189 lines (174 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""Persistent TCP listener for Global Caché serial data ports."""
from __future__ import annotations
import asyncio
import logging
from collections.abc import Awaitable, Callable
from .client import ItachClient, ItachError, serial_data_port
_LOGGER = logging.getLogger(__name__)
OnSerialData = Callable[[str, bool], Awaitable[None]]
class SerialPortSession:
"""Maintain one serial bridge connection (control port + module) with RX loop."""
def __init__(
self,
client: ItachClient,
*,
serial_id: str,
module: int,
connector_port: int,
settings: str,
append_cr: bool,
connect_timeout: float,
command_timeout: float,
on_data: OnSerialData,
) -> None:
self.serial_id = serial_id
self._client = client
self._module = module
self._connector_port = connector_port
self._settings = settings.strip()
self._append_cr = append_cr
self._connect_timeout = connect_timeout
self._command_timeout = command_timeout
self._on_data = on_data
self._host = client._host
self._control_port = client._port
self._data_port = serial_data_port(self._control_port, module)
self._reader: asyncio.StreamReader | None = None
self._writer: asyncio.StreamWriter | None = None
self._read_task: asyncio.Task[None] | None = None
self._closed = asyncio.Event()
self._write_lock = asyncio.Lock()
self._rx_queue: asyncio.Queue[str] = asyncio.Queue()
self._expecting_response = asyncio.Event()
self._connected = False
self.last_received: str = ""
@property
def is_connected(self) -> bool:
return self._connected
async def start(self) -> None:
"""Connect, apply line settings, and start background RX."""
self._closed.clear()
await self._connect()
if self._read_task is None:
self._read_task = asyncio.create_task(self._read_loop())
async def stop(self) -> None:
self._closed.set()
if self._read_task:
self._read_task.cancel()
try:
await self._read_task
except asyncio.CancelledError:
pass
self._read_task = None
await self._close()
async def _connect(self) -> None:
if self._settings:
await self._client.set_serial_settings(
self._module, self._connector_port, self._settings
)
self._reader, self._writer = await asyncio.wait_for(
asyncio.open_connection(self._host, self._data_port),
timeout=self._connect_timeout,
)
self._connected = True
_LOGGER.debug(
"Serial session %s connected on %s:%s",
self.serial_id,
self._host,
self._data_port,
)
async def _close(self) -> None:
self._connected = False
w = self._writer
self._writer = None
self._reader = None
if w and not w.is_closing():
w.close()
try:
await w.wait_closed()
except OSError:
pass
async def _read_loop(self) -> None:
backoff = 0.5
try:
while not self._closed.is_set():
if not self._connected or self._reader is None:
try:
await self._close()
await self._connect()
backoff = 0.5
except (TimeoutError, OSError, ItachError) as err:
_LOGGER.debug(
"Serial %s reconnect failed: %s", self.serial_id, err
)
await asyncio.sleep(min(backoff, 6.0))
backoff = min(backoff * 2, 6.0)
continue
try:
chunk = await self._reader.read(4096)
except OSError:
chunk = b""
if not chunk:
_LOGGER.debug("Serial %s RX EOF", self.serial_id)
self._connected = False
await self._close()
await asyncio.sleep(backoff)
backoff = min(backoff * 2, 6.0)
continue
text = chunk.decode("ascii", errors="replace").strip()
if not text:
continue
self.last_received = text
if self._expecting_response.is_set():
await self._rx_queue.put(text)
else:
await self._on_data(text, False)
except asyncio.CancelledError:
raise
finally:
await self._close()
async def send(
self,
payload: str,
*,
append_cr: bool | None = None,
wait_response: bool = True,
response_timeout: float | None = None,
) -> str:
"""Send on the listener connection; optionally wait for the next RX chunk."""
use_cr = self._append_cr if append_cr is None else append_cr
wire = payload.encode("ascii", errors="replace")
if use_cr and not wire.endswith(b"\r"):
wire += b"\r"
if not self._connected:
await self._connect()
assert self._writer is not None
tout = (
response_timeout
if response_timeout is not None
else self._command_timeout
)
async with self._write_lock:
if wait_response:
while not self._rx_queue.empty():
try:
self._rx_queue.get_nowait()
except asyncio.QueueEmpty:
break
self._expecting_response.set()
try:
self._writer.write(wire)
await self._writer.drain()
finally:
if not wait_response:
self._expecting_response.clear()
if not wait_response:
return ""
try:
text = await asyncio.wait_for(self._rx_queue.get(), timeout=tout)
except TimeoutError:
return ""
finally:
self._expecting_response.clear()
await self._on_data(text, True)
return text