using Admin.Core.Common; using Aucma.Core.BoxFoam.Business; using Aucma.Core.BoxFoam.Views; using Aucma.Core.HwPLc; using Aucma.Core.Scanner; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using log4net; using System; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace Aucma.Core.BoxFoam.ViewModels { public partial class MainWindowViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(MainWindowViewModel)); private IndexPageView firstPage = new IndexPageView();//首页 CollectionFoamLine line = new CollectionFoamLine(); CollectionFoamMachine machine = new CollectionFoamMachine(); RealTimeInventoryPageView realTimeInventoryPage = new RealTimeInventoryPageView(); EnterWarehouseStatisticsPageView enterWarehouseStatisticsPage = new EnterWarehouseStatisticsPageView(); FoamPlanPageView foamPlanPageView = new FoamPlanPageView(); FoamMonitorPageView foamMonitorPageView = new FoamMonitorPageView(); MonitorPageView monitorPage = new MonitorPageView();//任务监控 FoamMachinesPageView foamMachinesPageView = new FoamMachinesPageView(); public MainWindowViewModel() { UserContent = firstPage; // 创建一个DispatcherTimer对象 DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += Timer_Tick; timer.Start(); Task.Run(() => { init(); }); TeamSwitchBusiness teamSwitchBusiness = new TeamSwitchBusiness(); teamSwitchBusiness.Init(); } public Task init() { // 设备状态刷新定时器 System.Timers.Timer timer = new System.Timers.Timer(1000 * 5); timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshStatus); timer.AutoReset = true; timer.Enabled = true; timer.Start(); return Task.CompletedTask; } #region 设备状态刷新 /// /// 设备状态刷新 /// /// /// public void RefreshStatus(object sender, System.Timers.ElapsedEventArgs e) { RefreshMesDb(); RefreshPlc(); RefreshScanner(); } /// /// 数据库状态刷新 /// /// /// public void RefreshMesDb() { MesDbState(true); } /// /// plc状态刷新 /// /// /// public void RefreshPlc() { var info = PlcHelper.siemensList.ToList(); if (info != null) { if (info.Count > 0) { PlcUIColor.Clear(); info = info.OrderBy(x => x.Id).ToList(); PlcUIColor.Clear(); PlcUIIcon.Clear(); foreach (var item in info) { if (item.plc.IsConnected) { PlcState(true); } else { PlcState(false); } } } } for (int i = PlcUIColor.Count - 1; i < 4; i++) { PlcUIColor.Add("Red"); PlcUIIcon.Add("Assets/Images/Red.png"); } } /// /// 扫码器状态刷新 /// /// /// public void RefreshScanner() { string ip1 = Appsettings.app("Middleware", "Scanner1", "Ip"); bool flag1 = MvCodeHelper.ConnectionStatus(ip1); Scanner1State(flag1); } #endregion #region 更换界面 public System.Windows.Controls.UserControl _content; public System.Windows.Controls.UserControl UserContent { get => _content; set => SetProperty(ref _content, value); } #endregion #region 窗口操作 /// /// 窗口操作 /// [RelayCommand] private void FormControl(object obj) { try { string controlType = obj as string; switch (controlType) { // 关闭当前窗口 case "Exit": if (System.Windows.MessageBox.Show("确定要退出系统吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes) { log.Warn($"系统退出,当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); Application.Current.Shutdown(); Environment.Exit(0); } break; // 打开软盘 case "TabTip": OpenOsk(); break; // 还原 或者 最大化当前窗口 case "Normal": if (Application.Current.MainWindow.WindowState == WindowState.Normal) { Application.Current.MainWindow.WindowState = WindowState.Maximized; break; } if (Application.Current.MainWindow.WindowState == WindowState.Maximized) { Application.Current.MainWindow.WindowState = WindowState.Normal; break; } break; // 最小化当前窗口 case "Minimized": Application.Current.MainWindow.WindowState = WindowState.Minimized; break; default: break; } } catch (Exception ex) { log.Error("窗体控制逻辑异常", ex); } } #endregion #region 打开软盘 /// /// 打开软盘 /// public static void OpenOsk() { Process proc = new Process(); proc.StartInfo.FileName = @"C:\Windows\System32\osk.exe"; proc.StartInfo.UseShellExecute = true; proc.StartInfo.Verb = "runas"; proc.Start(); } #endregion #region 界面切换 /// /// 界面切换 /// [RelayCommand] private void SwitchPages(string page) { switch (page) { case "FirstPage": UserContent = firstPage; break; case "RealTimeInventoryPage": UserContent = realTimeInventoryPage; break; case "StatisticsPage": UserContent = enterWarehouseStatisticsPage; break; case "MonitorPage": UserContent = monitorPage; break; case "FoamPlanPage": UserContent = foamPlanPageView; break; case "FoamMonitorPage": UserContent = foamMonitorPageView; break; case "FoamMachinesPage": UserContent = foamMachinesPageView; break; default: break; } } #endregion #region MES数据库状态 /// /// MES数据库-文字 /// public string _mesDbUIStatusWb; public string MesDbUIStatusWb { get => _mesDbUIStatusWb; set => SetProperty(ref _mesDbUIStatusWb, value); } /// /// MES数据库-颜色 /// public string _mesDbUIColor; public string MesDbUIColor { get => _mesDbUIColor; set => SetProperty(ref _mesDbUIColor, value); } /// /// MES数据库-图标 /// public string _mesUIIcon; public string MesUIIcon { get => _mesUIIcon; set => SetProperty(ref _mesUIIcon, value); } /// /// MES数据库连接状态 /// /// public void MesDbState(bool type) { Application.Current.Dispatcher.Invoke(() => { if (type) { MesDbUIStatusWb = "MES数据库"; MesDbUIColor = "Green"; MesUIIcon = "Assets/Images/Green.png"; } else { MesDbUIStatusWb = "MES数据库"; MesDbUIColor = "Red"; MesUIIcon = "Assets/Images/Red.png"; } }); } #endregion #region plc 状态 /// /// UI plc 展示状态-文字 /// public string _plcUIStatusWb; public string PlcUIStatusWb { get => _plcUIStatusWb; set => SetProperty(ref _plcUIStatusWb, value); } /// /// UI plc 展示状态-颜色 /// private ObservableCollection _plcUIColor = new ObservableCollection(); public ObservableCollection PlcUIColor { get => _plcUIColor; set => SetProperty(ref _plcUIColor, value); } /// /// UI plc 展示状态-图标 /// private ObservableCollection _plcUIIcon = new ObservableCollection(); public ObservableCollection PlcUIIcon { get => _plcUIIcon; set => SetProperty(ref _plcUIIcon, value); } /// /// PLC连接状态-true:连接成功;false:失败 /// /// public void PlcState(bool type) { Application.Current.Dispatcher.Invoke(() => { if (type) { PlcUIStatusWb = "PLC"; PlcUIColor.Add("Green"); PlcUIIcon.Add("Assets/Images/Green.png"); } else { PlcUIStatusWb = "PLC"; PlcUIColor.Add("Red"); PlcUIIcon.Add("Assets/Images/Red.png"); } }); } #endregion #region 扫码器1状态 /// /// UI 展示状态-文字 /// public string _scanner1UIStatusWb; public string Scanner1UIStatusWb { //get { return plcUIStatusWb; } //set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); } get => _scanner1UIStatusWb; set => SetProperty(ref _scanner1UIStatusWb, value); } /// /// UI 展示状态-颜色 /// public string _scanner1UIColor; public string Scanner1UIColor { //get { return plcUIColor; } //set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); } get => _scanner1UIColor; set => SetProperty(ref _scanner1UIColor, value); } /// /// UI 展示状态-图标 /// public string _scanner1UIIcon; public string Scanner1UIIcon { //get { return plcUIIcon; } //set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); } get => _scanner1UIIcon; set => SetProperty(ref _scanner1UIIcon, value); } /// /// 扫码器1连接状态-true:连接成功;false:失败 /// /// public void Scanner1State(bool type) { Application.Current.Dispatcher.Invoke(() => { if (type) { Scanner1UIStatusWb = "扫码器"; Scanner1UIColor = "Green"; Scanner1UIIcon = "Assets/Images/Green.png"; } else { Scanner1UIStatusWb = "扫码器"; Scanner1UIColor = "Red"; Scanner1UIIcon = "Assets/Images/Red.png"; } }); } #endregion public string _currentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); public string CurrentDateTime { get => _currentDateTime; set => SetProperty(ref _currentDateTime, value); } public string _shiftStr = string.Empty; public string ShiftStr { get => _shiftStr; set => SetProperty(ref _shiftStr, value); } private void Timer_Tick(object sender, EventArgs e) { DateTime now = DateTime.Now; // 判断当前是否是白班时间段 if (now.Hour >= 8 && now.Hour < 20) { ShiftStr = $"白班 08点-20点"; } else { ShiftStr = $"夜班 20点-08点"; } CurrentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } } }