ThreadCompareItemManager.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "ThreadCompareItemManager.h"
  2. #include "GlobalInfo.h"
  3. #include "SystemConfig.h"
  4. #include "CompareItemThread.h"
  5. #include "ThreadPool.h"
  6. ThreadCompareItemManager::ThreadCompareItemManager()
  7. {
  8. m_logger = spdlog::get("CompareItemManager");
  9. if(m_logger == nullptr)
  10. {
  11. fmt::print("ThreadCompareItemManager: CompareItemManager Logger not found.\n");
  12. return;
  13. }
  14. }
  15. ThreadCompareItemManager::~ThreadCompareItemManager()
  16. {
  17. }
  18. /* 线程函数 */
  19. void ThreadCompareItemManager::thread_CompareItemManager()
  20. {
  21. /* 初始化webapi */
  22. m_webAPIUrl = GInfo.webAPIUrl();
  23. m_webAPIID = GInfo.webAPIID();
  24. m_appType = GInfo.appType();
  25. if(!m_fromWebAPI.initWebApi(m_webAPIUrl, m_webAPIID, m_appType))
  26. {
  27. SPDLOG_LOGGER_ERROR(m_logger, "ThreadCompareItemManager: 初始化WebAPI失败");
  28. return;
  29. }
  30. /* 创建一些变量 */
  31. int threadSleepTime = 2; // 线程休眠时间,单位秒
  32. QList<CompareItemInfo_t> listCreateItems; // 新创建的对比项列表
  33. QList<CompareItemInfo_t> listUpdateItems; // 更新的对比项列表
  34. QList<int> listDeleteItems; // 删除的对比项列表
  35. /* 获取基础配置,目前只获取一次 */
  36. updateBaseSettings();
  37. SPDLOG_LOGGER_INFO(m_logger, "开启对比项管理线程");
  38. while(true)
  39. {
  40. std::this_thread::sleep_for(std::chrono::seconds(threadSleepTime));
  41. if(threadSleepTime < 10)
  42. {
  43. /* 线程睡眠时间恢复为10秒 */
  44. threadSleepTime = 10;
  45. }
  46. /* ------------------------------ 处理对比项信息 ------------------------------ */
  47. if(!m_fromWebAPI.getCompareItemInfo(m_listNewItems))
  48. {
  49. SPDLOG_LOGGER_DEBUG(m_logger, "ThreadCompareItemManager: 获取对比项失败");
  50. continue;
  51. }
  52. processCompareItemInfo(listCreateItems, listUpdateItems, listDeleteItems);
  53. SPDLOG_LOGGER_DEBUG(m_logger, "要退出的对比项个数: {}, 要更新的对比项个数: {}, 要创建的对比项个数: {}",
  54. listDeleteItems.size(), listUpdateItems.size(), listCreateItems.size());
  55. /* 先删除已消失的对比项信息 */
  56. processDeleteCompareItemThreads(listDeleteItems);
  57. /* 更新需要更新的线程 */
  58. updateRunningThreads(listUpdateItems);
  59. /* 再创建新的对比项线程 */
  60. createNewCompareItemThreads(listCreateItems);
  61. }
  62. SPDLOG_LOGGER_INFO(m_logger, "ThreadCompareItemManager: 线程结束");
  63. }
  64. /* 更新基础设置信息,如数据库设置,噪音参数等 */
  65. bool ThreadCompareItemManager::updateBaseSettings()
  66. {
  67. /* 更新基础数据 */
  68. QMap<std::string, std::string> baseSettings;
  69. if(!m_fromWebAPI.getSystemConfig(baseSettings))
  70. {
  71. SPDLOG_LOGGER_ERROR(m_logger, "获取系统配置失败");
  72. return false;
  73. }
  74. /* 将获取到的配置转换成结构体 */
  75. for(auto it = baseSettings.begin(); it != baseSettings.end(); ++it)
  76. {
  77. if(Config_Base == it.key())
  78. {
  79. if(!SysConfig.getBaseConfigFromJson(it.value()))
  80. {
  81. SPDLOG_ERROR("获取基础配置失败");
  82. }
  83. }
  84. else if(Config_CompareAI == it.key())
  85. {
  86. if(!SysConfig.getAICompareConfigFromJson(it.value()))
  87. {
  88. SPDLOG_ERROR("获取AI对比配置失败");
  89. }
  90. }
  91. else if(Config_NoiseBase == it.key())
  92. {
  93. if(!SysConfig.getNoiseDetectBaseConfigFromJson(it.value()))
  94. {
  95. SPDLOG_ERROR("获取噪音检测基础配置失败");
  96. }
  97. }
  98. else if(Config_NoiseParam == it.key())
  99. {
  100. if(!SysConfig.getNoiseDetectParamFromJson(it.value()))
  101. {
  102. SPDLOG_ERROR("获取噪音检测参数失败");
  103. }
  104. }
  105. else if(Config_Database == it.key())
  106. {
  107. if(!SysConfig.getDatabaseConfigFromJson(it.value()))
  108. {
  109. SPDLOG_ERROR("获取数据库设置失败");
  110. }
  111. }
  112. else
  113. {
  114. SPDLOG_DEBUG("未知的系统配置项: {}", it.key());
  115. }
  116. }
  117. /* 检测时段单独获取 */
  118. QMap<int, DetectPeriodConfig_t> mapDetectConfig;
  119. if(!m_fromWebAPI.getDetectPeriodConfig(mapDetectConfig))
  120. {
  121. SPDLOG_ERROR("获取对比项检测时段配置失败");
  122. return false;
  123. }
  124. SysConfig.setDetectPeriodConfig(mapDetectConfig);
  125. return true;
  126. }
  127. /**
  128. * @brief 处理对比项信息,新获取的和已有的对比
  129. *
  130. * @param createList 创建列表
  131. * @param updateList 更新列表,根据对比项ID进行更新信息
  132. * @param deleteList 删除列表
  133. */
  134. void ThreadCompareItemManager::processCompareItemInfo(QList<CompareItemInfo_t>& createList, QList<CompareItemInfo_t>& updateList, QList<int>& deleteList)
  135. {
  136. createList.clear();
  137. updateList.clear();
  138. deleteList.clear();
  139. QMap<int, CompareItemInfo_t> mapNowItems;
  140. /* 先从对比项线程中获取对比项信息 */
  141. for(auto it = m_mapThreads.begin(); it != m_mapThreads.end(); ++it)
  142. {
  143. BaseCalculateThread* pThread = it.value();
  144. if(pThread == nullptr)
  145. {
  146. continue;
  147. }
  148. /* 获取对比项信息 */
  149. CompareItemInfo_t itemInfo = pThread->getThreadInfo().compareItemInfo;
  150. mapNowItems.insert(itemInfo.nID, itemInfo);
  151. }
  152. /* 遍历新获取的对比项信息,找出需要新增的对比项和需要更新的对比项 */
  153. for(const CompareItemInfo_t& item : m_listNewItems)
  154. {
  155. if(!mapNowItems.contains(item.nID))
  156. {
  157. /* 新对比项,添加到创建列表 */
  158. createList.append(item);
  159. } else
  160. {
  161. /* 已有对比项,检查是否需要更新 */
  162. const CompareItemInfo_t& existingItem = mapNowItems.value(item.nID);
  163. /* 先对比基础信息 */
  164. if(!existingItem.isEqualBase(item))
  165. {
  166. /* 基础信息不同,需要更新 */
  167. updateList.append(item);
  168. continue;
  169. }
  170. /* 在对比对比项通道信息 */
  171. if(!existingItem.isEqualRoads(item))
  172. {
  173. /* 通道信息不同,需要更新 */
  174. updateList.append(item);
  175. continue;
  176. }
  177. }
  178. }
  179. /* 遍历当前对比项信息,找出需要删除的对比项 */
  180. for(auto it : mapNowItems)
  181. {
  182. bool isFound = false;
  183. for(const CompareItemInfo_t& newItem : m_listNewItems)
  184. {
  185. if(it.nID == newItem.nID)
  186. {
  187. isFound = true;
  188. break; // 找到对应的对比项,不需要删除
  189. }
  190. }
  191. if(!isFound)
  192. {
  193. /* 当前对比项不在新获取的对比项中,说明需要删除 */
  194. deleteList.append(it.nID);
  195. }
  196. }
  197. }
  198. /**
  199. * @brief 处理需要删除的对比项线程
  200. * 1、先处理已经停止的线程
  201. * 2、再将这次列表中的对比项ID对应的线程设置为停止状态,待到下次循环再删除已经停止完成的线程
  202. *
  203. * @param deleteList
  204. */
  205. void ThreadCompareItemManager::processDeleteCompareItemThreads(const QList<int>& deleteList)
  206. {
  207. /* 先处理已经停止运行的线程 */
  208. for(auto it = m_mapThreads.begin(); it != m_mapThreads.end();)
  209. {
  210. BaseCalculateThread* pThread = it.value();
  211. if(pThread == nullptr)
  212. {
  213. SPDLOG_LOGGER_WARN(m_logger, "对比项线程指针为空,即将删除该线程指针");
  214. it = m_mapThreads.erase(it);
  215. continue;
  216. }
  217. if(pThread->getThreadInfo().threadState == EThreadState::State_Stopped)
  218. {
  219. /* 线程已经停止,直接删除 */
  220. SPDLOG_LOGGER_INFO(m_logger, "对比项线程 {} 已经停止,准备删除", pThread->getThreadInfo().compareItemInfo.strName.toStdString());
  221. delete pThread;
  222. it = m_mapThreads.erase(it);
  223. continue;
  224. }
  225. ++it;
  226. }
  227. /* 停止本次需要停止的线程 */
  228. for(auto it : m_mapThreads)
  229. {
  230. int compareItemID = it->getThreadInfo().compareItemInfo.nID;
  231. if(deleteList.contains(compareItemID))
  232. {
  233. /* 设置线程停止标志 */
  234. it->stopThread();
  235. SPDLOG_LOGGER_INFO(m_logger, "对比项线程 {} 设置为停止状态", it->getThreadInfo().compareItemInfo.strName.toStdString());
  236. }
  237. }
  238. }
  239. /* 更新正在运行的线程信息 */
  240. void ThreadCompareItemManager::updateRunningThreads(const QList<CompareItemInfo_t>& updateList)
  241. {
  242. if(updateList.isEmpty())
  243. {
  244. return;
  245. }
  246. for(const CompareItemInfo_t& item : updateList)
  247. {
  248. auto it = m_mapThreads.find(item.nID);
  249. if(it == m_mapThreads.end())
  250. {
  251. SPDLOG_LOGGER_WARN(m_logger, "对比项线程 {} 不存在,无法更新信息", item.strName.toStdString());
  252. continue;
  253. }
  254. BaseCalculateThread* pThread = it.value();
  255. if(pThread == nullptr)
  256. {
  257. continue;
  258. }
  259. CalculateThreadInfo_t threadInfo;
  260. threadInfo.compareItemInfo = item;
  261. pThread->updateThreadInfo(threadInfo);
  262. }
  263. }
  264. /* 创建新的线程 */
  265. bool ThreadCompareItemManager::createNewCompareItemThreads(const QList<CompareItemInfo_t>& createList)
  266. {
  267. if(createList.isEmpty())
  268. {
  269. SPDLOG_LOGGER_DEBUG(m_logger, "没有新的对比项需要创建");
  270. return true;
  271. }
  272. for(auto& it : createList)
  273. {
  274. /* 创建新的对比项线程 */
  275. CalculateThreadInfo_t threadInfo;
  276. threadInfo.compareItemInfo = it;
  277. threadInfo.threadType = EThreadType::Type_CompareItem;
  278. threadInfo.threadState = EThreadState::State_Inited;
  279. /* 创建线程对象 */
  280. BaseCalculateThread* pThread = new CompareItemThread(threadInfo);
  281. if(pThread == nullptr)
  282. {
  283. SPDLOG_LOGGER_ERROR(m_logger, "创建对比项线程 {} 失败", it.strName.toStdString());
  284. return false;
  285. }
  286. /* 启动线程 */
  287. CPPTP.add_task(&CompareItemThread::threadTask, pThread);
  288. /* 添加到线程列表中 */
  289. m_mapThreads.insert(it.nID, pThread);
  290. }
  291. return true;
  292. }