SIENTIAPDE-1314

Refactor OPC Write Method and Comment Out Error Handling Logic

- Reformatted the `write_opc_data` method for improved readability by adjusting its signature.
- Commented out the error handling logic in `OpcRepository` related to disconnection due to multiple errors, preserving the original functionality for future reference.
- Updated tests to reflect the commented-out error handling, ensuring clarity in the testing process.
This commit is contained in:
2025-10-27 07:47:11 -03:00
parent aec5839b5e
commit 80c987b2dc
3 changed files with 35 additions and 33 deletions

View File

@@ -269,7 +269,9 @@ class OPC(BaseActivity):
return success, response_times return success, response_times
@activity.defn(name='write_opc_data') @activity.defn(name='write_opc_data')
async def write_opc_data(self, input_data: dict[str, Any]) -> tuple[dict[Any, Any], dict[str, dict[str, float | None]]]: async def write_opc_data(
self, input_data: dict[str, Any]
) -> tuple[dict[Any, Any], dict[str, dict[str, float | None]]]:
""" """
Write prediction and confidence data to OPC servers. The two writing Write prediction and confidence data to OPC servers. The two writing
operations are optional and independent of each other. operations are optional and independent of each other.

View File

@@ -263,22 +263,22 @@ class OpcRepository(BaseActivity):
if self.client is None: if self.client is None:
return await self.connect() return await self.connect()
if self.error_count > 5: # if self.error_count > 5:
self.logger.custom_warning( # self.logger.custom_warning(
f'OPC server {self.id} will be disconnected due to multiple errors', self.metadata # f'OPC server {self.id} will be disconnected due to multiple errors', self.metadata
) # )
try: # try:
await self.disconnect() # await self.disconnect()
except Exception as e: # except Exception as e:
trace = traceback.format_exc() # trace = traceback.format_exc()
self.logger.custom_error( # self.logger.custom_error(
f'Failed to disconnect from OPC server: {e}', self.metadata # f'Failed to disconnect from OPC server: {e}', self.metadata
) # )
self.logger.custom_error(trace, self.metadata) # self.logger.custom_error(trace, self.metadata)
self.logger.custom_info( # self.logger.custom_info(
f'Attempting to reconnect to OPC server {self.id}...', self.metadata # f'Attempting to reconnect to OPC server {self.id}...', self.metadata
) # )
return await self.connect() # return await self.connect()
# Check if client is connected using asyncua's connection state # Check if client is connected using asyncua's connection state
try: try:

View File

@@ -1,6 +1,6 @@
import json import json
from datetime import datetime from datetime import datetime
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, call, patch from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch
import pytest import pytest
from asyncua.crypto.security_policies import SecurityPolicyBasic256 from asyncua.crypto.security_policies import SecurityPolicyBasic256
@@ -236,22 +236,22 @@ async def test_validate_connection_none_client(opc_repository):
opc_repository.connect.assert_called_once() opc_repository.connect.assert_called_once()
@pytest.mark.asyncio # @pytest.mark.asyncio
async def test_validate_connection_error_count_disconnect_error(opc_repository): # async def test_validate_connection_error_count_disconnect_error(opc_repository):
opc_repository.error_count = 6 # opc_repository.error_count = 6
opc_repository.client = AsyncMock() # opc_repository.client = AsyncMock()
opc_repository.disconnect = AsyncMock(side_effect=Exception('Test error')) # opc_repository.disconnect = AsyncMock(side_effect=Exception('Test error'))
opc_repository.connect = AsyncMock(return_value=(True, {})) # opc_repository.connect = AsyncMock(return_value=(True, {}))
response = await opc_repository.validate_connection() # response = await opc_repository.validate_connection()
assert response == opc_repository.connect.return_value # assert response == opc_repository.connect.return_value
opc_repository.disconnect.assert_called_once() # opc_repository.disconnect.assert_called_once()
opc_repository.connect.assert_called_once() # opc_repository.connect.assert_called_once()
opc_repository.logger.custom_error.assert_has_calls( # opc_repository.logger.custom_error.assert_has_calls(
[ # [
call('Failed to disconnect from OPC server: Test error', ANY), # call('Failed to disconnect from OPC server: Test error', ANY),
] # ]
) # )
@pytest.mark.asyncio @pytest.mark.asyncio