Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_reconnect_callback removed, on_connect_callback added. #2122

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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