| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670 |
- using OHV.Common.Events;
- using OHV.Common.Model;
- using OHV.Common.Shareds;
- using OHV.SqliteDAL;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Input;
- using System.Windows.Media;
- using static OHV.Common.Events.AxisControlEventArgs;
- namespace OHV.Module.Interactivity.PopUp
- {
- public class LockServoViewModel : BindableBase, IDialogAware
- {
- private DelegateCommand<string> _closeDialogCommand;
- public DelegateCommand<string> CloseDialogCommand =>
- _closeDialogCommand ?? ( _closeDialogCommand = new DelegateCommand<string>( CloseDialog ) );
- private string _title = "LockServoView";
- public string Title
- {
- get { return this._title; }
- set
- {
- this.SetProperty( ref this._title , value );
- }
- }
- private string _selectAxis = string.Empty;
- public string SelectAxis
- {
- get => this._selectAxis;
- set { this.SetProperty( ref this._selectAxis , value ); }
- }
- private string _selectedPosition = string.Empty;
- public string SelectedPosition
- {
- get => this._selectedPosition;
- set
- {
- this.SetProperty( ref this._selectedPosition , value );
- }
- }
- double _jogVelocity = 5;
- public double JogVelocity
- {
- get { return this._jogVelocity; }
- set { this.SetProperty( ref this._jogVelocity , value ); }
- }
- #region Binding Lock Left
- private double _targetPosLockLeft;
- public double TargetPosLockLeft
- {
- get { return this._targetPosLockLeft; }
- set { this.SetProperty( ref this._targetPosLockLeft , value ); }
- }
- private double _currentLockLeft;
- public double CurrentLockLeft
- {
- get { return this._currentLockLeft; }
- set { this.SetProperty( ref this._currentLockLeft , value ); }
- }
- private double _differenceLockLeft;
- public double DifferenceLockLeft
- {
- get { return this._differenceLockLeft; }
- set { this.SetProperty( ref this._differenceLockLeft , value ); }
- }
- Brush _leftAxisBrush = Brushes.Gray;
- public Brush LeftAxisBrush
- {
- get { return this._leftAxisBrush; }
- set { SetProperty( ref this._leftAxisBrush , value ); }
- }
- private Brush posLockBrush = Brushes.Gray;
- public Brush PosLockBrush
- {
- get { return posLockBrush; }
- set { SetProperty( ref this.posLockBrush , value ); }
- }
- private bool isLeftServoOn;
- public bool IsLeftServoOn
- {
- get { return isLeftServoOn; }
- set { SetProperty( ref isLeftServoOn , value ); }
- }
- private bool isLeftServoOriginReturn;
- public bool IsLeftServoOriginReturn
- {
- get { return isLeftServoOriginReturn; }
- set { SetProperty( ref isLeftServoOriginReturn , value ); }
- }
- private bool isLeftServoFalut;
- public bool IsLeftServoFault
- {
- get { return isLeftServoFalut; }
- set { SetProperty( ref isLeftServoFalut , value ); }
- }
- #endregion
- #region Binding Lock Right
- private double _targetPosLockRight;
- public double TargetPosLockRight
- {
- get { return this._targetPosLockRight; }
- set { this.SetProperty( ref this._targetPosLockRight , value ); }
- }
- private double _currentLockRight;
- public double CurrentLockRight
- {
- get { return this._currentLockRight; }
- set { this.SetProperty( ref this._currentLockRight , value ); }
- }
- private double _differenceLockRight;
- public double DifferenceLockRight
- {
- get { return this._differenceLockRight; }
- set { this.SetProperty( ref this._differenceLockRight , value ); }
- }
- Brush _rightAxisBrush = Brushes.Gray;
- public Brush RightAxisBrush
- {
- get { return this._rightAxisBrush; }
- set { SetProperty( ref this._rightAxisBrush , value ); }
- }
- private Brush posUnlockBursh = Brushes.Gray;
- public Brush PosUnlockBrush
- {
- get { return posUnlockBursh; }
- set { SetProperty( ref this.posUnlockBursh , value ); }
- }
- private bool isRightServoOn;
- public bool IsRightServoOn
- {
- get { return isRightServoOn; }
- set { SetProperty( ref isRightServoOn , value ); }
- }
- private bool isRightServoOriginReturn;
- public bool IsRightServoOriginReturn
- {
- get { return isRightServoOriginReturn; }
- set { SetProperty( ref isRightServoOriginReturn , value ); }
- }
- private bool isRightServoFalut;
- public bool IsRightServoFault
- {
- get { return isRightServoFalut; }
- set { SetProperty( ref isRightServoFalut , value ); }
- }
- #endregion
- #region ICommand
- public ICommand SelectAxisCommand { get; set; }
- public ICommand KeyInTargetPosCommand { get; set; }
- public ICommand SelectPosCommand { get; set; }
- public ICommand MoveToCommand { get; set; }
- public ICommand CurrentToTargetCommand { get; set; }
- public ICommand ServoOnCommand { get; set; }
- public ICommand ServoOffCommand { get; set; }
- public ICommand FaultResetCommand { get; set; }
- public ICommand OriginCommand { get; set; }
- public ICommand JogCommand { get; set; }
- public ICommand SelectedPosDataSave { get; set; }
- public ICommand JogVelPopupCommand { get; set; }
- public ICommand JogStopCommand { get; set; }
- public ICommand AllOriginCommand { get; set; }
- #endregion
- public event Action<IDialogResult> RequestClose;
- IEventAggregator eventAggregator;
- MessageController messageController;
- SqliteManager sql;
- public LockServoViewModel( IEventAggregator ea , MessageController _messageController , SqliteManager _sql )
- {
- this.SelectAxisCommand = new DelegateCommand<object>( ExecuteSelectAxisCommand );
- this.SelectPosCommand = new DelegateCommand<object>( ExecuteSelectPosCommand );
- this.MoveToCommand = new DelegateCommand( ExecuteMoveToCommand );
- this.CurrentToTargetCommand = new DelegateCommand( ExecuteCurrentToTargetCommand );
- this.KeyInTargetPosCommand = new DelegateCommand<object>( ExecuteKeyInCommand );
- this.ServoOnCommand = new DelegateCommand<object>( ExecuteServoOnCommand );
- this.ServoOffCommand = new DelegateCommand<object>( ExecuteServoOffCommand );
- this.FaultResetCommand = new DelegateCommand<object>( ExecuteFaultResetCommand );
- this.OriginCommand = new DelegateCommand<object>( ExecuteOriginCommand );
- this.AllOriginCommand = new DelegateCommand( ExecuteAllOriginCommand );
- this.SelectedPosDataSave = new DelegateCommand<object>( ExecuteSelectedPosDataSave );
- this.JogCommand = new DelegateCommand<object>( ExecuteJogCommand );
- this.JogVelPopupCommand = new DelegateCommand( ExecuteJogVelPopup );
- this.JogStopCommand = new DelegateCommand( ExecuteJogStop );
- this.eventAggregator = ea;
- this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Unsubscribe( AxisUICallbackCommunication );
- this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Subscribe( AxisUICallbackCommunication , ThreadOption.UIThread );
- this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Unsubscribe( UICallbackCommunication );
- this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallbackCommunication , ThreadOption.UIThread );
- this.messageController = _messageController;
- this.sql = _sql;
- //초기 선택 축
- }
- private void ExecuteAllOriginCommand( )
- {
- var msg = new AxisControlEventArgs
- {
- Dir = eEventDir.ToBack ,
- Kind = eAxisControlKind.AllOriginReturn ,
- };
- this.PublishEvent( msg );
- }
- public void Init()
- {
- this.SelectAxis = ConstString.AXIS_CARRIER_LOCK_LEFT;
- this.LeftAxisBrush = Brushes.Orange;
- List<AxisPositionData> positionList = null;
- positionList = sql.AxisPositionDataDAL.Get( x => x.Name.Equals( ConstString.TEACH_POSITION_LOCK ) ).ToList();
- this.TargetPosLockLeft = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) ).Single().Value;
- //초기 Pos 선택
- positionList = sql.AxisPositionDataDAL.Get( x => x.Name.Equals( ConstString.TEACH_POSITION_LOCK ) ).ToList();
- this.TargetPosLockRight = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_RIGHT ) ).Single().Value;
- this.SelectedPosition = ConstString.TEACH_POSITION_LOCK;
- this.PosLockBrush = Brushes.Orange;
- }
- public void ExecuteJogStop( )
- {
- var msg = new AxisControlEventArgs
- {
- Dir = eEventDir.ToBack ,
- Kind = eAxisControlKind.Stop,
- AxisName = this.SelectAxis ,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteJogVelPopup( )
- {
- var numPad = new CalcuratorView();
- var result = numPad.ShowDialog( this.JogVelocity );
- this.JogVelocity = result;
- }
- private void UICallbackCommunication( GUIMessageEventArgs obj )
- {
- if ( obj.Kind == GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange )
- {
- if ( obj.MessageKey.Equals( MessageKey.Vehicle ) )
- {
- }
- }
- }
- private void ExecuteSelectedPosDataSave( object obj )
- {
- List<AxisPositionData> dataList = null;
- this.messageController.ShowConfirmationPopupView( "Position Data Save ?" , r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- switch ( this.SelectedPosition )
- {
- case ConstString.TEACH_POSITION_LOCK:
- dataList = sql.AxisPositionDataDAL.Get( x => x.Name.Equals( ConstString.TEACH_POSITION_LOCK ) ).ToList();
- break;
- case ConstString.TEACH_POSITION_UNLOCK:
- dataList = sql.AxisPositionDataDAL.Get( x => x.Name.Equals( ConstString.TEACH_POSITION_UNLOCK ) ).ToList();
- break;
- }
- var sqlAxisLeft = dataList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) ).Single();
- sqlAxisLeft.Value = this.TargetPosLockLeft;
- sql.AxisPositionDataDAL.Update( sqlAxisLeft );
- var sqlAxisRight = dataList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_RIGHT ) ).Single();
- sqlAxisRight.Value = this.TargetPosLockRight;
- sql.AxisPositionDataDAL.Update( sqlAxisRight );
- }
- } );
- }
- public void ExecuteJogCommand( object obj )
- {
- if ( this.JogVelocity <= 0 )
- {
- this.messageController.ShowNotificationView( "Check Jog Velocity" );
- return;
- }
- var msg = new AxisControlEventArgs
- {
- Dir = eEventDir.ToBack,
- Kind = eAxisControlKind.Jog,
- AxisName = this.SelectAxis,
- JogVelocity = Convert.ToInt32( this.JogVelocity ),
- };
- if ( obj.ToString().Equals( "+" ) )
- msg.JogDir = eJogMoveDir.Positive;
- else
- msg.JogDir = eJogMoveDir.Negative;
- this.PublishEvent( msg );
- }
- void PublishEvent( AxisControlEventArgs args )
- {
- args.Dir = eEventDir.ToBack;
- this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Publish( args );
- }
- void AxisUICallbackCommunication( AxisControlEventArgs args )
- {
- if ( args.Dir == eEventDir.ToFront )
- {
- switch ( args.Kind )
- {
- case eAxisControlKind.Stop:
- break;
- case eAxisControlKind.EStop:
- break;
- case eAxisControlKind.Move:
- RspMove( args );
- break;
- case eAxisControlKind.Jog:
- break;
- case eAxisControlKind.ServoOn:
- break;
- case eAxisControlKind.ServoOff:
- break;
- case eAxisControlKind.NONE:
- break;
- case eAxisControlKind.OriginReturn:
- break;
- case eAxisControlKind.ReqCurrentPosition:
- break;
- case eAxisControlKind.ReqStopCurrentPosition:
- break;
- case eAxisControlKind.FaultReset:
- {
- var msg = string.Empty;
- if ( args.Result.IsSuccess )
- msg = $" {this.SelectAxis} Reset Success ";
- else
- msg = $" {this.SelectAxis} Reset Failed ";
- this.messageController.ShowNotificationView( msg );
- }
- break;
- case eAxisControlKind.AxisState:
- AxisState( args );
- break;
- case eAxisControlKind.AllOriginReturn:
- {
- this.RspOriginState( args );
- }
- break;
- case eAxisControlKind.SyncMove:
- break;
- case eAxisControlKind.SyncState:
- break;
- default:
- break;
- }
- }
- }
- void RspOriginState( AxisControlEventArgs args )
- {
- var msg = string.Empty;
- if ( args.Result.IsSuccess )
- msg = "All Origin Success";
- else
- msg = "Origin Fail";
- //if ( IsLeftServoOriginReturn && IsRightServoOriginReturn )
- // msg = "All Origin Success";
- //else
- // msg = "Origin Fail";
- this.messageController.ShowNotificationView( msg );
- }
- private void AxisState( AxisControlEventArgs args )
- {
- var left = args.Args[ "Left" ] as AxisState;
- var right = args.Args[ "Right" ] as AxisState;
- this.IsLeftServoOn = left.IsServoOn;
- this.IsLeftServoOriginReturn = left.IsOriginReturn;
- this.IsLeftServoFault = left.IsAmpFault;
- this.CurrentLockLeft = left.CurrentPosition;
- this.IsRightServoOn = right.IsServoOn;
- this.IsRightServoOriginReturn = right.IsOriginReturn;
- this.IsRightServoFault = right.IsAmpFault;
- this.CurrentLockRight = right.CurrentPosition;
- }
- private void RspMove( AxisControlEventArgs args )
- {
- string msg = string.Empty;
- if ( args.Result.IsSuccess )
- {
- if ( args.PosDir == ePosDir.Lock )
- msg = "Clamp Lock Success";
- else
- msg = "Clamp Unlock Success";
- }
- else
- {
- var error = args.Result.Errors.FirstOrDefault();
- var alarm = error.Metadata[ "Alarm" ] as Alarm;
- msg = alarm.Name + " " + alarm.Text;
- }
- this.messageController.ShowNotificationView( msg );
- }
- private void ExecuteOriginCommand( object obj )
- {
- var msg = new AxisControlEventArgs
- {
- Dir = eEventDir.ToBack ,
- Kind = eAxisControlKind.OriginReturn ,
- AxisName = this.SelectAxis ,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteFaultResetCommand( object obj )
- {
- var msg = new AxisControlEventArgs
- {
- Dir = eEventDir.ToBack ,
- Kind = eAxisControlKind.FaultReset ,
- AxisName = this.SelectAxis ,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteServoOffCommand( object obj )
- {
- var msg = new AxisControlEventArgs
- {
- Dir = eEventDir.ToBack ,
- Kind = eAxisControlKind.ServoOff ,
- AxisName = this.SelectAxis ,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteServoOnCommand( object obj )
- {
- var msg = new AxisControlEventArgs
- {
- Dir = eEventDir.ToBack ,
- Kind = eAxisControlKind.ServoOn ,
- AxisName = this.SelectAxis ,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteKeyInCommand( object obj )
- {
- var numPad = new CalcuratorView();
- if ( obj.ToString().Equals( "Left" ) )
- {
- var result = numPad.ShowDialog( this.TargetPosLockLeft );
- this.TargetPosLockLeft = result;
- }
- else
- {
- var result = numPad.ShowDialog( this.TargetPosLockRight );
- this.TargetPosLockRight = result;
- }
- }
- private void ExecuteCurrentToTargetCommand( )
- {
- this.messageController.ShowConfirmationPopupView( "Current Position To Target Position ?" , r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- switch ( this.SelectAxis )
- {
- case ConstString.AXIS_CARRIER_LOCK_LEFT:
- this.TargetPosLockLeft = this.CurrentLockLeft;
- break;
- case ConstString.AXIS_CARRIER_LOCK_RIGHT:
- this.TargetPosLockRight = this.CurrentLockRight;
- break;
- }
- }
- } );
- }
- private void ExecuteMoveToCommand( )
- {
- var error = string.Empty;
- if ( this.IsLeftServoFault || this.IsRightServoFault )
- error = "Is Servo Fault";
- else if ( !this.IsLeftServoOn || !this.IsRightServoOn )
- error = "Is Servo OFF";
- if ( error != string.Empty )
- this.messageController.ShowNotificationView( error );
- else
- {
- this.messageController.ShowConfirmationPopupView( $"Clamp Move To {this.SelectedPosition}" , r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- var msg = new AxisControlEventArgs();
- msg.Kind = eAxisControlKind.Move;
- msg.AxisName = this.SelectAxis;
- if ( SelectedPosition.Equals( ConstString.TEACH_POSITION_LOCK ) )
- msg.PosDir = ePosDir.Lock;
- else
- msg.PosDir = ePosDir.UnLock;
- this.PublishEvent( msg );
- }
- } );
- }
- }
- private void ExecuteSelectPosCommand( object obj )
- {
- List<AxisPositionData> positionList = null;
- this.SelectedPosition = obj.ToString();
- if ( obj.Equals( ConstString.TEACH_POSITION_LOCK ) )
- {
- this.PosLockBrush = Brushes.Orange;
- this.PosUnlockBrush = Brushes.Gray;
- }
- else
- {
- this.PosLockBrush = Brushes.Gray;
- this.PosUnlockBrush = Brushes.Orange;
- }
- switch ( this.SelectedPosition )
- {
- case ConstString.TEACH_POSITION_LOCK:
- positionList = sql.AxisPositionDataDAL.Get( x => x.Name.Equals( ConstString.TEACH_POSITION_LOCK ) ).ToList();
- break;
- case ConstString.TEACH_POSITION_UNLOCK:
- positionList = sql.AxisPositionDataDAL.Get( x => x.Name.Equals( ConstString.TEACH_POSITION_UNLOCK ) ).ToList();
- break;
- }
- var sqlAxisLeft = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) ).Single();
- this.TargetPosLockLeft = sqlAxisLeft.Value;
- var sqlAxisRight = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_RIGHT ) ).Single();
- this.TargetPosLockRight = sqlAxisRight.Value;
- }
- private void ExecuteSelectAxisCommand( object obj )
- {
- if ( obj.ToString().Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) )
- {
- this.LeftAxisBrush = Brushes.Orange;
- this.RightAxisBrush = Brushes.Gray;
- this.SelectAxis = ConstString.AXIS_CARRIER_LOCK_LEFT;
- }
- else
- {
- this.RightAxisBrush = Brushes.Orange;
- this.LeftAxisBrush = Brushes.Gray;
- this.SelectAxis = ConstString.AXIS_CARRIER_LOCK_RIGHT;
- }
- }
- #region Dialog
- public bool CanCloseDialog( )
- {
- //throw new NotImplementedException();
- return true;
- }
- public void OnDialogClosed( )
- {
- this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Publish( new AxisControlEventArgs { Dir = AxisControlEventArgs.eEventDir.ToBack , Kind = AxisControlEventArgs.eAxisControlKind.ReqStopCurrentPosition } );
- this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Unsubscribe( AxisUICallbackCommunication );
- this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Unsubscribe( UICallbackCommunication );
- }
- public async void OnDialogOpened( IDialogParameters parameters )
- {
- Action action = new Action(() => this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Publish( new AxisControlEventArgs { Dir = AxisControlEventArgs.eEventDir.ToBack, Kind = AxisControlEventArgs.eAxisControlKind.ReqCurrentPosition } ) );
- await Task.Run( action );
- }
- public virtual void RaiseRequestClose( IDialogResult dialogResult )
- {
- RequestClose?.Invoke( dialogResult );
- }
- private void CloseDialog( string parameter )
- {
- ButtonResult result = ButtonResult.None;
- if ( parameter?.ToLower() == "true" )
- result = ButtonResult.OK;
- else if ( parameter?.ToLower() == "false" )
- result = ButtonResult.Cancel;
- RaiseRequestClose( new DialogResult( result ) );
- }
- #endregion
- }
- }
|