D_MainWindowViewModel.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. using CommonServiceLocator;
  2. using GSG.NET.Extensions;
  3. using GSG.NET.Logging;
  4. using GSG.NET.Utils;
  5. using OHV.Common.Events;
  6. using OHV.Common.Model;
  7. using OHV.Common.Shareds;
  8. using OHV.LanguageHelper;
  9. using OHV.Module.Interactivity;
  10. using OHV.SqliteDAL;
  11. using Prism.Commands;
  12. using Prism.Events;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using Prism.Services.Dialogs;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Globalization;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using System.Windows;
  24. using System.Windows.Input;
  25. using System.Windows.Interop;
  26. using System.Windows.Media;
  27. using System.Windows.Threading;
  28. using VehicleControlSystem;
  29. namespace OHV.Vehicle.Concept
  30. {
  31. public enum eBatteryState
  32. {
  33. DisConnect,
  34. Connect,
  35. Charge,
  36. }
  37. class D_MainWindowViewModel : BindableBase
  38. {
  39. #region Properties
  40. private string _title = "Prism Unity Application";
  41. public string Title
  42. {
  43. get { return _title; }
  44. set { SetProperty( ref _title, value ); }
  45. }
  46. private double _cpu;
  47. public double CPU
  48. {
  49. get { return this._cpu; }
  50. set { this.SetProperty( ref this._cpu, value ); }
  51. }
  52. private double _totalCPU;
  53. public double TotalCPU
  54. {
  55. get { return this._totalCPU; }
  56. set
  57. {
  58. //this._totalCPU = dti.cpuResult.ToString();
  59. this.SetProperty( ref this._totalCPU, value );
  60. }
  61. }
  62. public int UsageCPU { get; set; }
  63. private double _ram;
  64. public double RAM
  65. {
  66. get { return this._ram; }
  67. set { this.SetProperty( ref this._ram, value ); }
  68. }
  69. private double _cDrive;
  70. public double CDrive
  71. {
  72. get { return this._cDrive; }
  73. set { this.SetProperty( ref this._cDrive, value ); }
  74. }
  75. DateTime _dateTime;
  76. public DateTime DateTime
  77. {
  78. get { return _dateTime; }
  79. set
  80. {
  81. this.SetProperty( ref _dateTime, value );
  82. }
  83. }
  84. private string _vehicleID;
  85. public string VehicleID
  86. {
  87. get { return this._vehicleID; }
  88. set { this.SetProperty( ref this._vehicleID, value ); }
  89. }
  90. private string _vehicleIP = "127.0.0.1";
  91. public string VehicleIP
  92. {
  93. get { return this._vehicleIP; }
  94. set { this.SetProperty( ref this._vehicleIP, value ); }
  95. }
  96. private string _ocsIP;
  97. public string OcsIP
  98. {
  99. get { return this._ocsIP; }
  100. set { this.SetProperty( ref this._ocsIP, value ); }
  101. }
  102. private string _swVersion;
  103. public string SwVersion
  104. {
  105. get { return this._swVersion; }
  106. set { this.SetProperty( ref this._swVersion, value ); }
  107. }
  108. bool _startEnable = true;
  109. public bool StartEnable
  110. {
  111. get { return this._startEnable; }
  112. set { this.SetProperty( ref this._startEnable, value ); }
  113. }
  114. bool _stopEnable = false;
  115. public bool StopEnable
  116. {
  117. get { return this._stopEnable; }
  118. set { this.SetProperty( ref this._stopEnable, value ); }
  119. }
  120. private DateTime _lastBuildedTime;
  121. public DateTime LastBuildedTime
  122. {
  123. get { return this._lastBuildedTime; }
  124. set { this.SetProperty( ref this._lastBuildedTime, value ); }
  125. }
  126. private bool isManualMode = true;
  127. public bool IsManualMode
  128. {
  129. get { return isManualMode; }
  130. set { SetProperty( ref isManualMode, value ); }
  131. }
  132. private bool isVehicleAlarm = false;
  133. public bool IsVehicleAlarm
  134. {
  135. get { return isVehicleAlarm; }
  136. set { SetProperty( ref this.isVehicleAlarm, value ); }
  137. }
  138. private eMachineMode machineMode = eMachineMode.LocalMode;
  139. public eMachineMode MachineMode
  140. {
  141. get { return machineMode; }
  142. set { SetProperty( ref this.machineMode, value ); }
  143. }
  144. eOcsState ocsState = eOcsState.DisConnect;
  145. public eOcsState OcsState
  146. {
  147. get { return this.ocsState; }
  148. set { this.SetProperty( ref this.ocsState, value ); }
  149. }
  150. eBatteryState batteryState = eBatteryState.DisConnect;
  151. public eBatteryState BatteryState
  152. {
  153. get { return this.batteryState; }
  154. set { this.SetProperty( ref this.batteryState, value ); }
  155. }
  156. string currentTag = "None";
  157. public string CurrentTag
  158. {
  159. get { return this.currentTag; }
  160. set { this.SetProperty( ref this.currentTag, value ); }
  161. }
  162. private bool isDryRun;
  163. public bool IsDryRun
  164. {
  165. get { return isDryRun; }
  166. set { SetProperty( ref this.isDryRun, value ); }
  167. }
  168. private string currentMCR = "None";
  169. public string CurrentMCR
  170. {
  171. get { return currentMCR; }
  172. set { SetProperty( ref this.currentMCR, value ); }
  173. }
  174. private bool isDriveConnected = false;
  175. public bool IsDriveConnected
  176. {
  177. get { return isDriveConnected; }
  178. set { SetProperty( ref this.isDriveConnected, value ); }
  179. }
  180. private bool isOnRoute;
  181. public bool IsOnRoute
  182. {
  183. get { return isOnRoute; }
  184. set { SetProperty(ref this.isOnRoute, value); }
  185. }
  186. private bool isBusy;
  187. public bool IsBusy
  188. {
  189. get { return isBusy; }
  190. set { SetProperty( ref this.isBusy, value ); }
  191. }
  192. private int timeOutLimit;
  193. public int TimeOutLimit
  194. {
  195. get { return timeOutLimit; }
  196. set { SetProperty( ref this.timeOutLimit, value ); }
  197. }
  198. private string busyText;
  199. public string BusyText
  200. {
  201. get { return busyText; }
  202. set { SetProperty( ref this.busyText, value ); }
  203. }
  204. eVehicleOnRailState vehicleOnRailState = eVehicleOnRailState.None;
  205. public eVehicleOnRailState VehicleOnRailState
  206. {
  207. get => this.vehicleOnRailState;
  208. set => SetProperty(ref this.vehicleOnRailState, value);
  209. }
  210. #endregion
  211. #region Brushes
  212. private Brush _ocsBrush;
  213. public Brush OcsBrush
  214. {
  215. get { return _ocsBrush; }
  216. set
  217. {
  218. SetProperty( ref _ocsBrush, value );
  219. }
  220. }
  221. private Brush batteryBrush;
  222. public Brush BatteryBrush
  223. {
  224. get { return batteryBrush; }
  225. set
  226. {
  227. SetProperty( ref batteryBrush, value );
  228. }
  229. }
  230. double soc;
  231. public double SOC
  232. {
  233. get { return this.soc; }
  234. set { this.SetProperty( ref this.soc, value ); }
  235. }
  236. #endregion
  237. #region Commands
  238. public ICommand TestCommand { get; set; }
  239. public ICommand NavigateCommand { get; set; }
  240. public ICommand SystemOffCommand { get; set; }
  241. public ICommand BuzzerStopCommand { get; set; }
  242. public ICommand EmergencyStopCommand { get; set; }
  243. public ICommand StartCommand { get; set; }
  244. public ICommand StopCommand { get; set; }
  245. public ICommand AlarmResetCommand { get; set; }
  246. public ICommand ChangeLanguage { get; set; }
  247. public ICommand MachineModeChgCommand { get; set; }
  248. public ICommand BatteryPopup { get; set; }
  249. public ICommand VehicleRailOnStateChgCommand { get; set; }
  250. public ICommand SelectLanguageCommand { get; set; }
  251. #endregion
  252. static Logger logger = Logger.GetLogger();
  253. private AutoResetEvent eventBusy = new AutoResetEvent( false );
  254. IEventAggregator eventAggregator = null;
  255. IRegionManager regionManager;
  256. VCSystem VCSystem = null;
  257. MessageController messageController;
  258. VCSMessagePubSubEvent vcsMessagePublisher;
  259. SqliteManager sql;
  260. public D_MainWindowViewModel( IEventAggregator _ea, VCSystem cSystem, IRegionManager _regionManager, MessageController _msgController, SqliteManager _sql )
  261. {
  262. this.regionManager = _regionManager;
  263. this.eventAggregator = _ea;
  264. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallbackCommunication, ThreadOption.UIThread );
  265. this.eventAggregator.GetEvent<HostConnectedPubSubEvent>().Subscribe( OcsConnectCallBack, ThreadOption.UIThread, false );
  266. vcsMessagePublisher = this.eventAggregator.GetEvent<VCSMessagePubSubEvent>();
  267. this.VCSystem = cSystem;
  268. this.messageController = _msgController;
  269. this.sql = _sql;
  270. this.VehicleID = sql.ConfigDal.GetById( ConstString.VehicleID ).Value;
  271. this.OcsIP = sql.ConfigDal.GetById( ConstString.Addr ).Value;
  272. this.TestCommand = new DelegateCommand( ExecuteTextCommand );
  273. this.NavigateCommand = new DelegateCommand<object>( this.Navigate );
  274. this.SystemOffCommand = new DelegateCommand( ExecuteSystemOffCommand );
  275. this.ChangeLanguage = new DelegateCommand( Execte_ChangeLanguage );
  276. this.StartCommand = new DelegateCommand( ExecuteStartCommand );
  277. this.StopCommand = new DelegateCommand( ExecuteStopCommand );
  278. this.AlarmResetCommand = new DelegateCommand( ExecuteAlarmResetCommand );
  279. this.EmergencyStopCommand = new DelegateCommand( ExecuteEStop );
  280. this.BuzzerStopCommand = new DelegateCommand( ExecuteBuzzerStop );
  281. this.MachineModeChgCommand = new DelegateCommand<string>( ExecuteMachineModeChgCommand );
  282. this.BatteryPopup = new DelegateCommand( ExecuteBatteryPopup );
  283. this.VehicleRailOnStateChgCommand = new DelegateCommand(ExecuteVehicleRailOnStateChgCommand);
  284. this.SelectLanguageCommand = new DelegateCommand(ExecuteSelectLanguageCommand);
  285. DispatcherTimer dateTimer = new DispatcherTimer();
  286. dateTimer.Tick += ( object sender, EventArgs e ) =>
  287. {
  288. this.DateTime = DateTime.Now;
  289. };
  290. dateTimer.Interval = TimeSpan.FromMilliseconds( 500 );
  291. dateTimer.Start();
  292. this.SwVersion = AssemblyUtils.GetVersion();
  293. this.LastBuildedTime = new Helpler.AssemblyInfo().Get_BuildDateTime();
  294. GSG.NET.Quartz.QuartzUtils.Invoke( "RESOURCE_CHECK", GSG.NET.Quartz.QuartzUtils.GetExpnSecond( 1 ), QuzOnResourceUsage );
  295. this.IsDryRun = false;
  296. }
  297. private void ExecuteSelectLanguageCommand()
  298. {
  299. this.messageController.ShowPopupView(ePopupType.SelectLanguageView);
  300. }
  301. private void ZmqManager_PropertyChanged( object sender, System.ComponentModel.PropertyChangedEventArgs e )
  302. {
  303. var property = sender.GetType().GetProperty( e.PropertyName );
  304. var newValue = property.GetValue( sender, null );
  305. switch ( e.PropertyName )
  306. {
  307. case "CurrentMCR":
  308. this.CurrentMCR = CastTo<string>.From<object>( newValue );
  309. break;
  310. case "CurrentPointNo":
  311. this.CurrentTag = CastTo<int>.From<object>( newValue ).ToString();
  312. break;
  313. case "IsReqConnected":
  314. this.IsDriveConnected = CastTo<bool>.From<object>( newValue );
  315. break;
  316. case "IsCanStanbyLocation":
  317. this.IsOnRoute = CastTo<bool>.From<object>(newValue);
  318. break;
  319. default:
  320. break;
  321. }
  322. }
  323. private void ExecuteBatteryPopup()
  324. {
  325. this.messageController.ShowPopupView( Common.Shareds.ePopupType.BatteryConfigView );
  326. }
  327. void QuzOnResourceUsage()
  328. {
  329. try
  330. {
  331. this.CPU = GSG.NET.OSView.Mgnt.CpuUseRate();
  332. this.RAM = GSG.NET.OSView.Mgnt.MemPhysicalUseRate();
  333. var ll = GSG.NET.OSView.Mgnt.HddList();
  334. this.CDrive = ll.FirstOrDefault().AvailableFreeSpace / ConstUtils.ONE_GIGA_BYTES;
  335. }
  336. catch (Exception ex)
  337. {
  338. logger.E(ex);
  339. }
  340. }
  341. private void ExecuteVehicleRailOnStateChgCommand()
  342. {
  343. var msg = new VCSMessageEventArgs
  344. {
  345. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleOnRailStateChg,
  346. };
  347. if (this.VehicleOnRailState == eVehicleOnRailState.Removed)
  348. msg.Arg = eVehicleOnRailState.Install;
  349. else
  350. msg.Arg = eVehicleOnRailState.Removed;
  351. var question = string.Format(LanguageHalper.LanguagesSelecter.GetStringFromResourceDic(Question.VehicleOnRailStateChange) + $" {msg.Arg} ?");
  352. this.messageController.ShowConfirmationPopupView(question, r =>
  353. {
  354. if (r.Result == ButtonResult.OK)
  355. vcsMessagePublisher.Publish(msg);
  356. });
  357. }
  358. private void ExecuteMachineModeChgCommand( string obj )
  359. {
  360. var msg = new VCSMessageEventArgs
  361. {
  362. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqMachineModeChg,
  363. };
  364. if ( this.MachineMode == eMachineMode.LocalMode )
  365. msg.Arg = eMachineMode.HostMode;
  366. else
  367. msg.Arg = eMachineMode.LocalMode;
  368. var question = string.Format(LanguageHalper.LanguagesSelecter.GetStringFromResourceDic(Question.VehicleModeChange) + $" {msg.Arg} ?");
  369. this.messageController.ShowConfirmationPopupView( question, r =>
  370. {
  371. if ( r.Result == ButtonResult.OK )
  372. vcsMessagePublisher.Publish( msg );
  373. } );
  374. }
  375. private void ExecuteBuzzerStop()
  376. {
  377. var msg = new VCSMessageEventArgs
  378. {
  379. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqBuzzerStop,
  380. Args = new Dictionary<string, object>(),
  381. };
  382. vcsMessagePublisher.Publish( msg );
  383. //this.ExecuteTextCommand();
  384. }
  385. private void Execte_ChangeLanguage()
  386. {
  387. //LanguageHalper.LanguagesSelcter.ChangLanguage(LanguageHalper.eLanguageType.Chinese);
  388. }
  389. private void ExecuteEStop()
  390. {
  391. this.IsDryRun = !this.IsDryRun;
  392. var msg = new VCSMessageEventArgs
  393. {
  394. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqEStop,
  395. };
  396. vcsMessagePublisher.Publish( msg );
  397. }
  398. private void ExecuteAlarmResetCommand()
  399. {
  400. this.messageController.ShowConfirmationPopupView( " Alarm Reset ? ", r =>
  401. {
  402. if ( r.Result == ButtonResult.OK )
  403. {
  404. var msg = new VCSMessageEventArgs
  405. {
  406. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqAlarmReset,
  407. };
  408. vcsMessagePublisher.Publish( msg );
  409. }
  410. } );
  411. }
  412. private void ExecuteStopCommand()
  413. {
  414. this.messageController.ShowConfirmationPopupView( LanguageHalper.LanguagesSelecter.GetStringFromResourceDic(Question.VehicleManualMode), r =>
  415. {
  416. if ( r.Result == ButtonResult.OK )
  417. {
  418. var msg = new VCSMessageEventArgs
  419. {
  420. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  421. MessageKey = MessageKey.ManualMode,
  422. };
  423. vcsMessagePublisher.Publish( msg );
  424. this.SetBusy( 5, "Wait for Vehicle Mode Change ..." );
  425. this.VCSystem.ZmqManager.GetCurrentMCR();
  426. }
  427. } );
  428. }
  429. private void ExecuteStartCommand()
  430. {
  431. //this.TimeOutLimit = 5000;
  432. //this.IsBusy = true;
  433. if (this.VehicleOnRailState != eVehicleOnRailState.Install)
  434. {
  435. this.messageController.ShowNotificationView(LanguageHalper.LanguagesSelecter.GetStringFromResourceDic(Notification.AutoModeChangeWranOnRailState), false);
  436. return;
  437. }
  438. this.messageController.ShowConfirmationPopupView( LanguageHalper.LanguagesSelecter.GetStringFromResourceDic(Question.VehicleAutoMode), r =>
  439. {
  440. if ( r.Result == ButtonResult.OK )
  441. {
  442. var msg = new VCSMessageEventArgs
  443. {
  444. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  445. MessageKey = MessageKey.AutoMode,
  446. };
  447. vcsMessagePublisher.Publish( msg );
  448. this.SetBusy( 15, "Wait for Vehicle Mode Change ...");
  449. }
  450. } );
  451. }
  452. private void OcsConnectCallBack( HostConnectedEventArgs obj )
  453. {
  454. var state = obj.State;
  455. switch ( state )
  456. {
  457. case HostConnectedEventArgs.eConnectedState.Disconnected:
  458. this.OcsState = eOcsState.DisConnect;
  459. break;
  460. case HostConnectedEventArgs.eConnectedState.Connected:
  461. this.OcsState = eOcsState.Connect;
  462. break;
  463. }
  464. }
  465. private void ExecuteSystemOffCommand()
  466. {
  467. this.messageController.ShowConfirmationPopupView( LanguageHalper.LanguagesSelecter.GetStringFromResourceDic(Question.ProgramExit), r =>
  468. {
  469. if ( r.Result == ButtonResult.OK )
  470. App.Current.Shutdown();
  471. } );
  472. }
  473. private void Navigate( object obj )
  474. {
  475. var selectItem = obj.ToString();
  476. if ( !string.IsNullOrEmpty( selectItem ) )
  477. regionManager.RequestNavigate( "MainView", selectItem );
  478. }
  479. void VehicleModeChange( bool isAutoMode )
  480. {
  481. if ( isAutoMode )
  482. {
  483. this.StartEnable = false;
  484. this.StopEnable = true;
  485. this.IsManualMode = false;
  486. regionManager.RequestNavigate( RegionNames.MainView, "AutoView" );
  487. }
  488. else
  489. {
  490. this.StartEnable = true;
  491. this.StopEnable = false;
  492. this.IsManualMode = true;
  493. }
  494. }
  495. private void UICallbackCommunication( GUIMessageEventArgs obj )
  496. {
  497. switch ( obj.Kind )
  498. {
  499. case GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange:
  500. this.UICallBackModelPropertyChange( obj );
  501. break;
  502. case GUIMessageEventArgs.eGUIMessageKind.RspIOObject:
  503. break;
  504. case GUIMessageEventArgs.eGUIMessageKind.RspIOMapList:
  505. break;
  506. case GUIMessageEventArgs.eGUIMessageKind.RspCommandList:
  507. break;
  508. case GUIMessageEventArgs.eGUIMessageKind.RspAutoModeChange:
  509. break;
  510. case GUIMessageEventArgs.eGUIMessageKind.RspManualModeChange:
  511. break;
  512. case GUIMessageEventArgs.eGUIMessageKind.RspAlarmReset:
  513. break;
  514. case GUIMessageEventArgs.eGUIMessageKind.RspVehicleModeChange:
  515. this.RspVehicleModeChange( obj );
  516. break;
  517. case GUIMessageEventArgs.eGUIMessageKind.RspEStop:
  518. break;
  519. case GUIMessageEventArgs.eGUIMessageKind.RspVihicleState:
  520. break;
  521. case GUIMessageEventArgs.eGUIMessageKind.RspMachineModeChg:
  522. RspMachineModeChg( obj );
  523. break;
  524. case GUIMessageEventArgs.eGUIMessageKind.RspVehicleOnRailStateChg:
  525. RspVehicleOnRailStateChg(obj);
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. private void RspVehicleOnRailStateChg(GUIMessageEventArgs obj)
  532. {
  533. string reason = string.Empty;
  534. FluentResults.Result result = obj.Result;
  535. if (obj.Result.IsFailed)
  536. {
  537. reason = result.Errors.FirstOrDefault().Message;
  538. }
  539. else
  540. {
  541. var mode = CastTo<eVehicleOnRailState>.From<object>(obj.Args);
  542. reason = $"Change To {mode} Successes";
  543. }
  544. this.messageController.ShowNotificationView(reason);
  545. }
  546. void RspBatteryConnected( GUIMessageEventArgs obj )
  547. {
  548. var connected = CastTo<bool>.From<object>( obj.Args );
  549. if ( connected )
  550. {
  551. this.BatteryBrush = Brushes.LimeGreen;
  552. BatteryState = eBatteryState.Connect;
  553. }
  554. else
  555. {
  556. this.BatteryBrush = Brushes.Gray;
  557. BatteryState = eBatteryState.DisConnect;
  558. }
  559. }
  560. private void RspMachineModeChg( GUIMessageEventArgs obj )
  561. {
  562. string reason = string.Empty;
  563. FluentResults.Result result = obj.Result;
  564. if ( obj.Result.IsFailed )
  565. {
  566. reason = result.Errors.FirstOrDefault().Message;
  567. }
  568. else
  569. {
  570. var mode = CastTo<eMachineMode>.From<object>( obj.Args );
  571. reason = $"Change To {mode} Successes";
  572. }
  573. this.messageController.ShowNotificationView( reason );
  574. }
  575. private void RspVehicleModeChange( GUIMessageEventArgs obj )
  576. {
  577. //this.IsBusy = false;
  578. this.ResetBusy();
  579. if ( obj.Result.IsSuccess )
  580. {
  581. if ( obj.MessageKey.Equals( MessageKey.AutoMode ) ) { this.VehicleModeChange( true ); }
  582. if ( obj.MessageKey.Equals( MessageKey.ManualMode ) ) { this.VehicleModeChange( false ); }
  583. }
  584. else
  585. {
  586. var msg = obj.Result.Errors.FirstOrDefault().Message;
  587. if (msg.Equals("NotOnRoute"))
  588. {
  589. var hisAlarm = new HisAlarm()
  590. {
  591. AlarmId = 999,
  592. Text = "Vehicle is not On Route! (自动驾驶无法变更的位置)",
  593. Solution = "Use a Jog to Move the Vehicle Onto the OnRoute(Jog Menu = Contol -> Drive ) \n" +
  594. "利用小号移动车辆移动到 OnRoute 变成绿色为止. 如果Servo Off,请先做Servo On \n" +
  595. "移动时,请给我位置的直线",
  596. };
  597. this.messageController.ShowNotificationAlarmView(hisAlarm);
  598. return;
  599. }
  600. this.messageController.ShowNotificationView( msg, false );
  601. }
  602. }
  603. void UICallBackModelPropertyChange( GUIMessageEventArgs args )
  604. {
  605. if ( args.MessageKey.Equals( MessageKey.Alarm ) )
  606. {
  607. var hisAlarm = args.Args as HisAlarm;
  608. this.VehicleModeChange( false );//알람이 발생하면 자동으로 Manual Mode 로 변경됨
  609. this.IsVehicleAlarm = true;
  610. this.ResetBusy(); // 자동모드 진행 중 알람이 생기면 자동으로 Busy 상태를 빠져 나온다. UI
  611. this.messageController.ShowNotificationAlarmView( CastTo<HisAlarm>.From<object>(args.Args));
  612. }
  613. if ( args.MessageKey.Equals( MessageKey.Vehicle ) )
  614. {
  615. switch ( args.ModelPropertyName )
  616. {
  617. case "VehicleStateProperty":
  618. {
  619. var vehicleState = CastTo<eVehicleState>.From<object>( args.Args );
  620. if ( vehicleState == eVehicleState.Abnormal )
  621. this.IsVehicleAlarm = true;
  622. else
  623. this.IsVehicleAlarm = false;
  624. }
  625. break;
  626. case "IsErrorProcessing":
  627. {
  628. var ll = CastTo<bool>.From<object>( args.Args );
  629. }
  630. break;
  631. case "MachineMode":
  632. this.MachineMode = CastTo<eMachineMode>.From<object>( args.Args );
  633. break;
  634. case "VehicleOnRailState":
  635. this.VehicleOnRailState = CastTo<eVehicleOnRailState>.From<object>(args.Args);
  636. break;
  637. case "BatteryIsConnect":
  638. this.RspBatteryConnected( args );
  639. break;
  640. case "BatteryStateOfCharge":
  641. this.SOC = CastTo<double>.From<object>( args.Args );
  642. break;
  643. case "BatteryChargeTime":
  644. {
  645. var cTime = CastTo<double>.From<object>( args.Args );
  646. //if ( cTime > 0 )
  647. // this.BatteryState = eBatteryState.Charge;
  648. //else
  649. // this.BatteryState = eBatteryState.Connect;
  650. }
  651. break;
  652. case "BatteryChargeState":
  653. {
  654. var isCharge = CastTo<bool>.From<object>( args.Args );
  655. if ( isCharge )
  656. this.BatteryState = eBatteryState.Charge;
  657. else
  658. this.BatteryState = eBatteryState.Connect;
  659. }
  660. break;
  661. case "BatteryDisChargeTime":
  662. {
  663. var dTime = CastTo<double>.From<object>( args.Args );
  664. }
  665. break;
  666. default:
  667. break;
  668. }
  669. }
  670. }
  671. private void ExecuteTextCommand()
  672. {
  673. var m = new VCSMessageEventArgs();
  674. m.MessageKey = "";
  675. m.MessageText = "Test Message";
  676. m.Kind = VCSMessageEventArgs.eVCSMessageKind.ReqTest;
  677. m.Command = new Common.Model.Command() { CommandID = "TestCommand123", Type = Common.Shareds.eCommandType.Move };
  678. this.eventAggregator.GetEvent<VCSMessagePubSubEvent>().Publish( m );
  679. }
  680. public void InitViewModel()
  681. {
  682. //VCSystem.Instance.Init();
  683. //var containerRegistry = ServiceLocator.Current.GetInstance<VCSystem>();
  684. //if ( containerRegistry.Equals( this.VCSystem ) )
  685. //{
  686. // Console.WriteLine( "==" );
  687. //}
  688. //var vsys = containerRegistry.GetContainer().Resolve<IContainerProvider>();
  689. regionManager.RequestNavigate( RegionNames.MainView, "AutoView" );
  690. //var msg = new DriveControlEventArgs
  691. //{
  692. // EventDir = DriveControlEventArgs.eEventDir.ToBack,
  693. // ControlKind = DriveControlEventArgs.eControlKind.Steering,
  694. //};
  695. //msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  696. //this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( msg );
  697. this.VCSystem.ZmqManager.PropertyChanged += ZmqManager_PropertyChanged;
  698. this.IsDriveConnected = this.VCSystem.ZmqManager.IsReqConnected;
  699. this.CurrentMCR = this.VCSystem.ZmqManager.CurrentMCR;
  700. this.CurrentTag = this.VCSystem.ZmqManager.CurrentPointNo.ToString();
  701. this.IsOnRoute = this.VCSystem.ZmqManager.IsCanStanbyLocation;
  702. LanguageHalper.LanguagesSelecter.ChangLanguage(LanguageHalper.eLanguageType.English);
  703. }
  704. public void Dispose()
  705. {
  706. //VCSystem.Instance.Dispose();
  707. }
  708. void SetBusy( int timeLimit = 5, string info = "Wait for Few Minute" )
  709. {
  710. this.IsBusy = true;
  711. this.BusyText = info;
  712. Task.Run( () =>
  713. {
  714. if ( eventBusy.WaitOne( timeLimit * ConstUtils.ONE_SECOND ) == false )
  715. {
  716. Application.Current.Dispatcher.Invoke( () =>
  717. {
  718. this.IsBusy = false;
  719. this.messageController.ShowNotificationView( "Response Time Out", false );
  720. } );
  721. }
  722. } );
  723. }
  724. void ResetBusy()
  725. {
  726. eventBusy.Set();
  727. this.IsBusy = false;
  728. }
  729. public static void SelectCulture( string culture )
  730. {
  731. if ( String.IsNullOrEmpty( culture ) )
  732. return;
  733. //Copy all MergedDictionarys into a auxiliar list.
  734. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  735. //Search for the specified culture.
  736. string requestedCulture = string.Format( "StringResources.{0}.xaml", culture );
  737. var resourceDictionary = dictionaryList.FirstOrDefault( d => d.Source.OriginalString
  738. == "/OHV.ResourceDic;component/Resources/StringResource.en-US.xaml" );
  739. if ( resourceDictionary == null )
  740. {
  741. //If not found, select our default language.
  742. requestedCulture = "StringResources.xaml";
  743. resourceDictionary = dictionaryList.FirstOrDefault( d => d.Source.OriginalString == requestedCulture );
  744. }
  745. //If we have the requested resource, remove it from the list and place at the end.
  746. //Then this language will be our string table to use.
  747. if ( resourceDictionary != null )
  748. {
  749. Application.Current.Resources.MergedDictionaries.Remove( resourceDictionary );
  750. Application.Current.Resources.MergedDictionaries.Add( resourceDictionary );
  751. }
  752. //Inform the threads of the new culture.
  753. Thread.CurrentThread.CurrentCulture = new CultureInfo( culture );
  754. Thread.CurrentThread.CurrentUICulture = new CultureInfo( culture );
  755. }
  756. }
  757. }