106 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			106 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C#
		
	
	
	
using DM_Weight.Models;
 | 
						|
using DM_Weight.msg;
 | 
						|
using DM_Weight.Port;
 | 
						|
using DM_Weight.util;
 | 
						|
using log4net;
 | 
						|
using Prism.Commands;
 | 
						|
using Prism.Events;
 | 
						|
using Prism.Mvvm;
 | 
						|
using Prism.Regions;
 | 
						|
using System;
 | 
						|
using System.Collections;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Diagnostics;
 | 
						|
using System.Linq;
 | 
						|
using System.Text;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using System.Windows.Threading;
 | 
						|
 | 
						|
namespace DM_Weight.ViewModels
 | 
						|
{
 | 
						|
    public class EmergencyWindowViewModel : BindableBase, IRegionMemberLifetime, INavigationAware
 | 
						|
    {
 | 
						|
        private readonly ILog logger = LogManager.GetLogger(typeof(HomeWindowViewModel));
 | 
						|
        IEventAggregator _eventAggregator;
 | 
						|
        private PortUtil _portUtil;
 | 
						|
        IRegionManager _regionManager;
 | 
						|
        private bool _dbConnectionFail = !App.DbConnectionFail;
 | 
						|
        public bool DBConnectionStatus { get => _dbConnectionFail; set => SetProperty(ref _dbConnectionFail, value); }
 | 
						|
        private int _status;
 | 
						|
 | 
						|
        public int Status { get => _status; set => SetProperty(ref _status, value); }
 | 
						|
        public EmergencyWindowViewModel(IRegionManager regionManager, PortUtil portUtil, IEventAggregator eventAggregator)
 | 
						|
        {
 | 
						|
            _portUtil = portUtil;
 | 
						|
            _eventAggregator = eventAggregator;
 | 
						|
            _regionManager = regionManager;
 | 
						|
        }
 | 
						|
        public DelegateCommand<string> OpenDrawer
 | 
						|
        {
 | 
						|
            get => new DelegateCommand<string>((DrawerNo) =>
 | 
						|
            {
 | 
						|
                //应急开锁
 | 
						|
                logger.Info($"应急开锁正在打开{DrawerNo}号抽屉");
 | 
						|
                Status = 1;
 | 
						|
                _portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
 | 
						|
                _portUtil.WindowName = "EmergencyWindow";
 | 
						|
                _portUtil.BoardType = 1;
 | 
						|
                _portUtil.ColNos = new int[] { 1 };
 | 
						|
                _portUtil.DrawerNo = Convert.ToInt32(DrawerNo);
 | 
						|
                Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, () => _portUtil.OpenDrawer());
 | 
						|
 | 
						|
            }
 | 
						|
            );
 | 
						|
        }
 | 
						|
        private DelegateCommand? _exitCommand;
 | 
						|
        public DelegateCommand ExitCommand => _exitCommand ??= new DelegateCommand(Exit);
 | 
						|
        void Exit()
 | 
						|
        {
 | 
						|
            Process.GetCurrentProcess().Kill();
 | 
						|
            Environment.Exit(0);
 | 
						|
        }
 | 
						|
        public DelegateCommand ReturnLoginDelegate
 | 
						|
        {
 | 
						|
            get => new DelegateCommand(() =>
 | 
						|
            {
 | 
						|
                _regionManager.RequestNavigate("MainRegion", "LoginWindow");
 | 
						|
            });
 | 
						|
        }
 | 
						|
        //间隔1分钟查询数据库连接状态
 | 
						|
        private void CheckDBConnect()
 | 
						|
        {
 | 
						|
            new PromiseUtil<int>().taskAsyncLoop(60000, 0, async (options, next, stop) =>
 | 
						|
            {
 | 
						|
                try
 | 
						|
                {
 | 
						|
                    var userList = SqlSugarHelper.Db.Queryable<UserList>()
 | 
						|
                                        .First(u => u.UserName == "admin");
 | 
						|
                    App.DbConnectionFail = false;
 | 
						|
                    DBConnectionStatus = true;
 | 
						|
                    stop();
 | 
						|
                }
 | 
						|
                catch (Exception ex)
 | 
						|
                {
 | 
						|
                    next();
 | 
						|
                }
 | 
						|
            });
 | 
						|
        }
 | 
						|
        public bool KeepAlive => false;
 | 
						|
        public void OnNavigatedTo(NavigationContext navigationContext)
 | 
						|
        {
 | 
						|
            DBConnectionStatus = !App.DbConnectionFail;
 | 
						|
            CheckDBConnect();
 | 
						|
        }
 | 
						|
 | 
						|
        public bool IsNavigationTarget(NavigationContext navigationContext)
 | 
						|
        {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        public void OnNavigatedFrom(NavigationContext navigationContext)
 | 
						|
        {
 | 
						|
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |