From fb168eda343a82e2160e9e7f9d2cb20c7f7d86bf Mon Sep 17 00:00:00 2001 From: maqiao <625215135@qq.com> Date: Mon, 3 Mar 2025 14:29:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=B7=BB=E5=8A=A0=E7=B4=A7=E6=80=A5=E5=BC=80?= =?UTF-8?q?=E9=94=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DM_Weight/App.config | 2 +- DM_Weight/App.xaml.cs | 9 +- .../ViewModels/EmergencyWindowViewModel.cs | 89 ++++++++++++++++++ DM_Weight/ViewModels/MainWindowViewModel.cs | 13 ++- DM_Weight/Views/EmergencyWindow.xaml | 90 +++++++++++++++++++ DM_Weight/Views/EmergencyWindow.xaml.cs | 28 ++++++ 6 files changed, 226 insertions(+), 5 deletions(-) create mode 100644 DM_Weight/ViewModels/EmergencyWindowViewModel.cs create mode 100644 DM_Weight/Views/EmergencyWindow.xaml create mode 100644 DM_Weight/Views/EmergencyWindow.xaml.cs diff --git a/DM_Weight/App.config b/DM_Weight/App.config index 2a95052..7388f7c 100644 --- a/DM_Weight/App.config +++ b/DM_Weight/App.config @@ -37,7 +37,7 @@ - + diff --git a/DM_Weight/App.xaml.cs b/DM_Weight/App.xaml.cs index 7f5d5ea..958b6b6 100644 --- a/DM_Weight/App.xaml.cs +++ b/DM_Weight/App.xaml.cs @@ -34,6 +34,7 @@ namespace DM_Weight /// public partial class App : PrismApplication { + public static bool DbConnectionFail { get; set; } = false; private readonly ILog logger = LogManager.GetLogger(typeof(App)); public App() { @@ -62,8 +63,12 @@ namespace DM_Weight { logger.Error($"发生错误:{e.Exception.Message}"); e.Handled = true; + if (e.Exception.Message.Contains("连接数据库过程中发生错误")) + { + DbConnectionFail = true; + Container.Resolve(); + } } - void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { @@ -294,6 +299,8 @@ namespace DM_Weight //containerRegistry.RegisterForNavigation(); containerRegistry.RegisterForNavigation(); + //紧急开锁页面 + containerRegistry.RegisterForNavigation(); logger.Info("结束APP-RegisterTypes"); diff --git a/DM_Weight/ViewModels/EmergencyWindowViewModel.cs b/DM_Weight/ViewModels/EmergencyWindowViewModel.cs new file mode 100644 index 0000000..fda0380 --- /dev/null +++ b/DM_Weight/ViewModels/EmergencyWindowViewModel.cs @@ -0,0 +1,89 @@ +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.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"); + }); + } + //间隔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(); + } + }); + } + } +} diff --git a/DM_Weight/ViewModels/MainWindowViewModel.cs b/DM_Weight/ViewModels/MainWindowViewModel.cs index fa40760..a7733a4 100644 --- a/DM_Weight/ViewModels/MainWindowViewModel.cs +++ b/DM_Weight/ViewModels/MainWindowViewModel.cs @@ -83,9 +83,16 @@ namespace DM_Weight.ViewModels //System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => //{ - - _container.RegisterType("LoginWindow"); - _regionManager.RegisterViewWithRegion("MainRegion", "LoginWindow"); + if (App.DbConnectionFail) + { + //_container.RegisterType("EmergencyWindow"); + _regionManager.RequestNavigate("MainRegion", "EmergencyWindow"); + } + else + { + _container.RegisterType("LoginWindow"); + _regionManager.RegisterViewWithRegion("MainRegion", "LoginWindow"); + } _socketHelper = socketHelper; new PromiseUtil().taskAsyncLoop(1200000, 0, async (options, next, stop) => { diff --git a/DM_Weight/Views/EmergencyWindow.xaml b/DM_Weight/Views/EmergencyWindow.xaml new file mode 100644 index 0000000..861d384 --- /dev/null +++ b/DM_Weight/Views/EmergencyWindow.xaml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +