using System;
using System.Reflection;
using System.Text.Json;
using Vinno.AI.AmnioticFluidDiagnosisSDK.Interfaces;
using Vinno.AI.AmnioticFluidDiagnosisSDK.Models;
using Vinno.AI.CommonSDK.Enums;
using Vinno.AI.CommonSDK.Interfaces;
using Vinno.AI.CommonSDK.Models;
using Vinno.AI.CommonSDK.Tools;
namespace Vinno.AI.AmnioticFluidDiagnosisSDK
{
public class AmnioticFluidDiagnosis : IAmnioticFluidDiagnosis
{
private readonly IAmnioticFluidDiagnosisService _amnioticFluidDiagnosisService;
private IAIImageProvider _imageProvider;
private bool _initialized;
private bool _disposed;
///
/// Used For Detect One Image
///
private PipeClient _singleImagePipeClient;
///
/// Used For Provide Raw Image
///
private PipeClient _rawImageProviderPipeClient;
///
/// Used For Provide Byte Image
///
private PipeClient _byteImageProviderPipeClient;
///
/// Raised when the image evaluation is started.
///
public event EventHandler StartEvaluationNotification;
///
/// Raised when the image evaluation is finished.
///
public event EventHandler FinishEvaluationNotification;
public AmnioticFluidDiagnosis()
{
_amnioticFluidDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService();
if (AIManager.Instance.AINotificationManager != null)
{
AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
}
_singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisSingleImage);
_singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
_singleImagePipeClient.Start();
_rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisRawImageProvider);
_rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
_rawImageProviderPipeClient.Start();
_byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAmnioticFluidDiagnosisByteImageProvider);
_byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
_byteImageProviderPipeClient.Start();
}
///
/// 初始化AmnioticFluid Diagnosis
///
/// AmnioticFluid Diagnosis Parameter
/// Image Provider
public void Initialize(AmnioticFluidDiagnosisParameter amnioticFluidDiagnosisParameter, IAIImageProvider imageProvider = null)
{
try
{
if (!_initialized)
{
_imageProvider = imageProvider;
_amnioticFluidDiagnosisService.Initialize(amnioticFluidDiagnosisParameter, _imageProvider != null);
_initialized = true;
}
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
}
}
///
/// Start Image Provider
///
public void Start()
{
if (_imageProvider != null)
{
_amnioticFluidDiagnosisService.Start();
_imageProvider.ByteImageProvided += OnByteImageProvided;
_imageProvider.RawImageProvided += OnRawImageProvided;
_imageProvider.Start();
}
}
///
/// Stop Image Provider
///
public void Stop()
{
if (_imageProvider != null)
{
_imageProvider.ByteImageProvided -= OnByteImageProvided;
_imageProvider.RawImageProvided -= OnRawImageProvided;
_imageProvider.Stop();
}
_amnioticFluidDiagnosisService.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.AmnioticFluidDiagnosisStartEvaluationRaised:
StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
break;
case AIEnumNotificationType.AmnioticFluidDiagnosisFinishEvaluationRaised:
var aiAmnioticFluidMeasurementResults = JsonSerializer.Deserialize(e.Params?.ToString());
FinishEvaluationNotification?.Invoke(this, aiAmnioticFluidMeasurementResults);
break;
case AIEnumNotificationType.AmnioticFluidDiagnosisNotifyLogRaised:
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
{
_amnioticFluidDiagnosisService.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
{
_amnioticFluidDiagnosisService.SetIntervalTime(intervalTime);
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
}
}
///
/// 设置一个像素代表的实际物理距离是多少cm
///
///
public void SetCmPerPixel(float cmPerPixel)
{
try
{
_amnioticFluidDiagnosisService.SetCmPerPixel(cmPerPixel);
}
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
{
_amnioticFluidDiagnosisService.SetDetectMode(detectMode);
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
}
}
///
/// 检测单张Raw Image
///
/// 图像资源
/// 设置一个像素代表的实际物理距离是多少cm
///
public AIAmnioticFluidMeasurementResults DetectOneImage(AIRawImage rawImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
{
if (rawImage == null)
{
throw new ArgumentNullException(nameof(rawImage));
}
if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
{
throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
}
try
{
var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
_singleImagePipeClient.SendBytes(rawImage.DataBuffer);
return _amnioticFluidDiagnosisService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType, cmPerPixel, transUltrasoundImageRegion);
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
return null;
}
}
///
/// 检测单张Byte Image
///
/// 图像资源
/// 设置一个像素代表的实际物理距离是多少cm
///
public AIAmnioticFluidMeasurementResults DetectOneImage(byte[] byteImage, float cmPerPixel, IAIUltrasoundImageRegion ultrasoundImageRegion)
{
if (byteImage == null)
{
throw new ArgumentNullException(nameof(byteImage));
}
if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
{
throw new ArgumentException("DetectOneImage Only Support AIConvexArrayUltrasoundImageRegion Type");
}
try
{
var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
_singleImagePipeClient.SendBytes(byteImage);
return _amnioticFluidDiagnosisService.DetectOneByteImage(cmPerPixel, transUltrasoundImageRegion);
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
return null;
}
}
///
/// 设置超声机图像信息(当使用ImageProvider时需设置)
///
///
public void SetUltrasoundImageRegionInfo(IAIUltrasoundImageRegion ultrasoundImageRegion)
{
if (!(ultrasoundImageRegion is AIConvexArrayUltrasoundImageRegion))
{
throw new ArgumentException("SetUltrasoundImageRegionInfo Only Support AIConvexArrayUltrasoundImageRegion Type");
}
try
{
var aiConvexArrayUltrasoundImageRegion = (AIConvexArrayUltrasoundImageRegion)ultrasoundImageRegion;
var transUltrasoundImageRegion = AICommonConvertHelper.ConvertIAIUltrasoundImageRegionToTransAIUltrasoundImageRegion(aiConvexArrayUltrasoundImageRegion);
_amnioticFluidDiagnosisService.SetProbeImageInfo(transUltrasoundImageRegion);
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
}
}
private void OnByteImageProvided(object sender, byte[] byteImage)
{
try
{
if (byteImage == null)
{
return;
}
_byteImageProviderPipeClient?.SendBytes(byteImage);
_amnioticFluidDiagnosisService.SendByteImageData();
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
}
}
private void OnRawImageProvided(object sender, AIRawImage rawImage)
{
try
{
if (rawImage == null)
{
return;
}
_rawImageProviderPipeClient?.SendBytes(rawImage.DataBuffer);
_amnioticFluidDiagnosisService.SendRawImageData(rawImage.Height, rawImage.Width, rawImage.ColorType);
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
}
}
///
/// Close AmnioticFluid Diagnosis
///
public void Close()
{
try
{
if (_disposed)
{
return;
}
_initialized = false;
if (_imageProvider != null)
{
_imageProvider.ByteImageProvided -= OnByteImageProvided;
_imageProvider.RawImageProvided -= OnRawImageProvided;
_imageProvider.Stop();
_imageProvider = null;
}
_amnioticFluidDiagnosisService.Close();
}
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.ByteImageProvided -= OnByteImageProvided;
_imageProvider.RawImageProvided -= OnRawImageProvided;
_imageProvider.Stop();
_imageProvider = null;
}
if (_singleImagePipeClient != null)
{
_singleImagePipeClient.Dispose();
_singleImagePipeClient.LogMsgThrow -= OnLogMsgThrow;
_singleImagePipeClient = null;
}
if (_rawImageProviderPipeClient != null)
{
_rawImageProviderPipeClient.Dispose();
_rawImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
_rawImageProviderPipeClient = null;
}
if (_byteImageProviderPipeClient != null)
{
_byteImageProviderPipeClient.Dispose();
_byteImageProviderPipeClient.LogMsgThrow -= OnLogMsgThrow;
_byteImageProviderPipeClient = null;
}
_disposed = true;
}
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
}
}
}
}