client_subscription_loop.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /**
  4. * Client disconnect handling
  5. * --------------------------
  6. * This example shows you how to handle a client disconnect, e.g., if the server
  7. * is shut down while the client is connected. You just need to call connect
  8. * again and the client will automatically reconnect.
  9. *
  10. * This example is very similar to the tutorial_client_firststeps.c. */
  11. #include <ua_config_default.h>
  12. #include <ua_client_subscriptions.h>
  13. #include <ua_log_stdout.h>
  14. #include <signal.h>
  15. UA_Boolean running = true;
  16. static void stopHandler(int sign) {
  17. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Received Ctrl-C");
  18. running = 0;
  19. }
  20. static void
  21. handler_currentTimeChanged(UA_Client *client, UA_UInt32 subId, void *subContext,
  22. UA_UInt32 monId, void *monContext, UA_DataValue *value) {
  23. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "currentTime has changed!");
  24. if(UA_Variant_hasScalarType(&value->value, &UA_TYPES[UA_TYPES_DATETIME])) {
  25. UA_DateTime raw_date = *(UA_DateTime *) value->value.data;
  26. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  27. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  28. "date is: %02u-%02u-%04u %02u:%02u:%02u.%03u",
  29. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  30. }
  31. }
  32. static void
  33. deleteSubscriptionCallback(UA_Client *client, UA_UInt32 subscriptionId, void *subscriptionContext) {
  34. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  35. "Subscription Id %u was deleted", subscriptionId);
  36. }
  37. static void
  38. subscriptionInactivityCallback (UA_Client *client, UA_UInt32 subId, void *subContext) {
  39. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Inactivity for subscription %u", subId);
  40. }
  41. static void
  42. stateCallback (UA_Client *client, UA_ClientState clientState) {
  43. switch(clientState) {
  44. case UA_CLIENTSTATE_DISCONNECTED:
  45. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "The client is disconnected");
  46. break;
  47. case UA_CLIENTSTATE_WAITING_FOR_ACK:
  48. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Waiting for ack");
  49. break;
  50. case UA_CLIENTSTATE_CONNECTED:
  51. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  52. "A TCP connection to the server is open");
  53. break;
  54. case UA_CLIENTSTATE_SECURECHANNEL:
  55. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  56. "A SecureChannel to the server is open");
  57. break;
  58. case UA_CLIENTSTATE_SESSION:{
  59. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "A session with the server is open");
  60. /* A new session was created. We need to create the subscription. */
  61. /* Create a subscription */
  62. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  63. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  64. NULL, NULL, deleteSubscriptionCallback);
  65. if(response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  66. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  67. "Create subscription succeeded, id %u", response.subscriptionId);
  68. else
  69. return;
  70. /* Add a MonitoredItem */
  71. UA_MonitoredItemCreateRequest monRequest =
  72. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
  73. UA_MonitoredItemCreateResult monResponse =
  74. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  75. UA_TIMESTAMPSTORETURN_BOTH,
  76. monRequest, NULL, handler_currentTimeChanged, NULL);
  77. if(monResponse.statusCode == UA_STATUSCODE_GOOD)
  78. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  79. "Monitoring UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME', id %u",
  80. monResponse.monitoredItemId);
  81. }
  82. break;
  83. case UA_CLIENTSTATE_SESSION_RENEWED:
  84. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  85. "A session with the server is open (renewed)");
  86. /* The session was renewed. We don't need to recreate the subscription. */
  87. break;
  88. case UA_CLIENTSTATE_SESSION_DISCONNECTED:
  89. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Session disconnected");
  90. break;
  91. }
  92. return;
  93. }
  94. int
  95. main(void) {
  96. signal(SIGINT, stopHandler); /* catches ctrl-c */
  97. UA_ClientConfig config = UA_ClientConfig_default;
  98. /* Set stateCallback */
  99. config.stateCallback = stateCallback;
  100. config.subscriptionInactivityCallback = subscriptionInactivityCallback;
  101. UA_Client *client = UA_Client_new(config);
  102. /* Endless loop runAsync */
  103. while(running) {
  104. /* if already connected, this will return GOOD and do nothing */
  105. /* if the connection is closed/errored, the connection will be reset and then reconnected */
  106. /* Alternatively you can also use UA_Client_getState to get the current state */
  107. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  108. if(retval != UA_STATUSCODE_GOOD) {
  109. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  110. "Not connected. Retrying to connect in 1 second");
  111. /* The connect may timeout after 1 second (see above) or it may fail immediately on network errors */
  112. /* E.g. name resolution errors or unreachable network. Thus there should be a small sleep here */
  113. UA_sleep_ms(1000);
  114. continue;
  115. }
  116. UA_Client_run_iterate(client, 1000);
  117. };
  118. /* Clean up */
  119. UA_Client_delete(client); /* Disconnects the client internally */
  120. return UA_STATUSCODE_GOOD;
  121. }