Browse Source

Add definitions for local MonitoredItems

Julius Pfrommer 6 years ago
parent
commit
0f0e6ae9ac

+ 51 - 0
include/ua_server.h

@@ -757,6 +757,57 @@ UA_Server_setVariableNode_valueCallback(UA_Server *server,
                                         const UA_NodeId nodeId,
                                         const UA_ValueCallback callback);
 
+/**
+ * .. _local-monitoreditems:
+ *
+ * Local MonitoredItems
+ * ^^^^^^^^^^^^^^^^^^^^
+ *
+ * MonitoredItems are used with the Subscription mechanism of OPC UA to
+ * transported notifications for data changes and events. MonitoredItems can
+ * also be registered locally. Notifications are then forwarded to a
+ * user-defined callback instead of a remote client. */
+
+typedef void (*UA_Server_DataChangeNotificationCallback)
+    (UA_Server *server, UA_UInt32 monitoredItemId, void *monitoredItemContext,
+     const UA_NodeId *nodeId, void *nodeContext, UA_UInt32 attributeId,
+     const UA_DataValue *value);
+
+typedef void (*UA_Server_EventNotificationCallback)
+    (UA_Server *server, UA_UInt32 monId, void *monContext,
+     size_t nEventFields, const UA_Variant *eventFields);
+
+/* Create a local MonitoredItem with a sampling interval that detects data
+ * changes.
+ *
+ * @param server The server executing the MonitoredItem
+ * @timestampsToReturn Shall timestamps be added to the value for the callback?
+ * @item The parameters of the new MonitoredItem. Note that the attribute of the
+ *       ReadValueId (the node that is monitored) can not be
+ *       ``UA_ATTRIBUTEID_EVENTNOTIFIER``. A different callback type needs to be
+ *       registered for event notifications.
+ * @monitoredItemContext A pointer that is forwarded with the callback
+ * @callback The callback that is executed on detected data changes
+ *
+ * @return Returns a description of the created MonitoredItem. The structure
+ * also contains a StatusCode (in case of an error) and the identifier of the
+ * new MonitoredItem. */
+UA_MonitoredItemCreateResult UA_EXPORT
+UA_Server_createDataChangeMonitoredItem(UA_Server *server,
+          UA_TimestampsToReturn timestampsToReturn,
+          const UA_MonitoredItemCreateRequest item,
+          void *monitoredItemContext,
+          UA_Server_DataChangeNotificationCallback callback);
+
+/* UA_MonitoredItemCreateResult UA_EXPORT */
+/* UA_Server_createEventMonitoredItem(UA_Server *server, */
+/*           UA_TimestampsToReturn timestampsToReturn, */
+/*           const UA_MonitoredItemCreateRequest item, void *context, */
+/*           UA_Server_EventNotificationCallback callback); */
+
+UA_StatusCode UA_EXPORT
+UA_Server_deleteMonitoredItem(UA_Server *server, UA_UInt32 monitoredItemId);
+
 /**
  * Method Callbacks
  * ^^^^^^^^^^^^^^^^

+ 12 - 0
src/server/ua_server.c

@@ -22,6 +22,10 @@
 #include "ua_namespaceinit_generated.h"
 #endif
 
+#ifdef UA_ENABLE_SUBSCRIPTIONS
+#include "ua_subscription.h"
+#endif
+
 /**********************/
 /* Namespace Handling */
 /**********************/
@@ -105,6 +109,14 @@ void UA_Server_delete(UA_Server *server) {
     UA_SessionManager_deleteMembers(&server->sessionManager);
     UA_Array_delete(server->namespaces, server->namespacesSize, &UA_TYPES[UA_TYPES_STRING]);
 
+#ifdef UA_ENABLE_SUBSCRIPTIONS
+    UA_MonitoredItem *mon, *mon_tmp;
+    LIST_FOREACH_SAFE(mon, &server->localMonitoredItems, listEntry, mon_tmp) {
+        LIST_REMOVE(mon, listEntry);
+        MonitoredItem_delete(server, mon);
+    }
+#endif
+
 #ifdef UA_ENABLE_PUBSUB
     UA_PubSubManager_delete(server, &server->pubSubManager);
 #endif

+ 19 - 0
src/server/ua_server_internal.h

@@ -30,6 +30,20 @@ extern "C" {
 #include "ua_pubsub_manager.h"
 #endif
 
+#ifdef UA_ENABLE_SUBSCRIPTIONS
+#include "ua_subscription.h"
+
+typedef struct {
+    UA_MonitoredItem monitoredItem;
+    void *context;
+    union {
+        UA_Server_DataChangeNotificationCallback dataChangeCallback;
+        /* UA_Server_EventNotificationCallback eventCallback; */
+    } callback;
+} UA_LocalMonitoredItem;
+
+#endif
+
 #ifdef UA_ENABLE_MULTITHREADING
 
 #include <pthread.h>
@@ -145,6 +159,11 @@ struct UA_Server {
      * the parent and member instantiation */
     UA_Boolean bootstrapNS0;
 
+#ifdef UA_ENABLE_SUBSCRIPTIONS
+    /* To be cast to UA_LocalMonitoredItem to get the callback and context */
+    LIST_HEAD(LocalMonitoredItems, UA_MonitoredItem) localMonitoredItems;
+#endif
+
 #ifdef UA_ENABLE_PUBSUB
     /* Publish/Subscribe toplevel container */
     UA_PubSubManager pubSubManager;

+ 1 - 1
src/server/ua_session.c

@@ -8,8 +8,8 @@
 
 #include "ua_session.h"
 #ifdef UA_ENABLE_SUBSCRIPTIONS
-#include "ua_subscription.h"
 #include "ua_server_internal.h"
+#include "ua_subscription.h"
 #endif
 
 #define UA_SESSION_NONCELENTH 32

+ 1 - 1
src/server/ua_subscription.c

@@ -14,8 +14,8 @@
  *    Copyright 2017 (c) Mattias Bornhager
  */
 
-#include "ua_subscription.h"
 #include "ua_server_internal.h"
+#include "ua_subscription.h"
 
 #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */
 

+ 1 - 1
src/server/ua_subscription_datachange.c

@@ -7,8 +7,8 @@
  *    Copyright 2018 (c) Thomas Stalder, Blue Time Concept SA
  */
 
-#include "ua_subscription.h"
 #include "ua_server_internal.h"
+#include "ua_subscription.h"
 #include "ua_types_encoding_binary.h"
 
 #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */