config.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * @Description:
  3. * @Version: 1.0
  4. * @Autor: lishengyin
  5. * @Date: 2022-03-07 11:52:23
  6. * @LastEditors: Your Name lishengyin@sz-sunwin.com
  7. * @LastEditTime: 2022-09-04 21:31:30
  8. */
  9. #include "config.hpp"
  10. #include "Shell.h"
  11. #include "BasicConfig.h"
  12. #include "httplib.h"
  13. #include <regex>
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <dirent.h>
  18. #include <cstddef>
  19. #include <string.h>
  20. static std::mutex m_mutex;
  21. /**
  22. * @description: 获取智能指针
  23. * @param {*}
  24. * @return {*}
  25. */
  26. std::shared_ptr<config> config::getPtr(){
  27. static Ptr m_config = nullptr;
  28. if(m_config == nullptr){
  29. std::lock_guard<std::mutex> lk(m_mutex);
  30. m_config = std::shared_ptr<config>(new config);
  31. }
  32. return m_config;
  33. }
  34. /**
  35. * @description: 更新数据
  36. * @param {*}
  37. * @return {*}
  38. */
  39. bool config::Update(){
  40. if(m_ini.load(configFile) != 0){
  41. ErrorL << "No configuration file is found, please check if the configuration file exists!";
  42. return false;
  43. }
  44. int ret = 0;
  45. this->NettyIP = m_ini.getStringValue("web", "wed_ip", ret);
  46. this->NettyPort = m_ini.getIntValue("web", "wed_port", ret);
  47. this->vpnIP = m_ini.getStringValue("vpn", "vpn_ip", ret);
  48. this->usr = m_ini.getStringValue("monitor", "usr", ret);
  49. this->pwd = m_ini.getStringValue("monitor", "pwd", ret);
  50. this->comPort = m_ini.getStringValue("expel", "com_port", ret);
  51. this->sensitivity = m_ini.getIntValue("Filter", "sensitivity", ret);
  52. this->interval = m_ini.getIntValue("Filter", "interval", ret);
  53. this->TimeOut = m_ini.getIntValue("Filter", "TimeOut", ret);
  54. this->Spug_IP = m_ini.getStringValue("Spug", "Spug_IP", ret);
  55. this->Spug_Port = m_ini.getIntValue("Spug", "Spug_Port", ret);
  56. this->mysql_IP = m_ini.getStringValue("Mysql", "mysql_IP", ret);
  57. this->mysql_port = m_ini.getIntValue("Mysql", "mysql_port", ret);
  58. this->payServerIP_tcp = m_ini.getStringValue("payServer", "IP", ret);
  59. this->payServerPort_tcp = m_ini.getIntValue("payServer", "Port", ret);
  60. this->SimCode = this->getSimCode();
  61. this->app = this->getApp();
  62. // 同步数据
  63. this->getBasicConfig();
  64. if(config::getPtr()->LowVersion){
  65. int count = m_ini.getIntValue("Device", "count", ret);
  66. DeviceIds.clear();
  67. for(int i = 1; i <= count; i++){
  68. int deviceId = m_ini.getIntValue("Device", "device_"+ std::to_string(i), ret);
  69. DeviceIds.push_back(deviceId);
  70. }
  71. this->deviceType = m_ini.getIntValue("Device", "DeviceType", ret);
  72. this->vpnIP = m_ini.getStringValue("vpn","vpn_ip", ret);
  73. }
  74. if(this->vpn == 1) return false;
  75. this->HostIPs = this->getHostIP();
  76. int flag = 0;
  77. for(auto iter = HostIPs.begin(); iter != HostIPs.end(); iter++){
  78. string::size_type idx;
  79. idx = iter->find("10.8.0.");
  80. if(idx != string::npos){
  81. this->vpnIP = *iter;
  82. flag = 1;
  83. }
  84. }
  85. if(this->localIP != "" && !flag) this->vpnIP = localIP;
  86. return true;
  87. }
  88. /**
  89. * @description: 获取SIMCODE
  90. * @param {*}
  91. * @return {*}
  92. */
  93. std::string config::getSimCode(){
  94. ifstream fin;
  95. fin.open("/etc/gsd/gsdinfo", ios::in);
  96. if (!fin.is_open()){
  97. ErrorL << "无法找到这个文件!" << endl;
  98. return "";
  99. }
  100. char buff[1024] = { 0 };
  101. while (fin >> buff);
  102. fin.close();
  103. std::string str = buff;
  104. return str;
  105. }
  106. /**
  107. * @description: 获取app
  108. * @param {*}
  109. * @return {*}
  110. */
  111. std::string config::getApp(){
  112. ifstream fin;
  113. fin.open("/etc/gsd/gsdApp", ios::in);
  114. if (!fin.is_open()){
  115. ErrorL << "无法找到这个文件!" << endl;
  116. return "";
  117. }
  118. char buff[1024] = { 0 };
  119. while (fin >> buff);
  120. fin.close();
  121. std::string str = buff;
  122. return str;
  123. }
  124. /**
  125. * @description: 获取基础配置
  126. * @param {*}
  127. * @return {*}
  128. */
  129. bool config::getBasicConfig(){
  130. static bool save = false;
  131. static shared_ptr<httplib::Client> httpClient = nullptr;
  132. if(httpClient == nullptr){
  133. httpClient = std::make_shared<httplib::Client>(this->Spug_IP, this->Spug_Port);
  134. save = true;
  135. }
  136. httplib::Params params;
  137. params.emplace("apiKey", this->apiKey);
  138. params.emplace("app", this->app + "_app");
  139. if(this->app != "gsd_dev") params.emplace("env", "gsd_deploy");
  140. else params.emplace("env", "gsd_dev");
  141. params.emplace("noPrefix", "1");
  142. params.emplace("format", "json");
  143. #ifdef RELEASE
  144. httplib::Headers headers = {};
  145. if (auto res = httpClient->Get("/api/apis/config/", params, headers)){
  146. if (res->status == 200) {
  147. BasicConfig basicConfig;
  148. if(basicConfig.JsonToObject(res->body)){
  149. this->NettyIP = basicConfig.NettyIP;
  150. this->NettyPort = basicConfig.NettyPort;
  151. this->usr = basicConfig.usr;
  152. this->pwd = basicConfig.pwd;
  153. this->DiskUsageThreshold = basicConfig.DiskUsageThreshold;
  154. this->HardDiskCleanupTime = basicConfig.HardDiskCleanupTime;
  155. this->InferDataRetentionTime = basicConfig.InferDataRetentionTime;
  156. this->interval = basicConfig.interval;
  157. this->sensitivity = basicConfig.sensitivity;
  158. this->TimeOut = basicConfig.TimeOut;
  159. this->mysql_IP = basicConfig.mysql_IP;
  160. this->mysql_port = basicConfig.mysql_port;
  161. this->debug = basicConfig.debug;
  162. this->getHistoryVideoFlag = basicConfig.getHistoryVideoFlag;
  163. this->ClearPastRecords = basicConfig.ClearPastRecords;
  164. this->bboxSize = basicConfig.bboxSize;
  165. this->ExpelInterval = basicConfig.ExpelInterval;
  166. this->LowVersion = basicConfig.LowVersion;
  167. this->ExpelPort = basicConfig.ExpelPort;
  168. this->InferInterval = basicConfig.InferInterval;
  169. this->payServerIP = basicConfig.payServerIP;
  170. this->filterLevel = basicConfig.filterLevel;
  171. this->vpn = basicConfig.vpn;
  172. this->InferChecker = basicConfig.InferChecker;
  173. this->InferCheckerIP = basicConfig.InferCheckerIP;
  174. this->InferCheckerPort = basicConfig.InferCheckerPort;
  175. if(basicConfig.com_port != "") this->comPort = basicConfig.com_port;
  176. if(!save) return false;
  177. // 写入并保存
  178. int ret;
  179. ret = m_ini.setValue("web", "wed_ip", this->NettyIP);
  180. m_ini.setValue("web", "wed_port", std::to_string(this->NettyPort));
  181. m_ini.setValue("monitor", "usr", this->usr);
  182. m_ini.setValue("monitor", "pwd", this->pwd);
  183. m_ini.setValue("expel", "com_port", this->comPort);
  184. m_ini.setValue("Filter", "sensitivity", std::to_string(this->sensitivity));
  185. m_ini.setValue("Filter", "interval", std::to_string(this->interval));
  186. m_ini.setValue("Filter", "TimeOut", std::to_string(this->TimeOut));
  187. m_ini.setValue("Mysql", "mysql_IP", this->mysql_IP);
  188. m_ini.setValue("Mysql", "mysql_port", std::to_string(this->mysql_port));
  189. m_ini.setValue("vpn", "vpn_ip", this->vpnIP);
  190. m_ini.save();
  191. save = false;
  192. return true;
  193. }else{
  194. WarnL << "Parsing failure" << endl;
  195. }
  196. }else{
  197. ErrorL << "status:" << res->status << "," << res->body << endl;
  198. }
  199. return false;
  200. }else{
  201. auto err = res.error();
  202. ErrorL << "Service unavailable " << err << endl;
  203. return false;
  204. }
  205. #endif
  206. return false;
  207. }
  208. /**
  209. * @description: 获取版本号
  210. * @return {*}
  211. */
  212. std::string config::getVersion(){
  213. std::string version;
  214. version = std::to_string(GSD_MAJOR_VERSION) + "." + std::to_string(GSD_MINOR_VERSION) + "." + std::to_string(GSD_PATCH_VERSION);
  215. return version;
  216. }
  217. /**
  218. * @description: 获取IP列表
  219. * @return {*}
  220. */
  221. vector<string> config::getHostIP(){
  222. std::string shell = "hostname -I | awk '{print NF}'";
  223. vector<string> results;
  224. CShell::exeShellCmd(shell, results);
  225. int num;
  226. for(auto iter = results.begin(); iter != results.end(); iter++){
  227. num = atoi(iter->c_str());
  228. }
  229. vector<std::string> ips;
  230. for(int i = 1; i <= num; i++){
  231. shell = "hostname -I | awk '{print $" + std::to_string(i) + "}'";
  232. results.clear();
  233. CShell::exeShellCmd(shell, results);
  234. ips.push_back(results[0]);
  235. }
  236. return ips;
  237. }
  238. void getFiles(const std::string path, std::vector<std::string> &files)
  239. {
  240. DIR *dir;
  241. struct dirent *ptr;
  242. if ((dir = opendir(path.c_str())) == NULL)
  243. {
  244. perror("Open dir error...");
  245. return;
  246. }
  247. while ((ptr = readdir(dir)) != NULL)
  248. {
  249. if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) {
  250. continue;
  251. } else if (ptr->d_type == 8) {
  252. files.push_back(ptr->d_name);
  253. } else if (ptr->d_type == 10) {
  254. continue;
  255. } else if (ptr->d_type == 4) {
  256. //files.push_back(ptr->d_name);
  257. getFiles(path + ptr->d_name + "/", files);
  258. }
  259. }
  260. closedir(dir);
  261. }
  262. /**
  263. * @description: 获取历史视频
  264. * @return {*}
  265. */
  266. void config::getHistoryVideo(vector<std::string>& result){
  267. getFiles( "/home/GSD/video/",result);
  268. }