gsd_CenterGroup.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. *
  3. * gsd_CenterGroup.cc
  4. *
  5. */
  6. #include "gsd_CenterGroup.h"
  7. using namespace drogon;
  8. using namespace gsd;
  9. /**
  10. * @description: 初始化
  11. * @param {Value} &config
  12. * @return {*}
  13. */
  14. void CenterGroup::initAndStart(const Json::Value &config)
  15. {
  16. InfoL;
  17. this->spug_ip = config.get("spug_ip", "").asString();
  18. this->spug_port = config.get("spug_port", 1).asInt();
  19. // config
  20. if(this->m_config == nullptr) this->m_config = config::getPtr();
  21. // 同步更新数据
  22. this->Update();
  23. // 定时更新
  24. if(this->ConfigTimer == nullptr) this->ConfigTimer = std::make_shared<Timer>(3.0f, [&](){
  25. this->Update();
  26. return true;
  27. }, nullptr);
  28. }
  29. /**
  30. * @description: 更新数据
  31. * @return {*}
  32. */
  33. void CenterGroup::Update()
  34. {
  35. static int num = 0;
  36. static shared_ptr<httplib::Client> httpClient = nullptr;
  37. this->app = m_config->getApp();
  38. if(httpClient == nullptr){
  39. httpClient = std::make_shared<httplib::Client>(this->spug_ip, this->spug_port);
  40. }
  41. httplib::Params params;
  42. params.emplace("apiKey", this->apiKey);
  43. params.emplace("app", this->app + "_app");
  44. if(this->app != "gsd_dev") params.emplace("env", "gsd_deploy");
  45. else params.emplace("env", "gsd_dev");
  46. params.emplace("noPrefix", "1");
  47. params.emplace("format", "json");
  48. httplib::Headers headers = {};
  49. if (auto res = httpClient->Get("/api/apis/config/", params, headers)){
  50. if (res->status == 200) {
  51. num = 0;
  52. auto json = readJsonFromString(res->body);
  53. this->gsd_check_Proportion_th = std::atof(json.get("gsd_check_Proportion_th", "0.5").asString().c_str());
  54. this->gsd_check_Engine = json.get("gsd_check_Engine", "../data/modules/best6_fp16.engine").asString();
  55. this->m_config->Debug = std::atoi(json.get("gsd_check_Debug", "1").asString().c_str()) ;
  56. this->gsd_check_InferSave = std::atoi(json.get("gsd_check_InferSave", "0").asString().c_str());
  57. this->gsd_check_SavePath = json.get("gsd_check_SavePath", "./data/").asString();
  58. vector<int> data;
  59. // target
  60. std::string data_json = json.get("gsd_check_Target", "[ 0 ]").asString();
  61. auto json_val = readJsonFromString(data_json);
  62. for(int i = 0; i < (int)json_val.size(); i++){
  63. int id = std::atoi(json_val[i].asString().c_str());
  64. data.push_back(id);
  65. }
  66. this->setInferTargets(data);
  67. data.clear();
  68. // AlienLabels
  69. data_json = json.get("gsd_check_AlienLabels", "[ 1 ]").asString();
  70. json_val = readJsonFromString(data_json);
  71. for(int i = 0; i < (int)json_val.size(); i++){
  72. int id = std::atoi(json_val[i].asString().c_str());
  73. data.push_back(id);
  74. }
  75. this->setAlienLabels(data);
  76. }else{
  77. if(num >= 6) return;
  78. ErrorL << "status: " << res->status << ", body:" << res->body << endl;
  79. num++;
  80. }
  81. }else{
  82. if(num >= 6) return;
  83. ErrorL << "Service unavailable" << endl;
  84. num ++;
  85. }
  86. }
  87. /**
  88. * @description: 释放资源
  89. * @return {*}
  90. */
  91. void CenterGroup::shutdown()
  92. {
  93. InfoL;
  94. /// Shutdown the plugin
  95. }
  96. /**
  97. * @description: 获取阈值
  98. * @return {*}
  99. */
  100. double CenterGroup::getProportion(){
  101. return this->gsd_check_Proportion_th;
  102. }
  103. /**
  104. * @description: 获取推理引擎
  105. * @return {*}
  106. */
  107. std::string CenterGroup::getInferEngine(){
  108. return this->gsd_check_Engine;
  109. }
  110. /**
  111. * @description: 获取version
  112. * @return {*}
  113. */
  114. std::string CenterGroup::getVersion(){
  115. return this->m_config->getVersion();
  116. }
  117. /**
  118. * @description: getDebug
  119. * @return {*}
  120. */
  121. int CenterGroup::getDebug(){
  122. return m_config->Debug;
  123. }
  124. /**
  125. * @description: 获取
  126. * @return {*}
  127. */
  128. vector<int> CenterGroup::getInferTargets(){
  129. return m_config->getInferTargets();
  130. }
  131. /**
  132. * @description: InferTargets
  133. * @param {vector<int>} data
  134. * @return {*}
  135. */
  136. void CenterGroup::setInferTargets(vector<int> data){
  137. this->m_config->setInferTargets(data);
  138. }
  139. /**
  140. * @description: getAlienLabels
  141. * @return {*}
  142. */
  143. vector<int> CenterGroup::getAlienLabels(){
  144. return this->m_config->getAlienLabels();
  145. }
  146. /**
  147. * @description: setAlienLabels
  148. * @param {vector<int>} data
  149. * @return {*}
  150. */
  151. void CenterGroup::setAlienLabels(vector<int> data){
  152. this->m_config->setAlienLabels(data);
  153. }
  154. /**
  155. * @description: readJsonFromString
  156. * @param {string&} mystr
  157. * @return {*}
  158. */
  159. Json::Value CenterGroup::readJsonFromString(const string& mystr )
  160. {
  161. //1.创建工厂对象
  162. Json::CharReaderBuilder ReaderBuilder;
  163. ReaderBuilder["emitUTF8"] = true;//utf8支持,不加这句,utf8的中文字符会编程\uxxx
  164. //2.通过工厂对象创建 json阅读器对象
  165. unique_ptr<Json::CharReader> charread(ReaderBuilder.newCharReader());
  166. //3.创建json对象
  167. Json::Value root;
  168. //4.把字符串转变为json对象,数据写入root
  169. string strerr;
  170. bool isok = charread->parse(mystr.c_str(),mystr.c_str()+mystr.size(),&root,&strerr);
  171. if(!isok || strerr.size() != 0){
  172. cerr<<"json解析出错";
  173. }
  174. //5.返回有数据的json对象,这个json对象已经能用了
  175. return root;
  176. }
  177. /**
  178. * @description: getInferSave
  179. * @param {*}
  180. * @return {*}
  181. */
  182. bool CenterGroup::getInferSave(){
  183. return this->gsd_check_InferSave ? true : false;
  184. }
  185. /**
  186. * @description: getSavePath
  187. * @param {*}
  188. * @return {*}
  189. */
  190. std::string CenterGroup::getSavePath(){
  191. return gsd_check_SavePath;
  192. }