using Aurora.Api; using Aurora.Api.Devices; using Aurora.Api.Manager; using Aurora.View; using AuroraACS; using System; using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Windows.Input; using System.Windows.Threading; namespace AuroraACS.Views { public class UCMotorMovingViewModel : NotifyPropertyChangedBase { static readonly Logger LOG = new Logger("ACSMotor"); public ICommand CMD_MoveTypeChanged { get; } public ICommand CMD_SpeedChanged { get; } public ICommand CMD_Stop_All { get; } public ICommand CMD_MoveCameraFocus { get; } public ICommand CMD_MoveLaserFocus { get; } public ICommand CMD_ResetReview { get; } public ICommand CMD_SetReview { get; } public ICommand CMD_MoveToCamera { get; } public ICommand CMD_MoveToScanner1 { get; } public ICommand CMD_MoveToUnloadPos { get; } private double posX; public double PosX { get => posX; set => SetProperty(ref posX, value); } private double posY; public double PosY { get => posY; set => SetProperty(ref posY, value); } private double posZ; public double PosZ { get => posZ; set => SetProperty(ref posZ, value); } private double posT; public double PosT { get => posT; set => SetProperty(ref posT, value); } private double posZ2; public double PosZ2 { get => posZ2; set => SetProperty(ref posZ2, value); } private double posReviewX = 0; public double PosReviewX { get => posReviewX; set => SetProperty(ref posReviewX, value); } private double posReviewY = 0; public double PosReviewY { get => posReviewY; set => SetProperty(ref posReviewY, value); } private int encX = 0; public int EncX { get => encX; set => SetProperty(ref encX, value); } private int encY = 0; public int EncY { get => encY; set => SetProperty(ref encY, value); } private int maskingInfo = 0; public int MaskingInfo { get => maskingInfo; set => SetProperty(ref maskingInfo, value); } public bool ACSIsActivated { get => ACS.IsActivated; } private DispatcherTimer MonitorTimer; private ACSMotor MotorX, MotorY, MotorZ, MotorT; private ACSDevice ACS; private string MoveType = "ABS"; private string Speed = "M"; public UCMotorMovingViewModel(IDeviceManager dManager, IEventObservable eventObservable, string acsId, List motorIDs) { ACS = dManager[acsId] as ACSDevice; if (motorIDs.FirstOrDefault(x => x.Contains("X")) != null) { MotorX = dManager[motorIDs.FirstOrDefault(x => x.Contains("X"))] as ACSMotor; } if (motorIDs.FirstOrDefault(x => x.Contains("Y")) != null) { MotorY = dManager[motorIDs.FirstOrDefault(x => x.Contains("Y"))] as ACSMotor; } if (motorIDs.FirstOrDefault(x => x.Contains("T")) != null) { MotorT = dManager[motorIDs.FirstOrDefault(x => x.Contains("T"))] as ACSMotor; } if (motorIDs.FirstOrDefault(x => x.Contains("Z")) != null) { MotorZ = dManager[motorIDs.FirstOrDefault(x => x.Contains("Z"))] as ACSMotor; } CMD_MoveTypeChanged = new DelegateCommand(OnMoveTypeChanged); CMD_SpeedChanged = new DelegateCommand(OnSpeedChanged); CMD_Stop_All = new DelegateCommand(StopAll); CMD_ResetReview = new DelegateCommand(ResetReviewPos); CMD_SetReview = new DelegateCommand(SetReviewPos); MonitorTimer = new DispatcherTimer(); MonitorTimer.Tick += OnMonitoring; MonitorTimer.Interval = TimeSpan.FromMilliseconds(100); StartMonitoring(); Observable.Merge( eventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_ACTIVATED), eventObservable.AsObservable(AuroraSysEvent.SYS_DEVICE_DEACTIVATED) ).Where( x => x.Param as string == ACS?.DeviceId ).ObserveOnDispatcher().Subscribe( x => OnACSActiveChanged() ); } private void OnACSActiveChanged() { NotifyPropertyChanged("ACSIsActivated"); } private void OnMonitoring(object sender, EventArgs e) { if (MotorX?.IsActivated ?? false) PosX = MotorX.GetActualPos(); if (MotorY?.IsActivated?? false) PosY = MotorY.GetActualPos(); if (MotorT?.IsActivated?? false) PosT = MotorT.GetActualPos(); if (MotorZ?.IsActivated?? false) PosZ2 = MotorZ.GetActualPos(); } public void StopMonitoring() { MonitorTimer.Stop(); } public void StartMonitoring() { MonitorTimer.Start(); } private void SetReviewPos(object obj) { PosReviewX = PosX; PosReviewY = PosY; } private void ResetReviewPos(object obj) { PosReviewX = 0; PosReviewY = 0; } internal void MoveZNegativeUp() { if (MoveType == "JOG") { MotorZ?.Stop(); } } internal void MoveZPositiveUp() { if (MoveType == "JOG") { MotorZ?.Stop(); } } internal void MoveYNegativeUp() { if (MoveType == "JOG") { MotorY?.Stop(); } } internal void MoveYPositiveUp() { if (MoveType == "JOG") { MotorY?.Stop(); } } internal void MoveXNegativeUp() { if (MoveType == "JOG") { MotorX?.Stop(); } } internal void MoveXPositiveUp() { if (MoveType == "JOG") { MotorX?.Stop(); } } internal void MoveTPositiveUp() { if (MoveType == "JOG") { MotorT?.Stop(); } } internal void MoveTNegativeUp() { if (MoveType == "JOG") { MotorT?.Stop(); } } internal void MoveTNegativeDown(double targetPos) { if (MotorT == null) return; if (MoveType == "ABS") { _ = MotorT.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorT.Move(); } else if (MoveType == "INC") { MotorT.SetTargetPos(targetPos * (-1), TargetPosType.REL); MotorT.Move(); } else if (MoveType == "JOG") { MotorT.SetTargetPos(targetPos); MotorT.Jog(-1); } } internal void MoveTPositiveDown(double targetPos) { if (MotorT == null) return; if (MoveType == "ABS") { _ = MotorT.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorT.Move(); } else if (MoveType == "INC") { MotorT.SetTargetPos(targetPos, TargetPosType.REL); MotorT.Move(); } else if (MoveType == "JOG") { MotorT.SetTargetPos(targetPos); MotorT.Jog(1); } } internal void MoveZNegativeDown(double targetPos) { if (MotorZ == null) return; if (MoveType == "ABS") { _ = MotorZ.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorZ.Move(); } else if (MoveType == "INC") { MotorZ.SetTargetPos(targetPos * (-1), TargetPosType.REL); MotorZ.Move(); } else if (MoveType == "JOG") { MotorZ.SetTargetPos(targetPos); MotorZ.Jog(-1); } } internal void MoveZPositiveDown(double targetPos) { if (MotorZ == null) return; if (MoveType == "ABS") { _ = MotorZ.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorZ.Move(); } else if (MoveType == "INC") { MotorZ.SetTargetPos(targetPos, TargetPosType.REL); MotorZ.Move(); } else if (MoveType == "JOG") { MotorZ.SetTargetPos(targetPos); MotorZ.Jog(1); } } internal void MoveYNegativeDown(double targetPos) { if (MotorY == null) return; if (MoveType == "ABS") { _ = MotorY.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorY.Move(); } else if (MoveType == "INC") { MotorY.SetTargetPos(targetPos * (-1), TargetPosType.REL); MotorY.Move(); } else if (MoveType == "JOG") { MotorY.SetTargetPos(targetPos); MotorY.Jog(-1); } } internal void MoveYPositiveDown(double targetPos) { if (MotorY == null) return; if (MoveType == "ABS") { _ = MotorY.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorY.Move(); } else if (MoveType == "INC") { MotorY.SetTargetPos(targetPos, TargetPosType.REL); MotorY.Move(); } else if (MoveType == "JOG") { MotorY.SetTargetPos(targetPos); MotorY.Jog(1); } } internal void MoveXNegativeDown(double targetPos) { if (MotorX == null) return; if (MoveType == "ABS") { _ = MotorX.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorX.Move(); } else if (MoveType == "INC") { MotorX.SetTargetPos(targetPos * (-1), TargetPosType.REL); MotorX.Move(); } else if (MoveType == "JOG") { MotorX.SetTargetPos(targetPos); MotorX.Jog(-1); } } internal void MoveXPositiveDown(double targetPos) { if (MotorX == null) return; if (MoveType == "ABS") { _ = MotorX.SetTargetPos(targetPos, TargetPosType.ABS); _ = MotorX.Move(); } else if (MoveType == "INC") { MotorX.SetTargetPos(targetPos, TargetPosType.REL); MotorX.Move(); } else if (MoveType == "JOG") { MotorX.SetTargetPos(targetPos); MotorX.Jog(1); } } private void StopAll(object obj) { if (ACS.IsActivated) ACS.SetHaltAll(); } private void OnSpeedChanged(object obj) { Speed = (string)obj; } private void OnMoveTypeChanged(object obj) { MoveType = (string)obj; } } }