config.cpp 8.6 KB

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