소스 검색

Add an example for local MonitoredItems

Julius Pfrommer 6 년 전
부모
커밋
ffb9ee5218
4개의 변경된 파일72개의 추가작업 그리고 15개의 파일을 삭제
  1. 3 0
      doc/CMakeLists.txt
  2. 4 0
      examples/CMakeLists.txt
  3. 0 15
      examples/tutorial_server_datasource.c
  4. 65 0
      examples/tutorial_server_monitoreditems.c

+ 3 - 0
doc/CMakeLists.txt

@@ -41,6 +41,7 @@ generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_server_firststeps.c ${DOC_S
 generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_server_variable.c ${DOC_SRC_DIR}/tutorial_server_variable.rst)
 generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_server_variabletype.c ${DOC_SRC_DIR}/tutorial_server_variabletype.rst)
 generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_server_datasource.c ${DOC_SRC_DIR}/tutorial_server_datasource.rst)
+generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_server_monitoreditems.c ${DOC_SRC_DIR}/tutorial_server_monitoreditems.rst)
 generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_server_object.c ${DOC_SRC_DIR}/tutorial_server_object.rst)
 generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_server_method.c ${DOC_SRC_DIR}/tutorial_server_method.rst)
 generate_rst(${PROJECT_SOURCE_DIR}/examples/tutorial_client_firststeps.c ${DOC_SRC_DIR}/tutorial_client_firststeps.rst)
@@ -63,6 +64,7 @@ add_custom_target(doc_latex ${SPHINX_EXECUTABLE}
           ${DOC_SRC_DIR}/tutorial_server_variable.rst
           ${DOC_SRC_DIR}/tutorial_server_variabletype.rst
           ${DOC_SRC_DIR}/tutorial_server_datasource.rst
+          ${DOC_SRC_DIR}/tutorial_server_monitoreditems.rst
           ${DOC_SRC_DIR}/tutorial_server_object.rst
           ${DOC_SRC_DIR}/tutorial_server_method.rst
           ${DOC_SRC_DIR}/tutorial_pubsub_publish.rst
@@ -96,6 +98,7 @@ add_custom_target(doc ${SPHINX_EXECUTABLE}
           ${DOC_SRC_DIR}/tutorial_server_variable.rst
           ${DOC_SRC_DIR}/tutorial_server_variabletype.rst
           ${DOC_SRC_DIR}/tutorial_server_datasource.rst
+          ${DOC_SRC_DIR}/tutorial_server_monitoreditems.rst
           ${DOC_SRC_DIR}/tutorial_server_object.rst
           ${DOC_SRC_DIR}/tutorial_server_method.rst
           ${DOC_SRC_DIR}/tutorial_pubsub_publish.rst

+ 4 - 0
examples/CMakeLists.txt

@@ -38,6 +38,10 @@ add_example(tutorial_server_variable tutorial_server_variable.c)
 
 add_example(tutorial_server_datasource tutorial_server_datasource.c)
 
+if(UA_ENABLE_SUBSCRIPTIONS)
+add_example(tutorial_server_monitoreditems tutorial_server_monitoreditems.c)
+endif()
+
 add_example(tutorial_server_variabletype tutorial_server_variabletype.c)
 
 add_example(tutorial_server_object tutorial_server_object.c)

+ 0 - 15
examples/tutorial_server_datasource.c

@@ -175,18 +175,3 @@ int main(void) {
     UA_ServerConfig_delete(config);
     return (int)retval;
 }
-
-/**
- * DataChange Notifications
- * ^^^^^^^^^^^^^^^^^^^^^^^^
- * A client that is interested in the current value of a variable does not need
- * to regularly poll the variable. Instead, he can use the Subscription
- * mechanism to be notified about changes.
- *
- * Within a Subscription, the client adds so-called MonitoredItems. A DataChange
- * MonitoredItem defines a node attribute (usually the value attribute) that is
- * monitored for changes. The server internally reads the value in the defined
- * interval and generates the appropriate notifications. The three ways of
- * updating node values discussed above are all usable in combination with
- * notifications. That is because notifications use the standard *Read* service
- * to look for value changes. */

+ 65 - 0
examples/tutorial_server_monitoreditems.c

@@ -0,0 +1,65 @@
+/* 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, he 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 send to the client.
+ *
+ * The local user can add MonitoredItems as well. Locally, the MonitoredItems to
+ * not go via a Subscription and each have an individual callback method and a
+ * context pointer.
+ */
+
+#include <signal.h>
+#include "open62541.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. */
+
+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_ServerConfig *config = UA_ServerConfig_new_default();
+    UA_Server *server = UA_Server_new(config);
+
+    addMonitoredItemToCurrentTimeVariable(server);
+
+    UA_StatusCode retval = UA_Server_run(server, &running);
+    UA_Server_delete(server);
+    UA_ServerConfig_delete(config);
+    return (int)retval;
+}