/** * * gsd_CenterGroup.cc * */ #include "gsd_CenterGroup.h" using namespace drogon; using namespace gsd; /** * @description: 初始化 * @param {Value} &config * @return {*} */ void CenterGroup::initAndStart(const Json::Value &config) { InfoL; this->spug_ip = config.get("spug_ip", "").asString(); this->spug_port = config.get("spug_port", 1).asInt(); // config if(this->m_config == nullptr) this->m_config = config::getPtr(); // 同步更新数据 this->Update(); // 定时更新 if(this->ConfigTimer == nullptr) this->ConfigTimer = std::make_shared(3.0f, [&](){ this->Update(); return true; }, nullptr); } /** * @description: 更新数据 * @return {*} */ void CenterGroup::Update() { static int num = 0; static shared_ptr httpClient = nullptr; this->app = m_config->getApp(); if(httpClient == nullptr){ httpClient = std::make_shared(this->spug_ip, this->spug_port); } httplib::Params params; params.emplace("apiKey", this->apiKey); params.emplace("app", this->app + "_app"); if(this->app != "gsd_dev") params.emplace("env", "gsd_deploy"); else params.emplace("env", "gsd_dev"); params.emplace("noPrefix", "1"); params.emplace("format", "json"); httplib::Headers headers = {}; if (auto res = httpClient->Get("/api/apis/config/", params, headers)){ if (res->status == 200) { num = 0; auto json = readJsonFromString(res->body); this->gsd_check_Proportion_th = std::atof(json.get("gsd_check_Proportion_th", "0.5").asString().c_str()); this->gsd_check_Engine = json.get("gsd_check_Engine", "../data/modules/best6_fp16.engine").asString(); this->m_config->Debug = std::atoi(json.get("gsd_check_Debug", "1").asString().c_str()) ; this->gsd_check_InferSave = std::atoi(json.get("gsd_check_InferSave", "0").asString().c_str()); this->gsd_check_SavePath = json.get("gsd_check_SavePath", "./data/").asString(); vector data; // target std::string data_json = json.get("gsd_check_Target", "[ 0 ]").asString(); auto json_val = readJsonFromString(data_json); for(int i = 0; i < (int)json_val.size(); i++){ int id = std::atoi(json_val[i].asString().c_str()); data.push_back(id); } this->setInferTargets(data); data.clear(); // AlienLabels data_json = json.get("gsd_check_AlienLabels", "[ 1 ]").asString(); json_val = readJsonFromString(data_json); for(int i = 0; i < (int)json_val.size(); i++){ int id = std::atoi(json_val[i].asString().c_str()); data.push_back(id); } this->setAlienLabels(data); }else{ if(num >= 6) return; ErrorL << "status: " << res->status << ", body:" << res->body << endl; num++; } }else{ if(num >= 6) return; ErrorL << "Service unavailable" << endl; num ++; } } /** * @description: 释放资源 * @return {*} */ void CenterGroup::shutdown() { InfoL; /// Shutdown the plugin } /** * @description: 获取阈值 * @return {*} */ double CenterGroup::getProportion(){ return this->gsd_check_Proportion_th; } /** * @description: 获取推理引擎 * @return {*} */ std::string CenterGroup::getInferEngine(){ return this->gsd_check_Engine; } /** * @description: 获取version * @return {*} */ std::string CenterGroup::getVersion(){ return this->m_config->getVersion(); } /** * @description: getDebug * @return {*} */ int CenterGroup::getDebug(){ return m_config->Debug; } /** * @description: 获取 * @return {*} */ vector CenterGroup::getInferTargets(){ return m_config->getInferTargets(); } /** * @description: InferTargets * @param {vector} data * @return {*} */ void CenterGroup::setInferTargets(vector data){ this->m_config->setInferTargets(data); } /** * @description: getAlienLabels * @return {*} */ vector CenterGroup::getAlienLabels(){ return this->m_config->getAlienLabels(); } /** * @description: setAlienLabels * @param {vector} data * @return {*} */ void CenterGroup::setAlienLabels(vector data){ this->m_config->setAlienLabels(data); } /** * @description: readJsonFromString * @param {string&} mystr * @return {*} */ Json::Value CenterGroup::readJsonFromString(const string& mystr ) { //1.创建工厂对象 Json::CharReaderBuilder ReaderBuilder; ReaderBuilder["emitUTF8"] = true;//utf8支持,不加这句,utf8的中文字符会编程\uxxx //2.通过工厂对象创建 json阅读器对象 unique_ptr charread(ReaderBuilder.newCharReader()); //3.创建json对象 Json::Value root; //4.把字符串转变为json对象,数据写入root string strerr; bool isok = charread->parse(mystr.c_str(),mystr.c_str()+mystr.size(),&root,&strerr); if(!isok || strerr.size() != 0){ cerr<<"json解析出错"; } //5.返回有数据的json对象,这个json对象已经能用了 return root; } /** * @description: getInferSave * @param {*} * @return {*} */ bool CenterGroup::getInferSave(){ return this->gsd_check_InferSave ? true : false; } /** * @description: getSavePath * @param {*} * @return {*} */ std::string CenterGroup::getSavePath(){ return gsd_check_SavePath; }