using System; using System.Collections.Generic; using System.Reflection; using System.Text.Json; using Vinno.AI.CommonSDK.Enums; using Vinno.AI.CommonSDK.Interfaces; using Vinno.AI.CommonSDK.Models; using Vinno.AI.CommonSDK.Models.Data; using Vinno.AI.CommonSDK.Tools; using Vinno.AI.CustomizeDiagnosisSDK.Interfaces; using Vinno.AI.VetHeartDiagnosisSDK.Enums; using Vinno.AI.VetHeartDiagnosisSDK.Interfaces; using Vinno.AI.VetHeartDiagnosisSDK.Models; using Vinno.AI.VetHeartDiagnosisSDK.Tools; namespace Vinno.AI.VetHeartDiagnosisSDK { public class VetHeartDiagnosis : IVetHeartDiagnosis { private readonly IVetHeartDiagnosisService _vetHeartDiagnosisService; private IAIImageProviderV2 _imageProvider; private Dictionary _aiEngineDict = new Dictionary(); private bool _initialized; private bool _disposed; /// /// Used For Detect One Image /// private PipeClient _singleImagePipeClient; /// /// Used For Provide Image /// private PipeClient _imageProviderPipeClient; /// /// Raised when the image evaluation is started. /// public event EventHandler StartEvaluationNotification; /// /// Raised when the image evaluation is finished. /// public event EventHandler>> FinishEvaluationNotification; public VetHeartDiagnosis() { _vetHeartDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService(); if (AIManager.Instance.AINotificationManager != null) { AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived; } _singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForVetHeartDiagnosisSingleImage); _singleImagePipeClient.LogMsgThrow += OnLogMsgThrow; _singleImagePipeClient.Start(); _imageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForVetHeartDiagnosisRawImageProvider); _imageProviderPipeClient.LogMsgThrow += OnLogMsgThrow; _imageProviderPipeClient.Start(); } /// /// 初始化VetHeartMLine Diagnosis /// /// VetHeartMLine Diagnosis Parameter /// Image Provider public void Initialize(VetHeartDiagnosisParameter vetHeartMLineDiagnosisParameter, IAIImageProviderV2 imageProvider = null) { try { if (!_initialized) { _imageProvider = imageProvider; _vetHeartDiagnosisService.Initialize(vetHeartMLineDiagnosisParameter, _imageProvider != null); foreach (var aiEngine in _aiEngineDict.Values) { aiEngine?.Dispose(); } _aiEngineDict.Clear(); foreach (var value in Enum.GetValues(typeof(AIEnumVetHeartEngineType))) { var type = (AIEnumVetHeartEngineType)value; var aiEngine = new AIVetHeartEngine(_vetHeartDiagnosisService, type, _singleImagePipeClient); _aiEngineDict[type] = aiEngine; } _initialized = true; } } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); } } public IAIVetHeartEngine GetAIEngine(AIEnumVetHeartEngineType engineType) { try { if (_aiEngineDict.ContainsKey(engineType)) { var aiEngine = _aiEngineDict[engineType]; if (_imageProvider != null) { _vetHeartDiagnosisService.SetCurrentEngine(engineType); } return aiEngine; } return null; } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); return null; } } /// /// Start Image Provider /// public void Start() { if (_imageProvider != null) { _vetHeartDiagnosisService.Start(); _imageProvider.ImageProvided += OnImageProvided; _imageProvider.Start(); } } /// /// Stop Image Provider /// public void Stop() { if (_imageProvider != null) { _imageProvider.ImageProvided -= OnImageProvided; _imageProvider.Stop(); } _vetHeartDiagnosisService.Stop(); } private void OnLogMsgThrow(object sender, AILogEventArgs e) { AIManager.Instance.AILogManager?.WriteLogInfo(e); } private void OnNotificationReceived(object sender, AINotificationArgs e) { switch (e.NotificationType) { case AIEnumNotificationType.VetHeartMLineDiagnosisStartEvaluationRaised: StartEvaluationNotification?.Invoke(this, EventArgs.Empty); break; case AIEnumNotificationType.VetHeartMLineDiagnosisFinishEvaluationRaised: var transResultList = JsonSerializer.Deserialize>(e.Params?.ToString()); var result = AIConvertHelper.ConvertTransResultListToAIResultDict(transResultList); FinishEvaluationNotification?.Invoke(this, result); break; case AIEnumNotificationType.VetHeartMLineDiagnosisNotifyLogRaised: var logEventArgs = JsonSerializer.Deserialize(e.Params?.ToString()); AIManager.Instance.AILogManager?.WriteLogInfo(logEventArgs); break; } } /// /// 设置每秒图片吞吐量 /// /// 每秒图片吞吐量,必须大于0 public void SetDetectTps(int detectTps) { if (detectTps <= 0) { throw new ArgumentOutOfRangeException($"DetectTps Must > 0. DetectTps : {detectTps}"); } try { _vetHeartDiagnosisService.SetDetectTps(detectTps); } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); } } /// /// 设置间隔时间 /// /// public void SetIntervalTime(int intervalTime) { try { _vetHeartDiagnosisService.SetIntervalTime(intervalTime); } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); } } /// /// 设置检测模式 /// /// public void SetDetectMode(AIEnumDetectMode detectMode) { try { _vetHeartDiagnosisService.SetDetectMode(detectMode); } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); } } private void OnImageProvided(object sender, AIImageInputData inputData) { try { if (inputData == null) { return; } if (inputData.Image == null) { throw new ArgumentNullException(nameof(inputData.Image)); } if (inputData.ImageDataId == null) { throw new ArgumentNullException(nameof(inputData.ImageDataId)); } if (inputData.UltrasoundImageRegion != null || inputData.UltrasoundImageRegion is AIConvexArrayUltrasoundImageRegion || inputData.UltrasoundImageRegion is AILinearArrayUltrasoundImageRegion) { throw new ArgumentOutOfRangeException(nameof(inputData.UltrasoundImageRegion)); } _imageProviderPipeClient?.SendBytes(inputData.Image.DataBuffer); var ultrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(inputData.UltrasoundImageRegion); _vetHeartDiagnosisService.SendImageData(inputData.ImageDataId, inputData.ImageExtraInfo, ultrasoundImageRegion, inputData.Image.Height, inputData.Image.Width, inputData.Image.ColorType); } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); } } /// /// Close VetHeartMLine Diagnosis /// public void Close() { try { if (_disposed) { return; } _initialized = false; if (_imageProvider != null) { _imageProvider.ImageProvided -= OnImageProvided; _imageProvider.Stop(); _imageProvider = null; } _vetHeartDiagnosisService.Close(); foreach (var aiEngine in _aiEngineDict.Values) { aiEngine?.Dispose(); } _aiEngineDict.Clear(); } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); } } public void Dispose() { try { if (!_disposed) { _initialized = false; if (AIManager.Instance.AINotificationManager != null) { AIManager.Instance.AINotificationManager.NotificationReceived -= OnNotificationReceived; } if (_imageProvider != null) { _imageProvider.ImageProvided -= OnImageProvided; _imageProvider.Stop(); _imageProvider = null; } if (_singleImagePipeClient != null) { _singleImagePipeClient.Dispose(); _singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow; _singleImagePipeClient = null; } if (_imageProviderPipeClient != null) { _imageProviderPipeClient.Dispose(); _imageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow; _imageProviderPipeClient = null; } foreach (var aiEngine in _aiEngineDict.Values) { aiEngine?.Dispose(); } _aiEngineDict.Clear(); _disposed = true; } } catch (Exception ex) { AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}")); } } } }