using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CoreAgent.ProtocolClient.Enums;
using CoreAgent.ProtocolClient.Models;
using Microsoft.Extensions.Logging;
namespace CoreAgent.ProtocolClient.Context.UeStateManager
{
///
/// UeIdentifierManager 协议解析部分
///
public partial class UeIdentifierManager
{
#region 协议解析方法
///
/// 解析提示信息
///
/// 协议日志
/// 解析配置
public void ParseHints(BuildProtocolLog log, Dictionary config)
{
if (log?.Info != (int)LogChannelId.ID_HINTS)
{
_logger?.LogDebug("跳过非提示信息日志: Info={Info}", log?.Info);
return;
}
try
{
var hints = ParseArguments(log.Message, config);
_logger?.LogDebug("解析提示信息成功,共解析 {Count} 个参数", hints.Count);
// TODO: 处理解析后的提示信息
}
catch (Exception ex)
{
_logger?.LogError(ex, "解析提示信息失败: {Message}", log.Message);
throw new ParsingException("Hints", log?.Message ?? "", $"解析提示信息失败: {ex.Message}", ex);
}
}
///
/// 解析参数字符串为字典
/// 性能优化:使用编译后的正则表达式
///
/// 参数字符串,格式如 "a=1 b=2 c=hello"
///
/// 配置字典,key为参数名,value为类型(Type)或处理函数(Func<string, object>)
/// 例如:{ "a", typeof(int) }, { "b", (Func<string, object>)(s => s.ToUpper()) }
///
/// 解析后的参数字典
private Dictionary ParseArguments(string arguments, Dictionary config)
{
if (string.IsNullOrEmpty(arguments))
{
return new Dictionary();
}
var result = new Dictionary();
// 使用编译后的正则表达式提高性能
var whitespaceRegex = new Regex(@"\s+", RegexOptions.Compiled);
try
{
foreach (var argument in whitespaceRegex.Split(arguments))
{
if (string.IsNullOrWhiteSpace(argument))
continue;
var keyValuePair = argument.Split('=', 2);
if (keyValuePair.Length != 2)
{
_logger?.LogWarning("跳过格式无效的参数: {Argument}", argument);
continue;
}
var key = keyValuePair[0];
var value = keyValuePair[1];
result[key] = ParseValue(value, key, config);
}
return result;
}
catch (Exception ex)
{
_logger?.LogError(ex, "解析参数字符串失败: {Arguments}", arguments);
throw new ParsingException("Arguments", arguments, $"解析参数字符串失败: {ex.Message}", ex);
}
}
///
/// 根据配置解析参数值
///
/// 原始值字符串
/// 参数键
/// 解析配置
/// 解析后的值
private object ParseValue(string value, string key, Dictionary config)
{
if (!config.TryGetValue(key, out var typeOrFunc))
return value;
try
{
return typeOrFunc switch
{
Type type when type == typeof(int) => int.TryParse(value, out var intValue) ? intValue : 0,
Type type when type == typeof(double) => double.TryParse(value, out var doubleValue) ? doubleValue : 0.0,
Func func => func(value),
_ => value
};
}
catch (Exception ex)
{
_logger?.LogError(ex, "解析参数值失败: Key={Key}, Value={Value}", key, value);
return value; // 返回原始值作为回退
}
}
///
/// 批量解析参数
/// 性能优化:批量处理减少重复计算
///
/// 参数字符串列表
/// 解析配置
/// 解析结果列表
public List> ParseArgumentsBatch(
IEnumerable argumentsList,
Dictionary config)
{
if (argumentsList == null)
{
throw new ArgumentNullException(nameof(argumentsList));
}
var results = new List>();
foreach (var arguments in argumentsList)
{
results.Add(ParseArguments(arguments, config));
}
_logger?.LogDebug("批量解析参数完成,共处理 {Count} 个参数字符串", results.Count);
return results;
}
#endregion
}
}