From 5b3d564e24a20c0fbb69bdc840254df9170fbaf5 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Fri, 19 Jul 2024 20:13:48 +0200 Subject: [PATCH] remove kwargs client. --- .gitignore | 1 + pymodbus/client/base.py | 69 ++++++++++++++++---------------- pymodbus/client/serial.py | 8 ++-- pymodbus/client/tcp.py | 10 ++--- pymodbus/client/tls.py | 8 +++- pymodbus/client/udp.py | 6 +-- test/framers/test_old_framers.py | 3 +- test/sub_client/test_client.py | 28 ++++++++----- 8 files changed, 74 insertions(+), 59 deletions(-) diff --git a/.gitignore b/.gitignore index c276f7914c..c2a1d0c336 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ prof/ /pymodbus.egg-info/ venv downloaded_files/ +pymodbus.log diff --git a/pymodbus/client/base.py b/pymodbus/client/base.py index 51602641b0..50cc81e417 100644 --- a/pymodbus/client/base.py +++ b/pymodbus/client/base.py @@ -37,7 +37,6 @@ class ModbusBaseClient(ModbusClientMixin[Awaitable[ModbusResponse]]): :param reconnect_delay_max: Maximum delay in seconds.milliseconds before reconnecting. :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 comm_type: Type of communication (set by interface class) :param kwargs: Experimental parameters. .. tip:: @@ -60,30 +59,30 @@ def __init__( # pylint: disable=too-many-arguments reconnect_delay_max: float = 300, on_connect_callback: Callable[[bool], None] | None = None, no_resend_on_retry: bool = False, - comm_type: CommType | None = None, source_address: tuple[str, int] | None = None, + + comm_params: CommParams | None = None, **kwargs: Any, ) -> None: """Initialize a client instance.""" ModbusClientMixin.__init__(self) # type: ignore[arg-type] + if comm_params: + self.comm_params = comm_params + self.comm_params.comm_name="comm" + self.comm_params.source_address=source_address + self.comm_params.reconnect_delay=reconnect_delay + self.comm_params.reconnect_delay_max=reconnect_delay_max + self.comm_params.timeout_connect=timeout + self.comm_params.host=kwargs.get("host", None) + self.comm_params.port=kwargs.get("port", 0) + self.comm_params.baudrate=kwargs.get("baudrate", None) + self.comm_params.bytesize=kwargs.get("bytesize", None) + self.comm_params.parity=kwargs.get("parity", None) + self.comm_params.stopbits=kwargs.get("stopbits", None) + self.comm_params.handle_local_echo=kwargs.get("handle_local_echo", False) self.ctx = ModbusClientProtocol( framer, - CommParams( - comm_type=comm_type, - comm_name="comm", - source_address=source_address, - reconnect_delay=reconnect_delay, - reconnect_delay_max=reconnect_delay_max, - timeout_connect=timeout, - host=kwargs.get("host", None), - port=kwargs.get("port", 0), - sslctx=kwargs.get("sslctx", None), - baudrate=kwargs.get("baudrate", None), - bytesize=kwargs.get("bytesize", None), - parity=kwargs.get("parity", None), - stopbits=kwargs.get("stopbits", None), - handle_local_echo=kwargs.get("handle_local_echo", False), - ), + self.comm_params, on_connect_callback, ) self.no_resend_on_retry = no_resend_on_retry @@ -276,26 +275,28 @@ def __init__( # pylint: disable=too-many-arguments no_resend_on_retry: bool = False, comm_type: CommType | None = None, source_address: tuple[str, int] | None = None, + comm_params: CommParams | None = None, **kwargs: Any, ) -> None: """Initialize a client instance.""" ModbusClientMixin.__init__(self) # type: ignore[arg-type] - self.comm_params = CommParams( - comm_type=comm_type, - comm_name="comm", - source_address=source_address, - reconnect_delay=reconnect_delay, - reconnect_delay_max=reconnect_delay_max, - timeout_connect=timeout, - host=kwargs.get("host", None), - port=kwargs.get("port", 0), - sslctx=kwargs.get("sslctx", None), - baudrate=kwargs.get("baudrate", None), - bytesize=kwargs.get("bytesize", None), - parity=kwargs.get("parity", None), - stopbits=kwargs.get("stopbits", None), - handle_local_echo=kwargs.get("handle_local_echo", False), - ) + if comm_params: + self.comm_params = comm_params + if comm_type: + self.comm_params.comm_type=comm_type + self.comm_params.comm_name="comm" + self.comm_params.source_address=source_address + self.comm_params.reconnect_delay=reconnect_delay + self.comm_params.reconnect_delay_max=reconnect_delay_max + self.comm_params.timeout_connect=timeout + self.comm_params.host=kwargs.get("host", None) + self.comm_params.port=kwargs.get("port", 0) + self.comm_params.sslctx=kwargs.get("sslctx", None) + self.comm_params.baudrate=kwargs.get("baudrate", None) + self.comm_params.bytesize=kwargs.get("bytesize", None) + self.comm_params.parity=kwargs.get("parity", None) + self.comm_params.stopbits=kwargs.get("stopbits", None) + self.comm_params.handle_local_echo=kwargs.get("handle_local_echo", False) self.params = self._params() self.params.retries = int(retries) self.params.retry_on_empty = bool(retry_on_empty) diff --git a/pymodbus/client/serial.py b/pymodbus/client/serial.py index 2972bcde0e..2d4fb1c8f8 100644 --- a/pymodbus/client/serial.py +++ b/pymodbus/client/serial.py @@ -9,7 +9,7 @@ from pymodbus.exceptions import ConnectionException from pymodbus.framer import FramerType from pymodbus.logging import Log -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType from pymodbus.utilities import ModbusTransactionState @@ -81,10 +81,10 @@ def __init__( "Serial client requires pyserial " 'Please install with "pip install pyserial" and try again.' ) + self.comm_params = CommParams(comm_type=CommType.SERIAL) ModbusBaseClient.__init__( self, framer, - comm_type=CommType.SERIAL, host=port, baudrate=baudrate, bytesize=bytesize, @@ -159,9 +159,9 @@ def __init__( **kwargs: Any, ) -> None: """Initialize Modbus Serial Client.""" + self.comm_params = CommParams(comm_type=CommType.SERIAL) super().__init__( framer, - comm_type=CommType.SERIAL, host=port, baudrate=baudrate, bytesize=bytesize, @@ -171,9 +171,7 @@ def __init__( ) self.socket: serial.Serial | None = None self.strict = bool(strict) - self.last_frame_end = None - self._t0 = float(1 + bytesize + stopbits) / baudrate # Check every 4 bytes / 2 registers if the reading is ready diff --git a/pymodbus/client/tcp.py b/pymodbus/client/tcp.py index e6ff7bf1f3..008140f118 100644 --- a/pymodbus/client/tcp.py +++ b/pymodbus/client/tcp.py @@ -10,7 +10,7 @@ from pymodbus.exceptions import ConnectionException from pymodbus.framer import FramerType from pymodbus.logging import Log -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType class AsyncModbusTcpClient(ModbusBaseClient): @@ -63,8 +63,8 @@ def __init__( **kwargs: Any, ) -> None: """Initialize Asyncio Modbus TCP Client.""" - if "comm_type" not in kwargs: - kwargs["comm_type"] = CommType.TCP + if not hasattr(self,"comm_params"): + self.comm_params = CommParams(comm_type=CommType.TCP) if source_address: kwargs["source_address"] = source_address ModbusBaseClient.__init__( @@ -132,8 +132,8 @@ def __init__( **kwargs: Any, ) -> None: """Initialize Modbus TCP Client.""" - if "comm_type" not in kwargs: - kwargs["comm_type"] = CommType.TCP + if not hasattr(self,"comm_params"): + self.comm_params = CommParams(comm_type=CommType.TCP) super().__init__( framer, host=host, diff --git a/pymodbus/client/tls.py b/pymodbus/client/tls.py index e906400049..a7db9bec6b 100644 --- a/pymodbus/client/tls.py +++ b/pymodbus/client/tls.py @@ -62,12 +62,12 @@ def __init__( **kwargs: Any, ): """Initialize Asyncio Modbus TLS Client.""" + self.comm_params = CommParams(comm_type=CommType.TLS) AsyncModbusTcpClient.__init__( self, host, port=port, framer=framer, - comm_type=CommType.TLS, sslctx=sslctx, **kwargs, ) @@ -149,8 +149,12 @@ def __init__( **kwargs: Any, ): """Initialize Modbus TLS Client.""" + self.comm_params = CommParams( + comm_type=CommType.TLS, + sslctx=sslctx, + ) super().__init__( - host, comm_type=CommType.TLS, port=port, framer=framer, **kwargs + host, port=port, framer=framer, **kwargs ) self.sslctx = sslctx self.server_hostname = server_hostname diff --git a/pymodbus/client/udp.py b/pymodbus/client/udp.py index 4d71e240e4..6ab0f93c00 100644 --- a/pymodbus/client/udp.py +++ b/pymodbus/client/udp.py @@ -8,7 +8,7 @@ from pymodbus.exceptions import ConnectionException from pymodbus.framer import FramerType from pymodbus.logging import Log -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType DGRAM_TYPE = socket.SOCK_DGRAM @@ -62,10 +62,10 @@ def __init__( **kwargs: Any, ) -> None: """Initialize Asyncio Modbus UDP Client.""" + self.comm_params = CommParams(comm_type=CommType.UDP) ModbusBaseClient.__init__( self, framer, - comm_type=CommType.UDP, host=host, port=port, **kwargs, @@ -130,11 +130,11 @@ def __init__( **kwargs: Any, ) -> None: """Initialize Modbus UDP Client.""" + self.comm_params = CommParams(comm_type=CommType.UDP) super().__init__( framer, port=port, host=host, - comm_type=CommType.UDP, **kwargs, ) self.params.source_address = source_address diff --git a/test/framers/test_old_framers.py b/test/framers/test_old_framers.py index 39bc5035b0..2690fe8693 100644 --- a/test/framers/test_old_framers.py +++ b/test/framers/test_old_framers.py @@ -14,7 +14,7 @@ ModbusTlsFramer, ) from pymodbus.pdu.bit_read_message import ReadCoilsRequest -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType from pymodbus.utilities import ModbusTransactionState @@ -330,6 +330,7 @@ async def test_send_packet(self, rtu_framer): host="localhost", port=BASE_PORT + 1, CommType=CommType.TCP, + comm_params=CommParams(), ) client.state = ModbusTransactionState.TRANSACTION_COMPLETE client.silent_interval = 1 diff --git a/test/sub_client/test_client.py b/test/sub_client/test_client.py index ec9405009f..d17b3a820b 100755 --- a/test/sub_client/test_client.py +++ b/test/sub_client/test_client.py @@ -22,7 +22,7 @@ from pymodbus.datastore.store import ModbusSequentialDataBlock from pymodbus.exceptions import ConnectionException, ModbusException, ModbusIOException from pymodbus.pdu import ModbusRequest -from pymodbus.transport import CommType +from pymodbus.transport import CommParams, CommType BASE_PORT = 6500 @@ -281,6 +281,7 @@ async def test_client_modbusbaseclient(): host="localhost", port=BASE_PORT + 1, CommType=CommType.TCP, + comm_params=CommParams(), ) client.register(pdu_bit_read.ReadCoilsResponse) assert str(client) @@ -316,6 +317,7 @@ async def test_client_base_async(): host="localhost", port=BASE_PORT + 2, CommType=CommType.TCP, + comm_params=CommParams(), ) as client: str(client) p_connect.return_value = asyncio.Future() @@ -327,7 +329,8 @@ async def test_client_base_async(): @pytest.mark.skip() async def test_client_protocol_receiver(): """Test the client protocol data received.""" - base = ModbusBaseClient(FramerType.SOCKET) + base = ModbusBaseClient(FramerType.SOCKET, comm_params=CommParams(), +) transport = mock.MagicMock() base.ctx.connection_made(transport) assert base.transport == transport @@ -349,7 +352,8 @@ async def test_client_protocol_receiver(): @pytest.mark.skip() async def test_client_protocol_response(): """Test the udp client protocol builds responses.""" - base = ModbusBaseClient(FramerType.SOCKET) + base = ModbusBaseClient(FramerType.SOCKET, comm_params=CommParams(), +) response = base.build_response(0x00) # pylint: disable=protected-access excp = response.exception() assert isinstance(excp, ConnectionException) @@ -363,7 +367,8 @@ async def test_client_protocol_response(): async def test_client_protocol_handler(): """Test the client protocol handles responses.""" base = ModbusBaseClient( - FramerType.ASCII, host="localhost", port=+3, CommType=CommType.TCP + FramerType.ASCII, host="localhost", port=+3, CommType=CommType.TCP, comm_params=CommParams(), + ) transport = mock.MagicMock() base.ctx.connection_made(transport=transport) @@ -411,7 +416,8 @@ def close(self): async def test_client_protocol_execute(): """Test the client protocol execute method.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1") + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", comm_params=CommParams(), +) request = pdu_bit_read.ReadCoilsRequest(1, 1) transport = MockTransport(base, request) base.ctx.connection_made(transport=transport) @@ -422,7 +428,8 @@ async def test_client_protocol_execute(): async def test_client_execute_broadcast(): """Test the client protocol execute method.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1") + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", comm_params=CommParams(), +) base.broadcast_enable = True request = pdu_bit_read.ReadCoilsRequest(1, 1) transport = MockTransport(base, request) @@ -432,7 +439,8 @@ async def test_client_execute_broadcast(): async def test_client_protocol_retry(): """Test the client protocol execute method with retries.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1) + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, comm_params=CommParams(), +) request = pdu_bit_read.ReadCoilsRequest(1, 1) transport = MockTransport(base, request, retries=2) base.ctx.connection_made(transport=transport) @@ -445,7 +453,8 @@ async def test_client_protocol_retry(): async def test_client_protocol_timeout(): """Test the client protocol execute method with timeout.""" - base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, retries=2) + base = ModbusBaseClient(FramerType.SOCKET, host="127.0.0.1", timeout=0.1, retries=2, comm_params=CommParams(), +) # Avoid creating do_reconnect() task base.ctx.connection_lost = mock.MagicMock() request = pdu_bit_read.ReadCoilsRequest(1, 1) @@ -645,7 +654,8 @@ def test_client_mixin_convert_fail(): async def test_client_build_response(): """Test fail of build_response.""" - client = ModbusBaseClient(FramerType.RTU) + client = ModbusBaseClient(FramerType.RTU, comm_params=CommParams(), +) with pytest.raises(ConnectionException): await client.build_response(ModbusRequest(0, 0, 0, False))