123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /**
- *
- * 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<Timer>(3.0f, [&](){
- this->Update();
- return true;
- }, nullptr);
- }
- /**
- * @description: 更新数据
- * @return {*}
- */
- void CenterGroup::Update()
- {
- static int num = 0;
- static shared_ptr<httplib::Client> httpClient = nullptr;
- this->app = m_config->getApp();
- if(httpClient == nullptr){
- httpClient = std::make_shared<httplib::Client>(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<int> 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<int> CenterGroup::getInferTargets(){
- return m_config->getInferTargets();
- }
- /**
- * @description: InferTargets
- * @param {vector<int>} data
- * @return {*}
- */
- void CenterGroup::setInferTargets(vector<int> data){
- this->m_config->setInferTargets(data);
- }
- /**
- * @description: getAlienLabels
- * @return {*}
- */
- vector<int> CenterGroup::getAlienLabels(){
- return this->m_config->getAlienLabels();
- }
- /**
- * @description: setAlienLabels
- * @param {vector<int>} data
- * @return {*}
- */
- void CenterGroup::setAlienLabels(vector<int> 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<Json::CharReader> 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;
- }
|