68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Windows; // 如果是 WPF
|
|
using SHH.Contracts;
|
|
|
|
namespace SHH.CameraDashboard
|
|
{
|
|
/// <summary>
|
|
/// 服务端状态管理器
|
|
/// <para>职责:维护在线设备列表,供 UI 绑定</para>
|
|
/// </summary>
|
|
public class ServerStateManager
|
|
{
|
|
// UI 绑定的集合
|
|
public ObservableCollection<ServerNodeInfo> OnlineServers { get; }
|
|
= new ObservableCollection<ServerNodeInfo>();
|
|
|
|
/// <summary>
|
|
/// 处理注册/心跳包,更新列表
|
|
/// </summary>
|
|
public void RegisterOrUpdate(RegisterPayload info)
|
|
{
|
|
// 确保在 UI 线程执行 (WPF 必须)
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
var existing = OnlineServers.FirstOrDefault(x => x.InstanceId == info.InstanceId);
|
|
|
|
if (existing != null)
|
|
{
|
|
// 更新已有节点状态
|
|
existing.IpAddress = info.ServerIp;
|
|
existing.WebApiPort = info.WebApiPort;
|
|
existing.LastHeartbeat = DateTime.Now;
|
|
existing.Status = "在线";
|
|
// 触发属性变更通知...
|
|
}
|
|
else
|
|
{
|
|
// 新增节点
|
|
OnlineServers.Add(new ServerNodeInfo
|
|
{
|
|
InstanceId = info.InstanceId,
|
|
IpAddress = info.ServerIp,
|
|
WebApiPort = info.WebApiPort,
|
|
ProcessId = info.ProcessId,
|
|
LastHeartbeat = DateTime.Now,
|
|
Status = "在线"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// UI 显示用的模型
|
|
/// </summary>
|
|
public class ServerNodeInfo
|
|
{
|
|
public string InstanceId { get; set; }
|
|
public string IpAddress { get; set; }
|
|
public int WebApiPort { get; set; }
|
|
public int ProcessId { get; set; }
|
|
public string Status { get; set; }
|
|
public DateTime LastHeartbeat { get; set; }
|
|
|
|
// 方便 UI 显示的字符串
|
|
public string DisplayName => $"{InstanceId} ({IpAddress}:{WebApiPort})";
|
|
}
|
|
} |