|
@@ -5,6 +5,18 @@
|
|
|
#include <stdio.h>
|
|
|
#include "open62541.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");
|
|
@@ -14,13 +26,13 @@ static void stopHandler(int sig) {
|
|
|
#ifdef UA_ENABLE_SUBSCRIPTIONS
|
|
|
|
|
|
static void
|
|
|
-handler_events(UA_Client *client,
|
|
|
- const UA_UInt32 monId, const size_t nEventFields,
|
|
|
- const UA_Variant *eventFields, void *context) {
|
|
|
+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*)context == monId);
|
|
|
+ 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])) {
|
|
@@ -91,6 +103,7 @@ int main(int argc, char *argv[]) {
|
|
|
UA_Client *client = UA_Client_new(UA_ClientConfig_default);
|
|
|
|
|
|
/* 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);
|
|
@@ -99,42 +112,59 @@ int main(int argc, char *argv[]) {
|
|
|
|
|
|
#ifdef UA_ENABLE_SUBSCRIPTIONS
|
|
|
/* Create a subscription */
|
|
|
- UA_UInt32 subId = 0;
|
|
|
- retval = UA_Client_Subscription_create(client, &UA_SubscriptionParameters_default,
|
|
|
- NULL, NULL, NULL, &subId);
|
|
|
- if(retval != UA_STATUSCODE_GOOD) {
|
|
|
+ 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 (int)retval;
|
|
|
}
|
|
|
+ UA_UInt32 subId = response.subscriptionId;
|
|
|
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", subId);
|
|
|
|
|
|
/* Add a MonitoredItem */
|
|
|
- UA_NodeId monitorThis = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
|
|
|
+ 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_SimpleAttributeOperand *selectClauses = setupSelectClauses();
|
|
|
+ UA_MonitoredItemCreateResult result =
|
|
|
+ UA_Client_MonitoredItems_createEvent(client, subId,
|
|
|
+ UA_TIMESTAMPSTORETURN_BOTH, item,
|
|
|
+ &monId, handler_events, NULL);
|
|
|
|
|
|
- retval = UA_Client_Subscriptions_addMonitoredEvent(client, subId, monitorThis,
|
|
|
- UA_ATTRIBUTEID_EVENTNOTIFIER,
|
|
|
- selectClauses, nSelectClauses,
|
|
|
- NULL, 0, &handler_events, &monId, &monId);
|
|
|
- if(retval != UA_STATUSCODE_GOOD) {
|
|
|
+ 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", subId);
|
|
|
+ "Monitoring 'Root->Objects->Server', id %u", response.subscriptionId);
|
|
|
}
|
|
|
|
|
|
- while (running)
|
|
|
+ monId = result.monitoredItemId;
|
|
|
+
|
|
|
+ while(running)
|
|
|
UA_Client_runAsync(client, 100);
|
|
|
|
|
|
/* Delete the subscription */
|
|
|
cleanup:
|
|
|
- UA_Client_Subscription_delete(client, subId);
|
|
|
- UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
|
|
|
+ UA_MonitoredItemCreateResult_deleteMembers(&result);
|
|
|
+ UA_Client_Subscriptions_deleteSingle(client, response.subscriptionId);
|
|
|
+ UA_Array_delete(filter.selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
|
|
|
#endif
|
|
|
|
|
|
UA_Client_disconnect(client);
|