D_MainWindowViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. #endregion
  137. #region Brushes
  138. private Brush _ocsBrush;// = Brushes.Gray;
  139. private Brush _batteryBrush;// = Brushes.Gray;
  140. private Brush _laserBrush;// = Brushes.Gray;
  141. public Brush OcsBrush
  142. {
  143. get { return _ocsBrush; }
  144. set
  145. {
  146. SetProperty(ref _ocsBrush, value);
  147. }
  148. }
  149. public Brush BatteryBrush
  150. {
  151. get { return _batteryBrush; }
  152. set
  153. {
  154. SetProperty(ref _batteryBrush, value);
  155. }
  156. }
  157. public Brush LaserBrush
  158. {
  159. get { return this._laserBrush; }
  160. set
  161. {
  162. SetProperty(ref _laserBrush, value);
  163. }
  164. }
  165. #endregion
  166. #region Commands
  167. public ICommand TestCommand { get; set; }
  168. public ICommand NavigateCommand { get; set; }
  169. public ICommand SystemOffCommand { get; set; }
  170. public ICommand BuzzerStopCommand { get; set; }
  171. public ICommand EmergencyStopCommand { get; set; }
  172. public ICommand StartCommand { get; set; }
  173. public ICommand StopCommand { get; set; }
  174. public ICommand AlarmResetCommand { get; set; }
  175. public ICommand ChangeLanguage { get; set; }
  176. public ICommand MachineModeChgCommand { get; set; }
  177. #endregion
  178. IEventAggregator eventAggregator = null;
  179. VCSystem VCSystem = null;
  180. MessageController messageController;
  181. VCSMessagePubSubEvent vcsMessagePublisher;
  182. IRegionManager regionManager;
  183. public Common.Events.HostConnectedEventArgs.eConnectedState State { get; set; }
  184. private DeskTopInfo dti = new DeskTopInfo();
  185. SqliteManager sql;
  186. public D_MainWindowViewModel( IEventAggregator _ea , VCSystem cSystem , IRegionManager _regionManager , MessageController _msgController, SqliteManager _sql )
  187. {
  188. this.regionManager = _regionManager;
  189. this.eventAggregator = _ea;
  190. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallbackCommunication , ThreadOption.UIThread );
  191. this.eventAggregator.GetEvent<HostConnectedPubSubEvent>().Subscribe( OcsConnectCallBack , ThreadOption.UIThread , false );
  192. vcsMessagePublisher = this.eventAggregator.GetEvent<VCSMessagePubSubEvent>();
  193. this.VCSystem = cSystem;
  194. this.messageController = _msgController;
  195. this.sql = _sql;
  196. this.VehicleID = sql.ConfigDal.GetK( ConstString.VehicleID ).Value;
  197. //this.VehicleIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  198. this.OcsIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  199. this.TestCommand = new DelegateCommand( ExecuteTextCommand );
  200. this.NavigateCommand = new DelegateCommand<object>(this.Navigate);
  201. this.SystemOffCommand = new DelegateCommand(ExecuteSystemOffCommand);
  202. this.ChangeLanguage = new DelegateCommand(Execte_ChangeLanguage);
  203. this.StartCommand = new DelegateCommand(ExecuteStartCommand);
  204. this.StopCommand = new DelegateCommand(ExecuteStopCommand);
  205. this.AlarmResetCommand = new DelegateCommand( ExecuteAlarmResetCommand);
  206. this.EmergencyStopCommand = new DelegateCommand(ExecuteEStop);
  207. this.BuzzerStopCommand = new DelegateCommand( ExecuteBuzzerStop );
  208. this.MachineModeChgCommand = new DelegateCommand<string>( ExecuteMachineModeChgCommand );
  209. DispatcherTimer dateTimer = new DispatcherTimer();
  210. dateTimer.Tick += (object sender, EventArgs e) =>
  211. {
  212. this.DateTime = DateTime.Now;
  213. };
  214. dateTimer.Interval = TimeSpan.FromMilliseconds(500);
  215. dateTimer.Start();
  216. this.SwVersion = AssemblyUtils.GetVersion();
  217. this.LastBuildedTime = new Helpler.AssemblyInfo().Get_BuildDateTime();
  218. GSG.NET.Quartz.QuartzUtils.Invoke("RESOURCE_CHECK", GSG.NET.Quartz.QuartzUtils.GetExpnSecond(1), QuzOnResourceUsage);
  219. }
  220. void QuzOnResourceUsage()
  221. {
  222. this.CPU = GSG.NET.OSView.Mgnt.CpuUseRate();
  223. this.RAM = GSG.NET.OSView.Mgnt.MemPhysicalUseRate();
  224. var ll = GSG.NET.OSView.Mgnt.HddList();
  225. this.CDrive = ll.FirstOrDefault().AvailableFreeSpace / ConstUtils.ONE_GIGA_BYTES;
  226. }
  227. private void ExecuteMachineModeChgCommand( string obj )
  228. {
  229. var msg = new VCSMessageEventArgs
  230. {
  231. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqMachineModeChg,
  232. };
  233. if ( this.MachineMode == eMachineMode.LocalMode )
  234. msg.Arg = eMachineMode.HostMode;
  235. else
  236. msg.Arg = eMachineMode.LocalMode;
  237. this.messageController.ShowConfirmationPopupView( $"Change to {msg.Arg} ?", r =>
  238. {
  239. if ( r.Result == ButtonResult.OK )
  240. vcsMessagePublisher.Publish( msg );
  241. } );
  242. }
  243. private void ExecuteBuzzerStop( )
  244. {
  245. var msg = new VCSMessageEventArgs
  246. {
  247. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqBuzzerStop ,
  248. };
  249. vcsMessagePublisher.Publish( msg );
  250. this.ExecuteTextCommand();
  251. }
  252. private void Execte_ChangeLanguage()
  253. {
  254. //LanguageHalper.LanguagesSelcter.ChangLanguage(LanguageHalper.eLanguageType.Chinese);
  255. }
  256. private void ExecuteEStop()
  257. {
  258. var msg = new VCSMessageEventArgs
  259. {
  260. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqEStop ,
  261. };
  262. vcsMessagePublisher.Publish( msg );
  263. }
  264. private void ExecuteAlarmResetCommand( )
  265. {
  266. this.messageController.ShowConfirmationPopupView(" Alarm Reset ? ", r =>
  267. {
  268. if (r.Result == ButtonResult.OK)
  269. {
  270. var msg = new VCSMessageEventArgs
  271. {
  272. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqAlarmReset,
  273. };
  274. vcsMessagePublisher.Publish(msg);
  275. }
  276. });
  277. }
  278. private void ExecuteStopCommand()
  279. {
  280. this.messageController.ShowConfirmationPopupView(" Vehicle Stop ? ", r =>
  281. {
  282. if (r.Result == ButtonResult.OK)
  283. {
  284. var msg = new VCSMessageEventArgs
  285. {
  286. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  287. MessageKey = MessageKey.ManualMode,
  288. };
  289. vcsMessagePublisher.Publish(msg);
  290. }
  291. });
  292. }
  293. private void ExecuteStartCommand()
  294. {
  295. this.messageController.ShowConfirmationPopupView("Request Vehicle Auto Mode ?", r =>
  296. {
  297. if (r.Result == ButtonResult.OK)
  298. {
  299. var msg = new VCSMessageEventArgs
  300. {
  301. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  302. MessageKey = MessageKey.AutoMode,
  303. };
  304. vcsMessagePublisher.Publish(msg);
  305. }
  306. });
  307. }
  308. private void OcsConnectCallBack(HostConnectedEventArgs obj)
  309. {
  310. this.State = obj.State;
  311. switch (this.State)
  312. {
  313. case HostConnectedEventArgs.eConnectedState.Disconnected:
  314. this.OcsBrush = Brushes.Transparent;
  315. break;
  316. case HostConnectedEventArgs.eConnectedState.Connected:
  317. this.OcsBrush = Brushes.LimeGreen;
  318. break;
  319. }
  320. }
  321. private void ExecuteSystemOffCommand()
  322. {
  323. this.messageController.ShowConfirmationPopupView("System ShutDown ?", r =>
  324. {
  325. if (r.Result == ButtonResult.OK)
  326. App.Current.Shutdown();
  327. });
  328. }
  329. private void Navigate(object obj)
  330. {
  331. var selectItem = obj.ToString();
  332. if (!string.IsNullOrEmpty(selectItem))
  333. regionManager.RequestNavigate("MainView", selectItem);
  334. }
  335. void VehicleModeChange(bool isAutoMode)
  336. {
  337. if (isAutoMode)
  338. {
  339. this.StartEnable = false;
  340. this.StopEnable = true;
  341. this.IsManualMode = false;
  342. regionManager.RequestNavigate(RegionNames.MainView, "AutoView");
  343. }
  344. else
  345. {
  346. this.StartEnable = true;
  347. this.StopEnable = false;
  348. this.IsManualMode = true;
  349. }
  350. }
  351. private void UICallbackCommunication(GUIMessageEventArgs obj)
  352. {
  353. switch ( obj.Kind )
  354. {
  355. case GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange:
  356. this.UICallBackModelPropertyChange( obj );
  357. break;
  358. case GUIMessageEventArgs.eGUIMessageKind.RspIOObject:
  359. break;
  360. case GUIMessageEventArgs.eGUIMessageKind.RspIOMapList:
  361. break;
  362. case GUIMessageEventArgs.eGUIMessageKind.RspCommandList:
  363. break;
  364. case GUIMessageEventArgs.eGUIMessageKind.RspAutoModeChange:
  365. break;
  366. case GUIMessageEventArgs.eGUIMessageKind.RspManualModeChange:
  367. break;
  368. case GUIMessageEventArgs.eGUIMessageKind.RspAlarmReset:
  369. break;
  370. case GUIMessageEventArgs.eGUIMessageKind.RspVehicleModeChange:
  371. this.RspVehicleModeChange( obj );
  372. break;
  373. case GUIMessageEventArgs.eGUIMessageKind.RspEStop:
  374. break;
  375. case GUIMessageEventArgs.eGUIMessageKind.RspVihicleState:
  376. break;
  377. case GUIMessageEventArgs.eGUIMessageKind.RspMachineModeChg:
  378. RspMachineModeChg(obj);
  379. break;
  380. default:
  381. break;
  382. }
  383. }
  384. private void RspMachineModeChg( GUIMessageEventArgs obj )
  385. {
  386. string reason = string.Empty;
  387. FluentResults.Result result = obj.Result;
  388. if ( obj.Result.IsFailed )
  389. {
  390. reason = result.Errors.FirstOrDefault().Message;
  391. }
  392. else
  393. {
  394. var mode = CastTo<eMachineMode>.From<object>( obj.Args );
  395. reason = $"Change To {mode} Successes";
  396. }
  397. this.messageController.ShowNotificationView( reason );
  398. }
  399. private void RspVehicleModeChange(GUIMessageEventArgs obj)
  400. {
  401. if ( obj.Result.IsSuccess)
  402. {
  403. if (obj.MessageKey.Equals(MessageKey.AutoMode)) { this.VehicleModeChange(true); }
  404. if (obj.MessageKey.Equals(MessageKey.ManualMode)) { this.VehicleModeChange(false); }
  405. }
  406. else
  407. {
  408. }
  409. }
  410. void UICallBackModelPropertyChange(GUIMessageEventArgs args)
  411. {
  412. if (args.MessageKey.Equals(MessageKey.Alarm))
  413. {
  414. var hisAlarm = args.Args as HisAlarm;
  415. this.messageController.ShowNotificationView(args.MessageText);
  416. this.VehicleModeChange(false);//알람이 발생하면 자동으로 Manual Mode 로 변경됨
  417. }
  418. if (args.MessageKey.Equals(MessageKey.Vehicle))
  419. {
  420. if (args.ModelPropertyName.Equals("VehicleStateProperty"))
  421. {
  422. var vehicleState = CastTo<eVehicleState>.From<object>(args.Args);
  423. if (vehicleState == eVehicleState.Abnormal)
  424. this.IsVehicleAlarm = true;
  425. else
  426. this.IsVehicleAlarm = false;
  427. }
  428. if ( args.ModelPropertyName.Equals( "MachineMode" ) )
  429. {
  430. this.MachineMode = CastTo<eMachineMode>.From<object>( args.Args );
  431. }
  432. }
  433. }
  434. private void ExecuteTextCommand()
  435. {
  436. var m = new VCSMessageEventArgs();
  437. m.MessageKey = "";
  438. m.MessageText = "Test Message";
  439. m.Kind = VCSMessageEventArgs.eVCSMessageKind.ReqTest;
  440. m.Command = new Common.Model.Command() { CommandID = "TestCommand123", Type = Common.Shareds.eCommandType.Move };
  441. this.eventAggregator.GetEvent<VCSMessagePubSubEvent>().Publish( m );
  442. }
  443. public void InitViewModel()
  444. {
  445. //VCSystem.Instance.Init();
  446. //var containerRegistry = ServiceLocator.Current.GetInstance<VCSystem>();
  447. //if ( containerRegistry.Equals( this.VCSystem ) )
  448. //{
  449. // Console.WriteLine( "==" );
  450. //}
  451. //var vsys = containerRegistry.GetContainer().Resolve<IContainerProvider>();
  452. regionManager.RequestNavigate(RegionNames.MainView, "AutoView");
  453. }
  454. public void Dispose()
  455. {
  456. //VCSystem.Instance.Dispose();
  457. }
  458. public static void SelectCulture(string culture)
  459. {
  460. if (String.IsNullOrEmpty(culture))
  461. return;
  462. //Copy all MergedDictionarys into a auxiliar list.
  463. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  464. //Search for the specified culture.
  465. string requestedCulture = string.Format("StringResources.{0}.xaml", culture);
  466. var resourceDictionary = dictionaryList.
  467. FirstOrDefault(d => d.Source.OriginalString == "/OHV.ResourceDic;component/Resources/StringResource.en-US.xaml");
  468. if (resourceDictionary == null)
  469. {
  470. //If not found, select our default language.
  471. requestedCulture = "StringResources.xaml";
  472. resourceDictionary = dictionaryList.
  473. FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  474. }
  475. //If we have the requested resource, remove it from the list and place at the end.
  476. //Then this language will be our string table to use.
  477. if (resourceDictionary != null)
  478. {
  479. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
  480. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  481. }
  482. //Inform the threads of the new culture.
  483. Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
  484. Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
  485. }
  486. }
  487. }