tutorial_client_events.c 6.6 KB

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