Files
Ayay/SHH.Contracts.Grpc/Images/VideoPayload.cs

92 lines
2.7 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 Newtonsoft.Json;
// 注意:如果不想依赖 Newtonsoft也可以用 System.Text.Json但 Newtonsoft 在 Std 2.0 中兼容性更好
namespace SHH.Contracts
{
/// <summary>
/// 视频数据传输契约(纯净版 POCO
/// </summary>
public class VideoPayload
{
/// <summary>
/// 构造函数
/// </summary>
public VideoPayload()
{
SubscriberIds = new List<string>(16);
Diagnostics = new Dictionary<string, object>(4);
}
#region --- 1. (Metadata) ---
public string CameraId { get; set; } = string.Empty;
/// <summary>采集时间戳 (Unix 毫秒)</summary>
public long CaptureTimestamp { get; set; }
/// <summary>分发时间戳 (Unix 毫秒)</summary>
public long DispatchTimestamp { get; set; }
/// <summary>原始图像宽度</summary>
public int OriginalWidth { get; set; }
/// <summary>原始图像高度</summary>
public int OriginalHeight { get; set; }
/// <summary>目标图像宽度</summary>
public int TargetWidth { get; set; }
/// <summary>目标图像高度</summary>
public int TargetHeight { get; set; }
/// <summary>订阅Ids</summary>
public List<string> SubscriberIds { get; set; }
/// <summary>诊断信息</summary>
public Dictionary<string, object> Diagnostics { get; set; }
/// <summary>
/// 指示标志:是否存在原始图
/// </summary>
public bool HasOriginalImage { get; set; }
/// <summary>
/// 指示标志:是否存在处理图
/// </summary>
public bool HasTargetImage { get; set; }
#endregion
#region --- 2. (Binary) ---
// 标记 JsonIgnore防止被错误序列化
[JsonIgnore]
public byte[]? OriginalImageBytes { get; set; }
[JsonIgnore]
public byte[]? TargetImageBytes { get; set; }
#endregion
#region --- 3. ( JSON ) ---
/// <summary>
/// 获取纯元数据的 JSON 字符串
/// </summary>
public string GetMetadataJson()
{
// 在序列化前自动更新标志位,防止逻辑不同步
HasOriginalImage = (OriginalImageBytes != null && OriginalImageBytes.Length > 0);
HasTargetImage = (TargetImageBytes != null && TargetImageBytes.Length > 0);
return JsonConvert.SerializeObject(this);
}
public static VideoPayload? FromMetadataJson(string json)
{
return JsonConvert.DeserializeObject<VideoPayload>(json);
}
#endregion
}
}