tutorial_client_events.c 5.1 KB

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