tutorial_client_events.c 5.1 KB

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