Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemConfig.cpp 4.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <sstream>
  2. #include <regex>
  3. #include "SystemConfig.h"
  4. #include "CustomStringUtilities.h"
  5. std::map<std::string, int> SystemConfig::intParameter;
  6. std::map<std::string, float> SystemConfig::floatParameter;
  7. std::map<std::string, std::string> SystemConfig::stringParameter;
  8. std::ostream& operator<<(std::ostream& os, SystemConfig& config) {
  9. os << "System Configuration: " << std::endl;
  10. std::for_each(config.intParameter.begin(), config.intParameter.end(),
  11. [&config, &os](auto& param) { os << "\t"<< param.first << ":\t" << param.second << std::endl; });
  12. return os;
  13. }
  14. //Read in the data from the config file
  15. void SystemConfig::readStandardConfig()
  16. {
  17. std::fstream fileStream(filePath, std::ios::in);
  18. if (fileStream.is_open()) {
  19. std::stringstream buf;
  20. while (std::getline(fileStream, fileContent))
  21. buf << fileContent << '\n';
  22. fileContent = buf.str();
  23. fileStream.close();
  24. parseConfFile();
  25. }
  26. else {
  27. LOG(ERROR) << "Couldn't open " << filePath << " for reading -> using standard configuration instead";
  28. }
  29. }
  30. //Place parameters to read in here
  31. SystemConfig::SystemConfig()
  32. {
  33. //define name and default value here
  34. intParameter.emplace("read_data_interval_s", 60);
  35. intParameter.emplace("tcp_server_port", 7777);
  36. stringParameter.emplace("modbus_poc_ip", "127.0.0.1");
  37. intParameter.emplace("modbus_poc_port", 502);
  38. intParameter.emplace("alert_read_interval", 5);
  39. stringParameter.emplace("modbus_rtu_device", "dev/tty0");
  40. intParameter.emplace("modbus_rtu_baud", 9800);
  41. intParameter.emplace("modbus_rtu_stop_bits", 1);
  42. stringParameter.emplace("modbus_rtu_pairity", "E");
  43. intParameter.emplace("modbus_rtu_slave_address", 1);
  44. intParameter.emplace("modbus_tcp_slave_address", 1);
  45. intParameter.emplace("permanent_param_history", 1000);
  46. floatParameter.emplace("crit_residual_current", 30.0);
  47. intParameter.emplace("crit_residual_timerange", 12);
  48. intParameter.emplace("update_model_rate", 100);
  49. intParameter.emplace("narrow_block", 100);
  50. }
  51. void SystemConfig::parseConfFile()
  52. {
  53. //remove comments
  54. std::regex patternComment("#(\\w|\\s)*\n+");
  55. fileContent = std::regex_replace(fileContent, patternComment, "\n");
  56. auto posHeader = fileContent.find("SystemConfig:");
  57. if (posHeader == std::string::npos){
  58. LOG(ERROR) << "Found no Header for the configuration file -> using standard configuration instead";
  59. return;
  60. }
  61. for (auto& param : intParameter) {
  62. std::regex pattern{ param.first + "\\s*=\\s*\\w+" };
  63. std::smatch match;
  64. if (std::regex_search(fileContent, match, pattern)) {
  65. std::string numberString = match.str();
  66. CustomStringUtilities::removeAllWhitespaces(numberString);
  67. param.second = std::stoi(numberString.substr(numberString.find('=') + 1));
  68. }
  69. else {
  70. LOG(INFO) << "No specified config paramter (int) for " << param.first << ", using the default value " << param.second;
  71. }
  72. }
  73. for (auto& param : floatParameter) {
  74. std::regex pattern{ param.first + "\\s*=\\s*\\d+\\.\\d*" };
  75. std::smatch match;
  76. if (std::regex_search(fileContent, match, pattern)) {
  77. std::string numberString = match.str();
  78. CustomStringUtilities::removeAllWhitespaces(numberString);
  79. param.second = std::stof(numberString.substr(numberString.find('=') + 1));
  80. }
  81. else {
  82. LOG(INFO) << "No specified config paramter (float) for " << param.first << ", using the default value " << param.second;
  83. }
  84. }
  85. for (auto& param : stringParameter) {
  86. std::regex pattern{ param.first + "\\s*=\\s*(\\w|.)*" };
  87. std::smatch match;
  88. if (std::regex_search(fileContent, match, pattern)) {
  89. std::string paramString = match.str();
  90. CustomStringUtilities::removeAllWhitespaces(paramString);
  91. param.second = paramString.substr(paramString.find('=') + 1);
  92. }
  93. else {
  94. LOG(INFO) << "No specified config paramter (float) for " << param.first << ", using the default value " << param.second;
  95. }
  96. }
  97. }
  98. int SystemConfig::getIntConfigParameter(std::string paramName){
  99. auto it = intParameter.find(paramName.data());
  100. if (it != intParameter.end()) {
  101. return it->second;
  102. }
  103. LOG(ERROR) << "Tried to read non existant int config parameter " << paramName;
  104. return 0;
  105. }
  106. float SystemConfig::getFloatConfigParameter(std::string paramName){
  107. auto it = floatParameter.find(paramName.data());
  108. if (it != floatParameter.end()) {
  109. return it->second;
  110. }
  111. LOG(ERROR) << "Tried to read non existant float config parameter " << paramName;
  112. return 0.0f;
  113. }
  114. std::string SystemConfig::getStringConfigParameter(std::string paramName)
  115. {
  116. auto it = stringParameter.find(paramName.data());
  117. if (it != stringParameter.end()) {
  118. return it->second;
  119. }
  120. LOG(ERROR) << "Tried to read non existant float config parameter " << paramName;
  121. return "";
  122. }