forked from open62541/open62541
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial_client_events.c
186 lines (154 loc) · 6.99 KB
/
tutorial_client_events.c
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
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
#include <open62541/client.h>
#include <open62541/client_config_default.h>
#include <open62541/client_highlevel.h>
#include <open62541/client_subscriptions.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
#include <open62541/server_config_default.h>
#include <open62541/util.h>
#include <signal.h>
#ifdef _MSC_VER
#pragma warning(disable:4996) // warning C4996: 'UA_Client_Subscriptions_addMonitoredEvent': was declared deprecated
#endif
#ifdef __clang__
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
static UA_Boolean running = true;
static void stopHandler(int sig) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
running = false;
}
#ifdef UA_ENABLE_SUBSCRIPTIONS
static void
handler_events(UA_Client *client, UA_UInt32 subId, void *subContext,
UA_UInt32 monId, void *monContext,
size_t nEventFields, UA_Variant *eventFields) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Notification");
/* The context should point to the monId on the stack */
UA_assert(*(UA_UInt32*)monContext == monId);
for(size_t i = 0; i < nEventFields; ++i) {
if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
UA_UInt16 severity = *(UA_UInt16 *)eventFields[i].data;
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Severity: %u", severity);
} else if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
UA_LocalizedText *lt = (UA_LocalizedText *)eventFields[i].data;
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Message: '%.*s'", (int)lt->text.length, lt->text.data);
}
else {
#ifdef UA_ENABLE_TYPEDESCRIPTION
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Don't know how to handle type: '%s'", eventFields[i].type->typeName);
#else
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Don't know how to handle type, enable UA_ENABLE_TYPEDESCRIPTION "
"for typename");
#endif
}
}
}
const size_t nSelectClauses = 2;
static UA_SimpleAttributeOperand *
setupSelectClauses(void) {
UA_SimpleAttributeOperand *selectClauses = (UA_SimpleAttributeOperand*)
UA_Array_new(nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
if(!selectClauses)
return NULL;
for(size_t i =0; i<nSelectClauses; ++i) {
UA_SimpleAttributeOperand_init(&selectClauses[i]);
}
selectClauses[0].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
selectClauses[0].browsePathSize = 1;
selectClauses[0].browsePath = (UA_QualifiedName*)
UA_Array_new(selectClauses[0].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
if(!selectClauses[0].browsePath) {
UA_SimpleAttributeOperand_delete(selectClauses);
return NULL;
}
selectClauses[0].attributeId = UA_ATTRIBUTEID_VALUE;
selectClauses[0].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Message");
selectClauses[1].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
selectClauses[1].browsePathSize = 1;
selectClauses[1].browsePath = (UA_QualifiedName*)
UA_Array_new(selectClauses[1].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
if(!selectClauses[1].browsePath) {
UA_SimpleAttributeOperand_delete(selectClauses);
return NULL;
}
selectClauses[1].attributeId = UA_ATTRIBUTEID_VALUE;
selectClauses[1].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Severity");
return selectClauses;
}
#endif
int main(int argc, char *argv[]) {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
if(argc < 2) {
printf("Usage: tutorial_client_events <opc.tcp://server-url>\n");
return EXIT_FAILURE;
}
UA_Client *client = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(client));
/* opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer */
/* opc.tcp://opcua.demo-this.com:51210/UA/SampleServer */
UA_StatusCode retval = UA_Client_connect(client, argv[1]);
if(retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client);
return EXIT_FAILURE;
}
#ifdef UA_ENABLE_SUBSCRIPTIONS
/* Create a subscription */
UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
NULL, NULL, NULL);
if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
UA_Client_disconnect(client);
UA_Client_delete(client);
return EXIT_FAILURE;
}
UA_UInt32 subId = response.subscriptionId;
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", subId);
/* Add a MonitoredItem */
UA_MonitoredItemCreateRequest item;
UA_MonitoredItemCreateRequest_init(&item);
item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
item.monitoringMode = UA_MONITORINGMODE_REPORTING;
UA_EventFilter filter;
UA_EventFilter_init(&filter);
filter.selectClauses = setupSelectClauses();
filter.selectClausesSize = nSelectClauses;
item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
item.requestedParameters.filter.content.decoded.data = &filter;
item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
UA_UInt32 monId = 0;
UA_MonitoredItemCreateResult result =
UA_Client_MonitoredItems_createEvent(client, subId,
UA_TIMESTAMPSTORETURN_BOTH, item,
&monId, handler_events, NULL);
if(result.statusCode != UA_STATUSCODE_GOOD) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Could not add the MonitoredItem with %s", UA_StatusCode_name(retval));
goto cleanup;
} else {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Monitoring 'Root->Objects->Server', id %u", response.subscriptionId);
}
monId = result.monitoredItemId;
while(running)
retval = UA_Client_run_iterate(client, 100);
/* Delete the subscription */
cleanup:
UA_MonitoredItemCreateResult_clear(&result);
UA_Client_Subscriptions_deleteSingle(client, response.subscriptionId);
UA_Array_delete(filter.selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
#endif
UA_Client_disconnect(client);
UA_Client_delete(client);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}