tutorial_server_events.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 all the rest mandatory EventFields of the BaseEventType:
  40. * `Time` (to make the example UaExpert compliant), `Severity`, `Message` and `SourceName`.
  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. UA_DateTime eventTime = UA_DateTime_now();
  52. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Time"),
  53. &eventTime, &UA_TYPES[UA_TYPES_DATETIME]);
  54. UA_UInt16 eventSeverity = 100;
  55. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Severity"),
  56. &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
  57. UA_LocalizedText eventMessage = UA_LOCALIZEDTEXT("en-US", "An event has been generated.");
  58. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "Message"),
  59. &eventMessage, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  60. UA_String eventSourceName = UA_STRING("Server");
  61. UA_Server_writeObjectProperty_scalar(server, *outId, UA_QUALIFIEDNAME(0, "SourceName"),
  62. &eventSourceName, &UA_TYPES[UA_TYPES_STRING]);
  63. return UA_STATUSCODE_GOOD;
  64. }
  65. /**
  66. * Triggering an event
  67. * ^^^^^^^^^^^^^^^^^^^
  68. * First a node representing an event is generated using ``setUpEvent``. Once our event is good to go, we specify
  69. * a node which emits the event - in this case the server node. We can use ``UA_Server_triggerEvent`` to trigger our
  70. * event onto said node. Passing ``NULL`` as the second-last argument means we will not receive the `EventId`.
  71. * The last boolean argument states whether the node should be deleted. */
  72. static UA_StatusCode
  73. generateEventMethodCallback(UA_Server *server,
  74. const UA_NodeId *sessionId, void *sessionHandle,
  75. const UA_NodeId *methodId, void *methodContext,
  76. const UA_NodeId *objectId, void *objectContext,
  77. size_t inputSize, const UA_Variant *input,
  78. size_t outputSize, UA_Variant *output) {
  79. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Creating event");
  80. /* set up event */
  81. UA_NodeId eventNodeId;
  82. UA_StatusCode retval = setUpEvent(server, &eventNodeId);
  83. if(retval != UA_STATUSCODE_GOOD) {
  84. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  85. "Creating event failed. StatusCode %s", UA_StatusCode_name(retval));
  86. return retval;
  87. }
  88. retval = UA_Server_triggerEvent(server, eventNodeId,
  89. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  90. NULL, UA_TRUE);
  91. if(retval != UA_STATUSCODE_GOOD)
  92. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  93. "Triggering event failed. StatusCode %s", UA_StatusCode_name(retval));
  94. return retval;
  95. }
  96. /**
  97. * Now, all that is left to do is to create a method node which uses our callback. We do not
  98. * require any input and as output we will be using the `EventId` we receive from ``triggerEvent``. The `EventId` is
  99. * generated by the server internally and is a random unique ID which identifies that specific event.
  100. *
  101. * This method node will be added to a basic server setup.
  102. */
  103. static void
  104. addGenerateEventMethod(UA_Server *server) {
  105. UA_MethodAttributes generateAttr = UA_MethodAttributes_default;
  106. generateAttr.description = UA_LOCALIZEDTEXT("en-US","Generate an event.");
  107. generateAttr.displayName = UA_LOCALIZEDTEXT("en-US","Generate Event");
  108. generateAttr.executable = true;
  109. generateAttr.userExecutable = true;
  110. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  111. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  112. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  113. UA_QUALIFIEDNAME(1, "Generate Event"),
  114. generateAttr, &generateEventMethodCallback,
  115. 0, NULL, 0, NULL, NULL, NULL);
  116. }
  117. /** It follows the main server code, making use of the above definitions. */
  118. UA_Boolean running = true;
  119. static void stopHandler(int sig) {
  120. running = false;
  121. }
  122. int main (void) {
  123. /* default server values */
  124. signal(SIGINT, stopHandler);
  125. signal(SIGTERM, stopHandler);
  126. UA_ServerConfig *config = UA_ServerConfig_new_default();
  127. UA_Server *server = UA_Server_new(config);
  128. addNewEventType(server);
  129. addGenerateEventMethod(server);
  130. /* return value */
  131. UA_StatusCode retval = UA_Server_run(server, &running);
  132. UA_Server_delete(server);
  133. UA_ServerConfig_delete(config);
  134. return (int) retval;
  135. }