tutorial_client_events.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 1;
  84. }
  85. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  86. /* opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer */
  87. /* opc.tcp://opcua.demo-this.com:51210/UA/SampleServer */
  88. UA_StatusCode retval = UA_Client_connect(client, argv[1]);
  89. if(retval != UA_STATUSCODE_GOOD) {
  90. UA_Client_delete(client);
  91. return (int)retval;
  92. }
  93. #ifdef UA_ENABLE_SUBSCRIPTIONS
  94. /* Create a subscription */
  95. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  96. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  97. NULL, NULL, NULL);
  98. if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  99. UA_Client_disconnect(client);
  100. UA_Client_delete(client);
  101. return (int)retval;
  102. }
  103. UA_UInt32 subId = response.subscriptionId;
  104. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", subId);
  105. /* Add a MonitoredItem */
  106. UA_MonitoredItemCreateRequest item;
  107. UA_MonitoredItemCreateRequest_init(&item);
  108. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  109. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  110. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  111. UA_EventFilter filter;
  112. UA_EventFilter_init(&filter);
  113. filter.selectClauses = setupSelectClauses();
  114. filter.selectClausesSize = nSelectClauses;
  115. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  116. item.requestedParameters.filter.content.decoded.data = &filter;
  117. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  118. UA_UInt32 monId = 0;
  119. UA_MonitoredItemCreateResult result =
  120. UA_Client_MonitoredItems_createEvent(client, subId,
  121. UA_TIMESTAMPSTORETURN_BOTH, item,
  122. &monId, handler_events, NULL);
  123. if(result.statusCode != UA_STATUSCODE_GOOD) {
  124. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  125. "Could not add the MonitoredItem with %s", UA_StatusCode_name(retval));
  126. goto cleanup;
  127. } else {
  128. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  129. "Monitoring 'Root->Objects->Server', id %u", response.subscriptionId);
  130. }
  131. monId = result.monitoredItemId;
  132. while(running)
  133. UA_Client_run_iterate(client, 100);
  134. /* Delete the subscription */
  135. cleanup:
  136. UA_MonitoredItemCreateResult_clear(&result);
  137. UA_Client_Subscriptions_deleteSingle(client, response.subscriptionId);
  138. UA_Array_delete(filter.selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  139. #endif
  140. UA_Client_disconnect(client);
  141. UA_Client_delete(client);
  142. return (int) retval;
  143. }