| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 |
- using GSG.NET.Extensions;
- 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.Input;
- using System.Windows.Media;
- 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 = 5;
- public double JogVelocity
- {
- get { return this._jogVelocity; }
- set { this.SetProperty(ref this._jogVelocity, value); }
- }
- #endregion
- public event Action<IDialogResult> RequestClose;
- 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 SteeringCWCommand { get; set; }
- public ICommand SteeringCCWCommand { get; set; }
- public ICommand SelectedDirection { get; set; }
- public ICommand JogVelPopupCommand { get; set; }
- public ICommand JogCommand { get; set; }
- #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); }
- }
- #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;
- public DriveServoViewModel( IEventAggregator _ea , SqliteManager _sql , MessageController _messageController )
- {
- 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.RouteList = new ObservableCollection<Route>( sql.RouteDal.All );
- this.messageController = _messageController;
- 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.SteeringCWCommand = new DelegateCommand<object>( ExecuteSteeringCWCommand );
- this.SteeringCCWCommand = new DelegateCommand<object>( ExecuteSteeringCCWCommand );
- this.JogVelPopupCommand = new DelegateCommand( ExecuteJogVelPopupCommand );
- this.JogCommand = new DelegateCommand<object>( ExecuteJogCommand );
- }
-
- 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 "CurrentTag":
- break;
- case "CurrentSpeed":
- break;
- case "CurrentTorque":
- break;
- default:
- break;
- }
- }
- }
- }
- 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.NONE:
- break;
- 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<DriveControlEventArgs.eMoveDir>().Value;
- // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
- //}
- break;
- case DriveControlEventArgs.eControlKind.SteeringState:
- //if ( args.Result.IsSuccess )
- //{
- // var dir = args.Result.ToResult<DriveControlEventArgs.eMoveDir>().Value;
- // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
- //}
- 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 Successs";
- }
- 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 ExecuteSteeringCCWCommand(object obj)
- {
- var msg = new DriveControlEventArgs
- {
- EventDir = DriveControlEventArgs.eEventDir.ToBack,
- ControlKind = DriveControlEventArgs.eControlKind.Steering,
- };
- if (obj.ToString().Equals("CCW"))
- msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
- else
- msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
- this.PublishEvent(msg);
- }
- private void ExecuteSteeringCWCommand(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 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 );
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- 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 } );
- }
- 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
- }
- }
|