|
1
|
|
|
from PyQt5.QtCore import QSettings, Qt |
|
2
|
|
|
from PyQt5.QtWidgets import QPushButton, QComboBox, QLabel, QLineEdit, QHBoxLayout, QDialog, QDialogButtonBox, QVBoxLayout, QCheckBox, QFrame |
|
3
|
|
|
|
|
4
|
|
|
from opcua.common.ua_utils import val_to_string, string_to_variant, data_type_to_variant_type, data_type_to_string |
|
5
|
|
|
from opcua.common.methods import call_method_full |
|
6
|
|
|
from opcua import ua |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class CallMethodDialog(QDialog): |
|
10
|
|
|
def __init__(self, parent, server, node): |
|
11
|
|
|
QDialog.__init__(self, parent) |
|
12
|
|
|
self.setWindowTitle("UA Method Call") |
|
13
|
|
|
self.server = server |
|
14
|
|
|
self.node = node |
|
15
|
|
|
|
|
16
|
|
|
self.vlayout = QVBoxLayout(self) |
|
17
|
|
|
self.layout = QHBoxLayout() |
|
18
|
|
|
self.vlayout.addLayout(self.layout) |
|
19
|
|
|
self.inputs = [] |
|
20
|
|
|
self.outputs = [] |
|
21
|
|
|
|
|
22
|
|
|
self.vlayout.addWidget(QLabel("Input Arguments:", self)) |
|
23
|
|
|
try: |
|
24
|
|
|
inputs = node.get_child("0:InputArguments") |
|
25
|
|
|
args = inputs.get_value() |
|
26
|
|
|
for arg in args: |
|
27
|
|
|
self._add_input(arg) |
|
28
|
|
|
except ua.UaError as ex: |
|
29
|
|
|
print(ex) |
|
30
|
|
|
|
|
31
|
|
|
layout = QHBoxLayout() |
|
32
|
|
|
self.vlayout.addLayout(layout) |
|
33
|
|
|
layout.addWidget(QLabel("Result:", self)) |
|
34
|
|
|
self.result_label = QLabel("None") |
|
35
|
|
|
layout.addWidget(self.result_label) |
|
36
|
|
|
|
|
37
|
|
|
self.vlayout.addWidget(QLabel("Output Arguments:", self)) |
|
38
|
|
|
try: |
|
39
|
|
|
outputs = node.get_child("0:OutputArguments") |
|
40
|
|
|
args = outputs.get_value() |
|
41
|
|
|
for arg in args: |
|
42
|
|
|
self._add_output(arg) |
|
43
|
|
|
except ua.UaError as ex: |
|
44
|
|
|
print(ex) |
|
45
|
|
|
|
|
46
|
|
|
layout = QHBoxLayout() |
|
47
|
|
|
self.vlayout.addLayout(layout) |
|
48
|
|
|
layout.addStretch() |
|
49
|
|
|
close_button = QPushButton("Close") |
|
50
|
|
|
close_button.clicked.connect(self.close) |
|
51
|
|
|
layout.addWidget(close_button) |
|
52
|
|
|
call_button = QPushButton("Call Method") |
|
53
|
|
|
call_button.clicked.connect(self.call) |
|
54
|
|
|
layout.addWidget(call_button) |
|
55
|
|
|
|
|
56
|
|
|
def call(self): |
|
57
|
|
|
try: |
|
58
|
|
|
self._call() |
|
59
|
|
|
except Exception as ex: |
|
60
|
|
|
self.result_label.setText(str(ex)) |
|
61
|
|
|
|
|
62
|
|
|
def _call(self): |
|
63
|
|
|
parent = self.node.get_parent() |
|
64
|
|
|
args = [] |
|
65
|
|
|
for inp in self.inputs: |
|
66
|
|
|
val = string_to_variant(inp.text(), data_type_to_variant_type(inp.data_type)) |
|
67
|
|
|
args.append(val) |
|
68
|
|
|
|
|
69
|
|
|
result = call_method_full(parent, self.node, *args) |
|
70
|
|
|
self.result_label.setText(str(result.StatusCode)) |
|
71
|
|
|
|
|
72
|
|
|
for idx, res in enumerate(result.OutputArguments): |
|
73
|
|
|
self.outputs[idx].setText(val_to_string(res)) |
|
74
|
|
|
|
|
75
|
|
|
def _add_input(self, arg): |
|
76
|
|
|
layout = QHBoxLayout() |
|
77
|
|
|
self.vlayout.addLayout(layout) |
|
78
|
|
|
layout.addWidget(QLabel("Name:{}".format(arg.Name), self)) |
|
79
|
|
|
layout.addWidget(QLabel("Data type:{}".format(data_type_to_string(arg.DataType)), self)) |
|
80
|
|
|
layout.addWidget(QLabel("Description:{}".format(arg.Description.Text), self)) |
|
81
|
|
|
lineedit = QLineEdit(self) |
|
82
|
|
|
lineedit.data_type = self.server.get_node(arg.DataType) |
|
83
|
|
|
self.inputs.append(lineedit) |
|
84
|
|
|
layout.addWidget(lineedit) |
|
85
|
|
|
|
|
86
|
|
|
def _add_output(self, arg): |
|
87
|
|
|
layout = QHBoxLayout() |
|
88
|
|
|
self.vlayout.addLayout(layout) |
|
89
|
|
|
layout.addWidget(QLabel("Data Type: {}".format(data_type_to_string(arg.DataType)))) |
|
90
|
|
|
layout.addWidget(QLabel("Value:")) |
|
91
|
|
|
label = QLabel("", self) |
|
92
|
|
|
self.outputs.append(label) |
|
93
|
|
|
layout.addWidget(label) |
|
94
|
|
|
|