using System;
using System.Reflection;
using System.Text.Json;
using Vinno.AI.AutoVTIDiagnosisSDK.Interfaces;
using Vinno.AI.AutoVTIDiagnosisSDK.Models;
using Vinno.AI.CommonSDK.Enums;
using Vinno.AI.CommonSDK.Interfaces;
using Vinno.AI.CommonSDK.Models;
using Vinno.AI.CommonSDK.Tools;
namespace Vinno.AI.AutoVTIDiagnosisSDK
{
public class AutoVTIDiagnosis : IAutoVTIDiagnosis
{
private readonly IAutoVTIDiagnosisService _autoVTIDiagnosisService;
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 AutoVTIDiagnosis()
{
_autoVTIDiagnosisService = AIManager.Instance.AIDiagnosisSystemJsonRpcClientManager?.GetService();
if (AIManager.Instance.AINotificationManager != null)
{
AIManager.Instance.AINotificationManager.NotificationReceived += OnNotificationReceived;
}
_singleImagePipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoVTIDiagnosisSingleImage);
_singleImagePipeClient.LogMsgThrow += OnLogMsgThrow;
_singleImagePipeClient.Start();
_rawImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoVTIDiagnosisRawImageProvider);
_rawImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
_rawImageProviderPipeClient.Start();
_byteImageProviderPipeClient = new PipeClient(AIDiagnosisSystemConsts.PipeForAutoVTIDiagnosisByteImageProvider);
_byteImageProviderPipeClient.LogMsgThrow += OnLogMsgThrow;
_byteImageProviderPipeClient.Start();
}
///
/// 初始化AutoVTI Diagnosis
///
/// AutoVTI Diagnosis Parameter
/// Image Provider
public void Initialize(AutoVTIDiagnosisParameter autoVTIParameter, IAIImageProvider imageProvider = null)
{
try
{
if (!_initialized)
{
_imageProvider = imageProvider;
_autoVTIDiagnosisService.Initialize(autoVTIParameter, _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)
{
_autoVTIDiagnosisService.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();
}
_autoVTIDiagnosisService.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.AutoVTIDiagnosisStartEvaluationRaised:
StartEvaluationNotification?.Invoke(this, EventArgs.Empty);
break;
case AIEnumNotificationType.AutoVTIDiagnosisFinishEvaluationRaised:
var aiAutoVTICalcResult = JsonSerializer.Deserialize(e.Params?.ToString());
FinishEvaluationNotification?.Invoke(this, aiAutoVTICalcResult);
break;
case AIEnumNotificationType.AutoVTIDiagnosisNotifyLogRaised:
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
{
_autoVTIDiagnosisService.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
{
_autoVTIDiagnosisService.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
{
_autoVTIDiagnosisService.SetCmPerPixel(cmPerPixel);
}
catch (Exception ex)
{
new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}");
}
}
///
/// 设置检测模式
///
///
public void SetDetectMode(AIEnumDetectMode detectMode)
{
try
{
_autoVTIDiagnosisService.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 AIAutoVTICalcResult DetectOneImage(AIRawImage rawImage, float cmPerPixel)
{
if (rawImage == null)
{
throw new ArgumentNullException(nameof(rawImage));
}
try
{
_singleImagePipeClient.SendBytes(rawImage.DataBuffer);
return _autoVTIDiagnosisService.DetectOneRawImage(rawImage.Height, rawImage.Width, rawImage.ColorType, cmPerPixel);
}
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 AIAutoVTICalcResult DetectOneImage(byte[] byteImage, float cmPerPixel)
{
if (byteImage == null)
{
throw new ArgumentNullException(nameof(byteImage));
}
try
{
_singleImagePipeClient.SendBytes(byteImage);
return _autoVTIDiagnosisService.DetectOneByteImage(cmPerPixel);
}
catch (Exception ex)
{
AIManager.Instance.AILogManager?.WriteLogInfo(new AILogEventArgs(AIEnumLogType.ErrorLog, $"错误方法名:{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}, 错误信息:{ex}"));
return null;
}
}
private void OnByteImageProvided(object sender, byte[] byteImage)
{
try
{
if (byteImage == null)
{
return;
}
_byteImageProviderPipeClient?.SendBytes(byteImage);
_autoVTIDiagnosisService.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);
_autoVTIDiagnosisService.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 AutoVTI Diagnosis
///
public void Close()
{
try
{
if (_disposed)
{
return;
}
_initialized = false;
if (_imageProvider != null)
{
_imageProvider.ByteImageProvided -= OnByteImageProvided;
_imageProvider.RawImageProvided -= OnRawImageProvided;
_imageProvider.Stop();
_imageProvider = null;
}
_autoVTIDiagnosisService.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}"));
}
}
}
}