D_MainWindowViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using CommonServiceLocator;
  2. using OHV.Common.Events;
  3. using OHV.Common.Shareds;
  4. using OHV.Module.Interactivity;
  5. using OHV.SqliteDAL;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Threading;
  22. using VehicleControlSystem;
  23. namespace OHV.Vehicle.Concept
  24. {
  25. class D_MainWindowViewModel : BindableBase
  26. {
  27. private string _title = "Prism Unity Application";
  28. public string Title
  29. {
  30. get { return _title; }
  31. set { SetProperty( ref _title , value ); }
  32. }
  33. private double _cpu;
  34. public double CPU
  35. {
  36. get { return this._cpu; }
  37. set { this.SetProperty( ref this._cpu , value ); }
  38. }
  39. private double _totalCPU;
  40. public double TotalCPU
  41. {
  42. get { return this._totalCPU; }
  43. set
  44. {
  45. //this._totalCPU = dti.cpuResult.ToString();
  46. this.SetProperty( ref this._totalCPU , value );
  47. }
  48. }
  49. public int UsageCPU { get; set; }
  50. private double _ram;
  51. public double RAM
  52. {
  53. get { return this._ram; }
  54. set { this.SetProperty( ref this._ram , value ); }
  55. }
  56. private double _cDrive;
  57. public double CDrive
  58. {
  59. get { return this._cDrive; }
  60. set { this.SetProperty( ref this._cDrive , value ); }
  61. }
  62. DateTime _dateTime;
  63. public DateTime DateTime
  64. {
  65. get { return _dateTime; }
  66. set
  67. {
  68. this.SetProperty( ref _dateTime , value );
  69. }
  70. }
  71. private string _vehicleID;
  72. public string VehicleID
  73. {
  74. get { return this._vehicleID; }
  75. set { this.SetProperty( ref this._vehicleID , value ); }
  76. }
  77. private string _vehicleIP = "127.0.0.1";
  78. public string VehicleIP
  79. {
  80. get { return this._vehicleIP; }
  81. set { this.SetProperty( ref this._vehicleIP , value ); }
  82. }
  83. private string _ocsIP;
  84. public string OcsIP
  85. {
  86. get { return this._ocsIP; }
  87. set { this.SetProperty( ref this._ocsIP , value ); }
  88. }
  89. private Brush _ocsBrush;// = Brushes.Gray;
  90. private Brush _batteryBrush;// = Brushes.Gray;
  91. private Brush _laserBrush;// = Brushes.Gray;
  92. public Brush OcsBrush
  93. {
  94. get { return _ocsBrush; }
  95. set
  96. {
  97. SetProperty( ref _ocsBrush , value );
  98. }
  99. }
  100. public Brush BatteryBrush
  101. {
  102. get { return _batteryBrush; }
  103. set
  104. {
  105. SetProperty( ref _batteryBrush , value );
  106. }
  107. }
  108. public Brush LaserBrush
  109. {
  110. get { return this._laserBrush; }
  111. set
  112. {
  113. SetProperty( ref _laserBrush , value );
  114. }
  115. }
  116. private string _swVersion;
  117. public string SwVersion
  118. {
  119. get { return this._swVersion; }
  120. set { this.SetProperty( ref this._swVersion , value ); }
  121. }
  122. private DateTime _lastBuildedTime;
  123. public DateTime LastBuildedTime
  124. {
  125. get { return this._lastBuildedTime; }
  126. set { this.SetProperty( ref this._lastBuildedTime , value ); }
  127. }
  128. IEventAggregator eventAggregator = null;
  129. VCSystem VCSystem = null;
  130. MessageController messageController;
  131. public ICommand TestCommand { get; set; }
  132. public ICommand NavigateCommand { get; set; }
  133. public ICommand SystemOffCommand { get; set; }
  134. public ICommand BuzzerStopCommand { get; set; }
  135. public ICommand EmergencyStopCommand { get; set; }
  136. IRegionManager regionManager;
  137. public Common.Events.HostConnectedEventArgs.eConnectedState State { get; set; }
  138. private DeskTopInfo dti = new DeskTopInfo();
  139. public ICommand ChangeLanguage { get; set; }
  140. private void Execte_ChangeLanguage()
  141. {
  142. //LanguageHalper.LanguagesSelcter.ChangLanguage(LanguageHalper.eLanguageType.Chinese);
  143. }
  144. SqliteManager sql;
  145. public D_MainWindowViewModel( IEventAggregator _ea , VCSystem cSystem , IRegionManager _regionManager , MessageController _msgController, SqliteManager _sql )
  146. {
  147. this.regionManager = _regionManager;
  148. this.eventAggregator = _ea;
  149. this.eventAggregator.GetEvent<GUIMessagePubSubEvent>().Subscribe( UICallbackCommunication , ThreadOption.UIThread );
  150. this.eventAggregator.GetEvent<HostConnectedPubSubEvent>().Subscribe( OcsConnectCallBack , ThreadOption.UIThread , false );
  151. this.VCSystem = cSystem;
  152. this.messageController = _msgController;
  153. this.sql = _sql;
  154. this.VehicleID = sql.ConfigDal.GetK( ConstString.VehicleID ).Value;
  155. //this.VehicleIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  156. this.OcsIP = sql.ConfigDal.GetK( ConstString.Addr ).Value;
  157. //this.TestCommand = new DelegateCommand( ExecuteTextCommand );
  158. this.NavigateCommand = new DelegateCommand<object>( this.Navigate );
  159. this.SystemOffCommand = new DelegateCommand( ExecuteSystemOffCommand );
  160. this.ChangeLanguage = new DelegateCommand(Execte_ChangeLanguage);
  161. DispatcherTimer dateTimer = new DispatcherTimer();
  162. dateTimer.Tick += ( object sender , EventArgs e ) =>
  163. {
  164. this.DateTime = DateTime.Now;
  165. };
  166. dateTimer.Interval = TimeSpan.FromMilliseconds( 500 );
  167. dateTimer.Start();
  168. DispatcherTimer deskTopTimer = new DispatcherTimer();
  169. deskTopTimer.Tick += ( object sender , EventArgs e ) =>
  170. {
  171. this.CPU = dti.GetCpuUsage();
  172. this.UsageCPU = ( int )dti.GetCpuUsage();
  173. this.RAM = dti.GetRamUsage();
  174. };
  175. deskTopTimer.Interval = TimeSpan.FromMilliseconds( 1000 );
  176. deskTopTimer.Start();
  177. this._totalCPU = dti.cpuResult;
  178. this._cDrive = dti.GetCDrive();
  179. this.SwVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  180. this.LastBuildedTime = new Helpler.AssemblyInfo().Get_BuildDateTime();
  181. }
  182. private void OcsConnectCallBack( HostConnectedEventArgs obj )
  183. {
  184. this.State = obj.State;
  185. switch ( this.State )
  186. {
  187. case HostConnectedEventArgs.eConnectedState.Disconnected:
  188. this.OcsBrush = Brushes.Yellow;
  189. break;
  190. case HostConnectedEventArgs.eConnectedState.Connected:
  191. this.OcsBrush = Brushes.LimeGreen;
  192. break;
  193. }
  194. }
  195. private void ExecuteSystemOffCommand( )
  196. {
  197. this.messageController.ShowConfirmationPopupView( "System ShutDown ?" , r =>
  198. {
  199. if ( r.Result == ButtonResult.OK )
  200. App.Current.Shutdown();
  201. } );
  202. }
  203. private void Navigate( object obj )
  204. {
  205. var selectItem = obj.ToString();
  206. if ( !string.IsNullOrEmpty(selectItem ))
  207. regionManager.RequestNavigate( "MainView" , selectItem );
  208. }
  209. private void UICallbackCommunication( GUIMessageEventArgs obj )
  210. {
  211. }
  212. private void ExecuteTextCommand( )
  213. {
  214. var m = new VCSMessageEventArgs();
  215. m.MessageKey = 1234;
  216. m.MessageText = "Test Message";
  217. m.Command = new Common.Model.Command() { CommandID = "TestCommand123" , Type = Common.Shareds.eCommandType.Move };
  218. this.eventAggregator.GetEvent<VCSMessagePubSubEvent>().Publish( m );
  219. }
  220. public void InitViewModel( )
  221. {
  222. //VCSystem.Instance.Init();
  223. //var containerRegistry = ServiceLocator.Current.GetInstance<VCSystem>();
  224. //if ( containerRegistry.Equals( this.VCSystem ) )
  225. //{
  226. // Console.WriteLine( "==" );
  227. //}
  228. //var vsys = containerRegistry.GetContainer().Resolve<IContainerProvider>();
  229. regionManager.RequestNavigate( RegionNames.MainView , "AutoView" );
  230. }
  231. public void Dispose( )
  232. {
  233. //VCSystem.Instance.Dispose();
  234. }
  235. public static void SelectCulture(string culture)
  236. {
  237. if (String.IsNullOrEmpty(culture))
  238. return;
  239. //Copy all MergedDictionarys into a auxiliar list.
  240. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  241. //Search for the specified culture.
  242. string requestedCulture = string.Format("StringResources.{0}.xaml", culture);
  243. var resourceDictionary = dictionaryList.
  244. FirstOrDefault(d => d.Source.OriginalString == "/OHV.ResourceDic;component/Resources/StringResource.en-US.xaml");
  245. if (resourceDictionary == null)
  246. {
  247. //If not found, select our default language.
  248. requestedCulture = "StringResources.xaml";
  249. resourceDictionary = dictionaryList.
  250. FirstOrDefault(d => d.Source.OriginalString == requestedCulture);
  251. }
  252. //If we have the requested resource, remove it from the list and place at the end.
  253. //Then this language will be our string table to use.
  254. if (resourceDictionary != null)
  255. {
  256. Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
  257. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  258. }
  259. //Inform the threads of the new culture.
  260. Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
  261. Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
  262. }
  263. }
  264. }