Skip to content

Commit

Permalink
on_reconnect_callback --> on_connect_callback. (#2122)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Mar 27, 2024
1 parent 2c36fd3 commit 0803ff7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 2 additions & 0 deletions API_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ API changes 3.7.0
-----------------
- class method generate_ssl() added to TLS client (sync/async).
- removed certfile, keyfile, password from TLS client, please use generate_ssl()
- on_reconnect_callback() removed from clients (sync/async).
- on_connect_callback(true/false) added to async clients.


API changes 3.6.0
Expand Down
13 changes: 7 additions & 6 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]], ModbusProto
:param broadcast_enable: True to treat id 0 as broadcast address.
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param on_reconnect_callback: Function that will be called just before a reconnection attempt.
:param on_connect_callback: Will be called when connected/disconnected (bool parameter)
:param no_resend_on_retry: Do not resend request when retrying due to missing response.
:param kwargs: Experimental parameters.
Expand All @@ -56,7 +56,7 @@ def __init__(
broadcast_enable: bool = False,
reconnect_delay: float = 0.1,
reconnect_delay_max: float = 300,
on_reconnect_callback: Callable[[], None] | None = None,
on_connect_callback: Callable[[bool], None] | None = None,
no_resend_on_retry: bool = False,
**kwargs: Any,
) -> None:
Expand All @@ -82,7 +82,7 @@ def __init__(
),
False,
)
self.on_reconnect_callback = on_reconnect_callback
self.on_connect_callback = on_connect_callback
self.retry_on_empty: int = 0
self.no_resend_on_retry = no_resend_on_retry
self.slaves: list[int] = []
Expand Down Expand Up @@ -189,12 +189,14 @@ def callback_new_connection(self):

def callback_connected(self) -> None:
"""Call when connection is succcesfull."""
if self.on_reconnect_callback:
self.on_reconnect_callback()
if self.on_connect_callback:
self.loop.call_soon(self.on_connect_callback, True)

def callback_disconnected(self, exc: Exception | None) -> None:
"""Call when connection is lost."""
Log.debug("callback_disconnected called: {}", exc)
if self.on_connect_callback:
self.loop.call_soon(self.on_connect_callback, False)

def callback_data(self, data: bytes, addr: tuple | None = None) -> int:
"""Handle received data.
Expand Down Expand Up @@ -281,7 +283,6 @@ class ModbusBaseSyncClient(ModbusClientMixin[ModbusResponse]):
:param broadcast_enable: True to treat id 0 as broadcast address.
:param reconnect_delay: Minimum delay in seconds.milliseconds before reconnecting.
:param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting.
:param on_reconnect_callback: Function that will be called just before a reconnection attempt.
:param no_resend_on_retry: Do not resend request when retrying due to missing response.
:param kwargs: Experimental parameters.
Expand Down

0 comments on commit 0803ff7

Please sign in to comment.