-
Notifications
You must be signed in to change notification settings - Fork 298
/
OpcUaClient.cpp
688 lines (626 loc) · 21.6 KB
/
OpcUaClient.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include "OpcUaClient.h"
#include "MainWindow.h"
#include "Plotting/PlotWindowContainer.h"
#include "Plotting/VariablesWidget.h"
#include <QThread>
#include <QDebug>
/*!
Write the value value to the real node with id id.
*/
static bool writeReal(UA_Client *client, UA_NodeId id, UA_Double value)
{
UA_WriteRequest wReq;
UA_WriteRequest_init(&wReq);
wReq.nodesToWrite = UA_WriteValue_new();
wReq.nodesToWriteSize = 1;
wReq.nodesToWrite[0].nodeId = id;
wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_DOUBLE];
wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE;
wReq.nodesToWrite[0].value.value.data = &value;
UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
if (wResp.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
UA_WriteRequest_deleteMembers(&wReq);
UA_WriteResponse_deleteMembers(&wResp);
return false;
}
UA_WriteRequest_deleteMembers(&wReq);
UA_WriteResponse_deleteMembers(&wResp);
return true;
}
/*!
Write the value value to the bool node with id id.
*/
static bool writeBool(UA_Client *client, UA_NodeId id, UA_Boolean value)
{
UA_WriteRequest wReq;
UA_WriteRequest_init(&wReq);
wReq.nodesToWrite = UA_WriteValue_new();
wReq.nodesToWriteSize = 1;
wReq.nodesToWrite[0].nodeId = id;
wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_BOOLEAN];
wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE;
wReq.nodesToWrite[0].value.value.data = &value;
UA_WriteResponse wResp = UA_Client_Service_write(client, wReq);
if (wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD) {
UA_WriteRequest_deleteMembers(&wReq);
UA_WriteResponse_deleteMembers(&wResp);
return true;
}
UA_WriteRequest_deleteMembers(&wReq);
UA_WriteResponse_deleteMembers(&wResp);
return false;
}
/*!
Contains OPC UA functionality to interact with an OPC UA server.
*/
OpcUaClient::OpcUaClient(SimulationOptions simulationOptions)
: mSimulationOptions(simulationOptions)
{
mpSampleThread = new QThread();
}
OpcUaClient::~OpcUaClient()
{
mpOpcUaWorker->pauseInteractiveSimulation();
UA_Client_disconnect(mpClient);
UA_Client_delete(mpClient);
}
/*!
Connect to an OPC UA server.
*/
bool OpcUaClient::connectToServer(QString *pErrorString)
{
std::string endPoint = "opc.tcp://localhost:" + std::to_string(mSimulationOptions.getInteractiveSimulationPortNumber());
mpClient = UA_Client_new(UA_ClientConfig_standard);
UA_StatusCode returnValue;
int tries = 0;
do {
Sleep::msleep(100);
returnValue = UA_Client_connect(mpClient, endPoint.c_str());
++tries;
if (tries > 50) { // Do not go in infinte loop. Try 50 times i.e., 5 secs max and then give up.
break;
}
} while (returnValue != UA_STATUSCODE_GOOD);
// qDebug() << "Connected to OPC-UA server " << endPoint;
if (returnValue != UA_STATUSCODE_GOOD) {
*pErrorString = tr("Could not connect to the embedded server. Status code %1.").arg(QString(UA_StatusCode_name(returnValue)));
return false;
} else {
*pErrorString = QString(UA_StatusCode_name(returnValue));
return true;
}
}
/*!
Browse the server after all the variable names and return them in a QStringList .
All the variable names are added to the temporary data structure.
Called one time when the simulation server is ready.
*/
QStringList OpcUaClient::fetchVariableNamesFromServer()
{
UA_BrowseRequest browseRequest;
UA_BrowseRequest_init(&browseRequest);
browseRequest.requestedMaxReferencesPerNode = 0;
browseRequest.nodesToBrowse = UA_BrowseDescription_new();
browseRequest.nodesToBrowseSize = 1;
browseRequest.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */
browseRequest.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
UA_BrowseResponse browseResponse = UA_Client_Service_browse(mpClient, browseRequest);
QStringList variablesList;
for (size_t i = 0; i < browseResponse.resultsSize; ++i) {
for (size_t j = 0; j < browseResponse.results[i].referencesSize; ++j) {
UA_ReferenceDescription *ref = &(browseResponse.results[i].references[j]);
if (ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
int nodeId = ref->nodeId.nodeId.identifier.numeric;
QString variableName = QString::fromUtf8((char *)ref->browseName.name.data, ref->browseName.name.length);
if (!variableName.startsWith("$")) {
Variable *pVariable = new Variable(nodeId, variableIsWritable(nodeId));
if (variableIsReal(nodeId)) {
double variableValue = readReal(nodeId);
pVariable->checkBounds(variableValue);
pVariable->setIsBool(false);
// append the list used in the variable widget
variablesList << variableName;
} else if (variableIsBool(nodeId)) {
int variableValue = readBool(nodeId);
pVariable->checkBounds(variableValue);
pVariable->setIsBool(true);
// append the list used in the variable widget
variablesList << variableName;
}
mVariables.insert(variableName, pVariable);
// manually set plotting bounds
pVariable->setXBounds(mSimulationOptions.getStartTime().toDouble(), mSimulationOptions.getStopTime().toDouble());
}
}
}
}
UA_BrowseRequest_deleteMembers(&browseRequest);
UA_BrowseResponse_deleteMembers(&browseResponse);
return variablesList;
}
/*!
Called when a variable is checked in the variable tree.
*/
void OpcUaClient::checkVariable(int nodeId, VariablesTreeItem *pVariablesTreeItem)
{
// item checked, inform the backend
mCheckedVariables.insert(nodeId, pVariablesTreeItem);
if (!mSimulationOptions.isInteractiveSimulationWithSteps()) {
emit mpOpcUaWorker->emitSendAddMonitoredItem(nodeId, pVariablesTreeItem->getPlotVariable());
}
}
/*!
Called when a variable is unchecked in the variable tree.
*/
void OpcUaClient::unCheckVariable(int nodeId, const QString &name)
{
mCheckedVariables.remove(nodeId);
if (!mSimulationOptions.isInteractiveSimulationWithSteps()) {
emit mpOpcUaWorker->emitSendRemoveMonitoredItem(name);
}
}
/*!
Use the write mask to check if a variable is writable.
*/
bool OpcUaClient::variableIsWritable(int nodeId)
{
// highlevel client functionality used
UA_UInt32 isWritable;
UA_Client_readWriteMaskAttribute(mpClient, UA_NODEID_NUMERIC(1, nodeId), &isWritable);
if (isWritable == 1) {
return true;
}
return false;
}
/*!
Two kind of datatypes are used in the OPC UA interface. Booleans and reals.
Check if a variable is of type real.
*/
bool OpcUaClient::variableIsReal(int nodeId)
{
UA_ReadRequest rReq;
UA_ReadRequest_init(&rReq);
rReq.nodesToRead = (UA_ReadValueId*)UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
rReq.nodesToReadSize = 1;
rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(1, nodeId);
rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
UA_ReadResponse rResp = UA_Client_Service_read(mpClient, rReq);
if (rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
rResp.resultsSize > 0 && rResp.results[0].hasValue &&
UA_Variant_isScalar(&rResp.results[0].value) &&
rResp.results[0].value.type == &UA_TYPES[UA_TYPES_DOUBLE]) {
UA_ReadRequest_deleteMembers(&rReq);
UA_ReadResponse_deleteMembers(&rResp);
return true;
}
UA_ReadRequest_deleteMembers(&rReq);
UA_ReadResponse_deleteMembers(&rResp);
return false;
}
/*!
Check if a variable is of type bool.
*/
bool OpcUaClient::variableIsBool(int nodeId)
{
UA_ReadRequest rReq;
UA_ReadRequest_init(&rReq);
rReq.nodesToRead = (UA_ReadValueId*)UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
rReq.nodesToReadSize = 1;
rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(1, nodeId);
rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
UA_ReadResponse rResp = UA_Client_Service_read(mpClient, rReq);
if (rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
rResp.resultsSize > 0 && rResp.results[0].hasValue &&
UA_Variant_isScalar(&rResp.results[0].value) &&
rResp.results[0].value.type == &UA_TYPES[UA_TYPES_BOOLEAN]) {
UA_ReadRequest_deleteMembers(&rReq);
UA_ReadResponse_deleteMembers(&rResp);
return true;
}
UA_ReadRequest_deleteMembers(&rReq);
UA_ReadResponse_deleteMembers(&rResp);
return false;
}
/*!
Read the value of a real node with the provided id, return the result.
*/
double OpcUaClient::readReal(int id)
{
double res = -1;
UA_ReadRequest rReq;
UA_ReadRequest_init(&rReq);
rReq.nodesToRead = (UA_ReadValueId*)UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
rReq.nodesToReadSize = 1;
rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(1, id);
rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
UA_ReadResponse rResp = UA_Client_Service_read(mpClient, rReq);
if (rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
rResp.resultsSize > 0 && rResp.results[0].hasValue &&
UA_Variant_isScalar(&rResp.results[0].value) &&
rResp.results[0].value.type == &UA_TYPES[UA_TYPES_DOUBLE]) {
res = *(UA_Double*)rResp.results[0].value.data;
}
UA_ReadRequest_deleteMembers(&rReq);
UA_ReadResponse_deleteMembers(&rResp);
return res;
}
/*!
Read the bool value of the provided id, return the result as an integer.
-1 indicates an error.
*/
int OpcUaClient::readBool(int id)
{
int res = -1;
UA_ReadRequest rReq;
UA_ReadRequest_init(&rReq);
rReq.nodesToRead = (UA_ReadValueId*)UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
rReq.nodesToReadSize = 1;
rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(1, id);
rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
UA_ReadResponse rResp = UA_Client_Service_read(mpClient, rReq);
if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
rResp.resultsSize > 0 && rResp.results[0].hasValue &&
UA_Variant_isScalar(&rResp.results[0].value) &&
rResp.results[0].value.type == &UA_TYPES[UA_TYPES_BOOLEAN]) {
res = *(UA_Boolean*)rResp.results[0].value.data;
}
UA_ReadRequest_deleteMembers(&rReq);
UA_ReadResponse_deleteMembers(&rResp);
return res;
}
/*!
Called when a value is entered in the variable tree.
Write the new value to the corresponding node.
*/
void OpcUaClient::writeValue(const QVariant &value, const QString &name)
{
int nodeId = mVariables.value(name)->getNodeId();
if (variableIsReal(nodeId)) {
writeReal(mpClient, UA_NODEID_NUMERIC(1, nodeId), value.toDouble());
} else if (variableIsBool(nodeId)) {
writeBool(mpClient, UA_NODEID_NUMERIC(1, nodeId), value.toBool());
}
}
/*!
Read the current simulation time by contacting the remote with help of open62541 high level functionality.
*/
double OpcUaClient::getCurrentSimulationTime()
{
UA_Variant *currentTime = UA_Variant_new();
UA_Client_readValueAttribute(mpClient, UA_NODEID_NUMERIC(0, 10004), currentTime);
return *(double*)currentTime->data;
}
/*!
Thread used to fetch data from the OPC UA remote.
It can be done by using subscriptions or contacting the remote directly each time step (simulate with steps).
*/
OpcUaWorker::OpcUaWorker(OpcUaClient *pClient, bool simulateWithSteps)
: mpParentClient(pClient), mSimulateWithSteps(simulateWithSteps), mSampleInterval(10), mSpeedValue(1.0), mServerSampleInterval(5)
{
setInterval(mSampleInterval);
if (!mSimulateWithSteps) {
createSubscription();
}
}
OpcUaWorker::~OpcUaWorker()
{
}
void OpcUaWorker::setInterval(double interval)
{
mInterval = interval;
}
void OpcUaWorker::startInteractiveSimulation()
{
mClock.start();
mIsRunning = true;
if (!mSimulateWithSteps) {
writeReal(mpParentClient->getClient(), UA_NODEID_NUMERIC(0, 10002), mSpeedValue);
writeBool(mpParentClient->getClient(), UA_NODEID_NUMERIC(0, 10001), true);
}
while(mIsRunning) {
const double elapsed = mClock.elapsed();
sample();
if (mSimulateWithSteps) {
if (mInterval > 0.0) {
const double ms = mInterval - (mClock.elapsed() - elapsed);
if (ms > 0.0) {
QTime waitTime= QTime::currentTime().addMSecs(ms);
while (QTime::currentTime() < waitTime) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
}
}
} else {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
Sleep::msleep(mServerSampleInterval);
}
}
}
void OpcUaWorker::pauseInteractiveSimulation()
{
mIsRunning = false;
if (!mSimulateWithSteps) {
writeBool(mpParentClient->getClient(), UA_NODEID_NUMERIC(0, 10001), false);
}
}
/*!
Called in intervals.
setInterval(ms) can be usd to adjust the time between each call.
*/
void OpcUaWorker::sample()
{
// draw selected curve lines
emit sendUpdateCurves();
if (mSimulateWithSteps) {
// append the new values
appendVariableValues();
// step the simulation by contacting the remote server
stepSimulation();
} else {
UA_Client_Subscriptions_manuallySendPublishRequest(mpParentClient->getClient());
insertValues();
}
}
void OpcUaWorker::setVariablesTreeItemRoot(VariablesTreeItem * pVariablesTreeItemRoot)
{
mpVariablesTreeItemRoot = pVariablesTreeItemRoot;
}
/*!
* Adjust the sample interval according to the value entered by the user.
*/
void OpcUaWorker::setSpeed(QString value)
{
bool isDouble = true;
double speedValue = value.toDouble(&isDouble);
if (isDouble && speedValue > 0.0) {
mSpeedValue = speedValue;
setInterval(mSampleInterval / speedValue);
} else if (isDouble && speedValue == 0.0) {
mSpeedValue = 0.0;
}
if (isDouble) {
writeReal(mpParentClient->getClient(), UA_NODEID_NUMERIC(0, 10002), mSpeedValue);
}
}
/*!
Simulate with steps. For each checked variable, contact the remote and fetch the data.
*/
void OpcUaWorker::appendVariableValues()
{
// fetch checked variables data only
for (auto & p : mpParentClient->getCheckedVariables()) {
Variable *pVariable = mpParentClient->getVariables()->value(p->getPlotVariable());
int nodeId = pVariable->getNodeId();
double variableValue;
if (pVariable->isBool()) {
variableValue = mpParentClient->readBool(nodeId);
} else {
variableValue = mpParentClient->readReal(nodeId);
}
pVariable->insertData(mpParentClient->getCurrentSimulationTime(), variableValue);
pVariable->checkBounds(variableValue);
checkMinMaxValues(variableValue);
}
}
/*!
Contact the remote and write true to the step node. Uses high level functionality.
UA_NODEID_NUMERIC(0, 10000) should probably be using OMC_OPC_NODEID_STEP.
*/
void OpcUaWorker::stepSimulation()
{
// set the step variable to true
writeBool(mpParentClient->getClient(), UA_NODEID_NUMERIC(0, 10000), UA_TRUE);
}
/*!
Called to catch the min max values. The information can be used useful to the plotting window.
*/
void OpcUaWorker::checkMinMaxValues(const double& value)
{
if (value < mMinMaxValues.first) {
mMinMaxValues.first = value;
emit sendUpdateYAxis(qMakePair(mMinMaxValues.first, mMinMaxValues.second));
} else if ( value > mMinMaxValues.second ) {
mMinMaxValues.second = value;
emit sendUpdateYAxis(qMakePair(mMinMaxValues.first, mMinMaxValues.second));
}
}
/*!
Create a subscription to the remote.
*/
void OpcUaWorker::createSubscription()
{
UA_SubscriptionSettings subscriptionSettings = UA_SubscriptionSettings_standard;
subscriptionSettings.requestedPublishingInterval = mServerSampleInterval;
subscriptionSettings.maxNotificationsPerPublish = 4096;
UA_Client_Subscriptions_new(mpParentClient->getClient(), subscriptionSettings, &mSubscriptionId);
monitorTime();
}
/*!
Contact the remote and write true to the step node. Uses high level functionality.
UA_NODEID_NUMERIC(0, 10004) should probably be using OMC_OPC_NODEID_TIME.
*/
void OpcUaWorker::monitorTime()
{
UA_UInt32 monitorId = 1;
UA_NodeId time = UA_NODEID_NUMERIC(0, 10004);
if (UA_STATUSCODE_GOOD != UA_Client_Subscriptions_addMonitoredItem(mpParentClient->getClient(), mSubscriptionId, time, UA_ATTRIBUTEID_VALUE,
&timeChanged, mpParentClient->getClient(), &monitorId)) {
qDebug() << "Monitor time failed";
}
}
/*!
Subscriptions make use of c style code. QThreadStorage will be used as a cache for
subscription values. Values will be stored in the calling thread's memory stack.
*/
QThreadStorage<double> OpcUaWorker::mCurrentTime;
QThreadStorage<QMap<UA_UInt32, Variable*>> OpcUaWorker::mMonitorIds;
QThreadStorage<QMap<UA_UInt32, double>> OpcUaWorker::mCurrentValues;
/*!
A time step has been taken. Cache the value and make another step.
*/
void OpcUaWorker::timeChanged(UA_UInt32 handle, UA_DataValue *pValue, void *pClient)
{
Q_UNUSED(handle);
if (pValue->hasValue) {
// catch the time value
mCurrentTime.setLocalData(*(UA_Double*)pValue->value.data);
}
/* if (mSimulateWithSteps) */ {
writeBool((UA_Client*) pClient, UA_NODEID_NUMERIC(0, 10000), UA_TRUE);
}
}
/*!
Inserts the data to the Variable data structure.
*/
void OpcUaWorker::insertValues()
{
for (const auto &p : mMonitorIds.localData()) {
double value = mCurrentValues.localData().value(mMonitorIds.localData().key(p));
p->insertData(mCurrentTime.localData(), value);
checkMinMaxValues(value);
}
}
/*!
A boolean value has been changed.
Update the cached value and check bounds for the new value.
*/
void OpcUaWorker::boolChanged(UA_UInt32 handle, UA_DataValue *pValue, void *pClient)
{
Q_UNUSED(pClient);
if (pValue->hasValue) {
// remember the value until next time the server notify a change
double variableValue = *(UA_Boolean*)pValue->value.data;
mCurrentValues.localData().insert(handle, variableValue);
mMonitorIds.localData().value(handle)->checkBounds(variableValue);
}
}
/*!
A real value has been changed.
Update the cached value and check bounds for the new value.
*/
void OpcUaWorker::realChanged(UA_UInt32 handle, UA_DataValue *pValue, void *pClient)
{
Q_UNUSED(pClient);
if (pValue->hasValue) {
// remember the value until next time the server notify a change
double variableValue = *(UA_Double*)pValue->value.data;
mCurrentValues.localData().insert(handle, variableValue);
mMonitorIds.localData().value(handle)->checkBounds(variableValue);
}
}
/*!
This slot is called when a variable is checked in subscription mode.
Start to monitor the variable.
*/
void OpcUaWorker::addMonitoredItem(int nodeId, const QString &variableName)
{
UA_UInt32 monitorId = 1;
UA_NodeId monitoredItem = UA_NODEID_NUMERIC(1, nodeId);
Variable *pVariable = mpParentClient->getVariables()->value(variableName);
if (pVariable->isBool()) {
UA_Client_Subscriptions_addMonitoredItem(mpParentClient->getClient(), mSubscriptionId, monitoredItem, UA_ATTRIBUTEID_VALUE,
&boolChanged, mpParentClient->getClient(), &monitorId);
} else {
UA_Client_Subscriptions_addMonitoredItem(mpParentClient->getClient(), mSubscriptionId, monitoredItem, UA_ATTRIBUTEID_VALUE,
&realChanged, mpParentClient->getClient(), &monitorId);
}
pVariable->setMonitoredItemId(monitorId);
mMonitorIds.localData().insert(monitorId, pVariable);
}
/*!
This slot is called when a variable is unchecked in subscription mode.
Remove it from the monitored items.
*/
void OpcUaWorker::removeMonitoredItem(const QString& variableName)
{
Variable *pVariable = mpParentClient->getVariables()->value(variableName);
UA_Client_Subscriptions_removeMonitoredItem(mpParentClient->getClient(), mSubscriptionId, pVariable->getMonitoredItemId());
mMonitorIds.localData().remove(pVariable->getMonitoredItemId());
}
/*!
Data structure to handle data between the OPC UA server and OMPlot.
*/
Variable::Variable(int id, bool isWritable)
{
// starting bounds
mCurveBounds.setRect(0.0, 0.0, -2.0, 1.0);
mNodeId = id;
mIsWritable = isWritable;
mXAxisVector = 0;
mYAxisVector = 0;
}
Variable::~Variable() {}
/*!
Determines where the data is to be stored.
*/
void Variable::setAxisVectors(QPair<QVector<double>*, QVector<double>*> axes)
{
mXAxisVector = axes.first;
mYAxisVector = axes.second;
}
/*!
Inserts new data in the vectors.
*/
void Variable::insertData(const double& xValue, const double& yValue)
{
if (mXAxisVector != 0 && mYAxisVector != 0)
{
mXAxisVector->push_back(xValue);
mYAxisVector->push_back(yValue);
}
}
/*!
Returns a QPointF of the values at position i.
*/
QPointF Variable::sample(size_t i) const
{
return QPointF(mXAxisVector->at(i), mYAxisVector->at(i));
}
/*!
Returns the number of points currently held.
*/
size_t Variable::size() const
{
return mXAxisVector->size();
}
/*!
Returns the bounding rectangle.
*/
QRectF Variable::boundingRect() const
{
return mCurveBounds;
}
/*!
Sets the y bounds of the bounding rectangle.
*/
void Variable::setYBounds(const double &minValue, const double &maxValue)
{
// not very intuitive, look after how qrectf works.
mCurveBounds.setBottom(maxValue);
mCurveBounds.setTop(minValue);
}
/*!
Sets the x bounds of the bounding rectangle.
*/
void Variable::setXBounds(const double &startBound, const double &stopBound)
{
mCurveBounds.setLeft(startBound);
mCurveBounds.setRight(stopBound);
}
/*!
Checks the current bounds and updates them if suitable.
*/
void Variable::checkBounds(const double &value)
{
if (value > minMaxBounds.second) {
minMaxBounds.second = value;
mCurveBounds.setBottom(value);
} else if (value < minMaxBounds.first) {
minMaxBounds.first = value;
mCurveBounds.setTop(value);
}
}