You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
338 lines
9.9 KiB
338 lines
9.9 KiB
using Newtonsoft.Json;
|
|
|
|
namespace CoreAgent.Domain.Models.Protocol;
|
|
|
|
/// <summary>
|
|
/// 协议日志解析结果
|
|
/// 用于存储ProtocolLog解析后的分析结果,用于数据传输和处理
|
|
/// 遵循DDD(领域驱动设计)原则,作为领域模型的一部分
|
|
/// </summary>
|
|
public class ProtocolLogParsedResult
|
|
{
|
|
/// <summary>
|
|
/// 唯一标识符
|
|
/// </summary>
|
|
[JsonProperty("id")]
|
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
|
|
|
/// <summary>
|
|
/// 日志索引
|
|
/// </summary>
|
|
[JsonProperty("index")]
|
|
public int? Index { get; set; }
|
|
|
|
/// <summary>
|
|
/// 消息ID
|
|
/// </summary>
|
|
[JsonProperty("messageId")]
|
|
public int? MessageId { get; set; }
|
|
|
|
/// <summary>
|
|
/// 协议层类型
|
|
/// </summary>
|
|
[JsonProperty("protocolLayer")]
|
|
public ProtocolLayerType ProtocolLayer { get; set; }
|
|
|
|
/// <summary>
|
|
/// 协议类型描述
|
|
/// </summary>
|
|
[JsonProperty("protocolType")]
|
|
public string ProtocolType { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 用户设备ID
|
|
/// </summary>
|
|
[JsonProperty("ueId")]
|
|
public int? UeId { get; set; }
|
|
|
|
/// <summary>
|
|
/// 公共陆地移动网络标识
|
|
/// </summary>
|
|
[JsonProperty("plmn")]
|
|
public string? Plmn { get; set; }
|
|
|
|
/// <summary>
|
|
/// 临时移动用户标识
|
|
/// </summary>
|
|
[JsonProperty("tmsi")]
|
|
public string? Tmsi { get; set; }
|
|
|
|
/// <summary>
|
|
/// 国际移动设备标识
|
|
/// </summary>
|
|
[JsonProperty("imei")]
|
|
public string? Imei { get; set; }
|
|
|
|
/// <summary>
|
|
/// 国际移动用户标识
|
|
/// </summary>
|
|
[JsonProperty("imsi")]
|
|
public string? Imsi { get; set; }
|
|
|
|
/// <summary>
|
|
/// 小区ID
|
|
/// </summary>
|
|
[JsonProperty("cellId")]
|
|
public int? CellId { get; set; }
|
|
|
|
/// <summary>
|
|
/// 小区信息
|
|
/// </summary>
|
|
[JsonProperty("cell")]
|
|
public string? Cell { get; set; }
|
|
|
|
/// <summary>
|
|
/// 日志信息
|
|
/// </summary>
|
|
[JsonProperty("info")]
|
|
public string? Info { get; set; }
|
|
|
|
/// <summary>
|
|
/// 消息内容
|
|
/// </summary>
|
|
[JsonProperty("message")]
|
|
public string? Message { get; set; }
|
|
|
|
/// <summary>
|
|
/// 消息数据数组
|
|
/// </summary>
|
|
[JsonProperty("messageData")]
|
|
public string[]? MessageData { get; set; }
|
|
|
|
/// <summary>
|
|
/// 时间间隔
|
|
/// </summary>
|
|
[JsonProperty("time")]
|
|
public TimeSpan Time { get; set; }
|
|
|
|
/// <summary>
|
|
/// 时间戳
|
|
/// </summary>
|
|
[JsonProperty("timestamp")]
|
|
public long Timestamp { get; set; }
|
|
|
|
/// <summary>
|
|
/// 日志方向
|
|
/// </summary>
|
|
[JsonProperty("direction")]
|
|
public DirectionLogsType Direction { get; set; }
|
|
|
|
/// <summary>
|
|
/// 协议层分类
|
|
/// </summary>
|
|
[JsonProperty("category")]
|
|
public string Category => ProtocolLayer.GetCategory();
|
|
|
|
/// <summary>
|
|
/// 协议层显示名称
|
|
/// </summary>
|
|
[JsonProperty("displayName")]
|
|
public string DisplayName => ProtocolLayer.GetDisplayName();
|
|
|
|
/// <summary>
|
|
/// 初始化协议日志解析结果的新实例
|
|
/// </summary>
|
|
public ProtocolLogParsedResult()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化协议日志解析结果的新实例
|
|
/// </summary>
|
|
/// <param name="messageId">消息ID</param>
|
|
/// <param name="index">日志索引</param>
|
|
/// <param name="protocolLayer">协议层类型</param>
|
|
/// <param name="ueId">用户设备ID</param>
|
|
/// <param name="plmn">公共陆地移动网络标识</param>
|
|
/// <param name="tmsi">临时移动用户标识</param>
|
|
/// <param name="imei">国际移动设备标识</param>
|
|
/// <param name="imsi">国际移动用户标识</param>
|
|
/// <param name="cellId">小区ID</param>
|
|
/// <param name="cell">小区信息</param>
|
|
/// <param name="info">日志信息</param>
|
|
/// <param name="message">消息内容</param>
|
|
/// <param name="messageData">消息数据数组</param>
|
|
/// <param name="direction">日志方向</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从ProtocolLog创建ProtocolLogParsedResult
|
|
/// </summary>
|
|
/// <param name="protocolLog">协议日志</param>
|
|
/// <param name="index">日志索引</param>
|
|
/// <returns>协议日志解析结果</returns>
|
|
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
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从ProtocolLogDetail创建ProtocolLogParsedResult
|
|
/// </summary>
|
|
/// <param name="detail">协议日志明细</param>
|
|
/// <param name="index">日志索引</param>
|
|
/// <returns>协议日志解析结果</returns>
|
|
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
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解析方向字符串为DirectionLogsType
|
|
/// </summary>
|
|
/// <param name="direction">方向字符串</param>
|
|
/// <returns>方向类型</returns>
|
|
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
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查解析结果是否包含有效数据
|
|
/// </summary>
|
|
/// <returns>是否包含有效数据</returns>
|
|
public bool HasValidData()
|
|
{
|
|
return !string.IsNullOrEmpty(Id) &&
|
|
(ProtocolLayer != ProtocolLayerType.NONE || !string.IsNullOrEmpty(ProtocolType));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取解析结果摘要信息
|
|
/// </summary>
|
|
/// <returns>摘要信息</returns>
|
|
public string GetSummary()
|
|
{
|
|
return $"ProtocolLogParsed[{Id}] - {ProtocolLayer.GetDisplayName()} - {Direction} - UE:{UeId} - Cell:{CellId}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建解析结果的副本
|
|
/// </summary>
|
|
/// <returns>新的协议日志解析结果</returns>
|
|
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
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为JSON字符串
|
|
/// </summary>
|
|
/// <returns>JSON字符串</returns>
|
|
public string ToJson()
|
|
{
|
|
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从JSON字符串创建解析结果
|
|
/// </summary>
|
|
/// <param name="json">JSON字符串</param>
|
|
/// <returns>协议日志解析结果</returns>
|
|
public static ProtocolLogParsedResult FromJson(string json)
|
|
{
|
|
return JsonConvert.DeserializeObject<ProtocolLogParsedResult>(json) ?? new ProtocolLogParsedResult();
|
|
}
|
|
}
|