/* * @Author: Jacky * @Date: 2024-05-22 16:16:11 * @LastEditors: Jacky * @LastEditTime: 2024-09-11 08:41:47 * @FilePath: /GeneralTracker/Universal/IConfiguration.cpp */ #include "IConfiguration.h" IConfiguration::IConfiguration(const std::string &configFile){ _config_file = configFile; } int IConfiguration::parse(ConfigHandle func){ if(_config_file.empty()){ return -1; } std::ifstream fs(_config_file); if(!fs.is_open()){ std::cout << "failed to open config file: " << _config_json << std::endl; return -1; } std::stringstream buffer; buffer << fs.rdbuf(); std::string conf = buffer.str(); fs.close(); _config_json = json::parse(conf); #if 1 for (auto& element : _config_json.items()) { parseConfig(element.key(), func); } #else parseConfig(MODULE_VI, "vi", func); parseConfig(MODULE_VO, "vo", func); parseConfig(MODULE_VENC, "venc", func); parseConfig(MODULE_VDEC, "vdec", func); parseConfig(MODULE_VPSS, "vpss", func); parseConfig(MODULE_ARITH, "arith", func); parseConfig(MODULE_RTSP, "rtsp", func); parseConfig(MODULE_TS, "ts", func); #endif return 0; } int IConfiguration::dump(){ return 0; } int IConfiguration::parseConfig(std::string name, ConfigHandle func){ MODULE_TYPE_E type = MODULE_ILLEGAL; if(name == "vi"){ type = MODULE_VI; }else if(name == "vo"){ type = MODULE_VO; }else if(name == "venc"){ type = MODULE_VENC; }else if(name == "vdec"){ type = MODULE_VDEC; }else if(name == "vpss"){ type = MODULE_VPSS; }else if(name == "arith"){ type = MODULE_ARITH; }else if(name == "osd"){ type = MODULE_OSD; }else if(name == "record"){ type = MODULE_RECORD; }else if(name == "post-vpss"){ //ͼÏñºó´¦Àí type = MODULE_POST_VPSS; } if(MODULE_ILLEGAL == type){ return -1; } return parseConfig(type, name, func); } int IConfiguration::parseConfig(MODULE_TYPE_E type, std::string name, ConfigHandle func){ if(_config_json.contains(name)){ if(_config_json[name].is_array()){ json arr = _config_json[name]; for(json &mod : arr){ module_config_t config; parseModule(type, mod, config, name); func(config); } }else if(_config_json[name].is_object()){ module_config_t config; parseModule(type, _config_json[name], config, name); func(config); } } return 0; } int IConfiguration::parseModule(MODULE_TYPE_E type, json &module, module_config_t &config, std::string key){ config.type = type; config.enable = module["enable"]; config.msg_enable = module["message"]; config.name = module["name"]; if(module.contains("bind")){ std::list bind = module["bind"]; std::cout << config.name << " bind : " ; for(std::string &m : bind){ config.bind.push_back(m); std::cout << m << ": "; } std::cout << ", size= " << config.bind.size()<< std::endl; } config.parent = key; return 0; }