using Newtonsoft.Json;
using OpenCvSharp;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading;
namespace PLCSharp.VVMs.Vision.Camera
{
///
/// 相机Base
///
public partial class CameraBase : BindableBase
{
///
/// 唯一标识
///
[Key]
public Guid ID { get; set; } = Guid.NewGuid();
private string _Name;
///
/// 相机配置
///
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Name, value);
}
}
private CameraBrand _Brand;
///
/// 相机配置
///
public CameraBrand Brand
{
get { return _Brand; }
set
{
SetProperty(ref _Brand, value);
Prompt = "已修改,请保存";
}
}
private string _Comment;
///
/// 备注
///
public string Comment
{
get { return _Comment; }
set
{
if (_Comment != value)
{
Prompt = "已修改,请保存";
}
SetProperty(ref _Comment, value);
}
}
///
/// 序列化Params
///
[Column("Params")]
public string SerializedParams
{
get => JsonConvert.SerializeObject(Params); // 自动序列化
set => Params = JsonConvert.DeserializeObject(value); // 自动反序列化
}
//-------------------------------------------------------NotMapped--------------------------------------------
private bool _LogSwitch;
///
/// 日志Switch
///
[JsonIgnore]
[NotMapped]
public bool LogSwitch
{
get { return _LogSwitch; }
set { SetProperty(ref _LogSwitch, value); }
}
///
/// 日志Queue
///
[JsonIgnore]
[NotMapped]
public ConcurrentQueue LogQueue { get; set; } = [];
///
/// 日志
///
/// 日志
public void Log(string log)
{
if (LogSwitch)
{ LogQueue.Enqueue(log); }
}
private string _Prompt;
///
/// 提示
///
[JsonIgnore]
[NotMapped]
public string Prompt
{
get { return _Prompt; }
set { SetProperty(ref _Prompt, value); }
}
private bool _Connected;
///
/// Connected
///
[JsonIgnore]
[NotMapped]
public bool Connected
{
get { return _Connected; }
set { SetProperty(ref _Connected, value); }
}
private CameraParams _Params;
///
/// 参数集合
///
[JsonIgnore]
[NotMapped]
public CameraParams Params
{
get
{
_Params ??= new CameraParams();
return _Params;
}
set
{
SetProperty(ref _Params, value);
}
}
//-------------------------------------------------------Method--------------------------------------------
private DelegateCommand _Command;
///
/// Command
///
public DelegateCommand Command =>
_Command ??= new DelegateCommand(ExecuteOpenCommand);
void ExecuteOpenCommand(string cmd)
{
switch (cmd)
{
case "Open":
Open();
break;
case "Trig":
SoftwareTrig();
break;
case "Continuous":
Continuous();
break;
case "StopContinuous":
StopContinuous();
break;
}
}
///
/// 打开
///
/// 返回布尔值
public virtual bool Open() { return false; }
///
/// 设置ExposureTime
///
/// 返回布尔值
public virtual bool SetExposureTime() { return false; }
///
/// 设置TriggerSource
///
/// 返回布尔值
public virtual bool SetTriggerSource() { return false; }
///
/// SoftwareTrig
///
public virtual void SoftwareTrig() { return; }
///
/// Trig
///
public virtual void Trig() { return; }
///
/// Continuous
///
public virtual void Continuous() { return; }
///
/// 停止Continuous
///
public virtual void StopContinuous() { return; }
///
/// 关闭
///
public virtual void Close() { }
///
/// _imageReadyEvent
///
protected readonly AutoResetEvent _imageReadyEvent = new(false);
///
/// WaitOne
///
/// 超时时间
/// 返回布尔值
public virtual bool WaitOne(int timeout)
{
return false;
}
///
/// 图像矩阵
///
[NotMapped]
public Mat Mat { get; set; }
///
/// 相机Params
///
public class CameraParams : BindableBase
{
private double _MinExposureTime;
///
/// 最小ExposureTime
///
public double MinExposureTime
{
get { return _MinExposureTime; }
set { SetProperty(ref _MinExposureTime, value); }
}
private double _MaxExposureTime;
///
/// 最大ExposureTime
///
public double MaxExposureTime
{
get { return _MaxExposureTime; }
set { SetProperty(ref _MaxExposureTime, value); }
}
private double _ExposureTime;
///
/// 相机配置
///
public double ExposureTime
{
get { return _ExposureTime; }
set
{
SetProperty(ref _ExposureTime, value);
}
}
private TriggerMethod _TriggerSource = TriggerMethod.Software;
///
/// 相机配置
///
public TriggerMethod TriggerSource
{
get { return _TriggerSource; }
set
{
SetProperty(ref _TriggerSource, value);
}
}
}
}
}