From e57938fbda9bff702004038f76d009422968bff5 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 06 十一月 2026 18:05:07 +0800
Subject: [PATCH] 提交代码

---
 DeviceService/DebugWindow.xaml.cs |  156 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 156 insertions(+), 0 deletions(-)

diff --git a/DeviceService/DebugWindow.xaml.cs b/DeviceService/DebugWindow.xaml.cs
new file mode 100644
index 0000000..f268ad2
--- /dev/null
+++ b/DeviceService/DebugWindow.xaml.cs
@@ -0,0 +1,156 @@
+using DeviceService.Service;
+using DeviceService.Service.NamePipe;
+using System.Collections.ObjectModel;
+using System.IO.Pipes;
+using System.Text;
+using System.Windows;
+using System.Windows.Input;
+
+namespace DeviceService;
+
+/// <summary>
+/// Interaction logic for DebugWindow.xaml
+/// </summary>
+public partial class DebugWindow : Window
+{
+    private const string PipeName = "DeviceService";
+
+    private class ClientItem
+    {
+        public ClientItem(IClient client, string name)
+        {
+            Client = client;
+            Name = name;
+        }
+
+        public IClient Client { get; }
+
+        public string Name { get; }
+
+        public override string ToString() => Name;
+    }
+
+    private NamePipeService? _pipeService;
+    private readonly ObservableCollection<ClientItem> _clients = new ObservableCollection<ClientItem>();
+
+    public DebugWindow()
+    {
+        InitializeComponent();
+        ClientList.ItemsSource = _clients;
+        Loaded += DebugWindow_Loaded;
+        Closed += DebugWindow_Closed;
+    }
+
+    private void DebugWindow_Loaded(object sender, RoutedEventArgs e)
+    {
+    }
+
+    
+
+    private string FindClientName(IClient client)
+    {
+        ClientItem? item = _clients.FirstOrDefault(c => ReferenceEquals(c.Client, client));
+        return item?.Name ?? "鏈煡瀹㈡埛绔�";
+    }
+
+    private async void SendTestButton_Click(object sender, RoutedEventArgs e)
+    {
+        string text = SendBox.Text;
+        if (string.IsNullOrEmpty(text))
+        {
+            return;
+        }
+
+        SendTestButton.IsEnabled = false;
+        try
+        {
+            string error = await Task.Run(() => SendViaPipe(text + "\r\n"));
+            AppendLine(string.IsNullOrEmpty(error)
+                ? $"[娴嬭瘯] 宸插彂閫侊細{text}"
+                : $"[娴嬭瘯] 鍙戦�佸け璐ワ細{error}");
+        }
+        finally
+        {
+            SendTestButton.IsEnabled = true;
+        }
+    }
+
+    private async void SendToClientButton_Click(object sender, RoutedEventArgs e)
+    {
+        if (ClientList.SelectedItem is not ClientItem item)
+        {
+            AppendLine("[鍙戦�乚 璇峰厛鍦ㄥ乏渚у垪琛ㄤ腑閫夋嫨涓�涓鎴风銆�");
+            return;
+        }
+
+        string text = SendBox.Text;
+        if (string.IsNullOrEmpty(text))
+        {
+            return;
+        }
+
+        string name = item.Name;
+        IClient client = item.Client;
+
+        SendToClientButton.IsEnabled = false;
+        try
+        {
+            string error = await Task.Run(() => client.Send(text, out string err) ? string.Empty : err);
+            AppendLine(string.IsNullOrEmpty(error)
+                ? $"[鍙戦�佲啋{name}] {text}"
+                : $"[鍙戦�佸け璐モ啋{name}] {error}");
+        }
+        finally
+        {
+            SendToClientButton.IsEnabled = true;
+        }
+    }
+
+    private void SendBox_KeyDown(object sender, KeyEventArgs e)
+    {
+        if (e.Key == Key.Enter)
+        {
+            // 宸查�変腑瀹㈡埛绔垯瀹氬悜鍙戦�侊紝鍚﹀垯璧版祴璇曢�氶亾
+            if (ClientList.SelectedItem is ClientItem)
+            {
+                SendToClientButton_Click(sender, e);
+            }
+            else
+            {
+                SendTestButton_Click(sender, e);
+            }
+        }
+    }
+
+    /// <summary>
+    /// 浠ュ鎴风韬唤杩炴帴鏈繘绋嬬殑绠¢亾锛屾寜琛屽崗璁啓鍏ヤ竴鏉� UTF8 娑堟伅锛堜互 \n 缁撳熬锛夈��
+    /// 杩斿洖绌哄瓧绗︿覆琛ㄧず鎴愬姛锛屽惁鍒欎负閿欒淇℃伅銆�
+    /// </summary>
+    private static string SendViaPipe(string text)
+    {
+        try
+        {
+            using var pipe = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut);
+            pipe.Connect(2000);
+
+            byte[] payload = Encoding.UTF8.GetBytes(text + "\n");
+            pipe.Write(payload, 0, payload.Length);
+            pipe.Flush();
+            return string.Empty;
+        }
+        catch (Exception ex)
+        {
+            return ex.Message;
+        }
+    }
+
+    private void AppendLine(string text)
+    {
+        ReceiveBox.AppendText(text + Environment.NewLine);
+        ReceiveBox.ScrollToEnd();
+    }
+
+    private void DebugWindow_Closed(object? sender, EventArgs e)
+    {
+    }
+}

--
Gitblit v1.9.1