D_MainWindowViewModel.cs 21 KB

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