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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/**
* @file BobinkNode.cpp
* @brief QML component representing a single OPC UA node.
*/
#include "BobinkNode.h"
#include "BobinkClient.h"
#include <QOpcUaClient>
#include <QOpcUaMonitoringParameters>
static constexpr double DEFAULT_PUBLISHING_INTERVAL = 250.0; // ms
/* ------------------------------------------------------------------ */
/* Construction / destruction */
/* ------------------------------------------------------------------ */
BobinkNode::BobinkNode (QQuickItem *parent) : QQuickItem (parent) {}
BobinkNode::~BobinkNode () { stopMonitoring (); }
/* ------------------------------------------------------------------ */
/* Properties */
/* ------------------------------------------------------------------ */
QString
BobinkNode::nodeId () const
{
return m_nodeId;
}
void
BobinkNode::setNodeId (const QString &id)
{
if (m_nodeId == id)
return;
m_nodeId = id;
emit nodeIdChanged ();
if (m_componentComplete && isVisible ())
{
stopMonitoring ();
startMonitoring ();
}
}
QVariant
BobinkNode::value () const
{
return m_value;
}
void
BobinkNode::setValue (const QVariant &value)
{
if (m_opcuaNode)
m_opcuaNode->writeValueAttribute (value);
}
BobinkNode::NodeStatus
BobinkNode::status () const
{
return m_status;
}
QDateTime
BobinkNode::sourceTimestamp () const
{
return m_sourceTimestamp;
}
QDateTime
BobinkNode::serverTimestamp () const
{
return m_serverTimestamp;
}
/* ------------------------------------------------------------------ */
/* readAttribute */
/* ------------------------------------------------------------------ */
void
BobinkNode::readAttribute (const QString &attributeName)
{
if (!m_opcuaNode)
return;
QOpcUa::NodeAttribute attr = attributeFromName (attributeName);
if (attr == QOpcUa::NodeAttribute::None)
return;
m_pendingReads.insert (attr, attributeName);
m_opcuaNode->readAttributes (attr);
}
/* ------------------------------------------------------------------ */
/* QQuickItem lifecycle */
/* ------------------------------------------------------------------ */
void
BobinkNode::componentComplete ()
{
QQuickItem::componentComplete ();
m_componentComplete = true;
auto *client = BobinkClient::instance ();
if (client)
connect (client, &BobinkClient::connectedChanged, this,
&BobinkNode::handleClientConnectedChanged);
if (isVisible ())
startMonitoring ();
}
void
BobinkNode::itemChange (ItemChange change, const ItemChangeData &data)
{
QQuickItem::itemChange (change, data);
if (change == ItemVisibleHasChanged && m_componentComplete)
{
if (data.boolValue)
startMonitoring ();
else
stopMonitoring ();
}
}
/* ------------------------------------------------------------------ */
/* Monitoring lifecycle */
/* ------------------------------------------------------------------ */
void
BobinkNode::startMonitoring ()
{
if (m_opcuaNode || m_nodeId.isEmpty ())
return;
auto *client = BobinkClient::instance ();
if (!client || !client->connected ())
return;
QOpcUaClient *opcua = client->opcuaClient ();
if (!opcua)
return;
m_opcuaNode = opcua->node (m_nodeId);
if (!m_opcuaNode)
return;
connect (m_opcuaNode, &QOpcUaNode::dataChangeOccurred, this,
&BobinkNode::handleDataChange);
connect (m_opcuaNode, &QOpcUaNode::attributeUpdated, this,
&BobinkNode::handleAttributeUpdated);
connect (m_opcuaNode, &QOpcUaNode::attributeWritten, this,
&BobinkNode::handleAttributeWritten);
QOpcUaMonitoringParameters params (DEFAULT_PUBLISHING_INTERVAL);
m_opcuaNode->enableMonitoring (QOpcUa::NodeAttribute::Value, params);
}
void
BobinkNode::stopMonitoring ()
{
if (!m_opcuaNode)
return;
m_pendingReads.clear ();
delete m_opcuaNode;
m_opcuaNode = nullptr;
}
/* ------------------------------------------------------------------ */
/* Signal handlers */
/* ------------------------------------------------------------------ */
void
BobinkNode::handleDataChange (QOpcUa::NodeAttribute attr, const QVariant &val)
{
if (attr != QOpcUa::NodeAttribute::Value)
return;
if (m_value != val)
{
m_value = val;
emit valueChanged ();
}
NodeStatus newStatus = statusFromCode (m_opcuaNode->valueAttributeError ());
if (m_status != newStatus)
{
m_status = newStatus;
emit statusChanged ();
}
QDateTime srcTs
= m_opcuaNode->sourceTimestamp (QOpcUa::NodeAttribute::Value);
if (m_sourceTimestamp != srcTs)
{
m_sourceTimestamp = srcTs;
emit sourceTimestampChanged ();
}
QDateTime srvTs
= m_opcuaNode->serverTimestamp (QOpcUa::NodeAttribute::Value);
if (m_serverTimestamp != srvTs)
{
m_serverTimestamp = srvTs;
emit serverTimestampChanged ();
}
}
void
BobinkNode::handleAttributeUpdated (QOpcUa::NodeAttribute attr,
const QVariant &val)
{
auto it = m_pendingReads.find (attr);
if (it != m_pendingReads.end ())
{
emit attributeRead (it.value (), val);
m_pendingReads.erase (it);
}
}
void
BobinkNode::handleAttributeWritten (QOpcUa::NodeAttribute attr,
QOpcUa::UaStatusCode statusCode)
{
if (attr != QOpcUa::NodeAttribute::Value)
return;
if (statusCode != QOpcUa::UaStatusCode::Good)
emit writeError (QStringLiteral ("Write failed: 0x%1")
.arg (static_cast<quint32> (statusCode), 8, 16,
QLatin1Char ('0')));
}
void
BobinkNode::handleClientConnectedChanged ()
{
auto *client = BobinkClient::instance ();
if (!client)
return;
if (client->connected ())
{
if (m_componentComplete && isVisible ())
startMonitoring ();
}
else
{
stopMonitoring ();
}
}
/* ------------------------------------------------------------------ */
/* Helpers */
/* ------------------------------------------------------------------ */
BobinkNode::NodeStatus
BobinkNode::statusFromCode (QOpcUa::UaStatusCode code)
{
quint32 severity = static_cast<quint32> (code) >> 30;
switch (severity)
{
case 0:
return Good;
case 1:
return Uncertain;
default:
return Bad;
}
}
QOpcUa::NodeAttribute
BobinkNode::attributeFromName (const QString &name)
{
static const QHash<QString, QOpcUa::NodeAttribute> map = {
{ "NodeId", QOpcUa::NodeAttribute::NodeId },
{ "NodeClass", QOpcUa::NodeAttribute::NodeClass },
{ "BrowseName", QOpcUa::NodeAttribute::BrowseName },
{ "DisplayName", QOpcUa::NodeAttribute::DisplayName },
{ "Description", QOpcUa::NodeAttribute::Description },
{ "Value", QOpcUa::NodeAttribute::Value },
{ "DataType", QOpcUa::NodeAttribute::DataType },
{ "ValueRank", QOpcUa::NodeAttribute::ValueRank },
{ "ArrayDimensions", QOpcUa::NodeAttribute::ArrayDimensions },
{ "AccessLevel", QOpcUa::NodeAttribute::AccessLevel },
{ "UserAccessLevel", QOpcUa::NodeAttribute::UserAccessLevel },
{ "MinimumSamplingInterval",
QOpcUa::NodeAttribute::MinimumSamplingInterval },
{ "Historizing", QOpcUa::NodeAttribute::Historizing },
{ "Executable", QOpcUa::NodeAttribute::Executable },
{ "UserExecutable", QOpcUa::NodeAttribute::UserExecutable },
};
return map.value (name, QOpcUa::NodeAttribute::None);
}
|