tutorial_client_events.c 7.0 KB

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