forked from open62541/open62541
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tutorial_server_monitoreditems.c
70 lines (58 loc) · 2.68 KB
/
tutorial_server_monitoreditems.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
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
/**
* Observing Attributes with Local MonitoredItems
* ----------------------------------------------
*
* A client that is interested in the current value of a variable does not need
* to regularly poll the variable. Instead, the client can use the Subscription
* mechanism to be notified about changes.
*
* So-called MonitoredItems define which values (node attributes) and events the
* client wants to monitor. Under the right conditions, a notification is
* created and added to the Subscription. The notifications currently in the
* queue are regularly sent to the client.
*
* The local user can add MonitoredItems as well. Locally, the MonitoredItems do
* not go via a Subscription and each have an individual callback method and a
* context pointer.
*/
#include <open62541/client_subscriptions.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
#include <open62541/server_config_default.h>
#include <signal.h>
#include <stdlib.h>
static void
dataChangeNotificationCallback(UA_Server *server, UA_UInt32 monitoredItemId,
void *monitoredItemContext, const UA_NodeId *nodeId,
void *nodeContext, UA_UInt32 attributeId,
const UA_DataValue *value) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Received Notification");
}
static void
addMonitoredItemToCurrentTimeVariable(UA_Server *server) {
UA_NodeId currentTimeNodeId =
UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
UA_MonitoredItemCreateRequest monRequest =
UA_MonitoredItemCreateRequest_default(currentTimeNodeId);
monRequest.requestedParameters.samplingInterval = 100.0; /* 100 ms interval */
UA_Server_createDataChangeMonitoredItem(server, UA_TIMESTAMPSTORETURN_SOURCE,
monRequest, NULL, dataChangeNotificationCallback);
}
/** It follows the main server code, making use of the above definitions. */
static volatile UA_Boolean running = true;
static void stopHandler(int sign) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = false;
}
int main(void) {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
UA_Server *server = UA_Server_new();
UA_ServerConfig_setDefault(UA_Server_getConfig(server));
addMonitoredItemToCurrentTimeVariable(server);
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}