tutorial_client_events.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/client.h>
  4. #include <open62541/client_config_default.h>
  5. #include <open62541/client_highlevel.h>
  6. #include <open62541/client_subscriptions.h>
  7. #include <open62541/plugin/log_stdout.h>
  8. #include <open62541/server.h>
  9. #include <open62541/server_config_default.h>
  10. #include <signal.h>
  11. #ifdef _MSC_VER
  12. #pragma warning(disable:4996) // warning C4996: 'UA_Client_Subscriptions_addMonitoredEvent': was declared deprecated
  13. #endif
  14. #ifdef __clang__
  15. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  16. #endif
  17. #ifdef __GNUC__
  18. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  19. #endif
  20. static UA_Boolean running = true;
  21. static void stopHandler(int sig) {
  22. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
  23. running = false;
  24. }
  25. #ifdef UA_ENABLE_SUBSCRIPTIONS
  26. static void
  27. handler_events(UA_Client *client, UA_UInt32 subId, void *subContext,
  28. UA_UInt32 monId, void *monContext,
  29. size_t nEventFields, UA_Variant *eventFields) {
  30. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Notification");
  31. /* The context should point to the monId on the stack */
  32. UA_assert(*(UA_UInt32*)monContext == monId);
  33. for(size_t i = 0; i < nEventFields; ++i) {
  34. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  35. UA_UInt16 severity = *(UA_UInt16 *)eventFields[i].data;
  36. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Severity: %u", severity);
  37. } else if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  38. UA_LocalizedText *lt = (UA_LocalizedText *)eventFields[i].data;
  39. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  40. "Message: '%.*s'", (int)lt->text.length, lt->text.data);
  41. }
  42. else {
  43. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  44. "Don't know how to handle type: '%s'", eventFields[i].type->typeName);
  45. }
  46. }
  47. }
  48. const size_t nSelectClauses = 2;
  49. static UA_SimpleAttributeOperand *
  50. setupSelectClauses(void) {
  51. UA_SimpleAttributeOperand *selectClauses = (UA_SimpleAttributeOperand*)
  52. UA_Array_new(nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  53. if(!selectClauses)
  54. return NULL;
  55. for(size_t i =0; i<nSelectClauses; ++i) {
  56. UA_SimpleAttributeOperand_init(&selectClauses[i]);
  57. }
  58. selectClauses[0].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
  59. selectClauses[0].browsePathSize = 1;
  60. selectClauses[0].browsePath = (UA_QualifiedName*)
  61. UA_Array_new(selectClauses[0].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  62. if(!selectClauses[0].browsePath) {
  63. UA_SimpleAttributeOperand_delete(selectClauses);
  64. return NULL;
  65. }
  66. selectClauses[0].attributeId = UA_ATTRIBUTEID_VALUE;
  67. selectClauses[0].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Message");
  68. selectClauses[1].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
  69. selectClauses[1].browsePathSize = 1;
  70. selectClauses[1].browsePath = (UA_QualifiedName*)
  71. UA_Array_new(selectClauses[1].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  72. if(!selectClauses[1].browsePath) {
  73. UA_SimpleAttributeOperand_delete(selectClauses);
  74. return NULL;
  75. }
  76. selectClauses[1].attributeId = UA_ATTRIBUTEID_VALUE;
  77. selectClauses[1].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Severity");
  78. return selectClauses;
  79. }
  80. #endif
  81. int main(int argc, char *argv[]) {
  82. signal(SIGINT, stopHandler);
  83. signal(SIGTERM, stopHandler);
  84. if(argc < 2) {
  85. printf("Usage: tutorial_client_events <opc.tcp://server-url>\n");
  86. return EXIT_FAILURE;
  87. }
  88. UA_Client *client = UA_Client_new();
  89. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  90. /* opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer */
  91. /* opc.tcp://opcua.demo-this.com:51210/UA/SampleServer */
  92. UA_StatusCode retval = UA_Client_connect(client, argv[1]);
  93. if(retval != UA_STATUSCODE_GOOD) {
  94. UA_Client_delete(client);
  95. return EXIT_FAILURE;
  96. }
  97. #ifdef UA_ENABLE_SUBSCRIPTIONS
  98. /* Create a subscription */
  99. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  100. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  101. NULL, NULL, NULL);
  102. if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  103. UA_Client_disconnect(client);
  104. UA_Client_delete(client);
  105. return EXIT_FAILURE;
  106. }
  107. UA_UInt32 subId = response.subscriptionId;
  108. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", subId);
  109. /* Add a MonitoredItem */
  110. UA_MonitoredItemCreateRequest item;
  111. UA_MonitoredItemCreateRequest_init(&item);
  112. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  113. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  114. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  115. UA_EventFilter filter;
  116. UA_EventFilter_init(&filter);
  117. filter.selectClauses = setupSelectClauses();
  118. filter.selectClausesSize = nSelectClauses;
  119. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  120. item.requestedParameters.filter.content.decoded.data = &filter;
  121. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  122. UA_UInt32 monId = 0;
  123. UA_MonitoredItemCreateResult result =
  124. UA_Client_MonitoredItems_createEvent(client, subId,
  125. UA_TIMESTAMPSTORETURN_BOTH, item,
  126. &monId, handler_events, NULL);
  127. if(result.statusCode != UA_STATUSCODE_GOOD) {
  128. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  129. "Could not add the MonitoredItem with %s", UA_StatusCode_name(retval));
  130. goto cleanup;
  131. } else {
  132. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  133. "Monitoring 'Root->Objects->Server', id %u", response.subscriptionId);
  134. }
  135. monId = result.monitoredItemId;
  136. while(running)
  137. retval = UA_Client_run_iterate(client, 100);
  138. /* Delete the subscription */
  139. cleanup:
  140. UA_MonitoredItemCreateResult_clear(&result);
  141. UA_Client_Subscriptions_deleteSingle(client, response.subscriptionId);
  142. UA_Array_delete(filter.selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  143. #endif
  144. UA_Client_disconnect(client);
  145. UA_Client_delete(client);
  146. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  147. }