You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2024/03/02 05:07:04 UTC

(plc4x) branch fix/plc4py/documentation updated (2abc6c39a1 -> 35439595b9)

This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a change to branch fix/plc4py/documentation
in repository https://gitbox.apache.org/repos/asf/plc4x.git


    from 2abc6c39a1 fix(plc4py): Add write support to the api
     new 219759466c fix(plc4py): Add write support to the api
     new 35439595b9 fix(plc4py): pytest-asyncio not installed and markers not added to tests

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 sandbox/plc4py/plc4py/api/PlcConnection.py            | 17 ++++++++++++-----
 sandbox/plc4py/plc4py/drivers/mock/MockConnection.py  |  9 +++++++--
 .../plc4py/plc4py/drivers/modbus/ModbusConnection.py  | 17 +++++++++++------
 sandbox/plc4py/plc4py/spi/Plc4xBaseProtocol.py        |  4 ++--
 sandbox/plc4py/pytest.ini                             | 19 +++++++++++++++++++
 sandbox/plc4py/setup.py                               |  1 +
 sandbox/plc4py/tests/test_plc4py.py                   |  8 +++++++-
 .../unit/plc4py/drivers/modbus/test_modbus_codegen.py |  4 ++++
 .../plc4py/drivers/modbus/test_modbus_connection.py   |  6 +++++-
 .../unit/plc4py/drivers/umas/test_umas_connection.py  |  9 +++++++--
 sandbox/plc4py/tests/unit/plc4py/spi/tcp/server.py    |  1 -
 sandbox/plc4py/tests/unit/plc4py/spi/test_protocol.py |  1 +
 .../plc4py/tests/unit/plc4py/spi/test_transport.py    |  2 ++
 13 files changed, 78 insertions(+), 20 deletions(-)


(plc4x) 02/02: fix(plc4py): pytest-asyncio not installed and markers not added to tests

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a commit to branch fix/plc4py/documentation
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 35439595b93165fb5e320581eea19f0191fb6007
Author: Ben Hutcheson <be...@gmail.com>
AuthorDate: Sat Mar 2 06:06:52 2024 +0100

    fix(plc4py): pytest-asyncio not installed and markers not added to tests
---
 sandbox/plc4py/plc4py/api/PlcConnection.py            |  9 +++++++--
 sandbox/plc4py/plc4py/drivers/mock/MockConnection.py  |  9 +++++++--
 sandbox/plc4py/plc4py/spi/Plc4xBaseProtocol.py        |  4 ++--
 sandbox/plc4py/pytest.ini                             | 19 +++++++++++++++++++
 sandbox/plc4py/setup.py                               |  1 +
 sandbox/plc4py/tests/test_plc4py.py                   |  8 +++++++-
 .../unit/plc4py/drivers/modbus/test_modbus_codegen.py |  4 ++++
 .../plc4py/drivers/modbus/test_modbus_connection.py   |  6 +++++-
 .../unit/plc4py/drivers/umas/test_umas_connection.py  |  9 +++++++--
 sandbox/plc4py/tests/unit/plc4py/spi/tcp/server.py    |  1 -
 sandbox/plc4py/tests/unit/plc4py/spi/test_protocol.py |  1 +
 .../plc4py/tests/unit/plc4py/spi/test_transport.py    |  2 ++
 12 files changed, 62 insertions(+), 11 deletions(-)

diff --git a/sandbox/plc4py/plc4py/api/PlcConnection.py b/sandbox/plc4py/plc4py/api/PlcConnection.py
index 62b48be7e4..a4ce8f3a42 100644
--- a/sandbox/plc4py/plc4py/api/PlcConnection.py
+++ b/sandbox/plc4py/plc4py/api/PlcConnection.py
@@ -20,8 +20,13 @@ import asyncio
 from abc import abstractmethod
 from typing import Awaitable, Dict, Union
 
-from plc4py.api.messages.PlcResponse import PlcResponse, PlcTagResponse, PlcReadResponse, PlcWriteResponse, \
-    PlcBrowseResponse
+from plc4py.api.messages.PlcResponse import (
+    PlcResponse,
+    PlcTagResponse,
+    PlcReadResponse,
+    PlcWriteResponse,
+    PlcBrowseResponse,
+)
 from plc4py.api.messages.PlcRequest import ReadRequestBuilder, PlcRequest
 from plc4py.api.value.PlcValue import PlcResponseCode, PlcValue
 from plc4py.spi.configuration.PlcConfiguration import PlcConfiguration
diff --git a/sandbox/plc4py/plc4py/drivers/mock/MockConnection.py b/sandbox/plc4py/plc4py/drivers/mock/MockConnection.py
index 64e9bdd00f..cbf954b020 100644
--- a/sandbox/plc4py/plc4py/drivers/mock/MockConnection.py
+++ b/sandbox/plc4py/plc4py/drivers/mock/MockConnection.py
@@ -38,9 +38,14 @@ from plc4py.api.messages.PlcField import PlcTag
 from plc4py.api.messages.PlcRequest import (
     ReadRequestBuilder,
     PlcReadRequest,
-    PlcRequest, PlcWriteRequest,
+    PlcRequest,
+    PlcWriteRequest,
+)
+from plc4py.api.messages.PlcResponse import (
+    PlcReadResponse,
+    PlcResponse,
+    PlcWriteResponse,
 )
-from plc4py.api.messages.PlcResponse import PlcReadResponse, PlcResponse, PlcWriteResponse
 from plc4py.api.value.PlcValue import PlcResponseCode, PlcValue
 from plc4py.drivers.PlcDriverLoader import PlcDriverLoader
 from plc4py.spi.messages.PlcReader import PlcReader
diff --git a/sandbox/plc4py/plc4py/spi/Plc4xBaseProtocol.py b/sandbox/plc4py/plc4py/spi/Plc4xBaseProtocol.py
index 916d420a9b..bf9d4115c0 100644
--- a/sandbox/plc4py/plc4py/spi/Plc4xBaseProtocol.py
+++ b/sandbox/plc4py/plc4py/spi/Plc4xBaseProtocol.py
@@ -17,7 +17,7 @@
 # under the License.
 #
 
-from asyncio import Protocol, Future
+from asyncio import Protocol, Future, BaseProtocol
 from dataclasses import dataclass
 from typing import Union
 
@@ -30,7 +30,7 @@ class Plc4xBaseProtocol(Protocol):
     def data_received(self, data) -> None:
         self.handler.set_result(data)
 
-    def connection_made(self):
+    def connection_made(self, transport):
         self.connected = True
 
     def connection_lost(self, exc: Union[Exception, None]) -> None:
diff --git a/sandbox/plc4py/pytest.ini b/sandbox/plc4py/pytest.ini
index 039d0a2fbd..537563cdb1 100644
--- a/sandbox/plc4py/pytest.ini
+++ b/sandbox/plc4py/pytest.ini
@@ -1,2 +1,21 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
 [pytest]
 log_cli = true
\ No newline at end of file
diff --git a/sandbox/plc4py/setup.py b/sandbox/plc4py/setup.py
index 81548a5668..af8ab71600 100644
--- a/sandbox/plc4py/setup.py
+++ b/sandbox/plc4py/setup.py
@@ -54,6 +54,7 @@ setup(
             "mock>=4.0.2",
             "mypy>=0.942",
             "flake8>=4.0.1",
+            "pytest-asyncio",
         ]
     },
     entry_points={
diff --git a/sandbox/plc4py/tests/test_plc4py.py b/sandbox/plc4py/tests/test_plc4py.py
index f53bf15503..2ed6cb37c0 100644
--- a/sandbox/plc4py/tests/test_plc4py.py
+++ b/sandbox/plc4py/tests/test_plc4py.py
@@ -18,6 +18,8 @@
 #
 from typing import cast
 
+import pytest
+
 from plc4py import __version__
 from plc4py.PlcDriverManager import PlcDriverManager
 from plc4py.api.PlcConnection import PlcConnection
@@ -32,24 +34,28 @@ def test_version():
     assert __version__ == "0.1.0"
 
 
+@pytest.mark.asyncio
 async def test_plc_driver_manager_init():
     driver_manager = PlcDriverManager()
     async with driver_manager.connection("mock:tcp://127.0.0.1:502") as connection:
         assert isinstance(connection, PlcConnection)
 
 
+@pytest.mark.asyncio
 async def manual_test_plc_driver_manager_init_modbus():
     driver_manager = PlcDriverManager()
     async with driver_manager.connection("nodbus:tcp://127.0.0.1:502") as connection:
         assert isinstance(connection, ModbusConnection)
 
 
+@pytest.mark.asyncio
 async def test_plc_driver_manager_init_mock():
     driver_manager = PlcDriverManager()
     async with driver_manager.connection("mock:tcp://127.0.0.1:502") as connection:
         assert isinstance(connection, MockConnection)
 
 
+@pytest.mark.asyncio
 async def test_plc_driver_manager_init_mock_read_request():
     driver_manager = PlcDriverManager()
     tag = "1:BOOL"
@@ -63,4 +69,4 @@ async def test_plc_driver_manager_init_mock_read_request():
             )
 
     # verify that request has one field
-    assert response.code == PlcResponseCode.OK
+    assert response.response_code == PlcResponseCode.OK
diff --git a/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_codegen.py b/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_codegen.py
index 70d9040e9e..3b881b3b40 100644
--- a/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_codegen.py
+++ b/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_codegen.py
@@ -16,6 +16,7 @@
 # specific language governing permissions and limitations
 # under the License.
 #
+import pytest
 
 from plc4py.protocols.modbus.readwrite.ModbusTcpADU import ModbusTcpADUBuilder
 from plc4py.spi.generation.WriteBuffer import WriteBufferByteBased
@@ -26,11 +27,13 @@ from plc4py.protocols.modbus.readwrite.ModbusPDUReadDiscreteInputsRequest import
 from plc4py.utils.GenericTypes import ByteOrder
 
 
+@pytest.mark.asyncio
 async def test_modbus_discrete_inputs_request():
     request = ModbusPDUReadDiscreteInputsRequestBuilder(0, 10).build()
     assert request is not None
 
 
+@pytest.mark.asyncio
 async def test_modbus_discrete_inputs_request_serialize():
     request = ModbusPDUReadDiscreteInputsRequestBuilder(5, 2).build()
     size = request.length_in_bytes()
@@ -44,6 +47,7 @@ async def test_modbus_discrete_inputs_request_serialize():
     assert write_buffer.get_bytes().tobytes() == b"\x02\x00\x05\x00\x02"
 
 
+@pytest.mark.asyncio
 async def test_modbus_ModbusTcpADUBuilder_serialize():
     pdu = ModbusPDUReadDiscreteInputsRequestBuilder(5, 2).build()
     request = ModbusTcpADUBuilder(10, 5, pdu).build(False)
diff --git a/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py b/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py
index 04daefe7ea..ce8b5fdad5 100644
--- a/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py
+++ b/sandbox/plc4py/tests/unit/plc4py/drivers/modbus/test_modbus_connection.py
@@ -18,6 +18,8 @@
 #
 import time
 
+import pytest
+
 from plc4py.PlcDriverManager import PlcDriverManager
 from plc4py.api.value.PlcValue import PlcResponseCode
 import logging
@@ -25,6 +27,7 @@ import logging
 logger = logging.getLogger("testing")
 
 
+@pytest.mark.asyncio
 async def manual_test_plc_driver_modbus_connect():
     driver_manager = PlcDriverManager()
     async with driver_manager.connection("modbus://127.0.0.1:5555") as connection:
@@ -32,7 +35,8 @@ async def manual_test_plc_driver_modbus_connect():
     assert not connection.is_connected()
 
 
-async def test_plc_driver_modbus_read():
+@pytest.mark.asyncio
+async def manual_test_plc_driver_modbus_read():
     driver_manager = PlcDriverManager()
     async with driver_manager.connection("modbus://192.168.1.177:502") as connection:
         with connection.read_request_builder() as builder:
diff --git a/sandbox/plc4py/tests/unit/plc4py/drivers/umas/test_umas_connection.py b/sandbox/plc4py/tests/unit/plc4py/drivers/umas/test_umas_connection.py
index 94ac19349f..9ca9263282 100644
--- a/sandbox/plc4py/tests/unit/plc4py/drivers/umas/test_umas_connection.py
+++ b/sandbox/plc4py/tests/unit/plc4py/drivers/umas/test_umas_connection.py
@@ -20,9 +20,12 @@ import asyncio
 import logging
 import time
 
+import pytest
+
 from plc4py.PlcDriverManager import PlcDriverManager
 
 
+@pytest.mark.asyncio
 async def manual_test_plc_driver_umas_connect():
     driver_manager = PlcDriverManager()
     async with driver_manager.connection("umas://127.0.0.1:5555") as connection:
@@ -30,11 +33,12 @@ async def manual_test_plc_driver_umas_connect():
     assert not connection.is_connected()
 
 
+@pytest.mark.asyncio
 async def manual_test_plc_driver_umas_read():
     log = logging.getLogger(__name__)
 
     driver_manager = PlcDriverManager()
-    async with driver_manager.connection("umas://192.168.1.174:502") as connection:
+    async with driver_manager.connection("umas://192.168.1.177:502") as connection:
         with connection.read_request_builder() as builder:
             builder.add_item(f"Random Tag {1}", "testing:DINT")
             request = builder.build()
@@ -42,12 +46,13 @@ async def manual_test_plc_driver_umas_read():
         future = connection.execute(request)
         await future
         response = future.result()
-        value = response.values["Random Tag 1"][0].value
+        value = response.tags["Random Tag 1"][0].value
         log.error(f"Read tag test_REAL - {value}")
         await asyncio.sleep(1)
     pass
 
 
+@pytest.mark.asyncio
 async def manual_test_plc_driver_umas_browse():
     driver_manager = PlcDriverManager()
     async with driver_manager.connection("umas://192.168.1.174:502") as connection:
diff --git a/sandbox/plc4py/tests/unit/plc4py/spi/tcp/server.py b/sandbox/plc4py/tests/unit/plc4py/spi/tcp/server.py
index e476b17fb8..6eb25aff0b 100644
--- a/sandbox/plc4py/tests/unit/plc4py/spi/tcp/server.py
+++ b/sandbox/plc4py/tests/unit/plc4py/spi/tcp/server.py
@@ -45,4 +45,3 @@ class Server:
             while True:
                 message = connection.recv(1024)
                 count = connection.send(message)
-        self._sock.close()
diff --git a/sandbox/plc4py/tests/unit/plc4py/spi/test_protocol.py b/sandbox/plc4py/tests/unit/plc4py/spi/test_protocol.py
index 53363e1950..9bffa16d14 100644
--- a/sandbox/plc4py/tests/unit/plc4py/spi/test_protocol.py
+++ b/sandbox/plc4py/tests/unit/plc4py/spi/test_protocol.py
@@ -41,6 +41,7 @@ def tcp_server():
         yield tcp_server
 
 
+@pytest.mark.asyncio
 async def test_tcp_protocol(mocker, tcp_server) -> None:
     """
     Unit test for a not implemented protocol
diff --git a/sandbox/plc4py/tests/unit/plc4py/spi/test_transport.py b/sandbox/plc4py/tests/unit/plc4py/spi/test_transport.py
index c455f59e32..9775da2978 100644
--- a/sandbox/plc4py/tests/unit/plc4py/spi/test_transport.py
+++ b/sandbox/plc4py/tests/unit/plc4py/spi/test_transport.py
@@ -45,6 +45,7 @@ def tcp_server():
         yield tcp_server
 
 
+@pytest.mark.asyncio
 async def test_base_transport_is_reading(mocker) -> None:
     """
     Unit test for the Base PLC4X Transport, is reading.
@@ -61,6 +62,7 @@ async def test_base_transport_is_reading(mocker) -> None:
     assert transport.is_reading()
 
 
+@pytest.mark.asyncio
 async def test_tcp_transport(mocker, tcp_server) -> None:
     """
     Unit test for the TCP Transport, write


(plc4x) 01/02: fix(plc4py): Add write support to the api

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a commit to branch fix/plc4py/documentation
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 219759466c3eadc3a65af2ae6e745827f7256d33
Author: Ben Hutcheson <be...@gmail.com>
AuthorDate: Sun Feb 25 04:35:50 2024 +0100

    fix(plc4py): Add write support to the api
---
 sandbox/plc4py/plc4py/api/PlcConnection.py              | 12 +++++++-----
 .../plc4py/plc4py/drivers/modbus/ModbusConnection.py    | 17 +++++++++++------
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/sandbox/plc4py/plc4py/api/PlcConnection.py b/sandbox/plc4py/plc4py/api/PlcConnection.py
index 3d4187169b..62b48be7e4 100644
--- a/sandbox/plc4py/plc4py/api/PlcConnection.py
+++ b/sandbox/plc4py/plc4py/api/PlcConnection.py
@@ -18,12 +18,14 @@
 #
 import asyncio
 from abc import abstractmethod
-from typing import Awaitable
+from typing import Awaitable, Dict, Union
 
-from plc4py.api.messages.PlcResponse import PlcResponse, PlcTagResponse, PlcReadResponse
+from plc4py.api.messages.PlcResponse import PlcResponse, PlcTagResponse, PlcReadResponse, PlcWriteResponse, \
+    PlcBrowseResponse
 from plc4py.api.messages.PlcRequest import ReadRequestBuilder, PlcRequest
-from plc4py.api.value.PlcValue import PlcResponseCode
+from plc4py.api.value.PlcValue import PlcResponseCode, PlcValue
 from plc4py.spi.configuration.PlcConfiguration import PlcConfiguration
+from plc4py.spi.messages.utils.ResponseItem import ResponseItem
 from plc4py.utils.GenericTypes import GenericGenerator
 
 
@@ -63,9 +65,9 @@ class PlcConnection(GenericGenerator):
         """
         pass
 
-    def _default_failed_read_request(
+    def _default_failed_request(
         self, code: PlcResponseCode
-    ) -> Awaitable[PlcReadResponse]:
+    ) -> Awaitable[Union[PlcReadResponse, PlcWriteResponse, PlcBrowseResponse]]:
         """
         Returns a default PlcResponse, mainly used in case of a failed request
         :param code: The response code to return
diff --git a/sandbox/plc4py/plc4py/drivers/modbus/ModbusConnection.py b/sandbox/plc4py/plc4py/drivers/modbus/ModbusConnection.py
index 491ded41a1..ddbaef29b0 100644
--- a/sandbox/plc4py/plc4py/drivers/modbus/ModbusConnection.py
+++ b/sandbox/plc4py/plc4py/drivers/modbus/ModbusConnection.py
@@ -117,22 +117,27 @@ class ModbusConnection(PlcConnection, PlcReader, PlcWriter, PlcConnectionMetaDat
 
         if isinstance(request, PlcReadRequest):
             return self._read(request)
+        elif isinstance(request, PlcWriteRequest):
+            return self._write(request)
 
         return self._default_failed_request(PlcResponseCode.NOT_CONNECTED)
 
+    def _check_connection(self) -> bool:
+        return self._device is None
+
     def _read(self, request: PlcReadRequest) -> Awaitable[PlcReadResponse]:
         """
         Executes a PlcReadRequest
         """
-        if self._device is None:
+        if self._check_connection():
             logging.error("No device is set in the modbus connection!")
-            return self._default_failed_read_request(PlcResponseCode.NOT_CONNECTED)
+            return self._default_failed_request(PlcResponseCode.NOT_CONNECTED)
 
         async def _request(req, device) -> PlcReadResponse:
             try:
                 response = await asyncio.wait_for(device.read(req, self._transport), 5)
                 return response
-            except Exception as e:
+            except Exception:
                 # TODO:- This exception is very general and probably should be replaced
                 return PlcReadResponse(PlcResponseCode.INTERNAL_ERROR, {})
 
@@ -144,15 +149,15 @@ class ModbusConnection(PlcConnection, PlcReader, PlcWriter, PlcConnectionMetaDat
         """
         Executes a PlcWriteRequest
         """
-        if self._device is None:
+        if self._check_connection():
             logging.error("No device is set in the modbus connection!")
-            return self._default_failed_write_request(PlcResponseCode.NOT_CONNECTED)
+            return self._default_failed_request(PlcResponseCode.NOT_CONNECTED)
 
         async def _request(req, device) -> PlcWriteResponse:
             try:
                 response = await asyncio.wait_for(device.write(req, self._transport), 5)
                 return response
-            except Exception as e:
+            except Exception:
                 # TODO:- This exception is very general and probably should be replaced
                 return PlcWriteResponse(PlcResponseCode.INTERNAL_ERROR, {})