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