LockServoViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. using OHV.Common.Events;
  2. using OHV.Common.Model;
  3. using OHV.Common.Shareds;
  4. using OHV.SqliteDAL;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using static OHV.Common.Events.AxisControlEventArgs;
  15. namespace OHV.Module.Interactivity.PopUp
  16. {
  17. public class LockServoViewModel : BindableBase, IDialogAware
  18. {
  19. private DelegateCommand<string> _closeDialogCommand;
  20. public DelegateCommand<string> CloseDialogCommand =>
  21. _closeDialogCommand ?? ( _closeDialogCommand = new DelegateCommand<string>( CloseDialog ) );
  22. private string _title = "LockServoView";
  23. public string Title
  24. {
  25. get { return this._title; }
  26. set
  27. {
  28. this.SetProperty( ref this._title , value );
  29. }
  30. }
  31. private string _selectAxis = string.Empty;
  32. public string SelectAxis
  33. {
  34. get => this._selectAxis;
  35. set { this.SetProperty( ref this._selectAxis , value ); }
  36. }
  37. private string _selectedPosition = string.Empty;
  38. public string SelectedPosition
  39. {
  40. get => this._selectedPosition;
  41. set
  42. {
  43. this.SetProperty( ref this._selectedPosition , value );
  44. }
  45. }
  46. double _jogVelocity = 5;
  47. public double JogVelocity
  48. {
  49. get { return this._jogVelocity; }
  50. set { this.SetProperty( ref this._jogVelocity , value ); }
  51. }
  52. #region Binding Lock Left
  53. private double _targetPosLockLeft;
  54. public double TargetPosLockLeft
  55. {
  56. get { return this._targetPosLockLeft; }
  57. set { this.SetProperty( ref this._targetPosLockLeft , value ); }
  58. }
  59. private double _currentLockLeft;
  60. public double CurrentLockLeft
  61. {
  62. get { return this._currentLockLeft; }
  63. set { this.SetProperty( ref this._currentLockLeft , value ); }
  64. }
  65. private double _differenceLockLeft;
  66. public double DifferenceLockLeft
  67. {
  68. get { return this._differenceLockLeft; }
  69. set { this.SetProperty( ref this._differenceLockLeft , value ); }
  70. }
  71. Brush _leftAxisBrush = Brushes.Gray;
  72. public Brush LeftAxisBrush
  73. {
  74. get { return this._leftAxisBrush; }
  75. set { SetProperty( ref this._leftAxisBrush , value ); }
  76. }
  77. private Brush posLockBrush = Brushes.Gray;
  78. public Brush PosLockBrush
  79. {
  80. get { return posLockBrush; }
  81. set { SetProperty( ref this.posLockBrush , value ); }
  82. }
  83. private bool isLeftServoOn;
  84. public bool IsLeftServoOn
  85. {
  86. get { return isLeftServoOn; }
  87. set { SetProperty( ref isLeftServoOn , value ); }
  88. }
  89. private bool isLeftServoOriginReturn;
  90. public bool IsLeftServoOriginReturn
  91. {
  92. get { return isLeftServoOriginReturn; }
  93. set { SetProperty( ref isLeftServoOriginReturn , value ); }
  94. }
  95. private bool isLeftServoFalut;
  96. public bool IsLeftServoFault
  97. {
  98. get { return isLeftServoFalut; }
  99. set { SetProperty( ref isLeftServoFalut , value ); }
  100. }
  101. #endregion
  102. #region Binding Lock Right
  103. private double _targetPosLockRight;
  104. public double TargetPosLockRight
  105. {
  106. get { return this._targetPosLockRight; }
  107. set { this.SetProperty( ref this._targetPosLockRight , value ); }
  108. }
  109. private double _currentLockRight;
  110. public double CurrentLockRight
  111. {
  112. get { return this._currentLockRight; }
  113. set { this.SetProperty( ref this._currentLockRight , value ); }
  114. }
  115. private double _differenceLockRight;
  116. public double DifferenceLockRight
  117. {
  118. get { return this._differenceLockRight; }
  119. set { this.SetProperty( ref this._differenceLockRight , value ); }
  120. }
  121. Brush _rightAxisBrush = Brushes.Gray;
  122. public Brush RightAxisBrush
  123. {
  124. get { return this._rightAxisBrush; }
  125. set { SetProperty( ref this._rightAxisBrush , value ); }
  126. }
  127. private Brush posUnlockBursh = Brushes.Gray;
  128. public Brush PosUnlockBrush
  129. {
  130. get { return posUnlockBursh; }
  131. set { SetProperty( ref this.posUnlockBursh , value ); }
  132. }
  133. private bool isRightServoOn;
  134. public bool IsRightServoOn
  135. {
  136. get { return isRightServoOn; }
  137. set { SetProperty( ref isRightServoOn , value ); }
  138. }
  139. private bool isRightServoOriginReturn;
  140. public bool IsRightServoOriginReturn
  141. {
  142. get { return isRightServoOriginReturn; }
  143. set { SetProperty( ref isRightServoOriginReturn , value ); }
  144. }
  145. private bool isRightServoFalut;
  146. public bool IsRightServoFault
  147. {
  148. get { return isRightServoFalut; }
  149. set { SetProperty( ref isRightServoFalut , value ); }
  150. }
  151. #endregion
  152. #region ICommand
  153. public ICommand SelectAxisCommand { get; set; }
  154. public ICommand KeyInTargetPosCommand { get; set; }
  155. public ICommand SelectPosCommand { get; set; }
  156. public ICommand MoveToCommand { get; set; }
  157. public ICommand CurrentToTargetCommand { get; set; }
  158. public ICommand ServoOnCommand { get; set; }
  159. public ICommand ServoOffCommand { get; set; }
  160. public ICommand FaultResetCommand { get; set; }
  161. public ICommand OriginCommand { get; set; }
  162. public ICommand JogCommand { get; set; }
  163. public ICommand SelectedPosDataSave { get; set; }
  164. public ICommand JogVelPopupCommand { get; set; }
  165. public ICommand JogStopCommand { get; set; }
  166. #endregion
  167. public event Action<IDialogResult> RequestClose;
  168. IEventAggregator eventAggregator;
  169. MessageController messageController;
  170. SqliteManager sql;
  171. public LockServoViewModel( IEventAggregator ea , MessageController _messageController , SqliteManager _sql )
  172. {
  173. this.SelectAxisCommand = new DelegateCommand<object>( ExecuteSelectAxisCommand );
  174. this.SelectPosCommand = new DelegateCommand<object>( ExecuteSelectPosCommand );
  175. this.MoveToCommand = new DelegateCommand( ExecuteMoveToCommand );
  176. this.CurrentToTargetCommand = new DelegateCommand( ExecuteCurrentToTargetCommand );
  177. this.KeyInTargetPosCommand = new DelegateCommand<object>( ExecuteKeyInCommand );
  178. this.ServoOnCommand = new DelegateCommand<object>( ExecuteServoOnCommand );
  179. this.ServoOffCommand = new DelegateCommand<object>( ExecuteServoOffCommand );
  180. this.FaultResetCommand = new DelegateCommand<object>( ExecuteFaultResetCommand );
  181. this.OriginCommand = new DelegateCommand<object>( ExecuteOriginCommand );
  182. this.SelectedPosDataSave = new DelegateCommand<object>( ExecuteSelectedPosDataSave );
  183. this.JogCommand = new DelegateCommand<object>( ExecuteJogCommand );
  184. this.JogVelPopupCommand = new DelegateCommand( ExecuteJogVelPopup );
  185. this.JogStopCommand = new DelegateCommand( ExecuteJogStop );
  186. this.eventAggregator = ea;
  187. this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Unsubscribe( AxisUICallbackCommunication );
  188. this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Subscribe( AxisUICallbackCommunication , ThreadOption.UIThread );
  189. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Unsubscribe( UICallbackCommunication );
  190. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallbackCommunication , ThreadOption.UIThread );
  191. this.messageController = _messageController;
  192. this.sql = _sql;
  193. //초기 선택 축
  194. this.SelectAxis = ConstString.AXIS_CARRIER_LOCK_LEFT;
  195. this.LeftAxisBrush = Brushes.Orange;
  196. List<AxisPositionData> positionList = null;
  197. positionList = sql.AxisPositionDataDAL.GetKFromPostion( ConstString.TEACH_POSITION_LOCK );
  198. this.TargetPosLockLeft = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) ).Single().Value;
  199. //초기 Pos 선택
  200. positionList = sql.AxisPositionDataDAL.GetKFromPostion( ConstString.TEACH_POSITION_LOCK );
  201. this.TargetPosLockRight = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_RIGHT ) ).Single().Value;
  202. this.SelectedPosition = ConstString.TEACH_POSITION_LOCK;
  203. this.PosLockBrush = Brushes.Orange;
  204. }
  205. public void ExecuteJogStop( )
  206. {
  207. var msg = new AxisControlEventArgs
  208. {
  209. Dir = eEventDir.ToBack ,
  210. Kind = eAxisControlKind.Stop,
  211. AxisName = this.SelectAxis ,
  212. };
  213. this.PublishEvent( msg );
  214. }
  215. private void ExecuteJogVelPopup( )
  216. {
  217. var numPad = new CalcuratorView();
  218. var result = numPad.ShowDialog( this.JogVelocity );
  219. this.JogVelocity = result;
  220. }
  221. private void UICallbackCommunication( GUIMessageEventArgs obj )
  222. {
  223. if ( obj.Kind == GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange )
  224. {
  225. if ( obj.MessageKey.Equals( MessageKey.Vehicle ) )
  226. {
  227. }
  228. }
  229. }
  230. private void ExecuteSelectedPosDataSave( object obj )
  231. {
  232. List<AxisPositionData> dataList = null;
  233. this.messageController.ShowConfirmationPopupView( "Position Data Save ?" , r =>
  234. {
  235. if ( r.Result == ButtonResult.OK )
  236. {
  237. switch ( this.SelectedPosition )
  238. {
  239. case ConstString.TEACH_POSITION_LOCK:
  240. dataList = sql.AxisPositionDataDAL.GetKFromPostion( ConstString.TEACH_POSITION_LOCK );
  241. break;
  242. case ConstString.TEACH_POSITION_UNLOCK:
  243. dataList = sql.AxisPositionDataDAL.GetKFromPostion( ConstString.TEACH_POSITION_UNLOCK );
  244. break;
  245. }
  246. var sqlAxisLeft = dataList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) ).Single();
  247. sqlAxisLeft.Value = this.TargetPosLockLeft;
  248. sql.AxisPositionDataDAL.Update( sqlAxisLeft );
  249. var sqlAxisRight = dataList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_RIGHT ) ).Single();
  250. sqlAxisRight.Value = this.TargetPosLockRight;
  251. sql.AxisPositionDataDAL.Update( sqlAxisRight );
  252. }
  253. } );
  254. }
  255. public void ExecuteJogCommand( object obj )
  256. {
  257. if ( this.JogVelocity <= 0 )
  258. {
  259. this.messageController.ShowNotificationView( "Check Jog Velocity" );
  260. return;
  261. }
  262. var msg = new AxisControlEventArgs
  263. {
  264. Dir = eEventDir.ToBack,
  265. Kind = eAxisControlKind.Jog,
  266. AxisName = this.SelectAxis,
  267. JogVelocity = Convert.ToInt32( this.JogVelocity ),
  268. };
  269. if ( obj.ToString().Equals( "+" ) )
  270. msg.JogDir = eJogMoveDir.Positive;
  271. else
  272. msg.JogDir = eJogMoveDir.Negative;
  273. this.PublishEvent( msg );
  274. }
  275. void PublishEvent( AxisControlEventArgs args )
  276. {
  277. args.Dir = eEventDir.ToBack;
  278. this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Publish( args );
  279. }
  280. void AxisUICallbackCommunication( AxisControlEventArgs args )
  281. {
  282. if ( args.Dir == eEventDir.ToFront )
  283. {
  284. switch ( args.Kind )
  285. {
  286. case eAxisControlKind.Stop:
  287. break;
  288. case eAxisControlKind.EStop:
  289. break;
  290. case eAxisControlKind.Move:
  291. RspMove( args );
  292. break;
  293. case eAxisControlKind.Jog:
  294. break;
  295. case eAxisControlKind.ServoOn:
  296. break;
  297. case eAxisControlKind.ServoOff:
  298. break;
  299. case eAxisControlKind.NONE:
  300. break;
  301. case eAxisControlKind.OriginReturn:
  302. break;
  303. case eAxisControlKind.ReqCurrentPosition:
  304. break;
  305. case eAxisControlKind.ReqStopCurrentPosition:
  306. break;
  307. case eAxisControlKind.FaultReset:
  308. {
  309. var msg = string.Empty;
  310. if ( args.Result.IsSuccess )
  311. msg = $" {this.SelectAxis} Reset Success ";
  312. else
  313. msg = $" {this.SelectAxis} Reset Failed ";
  314. this.messageController.ShowNotificationView( msg );
  315. }
  316. break;
  317. case eAxisControlKind.AxisState:
  318. AxisState( args );
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. }
  325. private void AxisState( AxisControlEventArgs args )
  326. {
  327. var left = args.Args[ "Left" ] as AxisState;
  328. var right = args.Args[ "Right" ] as AxisState;
  329. this.IsLeftServoOn = left.IsServoOn;
  330. this.IsLeftServoOriginReturn = left.IsOriginReturn;
  331. this.IsLeftServoFault = left.IsAmpFault;
  332. this.CurrentLockLeft = left.CurrentPosition;
  333. this.IsRightServoOn = right.IsServoOn;
  334. this.IsRightServoOriginReturn = right.IsOriginReturn;
  335. this.IsRightServoFault = right.IsAmpFault;
  336. this.CurrentLockRight = right.CurrentPosition;
  337. }
  338. private void RspMove( AxisControlEventArgs args )
  339. {
  340. string msg = string.Empty;
  341. if ( args.Result.IsSuccess )
  342. {
  343. if ( args.PosDir == ePosDir.Lock )
  344. msg = "Clamp Lock Success";
  345. else
  346. msg = "Clamp Unlock Success";
  347. }
  348. else
  349. {
  350. var error = args.Result.Errors.FirstOrDefault();
  351. var alarm = error.Metadata[ "Alarm" ] as Alarm;
  352. msg = alarm.Name + " " + alarm.Text;
  353. }
  354. this.messageController.ShowNotificationView( msg );
  355. }
  356. private void ExecuteOriginCommand( object obj )
  357. {
  358. var msg = new AxisControlEventArgs
  359. {
  360. Dir = eEventDir.ToBack ,
  361. Kind = eAxisControlKind.OriginReturn ,
  362. AxisName = this.SelectAxis ,
  363. };
  364. this.PublishEvent( msg );
  365. }
  366. private void ExecuteFaultResetCommand( object obj )
  367. {
  368. var msg = new AxisControlEventArgs
  369. {
  370. Dir = eEventDir.ToBack ,
  371. Kind = eAxisControlKind.FaultReset ,
  372. AxisName = this.SelectAxis ,
  373. };
  374. this.PublishEvent( msg );
  375. }
  376. private void ExecuteServoOffCommand( object obj )
  377. {
  378. var msg = new AxisControlEventArgs
  379. {
  380. Dir = eEventDir.ToBack ,
  381. Kind = eAxisControlKind.ServoOff ,
  382. AxisName = this.SelectAxis ,
  383. };
  384. this.PublishEvent( msg );
  385. }
  386. private void ExecuteServoOnCommand( object obj )
  387. {
  388. var msg = new AxisControlEventArgs
  389. {
  390. Dir = eEventDir.ToBack ,
  391. Kind = eAxisControlKind.ServoOn ,
  392. AxisName = this.SelectAxis ,
  393. };
  394. this.PublishEvent( msg );
  395. }
  396. private void ExecuteKeyInCommand( object obj )
  397. {
  398. var numPad = new CalcuratorView();
  399. if ( obj.ToString().Equals( "Left" ) )
  400. {
  401. var result = numPad.ShowDialog( this.TargetPosLockLeft );
  402. this.TargetPosLockLeft = result;
  403. }
  404. else
  405. {
  406. var result = numPad.ShowDialog( this.TargetPosLockRight );
  407. this.TargetPosLockRight = result;
  408. }
  409. }
  410. private void ExecuteCurrentToTargetCommand( )
  411. {
  412. this.messageController.ShowConfirmationPopupView( "Current Position To Target Position ?" , r =>
  413. {
  414. if ( r.Result == ButtonResult.OK )
  415. {
  416. switch ( this.SelectAxis )
  417. {
  418. case ConstString.AXIS_CARRIER_LOCK_LEFT:
  419. this.TargetPosLockLeft = this.CurrentLockLeft;
  420. break;
  421. case ConstString.AXIS_CARRIER_LOCK_RIGHT:
  422. this.TargetPosLockRight = this.CurrentLockRight;
  423. break;
  424. }
  425. }
  426. } );
  427. }
  428. private void ExecuteMoveToCommand( )
  429. {
  430. var error = string.Empty;
  431. if ( this.IsLeftServoFault || this.IsRightServoFault )
  432. error = "Is Servo Fault";
  433. else if ( !this.IsLeftServoOn || !this.IsRightServoOn )
  434. error = "Is Servo OFF";
  435. if ( error != string.Empty )
  436. this.messageController.ShowNotificationView( error );
  437. else
  438. {
  439. this.messageController.ShowConfirmationPopupView( $"Clamp Move To {this.SelectedPosition}" , r =>
  440. {
  441. if ( r.Result == ButtonResult.OK )
  442. {
  443. var msg = new AxisControlEventArgs();
  444. msg.Kind = eAxisControlKind.Move;
  445. msg.AxisName = this.SelectAxis;
  446. if ( SelectedPosition.Equals( ConstString.TEACH_POSITION_LOCK ) )
  447. msg.PosDir = ePosDir.Lock;
  448. else
  449. msg.PosDir = ePosDir.UnLock;
  450. this.PublishEvent( msg );
  451. }
  452. } );
  453. }
  454. }
  455. private void ExecuteSelectPosCommand( object obj )
  456. {
  457. List<AxisPositionData> positionList = null;
  458. this.SelectedPosition = obj.ToString();
  459. if ( obj.Equals( ConstString.TEACH_POSITION_LOCK ) )
  460. {
  461. this.PosLockBrush = Brushes.Orange;
  462. this.PosUnlockBrush = Brushes.Gray;
  463. }
  464. else
  465. {
  466. this.PosLockBrush = Brushes.Gray;
  467. this.PosUnlockBrush = Brushes.Orange;
  468. }
  469. switch ( this.SelectedPosition )
  470. {
  471. case ConstString.TEACH_POSITION_LOCK:
  472. positionList = sql.AxisPositionDataDAL.GetKFromPostion( ConstString.TEACH_POSITION_LOCK );
  473. break;
  474. case ConstString.TEACH_POSITION_UNLOCK:
  475. positionList = sql.AxisPositionDataDAL.GetKFromPostion( ConstString.TEACH_POSITION_UNLOCK );
  476. break;
  477. }
  478. var sqlAxisLeft = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) ).Single();
  479. this.TargetPosLockLeft = sqlAxisLeft.Value;
  480. var sqlAxisRight = positionList.Where( x => x.AxisName.Equals( ConstString.AXIS_CARRIER_LOCK_RIGHT ) ).Single();
  481. this.TargetPosLockRight = sqlAxisRight.Value;
  482. }
  483. private void ExecuteSelectAxisCommand( object obj )
  484. {
  485. if ( obj.ToString().Equals( ConstString.AXIS_CARRIER_LOCK_LEFT ) )
  486. {
  487. this.LeftAxisBrush = Brushes.Orange;
  488. this.RightAxisBrush = Brushes.Gray;
  489. this.SelectAxis = ConstString.AXIS_CARRIER_LOCK_LEFT;
  490. }
  491. else
  492. {
  493. this.RightAxisBrush = Brushes.Orange;
  494. this.LeftAxisBrush = Brushes.Gray;
  495. this.SelectAxis = ConstString.AXIS_CARRIER_LOCK_RIGHT;
  496. }
  497. }
  498. #region Dialog
  499. public bool CanCloseDialog( )
  500. {
  501. //throw new NotImplementedException();
  502. return true;
  503. }
  504. public void OnDialogClosed( )
  505. {
  506. this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Publish( new AxisControlEventArgs { Dir = AxisControlEventArgs.eEventDir.ToBack , Kind = AxisControlEventArgs.eAxisControlKind.ReqStopCurrentPosition } );
  507. this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Unsubscribe( AxisUICallbackCommunication );
  508. }
  509. public void OnDialogOpened( IDialogParameters parameters )
  510. {
  511. this.eventAggregator.GetEvent<AxisControlPubSubEvent>().Publish( new AxisControlEventArgs { Dir = AxisControlEventArgs.eEventDir.ToBack , Kind = AxisControlEventArgs.eAxisControlKind.ReqCurrentPosition } );
  512. }
  513. public virtual void RaiseRequestClose( IDialogResult dialogResult )
  514. {
  515. RequestClose?.Invoke( dialogResult );
  516. }
  517. private void CloseDialog( string parameter )
  518. {
  519. ButtonResult result = ButtonResult.None;
  520. if ( parameter?.ToLower() == "true" )
  521. result = ButtonResult.OK;
  522. else if ( parameter?.ToLower() == "false" )
  523. result = ButtonResult.Cancel;
  524. RaiseRequestClose( new DialogResult( result ) );
  525. }
  526. #endregion
  527. }
  528. }