在 AiVideo 中能看到图像

增加了在线状态同步逻辑
This commit is contained in:
2026-01-09 12:30:36 +08:00
parent 3d47c8f009
commit 3351ae739e
31 changed files with 1090 additions and 477 deletions

View File

@@ -437,4 +437,25 @@ public class CameraManager : IDisposable, IAsyncDisposable
// 复用现有的 GetAllDevices 逻辑
return GetAllDevices();
}
#region --- [] 线 (SDK ) ---
/// <summary>
/// 当设备在线/离线状态发生变更时触发
/// <para>参数1: DeviceId</para>
/// <para>参数2: IsOnline (true=在线, false=离线)</para>
/// <para>参数3: Reason (变更原因)</para>
/// </summary>
public event Action<long, bool, string>? OnDeviceStatusChanged;
/// <summary>
/// [内部方法] 供 Sentinel 调用,触发事件冒泡
/// </summary>
internal void NotifyStatusChange(long deviceId, bool isOnline, string reason)
{
// 仅仅是触发 C# 事件,完全不知道网络发送的存在
OnDeviceStatusChanged?.Invoke(deviceId, isOnline, reason);
}
#endregion
}

View File

@@ -1,4 +1,5 @@
using System.Net.NetworkInformation;
using System.Drawing;
using System.Net.NetworkInformation;
namespace SHH.CameraSdk;
@@ -15,6 +16,11 @@ public class ConnectivitySentinel
private readonly PeriodicTimer _timer;
private readonly CancellationTokenSource _cts = new();
// [关键] 状态缓存:用于“去重”。
// 只有当状态真的从 true 变 false (或反之) 时,才通知 Manager。
// 防止每 3 秒发一次 "在线" 骚扰上层。
private readonly ConcurrentDictionary<long, bool> _lastStates = new();
// [关键配置] 最大并发度
// 建议值CPU 核心数 * 4或者固定 16-32
// 50 个摄像头,设为 16意味着分 4 批完成,总耗时极短
@@ -77,6 +83,21 @@ public class ConnectivitySentinel
// [状态注入]:将探测结果“注入”回设备
device.SetNetworkStatus(isAlive);
// 3. [状态去重与上报]
// 获取上一次的状态,如果没记录过,假设它之前是反状态(强制第一次上报)
bool lastState = _lastStates.TryGetValue(device.Id, out bool val) ? val : !isAlive;
if (lastState != isAlive)
{
// 记录新状态
_lastStates[device.Id] = isAlive;
// ★★★ 核心动作:只通知 Manager不做任何网络操作 ★★★
_manager.NotifyStatusChange(device.Id, isAlive, "网络连通性哨兵检测结论");
// Console.WriteLine($"[Sentinel] 诊断变化: {device.Id} -> {isAlive}");
}
}
// 纯粹的 Ping 逻辑