From 8afa208fe9fc5b3fe221aa7f1d50a17825205f0f Mon Sep 17 00:00:00 2001 From: jan iversen Date: Tue, 18 Apr 2023 22:19:04 +0200 Subject: [PATCH] ypy.m --- pymodbus/transport/base.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/pymodbus/transport/base.py b/pymodbus/transport/base.py index 255bbc9d83..8fe63337ab 100644 --- a/pymodbus/transport/base.py +++ b/pymodbus/transport/base.py @@ -1,8 +1,7 @@ """Base for all transport types.""" from __future__ import annotations -from abc import abstractmethod -from typing import Any +from typing import Any, Callable from pymodbus.framer import ModbusFramer from pymodbus.logging import Log @@ -20,6 +19,8 @@ class BaseTransport: :param retry_on_empty: retry read on nothing :param timeout_connect: Max. time in milliseconds for connect to complete :param timeout_comm: Max. time in milliseconds for recv/send to complete + :param on_connection_made: callback + :param on_connection_lost: callback :property reconnect_delay_current: current delay in milliseconds for next reconnect (doubles with every try) :property transport: current transport class (none if not connected) @@ -29,7 +30,7 @@ class BaseTransport: This class is not available in the pymodbus API, and should not be referenced in Applications. """ - def __init__( + def __init__( # pylint: disable=too-many-arguments self, framer: ModbusFramer, slaves: list[int], @@ -40,6 +41,8 @@ def __init__( retry_on_empty: bool = False, timeout_connect: int = 10, timeout_comm: int = 5, + on_connection_made: Callable[[str], None] | None = None, + on_connection_lost: Callable[[str, Exception], None] | None = None, ) -> None: """Initialize a transport instance.""" # parameter variables @@ -52,6 +55,8 @@ def __init__( self.retry_on_empty = retry_on_empty self.timeout_connect = timeout_connect self.timeout_comm = timeout_comm + self.on_connection_made = on_connection_made + self.on_connection_lost = on_connection_lost # local variables self.reconnect_delay_current: int = 0 @@ -67,7 +72,8 @@ def connection_made(self, transport: Any): """ self.transport = transport Log.debug("Connected {}", self.comm_name) - self.cb_connection_made() + if self.on_connection_made: + self.on_connection_made(self.comm_name) def connection_lost(self, reason: Exception): """Call from asyncio, when the connection is lost or closed. @@ -76,7 +82,8 @@ def connection_lost(self, reason: Exception): """ self.transport = None Log.debug("Connection lost {} due to {}", self.comm_name, reason) - self.cb_connection_lost(reason) + if self.on_connection_lost: + self.on_connection_lost(self.comm_name, reason) def data_received(self, data: bytes): """Call when some data is received. @@ -97,17 +104,6 @@ def send(self, data: bytes) -> bool: def close(self) -> None: """Close the underlying connection.""" - # -------------------------- # - # Transport callback methods # - # -------------------------- # - @abstractmethod - def cb_connection_made(self) -> bool: - """Handle low level.""" - - @abstractmethod - def cb_connection_lost(self, _reason) -> bool: - """Handle low level.""" - # ----------------------------------------------------------------------- # # The magic methods # ----------------------------------------------------------------------- #