tutorial_server_events.c 6.7 KB

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