| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- // 线程管理中心,负责线程的管理,如线程的初始化,启动,停止,包括识别线程的算法加载
- using MvCameraControl;
- using MvvmScaffoldFrame48.DLL.CameraTools;
- using MvvmScaffoldFrame48.DLL.ConfigTools;
- using MvvmScaffoldFrame48.Model.ResultModel;
- using MvvmScaffoldFrame48.Model.StorageModel.ProcessingConfig;
- using MvvmScaffoldFrame48.Model.StorageModel.SystemConfig;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Media.Media3D;
- namespace MvvmScaffoldFrame48.DLL.ThreadManager
- {
- public class ThreadManager
- {
- #region 变量与实例
- //相机组
- private readonly CameraGroup[] _cameraGroups = new CameraGroup[1];
- // 通信线程
- private readonly CommunicationThread _communicationThread;
- // 相机配置参数
- private List<CameraProcessConfigModel> _cameraConfigurations;
- #endregion
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public ThreadManager()
- {
- // 加载相机配置
- LoadCameraConfigurations();
- // 获取相机列表
- HikVision.GetCameraList(out List<IDeviceInfo> cameraInfoList);
- // 初始化四个相机组
- for (int i = 0; i < _cameraGroups.Count(); i++)
- {
- // 加载对应相机位的配置参数
- var CheckConfig = _cameraConfigurations.Where(c => c.CameraId == i);
- if(CheckConfig.Count() == 0)
- {
- _cameraGroups[i] = null;
- continue;
- }
- var cameraConfig = CheckConfig.FirstOrDefault();
- // 获取参数对应的相机
- HikVision.GetCamera(out IDeviceInfo cameraInfo, cameraConfig.CameraSN);
- if (cameraInfo == null)
- {
- _cameraGroups[i] = new CameraGroup
- {
- CameraId = i,
- ImageQueue = new ConcurrentQueue<IImage>(),
- QueueSemaphore = new SemaphoreSlim(0),
- Camera = new HikCamera(),
- Configuration = cameraConfig
- };
- }
- else
- {
- _cameraGroups[i] = new CameraGroup
- {
- CameraId = i,
- ImageQueue = new ConcurrentQueue<IImage>(),
- QueueSemaphore = new SemaphoreSlim(0),
- Camera = new HikCamera(cameraInfo),
- Configuration = cameraConfig
- };
- }
- // 订阅处理结果事件
- _cameraGroups[i].ProcessResultAvailable += OnProcessResultAvailable;
- }
- // 初始化通信和显示线程
- _communicationThread = new CommunicationThread();
- }
- #endregion
- #region 公共方法
- /// <summary>
- /// 动态更改相机算法
- /// </summary>
- public void ChangeCameraAlgorithm(int cameraId, string algorithmName)
- {
- if (cameraId >= 0 && cameraId < _cameraGroups.Count())
- {
- // 相机组未初始化
- if (_cameraGroups == null)
- {
- Console.WriteLine($"相机组{cameraId}未初始化");
- return;
- }
- // 更新配置
- var config = _cameraConfigurations.FirstOrDefault(c => c.CameraId == cameraId);
- if (config != null)
- {
- config.ProcessingAlgorithmName = algorithmName;
- }
- // 如果相机正在运行,需要重启以应用新算法
- bool wasRunning = _cameraGroups[cameraId].IsRunning;
- if (wasRunning)
- {
- _cameraGroups[cameraId].Stop();
- Thread.Sleep(100); // 等待线程完全停止
- }
- // 更新相机组配置并重新启动
- _cameraGroups[cameraId].Configuration = config;
- if (wasRunning)
- {
- _cameraGroups[cameraId].Start();
- }
- }
- }
- /// <summary>
- /// 启动指定相机组
- /// </summary>
- public void StartCameraGroup(int cameraId)
- {
- if (cameraId >= 0 && cameraId < _cameraGroups.Count())
- {
- _cameraGroups[cameraId].Start();
- }
- }
- /// <summary>
- /// 停止指定相机组
- /// </summary>
- public void StopCameraGroup(int cameraId)
- {
- if (cameraId >= 0 && cameraId < 4)
- {
- _cameraGroups[cameraId].Stop();
- }
- }
- /// <summary>
- /// 启动通信线程
- /// </summary>
- public void StartCommunication()
- {
- _communicationThread.Start();
- }
- /// <summary>
- /// 停止通信线程
- /// </summary>
- public void StopCommunication()
- {
- _communicationThread.Stop();
- }
- /// <summary>
- /// 启动所有线程
- /// </summary>
- public void StartAll()
- {
- for (int i = 0; i < 4; i++)
- {
- StartCameraGroup(i);
- }
- StartCommunication();
- }
- /// <summary>
- /// 停止所有线程
- /// </summary>
- public void StopAll()
- {
- for (int i = 0; i < 4; i++)
- {
- StopCameraGroup(i);
- }
- StopCommunication();
- }
- /// <summary>
- /// 获取相机组运行状态
- /// </summary>
- public bool IsCameraGroupRunning(int cameraId)
- {
- return cameraId >= 0 && cameraId < _cameraGroups.Count() && _cameraGroups[cameraId].IsRunning;
- }
- /// <summary>
- /// 为每个相机创建配置
- /// </summary>
- private CameraProcessConfigModel CreateCameraConfiguration(int cameraId,string ProcessingAlgorithm)
- {
- var config = new CameraProcessConfigModel
- {
- CameraId = cameraId,
- CameraSN = "00E31768761",
- IsEnabled = true
- };
- switch (ProcessingAlgorithm)
- {
- case "ProcessingAlgorithm":
- config.ProcessingAlgorithmName = "ProcessingAlgorithm";
- config.AlgorithmParameters = XMLReadWrite.SerializeToString(new ProcessingAlgorithmConfigModel()
- {
- Threshold = 0.5,
- MinArea = 100,
- EnableFiltering = true,
- FilterType = "FilterType"
- });
- break;
- }
- return config;
- }
- /// <summary>
- /// 动态更新相机算法参数
- /// </summary>
- public void UpdateCameraParameters(int cameraId, object newParameters)
- {
- if (cameraId >= 0 && cameraId < _cameraGroups.Count())
- {
- _cameraGroups[cameraId].UpdateAlgorithmParameters(newParameters);
- }
- }
- /// <summary>
- /// 获取通信线程运行状态
- /// </summary>
- public bool IsCommunicationRunning => _communicationThread.IsRunning;
- #endregion
- #region 私有方法
- /// <summary>
- /// 加载相机配置
- /// </summary>
- private void LoadCameraConfigurations()
- {
- // 这里可以从XML文件、数据库或其他配置源加载配置
- if (File.Exists("TestConfig.xml"))
- {
- _cameraConfigurations = XMLReadWrite.DeserializeFromXml<List<CameraProcessConfigModel>>("TestConfig.xml");
- }
- else
- {
- _cameraConfigurations = new List<CameraProcessConfigModel>()
- {
- CreateCameraConfiguration(0, "ProcessingAlgorithm"),
- };
- }
- //XMLReadWrite.SerializeToXml(_cameraConfigurations, "TestConfig.xml");
- }
- /// <summary>
- /// 处理相机处理结果
- /// </summary>
- private void OnProcessResultAvailable(object sender, CameraProcessEventArgsResultModel e)
- {
- // 将处理结果发送到通信线程
- _communicationThread.SendData(e);
- // 可以在这里添加其他处理逻辑,如更新显示等
- Console.WriteLine($"收到相机{e.CameraId}的处理结果");
- }
- #endregion
- }
- }
|