-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent.cpp
More file actions
300 lines (246 loc) · 8.34 KB
/
Copy pathEvent.cpp
File metadata and controls
300 lines (246 loc) · 8.34 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
/*
Copyright 2017 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/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include "OpcUaStackCore/Base/os.h"
#include "OpcUaStackCore/Base/Log.h"
#include "OpcUaStackCore/Base/ConfigXml.h"
#include "OpcUaStackCore/StandardEventType/BaseEventType.h"
#include "OpcUaStackCore/StandardEventType/AlarmConditionType.h"
#include "OpcUaStackCore/Utility/Environment.h"
#include "OpcUaStackServer/ServiceSetApplication/ApplicationService.h"
#include "OpcUaStackServer/ServiceSetApplication/NodeReferenceApplication.h"
#include "ASNeG-Demo/Library/Event.h"
#include "ASNeG-Demo/CustomerEventType/CustomerEventType.h"
namespace OpcUaServerApplicationDemo
{
Event::Event(void)
: ioThread_(nullptr)
, applicationServiceIf_(nullptr)
, applicationInfo_(nullptr)
, namespaceIndex_(0)
, counter_(0)
, eventItemStartCallback_(boost::bind(&Event::eventItemStartCallback, this, _1))
, eventItemStopCallback_(boost::bind(&Event::eventItemStopCallback, this, _1))
{
}
Event::~Event(void)
{
}
bool
Event::startup(
IOThread& ioThread,
ApplicationServiceIf& applicationServiceIf,
ApplicationInfo* applicationInfo
)
{
Log(Debug, "Event::startup");
ioThread_ = &ioThread;
applicationServiceIf_ = &applicationServiceIf;
applicationInfo_ = applicationInfo;
// read namespace array from opc ua server
if (!getNamespaceInfo()) {
return false;
}
// register event callbacks
if (!registerEventCallbacks()) {
return false;
}
startTimerLoop();
return true;
}
bool
Event::shutdown(void)
{
Log(Debug, "Event::shutdown");
return true;
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
//
// private functions
//
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
void
Event::eventItemStartCallback(ApplicationEventItemStartContext* applicationEventItemStartContext)
{
Log(Debug, "Event::eventItemStartCallback");
}
void
Event::eventItemStopCallback(ApplicationEventItemStopContext* applicationEventItemStopContext)
{
Log(Debug, "Event::eventItemStopCallback");
}
bool
Event::registerEventCallbacks(void)
{
Log(Debug, "registern event callbacks");
ServiceTransactionRegisterForwardGlobal::SPtr trx = constructSPtr<ServiceTransactionRegisterForwardGlobal>();
RegisterForwardGlobalRequest::SPtr req = trx->request();
RegisterForwardGlobalResponse::SPtr res = trx->response();
req->forwardGlobalSync()->eventItemStartService().setCallback(eventItemStartCallback_);
req->forwardGlobalSync()->eventItemStopService().setCallback(eventItemStopCallback_);
applicationServiceIf_->sendSync(trx);
if (trx->statusCode() != Success) {
std::cout << "response error" << std::endl;
return false;
}
if (res->statusCode() != Success) {
std::cout << "register event callbacks error" << std::endl;
return false;
}
return true;
}
bool
Event::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/Event/") {
namespaceIndex_ = it->first;
return true;
}
}
Log(Error, "namespace not found in configuration")
.parameter("NamespaceUri", "http://ASNeG-Demo.de/Event/");
return false;
}
void
Event::startTimerLoop(void)
{
Log(Debug, "start Event loop");
slotTimerElement_ = constructSPtr<SlotTimerElement>();
slotTimerElement_->callback().reset(boost::bind(&Event::timerLoop, this));
slotTimerElement_->expireTime(boost::posix_time::microsec_clock::local_time(), 5000);
ioThread_->slotTimer()->start(slotTimerElement_);
}
void
Event::timerLoop(void)
{
Log(Debug, "send Event");
sendEvent11();
sendEvent12();
sendEvent21();
}
void
Event::sendEvent11(void)
{
BaseEventType::SPtr baseEventType = constructSPtr<BaseEventType>();
EventBase::SPtr eventBase;
OpcUaVariant::SPtr variant;
ServiceTransactionFireEvent::SPtr trx = constructSPtr<ServiceTransactionFireEvent>();
FireEventRequest::SPtr req = trx->request();
FireEventResponse::SPtr res = trx->response();
// set message value
std::stringstream ss;
counter_++;
ss << "BaseEventType: Event message " << counter_;
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);
// send event on node Event11
req->nodeId().set("Event11", namespaceIndex_);
eventBase = baseEventType;
req->eventBase(eventBase);
applicationServiceIf_->sendSync(trx);
if (trx->statusCode() != Success) {
std::cout << "event response error" << std::endl;
}
}
void
Event::sendEvent12(void)
{
CustomerEventType::SPtr customerEventType = constructSPtr<CustomerEventType>();
EventBase::SPtr eventBase;
OpcUaVariant::SPtr variant;
ServiceTransactionFireEvent::SPtr trx = constructSPtr<ServiceTransactionFireEvent>();
FireEventRequest::SPtr req = trx->request();
FireEventResponse::SPtr res = trx->response();
// set message value
std::stringstream ss;
counter_++;
ss << "CustomerEventType: Event message " << counter_;
variant = constructSPtr<OpcUaVariant>();
variant->setValue(OpcUaLocalizedText("de", ss.str()));
customerEventType->message(variant);
// set severity message
variant = constructSPtr<OpcUaVariant>();
variant->setValue((OpcUaUInt16)100);
customerEventType->severity(variant);
// set variable1
variant = constructSPtr<OpcUaVariant>();
variant->setValue((OpcUaDouble)1234);
customerEventType->variable1(variant);
// set variable2
variant = constructSPtr<OpcUaVariant>();
variant->setValue((OpcUaDouble)5678);
customerEventType->variable2(variant);
// send event on node Event12
req->nodeId().set("Event12", namespaceIndex_);
eventBase = customerEventType;
req->eventBase(eventBase);
applicationServiceIf_->sendSync(trx);
if (trx->statusCode() != Success) {
std::cout << "event response error" << std::endl;
}
}
void
Event::sendEvent21(void)
{
BaseEventType::SPtr eventType = constructSPtr<AlarmConditionType>();
EventBase::SPtr eventBase;
OpcUaVariant::SPtr variant;
ServiceTransactionFireEvent::SPtr trx = constructSPtr<ServiceTransactionFireEvent>();
FireEventRequest::SPtr req = trx->request();
FireEventResponse::SPtr res = trx->response();
// set message value
std::stringstream ss;
counter_++;
ss << "BaseEventType: Event message " << counter_;
variant = constructSPtr<OpcUaVariant>();
variant->setValue(OpcUaLocalizedText("de", ss.str()));
eventType->message(variant);
// set severity message
variant = constructSPtr<OpcUaVariant>();
variant->setValue((OpcUaUInt16)100);
eventType->severity(variant);
// send event on node Event11
req->nodeId().set("Event21", namespaceIndex_);
eventBase = eventType;
req->eventBase(eventBase);
applicationServiceIf_->sendSync(trx);
if (trx->statusCode() != Success) {
std::cout << "event response error" << std::endl;
}
}
}