using Admin.Core.IService; using Aucma.Core.PrintTo.Common; using Aucma.Core.PrintTo.Views; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using log4net; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace Aucma.Core.PrintTo.ViewModels { public partial class MainWindowViewModel : ObservableObject { private static readonly log4net.ILog log = LogManager.GetLogger(typeof(MainWindowViewModel)); private IndexPageView firstPage = new IndexPageView();//首页 ISysUserInfoServices _sysUserInfoServices; public MainWindowViewModel() { UserContent = firstPage; _sysUserInfoServices = App.ServiceProvider.GetService(); Task.Run(async () => { await RefreshTeamTime();//班组时间 }); DispatcherTimer timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 1); //间隔1秒 timer.Tick += new EventHandler(timer_Tick); timer.Start(); } #region 更换界面 public System.Windows.Controls.UserControl _content; public System.Windows.Controls.UserControl UserContent { get => _content; set => SetProperty(ref _content, value); } #endregion #region plc 状态 /// /// UI plc 展示状态-文字 /// public string _plcUIStatus; public string PlcUIStatusWb { //get { return plcUIStatusWb; } //set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); } get => _plcUIStatus; set => SetProperty(ref _plcUIStatus, value); } /// /// UI plc 展示状态-颜色 /// public string _plcUIColor; public string PlcUIColor { //get { return plcUIColor; } //set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); } get => _plcUIColor; set => SetProperty(ref _plcUIColor, value); } /// /// UI plc 展示状态-图标 /// public string _plcUIIcon; public string PlcUIIcon { //get { return plcUIIcon; } //set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); } get => _plcUIIcon; set => SetProperty(ref _plcUIIcon, value); } /// /// PLC连接状态-true:连接成功;false:失败 /// /// public void PlcState(bool type) { if (type) { PlcUIStatusWb = "PLC连接成功"; PlcUIColor = "White"; PlcUIIcon = "Assets/Images/正常.png"; } else { PlcUIStatusWb = "PLC状态异常"; PlcUIColor = "Red"; PlcUIIcon = "Assets/Images/失败-01.png"; } } #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) { Application.Current.Shutdown(); Environment.Exit(0); } break; // 打开软盘 case "TabTip": CommHelper.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 string _currentDateTime; public string CurrentDateTime { get => _currentDateTime; set => SetProperty(ref _currentDateTime, value); } #endregion #region 班组信息 /// /// 班组信息 /// public string _teamName; public string TeamName { get => _teamName; set => SetProperty(ref _teamName, value); } #endregion #region 切换班组 public async Task RefreshTeamTime() { while (true) { var list = await _sysUserInfoServices.GetTeamData(); if (list != null && list.Count > 0) { var sysUserInfo = list.First(); TeamName = $"{sysUserInfo.TeamName}({list.Min(d => d.StartTime).ToString("HH:mm")}~{list.Max(d => d.EndTime).ToString("HH:mm")})"; //TeamName = $"{sysUserInfo.TeamName}"; } Thread.Sleep(5000); } } #endregion #region 定时刷新时间 void timer_Tick(object sender, EventArgs e) { System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { CurrentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); })); } #endregion } }