tutorial_server_object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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<BR/>(mandatory) >] }
  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<BR/>(mandatory) >] }
  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. * Children that are marked mandatory are automatically instantiated together
  159. * with the parent object. This is indicated by a `hasModellingRule` reference
  160. * to an object that representes the `mandatory` modelling rule. */
  161. /* predefined identifier for later use */
  162. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  163. static void
  164. defineObjectTypes(UA_Server *server) {
  165. /* Define the object type for "Device" */
  166. UA_NodeId deviceTypeId; /* get the nodeid assigned by the server */
  167. UA_ObjectTypeAttributes dtAttr;
  168. UA_ObjectTypeAttributes_init(&dtAttr);
  169. dtAttr.displayName = UA_LOCALIZEDTEXT("en_US", "DeviceType");
  170. UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  171. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
  172. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  173. UA_QUALIFIEDNAME(1, "DeviceType"), dtAttr,
  174. NULL, &deviceTypeId);
  175. UA_VariableAttributes mnAttr;
  176. UA_VariableAttributes_init(&mnAttr);
  177. mnAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ManufacturerName");
  178. UA_NodeId manufacturerNameId;
  179. UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  180. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  181. UA_QUALIFIEDNAME(1, "ManufacturerName"),
  182. UA_NODEID_NULL, mnAttr, NULL, &manufacturerNameId);
  183. /* Make the manufacturer name mandatory */
  184. UA_Server_addReference(server, manufacturerNameId,
  185. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  186. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  187. UA_VariableAttributes modelAttr;
  188. UA_VariableAttributes_init(&modelAttr);
  189. modelAttr.displayName = UA_LOCALIZEDTEXT("en_US", "ModelName");
  190. UA_Server_addVariableNode(server, UA_NODEID_NULL, deviceTypeId,
  191. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  192. UA_QUALIFIEDNAME(1, "ModelName"),
  193. UA_NODEID_NULL, modelAttr, NULL, NULL);
  194. /* Define the object type for "Pump" */
  195. UA_ObjectTypeAttributes ptAttr;
  196. UA_ObjectTypeAttributes_init(&ptAttr);
  197. ptAttr.displayName = UA_LOCALIZEDTEXT("en_US", "PumpType");
  198. UA_Server_addObjectTypeNode(server, pumpTypeId,
  199. deviceTypeId, UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  200. UA_QUALIFIEDNAME(1, "PumpType"), ptAttr,
  201. NULL, NULL);
  202. UA_VariableAttributes statusAttr;
  203. UA_VariableAttributes_init(&statusAttr);
  204. statusAttr.displayName = UA_LOCALIZEDTEXT("en_US", "Status");
  205. statusAttr.valueRank = -1;
  206. UA_NodeId statusId;
  207. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  208. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  209. UA_QUALIFIEDNAME(1, "Status"),
  210. UA_NODEID_NULL, statusAttr, NULL, &statusId);
  211. /* Make the status variable mandatory */
  212. UA_Server_addReference(server, statusId,
  213. UA_NODEID_NUMERIC(0, UA_NS0ID_HASMODELLINGRULE),
  214. UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_MODELLINGRULE_MANDATORY), true);
  215. UA_VariableAttributes rpmAttr;
  216. UA_VariableAttributes_init(&rpmAttr);
  217. rpmAttr.displayName = UA_LOCALIZEDTEXT("en_US", "MotorRPM");
  218. rpmAttr.valueRank = -1;
  219. UA_Server_addVariableNode(server, UA_NODEID_NULL, pumpTypeId,
  220. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  221. UA_QUALIFIEDNAME(1, "MotorRPMs"),
  222. UA_NODEID_NULL, rpmAttr, NULL, NULL);
  223. }
  224. /**
  225. * Now we add the derived ObjectType for the pump that inherits from the device
  226. * object type. The resulting object contains all mandatory child variables.
  227. * These are simply copied over from the object type. The object has a reference
  228. * of type ``hasTypeDefinition`` to the object type, so that clients can detect
  229. * the type-instance relation at runtime.
  230. */
  231. static void
  232. addPumpObjectInstance(UA_Server *server, char *name) {
  233. UA_ObjectAttributes oAttr;
  234. UA_ObjectAttributes_init(&oAttr);
  235. oAttr.displayName = UA_LOCALIZEDTEXT("en_US", name);
  236. UA_Server_addObjectNode(server, UA_NODEID_NULL,
  237. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  238. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  239. UA_QUALIFIEDNAME(1, name),
  240. pumpTypeId, /* this refers to the object type
  241. identifier */
  242. oAttr, NULL, NULL);
  243. }
  244. /**
  245. * Often times, we want to run a constructor function on a new object. This is
  246. * especially useful when an object is instantiated at runtime (with the
  247. * AddNodes service) and the integration with an underlying process canot be
  248. * manually defined. In the following constructor example, we simply set the
  249. * pump status to on.
  250. */
  251. UA_Server *s = NULL; /* required to get the server pointer into the constructor
  252. function (will change for v0.3) */
  253. static void *
  254. pumpTypeConstructor(const UA_NodeId instance) {
  255. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "New pump created");
  256. /* Find the NodeId of the status child variable */
  257. UA_RelativePathElement rpe;
  258. UA_RelativePathElement_init(&rpe);
  259. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT);
  260. rpe.isInverse = false;
  261. rpe.includeSubtypes = false;
  262. rpe.targetName = UA_QUALIFIEDNAME(1, "Status");
  263. UA_BrowsePath bp;
  264. UA_BrowsePath_init(&bp);
  265. bp.startingNode = instance;
  266. bp.relativePath.elementsSize = 1;
  267. bp.relativePath.elements = &rpe;
  268. UA_BrowsePathResult bpr =
  269. UA_Server_translateBrowsePathToNodeIds(s, &bp);
  270. if(bpr.statusCode != UA_STATUSCODE_GOOD ||
  271. bpr.targetsSize < 1)
  272. return NULL;
  273. /* Set the status value */
  274. UA_Boolean status = true;
  275. UA_Variant value;
  276. UA_Variant_setScalar(&value, &status, &UA_TYPES[UA_TYPES_BOOLEAN]);
  277. UA_Server_writeValue(s, bpr.targets[0].targetId.nodeId, value);
  278. UA_BrowsePathResult_deleteMembers(&bpr);
  279. /* The return pointer of the constructor is attached to the ObjectNode */
  280. return NULL;
  281. }
  282. static void
  283. addPumpTypeConstructor(UA_Server *server) {
  284. UA_ObjectLifecycleManagement olm;
  285. olm.constructor = pumpTypeConstructor;
  286. olm.destructor = NULL;
  287. UA_Server_setObjectTypeNode_lifecycleManagement(server, pumpTypeId, olm);
  288. }
  289. /** It follows the main server code, making use of the above definitions. */
  290. UA_Boolean running = true;
  291. static void stopHandler(int sign) {
  292. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  293. running = false;
  294. }
  295. int main(void) {
  296. signal(SIGINT, stopHandler);
  297. signal(SIGTERM, stopHandler);
  298. UA_ServerConfig config = UA_ServerConfig_standard;
  299. UA_ServerNetworkLayer nl =
  300. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  301. config.networkLayers = &nl;
  302. config.networkLayersSize = 1;
  303. UA_Server *server = UA_Server_new(config);
  304. s = server; /* required for the constructor */
  305. manuallyDefinePump(server);
  306. defineObjectTypes(server);
  307. addPumpObjectInstance(server, "pump2");
  308. addPumpObjectInstance(server, "pump3");
  309. addPumpTypeConstructor(server);
  310. addPumpObjectInstance(server, "pump4");
  311. addPumpObjectInstance(server, "pump5");
  312. UA_Server_run(server, &running);
  313. UA_Server_delete(server);
  314. nl.deleteMembers(&nl);
  315. return 0;
  316. }