wenxiangchao
2026-11-02 fe2aa4880d5c9fd88088ad0ed81ebaca05629f5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using log4net;
using Plugin.DeviceDebug.Entity;
using System;
using System.Windows;
using System.Windows.Controls;
 
namespace Plugin.DeviceDebug.Panels
{
    /// <summary>
    /// PowerSupplyDebugPanel.xaml 的交互逻辑
    /// </summary>
    public partial class PowerSupplyDebugPanel : UserControl
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(PowerSupplyDebugPanel));
 
        /// <summary>
        /// 是否为维护模式(控制输入可用性)
        /// </summary>
        public static readonly DependencyProperty IsMaintenanceModeProperty =
            DependencyProperty.Register(nameof(IsMaintenanceMode), typeof(bool), typeof(PowerSupplyDebugPanel),
                new PropertyMetadata(false));
 
        public bool IsMaintenanceMode
        {
            get => (bool)GetValue(IsMaintenanceModeProperty);
            set => SetValue(IsMaintenanceModeProperty, value);
        }
 
        /// <summary>
        /// 命令执行事件
        /// </summary>
        public event EventHandler<DebugCommand> OnCommandExecute;
 
        public PowerSupplyDebugPanel()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// 更新实测值
        /// </summary>
        public void UpdateReadback(string voltage, string current, DateTime time)
        {
            try
            {
                measuredVoltage.Text = $"{voltage} V";
                measuredCurrent.Text = $"{current} A";
                lastUpdateTime.Text = time.ToString("yyyy-MM-dd HH:mm:ss");
            }
            catch (Exception ex)
            {
                log.Error("更新实测值失败", ex);
            }
        }
 
        private void ApplyBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsMaintenanceMode)
                {
                    MessageBox.Show("当前为只读模式,无法执行设置操作", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
 
                // 解析输入值
                if (!double.TryParse(voltageInput.Text, out double voltage))
                {
                    MessageBox.Show("电压值格式不正确", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
 
                if (!double.TryParse(currentInput.Text, out double currentLimit))
                {
                    MessageBox.Show("电流值格式不正确", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
 
                bool output = outputToggle.IsChecked == true;
 
                // 创建命令
                var command = new PowerSetCommand
                {
                    Voltage = voltage,
                    CurrentLimit = currentLimit,
                    Output = output
                };
 
                // 触发命令执行事件
                OnCommandExecute?.Invoke(this, command);
 
                log.Info($"发送电源设置命令: V={voltage}, I={currentLimit}, Output={output}");
            }
            catch (Exception ex)
            {
                log.Error("发送电源设置命令失败", ex);
                MessageBox.Show($"发送命令失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
 
        private void MeasureBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 创建测量命令
                var command = new PowerMeasureCommand();
 
                // 触发命令执行事件
                OnCommandExecute?.Invoke(this, command);
 
                log.Info("发送电源测量命令");
            }
            catch (Exception ex)
            {
                log.Error("发送电源测量命令失败", ex);
                MessageBox.Show($"发送命令失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
 
        private void OutputToggle_Changed(object sender, RoutedEventArgs e)
        {
            // ToggleButton 状态变化时的视觉反馈已在 XAML Style 中处理
        }
    }
}