tutorial_client_events.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. static UA_Boolean running = true;
  7. static void stopHandler(int sig) {
  8. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
  9. running = false;
  10. }
  11. #ifdef UA_ENABLE_SUBSCRIPTIONS
  12. static void
  13. handler_events(const UA_UInt32 monId, const size_t nEventFields,
  14. const UA_Variant *eventFields, void *context) {
  15. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Notification");
  16. /* The context should point to the monId on the stack */
  17. UA_assert(*(UA_UInt32*)context == monId);
  18. for(size_t i = 0; i < nEventFields; ++i) {
  19. if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  20. UA_UInt16 severity = *(UA_UInt16 *)eventFields[i].data;
  21. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Severity: %u", severity);
  22. } else if (UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  23. UA_LocalizedText *lt = (UA_LocalizedText *)eventFields[i].data;
  24. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  25. "Message: '%.*s'", (int)lt->text.length, lt->text.data);
  26. }
  27. else {
  28. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  29. "Don't know how to handle type: '%s'", eventFields[i].type->typeName);
  30. }
  31. }
  32. }
  33. const size_t nSelectClauses = 2;
  34. static UA_SimpleAttributeOperand *
  35. setupSelectClauses(void) {
  36. UA_SimpleAttributeOperand *selectClauses = (UA_SimpleAttributeOperand*)
  37. UA_Array_new(nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  38. if(!selectClauses)
  39. return NULL;
  40. for(size_t i =0; i<nSelectClauses; ++i) {
  41. UA_SimpleAttributeOperand_init(&selectClauses[i]);
  42. }
  43. selectClauses[0].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
  44. selectClauses[0].browsePathSize = 1;
  45. selectClauses[0].browsePath = (UA_QualifiedName*)
  46. UA_Array_new(selectClauses[0].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  47. if(!selectClauses[0].browsePath) {
  48. UA_SimpleAttributeOperand_delete(selectClauses);
  49. return NULL;
  50. }
  51. selectClauses[0].attributeId = UA_ATTRIBUTEID_VALUE;
  52. selectClauses[0].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Message");
  53. selectClauses[1].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
  54. selectClauses[1].browsePathSize = 1;
  55. selectClauses[1].browsePath = (UA_QualifiedName*)
  56. UA_Array_new(selectClauses[1].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  57. if(!selectClauses[1].browsePath) {
  58. UA_SimpleAttributeOperand_deleteMembers(selectClauses);
  59. UA_SimpleAttributeOperand_delete(selectClauses);
  60. return NULL;
  61. }
  62. selectClauses[1].attributeId = UA_ATTRIBUTEID_VALUE;
  63. selectClauses[1].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Severity");
  64. return selectClauses;
  65. }
  66. #endif
  67. int main(int argc, char *argv[]) {
  68. signal(SIGINT, stopHandler);
  69. signal(SIGTERM, stopHandler);
  70. if(argc < 2) {
  71. printf("Usage: tutorial_client_events <opc.tcp://server-url>\n");
  72. return 1;
  73. }
  74. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  75. /* opc.tcp://uademo.prosysopc.com:53530/OPCUA/SimulationServer */
  76. UA_StatusCode retval = UA_Client_connect(client, argv[1]);
  77. if(retval != UA_STATUSCODE_GOOD) {
  78. UA_Client_delete(client);
  79. return (int)retval;
  80. }
  81. #ifdef UA_ENABLE_SUBSCRIPTIONS
  82. /* Create a subscription */
  83. UA_UInt32 subId = 0;
  84. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  85. if(!subId) {
  86. UA_Client_disconnect(client);
  87. UA_Client_delete(client);
  88. return (int)retval;
  89. }
  90. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Create subscription succeeded, id %u", subId);
  91. /* Add a MonitoredItem */
  92. UA_NodeId monitorThis = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  93. UA_UInt32 monId = 0;
  94. UA_SimpleAttributeOperand *selectClauses = setupSelectClauses();
  95. retval = UA_Client_Subscriptions_addMonitoredEvent(client, subId, monitorThis,
  96. UA_ATTRIBUTEID_EVENTNOTIFIER,
  97. selectClauses, nSelectClauses,
  98. NULL, 0, &handler_events, &monId, &monId);
  99. if(retval != UA_STATUSCODE_GOOD) {
  100. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  101. "Could not add the MonitoredItem with %s", UA_StatusCode_name(retval));
  102. goto cleanup;
  103. } else {
  104. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  105. "Monitoring 'Root->Objects->Server', id %u", subId);
  106. }
  107. while (running)
  108. UA_Client_Subscriptions_manuallySendPublishRequest(client);
  109. /* Delete the subscription */
  110. cleanup:
  111. UA_Client_Subscriptions_remove(client, subId);
  112. UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  113. #endif
  114. UA_Client_disconnect(client);
  115. UA_Client_delete(client);
  116. return (int) retval;
  117. }