#include #include #include "SystemConfig.h" #include "CustomStringUtilities.h" std::map SystemConfig::intParameter; std::map SystemConfig::floatParameter; std::map SystemConfig::stringParameter; std::ostream& operator<<(std::ostream& os, SystemConfig& config) { os << "System Configuration: " << std::endl; std::for_each(config.intParameter.begin(), config.intParameter.end(), [&config, &os](auto& param) { os << "\t"<< param.first << ":\t" << param.second << std::endl; }); return os; } //Read in the data from the config file void SystemConfig::readStandardConfig() { std::fstream fileStream(filePath, std::ios::in); if (fileStream.is_open()) { std::stringstream buf; while (std::getline(fileStream, fileContent)) buf << fileContent << '\n'; fileContent = buf.str(); fileStream.close(); parseConfFile(); } else { LOG(ERROR) << "Couldn't open " << filePath << " for reading -> using standard configuration instead"; } } //Place parameters to read in here SystemConfig::SystemConfig() { //define name and default value here intParameter.emplace("read_data_interval_s", 60); intParameter.emplace("tcp_server_port", 7777); stringParameter.emplace("modbus_poc_ip", "127.0.0.1"); intParameter.emplace("modbus_poc_port", 502); intParameter.emplace("alert_read_interval", 5); stringParameter.emplace("modbus_rtu_device", "dev/tty0"); intParameter.emplace("modbus_rtu_baud", 9800); intParameter.emplace("modbus_rtu_stop_bits", 1); stringParameter.emplace("modbus_rtu_pairity", "E"); intParameter.emplace("modbus_rtu_slave_address", 1); intParameter.emplace("modbus_tcp_slave_address", 1); intParameter.emplace("permanent_param_history", 1000); floatParameter.emplace("crit_residual_current", 30.0); intParameter.emplace("crit_residual_timerange", 12); intParameter.emplace("update_model_rate", 100); intParameter.emplace("narrow_block", 100); } void SystemConfig::parseConfFile() { //remove comments std::regex patternComment("#(\\w|\\s)*\n+"); fileContent = std::regex_replace(fileContent, patternComment, "\n"); auto posHeader = fileContent.find("SystemConfig:"); if (posHeader == std::string::npos){ LOG(ERROR) << "Found no Header for the configuration file -> using standard configuration instead"; return; } for (auto& param : intParameter) { std::regex pattern{ param.first + "\\s*=\\s*\\w+" }; std::smatch match; if (std::regex_search(fileContent, match, pattern)) { std::string numberString = match.str(); CustomStringUtilities::removeAllWhitespaces(numberString); param.second = std::stoi(numberString.substr(numberString.find('=') + 1)); } else { LOG(INFO) << "No specified config paramter (int) for " << param.first << ", using the default value " << param.second; } } for (auto& param : floatParameter) { std::regex pattern{ param.first + "\\s*=\\s*\\d+\\.\\d*" }; std::smatch match; if (std::regex_search(fileContent, match, pattern)) { std::string numberString = match.str(); CustomStringUtilities::removeAllWhitespaces(numberString); param.second = std::stof(numberString.substr(numberString.find('=') + 1)); } else { LOG(INFO) << "No specified config paramter (float) for " << param.first << ", using the default value " << param.second; } } for (auto& param : stringParameter) { std::regex pattern{ param.first + "\\s*=\\s*(\\w|.)*" }; std::smatch match; if (std::regex_search(fileContent, match, pattern)) { std::string paramString = match.str(); CustomStringUtilities::removeAllWhitespaces(paramString); param.second = paramString.substr(paramString.find('=') + 1); } else { LOG(INFO) << "No specified config paramter (float) for " << param.first << ", using the default value " << param.second; } } } int SystemConfig::getIntConfigParameter(std::string paramName){ auto it = intParameter.find(paramName.data()); if (it != intParameter.end()) { return it->second; } LOG(ERROR) << "Tried to read non existant int config parameter " << paramName; return 0; } float SystemConfig::getFloatConfigParameter(std::string paramName){ auto it = floatParameter.find(paramName.data()); if (it != floatParameter.end()) { return it->second; } LOG(ERROR) << "Tried to read non existant float config parameter " << paramName; return 0.0f; } std::string SystemConfig::getStringConfigParameter(std::string paramName) { auto it = stringParameter.find(paramName.data()); if (it != stringParameter.end()) { return it->second; } LOG(ERROR) << "Tried to read non existant float config parameter " << paramName; return ""; }