using log4net;
using Plugin.DeviceDebug.Entity;
using System;
using System.Windows;
using System.Windows.Controls;
namespace Plugin.DeviceDebug.Panels
{
///
/// PowerSupplyDebugPanel.xaml 的交互逻辑
///
public partial class PowerSupplyDebugPanel : UserControl
{
private static readonly ILog log = LogManager.GetLogger(typeof(PowerSupplyDebugPanel));
///
/// 是否为维护模式(控制输入可用性)
///
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);
}
///
/// 命令执行事件
///
public event EventHandler OnCommandExecute;
public PowerSupplyDebugPanel()
{
InitializeComponent();
}
///
/// 更新实测值
///
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 中处理
}
}
}