diff --git a/App.xaml b/App.xaml index c50692e..916afff 100644 --- a/App.xaml +++ b/App.xaml @@ -1,7 +1,6 @@  + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> diff --git a/App.xaml.cs b/App.xaml.cs index bfb81d5..dae7d19 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,4 +1,6 @@ using MembranePoreTester.Communication; +using MembranePoreTester.Models; +using MembranePoreTester.Views; using Microsoft.Extensions.Configuration; using OfficeOpenXml; using System; @@ -9,18 +11,31 @@ namespace MembranePoreTester { public partial class App : Application { + public static UserRole CurrentUserRole { get; set; } = UserRole.Operator; + public static IPlcService PlcService { get; private set; } public static PlcConfiguration PlcConfig { get; private set; } - protected async override void OnStartup(StartupEventArgs e) + protected override async void OnStartup(StartupEventArgs e) { - ExcelPackage.LicenseContext = LicenseContext.NonCommercial; base.OnStartup(e); + // 防止在登录窗口关闭时应用程序因没有窗口而自动退出 + ShutdownMode = ShutdownMode.OnExplicitShutdown; + + // 显示登录窗口,验证用户身份 + var loginWindow = new LoginWindow(); + if (loginWindow.ShowDialog() != true) + { + Shutdown(); + return; + } + + // 登录成功后初始化数据库和PLC连接 using var db = new AppDbContext(); - db.Database.EnsureCreated(); // 自动建表 + db.Database.EnsureCreated(); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) @@ -43,6 +58,12 @@ namespace MembranePoreTester { MessageBox.Show($"PLC 连接失败:{ex.Message}"); } + + // 启动主窗口,设置为应用程序的主窗口并恢复默认的退出模式 + var mainWindow = new MainWindow(); + MainWindow = mainWindow; + ShutdownMode = ShutdownMode.OnMainWindowClose; + mainWindow.Show(); } protected override void OnExit(ExitEventArgs e) diff --git a/Models/UserRole.cs b/Models/UserRole.cs new file mode 100644 index 0000000..e4d04ac --- /dev/null +++ b/Models/UserRole.cs @@ -0,0 +1,8 @@ +namespace MembranePoreTester.Models +{ + public enum UserRole + { + Operator, // 操作员 + Admin // 管理员 + } +} \ No newline at end of file diff --git a/ViewModels/PoreDistributionViewModel.cs b/ViewModels/PoreDistributionViewModel.cs index c0cbb28..1dc59f2 100644 --- a/ViewModels/PoreDistributionViewModel.cs +++ b/ViewModels/PoreDistributionViewModel.cs @@ -114,6 +114,8 @@ namespace MembranePoreTester.ViewModels + + // 2. 读取当前工位的加压上限(实时) ushort upperLimitAddress = StationId == 1 ? _plcConfig.PressureUpperLimit : StationId == 2 ? _plcConfig.PressureUpperLimit2 @@ -121,12 +123,16 @@ namespace MembranePoreTester.ViewModels double pressureUpperLimit = await _plcService.ReadFloatAsync(upperLimitAddress); // 3. 如果压力已达到或超过上限,停止采集 - if (pressure >= pressureUpperLimit - 3) + if (pressure >= pressureUpperLimit * 1000 - 10 * 1000 && pressure > 0) { StopCollecting(); return; // 不再添加当前数据点 } + + + + // 2. 读取当前模式对应的流量 double flow = 0; if (TestMode.Contains("湿膜")) @@ -578,19 +584,19 @@ namespace MembranePoreTester.ViewModels System.Diagnostics.Debug.WriteLine($"P={p.Pressure}, Wet={p.WetFlow}, Dry={p.DryFlow}"); } - var cleanedPoints = CleanDataPoints(originalPoints); + //var cleanedPoints = CleanDataPoints(originalPoints); - if (cleanedPoints.Count < 2) - { - MessageBox.Show("有效数据点不足,至少需要 2 个数据点进行计算。"); - return; - } + //if (cleanedPoints.Count < 2) + //{ + // MessageBox.Show("有效数据点不足,至少需要 2 个数据点进行计算。"); + // return; + //} - int invalidCount = originalPoints.Count - cleanedPoints.Count; + //int invalidCount = originalPoints.Count - cleanedPoints.Count; // 用清洗后的点替换 ObservableCollection 的内容(触发UI更新) Record.DataPoints.Clear(); - foreach (var p in cleanedPoints) + foreach (var p in originalPoints) Record.DataPoints.Add(p); // 刷新曲线以显示清洗后的数据 @@ -603,11 +609,11 @@ namespace MembranePoreTester.ViewModels RangePercentage = PoreDistributionAnalysis.CalculatePoreRangePercentage( Record.DataPoints, Record.PressureUnit, Record.Liquid, LowerPore, UpperPore); - if (invalidCount > 0) - { - MessageBox.Show($"已自动过滤 {invalidCount} 个无效数据点"); - System.Diagnostics.Debug.WriteLine($"已自动过滤 {invalidCount} 个无效数据点"); - } + //if (invalidCount > 0) + //{ + // MessageBox.Show($"已自动过滤 {invalidCount} 个无效数据点"); + // System.Diagnostics.Debug.WriteLine($"已自动过滤 {invalidCount} 个无效数据点"); + //} } //private void Calculate() @@ -1146,6 +1152,11 @@ namespace MembranePoreTester.ViewModels //} + public void RefreshPlot() + { + UpdatePlot(); + } + private void UpdatePlot() { // 确保在 UI 线程执行 @@ -1156,11 +1167,11 @@ namespace MembranePoreTester.ViewModels } var sorted = Record.DataPoints.OrderBy(p => p.Pressure).ToList(); - if (sorted.Count == 0) - { - PlotModel = null; - return; - } + //if (sorted.Count == 0) + //{ + // PlotModel = null; + // return; + //} var model = new PlotModel { diff --git a/Views/LoginWindow.xaml b/Views/LoginWindow.xaml new file mode 100644 index 0000000..fd4fc08 --- /dev/null +++ b/Views/LoginWindow.xaml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + +