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; namespace DM_Weight.ViewModels { public class EmergencyWindowViewModel : BindableBase { private readonly ILog logger = LogManager.GetLogger(typeof(HomeWindowViewModel)); IEventAggregator _eventAggregator; private PortUtil _portUtil; IRegionManager _regionManager; SocketHelper _socketHelper; private bool _dbConnectionFail = !App.DbConnectionFail; public bool DBConnectionStatus { get=>_dbConnectionFail; set => SetProperty(ref _dbConnectionFail, value); } public EmergencyWindowViewModel(IRegionManager regionManager, PortUtil portUtil, IEventAggregator eventAggregator, SocketHelper socketHelper) { _portUtil = portUtil; _eventAggregator = eventAggregator; _regionManager = regionManager; _socketHelper = socketHelper; CheckDBConnect(); } public DelegateCommand UpdateDrawerNo { get => new DelegateCommand((DrawerNo) => { //应急开锁 logger.Info($"应急开锁正在打开{DrawerNo}号药箱"); try { logger.Info($"发送开{DrawerNo}号药箱指令"); _socketHelper.SendMessage(new MyBaseMessage() { lockNo = (short)(Convert.ToInt32(DrawerNo) - 1) }); _socketHelper.dateTime = DateTime.Now; logger.Info($"发开{DrawerNo}号药箱指令完成"); } catch (Exception ex) { AlertMsg alertMsg = new AlertMsg { Message = $"网口连接异常,开药箱正在重试{ex.Message}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); return; } } ); } public DelegateCommand ReturnLoginDelegate { get => new DelegateCommand(() => { _regionManager.RequestNavigate("MainRegion", "LoginWindow"); }); } private DelegateCommand? _exitCommand; public DelegateCommand ExitCommand => _exitCommand ??= new DelegateCommand(Exit); void Exit() { //_chkFunction.HIKLoginOut(); _socketHelper.SocketDisConnect(); Process.GetCurrentProcess().Kill(); Environment.Exit(0); } //间隔1分钟查询数据库连接状态 private void CheckDBConnect() { new PromiseUtil().taskAsyncLoop(60000, 0, async (options, next, stop) => { try { var userList = SqlSugarHelper.Db.Queryable() .First(u => u.UserName == "admin"); App.DbConnectionFail = false; DBConnectionStatus = true; stop(); } catch (Exception ex) { next(); } }); } } }