tutorial_server_object.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * Working with Objects and Object Types
  5. * -------------------------------------
  6. *
  7. * Using objects to structure information models
  8. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. * Assume a situation where we want to model a set of pumps and their runtime
  10. * state in an OPC UA information model. Of course, all pump representations
  11. * should follow the same basic structure, For example, we might have graphical
  12. * representation of pumps in a SCADA visualisation that shall be resuable for
  13. * all pumps.
  14. *
  15. * Following the object-oriented programming paradigm, every pump is represented
  16. * by an object with the following layout:
  17. *
  18. * .. graphviz::
  19. *
  20. * digraph tree {
  21. *
  22. * fixedsize=true;
  23. * node [width=2, height=0, shape=box, fillcolor="#E5E5E5", concentrate=true]
  24. *
  25. * node_root [label=< <I>ObjectNode</I><BR/>Pump >]
  26. *
  27. * { rank=same
  28. * point_1 [shape=point]
  29. * node_1 [label=< <I>VariableNode</I><BR/>ManufacturerName >] }
  30. * node_root -> point_1 [arrowhead=none]
  31. * point_1 -> node_1 [label="hasComponent"]
  32. *
  33. * { rank=same
  34. * point_2 [shape=point]
  35. * node_2 [label=< <I>VariableNode</I><BR/>ModelName >] }
  36. * point_1 -> point_2 [arrowhead=none]
  37. * point_2 -> node_2 [label="hasComponent"]
  38. *
  39. * { rank=same
  40. * point_4 [shape=point]
  41. * node_4 [label=< <I>VariableNode</I><BR/>Status >] }
  42. * point_2 -> point_4 [arrowhead=none]
  43. * point_4 -> node_4 [label="hasComponent"]
  44. *
  45. * { rank=same
  46. * point_5 [shape=point]
  47. * node_5 [label=< <I>VariableNode</I><BR/>MotorRPM >] }
  48. * point_4 -> point_5 [arrowhead=none]
  49. * point_5 -> node_5 [label="hasComponent"]
  50. *
  51. * }
  52. *
  53. * The following code manually defines a pump and its member variables. We omit
  54. * setting constraints on the variable values as this is not the focus of this
  55. * tutorial and was already covered. */
  56. #include <signal.h>
  57. #include "open62541.h"
  58. static void
  59. manuallyDefinePump(UA_Server *server) {
  60. UA_NodeId pumpId; /* get the nodeid assigned by the server */
  61. UA_ObjectAttributes oAttr;
  62. UA_ObjectAttributes_init(&oAttr);
  63. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Pump (Manual)");
  64. UA_Server_addObjectNode(server, UA_NODEID_NULL,
  65. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  66. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  67. UA_QUALIFIEDNAME(1, "Pump (Manual)"), UA_NODEID_NULL,
  68. oAttr, NULL, &pumpId);
  69. UA_VariableAttributes mnAttr;
  70. UA_VariableAttributes_init(&mnAttr);
  71. UA_String manufacturerName = UA_STRING("Pump King Ltd.");
  72. UA_Variant_setScalar(&mnAttr.value, &manufacturerName, &UA_TYPES[UA_TYPES_STRING]);
  73. mnAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ManufacturerName");
  74. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
  75. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  76. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  77. UA_NODEID_NULL, mnAttr, NULL, NULL);
  78. UA_VariableAttributes modelAttr;
  79. UA_VariableAttributes_init(&modelAttr);
  80. UA_String modelName = UA_STRING("Mega Pump 3000");
  81. UA_Variant_setScalar(&modelAttr.value, &modelName, &UA_TYPES[UA_TYPES_STRING]);
  82. modelAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ModelName");
  83. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
  84. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  85. UA_QUALIFIEDNAME(1, "ModelName"),
  86. UA_NODEID_NULL, modelAttr, NULL, NULL);
  87. UA_VariableAttributes statusAttr;
  88. UA_VariableAttributes_init(&statusAttr);
  89. UA_Boolean status = true;
  90. UA_Variant_setScalar(&statusAttr.value, &status, &UA_TYPES[UA_TYPES_BOOLEAN]);
  91. statusAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Status");
  92. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
  93. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  94. UA_QUALIFIEDNAME(1, "Status"),
  95. UA_NODEID_NULL, statusAttr, NULL, NULL);
  96. UA_VariableAttributes rpmAttr;
  97. UA_VariableAttributes_init(&rpmAttr);
  98. UA_Double rpm = 50.0;
  99. UA_Variant_setScalar(&rpmAttr.value, &rpm, &UA_TYPES[UA_TYPES_DOUBLE]);
  100. rpmAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MotorRPM");
  101. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpId,
  102. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  103. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  104. UA_NODEID_NULL, rpmAttr, NULL, NULL);
  105. }
  106. /**
  107. * Object types, type hierarchies and instantiation
  108. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  109. * Building up each object manually requires us to write a lot of code.
  110. * Furthermore, there is no way for clients to detect that an object represents
  111. * a pump. (We might use naming conventions or similar to detect pumps. But
  112. * that's not exactly a clean solution.) Furthermore, we might have more devices
  113. * than just pumps. And we require all devices to share some common structure.
  114. * The solution is to define ObjectTypes in a hierarchy with inheritance
  115. * relations.
  116. *
  117. * .. graphviz::
  118. *
  119. * digraph tree {
  120. *
  121. * fixedsize=true;
  122. * node [width=2, height=0, shape=box, fillcolor="#E5E5E5", concentrate=true]
  123. *
  124. * node_root [label=< <I>ObjectTypeNode</I><BR/>Device >]
  125. *
  126. * { rank=same
  127. * point_1 [shape=point]
  128. * node_1 [label=< <I>VariableNode</I><BR/>ManufacturerName >] }
  129. * node_root -> point_1 [arrowhead=none]
  130. * point_1 -> node_1 [label="hasComponent"]
  131. *
  132. * { rank=same
  133. * point_2 [shape=point]
  134. * node_2 [label=< <I>VariableNode</I><BR/>ModelName >] }
  135. * point_1 -> point_2 [arrowhead=none]
  136. * point_2 -> node_2 [label="hasComponent"]
  137. *
  138. * { rank=same
  139. * point_3 [shape=point]
  140. * node_3 [label=< <I>ObjectTypeNode</I><BR/>Pump >] }
  141. * point_2 -> point_3 [arrowhead=none]
  142. * point_3 -> node_3 [label="hasSubtype"]
  143. *
  144. * { rank=same
  145. * point_4 [shape=point]
  146. * node_4 [label=< <I>VariableNode</I><BR/>Status >] }
  147. * node_3 -> point_4 [arrowhead=none]
  148. * point_4 -> node_4 [label="hasComponent"]
  149. *
  150. * { rank=same
  151. * point_5 [shape=point]
  152. * node_5 [label=< <I>VariableNode</I><BR/>MotorRPM >] }
  153. * point_4 -> point_5 [arrowhead=none]
  154. * point_5 -> node_5 [label="hasComponent"]
  155. *
  156. * }
  157. *
  158. */
  159. /* predefined identifier for later use */
  160. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  161. static void
  162. defineObjectTypes(UA_Server *server) {
  163. /* Define the object type for "Device" */
  164. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  165. UA_ObjectTypeAttributes dtAttr;
  166. UA_ObjectTypeAttributes_init(&dtAttr);
  167. dtAttr.displayName = UA_LOCALIZEDTEXT("en_US", "DeviceType");
  168. UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  169. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  170. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  171. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  172. NULL, &deviceTypeId);
  173. UA_VariableAttributes mnAttr;
  174. UA_VariableAttributes_init(&mnAttr);
  175. mnAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ManufacturerName");
  176. UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  177. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  178. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  179. UA_NODEID_NULL, mnAttr, NULL, NULL);
  180. UA_VariableAttributes modelAttr;
  181. UA_VariableAttributes_init(&modelAttr);
  182. modelAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ModelName");
  183. UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  184. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  185. UA_QUALIFIEDNAME(1, "ModelName"),
  186. UA_NODEID_NULL, modelAttr, NULL, NULL);
  187. /* Define the object type for "Pump" */
  188. UA_ObjectTypeAttributes ptAttr;
  189. UA_ObjectTypeAttributes_init(&ptAttr);
  190. ptAttr.displayName = UA_LOCALIZEDTEXT("en_US", "PumpType");
  191. UA_Server_addObjectTypeNode(server, pumpTypeId,
  192. deviceTypeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  193. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  194. NULL, NULL);
  195. UA_VariableAttributes statusAttr;
  196. UA_VariableAttributes_init(&statusAttr);
  197. statusAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Status");
  198. statusAttr.valueRank = -1;
  199. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  200. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  201. UA_QUALIFIEDNAME(1, "Status"),
  202. UA_NODEID_NULL, statusAttr, NULL, NULL);
  203. UA_VariableAttributes rpmAttr;
  204. UA_VariableAttributes_init(&rpmAttr);
  205. rpmAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MotorRPM");
  206. rpmAttr.valueRank = -1;
  207. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  208. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  209. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  210. UA_NODEID_NULL, rpmAttr, NULL, NULL);
  211. }
  212. /**
  213. * Now we add the derived ObjectType for the pump that inherits from the device
  214. * object type. The resulting object contains all four inherited child
  215. * variables. The object has a reference of type ``hasTypeDefinition`` to the
  216. * object type. Clients can browse this information at runtime and adjust
  217. * accordingly.
  218. */
  219. static void
  220. addPumpObjectInstance(UA_Server *server, char *name) {
  221. UA_ObjectAttributes oAttr;
  222. UA_ObjectAttributes_init(&oAttr);
  223. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", name);
  224. UA_Server_addObjectNode(server, UA_NODEID_NULL,
  225. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  226. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  227. UA_QUALIFIEDNAME(1, name),
  228. pumpTypeId, /* this refers to the object type
  229. identifier */
  230. oAttr, NULL, NULL);
  231. }
  232. /**
  233. * Often times, we want to run a constructor function on a new object. This is
  234. * especially useful when an object is instantiated at runtime (with the
  235. * AddNodes service) and the integration with an underlying process canot be
  236. * manually defined. In the following constructor example, we simply set the
  237. * pump status to on.
  238. */
  239. UA_Server *s = NULL; /* required to get the server pointer into the constructor
  240. function (will change for v0.3) */
  241. static void *
  242. pumpTypeConstructor(const UA_NodeId instance) {
  243. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "New pump created");
  244. /* Find the NodeId of the status child variable */
  245. UA_RelativePathElement rpe;
  246. UA_RelativePathElement_init(&rpe);
  247. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  248. rpe.isInverse = false;
  249. rpe.includeSubtypes = false;
  250. rpe.targetName = UA_QUALIFIEDNAME(1, "Status");
  251. UA_BrowsePath bp;
  252. UA_BrowsePath_init(&bp);
  253. bp.startingNode = instance;
  254. bp.relativePath.elementsSize = 1;
  255. bp.relativePath.elements = &rpe;
  256. UA_BrowsePathResult bpr =
  257. UA_Server_translateBrowsePathToNodeIds(s, &bp);
  258. if(bpr.statusCode != UA_STATUSCODE_GOOD ||
  259. bpr.targetsSize < 1)
  260. return NULL;
  261. /* Set the status value */
  262. UA_Boolean status = true;
  263. UA_Variant value;
  264. UA_Variant_setScalar(&value, &status, &UA_TYPES[UA_TYPES_BOOLEAN]);
  265. UA_Server_writeValue(s, bpr.targets[0].targetId.nodeId, value);
  266. UA_BrowsePathResult_deleteMembers(&bpr);
  267. /* The return pointer of the constructor is attached to the ObjectNode */
  268. return NULL;
  269. }
  270. static void
  271. addPumpTypeConstructor(UA_Server *server) {
  272. UA_ObjectLifecycleManagement olm;
  273. olm.constructor = pumpTypeConstructor;
  274. olm.destructor = NULL;
  275. UA_Server_setObjectTypeNode_lifecycleManagement(server, pumpTypeId, olm);
  276. }
  277. /** It follows the main server code, making use of the above definitions. */
  278. UA_Boolean running = true;
  279. static void stopHandler(int sign) {
  280. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  281. running = false;
  282. }
  283. int main(void) {
  284. signal(SIGINT, stopHandler);
  285. signal(SIGTERM, stopHandler);
  286. UA_ServerConfig config = UA_ServerConfig_standard;
  287. UA_ServerNetworkLayer nl =
  288. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  289. config.networkLayers = &nl;
  290. config.networkLayersSize = 1;
  291. UA_Server *server = UA_Server_new(config);
  292. s = server; /* required for the constructor */
  293. manuallyDefinePump(server);
  294. defineObjectTypes(server);
  295. addPumpObjectInstance(server, "pump2");
  296. addPumpObjectInstance(server, "pump3");
  297. addPumpTypeConstructor(server);
  298. addPumpObjectInstance(server, "pump4");
  299. addPumpObjectInstance(server, "pump5");
  300. UA_Server_run(server, &running);
  301. UA_Server_delete(server);
  302. nl.deleteMembers(&nl);
  303. return 0;
  304. }