MainWindowViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using Basler.Pylon;
  2. using CSScriptLib;
  3. using NPOI.SS.Formula.Functions;
  4. using NPOI.XSSF.Streaming.Values;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Globalization;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Controls.Primitives;
  23. using System.Windows.Media;
  24. using Team.FFFeederService.Interfaces;
  25. using TeamAAS_VP;
  26. using TeamAAS_VP.Controls;
  27. using TeamAAS_VP.Core;
  28. using TeamAAS_VP.Data;
  29. using TeamAAS_VP.Enums;
  30. using TeamAAS_VP.Events;
  31. using TeamAAS_VP.Interfaces;
  32. using TeamAAS_VP.Models;
  33. using TeamAAS_VP.Resources.Languages;
  34. using TeamAAS_VP.Services;
  35. using TeamAAS_VP.Views;
  36. using WPFLocalizeExtension.Engine;
  37. using static MaterialDesignThemes.Wpf.Theme.ToolBar;
  38. namespace TeamAAS_VP.ViewModels
  39. {
  40. public class MainWindowViewModel : BindableBase
  41. {
  42. IRegionManager _regionManager;
  43. IRegionNavigationService _regionNavigationService;
  44. IContainerProvider _container;
  45. IDialogService _dialogService;
  46. IEventAggregator _eventAggregator;
  47. IConfigService _configService;
  48. IRobotService _robotService;
  49. ICameraService _cameraService;
  50. IFeederService _feederService;
  51. IPlcService _plcService;
  52. IProductService _productService;
  53. ICalibrationService _calibrationService;
  54. ISystemDatabaseService _systemDatabaseService;
  55. IDatabaseInitializer _databaseInitializer;
  56. private Management management;
  57. private Guid CurrentTime;
  58. private Guid RunTime;
  59. private DateTime StartTime;
  60. private Guid currentUser;
  61. Timer timer;
  62. #region 属性
  63. private string _CompanyTitle;
  64. public string CompanyTitle
  65. {
  66. get { return _CompanyTitle; }
  67. set { SetProperty(ref _CompanyTitle, value); }
  68. }
  69. private string _AppVersion;
  70. public string AppVersion
  71. {
  72. get { return _AppVersion; }
  73. set { SetProperty(ref _AppVersion, value); }
  74. }
  75. private int _NavigationIndex = 0;
  76. public int NavigationIndex
  77. {
  78. get { return _NavigationIndex; }
  79. set { SetProperty(ref _NavigationIndex, value); }
  80. }
  81. private ObservableCollection<StatusInfo> _Status;
  82. public ObservableCollection<StatusInfo> Status
  83. {
  84. get { return _Status; }
  85. set { SetProperty(ref _Status, value); }
  86. }
  87. private string _MyTitle;
  88. public string MyTitle
  89. {
  90. get { return _MyTitle; }
  91. set { SetProperty(ref _MyTitle, value); }
  92. }
  93. #endregion
  94. #region 命令
  95. private DelegateCommand<string> _NavigationCommand;
  96. public DelegateCommand<string> NavigationCommand
  97. {
  98. get { return _NavigationCommand; }
  99. set { _NavigationCommand = value; }
  100. }
  101. public DelegateCommand LoadedCommand { get; set; }
  102. public DelegateCommand ClosedCommand { get; set; }
  103. #endregion
  104. #region 事件
  105. #endregion
  106. public MainWindowViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  107. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
  108. ICalibrationService calibrationService, ISystemDatabaseService systemDatabaseService, IDatabaseInitializer databaseInitializer)
  109. {
  110. _regionManager = regionManager;
  111. _regionNavigationService = regionNavigationService;
  112. _container = container;
  113. _dialogService = dialogService;
  114. _eventAggregator = ea;
  115. _configService = configService;
  116. _robotService = robotService;
  117. _cameraService = cameraService;
  118. _feederService = feederService;
  119. _plcService = plcService;
  120. _productService = productService;
  121. _calibrationService = calibrationService;
  122. _systemDatabaseService = systemDatabaseService;
  123. _databaseInitializer = databaseInitializer;
  124. //订阅权限登录事件
  125. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  126. management = _container.Resolve<Management>();
  127. NavigationCommand = new DelegateCommand<string>(NavigationVoid);
  128. LoadedCommand = new DelegateCommand(OnLoad);
  129. ClosedCommand = new DelegateCommand(OnClose);
  130. Status = management.Status;
  131. }
  132. #region 方法
  133. //上一次导航的名称
  134. string lastNavigationName = string.Empty;
  135. private void NavigationVoid(string obj)
  136. {
  137. if (!string.IsNullOrEmpty(lastNavigationName))
  138. {
  139. if (lastNavigationName == obj)
  140. {
  141. return;
  142. }
  143. }
  144. lastNavigationName = obj;
  145. switch (obj)
  146. {
  147. case "Home":
  148. _regionManager.RequestNavigate("MainRegionContext", "HomeView");
  149. NavigationIndex = 0;
  150. _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
  151. break;
  152. case "Products":
  153. _regionManager.RequestNavigate("MainRegionContext", "ProductsView");
  154. NavigationIndex = 1;
  155. _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
  156. break;
  157. case "Debug":
  158. _regionManager.RequestNavigate("MainRegionContext", "DebugView");
  159. NavigationIndex = 2;
  160. _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
  161. break;
  162. case "Setting":
  163. _regionManager.RequestNavigate("MainRegionContext", "SettingView");
  164. NavigationIndex = 3;
  165. _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
  166. break;
  167. case "Calib":
  168. _regionManager.RequestNavigate("MainRegionContext", "CalibrationView");
  169. NavigationIndex = 5;
  170. _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
  171. break;
  172. case "Statistics":
  173. _regionManager.RequestNavigate("MainRegionContext", "StatisticsView");
  174. NavigationIndex = 6;
  175. _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
  176. break;
  177. case "User":
  178. _regionManager.RequestNavigate("MainRegionContext", "UserView");
  179. NavigationIndex = 7;
  180. _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. private async void OnLoad()
  187. {
  188. // 设置标题和版本号
  189. var systemconfig = _configService.GetSystemConfiguration();
  190. MyTitle = systemconfig.Title;
  191. CompanyTitle = systemconfig.CompanyTitle;
  192. AppVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  193. systemconfig.SoftwareVersion = AppVersion;
  194. _configService.SaveSystemConfiguration(systemconfig);
  195. // 设置语言
  196. //#region 设置语言
  197. //var currentlanguage = _configService.GetSystemConfiguration().CurrentLanguage;
  198. //_configService.SetCurrentLanguage(currentlanguage);
  199. //#endregion
  200. SendTaskMessage(Lang.软件启动, MessageLevel.Info);
  201. StartTime = DateTime.Now;
  202. _regionManager.RequestNavigate("MainRegionContext", "HomeView");
  203. double max = 14;
  204. double curvalue = 0;
  205. currentUser = Guid.NewGuid();
  206. Status.Add(new StatusInfo(currentUser, $"{Lang.当前用户}:{_systemDatabaseService.GetCurrentUser().UserName}[{_systemDatabaseService.GetCurrentUser().userPart.ToString()}]", new SolidColorBrush(Colors.White)));
  207. var _ = Application.Current.Dispatcher.BeginInvoke(new Action(() => { _dialogService.ShowDialog("UProgressBar", rst => { }); }));
  208. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  209. {
  210. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.初始化数据库}...", Value = curvalue });
  211. }));
  212. // 2. 加载校准
  213. _calibrationService.LoadAll();
  214. //-------------------SETP 1: 初始化数据库-----------------------------------------------------------------------
  215. await Task.Delay(200);
  216. //初始化数据库
  217. DatabaseHelper.InitDatabase();
  218. SendTaskMessage($"{Lang.数据库读取完成}!", MessageLevel.Info);
  219. curvalue += 1;
  220. //
  221. await management.ReadConfig();
  222. //-------------------SETP 2: 初始化Feeder-----------------------------------------------------------------------
  223. curvalue += 1;
  224. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  225. {
  226. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化Feeder}...", Value = curvalue });
  227. }));
  228. await management.InitFeeders();
  229. //-------------------SETP 3: 初始化PLC-----------------------------------------------------------------------
  230. curvalue += 1;
  231. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  232. {
  233. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化PLC}...", Value = curvalue });
  234. }));
  235. await management.InitPlc();
  236. //-------------------SETP 4: 初始化Robot-----------------------------------------------------------------------
  237. curvalue += 1;
  238. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  239. {
  240. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化机器人}...", Value = curvalue });
  241. }));
  242. await management.InitRobots();
  243. //-------------------SETP 5: 初始化Camera-----------------------------------------------------------------------
  244. curvalue += 1;
  245. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  246. {
  247. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化相机}...", Value = curvalue });
  248. }));
  249. await management.InitCameras();
  250. //-------------------SETP 6: 初始化光源控制器-----------------------------------------------------------------------
  251. curvalue += 1;
  252. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  253. {
  254. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化光源控制器}...", Value = curvalue });
  255. }));
  256. await management.InitLightControllers();
  257. //-------------------SETP 7: 初始化电批-----------------------------------------------------------------------
  258. curvalue += 1;
  259. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  260. {
  261. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = Lang.正在连接电批, Value = curvalue });
  262. }));
  263. await management.InitScrewDriver();
  264. //-------------------SETP 8: 加载所有产品-----------------------------------------------------------------------
  265. curvalue += 1;
  266. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  267. {
  268. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.加载所有产品}...", Value = curvalue });
  269. }));
  270. await _productService.InitializeAsync();
  271. //-------------------SETP 9: 开启服务器监听-----------------------------------------------------------------------
  272. curvalue += 1;
  273. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  274. {
  275. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.开启服务器监听}...", Value = curvalue });
  276. }));
  277. management.InitBgTcp();
  278. //-------------------SETP 10: 开启Modbus Tcp-----------------------------------------------------------------------
  279. curvalue += 1;
  280. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  281. {
  282. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在开启}Modbus TCP Slave...", Value = curvalue });
  283. }));
  284. management.InitModbusTcp();
  285. //-------------------SETP 11: 开启后台脚本-----------------------------------------------------------------------
  286. curvalue += 1;
  287. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  288. {
  289. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在开启后台脚本}...", Value = curvalue });
  290. }));
  291. management.InitBackgroundcript();
  292. //-------------------SETP 12: 开启扭力测量仪-----------------------------------------------------------------------
  293. curvalue += 1;
  294. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  295. {
  296. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在开启}...", Value = curvalue });
  297. }));
  298. await management.InitSignal();
  299. //当前日期
  300. CurrentTime = Guid.NewGuid();
  301. Status.Add(new StatusInfo(CurrentTime, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), new SolidColorBrush(Colors.White)));
  302. //当前运行时长
  303. RunTime = Guid.NewGuid();
  304. var run = (DateTime.Now - StartTime);
  305. string formattedTime = string.Format(Lang.已运行时长, (int)run.TotalDays, run.Hours, run.Minutes, run.Seconds);
  306. Status.Add(new StatusInfo(RunTime, formattedTime, new SolidColorBrush(Colors.White)));
  307. timer = new Timer(DoTime, null, 1000, 1000);
  308. curvalue += 1;
  309. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  310. {
  311. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在加载产品}...", Value = curvalue });
  312. }));
  313. Guid productid = _configService.GetSystemConfiguration().LastOpenedProductId;
  314. if (productid != Guid.Empty)
  315. {
  316. await _productService.LoadProductAsync(productid);
  317. _productService.SetCurrentProduct(productid);
  318. await management.WritePositionToPlcAsync();
  319. }
  320. var homeviewmodel = _container.Resolve<HomeViewModel>();
  321. homeviewmodel.OnNavigatedTo(null);
  322. _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
  323. {
  324. _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.完成}", Value = -1 });
  325. }));
  326. }
  327. private void OnClose()
  328. {
  329. }
  330. private void SendTaskMessage(string msg, MessageLevel level)
  331. {
  332. _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
  333. }
  334. private void DoTime(object state)
  335. {
  336. //当前日期
  337. var c = Status.FirstOrDefault(b => b.ID == CurrentTime);
  338. c.Message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  339. //当前运行时长
  340. var d = Status.FirstOrDefault(b => b.ID == RunTime);
  341. var run = (DateTime.Now - StartTime);
  342. d.Message = string.Format(Lang.已运行时长, (int)run.TotalDays, run.Hours, run.Minutes, run.Seconds);
  343. }
  344. private void LoginChange(Models.User user)
  345. {
  346. var us = Status.FirstOrDefault(u => u.ID == currentUser);
  347. if (us != null)
  348. {
  349. us.Message = $"{Lang.当前用户}:{_systemDatabaseService.GetCurrentUser().UserName}[{_systemDatabaseService.GetCurrentUser().userPart.ToString()}]";
  350. }
  351. }
  352. #endregion
  353. }
  354. }