DriveServoViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 _currentPosition;
  56. /// <summary>
  57. /// * Drive Current Position
  58. /// </summary>
  59. public double CurrentPosition
  60. {
  61. get { return this._currentPosition; }
  62. set { this.SetProperty(ref this._currentPosition , value); }
  63. }
  64. private double _differenceDrive;
  65. public double DifferenceDrive
  66. {
  67. get { return this._differenceDrive; }
  68. set { this.SetProperty(ref this._differenceDrive, value); }
  69. }
  70. private double _jogVelocity = 5;
  71. public double JogVelocity
  72. {
  73. get { return this._jogVelocity; }
  74. set { this.SetProperty(ref this._jogVelocity, value); }
  75. }
  76. #endregion
  77. public event Action<IDialogResult> RequestClose;
  78. public ICommand SelectAxisCommand { get; set; }
  79. public ICommand KeyInTargetPosCommand { get; set; }
  80. public ICommand SelectPosCommand { get; set; }
  81. public ICommand MoveToCommand { get; set; }
  82. public ICommand CurrentToTargetCommand { get; set; }
  83. public ICommand ServoOnCommand { get; set; }
  84. public ICommand ServoOffCommand { get; set; }
  85. public ICommand FaultResetCommand { get; set; }
  86. public ICommand OriginCommand { get; set; }
  87. public IEventAggregator eventAggregator;
  88. public ICommand PositionAddCommand { get; set; }
  89. public ICommand PositionDeleteCommand { get; set; }
  90. public ICommand PositionSaveCommand { get; set; }
  91. public ICommand SteeringCWCommand { get; set; }
  92. public ICommand SteeringCCWCommand { get; set; }
  93. public ICommand SelectedDirection { get; set; }
  94. public ICommand JogVelPopupCommand { get; set; }
  95. public ICommand JogCommand { get; set; }
  96. #region Brushes
  97. private Brush steeringLeftBrush = Brushes.Gray;
  98. public Brush SteeringLeftBrushProperty
  99. {
  100. get { return steeringLeftBrush; }
  101. set { SetProperty(ref this.steeringLeftBrush, value); }
  102. }
  103. private Brush steeringRightBrush = Brushes.Gray;
  104. public Brush SteeringRightBrushProperty
  105. {
  106. get { return steeringRightBrush; }
  107. set { SetProperty(ref this.steeringRightBrush, value); }
  108. }
  109. #endregion
  110. private ObservableCollection<Route> _routeList;
  111. public ObservableCollection<Route> RouteList
  112. {
  113. get { return this._routeList; }
  114. set { SetProperty(ref this._routeList, value); }
  115. }
  116. public SqliteDAL.DAL.AxisPositionDataDAL axisPositionDataDal;
  117. SqliteManager sql;
  118. MessageController messageController;
  119. public DriveServoViewModel( IEventAggregator _ea , SqliteManager _sql , MessageController _messageController )
  120. {
  121. this.eventAggregator = _ea;
  122. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Unsubscribe( DriveControlCallBack );
  123. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Subscribe( DriveControlCallBack , ThreadOption.UIThread );
  124. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Unsubscribe( UICallBackCommunication );
  125. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallBackCommunication , ThreadOption.UIThread );
  126. this.sql = _sql;
  127. this.RouteList = new ObservableCollection<Route>( sql.RouteDal.All );
  128. this.messageController = _messageController;
  129. this.SelectPosCommand = new DelegateCommand<object>( ExecuteSelectPosCommand );
  130. this.MoveToCommand = new DelegateCommand( ExecuteMoveToCommand );
  131. this.CurrentToTargetCommand = new DelegateCommand( ExecuteCurrentToTargetCommand );
  132. this.KeyInTargetPosCommand = new DelegateCommand<object>( ExecuteKeyInCommadn );
  133. this.ServoOnCommand = new DelegateCommand( ExecuteServoOnCommand );
  134. this.ServoOffCommand = new DelegateCommand( ExecuteServoOffCommand );
  135. this.FaultResetCommand = new DelegateCommand( ExecuteFaultResetCommand );
  136. this.OriginCommand = new DelegateCommand( ExecuteOriginCommand );
  137. this.PositionAddCommand = new DelegateCommand( ExecutePositionAddCommand );
  138. this.PositionDeleteCommand = new DelegateCommand( ExecutePositionDeleteCommand );
  139. this.PositionSaveCommand = new DelegateCommand( ExecutePositionSaveCommand );
  140. this.SelectedDirection = new DelegateCommand<object>( ExecuteSelectedDirection );
  141. this.SteeringCWCommand = new DelegateCommand<object>( ExecuteSteeringCWCommand );
  142. this.SteeringCCWCommand = new DelegateCommand<object>( ExecuteSteeringCCWCommand );
  143. this.JogVelPopupCommand = new DelegateCommand( ExecuteJogVelPopupCommand );
  144. this.JogCommand = new DelegateCommand<object>( ExecuteJogCommand );
  145. }
  146. private void UICallBackCommunication( GUIMessageEventArgs obj )
  147. {
  148. if ( obj.Kind == GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange )
  149. {
  150. if ( obj.MessageKey.Equals( MessageKey.Vehicle ) )
  151. {
  152. switch ( obj.ModelPropertyName )
  153. {
  154. case "SteeringState":
  155. {
  156. var dir = CastTo<eSteeringState>.From<object>( obj.Args );
  157. this.ChangeSteeringDirection( dir );
  158. }
  159. break;
  160. case "VehicleStateProperty":
  161. {
  162. var v = CastTo<eVehicleState>.From<object>( obj.Args );
  163. this.ChagneVehicleState( v );
  164. }
  165. break;
  166. case "CurrentPosition":
  167. {
  168. var v = CastTo<double>.From<object>(obj.Args);
  169. this.CurrentPosition = v;
  170. }
  171. break;
  172. case "CurrentTag":
  173. break;
  174. case "CurrentSpeed":
  175. break;
  176. case "CurrentTorque":
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. private void ChagneVehicleState( eVehicleState v )
  185. {
  186. switch ( v )
  187. {
  188. case eVehicleState.None:
  189. break;
  190. case eVehicleState.Idle:
  191. break;
  192. case eVehicleState.Move:
  193. break;
  194. case eVehicleState.Load:
  195. break;
  196. case eVehicleState.Unload:
  197. break;
  198. case eVehicleState.Charge:
  199. break;
  200. case eVehicleState.Abnormal:
  201. break;
  202. case eVehicleState.Blocked:
  203. break;
  204. case eVehicleState.Decelerate:
  205. break;
  206. }
  207. }
  208. private void DriveControlCallBack( DriveControlEventArgs args)
  209. {
  210. if (args.EventDir == DriveControlEventArgs.eEventDir.ToFront)
  211. {
  212. switch ( args.ControlKind )
  213. {
  214. case DriveControlEventArgs.eControlKind.NONE:
  215. break;
  216. case DriveControlEventArgs.eControlKind.MOVE:
  217. ResponseMove(args);
  218. break;
  219. case DriveControlEventArgs.eControlKind.STOP:
  220. break;
  221. case DriveControlEventArgs.eControlKind.Steering:
  222. //if ( args.Result.IsSuccess )
  223. //{
  224. // var dir = args.Result.ToResult<DriveControlEventArgs.eMoveDir>().Value;
  225. // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
  226. //}
  227. break;
  228. case DriveControlEventArgs.eControlKind.SteeringState:
  229. //if ( args.Result.IsSuccess )
  230. //{
  231. // var dir = args.Result.ToResult<DriveControlEventArgs.eMoveDir>().Value;
  232. // this.ChangeSteeringDirection( dir == DriveControlEventArgs.eMoveDir.LEFT ? true : false );
  233. //}
  234. break;
  235. case DriveControlEventArgs.eControlKind.ReqCurrentPos:
  236. this.CurrentPosition = args.CurrentPosition;
  237. break;
  238. case DriveControlEventArgs.eControlKind.ReqStopCurrentPos:
  239. break;
  240. case DriveControlEventArgs.eControlKind.FaultReset:
  241. break;
  242. case DriveControlEventArgs.eControlKind.DriveON:
  243. break;
  244. case DriveControlEventArgs.eControlKind.DriveOFF:
  245. break;
  246. case DriveControlEventArgs.eControlKind.JOG:
  247. break;
  248. case DriveControlEventArgs.eControlKind.VehicleState:
  249. ResponseVehicleState(args);
  250. break;
  251. default:
  252. break;
  253. }
  254. }
  255. }
  256. private void ResponseVehicleState(DriveControlEventArgs args)
  257. {
  258. var state = CastTo<VehicleInfo>.From<object>( args.Args );
  259. this.CurrentPosition = state.CurrentPosition;
  260. }
  261. private void ResponseMove( DriveControlEventArgs args )
  262. {
  263. var msg = string.Empty;
  264. if(args.Result.IsSuccess)
  265. {
  266. msg = "Move Successs";
  267. }
  268. else
  269. {
  270. var error = args.Result.Errors.FirstOrDefault();
  271. var alarm = error.Metadata[ "Alarm" ] as Alarm;
  272. msg = alarm.Name + " " + alarm.Text;
  273. }
  274. this.messageController.ShowNotificationView( msg );
  275. }
  276. void PublishEvent(DriveControlEventArgs args)
  277. {
  278. args.EventDir = DriveControlEventArgs.eEventDir.ToBack;
  279. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish(args);
  280. }
  281. void ChangeSteeringDirection(eSteeringState state)
  282. {
  283. if ( state == eSteeringState.Left )
  284. {
  285. this.SteeringLeftBrushProperty = ( Brush )new BrushConverter().ConvertFromString( "#FF00FFD3" );
  286. this.SteeringRightBrushProperty = Brushes.Gray;
  287. }
  288. else if ( state == eSteeringState.Right )
  289. {
  290. this.SteeringLeftBrushProperty = Brushes.Gray;
  291. this.SteeringRightBrushProperty = ( Brush )new BrushConverter().ConvertFromString( "#FF00FFD3" );
  292. }
  293. else
  294. {
  295. this.SteeringLeftBrushProperty = Brushes.Gray;
  296. this.SteeringRightBrushProperty = Brushes.Gray;
  297. }
  298. }
  299. #region Execute Method
  300. private void ExecuteJogCommand(object obj)
  301. {
  302. if ( this.JogVelocity <= 0 )
  303. {
  304. this.messageController.ShowNotificationView( "Check Jog Velocity" );
  305. return;
  306. }
  307. var msg = new DriveControlEventArgs
  308. {
  309. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  310. ControlKind = DriveControlEventArgs.eControlKind.JOG,
  311. };
  312. if ( obj.ToString().Equals( "+" ) )
  313. msg.JogDir = DriveControlEventArgs.eJogMoveDir.Positive;
  314. else
  315. msg.JogDir = DriveControlEventArgs.eJogMoveDir.Negative;
  316. this.PublishEvent( msg );
  317. }
  318. private void ExecuteJogVelPopupCommand()
  319. {
  320. var numPad = new CalcuratorView();
  321. var result = numPad.ShowDialog(this.JogVelocity);
  322. this.JogVelocity = result;
  323. }
  324. private void ExecuteSelectedDirection(object obj)
  325. {
  326. this.SelectDirection = obj.ToString();
  327. }
  328. private void ExecuteSteeringCCWCommand(object obj)
  329. {
  330. var msg = new DriveControlEventArgs
  331. {
  332. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  333. ControlKind = DriveControlEventArgs.eControlKind.Steering,
  334. };
  335. if (obj.ToString().Equals("CCW"))
  336. msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
  337. else
  338. msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  339. this.PublishEvent(msg);
  340. }
  341. private void ExecuteSteeringCWCommand(object obj)
  342. {
  343. var msg = new DriveControlEventArgs
  344. {
  345. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  346. ControlKind = DriveControlEventArgs.eControlKind.Steering,
  347. };
  348. if (obj.ToString().Equals("CW"))
  349. msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  350. else
  351. msg.MoveDir = DriveControlEventArgs.eMoveDir.RIGHT;
  352. this.PublishEvent(msg);
  353. }
  354. private void ExecutePositionSaveCommand()
  355. {
  356. this.messageController.ShowConfirmationPopupView("Save To Data ?", r =>
  357. {
  358. if (r.Result == ButtonResult.OK)
  359. {
  360. var result = this.RouteList.Any( x => x.IsSelected != false );
  361. if ( !result )
  362. {
  363. this.messageController.ShowNotificationView( "Pos Not Selected" );
  364. return;
  365. }
  366. var ll = this.RouteList.Where( x => x.IsSelected ).FirstOrDefault();
  367. }
  368. });
  369. }
  370. private void ExecutePositionDeleteCommand()
  371. {
  372. this.messageController.ShowConfirmationPopupView( "Select To Delete ?" , r =>
  373. {
  374. if ( r.Result == ButtonResult.OK )
  375. {
  376. var deleteList = new List<Route>();
  377. foreach ( var item in this.RouteList )
  378. {
  379. if ( item.IsSelected )
  380. deleteList.Add( item );
  381. }
  382. deleteList.ForEach( x => { this.RouteList.Remove( x ); } );
  383. }
  384. } );
  385. }
  386. private void ExecutePositionAddCommand()
  387. {
  388. this.messageController.ShowConfirmationPopupView( "Position Add ?" , r =>
  389. {
  390. if ( r.Result == ButtonResult.OK )
  391. {
  392. this.RouteList.Add( new Route() );
  393. this.messageController.ShowNotificationView( "Create Success" );
  394. }
  395. } );
  396. }
  397. private void ExecuteKeyInCommadn(object obj)
  398. {
  399. var numPad = new CalcuratorView();
  400. var result = numPad.ShowDialog(this.DriveTargetPos );
  401. this.DriveTargetPos = result;
  402. }
  403. private void ExecuteOriginCommand()
  404. {
  405. this.messageController.ShowConfirmationPopupView( "Origin ?" , r =>
  406. {
  407. if ( r.Result == ButtonResult.OK )
  408. {
  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.CurrentPosition;
  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. var result = this.RouteList.Any( x => x.IsSelected != false );
  456. if ( !result )
  457. {
  458. this.messageController.ShowNotificationView( "Pos Not Selected" );
  459. return;
  460. }
  461. var ll = this.RouteList.Where(x => x.IsSelected).FirstOrDefault();
  462. var msg = new DriveControlEventArgs
  463. {
  464. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  465. ControlKind = DriveControlEventArgs.eControlKind.MOVE,
  466. TargetRouteID = ll.Id,
  467. };
  468. this.PublishEvent( msg );
  469. }
  470. } );
  471. }
  472. private void ExecuteSelectPosCommand(object obj)
  473. {
  474. this.SelectedPosition = obj.ToString();
  475. }
  476. #endregion
  477. #region Dialog Function
  478. public bool CanCloseDialog()
  479. {
  480. return true;
  481. }
  482. public void OnDialogClosed()
  483. {
  484. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack , ControlKind = DriveControlEventArgs.eControlKind.ReqStopCurrentPos } );
  485. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Unsubscribe( DriveControlCallBack );
  486. }
  487. public void OnDialogOpened(IDialogParameters parameters)
  488. {
  489. var msg = new DriveControlEventArgs
  490. {
  491. EventDir = DriveControlEventArgs.eEventDir.ToBack,
  492. ControlKind = DriveControlEventArgs.eControlKind.SteeringState,
  493. };
  494. this.PublishEvent(msg);
  495. this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( new DriveControlEventArgs { EventDir = DriveControlEventArgs.eEventDir.ToBack , ControlKind = DriveControlEventArgs.eControlKind.VehicleState } );
  496. }
  497. private void CloseDialog(string parameter)
  498. {
  499. ButtonResult result = ButtonResult.None;
  500. if (parameter?.ToLower() == "true")
  501. result = ButtonResult.OK;
  502. else if (parameter?.ToLower() == "false")
  503. result = ButtonResult.Cancel;
  504. RaiseRequestClose(new DialogResult(result));
  505. }
  506. public virtual void RaiseRequestClose(IDialogResult dialogResult)
  507. {
  508. RequestClose?.Invoke(dialogResult);
  509. }
  510. #endregion
  511. }
  512. }