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