tutorial_server_events.c 7.4 KB

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