233 lines
8.7 KiB
C#
233 lines
8.7 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using Aurora;
|
|
using Aurora.Api;
|
|
using Aurora.View;
|
|
using VisionCommon;
|
|
using VisionCommonUI;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System.IO;
|
|
using Aurora.Api.DataType;
|
|
using System.Reactive.Linq;
|
|
using System.Collections.Generic;
|
|
using MilCommon;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
|
|
namespace MilUI {
|
|
public class CameraViewModel : INotifyPropertyChanged, IDisposable {
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
private Logger _Logger;
|
|
private CameraAgentBase _CameraAgent;
|
|
private CamConfig _CamConfig;
|
|
private eDisplayType _DisplayType;
|
|
private CameraDisplay _MilCameraDisplay;
|
|
private FovInfo _FovInfo;
|
|
private FovRecipe _FovRecipe;
|
|
private IDisposable _EventDisposable;
|
|
|
|
private MIL_ID _GrabImage = MIL.M_NULL;
|
|
|
|
#region Binding Properties
|
|
private string mDisplayName;
|
|
public string DisplayName {
|
|
get {
|
|
return mDisplayName;
|
|
}
|
|
set {
|
|
mDisplayName = value;
|
|
OnPropertyChanged("DisplayName");
|
|
}
|
|
}
|
|
|
|
private bool _isCollapsed = false;
|
|
public bool IsCollapsed {
|
|
get {
|
|
return _isCollapsed;
|
|
}
|
|
set {
|
|
_isCollapsed = value;
|
|
OnPropertyChanged("IsCollapsed");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region ButtonCommand
|
|
public ICommand ButtonCommandExpand { get; set; }
|
|
public ICommand ButtonCommandCollapse { get; set; }
|
|
public ICommand ButtonCommandSetCamDevice { get; set; }
|
|
public ICommand ButtonCommandSetLight { get; set; }
|
|
public ICommand ButtonCommandGrab { get; set; }
|
|
public ICommand ButtonCommandStartLive { get; set; }
|
|
public ICommand ButtonCommandStopLive { get; set; }
|
|
public ICommand ButtonCommandFindMark { get; set; }
|
|
public ICommand ButtonCommandSwitchCamView { get; set; }
|
|
#endregion
|
|
|
|
public CameraViewModel(FovInfo fovInfo, eDisplayType displayType, CameraDisplay milCameraDisplay) {
|
|
_DisplayType = displayType;
|
|
|
|
Init(fovInfo, milCameraDisplay);
|
|
SetDisplayName(fovInfo);
|
|
SetObservable();
|
|
SetButtonCommands();
|
|
}
|
|
|
|
public void Init(FovInfo fovInfo, CameraDisplay milCameraDisplay) {
|
|
_MilCameraDisplay = milCameraDisplay;
|
|
_FovInfo = VisionRecipeManager.GetFovInfo(fovInfo);
|
|
_FovRecipe = VisionRecipeManager.GetFovRecipe(fovInfo);
|
|
_Logger = new Logger(string.Format("CameraViewModel[{0}]", _FovRecipe.FovName));
|
|
_CameraAgent = CameraAgentManager.GetCameraAgent(fovInfo);
|
|
eCamId camId = _FovRecipe.CamRecipe.CamId;
|
|
_CamConfig = VisionEnv.AuroraEnv.DeviceManager.GetDeviceConfig(camId.ToString()) as CamConfig;
|
|
|
|
InitGrabImageBuffer();
|
|
}
|
|
|
|
public void SetDisplayName(FovInfo fovInfo) {
|
|
var fovRecipe = VisionRecipeManager.GetFovRecipe(fovInfo);
|
|
DisplayName = VisionRecipeManager.GetDisplayName(fovInfo.SubSysName, fovRecipe.FovName);
|
|
}
|
|
|
|
private void SetObservable() {
|
|
if (_EventDisposable == null) {
|
|
// 이미지 회전이 발생한 경우
|
|
_EventDisposable = Observable.Merge(
|
|
VisionEnv.AuroraEnv.EventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_ACTIVATED),
|
|
VisionEnv.AuroraEnv.EventObservable.AsObservable(VisionEventId.UPDATE_GRAB_IMAGE_BUFFER))
|
|
.Where(x => x.From == _FovRecipe?.CamRecipe.CamId.ToString())
|
|
.Subscribe(_ => InitGrabImageBuffer());
|
|
}
|
|
}
|
|
|
|
// 사용하는 곳에서 그랩 버퍼 할당, 디바이스에서는 복사본 전달
|
|
private void InitGrabImageBuffer() {
|
|
try {
|
|
MilEnvironment.FreeImage(_GrabImage);
|
|
_GrabImage = (MIL_ID)_CameraAgent.CloneImageBuffer();
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
private void SetButtonCommands() {
|
|
ButtonCommandExpand = new DelegateCommand(new Action<object>(DoExpand));
|
|
ButtonCommandCollapse = new DelegateCommand(new Action<object>(DoCollapse));
|
|
ButtonCommandSetCamDevice = new DelegateCommand(new Action<object>(DoSetCamDevice));
|
|
ButtonCommandSetLight = new DelegateCommand(new Action<object>(DoSetLight));
|
|
ButtonCommandGrab = new DelegateCommand(new Action<object>(DoGrab));
|
|
ButtonCommandStartLive = new DelegateCommand(new Action<object>(DoStartLive));
|
|
ButtonCommandStopLive = new DelegateCommand(new Action<object>(DoStopLive));
|
|
ButtonCommandFindMark = new DelegateCommand(new Action<object>(DoFindMark));
|
|
ButtonCommandSwitchCamView = new DelegateCommand(new Action<object>(DoSwitchCamView));
|
|
}
|
|
|
|
public void DoExpand(object o) {
|
|
IsCollapsed = false;
|
|
}
|
|
|
|
public void DoCollapse(object o) {
|
|
IsCollapsed = true;
|
|
}
|
|
|
|
public void DoSetCamDevice(object o) {
|
|
var param = new Tuple<FovInfo, ICameraDisplay>(_FovInfo, _MilCameraDisplay);
|
|
VisionEnv.AuroraEnv.EventPublisher.Publish(
|
|
new EventMessage(string.Empty, VisionEventId.SHOW_CAM_DEVICE_SETTING_WINDOW, param));
|
|
}
|
|
|
|
public void DoSetLight(object o) {
|
|
CameraLightWindowManager.ShowLightSettingWindow(_FovInfo);
|
|
}
|
|
|
|
public void DoGrab(object o) {
|
|
var image = (object)_GrabImage;
|
|
_CameraAgent.GrabSync(_MilCameraDisplay, ref image);
|
|
_GrabImage = (MIL_ID)image;
|
|
}
|
|
|
|
public async void DoStartLive(object o) {
|
|
await Task.Run(() => { _CameraAgent.StartLive(_MilCameraDisplay); });
|
|
}
|
|
|
|
public async void DoStopLive(object o) {
|
|
await Task.Run(() => { _CameraAgent.StopLive(); });
|
|
}
|
|
|
|
public void DoFindMark(object o) {
|
|
}
|
|
|
|
public void DoSwitchCamView(object o) {
|
|
FovInfo nextFovInfo = null;
|
|
SetNextVisionRecipes(ref nextFovInfo);
|
|
|
|
if (nextFovInfo == null) {
|
|
return;
|
|
}
|
|
|
|
var nextFovRecipe = VisionRecipeManager.GetFovRecipe(nextFovInfo);
|
|
if (nextFovRecipe != null) {
|
|
var fovUiPos = VisionRecipeManager.GetFovUiPos(nextFovRecipe, _DisplayType);
|
|
CameraViewManager.Instance.SetVisibilities(nextFovInfo, fovUiPos, _DisplayType);
|
|
}
|
|
}
|
|
|
|
private void SetNextVisionRecipes(ref FovInfo nextFovInfo) {
|
|
try {
|
|
var nextFovRecipe = VisionRecipeManager.GetFovRecipe(_FovInfo);
|
|
var fovUiPos = VisionRecipeManager.GetFovUiPos(nextFovRecipe, _DisplayType);
|
|
var sameLayoutFovInfos = CameraViewManager.Instance.GetSameUiPosFovInfos(_DisplayType, fovUiPos);
|
|
for (int i = 0; i < sameLayoutFovInfos.Count; i++) {
|
|
if (_FovInfo.Equals(sameLayoutFovInfos[i])) {
|
|
int nextIdx = i + 1;
|
|
if (nextIdx == sameLayoutFovInfos.Count) {
|
|
nextIdx = 0;
|
|
}
|
|
|
|
nextFovInfo = sameLayoutFovInfos[nextIdx];
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.FuncE(e.Message);
|
|
}
|
|
}
|
|
|
|
public void DoFitImage(object o) {
|
|
ImageSize imageSize = MilEnvironment.GetImageSize(_MilCameraDisplay.GetImage());
|
|
_MilCameraDisplay.FitImage(imageSize);
|
|
}
|
|
|
|
public void DoShowCamWindow(object o) {
|
|
FovCameraViewWindowParam fovCameraViewWindowParam = new FovCameraViewWindowParam();
|
|
fovCameraViewWindowParam.fovInfo = _FovInfo;
|
|
fovCameraViewWindowParam.displayType = _DisplayType;
|
|
VisionEnv.AuroraEnv.EventPublisher.Publish(
|
|
new EventMessage(_FovInfo.GetKey(), VisionEventId.SHOW_FOV_CAMERA_VIEW_WINDOW, fovCameraViewWindowParam));
|
|
}
|
|
|
|
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
|
|
if (string.IsNullOrEmpty(propertyName)) {
|
|
return;
|
|
}
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
public void Dispose() {
|
|
RemoveObservable();
|
|
}
|
|
|
|
private void RemoveObservable() {
|
|
_EventDisposable?.Dispose();
|
|
_EventDisposable = null;
|
|
}
|
|
}
|
|
}
|