Browse Source

Allow to use NS ID script by adding additional parameter for NS name

This change allows to use the generate_nodeid_header script to create a
header file which contains defines for custom nodesets as shown in the
example.
Stefan Profanter 6 years ago
parent
commit
fd304ae645

+ 4 - 4
CMakeLists.txt

@@ -621,12 +621,12 @@ add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes.h
         DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_statuscode_descriptions.py
                 ${UA_FILE_STATUSCODES})
 
-# nodeid explanation
+# Header containing defines for all NodeIds
 add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
         PRE_BUILD
-        COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_nodeid_descriptions.py
-        ${UA_FILE_NODEIDS}  ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
-        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeid_descriptions.py
+        COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_nodeid_header.py
+        ${UA_FILE_NODEIDS}  ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids NS0
+        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeid_header.py
         ${UA_FILE_NODEIDS})
 
 # we need a custom target to avoid that the generator is called concurrently and

+ 11 - 1
examples/nodeset/CMakeLists.txt

@@ -25,7 +25,17 @@ add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/example_nodeset.c
                    ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/backend_open62541_datatypes.py
                    ${PROJECT_SOURCE_DIR}/examples/nodeset/server_nodeset.xml)
 
-add_example(server_nodeset server_nodeset.c ${PROJECT_BINARY_DIR}/src_generated/example_nodeset.c)
+
+# The .csv file can be created from within UaModeler or manually
+
+add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/example_nodeset_ids.h
+                   PRE_BUILD
+                   COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_nodeid_header.py
+                   ${PROJECT_SOURCE_DIR}/examples/nodeset/server_nodeset.csv  ${PROJECT_BINARY_DIR}/src_generated/example_nodeset_ids EXAMPLE_NS
+                   DEPENDS ${PROJECT_SOURCE_DIR}/tools/generate_nodeid_header.py
+                   ${PROJECT_SOURCE_DIR}/examples/nodeset/server_nodeset.csv)
+
+add_example(server_nodeset server_nodeset.c ${PROJECT_BINARY_DIR}/src_generated/example_nodeset.c ${PROJECT_BINARY_DIR}/src_generated/example_nodeset_ids.h)
 if(UA_COMPILE_AS_CXX)
     set_source_files_properties(${PROJECT_BINARY_DIR}/src_generated/example_nodeset.c PROPERTIES LANGUAGE CXX)
 endif()

+ 13 - 0
examples/nodeset/server_nodeset.c

@@ -7,6 +7,7 @@
 /* Files example_namespace.h and example_namespace.c are created from server_nodeset.xml in the
  * /src_generated directory by CMake */
 #include "example_nodeset.h"
+#include "example_nodeset_ids.h"
 
 UA_Boolean running = true;
 
@@ -29,8 +30,20 @@ int main(int argc, char** argv) {
         "Check previous output for any error.");
         retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
     } else {
+
+        // Do some additional stuff with the nodes
+
+        // this will just get the namespace index, since it is already added to the server
+        UA_UInt16 nsIdx = UA_Server_addNamespace(server, "http://yourorganisation.org/test/");
+
+        UA_NodeId testInstanceId = UA_NODEID_NUMERIC(nsIdx, UA_EXAMPLE_NSID_TESTINSTANCE);
+
+        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "The testInstance has ns=%d;id=%d",
+                    testInstanceId.namespaceIndex, testInstanceId.identifier.numeric);
+
         retval = UA_Server_run(server, &running);
     }
+
     UA_Server_delete(server);
     UA_ServerConfig_delete(config);
     return (int)retval;

+ 5 - 0
examples/nodeset/server_nodeset.csv

@@ -0,0 +1,5 @@
+testType,1001,ObjectType
+testInstance,5001,Object
+testFolder,5002,Object
+testType_Var1,6001,Variable
+testInstance_Var1,6002,Variable

+ 8 - 8
tools/generate_nodeid_descriptions.py

@@ -15,6 +15,7 @@ from io import open
 parser = argparse.ArgumentParser()
 parser.add_argument('statuscodes', help='path/to/Opc.Ua.NodeIds.csv')
 parser.add_argument('outfile', help='outfile w/o extension')
+parser.add_argument('namespace', help='NS0')
 args = parser.parse_args()
 
 rows = []
@@ -33,23 +34,22 @@ def printh(string):
 
 printh(u'''/*---------------------------------------------------------
  * Autogenerated -- do not modify
- * Generated from %s with script %s
+ * Generated from {0} with script {1}
  *-------------------------------------------------------*/
 
-#ifndef UA_NODEIDS_H_
-#define UA_NODEIDS_H_
+#ifndef UA_NODEIDS_{2}_H_
+#define UA_NODEIDS_{2}_H_
 
 /**
  * Namespace Zero NodeIds
  * ----------------------
  * Numeric identifiers of standard-defined nodes in namespace zero. The
- * following definitions are autogenerated from the ``NodeIds.csv`` file
- * provided with the OPC UA standard. */
-''' % (args.statuscodes, sys.argv[0]))
+ * following definitions are autogenerated from the ``{0}`` file */
+'''.format(args.statuscodes, sys.argv[0], args.namespace))
 
 for row in rows:
-    printh(u"#define UA_NS0ID_%s %s /* %s */" % (row[0].upper(), row[1], row[2]))
+    printh(u"#define UA_{namespace}ID_{name} {id} /* {description} */".format(namespace=args.namespace, name=row[0].upper(), id=row[1], description=row[2]))
 
-printh(u'''#endif /* UA_NODEIDS_H_ */ ''')
+printh(u'''#endif /* UA_NODEIDS_{0}_H_ */ '''.format(args.namespace))
 
 fh.close()