using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Input.Platform;
using NewLife;
using NewLife.XCoder.Services;
namespace NewLife.XCoder.Tools.Platform;
/// <summary>平台诊断工具:能力声明、设备枚举与网卡配置命令生成</summary>
public partial class PlatformToolView : UserControl
{
private readonly List<ProbeItem> _adapters = [];
private NetConfigPlan? _plan;
/// <summary>实例化</summary>
public PlatformToolView()
{
InitializeComponent();
PlatformText.Text = $"{PlatformCapabilities.PlatformName} · {Environment.OSVersion}";
CapabilityBox.Text = FormatCapabilities();
var serial = SerialPermissionProbe.Check();
SerialText.Text = (serial.CanOpenSerial ? LocalizationService.T("Serial: ✔") : LocalizationService.T("Serial: ✘")) + " " + serial.Reason;
SerialText.Classes.Set("error", !serial.CanOpenSerial);
SerialText.Classes.Set("ok", serial.CanOpenSerial);
UsbButton.Click += (_, _) => _ = ListAsync("USB", DeviceProbeService.ListUsbAsync);
AudioButton.Click += (_, _) => _ = ListAsync(LocalizationService.T("Audio devices"), DeviceProbeService.ListAudioAsync);
AdapterButton.Click += (_, _) => _ = ListAdaptersAsync();
PlanButton.Click += (_, _) => BuildPlan(false);
DhcpButton.Click += (_, _) => BuildPlan(true);
CopyButton.Click += (_, _) => _ = CopyAsync();
ApplyButton.Click += (_, _) => _ = ApplyAsync();
}
private static String FormatCapabilities()
{
var sb = new StringBuilder();
foreach (var item in PlatformCapabilities.Current)
{
var flag = item.Supported ? "✔" : "✘";
sb.AppendLine($"{flag} {item.Key,-16} {item.Title}{(item.Reason.IsNullOrEmpty() ? "" : " — " + item.Reason)}");
}
return sb.ToString().TrimEnd();
}
private async Task ListAsync(String title, Func<Task<IReadOnlyList<ProbeItem>>> action)
{
SetStatus(LocalizationService.Format("Enumerating {0}...", title), false);
var items = await action();
DeviceBox.Text = items.Count == 0
? LocalizationService.Format("No {0} devices found (missing command or no device).", title)
: Format(items);
SetStatus(LocalizationService.Format("{0}: {1} items", title, items.Count), false);
}
private async Task ListAdaptersAsync()
{
SetStatus(LocalizationService.T("Enumerating NICs..."), false);
_adapters.Clear();
_adapters.AddRange(await DeviceProbeService.ListAdaptersAsync());
DeviceBox.Text = _adapters.Count == 0 ? LocalizationService.T("No NICs found.") : Format(_adapters);
AdapterBox.Items.Clear();
foreach (var item in _adapters)
{
AdapterBox.Items.Add(item.Name);
}
if (_adapters.Count > 0) AdapterBox.SelectedIndex = 0;
SetStatus(LocalizationService.Format("NICs: {0} items", _adapters.Count), false);
}
private static String Format(IReadOnlyList<ProbeItem> items)
{
var sb = new StringBuilder();
for (var i = 0; i < items.Count; i++)
{
sb.AppendLine($"{i + 1,3}. {items[i].Name} {items[i].Detail}");
}
return sb.ToString().TrimEnd();
}
private void BuildPlan(Boolean dhcp)
{
var adapter = AdapterBox.SelectedItem as String ?? AdapterBox.Text;
if (adapter.IsNullOrEmpty())
{
SetStatus(LocalizationService.T("Please enumerate NICs and select one first"), true);
return;
}
try
{
_plan = dhcp
? NetworkConfigService.BuildDhcp(adapter!)
: NetworkConfigService.BuildStatic(adapter!, IpBox.Text ?? "", MaskBox.Text ?? "", GatewayBox.Text);
PlanBox.Text = NetworkConfigService.Format(_plan);
SetStatus(LocalizationService.T("Command generated; review then copy or execute"), false);
}
catch (Exception ex)
{
_plan = null;
PlanBox.Text = "";
SetStatus(LocalizationService.T("Generation failed:") + " " + ex.Message, true);
}
}
private async Task CopyAsync()
{
var text = PlanBox.Text;
if (text.IsNullOrEmpty())
{
SetStatus(LocalizationService.T("No command to copy"), true);
return;
}
var clipboard = TopLevel.GetTopLevel(this)?.Clipboard;
if (clipboard == null)
{
SetStatus(LocalizationService.T("Clipboard is unavailable in the current environment"), true);
return;
}
await clipboard.SetTextAsync(text);
SetStatus(LocalizationService.T("Copied to clipboard"), false);
}
private async Task ApplyAsync()
{
var plan = _plan;
if (plan == null)
{
SetStatus(LocalizationService.T("Generate a command first"), true);
return;
}
ApplyButton.IsEnabled = false;
try
{
var text = await NetworkConfigService.ApplyAsync(plan);
PlanBox.Text = PlanBox.Text + Environment.NewLine + Environment.NewLine + text;
SetStatus(LocalizationService.T("Execution completed (if permission is denied, copy the command to an administrator terminal)"), false);
}
catch (Exception ex)
{
SetStatus(LocalizationService.T("Execution failed:") + " " + ex.Message, true);
}
finally
{
ApplyButton.IsEnabled = true;
}
}
private void SetStatus(String text, Boolean error)
{
StatusText.Text = NewLife.XCoder.Services.LocalizationService.Translate(text);
StatusText.Classes.Set("error", error);
StatusText.Classes.Set("ok", !error);
}
}
|