Ver código fonte

Use new client subscriptions API in examples

StalderT 6 anos atrás
pai
commit
1cd9c469e1
3 arquivos alterados com 87 adições e 44 exclusões
  1. 21 13
      examples/client.c
  2. 17 12
      examples/client_subscription_loop.c
  3. 49 19
      examples/tutorial_client_events.c

+ 21 - 13
examples/client.c

@@ -6,7 +6,8 @@
 
 #ifdef UA_ENABLE_SUBSCRIPTIONS
 static void
-handler_TheAnswerChanged(UA_Client *client, UA_UInt32 monId, UA_DataValue *value, void *context) {
+handler_TheAnswerChanged(UA_Client *client, UA_UInt32 subId, void *subContext,
+                         UA_UInt32 monId, void *monContext, UA_DataValue *value) {
     printf("The Answer has changed!\n");
 }
 #endif
@@ -93,18 +94,25 @@ int main(int argc, char *argv[]) {
 
 #ifdef UA_ENABLE_SUBSCRIPTIONS
     /* Create a subscription */
-    UA_UInt32 subId = 0;
-    UA_Client_Subscription_create(client, &UA_SubscriptionParameters_default,
-                                  NULL, NULL, NULL, &subId);
-    if(subId)
+    UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
+    UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
+                                                                            NULL, NULL, NULL);
+
+    UA_UInt32 subId = response.subscriptionId;
+    if(response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
         printf("Create subscription succeeded, id %u\n", subId);
-    /* Add a MonitoredItem */
-    UA_NodeId monitorThis = UA_NODEID_STRING(1, "the.answer");
-    UA_UInt32 monId = 0;
-    UA_Client_Subscriptions_addMonitoredItem(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE,
-                                             &handler_TheAnswerChanged, NULL, &monId, 250);
-    if(monId)
-        printf("Monitoring 'the.answer', id %u\n", monId);
+
+    UA_MonitoredItemCreateRequest monRequest =
+        UA_MonitoredItemCreateRequest_default(UA_NODEID_STRING(1, "the.answer"));
+
+    UA_MonitoredItemCreateResult monResponse =
+    UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
+                                              UA_TIMESTAMPSTORETURN_BOTH,
+                                              monRequest, NULL, handler_TheAnswerChanged, NULL);
+    if(monResponse.statusCode == UA_STATUSCODE_GOOD)
+        printf("Monitoring 'the.answer', id %u\n", monResponse.monitoredItemId);
+
+
     /* The first publish request should return the initial value of the variable */
     UA_Client_runAsync(client, 1000);
 #endif
@@ -151,7 +159,7 @@ int main(int argc, char *argv[]) {
     /* Take another look at the.answer */
     UA_Client_runAsync(client, 100);
     /* Delete the subscription */
-    if(!UA_Client_Subscription_delete(client, subId))
+    if(UA_Client_Subscriptions_deleteSingle(client, subId) == UA_STATUSCODE_GOOD)
         printf("Subscription removed\n");
 #endif
 

+ 17 - 12
examples/client_subscription_loop.c

@@ -43,7 +43,8 @@ static void stopHandler(int sign) {
 }
 
 static void
-handler_currentTimeChanged(UA_Client *client, UA_UInt32 monId, UA_DataValue *value, void *context) {
+handler_currentTimeChanged(UA_Client *client, UA_UInt32 subId, void *subContext,
+                           UA_UInt32 monId, void *monContext, UA_DataValue *value) {
     UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "currentTime has changed!");
     if(UA_Variant_hasScalarType(&value->value, &UA_TYPES[UA_TYPES_DATETIME])) {
         UA_DateTime raw_date = *(UA_DateTime *) value->value.data;
@@ -74,21 +75,25 @@ stateCallback (UA_Client *client, UA_ClientState clientState) {
             UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "A session with the server is open");
             /* A new session was created. We need to create the subscription. */
             /* Create a subscription */
-            UA_UInt32 subId = 0;
-            UA_StatusCode retval = UA_Client_Subscription_create(client, &UA_SubscriptionParameters_default,
-                                                   NULL, NULL, deleteSubscriptionCallback, &subId);
-            if(retval == UA_STATUSCODE_GOOD)
-                UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", subId);
+            UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
+            UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
+                                                                                    NULL, NULL, deleteSubscriptionCallback);
+
+            if(response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
+                UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", response.subscriptionId);
             else
                 return;
 
             /* Add a MonitoredItem */
-            UA_UInt32 monId = 0;
-            UA_NodeId monitorThis = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
-            UA_Client_Subscriptions_addMonitoredItem(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE,
-                                                     &handler_currentTimeChanged, NULL, &monId, 250);
-            if(monId)
-                UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Monitoring UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME', id %u", monId);
+            UA_MonitoredItemCreateRequest monRequest =
+                UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
+
+            UA_MonitoredItemCreateResult monResponse =
+                UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
+                                                          UA_TIMESTAMPSTORETURN_BOTH,
+                                                          monRequest, NULL, handler_currentTimeChanged, NULL);
+            if(monResponse.statusCode == UA_STATUSCODE_GOOD)
+                UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Monitoring UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME', id %u", monResponse.monitoredItemId);
         }
         break;
         case UA_CLIENTSTATE_SESSION_RENEWED:

+ 49 - 19
examples/tutorial_client_events.c

@@ -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);