Skip to content

Commit

Permalink
Fix : parameter order in connect method and allow dynamic host connec…
Browse files Browse the repository at this point in the history
…tions (#68)

- Corrected the parameter order in the connect method of both OpcDaClient and OpcCom classes to ensure the correct sequence of opc_server and opc_host.
- Updated the connect method to allow dynamic host connections instead of defaulting to 'localhost', providing more flexibility in server connections.
- Improved type annotations for better code clarity and type safety.
Changes:
- Swapped the order of parameters in the connect method to server, host for consistency and correctness.
- Removed the hardcoded 'localhost' assignment, allowing the host to be specified dynamically.
- Enhanced type annotations for opc_server and opc_host parameters.
Impact:
- These changes ensure that the OPC client can connect to specified servers and hosts correctly, enhancing the flexibility and usability of the library.
  • Loading branch information
Jbiscode authored Nov 7, 2024
1 parent 9889042 commit 3539363
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions openopc2/da_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ def set_trace(self, trace):
if self._open_serv is None:
self.trace = trace

def connect(self, opc_server=None, opc_host='localhost'):
def connect(self, opc_server: str | None = None, opc_host: str = 'localhost'):
"""Connect to the specified OPC server"""

log.info(f"OPC DA OpcDaClient connecting to {opc_server} {opc_host}")
self._opc.connect(opc_host, opc_server)
self._opc.connect(opc_server, opc_host)
self.connected = True

# With some OPC servers, the next OPC call immediately after Connect()
Expand Down
8 changes: 4 additions & 4 deletions openopc2/da_com.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class OpcCom:

def __init__(self, opc_class: str):
# TODO: Get browser type (hierarchical etc)
self.server: string = None
self.host: string = 'localhost'
self.server: str | None = None
self.host: str = 'localhost'
self.groups = None
self.opc_class = opc_class
self.client_name = None
Expand All @@ -69,9 +69,9 @@ def initialize_client(self, opc_class):
pythoncom.CoUninitialize()
raise OPCError(f'Dispatch: {err} opc_class:"{opc_class}"')

def connect(self, host: str, server: str):
def connect(self, server: str | None, host: str):
self.server = server
self.host = "localhost"
self.host = host
try:
log.info(f"Connecting OPC Client Com interface: '{self.server}', '{self.host}'")
self.opc_client.Connect(self.server, self.host)
Expand Down

0 comments on commit 3539363

Please sign in to comment.