tutorial_server_events.c 7.3 KB

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