using Newtonsoft.Json; namespace CoreAgent.Domain.Models.Protocol; /// /// 协议日志解析结果 /// 用于存储ProtocolLog解析后的分析结果,用于数据传输和处理 /// 遵循DDD(领域驱动设计)原则,作为领域模型的一部分 /// public class ProtocolLogParsedResult { /// /// 唯一标识符 /// [JsonProperty("id")] public string Id { get; set; } = Guid.NewGuid().ToString(); /// /// 日志索引 /// [JsonProperty("index")] public int? Index { get; set; } /// /// 消息ID /// [JsonProperty("messageId")] public int? MessageId { get; set; } /// /// 协议层类型 /// [JsonProperty("protocolLayer")] public ProtocolLayerType ProtocolLayer { get; set; } /// /// 协议类型描述 /// [JsonProperty("protocolType")] public string ProtocolType { get; set; } = string.Empty; /// /// 用户设备ID /// [JsonProperty("ueId")] public int? UeId { get; set; } /// /// 公共陆地移动网络标识 /// [JsonProperty("plmn")] public string? Plmn { get; set; } /// /// 临时移动用户标识 /// [JsonProperty("tmsi")] public string? Tmsi { get; set; } /// /// 国际移动设备标识 /// [JsonProperty("imei")] public string? Imei { get; set; } /// /// 国际移动用户标识 /// [JsonProperty("imsi")] public string? Imsi { get; set; } /// /// 小区ID /// [JsonProperty("cellId")] public int? CellId { get; set; } /// /// 小区信息 /// [JsonProperty("cell")] public string? Cell { get; set; } /// /// 日志信息 /// [JsonProperty("info")] public string? Info { get; set; } /// /// 消息内容 /// [JsonProperty("message")] public string? Message { get; set; } /// /// 消息数据数组 /// [JsonProperty("messageData")] public string[]? MessageData { get; set; } /// /// 时间间隔 /// [JsonProperty("time")] public TimeSpan Time { get; set; } /// /// 时间戳 /// [JsonProperty("timestamp")] public long Timestamp { get; set; } /// /// 日志方向 /// [JsonProperty("direction")] public DirectionLogsType Direction { get; set; } /// /// 协议层分类 /// [JsonProperty("category")] public string Category => ProtocolLayer.GetCategory(); /// /// 协议层显示名称 /// [JsonProperty("displayName")] public string DisplayName => ProtocolLayer.GetDisplayName(); /// /// 初始化协议日志解析结果的新实例 /// public ProtocolLogParsedResult() { } /// /// 初始化协议日志解析结果的新实例 /// /// 消息ID /// 日志索引 /// 协议层类型 /// 用户设备ID /// 公共陆地移动网络标识 /// 临时移动用户标识 /// 国际移动设备标识 /// 国际移动用户标识 /// 小区ID /// 小区信息 /// 日志信息 /// 消息内容 /// 消息数据数组 /// 日志方向 public ProtocolLogParsedResult( int? messageId = null, int? index = null, ProtocolLayerType protocolLayer = ProtocolLayerType.NONE, int? ueId = null, string? plmn = null, string? tmsi = null, string? imei = null, string? imsi = null, int? cellId = null, string? cell = null, string? info = null, string? message = null, string[]? messageData = null, DirectionLogsType direction = DirectionLogsType.Unknown) { MessageId = messageId; Index = index; ProtocolLayer = protocolLayer; UeId = ueId; Plmn = plmn; Tmsi = tmsi; Imei = imei; Imsi = imsi; CellId = cellId; Cell = cell; Info = info; Message = message; MessageData = messageData; Direction = direction; ProtocolType = protocolLayer.GetDisplayName(); } /// /// 从ProtocolLog创建ProtocolLogParsedResult /// /// 协议日志 /// 日志索引 /// 协议日志解析结果 public static ProtocolLogParsedResult FromProtocolLog(ProtocolLog protocolLog, int? index = null) { // 处理时间戳转换 long timestamp; if (protocolLog.Time.HasValue) { // 如果 Time 是秒为单位,转换为毫秒 timestamp = (long)(protocolLog.Time.Value * 1000); } else if (protocolLog.Utc.HasValue) { // 如果 Utc 是秒为单位,转换为毫秒 timestamp = (long)(protocolLog.Utc.Value * 1000); } else { // 默认使用当前时间 timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); } return new ProtocolLogParsedResult { MessageId = protocolLog.MessageId, Index = index, ProtocolLayer = ProtocolLayerType.NONE, // 需要根据实际内容解析 Message = protocolLog.Message, MessageData = protocolLog.Headers, Timestamp = timestamp, Time = TimeSpan.FromMilliseconds(protocolLog.Time ?? 0), Direction = DirectionLogsType.Unknown, ProtocolType = protocolLog.Type ?? string.Empty }; } /// /// 从ProtocolLogDetail创建ProtocolLogParsedResult /// /// 协议日志明细 /// 日志索引 /// 协议日志解析结果 public static ProtocolLogParsedResult FromProtocolLogDetail(ProtocolLogDetail detail, int? index = null) { return new ProtocolLogParsedResult { Index = index ?? detail.Idx, ProtocolLayer = ProtocolLayerTypeExtensions.FromString(detail.Layer), UeId = detail.UeId, CellId = detail.Cell, Info = detail.Src, MessageData = detail.Data?.ToArray(), Timestamp = detail.Timestamp, Time = TimeSpan.FromMilliseconds(detail.Timestamp), Direction = ParseDirection(detail.Dir), ProtocolType = detail.Layer }; } /// /// 解析方向字符串为DirectionLogsType /// /// 方向字符串 /// 方向类型 private static DirectionLogsType ParseDirection(string? direction) { if (string.IsNullOrWhiteSpace(direction)) return DirectionLogsType.Unknown; return direction.ToLower() switch { "up" or "uplink" or "ul" => DirectionLogsType.Uplink, "down" or "downlink" or "dl" => DirectionLogsType.Downlink, "bidirectional" or "bi" => DirectionLogsType.Bidirectional, "internal" or "int" => DirectionLogsType.Internal, _ => DirectionLogsType.Unknown }; } /// /// 检查解析结果是否包含有效数据 /// /// 是否包含有效数据 public bool HasValidData() { return !string.IsNullOrEmpty(Id) && (ProtocolLayer != ProtocolLayerType.NONE || !string.IsNullOrEmpty(ProtocolType)); } /// /// 获取解析结果摘要信息 /// /// 摘要信息 public string GetSummary() { return $"ProtocolLogParsed[{Id}] - {ProtocolLayer.GetDisplayName()} - {Direction} - UE:{UeId} - Cell:{CellId}"; } /// /// 创建解析结果的副本 /// /// 新的协议日志解析结果 public ProtocolLogParsedResult Copy() { return new ProtocolLogParsedResult { Id = Guid.NewGuid().ToString(), // 生成新的ID Index = Index, MessageId = MessageId, ProtocolLayer = ProtocolLayer, ProtocolType = ProtocolType, UeId = UeId, Plmn = Plmn, Tmsi = Tmsi, Imei = Imei, Imsi = Imsi, CellId = CellId, Cell = Cell, Info = Info, Message = Message, MessageData = MessageData?.Clone() as string[], Time = Time, Timestamp = Timestamp, Direction = Direction }; } /// /// 转换为JSON字符串 /// /// JSON字符串 public string ToJson() { return JsonConvert.SerializeObject(this, Formatting.Indented); } /// /// 从JSON字符串创建解析结果 /// /// JSON字符串 /// 协议日志解析结果 public static ProtocolLogParsedResult FromJson(string json) { return JsonConvert.DeserializeObject(json) ?? new ProtocolLogParsedResult(); } }