ThreadCompareItemManager.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. listCreateItems.clear();
  53. listUpdateItems.clear();
  54. listDeleteItems.clear();
  55. processCompareItemInfo(listCreateItems, listUpdateItems, listDeleteItems);
  56. /* 先删除已消失的对比项信息 */
  57. processDeleteCompareItemThreads(listDeleteItems);
  58. /* 更新需要更新的线程 */
  59. updateRunningThreads(listUpdateItems);
  60. /* 再创建新的对比项线程 */
  61. createNewCompareItemThreads(listCreateItems);
  62. }
  63. SPDLOG_LOGGER_INFO(m_logger, "ThreadCompareItemManager: 线程结束");
  64. }
  65. /* 更新基础设置信息,如数据库设置,噪音参数等 */
  66. bool ThreadCompareItemManager::updateBaseSettings()
  67. {
  68. /* 更新基础数据 */
  69. QMap<std::string, std::string> baseSettings;
  70. if(!m_fromWebAPI.getSystemConfig(baseSettings))
  71. {
  72. SPDLOG_LOGGER_ERROR(m_logger, "获取系统配置失败");
  73. return false;
  74. }
  75. /* 将获取到的配置转换成结构体 */
  76. for(auto it = baseSettings.begin(); it != baseSettings.end(); ++it)
  77. {
  78. if(Config_Base == it.key())
  79. {
  80. if(!SysConfig.getBaseConfigFromJson(it.value()))
  81. {
  82. SPDLOG_ERROR("获取基础配置失败");
  83. }
  84. }
  85. else if(Config_CompareAI == it.key())
  86. {
  87. if(!SysConfig.getAICompareConfigFromJson(it.value()))
  88. {
  89. SPDLOG_ERROR("获取AI对比配置失败");
  90. }
  91. }
  92. else if(Config_NoiseBase == it.key())
  93. {
  94. if(!SysConfig.getNoiseDetectBaseConfigFromJson(it.value()))
  95. {
  96. SPDLOG_ERROR("获取噪音检测基础配置失败");
  97. }
  98. }
  99. else if(Config_NoiseParam == it.key())
  100. {
  101. if(!SysConfig.getNoiseDetectParamFromJson(it.value()))
  102. {
  103. SPDLOG_ERROR("获取噪音检测参数失败");
  104. }
  105. }
  106. else if(Config_Database == it.key())
  107. {
  108. if(!SysConfig.getDatabaseConfigFromJson(it.value()))
  109. {
  110. SPDLOG_ERROR("获取数据库设置失败");
  111. }
  112. }
  113. else
  114. {
  115. SPDLOG_DEBUG("未知的系统配置项: {}", it.key());
  116. }
  117. }
  118. /* 检测时段单独获取 */
  119. QMap<int, DetectPeriodConfig_t> mapDetectConfig;
  120. if(!m_fromWebAPI.getDetectPeriodConfig(mapDetectConfig))
  121. {
  122. SPDLOG_ERROR("获取对比项检测时段配置失败");
  123. return false;
  124. }
  125. SysConfig.setDetectPeriodConfig(mapDetectConfig);
  126. return true;
  127. }
  128. /**
  129. * @brief 处理对比项信息,新获取的和已有的对比
  130. *
  131. * @param createList 创建列表
  132. * @param updateList 更新列表,根据对比项ID进行更新信息
  133. * @param deleteList 删除列表
  134. */
  135. void ThreadCompareItemManager::processCompareItemInfo(QList<CompareItemInfo_t>& createList, QList<CompareItemInfo_t>& updateList, QList<int>& deleteList)
  136. {
  137. QMap<int, CompareItemInfo_t> mapNowItems;
  138. /* 先从对比项线程中获取对比项信息 */
  139. for(auto it = m_mapThreads.begin(); it != m_mapThreads.end(); ++it)
  140. {
  141. BaseCalculateThread* pThread = it.value();
  142. if(pThread == nullptr)
  143. {
  144. continue;
  145. }
  146. /* 获取对比项信息 */
  147. CompareItemInfo_t itemInfo = pThread->getThreadInfo().compareItemInfo;
  148. mapNowItems.insert(itemInfo.nID, itemInfo);
  149. }
  150. /* 遍历新获取的对比项信息,找出需要新增的对比项和需要更新的对比项 */
  151. for(const CompareItemInfo_t& item : m_listNewItems)
  152. {
  153. if(!mapNowItems.contains(item.nID))
  154. {
  155. /* 新对比项,添加到创建列表 */
  156. createList.append(item);
  157. } else
  158. {
  159. /* 已有对比项,检查是否需要更新 */
  160. const CompareItemInfo_t& existingItem = mapNowItems.value(item.nID);
  161. /* 先对比基础信息 */
  162. if(!existingItem.isEqualBase(item))
  163. {
  164. /* 基础信息不同,需要更新 */
  165. updateList.append(item);
  166. continue;
  167. }
  168. /* 在对比对比项通道信息 */
  169. if(!existingItem.isEqualRoads(item))
  170. {
  171. /* 通道信息不同,需要更新 */
  172. updateList.append(item);
  173. continue;
  174. }
  175. }
  176. }
  177. /* 遍历当前对比项信息,找出需要删除的对比项 */
  178. for(auto it : mapNowItems)
  179. {
  180. for(const CompareItemInfo_t& newItem : m_listNewItems)
  181. {
  182. if(it.nID == newItem.nID)
  183. {
  184. break; // 找到对应的对比项,不需要删除
  185. }
  186. }
  187. /* 当前对比项不在新获取的对比项中,说明需要删除 */
  188. deleteList.append(it.nID);
  189. }
  190. }
  191. /**
  192. * @brief 处理需要删除的对比项线程
  193. * 1、先处理已经停止的线程
  194. * 2、再将这次列表中的对比项ID对应的线程设置为停止状态,待到下次循环再删除已经停止完成的线程
  195. *
  196. * @param deleteList
  197. */
  198. void ThreadCompareItemManager::processDeleteCompareItemThreads(const QList<int>& deleteList)
  199. {
  200. /* 先处理已经停止运行的线程 */
  201. for(auto it = m_mapThreads.begin(); it != m_mapThreads.end();)
  202. {
  203. BaseCalculateThread* pThread = it.value();
  204. if(pThread == nullptr)
  205. {
  206. SPDLOG_LOGGER_WARN(m_logger, "对比项线程指针为空,即将删除该线程指针");
  207. it = m_mapThreads.erase(it);
  208. continue;
  209. }
  210. if(pThread->getThreadInfo().threadState == EThreadState::State_Stopped)
  211. {
  212. /* 线程已经停止,直接删除 */
  213. SPDLOG_LOGGER_INFO(m_logger, "对比项线程 {} 已经停止,准备删除", pThread->getThreadInfo().compareItemInfo.strName.toStdString());
  214. delete pThread;
  215. it = m_mapThreads.erase(it);
  216. continue;
  217. }
  218. ++it;
  219. }
  220. /* 停止本次需要停止的线程 */
  221. for(auto it : m_mapThreads)
  222. {
  223. int compareItemID = it->getThreadInfo().compareItemInfo.nID;
  224. if(deleteList.contains(compareItemID))
  225. {
  226. /* 设置线程停止标志 */
  227. it->stopThread();
  228. SPDLOG_LOGGER_INFO(m_logger, "对比项线程 {} 设置为停止状态", it->getThreadInfo().compareItemInfo.strName.toStdString());
  229. }
  230. }
  231. }
  232. /* 更新正在运行的线程信息 */
  233. void ThreadCompareItemManager::updateRunningThreads(const QList<CompareItemInfo_t>& updateList)
  234. {
  235. if(updateList.isEmpty())
  236. {
  237. return;
  238. }
  239. for(const CompareItemInfo_t& item : updateList)
  240. {
  241. auto it = m_mapThreads.find(item.nID);
  242. if(it == m_mapThreads.end())
  243. {
  244. SPDLOG_LOGGER_WARN(m_logger, "对比项线程 {} 不存在,无法更新信息", item.strName.toStdString());
  245. continue;
  246. }
  247. BaseCalculateThread* pThread = it.value();
  248. if(pThread == nullptr)
  249. {
  250. continue;
  251. }
  252. CalculateThreadInfo_t threadInfo;
  253. threadInfo.compareItemInfo = item;
  254. pThread->updateThreadInfo(threadInfo);
  255. }
  256. }
  257. /* 创建新的线程 */
  258. bool ThreadCompareItemManager::createNewCompareItemThreads(const QList<CompareItemInfo_t>& createList)
  259. {
  260. if(createList.isEmpty())
  261. {
  262. SPDLOG_LOGGER_DEBUG(m_logger, "没有新的对比项需要创建");
  263. return true;
  264. }
  265. for(auto& it : createList)
  266. {
  267. /* 创建新的对比项线程 */
  268. CalculateThreadInfo_t threadInfo;
  269. threadInfo.compareItemInfo = it;
  270. threadInfo.threadType = EThreadType::Type_CompareItem;
  271. threadInfo.threadState = EThreadState::State_Inited;
  272. /* 创建线程对象 */
  273. BaseCalculateThread* pThread = new CompareItemThread(threadInfo);
  274. if(pThread == nullptr)
  275. {
  276. SPDLOG_LOGGER_ERROR(m_logger, "创建对比项线程 {} 失败", it.strName.toStdString());
  277. return false;
  278. }
  279. /* 启动线程 */
  280. CPPTP.add_task(&CompareItemThread::threadTask, pThread);
  281. /* 添加到线程列表中 */
  282. m_mapThreads.insert(it.nID, pThread);
  283. }
  284. return true;
  285. }