Repository navigation
Expand file tree
/
Copy pathHistoricalAccess.cpp
More file actions
368 lines (298 loc) · 11.4 KB
/
Copy pathHistoricalAccess.cpp
File metadata and controls
368 lines (298 loc) · 11.4 KB
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
/*
Copyright 2018 Kai Huebl (kai@huebl-sgh.de)
Lizenziert gemäß Apache Licence Version 2.0 (die „Lizenz“); Nutzung dieser
Datei nur in Übereinstimmung mit der Lizenz erlaubt.
Eine Kopie der Lizenz erhalten Sie auf http://www.apache.org/licenses/LICENSE-2.0.
Sofern nicht gemäß geltendem Recht vorgeschrieben oder schriftlich vereinbart,
erfolgt die Bereitstellung der im Rahmen der Lizenz verbreiteten Software OHNE
GEWÄHR ODER VORBEHALTE – ganz gleich, ob ausdrücklich oder stillschweigend.
Informationen über die jeweiligen Bedingungen für Genehmigungen und Einschränkungen
im Rahmen der Lizenz finden Sie in der Lizenz.
Autor: Kai Huebl (kai@huebl-sgh.de)
*/
#include <boost/lexical_cast.hpp>
#include "OpcUaStackCore/Base/os.h"
#include "OpcUaStackCore/Base/Log.h"
#include "OpcUaStackCore/ServiceSet/EventField.h"
#include "OpcUaStackCore/StandardEventType/BaseEventType.h"
#include "OpcUaStackServer/ServiceSetApplication/NodeReferenceApplication.h"
#include "ASNeG-Demo/Library/HistoricalAccess.h"
namespace OpcUaServerApplicationDemo
{
HistoricalAccess::HistoricalAccess(void)
: ioThread_(nullptr)
, applicationServiceIf_(nullptr)
, applicationInfo_(nullptr)
, namespaceIndex_(0)
{
}
HistoricalAccess::~HistoricalAccess(void)
{
}
bool
HistoricalAccess::startup(
IOThread& ioThread,
ApplicationServiceIf& applicationServiceIf,
ApplicationInfo* applicationInfo
)
{
Log(Debug, "HistoricalAccess::startup");
ioThread_ = &ioThread;
applicationServiceIf_ = &applicationServiceIf;
applicationInfo_ = applicationInfo;
// read namespace array from opc ua information model
// we will find the right namespace index
if (!getNamespaceInfo()) {
return false;
}
// register historical data access
if (!registerHADataCallback()) {
return false;
}
// register historical event access
if (!registerHAEventCallback()) {
return false;
}
return true;
}
bool
HistoricalAccess::shutdown(void)
{
Log(Debug, "HistoricalAccess::shutdown");
return true;
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
//
// private functions
//
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
bool
HistoricalAccess::getNamespaceInfo(void)
{
Log(Debug, "get namespace info");
ServiceTransactionNamespaceInfo::SPtr trx = constructSPtr<ServiceTransactionNamespaceInfo>();
NamespaceInfoRequest::SPtr req = trx->request();
NamespaceInfoResponse::SPtr res = trx->response();
applicationServiceIf_->sendSync(trx);
if (trx->statusCode() != Success) {
Log(Error, "NamespaceInfoResponse error")
.parameter("StatusCode", OpcUaStatusCodeMap::shortString(trx->statusCode()));
return false;
}
NamespaceInfoResponse::Index2NamespaceMap::iterator it;
for (
it = res->index2NamespaceMap().begin();
it != res->index2NamespaceMap().end();
it++
)
{
if (it->second == "http://ASNeG-Demo.de/HistoricalAccess/") {
namespaceIndex_ = it->first;
return true;
}
}
Log(Error, "namespace not found in opc ua information model")
.parameter("NamespaceUri", "http://ASNeG-Demo.de/HistoricalAccess/");
return false;
}
bool
HistoricalAccess::registerHADataCallback(void)
{
Log(Debug, "registerHADataCallback");
ServiceTransactionRegisterForwardNode::SPtr trx = constructSPtr<ServiceTransactionRegisterForwardNode>();
RegisterForwardNodeRequest::SPtr req = trx->request();
RegisterForwardNodeResponse::SPtr res = trx->response();
OpcUaNodeId::SPtr nodeId = constructSPtr<OpcUaNodeId>("DoubleValue", namespaceIndex_);
req->forwardNodeSync()->readHService().setCallback(boost::bind(&HistoricalAccess::readHValue, this, _1));
req->nodesToRegister()->resize(1);
req->nodesToRegister()->set(nodeId);
applicationServiceIf_->sendSync(trx);
if (trx->statusCode() != Success) {
Log(Error, "registerHADataCallback - transaction error");
return false;
}
for (uint32_t pos = 0; pos < res->statusCodeArray()->size(); pos++) {
OpcUaStatusCode statusCode;
res->statusCodeArray()->get(pos, statusCode);
if (statusCode != Success) {
Log(Error, "registerHADataCallback - response error");
return false;
}
}
return true;
}
bool
HistoricalAccess::registerHAEventCallback(void)
{
Log(Debug, "registerHAEventCallback");
ServiceTransactionRegisterForwardNode::SPtr trx = constructSPtr<ServiceTransactionRegisterForwardNode>();
RegisterForwardNodeRequest::SPtr req = trx->request();
RegisterForwardNodeResponse::SPtr res = trx->response();
OpcUaNodeId::SPtr nodeId = constructSPtr<OpcUaNodeId>("EventObject", namespaceIndex_);
req->forwardNodeSync()->readHEService().setCallback(boost::bind(&HistoricalAccess::readHEvent, this, _1));
req->nodesToRegister()->resize(1);
req->nodesToRegister()->set(nodeId);
applicationServiceIf_->sendSync(trx);
if (trx->statusCode() != Success) {
Log(Error, "registerHAEventCallback - transaction error");
return false;
}
for (uint32_t pos = 0; pos < res->statusCodeArray()->size(); pos++) {
OpcUaStatusCode statusCode;
res->statusCodeArray()->get(pos, statusCode);
if (statusCode != Success) {
Log(Error, "registerHAEventCallback - response error");
return false;
}
}
return true;
}
void
HistoricalAccess::readHValue(ApplicationHReadContext* applicationHReadContext)
{
Log(Debug, "readHValue");
// create start index
uint32_t startIdx = 0;
if (!applicationHReadContext->continousPoint_.empty()) {
try {
startIdx = boost::lexical_cast<uint32_t>(applicationHReadContext->continousPoint_);
} catch (boost::bad_lexical_cast& e) {
startIdx = 0;
}
}
// create end index
uint32_t endIdx = numberEntries_;
if (applicationHReadContext->numValuesPerNode_ != 0) {
if ((startIdx + applicationHReadContext->numValuesPerNode_) < endIdx) {
endIdx = startIdx + applicationHReadContext->numValuesPerNode_;
}
}
Log(Debug, "read historical data")
.parameter("NodeId", applicationHReadContext->nodeId_)
.parameter("NumValuesPerNode", applicationHReadContext->numValuesPerNode_)
.parameter("ContinousPoint", applicationHReadContext->continousPoint_)
.parameter("ReleaseContinousPoint", applicationHReadContext->releaseContinuationPoints_);
// check if user wants to remove order
if (applicationHReadContext->continousPoint_.empty() && applicationHReadContext->releaseContinuationPoints_) {
applicationHReadContext->statusCode_ = Success;
return;
}
// set continuation point
applicationHReadContext->continousPoint_ = "";
if (endIdx != numberEntries_ && !applicationHReadContext->releaseContinuationPoints_) {
std::stringstream ss;
ss << endIdx;
applicationHReadContext->continousPoint_ = ss.str();
}
// set example data
applicationHReadContext->dataValueArray_ = constructSPtr<OpcUaDataValueArray>();
applicationHReadContext->dataValueArray_->resize(endIdx - startIdx);
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
for (uint32_t idx=startIdx; idx<endIdx; idx++) {
OpcUaDateTime dateTime(time - boost::posix_time::seconds(10+idx));
OpcUaDataValue::SPtr dataValue = constructSPtr<OpcUaDataValue>();
dataValue->variant()->variant((double)idx);
dataValue->statusCode(Success);
dataValue->sourceTimestamp(dateTime);
dataValue->serverTimestamp(dateTime);
applicationHReadContext->dataValueArray_->push_back(dataValue);
}
applicationHReadContext->statusCode_ = Success;
}
void
HistoricalAccess::readHEvent(ApplicationHReadEventContext* applicationHReadEventContext)
{
Log(Debug, "readHEvent");
uint32_t numberEvents = 5;
// set example data
applicationHReadEventContext->eventFieldArray_ = constructSPtr<HistoryEventFieldListArray>();
applicationHReadEventContext->eventFieldArray_->resize(numberEvents);
for (uint32_t idx=0; idx<numberEvents; idx++) {
//
// create event
//
OpcUaVariant::SPtr variant;
BaseEventType::SPtr baseEventType = constructSPtr<BaseEventType>();
EventBase::SPtr eventBase = baseEventType;
// set message value
std::stringstream ss;
ss << "BaseEventType: Event message " << idx;
variant = constructSPtr<OpcUaVariant>();
variant->setValue(OpcUaLocalizedText("de", ss.str()));
baseEventType->message(variant);
// set severity message
variant = constructSPtr<OpcUaVariant>();
variant->setValue((OpcUaUInt16)100);
baseEventType->severity(variant);
// generate event id
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
OpcUaByteString byteString;
byteString.value((char*)&time, sizeof(boost::posix_time::ptime));
variant = constructSPtr<OpcUaVariant>();
variant->setValue(byteString);
baseEventType->eventId(variant);
// set source node id if necessary
variant = constructSPtr<OpcUaVariant>();
variant->setValue(OpcUaNodeId("EventObject", namespaceIndex_));
baseEventType->sourceNode(variant);
// set source name if necessary
variant = constructSPtr<OpcUaVariant>();
variant->setValue("EventObject");
baseEventType->sourceName(variant);
// set time if necessary
variant = constructSPtr<OpcUaVariant>();
variant->setValue(OpcUaDateTime(boost::posix_time::microsec_clock::local_time()));
baseEventType->time(variant);
// set receive time if necessary
variant = constructSPtr<OpcUaVariant>();
variant->setValue(OpcUaDateTime(boost::posix_time::microsec_clock::local_time()));
baseEventType->receiveTime(variant);
// set message if necessary
variant = constructSPtr<OpcUaVariant>();
variant->setValue(OpcUaLocalizedText("", "EventObject"));
baseEventType->message(variant);
// set severity if necessary
variant = constructSPtr<OpcUaVariant>();
variant->setValue((OpcUaUInt16)100);
baseEventType->severity(variant);
//
// filter event
//
EventFilter::SPtr eventFilter = applicationHReadEventContext->filter_;
HistoryEventFieldList::SPtr eventFieldList = constructSPtr<HistoryEventFieldList>();
eventFieldList->eventFields()->resize(eventFilter->selectClauses()->size());
applicationHReadEventContext->eventFieldArray_->push_back(eventFieldList);
for (uint32_t j=0; j<eventFilter->selectClauses()->size(); j++) {
// get simple attribute operand
SimpleAttributeOperand::SPtr simpleAttributeOperand;
eventFilter->selectClauses()->get(j, simpleAttributeOperand);
std::list<OpcUaQualifiedName::SPtr> browseNameList;
for (uint32_t j=0; j<simpleAttributeOperand->browsePath()->size(); j++) {
OpcUaQualifiedName::SPtr browseName;
simpleAttributeOperand->browsePath()->get(j, browseName);
browseNameList.push_back(browseName);
}
// get variant value from event
OpcUaVariant::SPtr value;
EventResult::Code resultCode = eventBase->get(
simpleAttributeOperand->typeId(),
browseNameList,
value
);
// insert variant into event field list
EventField::SPtr eventField;
eventField = constructSPtr<EventField>();
if (resultCode != EventResult::Success) {
value = constructSPtr<OpcUaVariant>();
}
else {
}
eventField->variant(value);
eventFieldList->eventFields()->push_back(eventField);
}
}
applicationHReadEventContext->statusCode_ = Success;
}
}