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.
80 lines
3.5 KiB
80 lines
3.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using CoreAgent.ProtocolClient.Context.UeStateManager;
|
|
using CoreAgent.ProtocolClient.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CoreAgent.ProtocolClient.Context
|
|
{
|
|
/// <summary>
|
|
/// 日志相关
|
|
/// </summary>
|
|
public class ProtocolLogContext
|
|
{
|
|
private static int _logIdCounter;
|
|
private readonly ILogger<ProtocolLogContext> _logger;
|
|
private readonly ProtocolClientContext _context;
|
|
|
|
public List<SourceProtocolLog> Logs { get; set; } = new();
|
|
public int LogCount => Logs.Count;
|
|
|
|
public ProtocolLogContext(ILogger<ProtocolLogContext> logger, ProtocolClientContext context)
|
|
{
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
_context = context ?? throw new ArgumentNullException(nameof(context));
|
|
}
|
|
public void ResetLogs()
|
|
{
|
|
if (LogCount > 0)
|
|
{
|
|
Logs.Clear();
|
|
_context.FeatureFlags.HasCell = false;
|
|
_context.FeatureFlags.HasPhy = false;
|
|
_context.FeatureFlags.HasData = false;
|
|
_context.FeatureFlags.HasRnti = false;
|
|
_context.FeatureFlags.HasRb = false;
|
|
_context.FeatureFlags.HasSignalRecord = false;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 更新日志的时间戳并分配唯一ID。
|
|
/// 检查并处理时间戳回绕,详细记录回绕前后各关键字段,包括:
|
|
/// 1. 原始日志时间戳(log.Timestamp)
|
|
/// 2. 当前偏移量(parser.TimestampOffset)
|
|
/// 3. 计算得到的当前时间戳(timestamp)
|
|
/// 4. 上次处理的时间戳(parser.LastTimestamp)
|
|
/// 5. 回绕后新的时间戳(timestamp + 86400000)
|
|
/// 6. 本次回绕的增量(86400000)
|
|
/// 7. 日志分配的唯一ID(log.Id)
|
|
/// </summary>
|
|
public void UpdateLogTimestampAndAssignId(ref BuildProtocolLog log)
|
|
{
|
|
// 检查时间戳回绕
|
|
var parser = _context.UeIdentifier;
|
|
var timestamp = log.Timestamp + parser.TimestampOffset;
|
|
if (timestamp < parser.LastTimestamp - 100)
|
|
{
|
|
_logger.LogInformation($"[Log回绕] 原始时间戳: {log.Timestamp}, 偏移量: {parser.TimestampOffset}, 回绕前: {timestamp}, parser.LastTimestamp(回绕前): {parser.LastTimestamp}, 回绕后: {timestamp + 86400000}, parser.LastTimestamp(回绕后): {parser.LastTimestamp}, 增量: 86400000, 日志ID(回绕前): {log.Id}");
|
|
timestamp += 86400000; // 24小时
|
|
parser.TimestampOffset += 86400000;
|
|
_logger.LogInformation($"[Log回绕后] 新时间戳: {timestamp}, parser.LastTimestamp(回绕后): {parser.LastTimestamp}, 新偏移量: {parser.TimestampOffset}, 日志ID(回绕后): {log.Id}");
|
|
}
|
|
var lastTimestampBefore = parser.LastTimestamp;
|
|
parser.LastTimestamp = log.Timestamp = timestamp;
|
|
log.Id = GenerateLogId();
|
|
_logger.LogDebug($"[Log分配ID] 日志最终时间戳: {log.Timestamp}, 分配ID: {log.Id}, 当前偏移量: {parser.TimestampOffset}, parser.LastTimestamp(分配前): {lastTimestampBefore}, parser.LastTimestamp(分配后): {parser.LastTimestamp}");
|
|
}
|
|
|
|
|
|
private static int GenerateLogId()
|
|
{
|
|
return Interlocked.Increment(ref _logIdCounter);
|
|
}
|
|
|
|
}
|
|
}
|
|
|