-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcua_worker.py
45 lines (38 loc) · 1.59 KB
/
opcua_worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# opcua_worker.py
from PyQt5.QtCore import QObject, pyqtSignal
import asyncio
from opcua_client import OPCUAClient
class OPCUAWorker(QObject):
finished = pyqtSignal(dict)
error = pyqtSignal(str)
def __init__(self, connection_params):
super().__init__()
self.connection_params = connection_params
self.client = OPCUAClient()
async def run_async(self):
"""
Asynchronously connect to the OPCUA server and scrape nodes.
"""
try:
nodes = await self.client.connect_and_discover(
server_address=self.connection_params['server_address'],
p12_path=self.connection_params.get('p12_path'),
p12_pass=self.connection_params.get('p12_pass'),
server_cert_path=self.connection_params.get('server_cert_path'),
user=self.connection_params.get('user'),
password=self.connection_params.get('password'),
use_cert=self.connection_params.get('use_cert', False),
use_login=self.connection_params.get('use_login', False),
USE_TRUST_STORE=self.connection_params.get('USE_TRUST_STORE', False)
)
self.finished.emit(nodes)
except Exception as e:
self.error.emit(f"Failed to connect or scrape nodes: {e}")
def run(self):
"""
Wrapper to execute the asynchronous run in the asyncio event loop.
"""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.run_async())
loop.close()