#include "VrConfig.h" #include "tinyxml2.h" #include using namespace tinyxml2; bool IDroneScrewCtrlConfig::CreateInstance(IDroneScrewCtrlConfig** pp) { if (!pp) return false; *pp = new CVrDroneScrewCtrlConfig(); return true; } CVrDroneScrewCtrlConfig::CVrDroneScrewCtrlConfig() = default; CVrDroneScrewCtrlConfig::~CVrDroneScrewCtrlConfig() = default; static std::string SafeAttr(const XMLElement* e, const char* name, const std::string& dft = "") { if (!e) return dft; const char* v = e->Attribute(name); return v ? std::string(v) : dft; } static int IntAttr(const XMLElement* e, const char* name, int dft) { int v = dft; if (e) e->QueryIntAttribute(name, &v); return v; } static float FloatAttr(const XMLElement* e, const char* name, float dft) { float v = dft; if (e) e->QueryFloatAttribute(name, &v); return v; } static double DoubleAttr(const XMLElement* e, const char* name, double dft) { double v = dft; if (e) e->QueryDoubleAttribute(name, &v); return v; } static bool BoolAttr(const XMLElement* e, const char* name, bool dft) { bool v = dft; if (e) e->QueryBoolAttribute(name, &v); return v; } int CVrDroneScrewCtrlConfig::LoadConfig(const std::string& filePath, DroneScrewCtrlConfigResult& result) { XMLDocument doc; if (doc.LoadFile(filePath.c_str()) != XML_SUCCESS) { return DRONE_CFG_FILE_NOT_FOUND; } XMLElement* root = doc.RootElement(); if (!root) return DRONE_CFG_INVALID_FMT; if (XMLElement* s = root->FirstChildElement("Server")) { result.server.serverIp = SafeAttr(s, "ip", result.server.serverIp); result.server.zmqControlPort = IntAttr(s, "zmqControl", result.server.zmqControlPort); result.server.zmqResultPort = IntAttr(s, "zmqResult", result.server.zmqResultPort); result.server.zmqRawImagePort = IntAttr(s, "zmqRawImage", result.server.zmqRawImagePort); result.server.rtspPort = IntAttr(s, "rtspPort", result.server.rtspPort); result.server.rtspPath = SafeAttr(s, "rtspPath", result.server.rtspPath); result.server.rtspUser = SafeAttr(s, "rtspUser", result.server.rtspUser); result.server.rtspPass = SafeAttr(s, "rtspPass", result.server.rtspPass); result.server.rtspUseTcp = BoolAttr(s, "rtspUseTcp", result.server.rtspUseTcp); } if (XMLElement* a = root->FirstChildElement("Algorithm")) { result.algo.scoreThreshold = FloatAttr(a, "scoreThreshold", result.algo.scoreThreshold); result.algo.nmsThreshold = FloatAttr(a, "nmsThreshold", result.algo.nmsThreshold); result.algo.inputWidth = IntAttr(a, "inputWidth", result.algo.inputWidth); result.algo.inputHeight = IntAttr(a, "inputHeight", result.algo.inputHeight); result.algo.modelType = IntAttr(a, "modelType", result.algo.modelType); result.algo.modelPath = SafeAttr(a, "modelPath", result.algo.modelPath); } if (XMLElement* c = root->FirstChildElement("Camera")) { result.camera.exposure = DoubleAttr(c, "exposureTime", result.camera.exposure); result.camera.gain = DoubleAttr(c, "gain", result.camera.gain); } if (XMLElement* d = root->FirstChildElement("Display")) { result.display.drawBoxes = BoolAttr(d, "drawBoxes", result.display.drawBoxes); result.display.drawScores = BoolAttr(d, "drawScores", result.display.drawScores); result.display.drawClassId = BoolAttr(d, "drawClassId", result.display.drawClassId); result.display.boxThickness = IntAttr(d, "boxThickness", result.display.boxThickness); result.display.maxResultListItems = IntAttr(d, "maxResultListItems", result.display.maxResultListItems); } if (m_pNotify) m_pNotify->OnConfigChanged(result); return DRONE_CFG_OK; } bool CVrDroneScrewCtrlConfig::SaveConfig(const std::string& filePath, const DroneScrewCtrlConfigResult& result) { XMLDocument doc; XMLElement* root = doc.NewElement("DroneScrewCtrlConfig"); doc.InsertEndChild(root); XMLElement* s = doc.NewElement("Server"); s->SetAttribute("ip", result.server.serverIp.c_str()); s->SetAttribute("zmqControl", result.server.zmqControlPort); s->SetAttribute("zmqResult", result.server.zmqResultPort); s->SetAttribute("zmqRawImage", result.server.zmqRawImagePort); s->SetAttribute("rtspPort", result.server.rtspPort); s->SetAttribute("rtspPath", result.server.rtspPath.c_str()); s->SetAttribute("rtspUser", result.server.rtspUser.c_str()); s->SetAttribute("rtspPass", result.server.rtspPass.c_str()); s->SetAttribute("rtspUseTcp", result.server.rtspUseTcp); root->InsertEndChild(s); XMLElement* a = doc.NewElement("Algorithm"); a->SetAttribute("scoreThreshold", result.algo.scoreThreshold); a->SetAttribute("nmsThreshold", result.algo.nmsThreshold); a->SetAttribute("inputWidth", result.algo.inputWidth); a->SetAttribute("inputHeight", result.algo.inputHeight); a->SetAttribute("modelType", result.algo.modelType); a->SetAttribute("modelPath", result.algo.modelPath.c_str()); root->InsertEndChild(a); XMLElement* c = doc.NewElement("Camera"); c->SetAttribute("exposureTime", result.camera.exposure); c->SetAttribute("gain", result.camera.gain); root->InsertEndChild(c); XMLElement* d = doc.NewElement("Display"); d->SetAttribute("drawBoxes", result.display.drawBoxes); d->SetAttribute("drawScores", result.display.drawScores); d->SetAttribute("drawClassId", result.display.drawClassId); d->SetAttribute("boxThickness", result.display.boxThickness); d->SetAttribute("maxResultListItems", result.display.maxResultListItems); root->InsertEndChild(d); if (doc.SaveFile(filePath.c_str()) != XML_SUCCESS) return false; if (m_pNotify) m_pNotify->OnConfigChanged(result); return true; }