D_MainWindowViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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.Module.Interactivity;
  9. using OHV.SqliteDAL;
  10. using Prism.Commands;
  11. using Prism.Events;
  12. using Prism.Mvvm;
  13. using Prism.Regions;
  14. using Prism.Services.Dialogs;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Globalization;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using System.Windows.Input;
  24. using System.Windows.Media;
  25. using System.Windows.Threading;
  26. using VehicleControlSystem;
  27. namespace OHV.Vehicle.Concept
  28. {
  29. public enum eBatteryState
  30. {
  31. DisConnect,
  32. Connect,
  33. Charge,
  34. }
  35. class D_MainWindowViewModel : BindableBase
  36. {
  37. #region Properties
  38. private string _title = "Prism Unity Application";
  39. public string Title
  40. {
  41. get { return _title; }
  42. set { SetProperty( ref _title, value ); }
  43. }
  44. private double _cpu;
  45. public double CPU
  46. {
  47. get { return this._cpu; }
  48. set { this.SetProperty( ref this._cpu, value ); }
  49. }
  50. private double _totalCPU;
  51. public double TotalCPU
  52. {
  53. get { return this._totalCPU; }
  54. set
  55. {
  56. //this._totalCPU = dti.cpuResult.ToString();
  57. this.SetProperty( ref this._totalCPU, value );
  58. }
  59. }
  60. public int UsageCPU { get; set; }
  61. private double _ram;
  62. public double RAM
  63. {
  64. get { return this._ram; }
  65. set { this.SetProperty( ref this._ram, value ); }
  66. }
  67. private double _cDrive;
  68. public double CDrive
  69. {
  70. get { return this._cDrive; }
  71. set { this.SetProperty( ref this._cDrive, value ); }
  72. }
  73. DateTime _dateTime;
  74. public DateTime DateTime
  75. {
  76. get { return _dateTime; }
  77. set
  78. {
  79. this.SetProperty( ref _dateTime, value );
  80. }
  81. }
  82. private string _vehicleID;
  83. public string VehicleID
  84. {
  85. get { return this._vehicleID; }
  86. set { this.SetProperty( ref this._vehicleID, value ); }
  87. }
  88. private string _vehicleIP = "127.0.0.1";
  89. public string VehicleIP
  90. {
  91. get { return this._vehicleIP; }
  92. set { this.SetProperty( ref this._vehicleIP, value ); }
  93. }
  94. private string _ocsIP;
  95. public string OcsIP
  96. {
  97. get { return this._ocsIP; }
  98. set { this.SetProperty( ref this._ocsIP, value ); }
  99. }
  100. private string _swVersion;
  101. public string SwVersion
  102. {
  103. get { return this._swVersion; }
  104. set { this.SetProperty( ref this._swVersion, value ); }
  105. }
  106. bool _startEnable = true;
  107. public bool StartEnable
  108. {
  109. get { return this._startEnable; }
  110. set { this.SetProperty( ref this._startEnable, value ); }
  111. }
  112. bool _stopEnable = false;
  113. public bool StopEnable
  114. {
  115. get { return this._stopEnable; }
  116. set { this.SetProperty( ref this._stopEnable, value ); }
  117. }
  118. private DateTime _lastBuildedTime;
  119. public DateTime LastBuildedTime
  120. {
  121. get { return this._lastBuildedTime; }
  122. set { this.SetProperty( ref this._lastBuildedTime, value ); }
  123. }
  124. private bool isManualMode = true;
  125. public bool IsManualMode
  126. {
  127. get { return isManualMode; }
  128. set { SetProperty( ref isManualMode, value ); }
  129. }
  130. private bool isVehicleAlarm = false;
  131. public bool IsVehicleAlarm
  132. {
  133. get { return isVehicleAlarm; }
  134. set { SetProperty( ref this.isVehicleAlarm, value ); }
  135. }
  136. private eMachineMode machineMode = eMachineMode.LocalMode;
  137. public eMachineMode MachineMode
  138. {
  139. get { return machineMode; }
  140. set { SetProperty( ref this.machineMode, value ); }
  141. }
  142. eOcsState ocsState = eOcsState.DisConnect;
  143. public eOcsState OcsState
  144. {
  145. get { return this.ocsState; }
  146. set { this.SetProperty( ref this.ocsState, value ); }
  147. }
  148. eBatteryState batteryState = eBatteryState.DisConnect;
  149. public eBatteryState BatteryState
  150. {
  151. get { return this.batteryState; }
  152. set { this.SetProperty( ref this.batteryState , value ); }
  153. }
  154. #endregion
  155. #region Brushes
  156. private Brush _ocsBrush;
  157. public Brush OcsBrush
  158. {
  159. get { return _ocsBrush; }
  160. set
  161. {
  162. SetProperty( ref _ocsBrush, value );
  163. }
  164. }
  165. private Brush batteryBrush;
  166. public Brush BatteryBrush
  167. {
  168. get { return batteryBrush; }
  169. set
  170. {
  171. SetProperty( ref batteryBrush, value );
  172. }
  173. }
  174. double soc;
  175. public double SOC
  176. {
  177. get { return this.soc; }
  178. set { this.SetProperty( ref this.soc, value ); }
  179. }
  180. #endregion
  181. #region Commands
  182. public ICommand TestCommand { get; set; }
  183. public ICommand NavigateCommand { get; set; }
  184. public ICommand SystemOffCommand { get; set; }
  185. public ICommand BuzzerStopCommand { get; set; }
  186. public ICommand EmergencyStopCommand { get; set; }
  187. public ICommand StartCommand { get; set; }
  188. public ICommand StopCommand { get; set; }
  189. public ICommand AlarmResetCommand { get; set; }
  190. public ICommand ChangeLanguage { get; set; }
  191. public ICommand MachineModeChgCommand { get; set; }
  192. public ICommand BatteryPopup { get; set; }
  193. #endregion
  194. IEventAggregator eventAggregator = null;
  195. VCSystem VCSystem = null;
  196. MessageController messageController;
  197. VCSMessagePubSubEvent vcsMessagePublisher;
  198. IRegionManager regionManager;
  199. DeskTopInfo dti = new DeskTopInfo();
  200. SqliteManager sql;
  201. public D_MainWindowViewModel( IEventAggregator _ea, VCSystem cSystem, IRegionManager _regionManager, MessageController _msgController, SqliteManager _sql )
  202. {
  203. this.regionManager = _regionManager;
  204. this.eventAggregator = _ea;
  205. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallbackCommunication, ThreadOption.UIThread );
  206. this.eventAggregator.GetEvent<HostConnectedPubSubEvent>().Subscribe( OcsConnectCallBack, ThreadOption.UIThread, false );
  207. vcsMessagePublisher = this.eventAggregator.GetEvent<VCSMessagePubSubEvent>();
  208. this.VCSystem = cSystem;
  209. this.messageController = _msgController;
  210. this.sql = _sql;
  211. this.VehicleID = sql.ConfigDal.GetK( ConstString.VehicleID ).Value;
  212. //this.VehicleIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  213. this.OcsIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  214. this.TestCommand = new DelegateCommand( ExecuteTextCommand );
  215. this.NavigateCommand = new DelegateCommand<object>( this.Navigate );
  216. this.SystemOffCommand = new DelegateCommand( ExecuteSystemOffCommand );
  217. this.ChangeLanguage = new DelegateCommand( Execte_ChangeLanguage );
  218. this.StartCommand = new DelegateCommand( ExecuteStartCommand );
  219. this.StopCommand = new DelegateCommand( ExecuteStopCommand );
  220. this.AlarmResetCommand = new DelegateCommand( ExecuteAlarmResetCommand );
  221. this.EmergencyStopCommand = new DelegateCommand( ExecuteEStop );
  222. this.BuzzerStopCommand = new DelegateCommand( ExecuteBuzzerStop );
  223. this.MachineModeChgCommand = new DelegateCommand<string>( ExecuteMachineModeChgCommand );
  224. this.BatteryPopup = new DelegateCommand( ExecuteBatteryPopup );
  225. DispatcherTimer dateTimer = new DispatcherTimer();
  226. dateTimer.Tick += ( object sender, EventArgs e ) =>
  227. {
  228. this.DateTime = DateTime.Now;
  229. };
  230. dateTimer.Interval = TimeSpan.FromMilliseconds( 500 );
  231. dateTimer.Start();
  232. this.SwVersion = AssemblyUtils.GetVersion();
  233. this.LastBuildedTime = new Helpler.AssemblyInfo().Get_BuildDateTime();
  234. GSG.NET.Quartz.QuartzUtils.Invoke( "RESOURCE_CHECK", GSG.NET.Quartz.QuartzUtils.GetExpnSecond( 1 ), QuzOnResourceUsage );
  235. }
  236. private void ExecuteBatteryPopup( )
  237. {
  238. this.messageController.ShowBatteryPopupView();
  239. }
  240. void QuzOnResourceUsage()
  241. {
  242. this.CPU = GSG.NET.OSView.Mgnt.CpuUseRate();
  243. this.RAM = GSG.NET.OSView.Mgnt.MemPhysicalUseRate();
  244. var ll = GSG.NET.OSView.Mgnt.HddList();
  245. this.CDrive = ll.FirstOrDefault().AvailableFreeSpace / ConstUtils.ONE_GIGA_BYTES;
  246. }
  247. private void ExecuteMachineModeChgCommand( string obj )
  248. {
  249. var msg = new VCSMessageEventArgs
  250. {
  251. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqMachineModeChg,
  252. };
  253. if ( this.MachineMode == eMachineMode.LocalMode )
  254. msg.Arg = eMachineMode.HostMode;
  255. else
  256. msg.Arg = eMachineMode.LocalMode;
  257. this.messageController.ShowConfirmationPopupView( $"Change to {msg.Arg} ?", r =>
  258. {
  259. if ( r.Result == ButtonResult.OK )
  260. vcsMessagePublisher.Publish( msg );
  261. } );
  262. }
  263. private void ExecuteBuzzerStop()
  264. {
  265. var msg = new VCSMessageEventArgs
  266. {
  267. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqBuzzerStop,
  268. };
  269. vcsMessagePublisher.Publish( msg );
  270. this.ExecuteTextCommand();
  271. }
  272. private void Execte_ChangeLanguage()
  273. {
  274. //LanguageHalper.LanguagesSelcter.ChangLanguage(LanguageHalper.eLanguageType.Chinese);
  275. }
  276. private void ExecuteEStop()
  277. {
  278. var msg = new VCSMessageEventArgs
  279. {
  280. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqEStop,
  281. };
  282. vcsMessagePublisher.Publish( msg );
  283. }
  284. private void ExecuteAlarmResetCommand()
  285. {
  286. this.messageController.ShowConfirmationPopupView( " Alarm Reset ? ", r =>
  287. {
  288. if ( r.Result == ButtonResult.OK )
  289. {
  290. var msg = new VCSMessageEventArgs
  291. {
  292. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqAlarmReset,
  293. };
  294. vcsMessagePublisher.Publish( msg );
  295. }
  296. } );
  297. }
  298. private void ExecuteStopCommand()
  299. {
  300. this.messageController.ShowConfirmationPopupView( " Vehicle Stop ? ", r =>
  301. {
  302. if ( r.Result == ButtonResult.OK )
  303. {
  304. var msg = new VCSMessageEventArgs
  305. {
  306. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  307. MessageKey = MessageKey.ManualMode,
  308. };
  309. vcsMessagePublisher.Publish( msg );
  310. }
  311. } );
  312. }
  313. private void ExecuteStartCommand()
  314. {
  315. this.messageController.ShowConfirmationPopupView( "Request Vehicle Auto Mode ?", r =>
  316. {
  317. if ( r.Result == ButtonResult.OK )
  318. {
  319. var msg = new VCSMessageEventArgs
  320. {
  321. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  322. MessageKey = MessageKey.AutoMode,
  323. };
  324. vcsMessagePublisher.Publish( msg );
  325. }
  326. } );
  327. }
  328. private void OcsConnectCallBack( HostConnectedEventArgs obj )
  329. {
  330. var state = obj.State;
  331. switch ( state )
  332. {
  333. case HostConnectedEventArgs.eConnectedState.Disconnected:
  334. this.OcsState = eOcsState.DisConnect;
  335. break;
  336. case HostConnectedEventArgs.eConnectedState.Connected:
  337. this.OcsState = eOcsState.Connect;
  338. break;
  339. }
  340. }
  341. private void ExecuteSystemOffCommand()
  342. {
  343. this.messageController.ShowConfirmationPopupView( "System ShutDown ?", r =>
  344. {
  345. if ( r.Result == ButtonResult.OK )
  346. App.Current.Shutdown();
  347. } );
  348. }
  349. private void Navigate( object obj )
  350. {
  351. var selectItem = obj.ToString();
  352. if ( !string.IsNullOrEmpty( selectItem ) )
  353. regionManager.RequestNavigate( "MainView", selectItem );
  354. }
  355. void VehicleModeChange( bool isAutoMode )
  356. {
  357. if ( isAutoMode )
  358. {
  359. this.StartEnable = false;
  360. this.StopEnable = true;
  361. //this.IsManualMode = false;
  362. //regionManager.RequestNavigate( RegionNames.MainView, "AutoView" );
  363. }
  364. else
  365. {
  366. this.StartEnable = true;
  367. this.StopEnable = false;
  368. //this.IsManualMode = true;
  369. }
  370. }
  371. private void UICallbackCommunication( GUIMessageEventArgs obj )
  372. {
  373. switch ( obj.Kind )
  374. {
  375. case GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange:
  376. this.UICallBackModelPropertyChange( obj );
  377. break;
  378. case GUIMessageEventArgs.eGUIMessageKind.RspIOObject:
  379. break;
  380. case GUIMessageEventArgs.eGUIMessageKind.RspIOMapList:
  381. break;
  382. case GUIMessageEventArgs.eGUIMessageKind.RspCommandList:
  383. break;
  384. case GUIMessageEventArgs.eGUIMessageKind.RspAutoModeChange:
  385. break;
  386. case GUIMessageEventArgs.eGUIMessageKind.RspManualModeChange:
  387. break;
  388. case GUIMessageEventArgs.eGUIMessageKind.RspAlarmReset:
  389. break;
  390. case GUIMessageEventArgs.eGUIMessageKind.RspVehicleModeChange:
  391. this.RspVehicleModeChange( obj );
  392. break;
  393. case GUIMessageEventArgs.eGUIMessageKind.RspEStop:
  394. break;
  395. case GUIMessageEventArgs.eGUIMessageKind.RspVihicleState:
  396. break;
  397. case GUIMessageEventArgs.eGUIMessageKind.RspMachineModeChg:
  398. RspMachineModeChg( obj );
  399. break;
  400. default:
  401. break;
  402. }
  403. }
  404. void RspBatteryConnected( GUIMessageEventArgs obj )
  405. {
  406. var connected = CastTo<bool>.From<object>( obj.Args );
  407. if ( connected )
  408. {
  409. this.BatteryBrush = Brushes.LimeGreen;
  410. BatteryState = eBatteryState.Connect;
  411. }
  412. else
  413. {
  414. this.BatteryBrush = Brushes.Gray;
  415. BatteryState = eBatteryState.DisConnect;
  416. }
  417. }
  418. private void RspMachineModeChg( GUIMessageEventArgs obj )
  419. {
  420. string reason = string.Empty;
  421. FluentResults.Result result = obj.Result;
  422. if ( obj.Result.IsFailed )
  423. {
  424. reason = result.Errors.FirstOrDefault().Message;
  425. }
  426. else
  427. {
  428. var mode = CastTo<eMachineMode>.From<object>( obj.Args );
  429. reason = $"Change To {mode} Successes";
  430. }
  431. this.messageController.ShowNotificationView( reason );
  432. }
  433. private void RspVehicleModeChange( GUIMessageEventArgs obj )
  434. {
  435. if ( obj.Result.IsSuccess )
  436. {
  437. if ( obj.MessageKey.Equals( MessageKey.AutoMode ) ) { this.VehicleModeChange( true ); }
  438. if ( obj.MessageKey.Equals( MessageKey.ManualMode ) ) { this.VehicleModeChange( false ); }
  439. }
  440. else
  441. {
  442. }
  443. }
  444. void UICallBackModelPropertyChange( GUIMessageEventArgs args )
  445. {
  446. if ( args.MessageKey.Equals( MessageKey.Alarm ) )
  447. {
  448. var hisAlarm = args.Args as HisAlarm;
  449. this.messageController.ShowNotificationView( args.MessageText );
  450. this.VehicleModeChange( false );//알람이 발생하면 자동으로 Manual Mode 로 변경됨
  451. this.IsVehicleAlarm = true;
  452. }
  453. if ( args.MessageKey.Equals( MessageKey.Vehicle ) )
  454. {
  455. switch(args.ModelPropertyName)
  456. {
  457. //TODO:[20/04/27]::비클 상태 표시 변화 확인, 리셋 후에 변화 되는지
  458. case "VehicleStateProperty":
  459. {
  460. var vehicleState = CastTo<eVehicleState>.From<object>( args.Args );
  461. if ( vehicleState == eVehicleState.Abnormal )
  462. this.IsVehicleAlarm = true;
  463. else
  464. this.IsVehicleAlarm = false;
  465. }
  466. break;
  467. case "MachineMode":
  468. this.MachineMode = CastTo<eMachineMode>.From<object>( args.Args );
  469. break;
  470. case "BatteryIsConnect":
  471. this.RspBatteryConnected( args );
  472. break;
  473. case "BatteryStateOfCharge":
  474. this.SOC = CastTo<double>.From<object>( args.Args );
  475. break;
  476. case "BatteryChargeTime":
  477. {
  478. var cTime = CastTo<double>.From<object>( args.Args );
  479. if ( cTime > 0 )
  480. this.BatteryState = eBatteryState.Charge;
  481. else
  482. this.BatteryState = eBatteryState.Connect;
  483. }
  484. break;
  485. case "BatteryDisChargeTime":
  486. {
  487. var dTime = CastTo<double>.From<object>( args.Args );
  488. }
  489. break;
  490. }
  491. }
  492. }
  493. private void ExecuteTextCommand()
  494. {
  495. var m = new VCSMessageEventArgs();
  496. m.MessageKey = "";
  497. m.MessageText = "Test Message";
  498. m.Kind = VCSMessageEventArgs.eVCSMessageKind.ReqTest;
  499. m.Command = new Common.Model.Command() { CommandID = "TestCommand123", Type = Common.Shareds.eCommandType.Move };
  500. this.eventAggregator.GetEvent<VCSMessagePubSubEvent>().Publish( m );
  501. }
  502. public void InitViewModel()
  503. {
  504. //VCSystem.Instance.Init();
  505. //var containerRegistry = ServiceLocator.Current.GetInstance<VCSystem>();
  506. //if ( containerRegistry.Equals( this.VCSystem ) )
  507. //{
  508. // Console.WriteLine( "==" );
  509. //}
  510. //var vsys = containerRegistry.GetContainer().Resolve<IContainerProvider>();
  511. regionManager.RequestNavigate( RegionNames.MainView, "AutoView" );
  512. //var msg = new DriveControlEventArgs
  513. //{
  514. // EventDir = DriveControlEventArgs.eEventDir.ToBack,
  515. // ControlKind = DriveControlEventArgs.eControlKind.Steering,
  516. //};
  517. //msg.MoveDir = DriveControlEventArgs.eMoveDir.LEFT;
  518. //this.eventAggregator.GetEvent<DriveControlPubSubEvent>().Publish( msg );
  519. }
  520. public void Dispose()
  521. {
  522. //VCSystem.Instance.Dispose();
  523. }
  524. public static void SelectCulture( string culture )
  525. {
  526. if ( String.IsNullOrEmpty( culture ) )
  527. return;
  528. //Copy all MergedDictionarys into a auxiliar list.
  529. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  530. //Search for the specified culture.
  531. string requestedCulture = string.Format( "StringResources.{0}.xaml", culture );
  532. var resourceDictionary = dictionaryList.
  533. FirstOrDefault( d => d.Source.OriginalString == "/OHV.ResourceDic;component/Resources/StringResource.en-US.xaml" );
  534. if ( resourceDictionary == null )
  535. {
  536. //If not found, select our default language.
  537. requestedCulture = "StringResources.xaml";
  538. resourceDictionary = dictionaryList.
  539. FirstOrDefault( d => d.Source.OriginalString == requestedCulture );
  540. }
  541. //If we have the requested resource, remove it from the list and place at the end.
  542. //Then this language will be our string table to use.
  543. if ( resourceDictionary != null )
  544. {
  545. Application.Current.Resources.MergedDictionaries.Remove( resourceDictionary );
  546. Application.Current.Resources.MergedDictionaries.Add( resourceDictionary );
  547. }
  548. //Inform the threads of the new culture.
  549. Thread.CurrentThread.CurrentCulture = new CultureInfo( culture );
  550. Thread.CurrentThread.CurrentUICulture = new CultureInfo( culture );
  551. }
  552. }
  553. }