DriveServoViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. using GSG.NET.Extensions;
  2. using OHV.Common.Events;
  3. using OHV.Common.Model;
  4. using OHV.Common.Shareds;
  5. using OHV.SqliteDAL;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. namespace OHV.Module.Interactivity.PopUp
  18. {
  19. public class DriveServoViewModel : BindableBase, IDialogAware
  20. {
  21. private DelegateCommand<string> _closeDialogCommand;
  22. public DelegateCommand<string> CloseDialogCommand =>
  23. _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));
  24. private string _title = "DriveServoView";
  25. public string Title
  26. {
  27. get { return this._title; }
  28. set
  29. {
  30. this.SetProperty(ref this._title, value);
  31. }
  32. }
  33. private string _selectedPosition = string.Empty;
  34. public string SelectedPosition
  35. {
  36. get => this._selectedPosition;
  37. set
  38. {
  39. this.SetProperty(ref this._selectedPosition, value);
  40. }
  41. }
  42. private string _selectDirection = string.Empty;
  43. public string SelectDirection
  44. {
  45. get => this._selectDirection;
  46. set { this.SetProperty(ref this._selectDirection, value); }
  47. }
  48. #region Motor Left Binding Value
  49. private double _driveTargetPos;
  50. public double DriveTargetPos
  51. {
  52. get { return this._driveTargetPos; }
  53. set { this.SetProperty(ref this._driveTargetPos , value); }
  54. }
  55. private double _currentDrive;
  56. public double CurrentDrive
  57. {
  58. get { return this._currentDrive; }
  59. set { this.SetProperty(ref this._currentDrive, value); }
  60. }
  61. private double _differenceDrive;
  62. public double DifferenceDrive
  63. {
  64. get { return this._differenceDrive; }
  65. set { this.SetProperty(ref this._differenceDrive, value); }
  66. }
  67. private double _jogVelocity;
  68. public double JogVelocity
  69. {
  70. get { return this._jogVelocity; }
  71. set { this.SetProperty(ref this._jogVelocity, value); }
  72. }
  73. #endregion
  74. public event Action<IDialogResult> RequestClose;
  75. public ICommand SelectAxisCommand { get; set; }
  76. public ICommand KeyInTargetPosCommand { get; set; }
  77. public ICommand SelectPosCommand { get; set; }
  78. public ICommand MoveToCommand { get; set; }
  79. public ICommand CurrentToTargetCommand { get; set; }
  80. public ICommand ServoOnCommand { get; set; }
  81. public ICommand ServoOffCommand { get; set; }
  82. public ICommand FaultResetCommand { get; set; }
  83. public ICommand OriginCommand { get; set; }
  84. public IEventAggregator eventAggregator;
  85. public ICommand PositionAddCommand { get; set; }
  86. public ICommand PositionDeleteCommand { get; set; }
  87. public ICommand PositionSaveCommand { get; set; }
  88. public ICommand SteeringCWCommand { get; set; }
  89. public ICommand SteeringCCWCommand { get; set; }
  90. public ICommand SelectedDirection { get; set; }
  91. public ICommand JogVelPopupCommand { get; set; }
  92. public ICommand JogCommand { get; set; }
  93. #region Brushes
  94. private Brush steeringLeftBrush = Brushes.Gray;
  95. public Brush SteeringLeftBrushProperty
  96. {
  97. get { return steeringLeftBrush; }
  98. set { SetProperty(ref this.steeringLeftBrush, value); }
  99. }
  100. private Brush steeringRightBrush = Brushes.Gray;
  101. public Brush SteeringRightBrushProperty
  102. {
  103. get { return steeringRightBrush; }
  104. set { SetProperty(ref this.steeringRightBrush, value); }
  105. }
  106. #endregion
  107. private ObservableCollection<Route> _routeList;
  108. public ObservableCollection<Route> RouteList
  109. {
  110. get { return this._routeList; }
  111. set { SetProperty(ref this._routeList, value); }
  112. }
  113. public SqliteDAL.DAL.AxisPositionDataDAL axisPositionDataDal;
  114. SqliteManager sql;
  115. MessageController messageController;
  116. public DriveServoViewModel(IEventAggregator _ea, SqliteManager _sql, MessageController _messageController)
  117. {
  118. this.eventAggregator = _ea;
  119. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Unsubscribe( DriveControlCallBack );
  120. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Subscribe( DriveControlCallBack , ThreadOption.UIThread );
  121. /*상태 회신을 받기 위함*/
  122. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Unsubscribe( UICallBackCommunication );
  123. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallBackCommunication , ThreadOption.UIThread );
  124. this.sql = _sql;
  125. this.RouteList = new ObservableCollection<Route>(sql.RouteDal.All);
  126. this.messageController = _messageController;
  127. this.SelectPosCommand = new DelegateCommand<object>(ExecuteSelectPosCommand);
  128. this.MoveToCommand = new DelegateCommand(ExecuteMoveToCommand);
  129. this.CurrentToTargetCommand = new DelegateCommand(ExecuteCurrentToTargetCommand);
  130. this.KeyInTargetPosCommand = new DelegateCommand<object>(ExecuteKeyInCommadn);
  131. this.ServoOnCommand = new DelegateCommand(ExecuteServoOnCommand);
  132. this.ServoOffCommand = new DelegateCommand(ExecuteServoOffCommand);
  133. this.FaultResetCommand = new DelegateCommand(ExecuteFaultResetCommand);
  134. this.OriginCommand = new DelegateCommand(ExecuteOriginCommand);
  135. this.PositionAddCommand = new DelegateCommand(ExecutePositionAddCommand);
  136. this.PositionDeleteCommand = new DelegateCommand(ExecutePositionDeleteCommand);
  137. this.PositionSaveCommand = new DelegateCommand(ExecutePositionSaveCommand);
  138. this.SelectedDirection = new DelegateCommand<object>(ExecuteSelectedDirection);
  139. this.SteeringCWCommand = new DelegateCommand<object>(ExecuteSteeringCWCommand);
  140. this.SteeringCCWCommand = new DelegateCommand<object>(ExecuteSteeringCCWCommand);
  141. this.JogVelPopupCommand = new DelegateCommand(ExecuteJogVelPopupCommand);
  142. this.JogCommand = new DelegateCommand<object>(ExecuteJogCommand);
  143. }
  144. private void UICallBackCommunication( GUIMessageEventArgs obj )
  145. {
  146. //Property 상태가 바뀔때 마다 표현만 하면 된다?
  147. if ( obj.Kind == GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange )
  148. {
  149. if ( obj.MessageKey.Equals( MessageKey.Vehicle ) )
  150. {
  151. switch ( obj.ModelPropertyName )
  152. {
  153. case "SteeringState":
  154. {
  155. var dir = CastTo<eSteeringState>.From<object>( obj.Args );
  156. this.ChangeSteeringDirection( dir );
  157. }
  158. break;
  159. case "VehicleStateProperty":
  160. {
  161. var v = CastTo<eVehicleState>.From<object>( obj.Args );
  162. this.ChagneVehicleState( v );
  163. }
  164. break;
  165. case "ObstacleStateProperty":
  166. {
  167. var reply = CastTo<eObstacleState>.From<object>( obj.Args );
  168. this.ChangeObstacleState( reply );
  169. }
  170. break;
  171. case "CurrentPosition":
  172. {
  173. var v = CastTo<double>.From<object>(obj.Args);
  174. this.CurrentDrive = v;
  175. }
  176. break;
  177. case "CurrentTag":
  178. break;
  179. case "CurrentSpeed":
  180. break;
  181. case "CurrentTorque":
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. private void ChangeObstacleState( eObstacleState reply )
  190. {
  191. }
  192. private void ChagneVehicleState( eVehicleState v )
  193. {
  194. //TODO:[20/03/20 ys-hwang] Vehicle Manual Move 할때? 상태를 보고 해야?, 아니면 Manual 이니까????
  195. // 아니면 busy 상태 확인만 ?
  196. switch ( v )
  197. {
  198. case eVehicleState.None:
  199. break;
  200. case eVehicleState.Idle:
  201. break;
  202. case eVehicleState.Move:
  203. break;
  204. case eVehicleState.Load:
  205. break;
  206. case eVehicleState.Unload:
  207. break;
  208. case eVehicleState.Charge:
  209. break;
  210. case eVehicleState.Abnormal:
  211. break;
  212. case eVehicleState.Blocked:
  213. break;
  214. case eVehicleState.Decelerate:
  215. break;
  216. }
  217. }
  218. private void DriveControlCallBack( DriveControlEventArgs args)
  219. {
  220. //TODO:[20/03/19 ys-hwang] Drive UI return msg popup
  221. if (args.EventDir == DriveControlEventArgs.eEventDir.ToFront)
  222. {
  223. switch ( args.ControlKind )
  224. {
  225. case DriveControlEventArgs.eControlKind.MOVE:
  226. ResponseMove(args);
  227. break;
  228. case DriveControlEventArgs.eControlKind.STOP:
  229. break;
  230. case DriveControlEventArgs.eControlKind.Steering:
  231. //if ( args.Result.IsSuccess )
  232. //{
  233. // var dir = args.Result.ToResult<DriveControlEventArgs.eMoveDir>().Value;
  234. // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
  235. //}
  236. break;
  237. case DriveControlEventArgs.eControlKind.SteeringState:
  238. //if ( args.Result.IsSuccess )
  239. //{
  240. // var dir = args.Result.ToResult<DriveControlEventArgs.eMoveDir>().Value;
  241. // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
  242. //}
  243. break;
  244. case DriveControlEventArgs.eControlKind.ReqCurrentPos:
  245. this.CurrentDrive = args.CurrentPosition;
  246. break;
  247. case DriveControlEventArgs.eControlKind.ReqStopCurrentPos:
  248. break;
  249. case DriveControlEventArgs.eControlKind.FaultReset:
  250. break;
  251. case DriveControlEventArgs.eControlKind.DriveON:
  252. break;
  253. case DriveControlEventArgs.eControlKind.DriveOFF:
  254. break;
  255. case DriveControlEventArgs.eControlKind.JOG:
  256. break;
  257. case DriveControlEventArgs.eControlKind.VehicleState:
  258. ResponseVehicleState(args);
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. }
  265. private void ResponseVehicleState(DriveControlEventArgs args)
  266. {
  267. var state = CastTo<VehicleState>.From<object>( args.Args );
  268. this.CurrentDrive = state.CurrentPosition;
  269. }
  270. private void ResponseMove( DriveControlEventArgs args )
  271. {
  272. var msg = string.Empty;
  273. if(args.Result.IsSuccess)
  274. {
  275. msg = "Move Successs";
  276. }
  277. else
  278. {
  279. var error = args.Result.Errors.FirstOrDefault();
  280. var alarm = error.Metadata[ "Alarm" ] as Alarm;
  281. msg = alarm.Name + " " + alarm.Text;
  282. }
  283. this.messageController.ShowNotificationView( msg );
  284. }
  285. void PublishEvent(DriveControlEventArgs args)
  286. {
  287. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish(args);
  288. }
  289. void ChangeSteeringDirection(eSteeringState state)
  290. {
  291. if ( state == eSteeringState.Left )
  292. {
  293. this.SteeringLeftBrushProperty = ( System.Windows.Media.Brush )new System.Windows.Media.BrushConverter().ConvertFromString( "#FF00FFD3" );
  294. this.SteeringRightBrushProperty = Brushes.Gray;
  295. }
  296. else if ( state == eSteeringState.Right )
  297. {
  298. this.SteeringLeftBrushProperty = Brushes.Gray;
  299. this.SteeringRightBrushProperty = ( System.Windows.Media.Brush )new System.Windows.Media.BrushConverter().ConvertFromString( "#FF00FFD3" );
  300. }
  301. else
  302. {
  303. this.SteeringLeftBrushProperty = Brushes.Gray;
  304. this.SteeringRightBrushProperty = Brushes.Gray;
  305. }
  306. }
  307. #region Execute Method
  308. private void ExecuteJogCommand(object obj)
  309. {
  310. var msg = new DriveControlEventArgs
  311. {
  312. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  313. ControlKind = DriveControlEventArgs.eControlKind.JOG,
  314. };
  315. if ( obj.ToString().Equals( "+" ) )
  316. msg.JogDir = DriveControlEventArgs.eJogMoveDir.Positive;
  317. else
  318. msg.JogDir = DriveControlEventArgs.eJogMoveDir.Negative;
  319. this.PublishEvent( msg );
  320. }
  321. private void ExecuteJogVelPopupCommand()
  322. {
  323. var numPad = new CalcuratorView();
  324. var result = numPad.ShowDialog(this.JogVelocity);
  325. this.JogVelocity = result;
  326. }
  327. private void ExecuteSelectedDirection(object obj)
  328. {
  329. this.SelectDirection = obj.ToString();
  330. }
  331. private void ExecuteSteeringCCWCommand(object obj)
  332. {
  333. var msg = new DriveControlEventArgs
  334. {
  335. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  336. ControlKind = DriveControlEventArgs.eControlKind.Steering,
  337. };
  338. if (obj.ToString().Equals("CCW"))
  339. msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
  340. else
  341. msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  342. this.PublishEvent(msg);
  343. }
  344. private void ExecuteSteeringCWCommand(object obj)
  345. {
  346. var msg = new DriveControlEventArgs
  347. {
  348. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  349. ControlKind = DriveControlEventArgs.eControlKind.Steering,
  350. };
  351. if (obj.ToString().Equals("CW"))
  352. msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  353. else
  354. msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
  355. this.PublishEvent(msg);
  356. }
  357. private void ExecutePositionSaveCommand()
  358. {
  359. this.messageController.ShowConfirmationPopupView("Save To Data ?", r =>
  360. {
  361. if (r.Result == ButtonResult.OK)
  362. {
  363. //TODO:[20/03/18 ys-hwang] DB Table Update
  364. }
  365. });
  366. }
  367. private void ExecutePositionDeleteCommand()
  368. {
  369. this.messageController.ShowConfirmationPopupView( "Select To Delete ?" , r =>
  370. {
  371. if ( r.Result == ButtonResult.OK )
  372. {
  373. var deleteList = new List<Route>();
  374. foreach ( var item in this.RouteList )
  375. {
  376. if ( item.IsSelected )
  377. deleteList.Add( item );
  378. }
  379. deleteList.ForEach( x => { this.RouteList.Remove( x ); } );
  380. }
  381. } );
  382. }
  383. private void ExecutePositionAddCommand()
  384. {
  385. this.messageController.ShowConfirmationPopupView( "Position Add ?" , r =>
  386. {
  387. if ( r.Result == ButtonResult.OK )
  388. {
  389. this.RouteList.Add( new Route() );
  390. this.messageController.ShowNotificationView( "Create Success" );
  391. }
  392. } );
  393. }
  394. private void ExecuteKeyInCommadn(object obj)
  395. {
  396. var numPad = new CalcuratorView();
  397. var result = numPad.ShowDialog(this.DriveTargetPos );
  398. this.DriveTargetPos = result;
  399. }
  400. private void ExecuteOriginCommand()
  401. {
  402. this.messageController.ShowConfirmationPopupView( "Origin ?" , r =>
  403. {
  404. if ( r.Result == ButtonResult.OK )
  405. {
  406. //TODO: How to use
  407. }
  408. } );
  409. }
  410. private void ExecuteFaultResetCommand()
  411. {
  412. var msg = new DriveControlEventArgs
  413. {
  414. EventDir = DriveControlEventArgs.eEventDir.ToBack ,
  415. ControlKind = DriveControlEventArgs.eControlKind.FaultReset ,
  416. };
  417. this.PublishEvent( msg );
  418. }
  419. private void ExecuteServoOffCommand()
  420. {
  421. var msg = new DriveControlEventArgs
  422. {
  423. EventDir = DriveControlEventArgs.eEventDir.ToBack ,
  424. ControlKind = DriveControlEventArgs.eControlKind.DriveOFF ,
  425. };
  426. this.PublishEvent( msg );
  427. }
  428. private void ExecuteServoOnCommand()
  429. {
  430. var msg = new DriveControlEventArgs
  431. {
  432. EventDir = DriveControlEventArgs.eEventDir.ToBack ,
  433. ControlKind = DriveControlEventArgs.eControlKind.DriveON ,
  434. };
  435. this.PublishEvent( msg );
  436. }
  437. private void ExecuteCurrentToTargetCommand()
  438. {
  439. this.messageController.ShowConfirmationPopupView( "Current To Target ?" , r =>
  440. {
  441. if ( r.Result == ButtonResult.OK )
  442. {
  443. this.DriveTargetPos = this.CurrentDrive;
  444. }
  445. } );
  446. }
  447. private void ExecuteMoveToCommand()
  448. {
  449. this.messageController.ShowConfirmationPopupView( "Move To Selected Position ?" , r =>
  450. {
  451. if ( r.Result == ButtonResult.OK )
  452. {
  453. //TODO:[20/03/20 ys-hwang] RoutList.IsSelected Check
  454. //if(this.RouteList.Any( x => x.IsSelected != true ))
  455. // return;
  456. var result = this.RouteList.Any( x => x.IsSelected != false );
  457. var ll = this.RouteList.Where(x => x.IsSelected).FirstOrDefault();
  458. var msg = new DriveControlEventArgs
  459. {
  460. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  461. ControlKind = DriveControlEventArgs.eControlKind.MOVE,
  462. TargetRouteID = ll.Id,
  463. };
  464. this.PublishEvent( msg );
  465. }
  466. } );
  467. }
  468. private void ExecuteSelectPosCommand(object obj)
  469. {
  470. this.SelectedPosition = obj.ToString();
  471. }
  472. #endregion
  473. #region Dialog Function
  474. public bool CanCloseDialog()
  475. {
  476. return true;
  477. }
  478. public void OnDialogClosed()
  479. {
  480. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack , ControlKind = DriveControlEventArgs.eControlKind.ReqStopCurrentPos } );
  481. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Unsubscribe( DriveControlCallBack );
  482. }
  483. public void OnDialogOpened(IDialogParameters parameters)
  484. {
  485. var msg = new DriveControlEventArgs
  486. {
  487. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  488. ControlKind = DriveControlEventArgs.eControlKind.SteeringState,
  489. };
  490. this.PublishEvent(msg);
  491. //this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack , ControlKind = DriveControlEventArgs.eControlKind.VehicleState } );
  492. }
  493. private void CloseDialog(string parameter)
  494. {
  495. ButtonResult result = ButtonResult.None;
  496. if (parameter?.ToLower() == "true")
  497. result = ButtonResult.OK;
  498. else if (parameter?.ToLower() == "false")
  499. result = ButtonResult.Cancel;
  500. RaiseRequestClose(new DialogResult(result));
  501. }
  502. public virtual void RaiseRequestClose(IDialogResult dialogResult)
  503. {
  504. RequestClose?.Invoke(dialogResult);
  505. }
  506. #endregion
  507. }
  508. }