ThreadManager.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // 线程管理中心,负责线程的管理,如线程的初始化,启动,停止,包括识别线程的算法加载
  2. using MvCameraControl;
  3. using MvvmScaffoldFrame48.DLL.CameraTools;
  4. using MvvmScaffoldFrame48.DLL.ConfigTools;
  5. using MvvmScaffoldFrame48.Model.ResultModel;
  6. using MvvmScaffoldFrame48.Model.StorageModel.ProcessingConfig;
  7. using MvvmScaffoldFrame48.Model.StorageModel.SystemConfig;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Media.Media3D;
  16. namespace MvvmScaffoldFrame48.DLL.ThreadManager
  17. {
  18. public class ThreadManager
  19. {
  20. #region 变量与实例
  21. //相机组
  22. private readonly CameraGroup[] _cameraGroups = new CameraGroup[1];
  23. // 通信线程
  24. private readonly CommunicationThread _communicationThread;
  25. // 相机配置参数
  26. private List<CameraProcessConfigModel> _cameraConfigurations;
  27. #endregion
  28. #region 构造函数
  29. /// <summary>
  30. /// 构造函数
  31. /// </summary>
  32. public ThreadManager()
  33. {
  34. // 加载相机配置
  35. LoadCameraConfigurations();
  36. // 获取相机列表
  37. HikVision.GetCameraList(out List<IDeviceInfo> cameraInfoList);
  38. // 初始化四个相机组
  39. for (int i = 0; i < _cameraGroups.Count(); i++)
  40. {
  41. // 加载对应相机位的配置参数
  42. var CheckConfig = _cameraConfigurations.Where(c => c.CameraId == i);
  43. if(CheckConfig.Count() == 0)
  44. {
  45. _cameraGroups[i] = null;
  46. continue;
  47. }
  48. var cameraConfig = CheckConfig.FirstOrDefault();
  49. // 获取参数对应的相机
  50. HikVision.GetCamera(out IDeviceInfo cameraInfo, cameraConfig.CameraSN);
  51. if (cameraInfo == null)
  52. {
  53. _cameraGroups[i] = new CameraGroup
  54. {
  55. CameraId = i,
  56. ImageQueue = new ConcurrentQueue<IImage>(),
  57. QueueSemaphore = new SemaphoreSlim(0),
  58. Camera = new HikCamera(),
  59. Configuration = cameraConfig
  60. };
  61. }
  62. else
  63. {
  64. _cameraGroups[i] = new CameraGroup
  65. {
  66. CameraId = i,
  67. ImageQueue = new ConcurrentQueue<IImage>(),
  68. QueueSemaphore = new SemaphoreSlim(0),
  69. Camera = new HikCamera(cameraInfo),
  70. Configuration = cameraConfig
  71. };
  72. }
  73. // 订阅处理结果事件
  74. _cameraGroups[i].ProcessResultAvailable += OnProcessResultAvailable;
  75. }
  76. // 初始化通信和显示线程
  77. _communicationThread = new CommunicationThread();
  78. }
  79. #endregion
  80. #region 公共方法
  81. /// <summary>
  82. /// 动态更改相机算法
  83. /// </summary>
  84. public void ChangeCameraAlgorithm(int cameraId, string algorithmName)
  85. {
  86. if (cameraId >= 0 && cameraId < _cameraGroups.Count())
  87. {
  88. // 相机组未初始化
  89. if (_cameraGroups == null)
  90. {
  91. Console.WriteLine($"相机组{cameraId}未初始化");
  92. return;
  93. }
  94. // 更新配置
  95. var config = _cameraConfigurations.FirstOrDefault(c => c.CameraId == cameraId);
  96. if (config != null)
  97. {
  98. config.ProcessingAlgorithmName = algorithmName;
  99. }
  100. // 如果相机正在运行,需要重启以应用新算法
  101. bool wasRunning = _cameraGroups[cameraId].IsRunning;
  102. if (wasRunning)
  103. {
  104. _cameraGroups[cameraId].Stop();
  105. Thread.Sleep(100); // 等待线程完全停止
  106. }
  107. // 更新相机组配置并重新启动
  108. _cameraGroups[cameraId].Configuration = config;
  109. if (wasRunning)
  110. {
  111. _cameraGroups[cameraId].Start();
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 启动指定相机组
  117. /// </summary>
  118. public void StartCameraGroup(int cameraId)
  119. {
  120. if (cameraId >= 0 && cameraId < _cameraGroups.Count())
  121. {
  122. _cameraGroups[cameraId].Start();
  123. }
  124. }
  125. /// <summary>
  126. /// 停止指定相机组
  127. /// </summary>
  128. public void StopCameraGroup(int cameraId)
  129. {
  130. if (cameraId >= 0 && cameraId < 4)
  131. {
  132. _cameraGroups[cameraId].Stop();
  133. }
  134. }
  135. /// <summary>
  136. /// 启动通信线程
  137. /// </summary>
  138. public void StartCommunication()
  139. {
  140. _communicationThread.Start();
  141. }
  142. /// <summary>
  143. /// 停止通信线程
  144. /// </summary>
  145. public void StopCommunication()
  146. {
  147. _communicationThread.Stop();
  148. }
  149. /// <summary>
  150. /// 启动所有线程
  151. /// </summary>
  152. public void StartAll()
  153. {
  154. for (int i = 0; i < 4; i++)
  155. {
  156. StartCameraGroup(i);
  157. }
  158. StartCommunication();
  159. }
  160. /// <summary>
  161. /// 停止所有线程
  162. /// </summary>
  163. public void StopAll()
  164. {
  165. for (int i = 0; i < 4; i++)
  166. {
  167. StopCameraGroup(i);
  168. }
  169. StopCommunication();
  170. }
  171. /// <summary>
  172. /// 获取相机组运行状态
  173. /// </summary>
  174. public bool IsCameraGroupRunning(int cameraId)
  175. {
  176. return cameraId >= 0 && cameraId < _cameraGroups.Count() && _cameraGroups[cameraId].IsRunning;
  177. }
  178. /// <summary>
  179. /// 为每个相机创建配置
  180. /// </summary>
  181. private CameraProcessConfigModel CreateCameraConfiguration(int cameraId,string ProcessingAlgorithm)
  182. {
  183. var config = new CameraProcessConfigModel
  184. {
  185. CameraId = cameraId,
  186. CameraSN = "00E31768761",
  187. IsEnabled = true
  188. };
  189. switch (ProcessingAlgorithm)
  190. {
  191. case "ProcessingAlgorithm":
  192. config.ProcessingAlgorithmName = "ProcessingAlgorithm";
  193. config.AlgorithmParameters = XMLReadWrite.SerializeToString(new ProcessingAlgorithmConfigModel()
  194. {
  195. Threshold = 0.5,
  196. MinArea = 100,
  197. EnableFiltering = true,
  198. FilterType = "FilterType"
  199. });
  200. break;
  201. }
  202. return config;
  203. }
  204. /// <summary>
  205. /// 动态更新相机算法参数
  206. /// </summary>
  207. public void UpdateCameraParameters(int cameraId, object newParameters)
  208. {
  209. if (cameraId >= 0 && cameraId < _cameraGroups.Count())
  210. {
  211. _cameraGroups[cameraId].UpdateAlgorithmParameters(newParameters);
  212. }
  213. }
  214. /// <summary>
  215. /// 获取通信线程运行状态
  216. /// </summary>
  217. public bool IsCommunicationRunning => _communicationThread.IsRunning;
  218. #endregion
  219. #region 私有方法
  220. /// <summary>
  221. /// 加载相机配置
  222. /// </summary>
  223. private void LoadCameraConfigurations()
  224. {
  225. // 这里可以从XML文件、数据库或其他配置源加载配置
  226. if (File.Exists("TestConfig.xml"))
  227. {
  228. _cameraConfigurations = XMLReadWrite.DeserializeFromXml<List<CameraProcessConfigModel>>("TestConfig.xml");
  229. }
  230. else
  231. {
  232. _cameraConfigurations = new List<CameraProcessConfigModel>()
  233. {
  234. CreateCameraConfiguration(0, "ProcessingAlgorithm"),
  235. };
  236. }
  237. //XMLReadWrite.SerializeToXml(_cameraConfigurations, "TestConfig.xml");
  238. }
  239. /// <summary>
  240. /// 处理相机处理结果
  241. /// </summary>
  242. private void OnProcessResultAvailable(object sender, CameraProcessEventArgsResultModel e)
  243. {
  244. // 将处理结果发送到通信线程
  245. _communicationThread.SendData(e);
  246. // 可以在这里添加其他处理逻辑,如更新显示等
  247. Console.WriteLine($"收到相机{e.CameraId}的处理结果");
  248. }
  249. #endregion
  250. }
  251. }