tutorial_server_historicaldata.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 volatile 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_Server *server = UA_Server_new();
  25. UA_ServerConfig *config = UA_Server_getConfig(server);
  26. UA_ServerConfig_setDefault(config);
  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. UA_HistoryDataGathering gathering = UA_HistoryDataGathering_Default(1);
  32. /* We set the responsible plugin in the configuration. UA_HistoryDatabase is
  33. * the main plugin which handles the historical data service. */
  34. config->historyDatabase = UA_HistoryDatabase_default(gathering);
  35. /* Define the attribute of the uint32 variable node */
  36. UA_VariableAttributes attr = UA_VariableAttributes_default;
  37. UA_UInt32 myUint32 = 40;
  38. UA_Variant_setScalar(&attr.value, &myUint32, &UA_TYPES[UA_TYPES_UINT32]);
  39. attr.description = UA_LOCALIZEDTEXT("en-US","myUintValue");
  40. attr.displayName = UA_LOCALIZEDTEXT("en-US","myUintValue");
  41. attr.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  42. /* We set the access level to also support history read
  43. * This is what will be reported to clients */
  44. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE | UA_ACCESSLEVELMASK_HISTORYREAD;
  45. /* We also set this node to historizing, so the server internals also know from it. */
  46. attr.historizing = true;
  47. /* Add the variable node to the information model */
  48. UA_NodeId uint32NodeId = UA_NODEID_STRING(1, "myUintValue");
  49. UA_QualifiedName uint32Name = UA_QUALIFIEDNAME(1, "myUintValue");
  50. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  51. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  52. UA_NodeId outNodeId;
  53. UA_NodeId_init(&outNodeId);
  54. UA_StatusCode retval = UA_Server_addVariableNode(server,
  55. uint32NodeId,
  56. parentNodeId,
  57. parentReferenceNodeId,
  58. uint32Name,
  59. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  60. attr,
  61. NULL,
  62. &outNodeId);
  63. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  64. "UA_Server_addVariableNode %s", UA_StatusCode_name(retval));
  65. /* Now we define the settings for our node */
  66. UA_HistorizingNodeIdSettings setting;
  67. /* There is a memory based database plugin. We will use that. We just
  68. * reserve space for 3 nodes with 100 values each. This will also
  69. * automaticaly grow if needed, but that is expensive, because all data must
  70. * be copied. */
  71. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  72. /* We want the server to serve a maximum of 100 values per request. This
  73. * value depend on the plattform you are running the server. A big server
  74. * can serve more values, smaller ones less. */
  75. setting.maxHistoryDataResponseSize = 100;
  76. /* If we have a sensor which do not report updates
  77. * and need to be polled we change the setting like that.
  78. * The polling interval in ms.
  79. *
  80. setting.pollingInterval = 100;
  81. *
  82. * Set the update strategie to polling.
  83. *
  84. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_POLL;
  85. */
  86. /* If you want to insert the values to the database yourself, we can set the user strategy here.
  87. * This is useful if you for example want a value stored, if a defined delta is reached.
  88. * Then you should use a local monitored item with a fuzziness and store the value in the callback.
  89. *
  90. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  91. */
  92. /* We want the values stored in the database, when the nodes value is
  93. * set. */
  94. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_VALUESET;
  95. /* At the end we register the node for gathering data in the database. */
  96. retval = gathering.registerNodeId(server, gathering.context, &outNodeId, setting);
  97. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "registerNodeId %s", UA_StatusCode_name(retval));
  98. /* If you use UA_HISTORIZINGUPDATESTRATEGY_POLL, then start the polling.
  99. *
  100. retval = gathering.startPoll(server, gathering.context, &outNodeId);
  101. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "startPoll %s", UA_StatusCode_name(retval));
  102. */
  103. retval = UA_Server_run(server, &running);
  104. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "UA_Server_run %s", UA_StatusCode_name(retval));
  105. /*
  106. * If you use UA_HISTORIZINGUPDATESTRATEGY_POLL, then stop the polling.
  107. *
  108. retval = gathering.stopPoll(server, gathering.context, &outNodeId);
  109. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "stopPoll %s", UA_StatusCode_name(retval));
  110. */
  111. UA_Server_delete(server);
  112. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  113. }