D_MainWindowViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using CommonServiceLocator;
  2. using GSG.NET.Utils;
  3. using OHV.Common.Events;
  4. using OHV.Common.Model;
  5. using OHV.Common.Shareds;
  6. using OHV.Module.Interactivity;
  7. using OHV.SqliteDAL;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Threading;
  24. using VehicleControlSystem;
  25. namespace OHV.Vehicle.Concept
  26. {
  27. class D_MainWindowViewModel : BindableBase
  28. {
  29. private string _title = "Prism Unity Application";
  30. public string Title
  31. {
  32. get { return _title; }
  33. set { SetProperty(ref _title, value); }
  34. }
  35. private double _cpu;
  36. public double CPU
  37. {
  38. get { return this._cpu; }
  39. set { this.SetProperty(ref this._cpu, value); }
  40. }
  41. private double _totalCPU;
  42. public double TotalCPU
  43. {
  44. get { return this._totalCPU; }
  45. set
  46. {
  47. //this._totalCPU = dti.cpuResult.ToString();
  48. this.SetProperty(ref this._totalCPU, value);
  49. }
  50. }
  51. public int UsageCPU { get; set; }
  52. private double _ram;
  53. public double RAM
  54. {
  55. get { return this._ram; }
  56. set { this.SetProperty(ref this._ram, value); }
  57. }
  58. private double _cDrive;
  59. public double CDrive
  60. {
  61. get { return this._cDrive; }
  62. set { this.SetProperty(ref this._cDrive, value); }
  63. }
  64. DateTime _dateTime;
  65. public DateTime DateTime
  66. {
  67. get { return _dateTime; }
  68. set
  69. {
  70. this.SetProperty(ref _dateTime, value);
  71. }
  72. }
  73. private string _vehicleID;
  74. public string VehicleID
  75. {
  76. get { return this._vehicleID; }
  77. set { this.SetProperty(ref this._vehicleID, value); }
  78. }
  79. private string _vehicleIP = "127.0.0.1";
  80. public string VehicleIP
  81. {
  82. get { return this._vehicleIP; }
  83. set { this.SetProperty( ref this._vehicleIP , value ); }
  84. }
  85. private string _ocsIP;
  86. public string OcsIP
  87. {
  88. get { return this._ocsIP; }
  89. set { this.SetProperty( ref this._ocsIP , value ); }
  90. }
  91. private Brush _ocsBrush;// = Brushes.Gray;
  92. private Brush _batteryBrush;// = Brushes.Gray;
  93. private Brush _laserBrush;// = Brushes.Gray;
  94. public Brush OcsBrush
  95. {
  96. get { return _ocsBrush; }
  97. set
  98. {
  99. SetProperty(ref _ocsBrush, value);
  100. }
  101. }
  102. public Brush BatteryBrush
  103. {
  104. get { return _batteryBrush; }
  105. set
  106. {
  107. SetProperty(ref _batteryBrush, value);
  108. }
  109. }
  110. public Brush LaserBrush
  111. {
  112. get { return this._laserBrush; }
  113. set
  114. {
  115. SetProperty(ref _laserBrush, value);
  116. }
  117. }
  118. private string _swVersion;
  119. public string SwVersion
  120. {
  121. get { return this._swVersion; }
  122. set { this.SetProperty(ref this._swVersion, value); }
  123. }
  124. private DateTime _lastBuildedTime;
  125. public DateTime LastBuildedTime
  126. {
  127. get { return this._lastBuildedTime; }
  128. set { this.SetProperty(ref this._lastBuildedTime, value); }
  129. }
  130. bool _startEnable = true;
  131. public bool StartEnable
  132. {
  133. get { return this._startEnable; }
  134. set { this.SetProperty( ref this._startEnable , value ); }
  135. }
  136. bool _stopEnable = false;
  137. public bool StopEnable
  138. {
  139. get { return this._stopEnable; }
  140. set { this.SetProperty( ref this._stopEnable , value ); }
  141. }
  142. IEventAggregator eventAggregator = null;
  143. VCSystem VCSystem = null;
  144. MessageController messageController;
  145. VCSMessagePubSubEvent vcsMessagePublisher;
  146. public ICommand TestCommand { get; set; }
  147. public ICommand NavigateCommand { get; set; }
  148. public ICommand SystemOffCommand { get; set; }
  149. public ICommand BuzzerStopCommand { get; set; }
  150. public ICommand EmergencyStopCommand { get; set; }
  151. public ICommand StartCommand { get; set; }
  152. public ICommand StopCommand { get; set; }
  153. public ICommand AlarmResetCommand { get; set; }
  154. IRegionManager regionManager;
  155. public Common.Events.HostConnectedEventArgs.eConnectedState State { get; set; }
  156. private DeskTopInfo dti = new DeskTopInfo();
  157. public ICommand ChangeLanguage { get; set; }
  158. private void Execte_ChangeLanguage()
  159. {
  160. //LanguageHalper.LanguagesSelcter.ChangLanguage(LanguageHalper.eLanguageType.Chinese);
  161. }
  162. SqliteManager sql;
  163. public D_MainWindowViewModel( IEventAggregator _ea , VCSystem cSystem , IRegionManager _regionManager , MessageController _msgController, SqliteManager _sql )
  164. {
  165. this.regionManager = _regionManager;
  166. this.eventAggregator = _ea;
  167. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallbackCommunication , ThreadOption.UIThread );
  168. this.eventAggregator.GetEvent<HostConnectedPubSubEvent>().Subscribe( OcsConnectCallBack , ThreadOption.UIThread , false );
  169. vcsMessagePublisher = this.eventAggregator.GetEvent<VCSMessagePubSubEvent>();
  170. this.VCSystem = cSystem;
  171. this.messageController = _msgController;
  172. this.sql = _sql;
  173. this.VehicleID = sql.ConfigDal.GetK( ConstString.VehicleID ).Value;
  174. //this.VehicleIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  175. this.OcsIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  176. //this.TestCommand = new DelegateCommand( ExecuteTextCommand );
  177. this.NavigateCommand = new DelegateCommand<object>(this.Navigate);
  178. this.SystemOffCommand = new DelegateCommand(ExecuteSystemOffCommand);
  179. this.ChangeLanguage = new DelegateCommand(Execte_ChangeLanguage);
  180. this.StartCommand = new DelegateCommand(ExecuteStartCommand);
  181. this.StopCommand = new DelegateCommand(ExecuteStopCommand);
  182. this.AlarmResetCommand = new DelegateCommand( ExecuteAlarmResetCommand);
  183. this.EmergencyStopCommand = new DelegateCommand(ExecuteEStop);
  184. DispatcherTimer dateTimer = new DispatcherTimer();
  185. dateTimer.Tick += (object sender, EventArgs e) =>
  186. {
  187. this.DateTime = DateTime.Now;
  188. };
  189. dateTimer.Interval = TimeSpan.FromMilliseconds(500);
  190. dateTimer.Start();
  191. //DispatcherTimer deskTopTimer = new DispatcherTimer();
  192. //deskTopTimer.Tick += (object sender, EventArgs e) =>
  193. //{
  194. // this.CPU = dti.GetCpuUsage();
  195. // this.UsageCPU = (int)dti.GetCpuUsage();
  196. // this.RAM = dti.GetRamUsage();
  197. //};
  198. //deskTopTimer.Interval = TimeSpan.FromMilliseconds(1000);
  199. //deskTopTimer.Start();
  200. //this._totalCPU = dti.cpuResult;
  201. //this._cDrive = dti.GetCDrive();
  202. this.SwVersion = AssemblyUtils.GetVersion();
  203. this.LastBuildedTime = new Helpler.AssemblyInfo().Get_BuildDateTime();
  204. GSG.NET.Quartz.QuartzUtils.Invoke("RESOURCE_CHECK", GSG.NET.Quartz.QuartzUtils.GetExpnSecond(1), QuzOnResourceUsage);
  205. }
  206. private void ExecuteEStop()
  207. {
  208. var msg = new VCSMessageEventArgs
  209. {
  210. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqEStop,
  211. };
  212. vcsMessagePublisher.Publish(msg);
  213. }
  214. void QuzOnResourceUsage()
  215. {
  216. this.CPU = GSG.NET.OSView.Mgnt.CpuUseRate();
  217. this.RAM = GSG.NET.OSView.Mgnt.MemPhysicalUseRate();
  218. var ll = GSG.NET.OSView.Mgnt.HddList();
  219. this.CDrive = ll.FirstOrDefault().AvailableFreeSpace / ConstUtils.ONE_GIGA_BYTES;
  220. }
  221. private void ExecuteAlarmResetCommand( )
  222. {
  223. }
  224. private void ExecuteStopCommand()
  225. {
  226. this.messageController.ShowConfirmationPopupView(" Vehicle Stop ? ", r =>
  227. {
  228. if (r.Result == ButtonResult.OK)
  229. {
  230. var msg = new VCSMessageEventArgs
  231. {
  232. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  233. MessageKey = MessageKey.ManualMode,
  234. };
  235. vcsMessagePublisher.Publish(msg);
  236. }
  237. });
  238. }
  239. private void ExecuteStartCommand()
  240. {
  241. this.messageController.ShowConfirmationPopupView("Request Vehicle Auto Mode ?", r =>
  242. {
  243. if (r.Result == ButtonResult.OK)
  244. {
  245. var msg = new VCSMessageEventArgs
  246. {
  247. Kind = VCSMessageEventArgs.eVCSMessageKind.ReqVehicleModeChange,
  248. MessageKey = MessageKey.AutoMode,
  249. };
  250. vcsMessagePublisher.Publish(msg);
  251. }
  252. });
  253. }
  254. private void OcsConnectCallBack(HostConnectedEventArgs obj)
  255. {
  256. this.State = obj.State;
  257. switch (this.State)
  258. {
  259. case HostConnectedEventArgs.eConnectedState.Disconnected:
  260. this.OcsBrush = Brushes.Transparent;
  261. break;
  262. case HostConnectedEventArgs.eConnectedState.Connected:
  263. this.OcsBrush = Brushes.LimeGreen;
  264. break;
  265. }
  266. }
  267. private void ExecuteSystemOffCommand()
  268. {
  269. this.messageController.ShowConfirmationPopupView("System ShutDown ?", r =>
  270. {
  271. if (r.Result == ButtonResult.OK)
  272. App.Current.Shutdown();
  273. });
  274. }
  275. private void Navigate(object obj)
  276. {
  277. var selectItem = obj.ToString();
  278. if (!string.IsNullOrEmpty(selectItem))
  279. regionManager.RequestNavigate("MainView", selectItem);
  280. }
  281. void VehicleModeChang(bool isAutoMode)
  282. {
  283. if (isAutoMode)
  284. {
  285. this.StartEnable = false;
  286. this.StopEnable = true;
  287. }
  288. else
  289. {
  290. this.StartEnable = true;
  291. this.StopEnable = false;
  292. }
  293. }
  294. private void UICallbackCommunication(GUIMessageEventArgs obj)
  295. {
  296. switch (obj.Kind)
  297. {
  298. case GUIMessageEventArgs.eGUIMessageKind.ModelPropertyChange:
  299. this.UICallBackModelPropertyChange(obj);
  300. break;
  301. case GUIMessageEventArgs.eGUIMessageKind.RspIOObject:
  302. break;
  303. case GUIMessageEventArgs.eGUIMessageKind.RspIOMapList:
  304. break;
  305. case GUIMessageEventArgs.eGUIMessageKind.RspCommandList:
  306. break;
  307. case GUIMessageEventArgs.eGUIMessageKind.RspAutoModeChange:
  308. break;
  309. case GUIMessageEventArgs.eGUIMessageKind.RspManualModeChange:
  310. break;
  311. case GUIMessageEventArgs.eGUIMessageKind.RspAlarmReset:
  312. break;
  313. case GUIMessageEventArgs.eGUIMessageKind.RspVehicleModeChange:
  314. this.RspVehicleModeChange(obj);
  315. break;
  316. default:
  317. break;
  318. }
  319. }
  320. private void RspVehicleModeChange(GUIMessageEventArgs obj)
  321. {
  322. if ( obj.Result.IsSuccess)
  323. {
  324. if (obj.MessageKey.Equals(MessageKey.AutoMode)) { this.VehicleModeChang(true); }
  325. if (obj.MessageKey.Equals(MessageKey.ManualMode)) { this.VehicleModeChang(false); }
  326. }
  327. else
  328. {
  329. }
  330. }
  331. void UICallBackModelPropertyChange(GUIMessageEventArgs args)
  332. {
  333. if (args.MessageKey.Equals(MessageKey.Alarm))
  334. {
  335. var hisAlarm = args.Args as HisAlarm;
  336. this.messageController.ShowNotificationView(args.MessageText);
  337. }
  338. }
  339. private void ExecuteTextCommand()
  340. {
  341. //var m = new VCSMessageEventArgs();
  342. //m.MessageKey = 1234;
  343. //m.MessageText = "Test Message";
  344. //m.Command = new Common.Model.Command() { CommandID = "TestCommand123", Type = Common.Shareds.eCommandType.Move };
  345. //this.eventAggregator.GetEvent<VCSMessagePubSubEvent>().Publish(m);
  346. }
  347. public void InitViewModel()
  348. {
  349. //VCSystem.Instance.Init();
  350. //var containerRegistry = ServiceLocator.Current.GetInstance<VCSystem>();
  351. //if ( containerRegistry.Equals( this.VCSystem ) )
  352. //{
  353. // Console.WriteLine( "==" );
  354. //}
  355. //var vsys = containerRegistry.GetContainer().Resolve<IContainerProvider>();
  356. regionManager.RequestNavigate(RegionNames.MainView, "AutoView");
  357. }
  358. public void Dispose()
  359. {
  360. //VCSystem.Instance.Dispose();
  361. }
  362. public static void SelectCulture(string culture)
  363. {
  364. if (String.IsNullOrEmpty(culture))
  365. return;
  366. //Copy all MergedDictionarys into a auxiliar list.
  367. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  368. //Search for the specified culture.
  369. string requestedCulture = string.Format("StringResources.{0}.xaml", culture);
  370. var resourceDictionary = dictionaryList.
  371. FirstOrDefault(d => d.Source.OriginalString == "/OHV.ResourceDic;component/Resources/StringResource.en-US.xaml");
  372. if (resourceDictionary == null)
  373. {
  374. //If not found, select our default language.
  375. requestedCulture = "StringResources.xaml";
  376. resourceDictionary = dictionaryList.
  377. FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  378. }
  379. //If we have the requested resource, remove it from the list and place at the end.
  380. //Then this language will be our string table to use.
  381. if (resourceDictionary != null)
  382. {
  383. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
  384. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  385. }
  386. //Inform the threads of the new culture.
  387. Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
  388. Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
  389. }
  390. }
  391. }