using System; using System.Windows; using System.Windows.Controls; using Newtonsoft.Json.Linq; namespace YKC { public partial class ChargerPage : Page { public ChargerPage() { InitializeComponent(); } private async void SetPileId_Click(object sender, RoutedEventArgs e) { string idx = txtSetPileIdx.Text.Trim(); string id = txtSetPileId.Text.Trim(); if (string.IsNullOrEmpty(id) || id.Length != 14) { ShowResult(txtSetResult, "序列号需14位HEX", false); return; } ShowResult(txtSetResult, "发送中...", true); await System.Threading.Tasks.Task.Run(() => { var result = UdpClientHolder.Instance.SendSync(new JObject { ["cmd"] = Config.Cmd["SET_PILE_ID"], ["pile_index"] = int.Parse(idx), ["pile_id"] = id }); Dispatcher.BeginInvoke(new Action(() => { if (result.Value("success")) ShowResult(txtSetResult, "写入成功", true); else ShowResult(txtSetResult, result.Value("error") ?? "失败", false); })); }); } private async void GetPileInfo_Click(object sender, RoutedEventArgs e) { string idx = txtInfoPileIdx.Text.Trim(); panelInfo.Visibility = Visibility.Visible; txtInfoResult.Text = "查询中..."; txtInfoResult.Foreground = new System.Windows.Media.SolidColorBrush( (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#64748B")); await System.Threading.Tasks.Task.Run(() => { var result = UdpClientHolder.Instance.SendSync(new JObject { ["cmd"] = Config.Cmd["GET_PILE_INFO"], ["pile_index"] = int.Parse(idx) }); Dispatcher.BeginInvoke(new Action(() => { if (result.Value("success") && result["serial"] != null) { txtInfoSerial.Text = result.Value("serial") ?? "--"; txtInfoType.Text = result.Value("type") ?? "--"; txtInfoGuns.Text = result.Value("gun_num") ?? "--"; txtInfoProto.Text = result.Value("protocol_ver") ?? "--"; txtInfoSw.Text = result.Value("software_ver") ?? "--"; txtInfoSim.Text = result.Value("sim") ?? "--"; txtInfoResult.Text = ""; } else { txtInfoSerial.Text = "--"; txtInfoType.Text = "--"; txtInfoGuns.Text = "--"; txtInfoProto.Text = "--"; txtInfoSw.Text = "--"; txtInfoSim.Text = "--"; ShowResult(txtInfoResult, result.Value("error") ?? "查询失败", false); } })); }); } private async void Reboot_Click(object sender, RoutedEventArgs e) { var dialogResult = MessageBox.Show("确认重启设备?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (dialogResult != MessageBoxResult.Yes) return; ShowResult(txtRebootResult, "发送中...", true); await System.Threading.Tasks.Task.Run(() => { var result = UdpClientHolder.Instance.SendSync(new JObject { ["cmd"] = Config.Cmd["REBOOT"] }); Dispatcher.BeginInvoke(new Action(() => { if (result.Value("success")) ShowResult(txtRebootResult, "重启指令已发送", true); else ShowResult(txtRebootResult, result.Value("error") ?? "失败", false); })); }); } private void ShowResult(TextBlock el, string msg, bool ok) { el.Text = msg; el.Foreground = new System.Windows.Media.SolidColorBrush( (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString( ok ? "#10B981" : "#EF4444")); var timer = new System.Windows.Threading.DispatcherTimer { Interval = TimeSpan.FromSeconds(4) }; timer.Tick += (s, _) => { el.Text = ""; timer.Stop(); }; timer.Start(); } } }