client_historical.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <stdio.h>
  4. #include <ua_client_highlevel.h>
  5. #include <ua_client.h>
  6. #include <ua_config_default.h>
  7. #ifdef UA_ENABLE_EXPERIMENTAL_HISTORIZING
  8. static void
  9. printUpdateType(UA_HistoryUpdateType type) {
  10. switch (type) {
  11. case UA_HISTORYUPDATETYPE_INSERT:
  12. printf("Insert\n");
  13. return;
  14. case UA_HISTORYUPDATETYPE_REPLACE:
  15. printf("Replace\n");
  16. return;
  17. case UA_HISTORYUPDATETYPE_UPDATE:
  18. printf("Update\n");
  19. return;
  20. case UA_HISTORYUPDATETYPE_DELETE:
  21. printf("Delete\n");
  22. return;
  23. default:
  24. printf("Unknown\n");
  25. return;
  26. }
  27. }
  28. #endif
  29. static void
  30. printTimestamp(char *name, UA_DateTime date) {
  31. UA_DateTimeStruct dts = UA_DateTime_toStruct(date);
  32. if (name)
  33. printf("%s: %02u-%02u-%04u %02u:%02u:%02u.%03u, ", name,
  34. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  35. else
  36. printf("%02u-%02u-%04u %02u:%02u:%02u.%03u, ",
  37. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  38. }
  39. static void
  40. printDataValue(UA_DataValue *value) {
  41. /* Print status and timestamps */
  42. if (value->hasServerTimestamp)
  43. printTimestamp("ServerTime", value->serverTimestamp);
  44. if (value->hasSourceTimestamp)
  45. printTimestamp("SourceTime", value->sourceTimestamp);
  46. if (value->hasStatus)
  47. printf("Status 0x%08x, ", value->status);
  48. if (value->value.type == &UA_TYPES[UA_TYPES_UINT32]) {
  49. UA_UInt32 hrValue = *(UA_UInt32 *)value->value.data;
  50. printf("Uint32Value %u\n", hrValue);
  51. }
  52. if (value->value.type == &UA_TYPES[UA_TYPES_DOUBLE]) {
  53. UA_Double hrValue = *(UA_Double *)value->value.data;
  54. printf("DoubleValue %f\n", hrValue);
  55. }
  56. }
  57. static UA_Boolean
  58. readRaw(const UA_HistoryData *data) {
  59. printf("readRaw Value count: %lu\n", (long unsigned)data->dataValuesSize);
  60. /* Iterate over all values */
  61. for (UA_UInt32 i = 0; i < data->dataValuesSize; ++i)
  62. {
  63. printDataValue(&data->dataValues[i]);
  64. }
  65. /* We want more data! */
  66. return true;
  67. }
  68. #ifdef UA_ENABLE_EXPERIMENTAL_HISTORIZING
  69. static UA_Boolean
  70. readRawModified(const UA_HistoryModifiedData *data) {
  71. printf("readRawModified Value count: %lu\n", (long unsigned)data->dataValuesSize);
  72. /* Iterate over all values */
  73. for (size_t i = 0; i < data->dataValuesSize; ++i) {
  74. printDataValue(&data->dataValues[i]);
  75. }
  76. printf("Modificaton Value count: %lu\n", data->modificationInfosSize);
  77. for (size_t j = 0; j < data->modificationInfosSize; ++j) {
  78. if (data->modificationInfos[j].userName.data)
  79. printf("Username: %s, ", data->modificationInfos[j].userName.data);
  80. printTimestamp("Modtime", data->modificationInfos[j].modificationTime);
  81. printUpdateType(data->modificationInfos[j].updateType);
  82. }
  83. /* We want more data! */
  84. return true;
  85. }
  86. static UA_Boolean
  87. readEvents(const UA_HistoryEvent *data) {
  88. printf("readEvent Value count: %lu\n", (long unsigned)data->eventsSize);
  89. for (size_t i = 0; i < data->eventsSize; ++i) {
  90. printf("Processing event: %lu\n", (long unsigned)i);
  91. for (size_t j = 0; j < data->events[i].eventFieldsSize; ++j) {
  92. printf("Processing %lu: %s\n", (long unsigned)j, data->events[i].eventFields[j].type->typeName);
  93. }
  94. }
  95. return true;
  96. }
  97. #endif
  98. static UA_Boolean
  99. readHist(UA_Client *client, const UA_NodeId *nodeId,
  100. UA_Boolean moreDataAvailable,
  101. const UA_ExtensionObject *data, void *unused) {
  102. printf("\nRead historical callback:\n");
  103. printf("\tHas more data:\t%d\n\n", moreDataAvailable);
  104. if (data->content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]) {
  105. return readRaw((UA_HistoryData*)data->content.decoded.data);
  106. }
  107. #ifdef UA_ENABLE_EXPERIMENTAL_HISTORIZING
  108. if (data->content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYMODIFIEDDATA]) {
  109. return readRawModified((UA_HistoryModifiedData*)data->content.decoded.data);
  110. }
  111. if (data->content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYEVENT]) {
  112. return readEvents((UA_HistoryEvent*)data->content.decoded.data);
  113. }
  114. #endif
  115. return true;
  116. }
  117. int main(int argc, char *argv[]) {
  118. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  119. /* Connect to the Unified Automation demo server */
  120. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:53530/OPCUA/SimulationServer");
  121. if(retval != UA_STATUSCODE_GOOD) {
  122. UA_Client_delete(client);
  123. return (int)retval;
  124. }
  125. /* Read historical values (uint32) */
  126. printf("\nStart historical read (1, \"myUintValue\"):\n");
  127. UA_NodeId node = UA_NODEID_STRING(2, "MyLevel");
  128. retval = UA_Client_HistoryRead_raw(client, &node, readHist,
  129. UA_DateTime_fromUnixTime(0), UA_DateTime_now(), UA_STRING_NULL, false, 10, UA_TIMESTAMPSTORETURN_BOTH, (void *)UA_FALSE);
  130. if (retval != UA_STATUSCODE_GOOD) {
  131. printf("Failed. %s\n", UA_StatusCode_name(retval));
  132. }
  133. #ifdef UA_ENABLE_EXPERIMENTAL_HISTORIZING
  134. printf("\nStart historical modified read (1, \"myUintValue\"):\n");
  135. retval = UA_Client_HistoryRead_modified(client, &node, readHist,
  136. UA_DateTime_fromUnixTime(0), UA_DateTime_now(), UA_STRING_NULL, false, 10, UA_TIMESTAMPSTORETURN_BOTH, (void *)UA_FALSE);
  137. if (retval != UA_STATUSCODE_GOOD) {
  138. printf("Failed. %s\n", UA_StatusCode_name(retval));
  139. }
  140. printf("\nStart historical event read (1, \"myUintValue\"):\n");
  141. UA_EventFilter filter;
  142. UA_EventFilter_init(&filter);
  143. UA_NodeId eventNode = UA_NODEID_NUMERIC(0, 2253);
  144. retval = UA_Client_HistoryRead_events(client, &eventNode, readHist,
  145. UA_DateTime_fromUnixTime(0), UA_DateTime_now(), UA_STRING_NULL, filter, 10, UA_TIMESTAMPSTORETURN_BOTH, (void *)UA_FALSE);
  146. if (retval != UA_STATUSCODE_GOOD) {
  147. printf("Failed. %s\n", UA_StatusCode_name(retval));
  148. }
  149. #endif
  150. UA_Client_disconnect(client);
  151. UA_Client_delete(client);
  152. return (int) retval;
  153. }