tutorial_server_historicaldata.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Copyright 2019 (c) basysKom GmbH <opensource@basyskom.com> (Author: Peter Rustler)
  5. */
  6. #include <ua_server.h>
  7. #include <ua_config_default.h>
  8. #include <ua_log_stdout.h>
  9. #include <ua_plugin_historydatabase.h>
  10. #include <ua_plugin_history_data_gathering.h>
  11. #include <ua_historydatagathering_default.h>
  12. #include <ua_historydatabase_default.h>
  13. #include <ua_historydatabackend_memory.h>
  14. #include <signal.h>
  15. #include <stdlib.h>
  16. static UA_Boolean running = true;
  17. static void stopHandler(int sign) {
  18. (void)sign;
  19. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  20. running = false;
  21. }
  22. int main(void) {
  23. signal(SIGINT, stopHandler);
  24. signal(SIGTERM, stopHandler);
  25. UA_ServerConfig *config = UA_ServerConfig_new_default();
  26. /*
  27. * We need a gathering for the plugin to constuct.
  28. * The UA_HistoryDataGathering is responsible to collect data and store it to the database.
  29. * We will use this gathering for one node, only. initialNodeIdStoreSize = 1
  30. * The store will grow if you register more than one node, but this is expensive.
  31. */
  32. UA_HistoryDataGathering gathering = UA_HistoryDataGathering_Default(1);
  33. /*
  34. * We set the responsible plugin in the configuration.
  35. * UA_HistoryDatabase is the main plugin which handles the historical data service.
  36. */
  37. config->historyDatabase = UA_HistoryDatabase_default(gathering);
  38. UA_Server *server = UA_Server_new(config);
  39. /* Define the attribute of the uint32 variable node */
  40. UA_VariableAttributes attr = UA_VariableAttributes_default;
  41. UA_UInt32 myUint32 = 40;
  42. UA_Variant_setScalar(&attr.value, &myUint32, &UA_TYPES[UA_TYPES_UINT32]);
  43. attr.description = UA_LOCALIZEDTEXT("en-US","myUintValue");
  44. attr.displayName = UA_LOCALIZEDTEXT("en-US","myUintValue");
  45. attr.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  46. /*
  47. * We set the access level to also support history read
  48. * This is what will be reported to clients
  49. */
  50. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE | UA_ACCESSLEVELMASK_HISTORYREAD;
  51. /*
  52. * We also set this node to historizing, so the server internals also know from it.
  53. */
  54. attr.historizing = true;
  55. /* Add the variable node to the information model */
  56. UA_NodeId uint32NodeId = UA_NODEID_STRING(1, "myUintValue");
  57. UA_QualifiedName uint32Name = UA_QUALIFIEDNAME(1, "myUintValue");
  58. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  59. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  60. UA_NodeId outNodeId;
  61. UA_NodeId_init(&outNodeId);
  62. UA_StatusCode retval = UA_Server_addVariableNode(server,
  63. uint32NodeId,
  64. parentNodeId,
  65. parentReferenceNodeId,
  66. uint32Name,
  67. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  68. attr,
  69. NULL,
  70. &outNodeId);
  71. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "UA_Server_addVariableNode %s", UA_StatusCode_name(retval));
  72. /*
  73. * Now we define the settings for our node
  74. */
  75. UA_HistorizingNodeIdSettings setting;
  76. /*
  77. * There is a memory based database plugin. We will use that.
  78. * We just reserve space for 3 nodes with 100 values each.
  79. * This will also automaticaly grow if needed, but that is expensive,
  80. * because all data must be copied.
  81. */
  82. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  83. /*
  84. * We want the server to serve a maximum of 100 values per request.
  85. * This value depend on the plattform you are running the server.
  86. * A big server can serve more values, smaller ones less.
  87. */
  88. setting.maxHistoryDataResponseSize = 100;
  89. /*
  90. * If we have a sensor which do not report updates
  91. * and need to be polled we change the setting like that.
  92. * The polling interval in ms.
  93. *
  94. setting.pollingInterval = 100;
  95. *
  96. * Set the update strategie to polling.
  97. *
  98. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_POLL;
  99. */
  100. /*
  101. * If you want to insert the values to the database yourself, we can set the user strategy here.
  102. * This is useful if you for example want a value stored, if a defined delta is reached.
  103. * Then you should use a local monitored item with a fuzziness and store the value in the callback.
  104. *
  105. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  106. */
  107. /*
  108. * We want the values stored in the database, when the nodes value is set.
  109. */
  110. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_VALUESET;
  111. /*
  112. * At the end we register the node for gathering data in the database.
  113. */
  114. retval = gathering.registerNodeId(server, gathering.context, &outNodeId, setting);
  115. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "registerNodeId %s", UA_StatusCode_name(retval));
  116. /*
  117. * If you use UA_HISTORIZINGUPDATESTRATEGY_POLL, then start the polling.
  118. *
  119. retval = gathering.startPoll(server, gathering.context, &outNodeId);
  120. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "startPoll %s", UA_StatusCode_name(retval));
  121. */
  122. retval = UA_Server_run(server, &running);
  123. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "UA_Server_run %s", UA_StatusCode_name(retval));
  124. /*
  125. * If you use UA_HISTORIZINGUPDATESTRATEGY_POLL, then stop the polling.
  126. *
  127. retval = gathering.stopPoll(server, gathering.context, &outNodeId);
  128. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "stopPoll %s", UA_StatusCode_name(retval));
  129. */
  130. UA_Server_run_shutdown(server);
  131. UA_Server_delete(server);
  132. UA_ServerConfig_delete(config);
  133. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  134. }