tutorial_client_events.c 6.5 KB

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