Skip to content

Commit 38833a8

Browse files
committed
ypy.m
1 parent 758003d commit 38833a8

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

pymodbus/transport/base.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""Base for all transport types."""
22
from __future__ import annotations
33

4-
from abc import abstractmethod
5-
from typing import Any
4+
from typing import Any, Callable
65

76
from pymodbus.framer import ModbusFramer
87
from pymodbus.logging import Log
@@ -20,6 +19,8 @@ class BaseTransport:
2019
:param retry_on_empty: retry read on nothing
2120
:param timeout_connect: Max. time in milliseconds for connect to complete
2221
:param timeout_comm: Max. time in milliseconds for recv/send to complete
22+
:param on_connection_made: callback
23+
:param on_connection_lost: callback
2324
2425
:property reconnect_delay_current: current delay in milliseconds for next reconnect (doubles with every try)
2526
:property transport: current transport class (none if not connected)
@@ -29,7 +30,7 @@ class BaseTransport:
2930
This class is not available in the pymodbus API, and should not be referenced in Applications.
3031
"""
3132

32-
def __init__(
33+
def __init__( # pylint: disable=too-many-arguments
3334
self,
3435
framer: ModbusFramer,
3536
slaves: list[int],
@@ -40,6 +41,8 @@ def __init__(
4041
retry_on_empty: bool = False,
4142
timeout_connect: int = 10,
4243
timeout_comm: int = 5,
44+
on_connection_made: Callable[[str], None] | None = None,
45+
on_connection_lost: Callable[[str, Exception], None] | None = None,
4346
) -> None:
4447
"""Initialize a transport instance."""
4548
# parameter variables
@@ -52,6 +55,8 @@ def __init__(
5255
self.retry_on_empty = retry_on_empty
5356
self.timeout_connect = timeout_connect
5457
self.timeout_comm = timeout_comm
58+
self.on_connection_made = on_connection_made
59+
self.on_connection_lost = on_connection_lost
5560

5661
# local variables
5762
self.reconnect_delay_current: int = 0
@@ -67,7 +72,8 @@ def connection_made(self, transport: Any):
6772
"""
6873
self.transport = transport
6974
Log.debug("Connected {}", self.comm_name)
70-
self.cb_connection_made()
75+
if self.on_connection_made:
76+
self.on_connection_made(self.comm_name)
7177

7278
def connection_lost(self, reason: Exception):
7379
"""Call from asyncio, when the connection is lost or closed.
@@ -76,7 +82,8 @@ def connection_lost(self, reason: Exception):
7682
"""
7783
self.transport = None
7884
Log.debug("Connection lost {} due to {}", self.comm_name, reason)
79-
self.cb_connection_lost(reason)
85+
if self.on_connection_lost:
86+
self.on_connection_lost(self.comm_name, reason)
8087

8188
def data_received(self, data: bytes):
8289
"""Call when some data is received.
@@ -97,17 +104,6 @@ def send(self, data: bytes) -> bool:
97104
def close(self) -> None:
98105
"""Close the underlying connection."""
99106

100-
# -------------------------- #
101-
# Transport callback methods #
102-
# -------------------------- #
103-
@abstractmethod
104-
def cb_connection_made(self) -> bool:
105-
"""Handle low level."""
106-
107-
@abstractmethod
108-
def cb_connection_lost(self, _reason) -> bool:
109-
"""Handle low level."""
110-
111107
# ----------------------------------------------------------------------- #
112108
# The magic methods
113109
# ----------------------------------------------------------------------- #

0 commit comments

Comments
 (0)