| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- using Basler.Pylon;
- using CSScriptLib;
- using NPOI.SS.Formula.Functions;
- using NPOI.XSSF.Streaming.Values;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Media;
- using Team.FFFeederService.Interfaces;
- using TeamAAS_VP;
- using TeamAAS_VP.Controls;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Data;
- using TeamAAS_VP.Enums;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.Services;
- using TeamAAS_VP.ViewModels.Home;
- using TeamAAS_VP.Views;
- using WPFLocalizeExtension.Engine;
- using static MaterialDesignThemes.Wpf.Theme.ToolBar;
- namespace TeamAAS_VP.ViewModels
- {
- public class MainWindowViewModel : BindableBase
- {
- IRegionManager _regionManager;
- IRegionNavigationService _regionNavigationService;
- IContainerProvider _container;
- IDialogService _dialogService;
- IEventAggregator _eventAggregator;
- IConfigService _configService;
- IRobotService _robotService;
- ICameraService _cameraService;
- IFeederService _feederService;
- IPlcService _plcService;
- IProductService _productService;
- ICalibrationService _calibrationService;
- ISystemDatabaseService _systemDatabaseService;
- IDatabaseInitializer _databaseInitializer;
-
- private Guid CurrentTime;
- private Guid RunTime;
- private DateTime StartTime;
- private Guid currentUser;
- Timer timer;
- #region 属性
- private Management _management;
- public Management management
- {
- get { return _management; }
- set { SetProperty(ref _management, value); }
- }
- private string _CompanyTitle;
- public string CompanyTitle
- {
- get { return _CompanyTitle; }
- set { SetProperty(ref _CompanyTitle, value); }
- }
- private string _AppVersion;
- public string AppVersion
- {
- get { return _AppVersion; }
- set { SetProperty(ref _AppVersion, value); }
- }
- private int _NavigationIndex = 0;
- public int NavigationIndex
- {
- get { return _NavigationIndex; }
- set { SetProperty(ref _NavigationIndex, value); }
- }
- private ObservableCollection<StatusInfo> _Status;
- public ObservableCollection<StatusInfo> Status
- {
- get { return _Status; }
- set { SetProperty(ref _Status, value); }
- }
- private string _MyTitle;
- public string MyTitle
- {
- get { return _MyTitle; }
- set { SetProperty(ref _MyTitle, value); }
- }
- #endregion
- #region 命令
- private DelegateCommand<string> _NavigationCommand;
- public DelegateCommand<string> NavigationCommand
- {
- get { return _NavigationCommand; }
- set { _NavigationCommand = value; }
- }
- public DelegateCommand LoadedCommand { get; set; }
- public DelegateCommand ClosedCommand { get; set; }
- #endregion
- #region 事件
- #endregion
- public MainWindowViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
- IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
- ICalibrationService calibrationService, ISystemDatabaseService systemDatabaseService, IDatabaseInitializer databaseInitializer)
- {
- _regionManager = regionManager;
- _regionNavigationService = regionNavigationService;
- _container = container;
- _dialogService = dialogService;
- _eventAggregator = ea;
- _configService = configService;
- _robotService = robotService;
- _cameraService = cameraService;
- _feederService = feederService;
- _plcService = plcService;
- _productService = productService;
- _calibrationService = calibrationService;
- _systemDatabaseService = systemDatabaseService;
- _databaseInitializer = databaseInitializer;
- //订阅权限登录事件
- _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
- management = _container.Resolve<Management>();
- NavigationCommand = new DelegateCommand<string>(NavigationVoid);
- LoadedCommand = new DelegateCommand(OnLoad);
- ClosedCommand = new DelegateCommand(OnClose);
- Status = management.Status;
- }
- #region 方法
- //上一次导航的名称
- string lastNavigationName = string.Empty;
- private void NavigationVoid(string obj)
- {
- if(!string.IsNullOrEmpty(lastNavigationName))
- {
- if (lastNavigationName == obj)
- {
- return;
- }
- }
- lastNavigationName = obj;
- switch (obj)
- {
- case "Home":
- _regionManager.RequestNavigate("MainRegionContext", "HomeView");
- NavigationIndex = 0;
- _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
- break;
- case "Products":
- _regionManager.RequestNavigate("MainRegionContext", "ProductsView");
- NavigationIndex = 1;
- _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
- break;
- case "Debug":
- _regionManager.RequestNavigate("MainRegionContext", "DebugView");
- NavigationIndex = 2;
- _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
- break;
- case "Setting":
- _regionManager.RequestNavigate("MainRegionContext", "SettingView");
- NavigationIndex = 3;
- _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
- break;
- case "Calib":
- _regionManager.RequestNavigate("MainRegionContext", "CalibrationView");
- NavigationIndex = 5;
- _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
- break;
- case "Statistics":
- _regionManager.RequestNavigate("MainRegionContext", "StatisticsView");
- NavigationIndex = 6;
- _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
- break;
- case "User":
- _regionManager.RequestNavigate("MainRegionContext", "UserView");
- NavigationIndex = 7;
- _eventAggregator.GetEvent<MainTabSwitchNotification>().Publish(NavigationIndex);
- break;
- default:
- break;
- }
- }
- private async void OnLoad()
- {
- // 设置标题和版本号
- var systemconfig = _configService.GetSystemConfiguration();
- MyTitle = systemconfig.Title;
- CompanyTitle = systemconfig.CompanyTitle;
- AppVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
- systemconfig.SoftwareVersion = AppVersion;
- _configService.SaveSystemConfiguration(systemconfig);
- SendTaskMessage(Lang.软件启动, MessageLevel.Info);
- StartTime = DateTime.Now;
- _regionManager.RequestNavigate("MainRegionContext", "HomeView");
- double max = 14;
- double curvalue = 0;
- currentUser = Guid.NewGuid();
- Status.Add(new StatusInfo(currentUser, $"{Lang.当前用户}:{_systemDatabaseService.GetCurrentUser().UserName}[{_systemDatabaseService.GetCurrentUser().userPart.ToString()}]", new SolidColorBrush(Colors.White)));
- var _= Application.Current.Dispatcher.BeginInvoke(new Action(() => { _dialogService.ShowDialog("UProgressBar", rst => { }); }));
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.初始化数据库}...", Value = curvalue });
- }));
- // 2. 加载校准
- _calibrationService.LoadAll();
- //-------------------SETP 1: 初始化数据库-----------------------------------------------------------------------
- await Task.Delay(200);
- //初始化数据库
- DatabaseHelper.InitDatabase();
- SendTaskMessage($"{Lang.数据库读取完成}!", MessageLevel.Info);
- curvalue += 1;
- //-------------------SETP 2: 初始化Feeder-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化Feeder}...", Value = curvalue });
- }));
- await management.InitFeeders();
- //-------------------SETP 3: 初始化PLC-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化PLC}...", Value = curvalue });
- }));
- await management.InitPlc();
- //-------------------SETP 4: 初始化Robot-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化机器人}...", Value = curvalue });
- }));
- await management.InitRobots();
- //-------------------SETP 5: 初始化Camera-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化相机}...", Value = curvalue });
- }));
- await management.InitCameras();
- //-------------------SETP 6: 初始化光源控制器-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在初始化光源控制器}...", Value = curvalue });
- }));
- await management.InitLightControllers();
- //-------------------SETP 7: 初始化电批-----------------------------------------------------------------------
- //curvalue += 1;
- //_ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- //{
- // _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"正在连接电批...", Value = curvalue });
- //}));
- //await management.InitScrewDriver();
- //-------------------SETP 8: 加载所有产品-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.加载所有产品}...", Value = curvalue });
- }));
- await _productService.InitializeAsync();
- //-------------------SETP 9: 开启服务器监听-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.开启服务器监听}...", Value = curvalue });
- }));
- management.InitBgTcp();
- //-------------------SETP 10: 开启Modbus Tcp-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在开启}Modbus TCP Slave...", Value = curvalue });
- }));
- management.InitModbusTcp();
- //-------------------SETP 11: 开启后台脚本-----------------------------------------------------------------------
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在开启后台脚本}...", Value = curvalue });
- }));
- management.InitBackgroundcript();
- //当前日期
- CurrentTime = Guid.NewGuid();
- Status.Add(new StatusInfo(CurrentTime, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), new SolidColorBrush(Colors.White)));
- //当前运行时长
- RunTime = Guid.NewGuid();
- var run = (DateTime.Now - StartTime);
- string formattedTime = string.Format(Lang.已运行时长, (int)run.TotalDays, run.Hours, run.Minutes, run.Seconds);
- Status.Add(new StatusInfo(RunTime, formattedTime, new SolidColorBrush(Colors.White)));
- timer = new Timer(DoTime, null, 1000, 1000);
- curvalue += 1;
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.正在加载产品}...", Value = curvalue });
- }));
- Guid productid = _configService.GetSystemConfiguration().LastOpenedProductId;
- if (productid != Guid.Empty)
- {
- var res = _productService.LoadProduct(productid);
- _productService.SetCurrentProduct(productid);
- foreach (var item1 in res.CameraProcedures)
- {
- foreach (var item2 in item1.ProcedureModels)
- {
- if (item2.IsImageVisibility)
- {
- management.Renders.Add(new ShowRender() { Id = item2.Id, ProceductName = item2.Name });
- }
- }
- }
- _container.Resolve<ShowVisionRenderViewModel>().Renders = management.Renders;
- await management.WriteParameterToPlcExAsync();
- }
- var homeviewmodel = _container.Resolve<HomeViewModel>();
- homeviewmodel.OnNavigatedTo(null);
- _ = App.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- _eventAggregator.GetEvent<ProgressNotification>().Publish(new UProgressBar.ProgressParameter { Maximum = max, Minimum = 0, SubTitle = $"{Lang.初始化中}...", Message = $"{Lang.完成}", Value = -1 });
- }));
- }
- private void OnClose()
- {
- }
- private void SendTaskMessage(string msg, MessageLevel level)
- {
- _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
- }
- private void DoTime(object state)
- {
- //当前日期
- var c = Status.FirstOrDefault(b => b.ID == CurrentTime);
- c.Message = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- //当前运行时长
- var d = Status.FirstOrDefault(b => b.ID == RunTime);
- var run = (DateTime.Now - StartTime);
- d.Message = string.Format(Lang.已运行时长, (int)run.TotalDays, run.Hours, run.Minutes, run.Seconds);
- }
- private void LoginChange(Models.User user)
- {
- var us = Status.FirstOrDefault(u => u.ID == currentUser);
- if (us != null)
- {
- us.Message = $"{Lang.当前用户}:{_systemDatabaseService.GetCurrentUser().UserName}[{_systemDatabaseService.GetCurrentUser().userPart.ToString()}]";
- }
- }
- #endregion
- }
- }
|