#include "VrBeltTearingConfig.h" #include "IVrBeltTearingConfig.h" #include "tinyxml2.h" #include #include using namespace tinyxml2; VrBeltTearingConfig::VrBeltTearingConfig() : m_notify(nullptr) { } VrBeltTearingConfig::~VrBeltTearingConfig() { } // Also add the static CreateInstance method to the interface class bool IVrBeltTearingConfig::CreateInstance(IVrBeltTearingConfig** ppVrConfig) { if (!ppVrConfig) { return false; } *ppVrConfig = new VrBeltTearingConfig(); return true; } BeltTearingConfigResult VrBeltTearingConfig::LoadConfig(const std::string& filePath) { BeltTearingConfigResult result; XMLDocument doc; if (doc.LoadFile(filePath.c_str()) != XML_SUCCESS) { return result; } XMLElement* root = doc.RootElement(); if (!root) { return result; } // 加载服务器配置 XMLElement* serversElem = root->FirstChildElement("Servers"); if (serversElem) { XMLElement* serverElem = serversElem->FirstChildElement("Server"); while (serverElem) { ServerInfo server; server.name = serverElem->Attribute("name") ? serverElem->Attribute("name") : ""; server.ip = serverElem->Attribute("ip") ? serverElem->Attribute("ip") : ""; server.port = serverElem->IntAttribute("port", 5000); result.servers.push_back(server); serverElem = serverElem->NextSiblingElement("Server"); } } // 加载算法参数 XMLElement* algoParamsElem = root->FirstChildElement("AlgorithmParams"); if (algoParamsElem) { // 皮带撕裂参数 XMLElement* beltTearingElem = algoParamsElem->FirstChildElement("BeltTearingParam"); if (beltTearingElem) { result.algorithmParams.beltTearingParam.tearThreshold = beltTearingElem->DoubleAttribute("TearThreshold", 50.0); result.algorithmParams.beltTearingParam.minTearLength = beltTearingElem->DoubleAttribute("MinTearLength", 100.0); result.algorithmParams.beltTearingParam.maxTearWidth = beltTearingElem->DoubleAttribute("MaxTearWidth", 30.0); } // 图像处理参数 XMLElement* imageProcElem = algoParamsElem->FirstChildElement("ImageProcessingParam"); if (imageProcElem) { result.algorithmParams.imageProcessingParam.blurSize = imageProcElem->IntAttribute("BlurSize", 5); result.algorithmParams.imageProcessingParam.cannyThreshold1 = imageProcElem->IntAttribute("CannyThreshold1", 50); result.algorithmParams.imageProcessingParam.cannyThreshold2 = imageProcElem->IntAttribute("CannyThreshold2", 150); result.algorithmParams.imageProcessingParam.morphologySize = imageProcElem->IntAttribute("MorphologySize", 3); } // 监控参数 XMLElement* monitoringElem = algoParamsElem->FirstChildElement("MonitoringParam"); if (monitoringElem) { result.algorithmParams.monitoringParam.checkInterval = monitoringElem->IntAttribute("CheckInterval", 1000); result.algorithmParams.monitoringParam.alertThreshold = monitoringElem->IntAttribute("AlertThreshold", 3); result.algorithmParams.monitoringParam.maxHistorySize = monitoringElem->IntAttribute("MaxHistorySize", 100); } } // 加载调试参数 XMLElement* debugElem = root->FirstChildElement("DebugParam"); if (debugElem) { result.debugParam.enableDebug = debugElem->BoolAttribute("enableDebug", false); result.debugParam.saveDebugImage = debugElem->BoolAttribute("saveDebugImage", false); result.debugParam.printDetailLog = debugElem->BoolAttribute("printDetailLog", false); const char* debugPath = debugElem->Attribute("debugOutputPath"); if (debugPath) { result.debugParam.debugOutputPath = debugPath; } } // 加载项目类型 XMLElement* projectTypeElem = root->FirstChildElement("ProjectType"); if (projectTypeElem) { const char* typeStr = projectTypeElem->Attribute("type"); if (typeStr && std::string(typeStr) == "BeltMonitoring") { result.projectType = BeltTearingProjectType::BeltMonitoring; } else { result.projectType = BeltTearingProjectType::BeltTearing; } } return result; } bool VrBeltTearingConfig::SaveConfig(const std::string& filePath, BeltTearingConfigResult& configResult) { XMLDocument doc; // 创建根元素 XMLElement* root = doc.NewElement("BeltTearingConfig"); doc.InsertEndChild(root); // 保存服务器配置 if (!configResult.servers.empty()) { XMLElement* serversElem = doc.NewElement("Servers"); root->InsertEndChild(serversElem); for (const auto& server : configResult.servers) { XMLElement* serverElem = doc.NewElement("Server"); serverElem->SetAttribute("name", server.name.c_str()); serverElem->SetAttribute("ip", server.ip.c_str()); serverElem->SetAttribute("port", server.port); serversElem->InsertEndChild(serverElem); } } // 保存算法参数 XMLElement* algoParamsElem = doc.NewElement("AlgorithmParams"); root->InsertEndChild(algoParamsElem); // 皮带撕裂参数 XMLElement* beltTearingElem = doc.NewElement("BeltTearingParam"); beltTearingElem->SetAttribute("TearThreshold", configResult.algorithmParams.beltTearingParam.tearThreshold); beltTearingElem->SetAttribute("MinTearLength", configResult.algorithmParams.beltTearingParam.minTearLength); beltTearingElem->SetAttribute("MaxTearWidth", configResult.algorithmParams.beltTearingParam.maxTearWidth); algoParamsElem->InsertEndChild(beltTearingElem); // 图像处理参数 XMLElement* imageProcElem = doc.NewElement("ImageProcessingParam"); imageProcElem->SetAttribute("BlurSize", configResult.algorithmParams.imageProcessingParam.blurSize); imageProcElem->SetAttribute("CannyThreshold1", configResult.algorithmParams.imageProcessingParam.cannyThreshold1); imageProcElem->SetAttribute("CannyThreshold2", configResult.algorithmParams.imageProcessingParam.cannyThreshold2); imageProcElem->SetAttribute("MorphologySize", configResult.algorithmParams.imageProcessingParam.morphologySize); algoParamsElem->InsertEndChild(imageProcElem); // 监控参数 XMLElement* monitoringElem = doc.NewElement("MonitoringParam"); monitoringElem->SetAttribute("CheckInterval", configResult.algorithmParams.monitoringParam.checkInterval); monitoringElem->SetAttribute("AlertThreshold", configResult.algorithmParams.monitoringParam.alertThreshold); monitoringElem->SetAttribute("MaxHistorySize", configResult.algorithmParams.monitoringParam.maxHistorySize); algoParamsElem->InsertEndChild(monitoringElem); // 保存调试参数 XMLElement* debugElem = doc.NewElement("DebugParam"); debugElem->SetAttribute("enableDebug", configResult.debugParam.enableDebug); debugElem->SetAttribute("saveDebugImage", configResult.debugParam.saveDebugImage); debugElem->SetAttribute("printDetailLog", configResult.debugParam.printDetailLog); debugElem->SetAttribute("debugOutputPath", configResult.debugParam.debugOutputPath.c_str()); root->InsertEndChild(debugElem); // 保存项目类型 XMLElement* projectTypeElem = doc.NewElement("ProjectType"); projectTypeElem->SetAttribute("type", configResult.projectType == BeltTearingProjectType::BeltMonitoring ? "BeltMonitoring" : "BeltTearing"); root->InsertEndChild(projectTypeElem); return doc.SaveFile(filePath.c_str()) == XML_SUCCESS; } void VrBeltTearingConfig::SetConfigChangeNotify(IVrBeltTearingConfigChangeNotify* notify) { m_notify = notify; }