Files
Ayay/SHH.CameraDashboard/App/WizardControl.xaml.cs

94 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using SHH.CameraDashboard.Services;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
namespace SHH.CameraDashboard
{
public partial class WizardControl : UserControl
{
private const string ServerConfigFile = "servers.config.json";
// 绑定源
public ObservableCollection<ServerNode> Nodes { get; set; } = new ObservableCollection<ServerNode>();
// 定义关闭事件,通知主窗体关闭模态框
public event EventHandler RequestClose;
public WizardControl()
{
InitializeComponent();
// 使用泛型加载,指定返回类型为 ObservableCollection<ServerNode>
// 这里的 LoadServers 逻辑变为了通用的 Load<T>
Nodes = StorageService.Load<ObservableCollection<ServerNode>>(ServerConfigFile);
NodeList.ItemsSource = Nodes;
}
private void AddNode_Click(object sender, RoutedEventArgs e)
{
Nodes.Add(new ServerNode());
}
private void DeleteNode_Click(object sender, RoutedEventArgs e)
{
if (sender is Button btn && btn.DataContext is ServerNode node)
{
Nodes.Remove(node);
}
}
private async void Check_Click(object sender, RoutedEventArgs e)
{
// 禁用按钮防止重复点击
var btn = sender as Button;
if (btn != null) btn.IsEnabled = false;
foreach (var node in Nodes)
{
node.SetResult(false, "⏳ 检测中...");
// 构造 URL
string url = $"http://{node.Ip}:{node.Port}/api/Cameras";
// 或者是 /api/health看你的后端提供什么接口
try
{
// 【修改】这里调用封装好的 HttpService
// 我们使用 TestConnectionAsync它内部会触发 OnApiLog 事件记录日志
bool isConnected = await HttpService.TestConnectionAsync(url);
if (isConnected)
node.SetResult(true, "✅ 连接成功");
else
node.SetResult(false, "❌ 状态码异常");
}
catch (Exception ex)
{
// 异常也被 HttpService 记录了,这里只负责更新 UI 状态
node.SetResult(false, "❌ 无法连接");
}
}
if (btn != null) btn.IsEnabled = true;
}
private void Apply_Click(object sender, RoutedEventArgs e)
{
// 将当前的 Nodes 集合保存到指定文件
StorageService.Save(Nodes, ServerConfigFile);
// 同步到全局单例内存中
AppGlobalData.SaveConfig(Nodes);
RequestClose?.Invoke(this, EventArgs.Empty);
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
RequestClose?.Invoke(this, EventArgs.Empty);
}
}
}