tutorial_client_events.c 6.4 KB

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