tutorial_server_events.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include <ua_server.h>
  4. #include <ua_config_default.h>
  5. #include <ua_log_stdout.h>
  6. #include <signal.h>
  7. /**
  8. * Generating events
  9. * -----------------
  10. * To make sense of the many things going on in a server, monitoring items can be useful. Though in many cases, data
  11. * change does not convey enough information to be the optimal solution. Events can be generated at any time,
  12. * hold a lot of information and can be filtered so the client only receives the specific attributes he is interested in.
  13. *
  14. * Emitting events by calling methods
  15. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  16. * The following example will be based on the server method tutorial. We will be
  17. * creating a method node which generates an event from the server node.
  18. *
  19. * The event we want to generate should be very simple. Since the `BaseEventType` is abstract,
  20. * we will have to create our own event type. `EventTypes` are saved internally as `ObjectTypes`,
  21. * so add the type as you would a new `ObjectType`. */
  22. static UA_NodeId eventType;
  23. static UA_StatusCode
  24. addNewEventType(UA_Server *server) {
  25. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  26. attr.displayName = UA_LOCALIZEDTEXT("en-US", "SimpleEventType");
  27. attr.description = UA_LOCALIZEDTEXT("en-US", "The simple event type we created");
  28. return UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  29. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE),
  30. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  31. UA_QUALIFIEDNAME(0, "SimpleEventType"),
  32. attr, NULL, &eventType);
  33. }
  34. /**
  35. * Setting up an event
  36. * ^^^^^^^^^^^^^^^^^^^
  37. * In order to set up the event, we can first use ``UA_Server_createEvent`` to give us a node representation of the event.
  38. * All we need for this is our `EventType`. Once we have our event node, which is saved internally as an `ObjectNode`,
  39. * we can define the attributes the event has the same way we would define the attributes of an object node. It is not
  40. * necessary to define the attributes `EventId`, `ReceiveTime`, `SourceNode` or `EventType` since these are set
  41. * automatically by the server. In this example, we will be setting the fields 'Message' and 'Severity' in addition
  42. * to `Time` which is needed to make the example UaExpert compliant.
  43. */
  44. static UA_StatusCode
  45. setUpEvent(UA_Server *server, UA_NodeId *outId) {
  46. UA_StatusCode retval = UA_Server_createEvent(server, eventType, outId);
  47. if (retval != UA_STATUSCODE_GOOD) {
  48. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  49. "createEvent failed. StatusCode %s", UA_StatusCode_name(retval));
  50. return retval;
  51. }
  52. /* Set the Event Attributes */
  53. /* Setting the Time is required or else the event will not show up in UAExpert! */
  54. UA_DateTime eventTime = UA_DateTime_now();
  55. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Time"),
  56. &eventTime, &UA_TYPES[UA_TYPES_DATETIME]);
  57. UA_UInt16 eventSeverity = 100;
  58. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Severity"),
  59. &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
  60. UA_LocalizedText eventMessage = UA_LOCALIZEDTEXT("en-US", "An event has been generated.");
  61. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Message"),
  62. &eventMessage, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  63. UA_String eventSourceName = UA_STRING("Server");
  64. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "SourceName"),
  65. &eventSourceName, &UA_TYPES[UA_TYPES_STRING]);
  66. return UA_STATUSCODE_GOOD;
  67. }
  68. /**
  69. * Triggering an event
  70. * ^^^^^^^^^^^^^^^^^^^
  71. * First a node representing an event is generated using ``setUpEvent``. Once our event is good to go, we specify
  72. * a node which emits the event - in this case the server node. We can use ``UA_Server_triggerEvent`` to trigger our
  73. * event onto said node. Passing ``NULL`` as the second-last argument means we will not receive the `EventId`.
  74. * The last boolean argument states whether the node should be deleted. */
  75. static UA_StatusCode
  76. generateEventMethodCallback(UA_Server *server,
  77. const UA_NodeId *sessionId, void *sessionHandle,
  78. const UA_NodeId *methodId, void *methodContext,
  79. const UA_NodeId *objectId, void *objectContext,
  80. size_t inputSize, const UA_Variant *input,
  81. size_t outputSize, UA_Variant *output) {
  82. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Creating event");
  83. /* set up event */
  84. UA_NodeId eventNodeId;
  85. UA_StatusCode retval = setUpEvent(server, &eventNodeId);
  86. if(retval != UA_STATUSCODE_GOOD) {
  87. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  88. "Creating event failed. StatusCode %s", UA_StatusCode_name(retval));
  89. return retval;
  90. }
  91. retval = UA_Server_triggerEvent(server, eventNodeId,
  92. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  93. NULL, UA_TRUE);
  94. if(retval != UA_STATUSCODE_GOOD)
  95. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  96. "Triggering event failed. StatusCode %s", UA_StatusCode_name(retval));
  97. return retval;
  98. }
  99. /**
  100. * Now, all that is left to do is to create a method node which uses our callback. We do not
  101. * require any input and as output we will be using the `EventId` we receive from ``triggerEvent``. The `EventId` is
  102. * generated by the server internally and is a random unique ID which identifies that specific event.
  103. *
  104. * This method node will be added to a basic server setup.
  105. */
  106. static void
  107. addGenerateEventMethod(UA_Server *server) {
  108. UA_MethodAttributes generateAttr = UA_MethodAttributes_default;
  109. generateAttr.description = UA_LOCALIZEDTEXT("en-US","Generate an event.");
  110. generateAttr.displayName = UA_LOCALIZEDTEXT("en-US","Generate Event");
  111. generateAttr.executable = true;
  112. generateAttr.userExecutable = true;
  113. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  114. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  115. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  116. UA_QUALIFIEDNAME(1, "Generate Event"),
  117. generateAttr, &generateEventMethodCallback,
  118. 0, NULL, 0, NULL, NULL, NULL);
  119. }
  120. /** It follows the main server code, making use of the above definitions. */
  121. UA_Boolean running = true;
  122. static void stopHandler(int sig) {
  123. running = false;
  124. }
  125. int main (void) {
  126. /* default server values */
  127. signal(SIGINT, stopHandler);
  128. signal(SIGTERM, stopHandler);
  129. UA_ServerConfig *config = UA_ServerConfig_new_default();
  130. UA_Server *server = UA_Server_new(config);
  131. addNewEventType(server);
  132. addGenerateEventMethod(server);
  133. /* return value */
  134. UA_StatusCode retval = UA_Server_run(server, &running);
  135. UA_Server_delete(server);
  136. UA_ServerConfig_delete(config);
  137. return (int) retval;
  138. }