DriveServoViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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.NONE:
  226. break;
  227. case DriveControlEventArgs.eControlKind.MOVE:
  228. ResponseMove(args);
  229. break;
  230. case DriveControlEventArgs.eControlKind.STOP:
  231. break;
  232. case DriveControlEventArgs.eControlKind.Steering:
  233. //if ( args.Result.IsSuccess )
  234. //{
  235. // var dir = args.Result.ToResult<DriveControlEventArgs.eMoveDir>().Value;
  236. // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
  237. //}
  238. break;
  239. case DriveControlEventArgs.eControlKind.SteeringState:
  240. //if ( args.Result.IsSuccess )
  241. //{
  242. // var dir = args.Result.ToResult<DriveControlEventArgs.eMoveDir>().Value;
  243. // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
  244. //}
  245. break;
  246. case DriveControlEventArgs.eControlKind.ReqCurrentPos:
  247. this.CurrentDrive = args.CurrentPosition;
  248. break;
  249. case DriveControlEventArgs.eControlKind.ReqStopCurrentPos:
  250. break;
  251. case DriveControlEventArgs.eControlKind.FaultReset:
  252. break;
  253. case DriveControlEventArgs.eControlKind.DriveON:
  254. break;
  255. case DriveControlEventArgs.eControlKind.DriveOFF:
  256. break;
  257. case DriveControlEventArgs.eControlKind.JOG:
  258. break;
  259. case DriveControlEventArgs.eControlKind.VehicleState:
  260. ResponseVehicleState(args);
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. }
  267. private void ResponseVehicleState(DriveControlEventArgs args)
  268. {
  269. var state = CastTo<VehicleInfo>.From<object>( args.Args );
  270. this.CurrentDrive = state.CurrentPosition;
  271. }
  272. private void ResponseMove( DriveControlEventArgs args )
  273. {
  274. var msg = string.Empty;
  275. if(args.Result.IsSuccess)
  276. {
  277. msg = "Move Successs";
  278. }
  279. else
  280. {
  281. var error = args.Result.Errors.FirstOrDefault();
  282. var alarm = error.Metadata[ "Alarm" ] as Alarm;
  283. msg = alarm.Name + " " + alarm.Text;
  284. }
  285. this.messageController.ShowNotificationView( msg );
  286. }
  287. void PublishEvent(DriveControlEventArgs args)
  288. {
  289. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish(args);
  290. }
  291. void ChangeSteeringDirection(eSteeringState state)
  292. {
  293. if ( state == eSteeringState.Left )
  294. {
  295. this.SteeringLeftBrushProperty = ( System.Windows.Media.Brush )new System.Windows.Media.BrushConverter().ConvertFromString( "#FF00FFD3" );
  296. this.SteeringRightBrushProperty = Brushes.Gray;
  297. }
  298. else if ( state == eSteeringState.Right )
  299. {
  300. this.SteeringLeftBrushProperty = Brushes.Gray;
  301. this.SteeringRightBrushProperty = ( System.Windows.Media.Brush )new System.Windows.Media.BrushConverter().ConvertFromString( "#FF00FFD3" );
  302. }
  303. else
  304. {
  305. this.SteeringLeftBrushProperty = Brushes.Gray;
  306. this.SteeringRightBrushProperty = Brushes.Gray;
  307. }
  308. }
  309. #region Execute Method
  310. private void ExecuteJogCommand(object obj)
  311. {
  312. var msg = new DriveControlEventArgs
  313. {
  314. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  315. ControlKind = DriveControlEventArgs.eControlKind.JOG,
  316. };
  317. if ( obj.ToString().Equals( "+" ) )
  318. msg.JogDir = DriveControlEventArgs.eJogMoveDir.Positive;
  319. else
  320. msg.JogDir = DriveControlEventArgs.eJogMoveDir.Negative;
  321. this.PublishEvent( msg );
  322. }
  323. private void ExecuteJogVelPopupCommand()
  324. {
  325. var numPad = new CalcuratorView();
  326. var result = numPad.ShowDialog(this.JogVelocity);
  327. this.JogVelocity = result;
  328. }
  329. private void ExecuteSelectedDirection(object obj)
  330. {
  331. this.SelectDirection = obj.ToString();
  332. }
  333. private void ExecuteSteeringCCWCommand(object obj)
  334. {
  335. var msg = new DriveControlEventArgs
  336. {
  337. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  338. ControlKind = DriveControlEventArgs.eControlKind.Steering,
  339. };
  340. if (obj.ToString().Equals("CCW"))
  341. msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
  342. else
  343. msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  344. this.PublishEvent(msg);
  345. }
  346. private void ExecuteSteeringCWCommand(object obj)
  347. {
  348. var msg = new DriveControlEventArgs
  349. {
  350. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  351. ControlKind = DriveControlEventArgs.eControlKind.Steering,
  352. };
  353. if (obj.ToString().Equals("CW"))
  354. msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  355. else
  356. msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
  357. this.PublishEvent(msg);
  358. }
  359. private void ExecutePositionSaveCommand()
  360. {
  361. this.messageController.ShowConfirmationPopupView("Save To Data ?", r =>
  362. {
  363. if (r.Result == ButtonResult.OK)
  364. {
  365. //TODO:[20/03/18 ys-hwang] DB Table Update
  366. }
  367. });
  368. }
  369. private void ExecutePositionDeleteCommand()
  370. {
  371. this.messageController.ShowConfirmationPopupView( "Select To Delete ?" , r =>
  372. {
  373. if ( r.Result == ButtonResult.OK )
  374. {
  375. var deleteList = new List<Route>();
  376. foreach ( var item in this.RouteList )
  377. {
  378. if ( item.IsSelected )
  379. deleteList.Add( item );
  380. }
  381. deleteList.ForEach( x => { this.RouteList.Remove( x ); } );
  382. }
  383. } );
  384. }
  385. private void ExecutePositionAddCommand()
  386. {
  387. this.messageController.ShowConfirmationPopupView( "Position Add ?" , r =>
  388. {
  389. if ( r.Result == ButtonResult.OK )
  390. {
  391. this.RouteList.Add( new Route() );
  392. this.messageController.ShowNotificationView( "Create Success" );
  393. }
  394. } );
  395. }
  396. private void ExecuteKeyInCommadn(object obj)
  397. {
  398. var numPad = new CalcuratorView();
  399. var result = numPad.ShowDialog(this.DriveTargetPos );
  400. this.DriveTargetPos = result;
  401. }
  402. private void ExecuteOriginCommand()
  403. {
  404. this.messageController.ShowConfirmationPopupView( "Origin ?" , r =>
  405. {
  406. if ( r.Result == ButtonResult.OK )
  407. {
  408. //TODO: How to use
  409. }
  410. } );
  411. }
  412. private void ExecuteFaultResetCommand()
  413. {
  414. var msg = new DriveControlEventArgs
  415. {
  416. EventDir = DriveControlEventArgs.eEventDir.ToBack ,
  417. ControlKind = DriveControlEventArgs.eControlKind.FaultReset ,
  418. };
  419. this.PublishEvent( msg );
  420. }
  421. private void ExecuteServoOffCommand()
  422. {
  423. var msg = new DriveControlEventArgs
  424. {
  425. EventDir = DriveControlEventArgs.eEventDir.ToBack ,
  426. ControlKind = DriveControlEventArgs.eControlKind.DriveOFF ,
  427. };
  428. this.PublishEvent( msg );
  429. }
  430. private void ExecuteServoOnCommand()
  431. {
  432. var msg = new DriveControlEventArgs
  433. {
  434. EventDir = DriveControlEventArgs.eEventDir.ToBack ,
  435. ControlKind = DriveControlEventArgs.eControlKind.DriveON ,
  436. };
  437. this.PublishEvent( msg );
  438. }
  439. private void ExecuteCurrentToTargetCommand()
  440. {
  441. this.messageController.ShowConfirmationPopupView( "Current To Target ?" , r =>
  442. {
  443. if ( r.Result == ButtonResult.OK )
  444. {
  445. this.DriveTargetPos = this.CurrentDrive;
  446. }
  447. } );
  448. }
  449. private void ExecuteMoveToCommand()
  450. {
  451. this.messageController.ShowConfirmationPopupView( "Move To Selected Position ?" , r =>
  452. {
  453. if ( r.Result == ButtonResult.OK )
  454. {
  455. //TODO:[20/03/20 ys-hwang] RoutList.IsSelected Check
  456. //if(this.RouteList.Any( x => x.IsSelected != true ))
  457. // return;
  458. var result = this.RouteList.Any( x => x.IsSelected != false );
  459. var ll = this.RouteList.Where(x => x.IsSelected).FirstOrDefault();
  460. var msg = new DriveControlEventArgs
  461. {
  462. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  463. ControlKind = DriveControlEventArgs.eControlKind.MOVE,
  464. TargetRouteID = ll.Id,
  465. };
  466. this.PublishEvent( msg );
  467. }
  468. } );
  469. }
  470. private void ExecuteSelectPosCommand(object obj)
  471. {
  472. this.SelectedPosition = obj.ToString();
  473. }
  474. #endregion
  475. #region Dialog Function
  476. public bool CanCloseDialog()
  477. {
  478. return true;
  479. }
  480. public void OnDialogClosed()
  481. {
  482. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack , ControlKind = DriveControlEventArgs.eControlKind.ReqStopCurrentPos } );
  483. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Unsubscribe( DriveControlCallBack );
  484. }
  485. public void OnDialogOpened(IDialogParameters parameters)
  486. {
  487. var msg = new DriveControlEventArgs
  488. {
  489. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  490. ControlKind = DriveControlEventArgs.eControlKind.SteeringState,
  491. };
  492. this.PublishEvent(msg);
  493. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack , ControlKind = DriveControlEventArgs.eControlKind.VehicleState } );
  494. }
  495. private void CloseDialog(string parameter)
  496. {
  497. ButtonResult result = ButtonResult.None;
  498. if (parameter?.ToLower() == "true")
  499. result = ButtonResult.OK;
  500. else if (parameter?.ToLower() == "false")
  501. result = ButtonResult.Cancel;
  502. RaiseRequestClose(new DialogResult(result));
  503. }
  504. public virtual void RaiseRequestClose(IDialogResult dialogResult)
  505. {
  506. RequestClose?.Invoke(dialogResult);
  507. }
  508. #endregion
  509. }
  510. }