| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 |
- using GSG.NET.Extensions;
- using GSG.NET.Utils;
- 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.Collections.ObjectModel;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using VehicleControlSystem;
- using VehicleControlSystem.ControlLayer.MQ;
- namespace OHV.Module.Interactivity.PopUp
- {
- public class DriveServoViewModel : BindableBase, IDialogAware
- {
- private DelegateCommand<string> _closeDialogCommand;
- public DelegateCommand<string> CloseDialogCommand =>
- _closeDialogCommand ?? ( _closeDialogCommand = new DelegateCommand<string>( CloseDialog ) );
- private string _title = "DriveServoView";
- public string Title
- {
- get { return this._title; }
- set
- {
- this.SetProperty( ref this._title, value );
- }
- }
- private string _selectedPosition = string.Empty;
- public string SelectedPosition
- {
- get => this._selectedPosition;
- set
- {
- this.SetProperty( ref this._selectedPosition, value );
- }
- }
- private string _selectDirection = string.Empty;
- public string SelectDirection
- {
- get => this._selectDirection;
- set { this.SetProperty( ref this._selectDirection, value ); }
- }
- #region Motor Left Binding Value
- private double _driveTargetPos;
- public double DriveTargetPos
- {
- get { return this._driveTargetPos; }
- set { this.SetProperty( ref this._driveTargetPos, value ); }
- }
- private double _currentPosition;
- /// <summary>
- /// * Drive Current Position
- /// </summary>
- public double CurrentPosition
- {
- get { return this._currentPosition; }
- set { this.SetProperty( ref this._currentPosition, value ); }
- }
- private double _differenceDrive;
- public double DifferenceDrive
- {
- get { return this._differenceDrive; }
- set { this.SetProperty( ref this._differenceDrive, value ); }
- }
- private double _jogVelocity = 3.0;
- public double JogVelocity
- {
- get { return this._jogVelocity; }
- set { this.SetProperty( ref this._jogVelocity, value ); }
- }
- #endregion
- public event Action<IDialogResult> RequestClose;
- #region Commands
- 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 IEventAggregator eventAggregator;
- public ICommand PositionAddCommand { get; set; }
- public ICommand PositionDeleteCommand { get; set; }
- public ICommand PositionSaveCommand { get; set; }
- public ICommand SteeringMoveCommand { get; set; }
- public ICommand SelectedDirection { get; set; }
- public ICommand JogVelPopupCommand { get; set; }
- public ICommand JogCommand { get; set; }
- #endregion
- #region Brushes
- private Brush steeringLeftBrush = Brushes.Gray;
- public Brush SteeringLeftBrushProperty
- {
- get { return steeringLeftBrush; }
- set { SetProperty( ref this.steeringLeftBrush, value ); }
- }
- private Brush steeringRightBrush = Brushes.Gray;
- public Brush SteeringRightBrushProperty
- {
- get { return steeringRightBrush; }
- set { SetProperty( ref this.steeringRightBrush, value ); }
- }
- Brush driveOnStateBrush = Brushes.DodgerBlue;
- public Brush DriveOnStateBrush
- {
- get { return this.driveOnStateBrush; }
- set { SetProperty(ref this.driveOnStateBrush, value); }
- }
- Brush driveOffStateBrush = Brushes.DodgerBlue;
- public Brush DriveOffStateBrush
- {
- get { return this.driveOffStateBrush; }
- set { SetProperty(ref this.driveOffStateBrush, value); }
- }
- #endregion
- private ObservableCollection<Route> _routeList;
- public ObservableCollection<Route> RouteList
- {
- get { return this._routeList; }
- set { SetProperty( ref this._routeList, value ); }
- }
- public SqliteDAL.DAL.AxisPositionDataDAL axisPositionDataDal;
- SqliteManager sql;
- MessageController messageController;
- private string autoReadyFlag = "NG";
- public string AutoReadyFlag
- {
- get { return autoReadyFlag; }
- set { this.SetProperty(ref this.autoReadyFlag, value); }
- }
- Brush autoReadyFlagColor = Brushes.Gray;
- public Brush AutoReadyFlagColor
- {
- get { return this.autoReadyFlagColor; }
- set { this.SetProperty(ref this.autoReadyFlagColor, value); }
- }
- private string currentMCR = "None";
- public string CurrentMCR
- {
- get { return currentMCR; }
- set { this.SetProperty(ref this.currentMCR, value); }
- }
- private string currentTag = "None";
- public string CurrentTag
- {
- get { return currentTag; }
- set { SetProperty(ref this.currentTag, value); }
- }
- ZmqManager zmqManager;
-
- public DriveServoViewModel( IEventAggregator _ea, SqliteManager _sql, MessageController _messageController, VCSystem system )
- {
- this.eventAggregator = _ea;
- this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Unsubscribe( DriveControlCallBack );
- this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Subscribe( DriveControlCallBack, ThreadOption.UIThread );
- this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Unsubscribe( UICallBackCommunication );
- this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallBackCommunication, ThreadOption.UIThread );
- this.sql = _sql;
- this.messageController = _messageController;
- this.zmqManager = system.ZmqManager;
- system.ZmqManager.PropertyChanged += ZmqManager_PropertyChanged;
- this.SelectPosCommand = new DelegateCommand<object>( ExecuteSelectPosCommand );
- this.MoveToCommand = new DelegateCommand( ExecuteMoveToCommand );
- this.CurrentToTargetCommand = new DelegateCommand( ExecuteCurrentToTargetCommand );
- this.KeyInTargetPosCommand = new DelegateCommand<object>( ExecuteKeyInCommadn );
- this.ServoOnCommand = new DelegateCommand( ExecuteServoOnCommand );
- this.ServoOffCommand = new DelegateCommand( ExecuteServoOffCommand );
- this.FaultResetCommand = new DelegateCommand( ExecuteFaultResetCommand );
- this.OriginCommand = new DelegateCommand( ExecuteOriginCommand );
- this.PositionAddCommand = new DelegateCommand( ExecutePositionAddCommand );
- this.PositionDeleteCommand = new DelegateCommand( ExecutePositionDeleteCommand );
- this.PositionSaveCommand = new DelegateCommand( ExecutePositionSaveCommand );
- this.SelectedDirection = new DelegateCommand<object>( ExecuteSelectedDirection );
- this.SteeringMoveCommand = new DelegateCommand<object>( ExecuteSteeringMove );
- this.JogVelPopupCommand = new DelegateCommand( ExecuteJogVelPopupCommand );
- this.JogCommand = new DelegateCommand<object>( ExecuteJogCommand );
- this.InitializeData();
- }
- private void InitializeData()
- {
- this.CurrentMCR = this.zmqManager.CurrentMCR;
- this.CurrentTag = this.zmqManager.GetCurrentPointNo().ToString();
- //TODO: Drive 상태 Update (On/Off, Fault)
- //if (zmqManager.IsDriveOn)
- // this.DriveOnStateBrush = Brushes.LimeGreen;
- //else
- // this.DriveOnStateBrush = Brushes.DodgerBlue;
- DriveAutoReadyState(this.zmqManager.IsCanStanbyLocation);
- }
- private void ZmqManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- var property = sender.GetType().GetProperty(e.PropertyName);
- var newValue = property.GetValue(sender, null);
- switch(e.PropertyName)
- {
- case "CurrentTag":
- this.CurrentTag = CastTo<int>.From<object>(newValue).ToString();
- break;
- case "CurrentMCR":
- this.CurrentMCR = CastTo<string>.From<object>(newValue);
- break;
- case "IsCanStanbyLocation":
- {
- var ll = CastTo<bool>.From<object>(newValue);
- DriveAutoReadyState(ll);
- }
- break;
- case "IsDriveOn":
- {
- var result = CastTo<bool>.From<object>(newValue);
- if (result)
- this.DriveOnStateBrush = Brushes.LimeGreen;
- else
- this.DriveOnStateBrush = Brushes.DodgerBlue;
- }
- break;
- }
- }
- private void ExecuteSteeringMove( object obj )
- {
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack ,
- ControlKind = DriveControlEventArgs.eControlKind.Steering ,
- };
- if ( obj.ToString().Equals( "CW" ) )
- msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
- else
- msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
- this.PublishEvent( msg );
- }
- private void UICallBackCommunication( GUIMessageEventArgs obj )
- {
- if ( obj.Kind == GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange )
- {
- if ( obj.MessageKey.Equals( MessageKey.Vehicle ) )
- {
- switch ( obj.ModelPropertyName )
- {
- case "SteeringState":
- {
- var dir = CastTo<eSteeringState>.From<object>( obj.Args );
- this.ChangeSteeringDirection( dir );
- }
- break;
- case "VehicleStateProperty":
- {
- var v = CastTo<eVehicleState>.From<object>( obj.Args );
- this.ChagneVehicleState( v );
- }
- break;
- case "CurrentPosition":
- {
- var v = CastTo<double>.From<object>( obj.Args );
- this.CurrentPosition = v;
- }
- break;
- case "CurrentSpeed":
- break;
- case "CurrentTorque":
- break;
- default:
- break;
- }
- }
- }
- if ( obj.Kind == GUIMessageEventArgs.eGUIMessageKind.RspRouteManager )
- {
- }
- }
- void DriveAutoReadyState(bool state)
- {
- if(state)
- {
- this.AutoReadyFlag = "OK";
- this.AutoReadyFlagColor = Brushes.LimeGreen;
- }
- else
- {
- this.AutoReadyFlag = "NG";
- this.AutoReadyFlagColor = Brushes.Gray;
- }
- }
- private void ChagneVehicleState( eVehicleState v )
- {
- switch ( v )
- {
- case eVehicleState.None:
- break;
- case eVehicleState.Idle:
- break;
- case eVehicleState.Move:
- break;
- case eVehicleState.Load:
- break;
- case eVehicleState.Unload:
- break;
- case eVehicleState.Charge:
- break;
- case eVehicleState.Abnormal:
- break;
- case eVehicleState.Blocked:
- break;
- case eVehicleState.Decelerate:
- break;
- }
- }
- private void DriveControlCallBack( DriveControlEventArgs args )
- {
- if ( args.EventDir == DriveControlEventArgs.eEventDir.ToFront )
- {
- switch ( args.ControlKind )
- {
- case DriveControlEventArgs.eControlKind.MOVE:
- ResponseMove( args );
- break;
- case DriveControlEventArgs.eControlKind.STOP:
- break;
- case DriveControlEventArgs.eControlKind.Steering:
- //if ( args.Result.IsSuccess )
- //{
- // var dir = args.Result.ToResult<eSteeringState>().Value;
- // this.ChangeSteeringDirection( dir );
- //}
- break;
- case DriveControlEventArgs.eControlKind.SteeringState:
- if ( args.Result.IsSuccess )
- {
- var dir = CastTo<eSteeringState>.From<object>( args.Args );
- this.ChangeSteeringDirection( dir );
- }
- else
- {
- this.ChangeSteeringDirection( eSteeringState.None );
- }
- break;
- case DriveControlEventArgs.eControlKind.ReqCurrentPos:
- this.CurrentPosition = args.CurrentPosition;
- break;
- case DriveControlEventArgs.eControlKind.ReqStopCurrentPos:
- break;
- case DriveControlEventArgs.eControlKind.FaultReset:
- break;
- case DriveControlEventArgs.eControlKind.DriveON:
- break;
- case DriveControlEventArgs.eControlKind.DriveOFF:
- break;
- case DriveControlEventArgs.eControlKind.JOG:
- break;
- case DriveControlEventArgs.eControlKind.VehicleState:
- ResponseVehicleState( args );
- break;
- default:
- break;
- }
- }
- }
- private void ResponseVehicleState( DriveControlEventArgs args )
- {
- var state = CastTo<VehicleInfo>.From<object>( args.Args );
- this.CurrentPosition = state.CurrentPosition;
- }
- private void ResponseMove( DriveControlEventArgs args )
- {
- var msg = string.Empty;
- if ( args.Result.IsSuccess )
- {
- msg = "Move Success";
- }
- else
- {
- var error = args.Result.Errors.FirstOrDefault();
- var alarm = error.Metadata["Alarm"] as Alarm;
- msg = alarm.Name + " " + alarm.Text;
- }
- this.messageController.ShowNotificationView( msg );
- }
- void PublishEvent( DriveControlEventArgs args )
- {
- args.EventDir = DriveControlEventArgs.eEventDir.ToBack;
- this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( args );
- }
- void ChangeSteeringDirection( eSteeringState state )
- {
- if ( state == eSteeringState.Left )
- {
- this.SteeringLeftBrushProperty = (Brush)new BrushConverter().ConvertFromString( "#FF00FFD3" );
- this.SteeringRightBrushProperty = Brushes.Gray;
- }
- else if ( state == eSteeringState.Right )
- {
- this.SteeringLeftBrushProperty = Brushes.Gray;
- this.SteeringRightBrushProperty = (Brush)new BrushConverter().ConvertFromString( "#FF00FFD3" );
- }
- else
- {
- this.SteeringLeftBrushProperty = Brushes.Gray;
- this.SteeringRightBrushProperty = Brushes.Gray;
- }
- }
- #region Execute Method
- private void ExecuteJogCommand( object obj )
- {
- if ( this.JogVelocity <= 0 )
- {
- this.messageController.ShowNotificationView( "Check Jog Velocity" );
- return;
- }
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack,
- ControlKind = DriveControlEventArgs.eControlKind.JOG,
- };
- if ( obj.ToString().Equals( "+" ) )
- msg.JogDir = DriveControlEventArgs.eJogMoveDir.Positive;
- else
- msg.JogDir = DriveControlEventArgs.eJogMoveDir.Negative;
- this.PublishEvent( msg );
- }
- private void ExecuteJogVelPopupCommand()
- {
- var numPad = new CalcuratorView();
- var result = numPad.ShowDialog( this.JogVelocity );
- this.JogVelocity = result;
- }
- private void ExecuteSelectedDirection( object obj )
- {
- this.SelectDirection = obj.ToString();
- }
- private void ExecutePositionSaveCommand()
- {
- this.messageController.ShowConfirmationPopupView( "Save To Data ?", r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- var result = this.RouteList.Any( x => x.IsSelected != false );
- if ( !result )
- {
- this.messageController.ShowNotificationView( "Pos Not Selected" );
- return;
- }
- var ll = this.RouteList.Where( x => x.IsSelected ).FirstOrDefault();
- }
- } );
- }
- private void ExecutePositionDeleteCommand()
- {
- this.messageController.ShowConfirmationPopupView( "Select To Delete ?", r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- var deleteList = new List<Route>();
- foreach ( var item in this.RouteList )
- {
- if ( item.IsSelected )
- deleteList.Add( item );
- }
- deleteList.ForEach( x => { this.RouteList.Remove( x ); } );
- }
- } );
- }
- private void ExecutePositionAddCommand()
- {
- this.messageController.ShowConfirmationPopupView( "Position Add ?", r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- this.RouteList.Add( new Route() );
- this.messageController.ShowNotificationView( "Create Success" );
- }
- } );
- }
- private void ExecuteKeyInCommadn( object obj )
- {
- var numPad = new CalcuratorView();
- var result = numPad.ShowDialog( this.DriveTargetPos );
- this.DriveTargetPos = result;
- }
- private void ExecuteOriginCommand()
- {
- this.messageController.ShowConfirmationPopupView( "Origin ?", r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- }
- } );
- }
- private void ExecuteFaultResetCommand()
- {
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack,
- ControlKind = DriveControlEventArgs.eControlKind.FaultReset,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteServoOffCommand()
- {
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack,
- ControlKind = DriveControlEventArgs.eControlKind.DriveOFF,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteServoOnCommand()
- {
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack,
- ControlKind = DriveControlEventArgs.eControlKind.DriveON,
- };
- this.PublishEvent( msg );
- }
- private void ExecuteCurrentToTargetCommand()
- {
- this.messageController.ShowConfirmationPopupView( "Current To Target ?", r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- this.DriveTargetPos = this.CurrentPosition;
- }
- } );
- }
- private void ExecuteMoveToCommand()
- {
- this.messageController.ShowConfirmationPopupView( "Move To Selected Position ?", r =>
- {
- if ( r.Result == ButtonResult.OK )
- {
- var result = this.RouteList.Any( x => x.IsSelected != false );
- if ( !result )
- {
- this.messageController.ShowNotificationView( "Pos Not Selected" );
- return;
- }
- var ll = this.RouteList.Where( x => x.IsSelected ).FirstOrDefault();
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack,
- ControlKind = DriveControlEventArgs.eControlKind.MOVE,
- TargetRouteID = ll.Id,
- };
- this.PublishEvent( msg );
- }
- } );
- }
- private void ExecuteSelectPosCommand( object obj )
- {
- this.SelectedPosition = obj.ToString();
- }
- #endregion
- #region Dialog Function
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack, ControlKind = DriveControlEventArgs.eControlKind.ReqStopCurrentPos } );
- this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Unsubscribe( DriveControlCallBack );
- this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Unsubscribe( UICallBackCommunication );
- }
- public async void OnDialogOpened( IDialogParameters parameters )
- {
- Task task = Task.Run( () =>
- {
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack,
- ControlKind = DriveControlEventArgs.eControlKind.SteeringState,
- };
- this.PublishEvent( msg );
- this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack, ControlKind = DriveControlEventArgs.eControlKind.VehicleState } );
- var vcsMsg = new VCSMessageEventArgs() { Kind = VCSMessageEventArgs.eVCSMessageKind.ReqRouteManager };
- this.eventAggregator.GetEvent<VCSMessagePubSubEvent>().Publish( vcsMsg );
- var sT = SwUtils.CurrentTimeMillis;
- this.RouteList = new ObservableCollection<Route>( sql.RouteDal.GetAll() );
- Console.WriteLine( SwUtils.Elapsed( sT ) );
- } );
- await task;
- }
- 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 ) );
- }
- public virtual void RaiseRequestClose( IDialogResult dialogResult )
- {
- RequestClose?.Invoke( dialogResult );
- }
- #endregion
- }
- }
|