diff --git a/全自动水压检测仪/NormalTemperatureMode.cs b/全自动水压检测仪/NormalTemperatureMode.cs index 607db6c..0066190 100644 --- a/全自动水压检测仪/NormalTemperatureMode.cs +++ b/全自动水压检测仪/NormalTemperatureMode.cs @@ -54,9 +54,16 @@ namespace 全自动水压检测仪 // 温度模式状态跟踪(用于防抖优化) private bool _lastHighTempMode = false; private bool _lastLowTempMode = false; + private Panel[] _normalTempLevelBarSegments = Array.Empty(); + private Panel[] _highTempLevelBarSegments = Array.Empty(); private ConductivityRepository _repository; + private static readonly Color LevelBarOffColor = Color.FromArgb(224, 228, 232); + private static readonly Color LevelBarNormalColor = Color.FromArgb(82, 196, 26); + private static readonly Color LevelBarAlertColor = Color.FromArgb(255, 77, 79); + private static readonly Color LevelBarFaultColor = Color.FromArgb(250, 173, 20); + #region 报警监控相关字段和属性 // 报警寄存器地址定义(根据图片内容) private const ushort PRESSURE_PROTECTION_ADDR = 10200; // M200 压力保护大于 @@ -97,6 +104,7 @@ namespace 全自动水压检测仪 public NormalTemperatureMode() { InitializeComponent(); + InitializeLiquidLevelStatusBars(); pressStopwatch = new Stopwatch(); // 只创建定时器,不在构造器中启动,避免在 Load 前访问未初始化的资源 _readTimer = InitTimer(); // 保存引用 @@ -112,6 +120,150 @@ namespace 全自动水压检测仪 _chartManager = new ChartManager(); } + private void InitializeLiquidLevelStatusBars() + { + HideLegacyLiquidLevelControls(); + + _normalTempLevelBarSegments = CreateLiquidLevelStatusBar(uiPanel23, "normalTempLevelBar"); + _highTempLevelBarSegments = CreateLiquidLevelStatusBar(uiPanel31, "highTempLevelBar"); + + UpdateLiquidLevelStatusBar(_normalTempLevelBarSegments, false, false, "常温液位"); + UpdateLiquidLevelStatusBar(_highTempLevelBarSegments, false, false, "高温液位"); + } + + private void HideLegacyLiquidLevelControls() + { + Control[] legacyControls = + { + uiLight13, uiLight14, uiLight15, uiLight16, + uiLabel8, uiLabel12, uiLabel16, uiLabel27, + uiLabel17, uiLabel34 + }; + + foreach (var control in legacyControls) + { + if (control == null || control.IsDisposed) + { + continue; + } + + control.Visible = false; + } + } + + private Panel[] CreateLiquidLevelStatusBar(Control hostPanel, string barName) + { + if (hostPanel == null || hostPanel.IsDisposed) + { + return Array.Empty(); + } + + var framePanel = new Panel + { + Name = $"{barName}Frame", + Size = new Size(32, 34), + Anchor = AnchorStyles.Top | AnchorStyles.Right, + BackColor = Color.FromArgb(208, 213, 219), + Padding = new Padding(2) + }; + + var segmentLayout = new TableLayoutPanel + { + Name = $"{barName}Layout", + Dock = DockStyle.Fill, + Margin = Padding.Empty, + Padding = Padding.Empty, + ColumnCount = 1, + RowCount = 3, + BackColor = Color.White + }; + + segmentLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); + segmentLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 33.3333F)); + segmentLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 33.3333F)); + segmentLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 33.3334F)); + + var segments = new Panel[3]; + for (int index = 0; index < segments.Length; index++) + { + var segment = new Panel + { + Name = $"{barName}Segment{index}", + Dock = DockStyle.Fill, + Margin = new Padding(1), + BackColor = LevelBarOffColor + }; + + segments[index] = segment; + segmentLayout.Controls.Add(segment, 0, 2 - index); + } + + framePanel.Controls.Add(segmentLayout); + hostPanel.Controls.Add(framePanel); + framePanel.BringToFront(); + + void RepositionStatusBar(object sender, EventArgs e) + { + framePanel.Location = new Point( + Math.Max(110, hostPanel.ClientSize.Width - framePanel.Width - 16), + Math.Max(3, (hostPanel.ClientSize.Height - framePanel.Height) / 2)); + } + + hostPanel.SizeChanged += RepositionStatusBar; + RepositionStatusBar(hostPanel, EventArgs.Empty); + + return segments; + } + + private static bool TryGetCoilValue(bool[] coilData, out bool value) + { + if (coilData != null && coilData.Length > 0) + { + value = coilData[0]; + return true; + } + + value = false; + return false; + } + + private void UpdateLiquidLevelStatusBar(Panel[] segments, bool sensorA, bool sensorB, string barName) + { + if (segments == null || segments.Length != 3) + { + return; + } + + Color[] colors; + if (!sensorA && !sensorB) + { + colors = new[] { LevelBarAlertColor, LevelBarAlertColor, LevelBarAlertColor }; + } + else if (sensorA && !sensorB) + { + colors = new[] { LevelBarNormalColor, LevelBarNormalColor, LevelBarOffColor }; + } + else if (sensorA && sensorB) + { + colors = new[] { LevelBarNormalColor, LevelBarNormalColor, LevelBarAlertColor }; + } + else + { + colors = new[] { LevelBarFaultColor, LevelBarFaultColor, LevelBarFaultColor }; + Debug.WriteLine($"[LiquidLevelStatusBar] 检测到异常液位组合: {barName}, A={sensorA}, B={sensorB}"); + } + + for (int index = 0; index < segments.Length; index++) + { + if (segments[index] == null || segments[index].IsDisposed) + { + continue; + } + + segments[index].BackColor = colors[index]; + } + } + private System.Windows.Forms.Timer InitAlarmMonitorTimer() { var timer = new System.Windows.Forms.Timer() @@ -501,59 +653,21 @@ namespace 全自动水压检测仪 // if (percentage > 100) percentage = 100; - bool[] a = _modbusMaster?.ReadCoils(1, 10042, 1); - bool[] b = _modbusMaster?.ReadCoils(1, 10043, 1); - bool[] cc = _modbusMaster?.ReadCoils(1, 10040, 1); - bool[] d = _modbusMaster?.ReadCoils(1, 10041, 1); + bool[] a = _modbusMaster?.ReadCoils(1, 10042, 1); // 常温低液位 0无水/1有水 + bool[] b = _modbusMaster?.ReadCoils(1, 10043, 1); // 常温高液位 0无水/1有水 + bool[] cc = _modbusMaster?.ReadCoils(1, 10040, 1); // 高温低液位 0无水/1有水 + bool[] d = _modbusMaster?.ReadCoils(1, 10041, 1); // 高温高液位 0无水/1有水 - if (a != null && a.Length > 0) + if (TryGetCoilValue(a, out bool normalSensorA) && + TryGetCoilValue(b, out bool normalSensorB)) { - if (a[0]) - { - uiLight13.OnColor = System.Drawing.Color.Green;//有水 - } - - else - { - uiLight13.OnColor = System.Drawing.Color.Red; - } + UpdateLiquidLevelStatusBar(_normalTempLevelBarSegments, normalSensorA, normalSensorB, "常温液位"); } - if (b != null && b.Length > 0) + if (TryGetCoilValue(cc, out bool highSensorA) && + TryGetCoilValue(d, out bool highSensorB)) { - if (!b[0]) - { - uiLight14.OnColor = System.Drawing.Color.Green;//有水 - } - - else - { - uiLight14.OnColor = System.Drawing.Color.Red; - } - } - if (cc != null && cc.Length > 0) - { - if (cc[0]) - { - uiLight16.OnColor = System.Drawing.Color.Green;//有水 - } - - else - { - uiLight16.OnColor = System.Drawing.Color.Red; - } - } - if (d != null && d.Length > 0) - { - if (!d[0]) - { - uiLight15.OnColor = System.Drawing.Color.Green;//有水 - } - - else - { - uiLight15.OnColor = System.Drawing.Color.Red; - } + UpdateLiquidLevelStatusBar(_highTempLevelBarSegments, highSensorA, highSensorB, "高温液位"); }