using CoreAgent.Domain.Helpers; using CoreAgent.Domain.Interfaces.ProtocolLogHandlers; using CoreAgent.Domain.Models.Protocol; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace CoreAgent.Infrastructure.Services.ProtocolLogHandlers { public class ProtocolLogsParesRanHandle : ProtocolParesHandleLogs { private readonly ILogger logger; private readonly IProtocolLogsProviderObserver observer; public ProtocolLogsParesRanHandle(IProtocolLogsProviderObserver observer, ILogger logger) { this.observer = observer; this.logger = logger; } public override async Task GetTryParesLogDataHandle(ProtocolLog model) { try { if (model is { Logs: null }) { logger.LogError($"logs is null =====>GetTryParesLogDataHandle Data {model?.ObjToJson()}"); return; } var ParseJsonData = model.Logs.ToString().JsonToObj(); //var parseResultData = RanLogParseHandle(ParseJsonData, (model.MessageId ?? 0)); //if (parseResultData.Any()) //{ // var MsgData = new { data = parseResultData, MessageType = CSCIMessageType.ProtocolLogsRAN }.ObjToJson(); // logger.LogInformation($"OnData:{MsgData}"); // observer.OnData(MsgData); //} } catch (Exception ex) { logger.LogError($"GetTryParesLogDataHandle Data {model?.ObjToJson()}"); logger.LogError(ex.ToString()); } } //#region 分析 //private List RanLogParseHandle(ProtocolLogDetail[] data, int MessageID) //{ // List cellulars = new List(); // try // { // foreach (var msg in data) // { // ProtocolLogslayerType LayerType; // Enum.TryParse(msg.layer, out LayerType); // string info = string.Empty; // string TMIS = string.Empty; // string MessageInfo = msg.data[0]; // if (LayerType == ProtocolLogslayerType.RRC) // { // var reg = _regExpRRC_UE_ID.Match(MessageInfo); // if (reg is { Success: true }) // { // int ue_id = Convert.ToInt32(reg.Groups[1].ToString(), 16); // } // reg = _regExpInfo1.Match(MessageInfo); // if (reg.Success) // { // info = reg.Groups[1].Value; // MessageInfo = reg.Groups[2].Value; // GetlogListRRC(msg, MessageInfo); // } // reg = _regExpRRC_BC.Match(MessageInfo); // if (reg is { Success: true }) // { // string result = string.Join("\n", msg.data); // } // } // else if (LayerType == ProtocolLogslayerType.NAS) // { // var reg = _regExpInfo1.Match(MessageInfo); // if (reg is { Success: true }) // { // info = reg.Groups[1].Value; // MessageInfo = reg.Groups[2].Value; // TMIS = GetlogListNAS(msg, MessageInfo); // if (info.Equals("BCCH_NR")) // { // } // } // } // else if (LayerType == ProtocolLogslayerType.PHY) // { // var reg = _regExpPhy.Match(MessageInfo); // if (reg is { Success: true }) // { // int cell = Convert.ToInt32(reg.Groups[1].Value, 16); // int rnti = Convert.ToInt32(reg.Groups[2].Value, 16); // var channel = reg.Groups[4].Value; // string[] frames = reg.Groups[3].Value.Split('.'); // int frame = Convert.ToInt32(frames[0]) - 0; // int slot = Convert.ToInt32(frames[1]) - 0; // } // } // else if (LayerType == ProtocolLogslayerType.MAC) // { // var reg = _regExpCellID.Match(MessageInfo); // if (reg is { Success: true }) // { // int cell = Convert.ToInt32(reg.Groups[1].Value, 16); // MessageInfo = reg.Groups[2].Value; // } // } // CellularNetworkProtocolLogsEntity entity = new CellularNetworkProtocolLogsEntity // { // MessageID = MessageID, // CellID = msg.cell, // Timestamp = msg.timestamp, // UEID = msg.ue_id, // Time = msg.timestamp.UnixTimeStamp13ToBeijingTime().TimeOfDay, // ProtocolLayer = LayerType, // Info = info, // TMSI = TMIS, // Message = MessageInfo, // ProtocolType = msg.src, // Index = msg.idx, // Direction = msg.dir switch // { // "UL" => DirectionLogsType.UL, // "DL" => DirectionLogsType.DL, // _ => DirectionLogsType._ // }, // MessageData = msg.data.ToArray(), // }; // cellulars.Add(entity); // } // return cellulars; // } // catch (Exception ex) // { // logger.Error("ran logs analysis combination" + ex.Message); // return cellulars; // } //} //private void GetlogListRRC(ProtocolNetWorkCoreLogsDetailEntity log, string logType) //{ // try // { // switch (logType.ToLower()) // { // case "sib1": // break; // case "sib": // break; // case "rrc connection request": // var lte = GetFindData(log.data, _regExpRRC_TMSI); // if (lte is { Success: true }) // { // int tmsi = GetTMSI(log, lte.Groups[2].Value.ToString()); // } // break; // case "rrc connection reconfiguration": // var connection = GetFindData(log.data, _regExpRRC_TMSI); // if (connection is { Success: true }) // { // int rnti = GetRNTI(log, connection.Groups[1].Value); // } // break; // case "rrc connection reestablishment request": // var request = GetFindData(log.data, _regExpRRC_TMSI); // if (request is { Success: true }) // { // int rnti = GetRNTI(log, request.Groups[1].Value); // } // break; // case "rrc setup request": // var RRCrequest = GetFindData(log.data, _regExpRRC_TMSI); // break; // case "ue capability information": // break; // default: // break; // } // } // catch (Exception ex) // { // logger.Error("GetlogListRRC" + ex.Message); // } //} //private string GetlogListNAS(ProtocolNetWorkCoreLogsDetailEntity log, string logType) //{ // switch (logType.ToLower()) // { // case "attach accept": // var lte = GetFindData(log.data, _regExpNAS_TMSI); // if (lte is not null && !string.IsNullOrWhiteSpace(lte.Groups[1]?.ToString())) // { // int tmsi = GetTMSI(log, lte.Groups[1].Value); // return lte.Groups[1].Value; // } // break; // case "attach request": // var data = log.data; // break; // case "registration accept": // var Nr = GetFindData(log.data, _regExpNAS_5GTMSI); // if (Nr is not null && !string.IsNullOrWhiteSpace(Nr?.Groups[1]?.ToString())) // { // int tmsi = GetTMSI(log, Nr.Groups[1].Value); // return Nr.Groups[1].Value; // } // break; // case "registration request": // var requestData = log.data; // break; // } // return string.Empty; //} //private Match GetFindData(List data, Regex regExp) //{ // if (data.Any()) // { // for (var i = 0; i < data.Count; i++) // { // Match m = regExp.Match(data[i]); // if (m.Success) // return m; // } // } // return default; //} //private int GetTMSI(ProtocolNetWorkCoreLogsDetailEntity log, string tmsi) //{ // return Convert.ToInt32(tmsi, 16); //} //private int GetRNTI(ProtocolNetWorkCoreLogsDetailEntity log, string rnti) //{ // // 定义正则表达式模式 // string pattern = @"'([\dA-F]+)'H"; // // 创建正则表达式对象 // Regex regex = new Regex(pattern); // // 进行匹配 // Match match = regex.Match(rnti); // // 检查是否有匹配 // if (match.Success) // return Convert.ToInt32(match.Groups[1].Value, 16); // else return 0; //} //private bool GetCheckLayerTypeInfo(string layer) //{ // string[] ArraryLayer = new string[] { "PDSCH", "PDCCH", "EPDCCH", "PUSCH", "PUCCH", "NPDSCH", "NPUSCH", "NPBCH", "BCCH-NR", "HINTS", "SRS", "CSI", "SIM-Even", "IP-SIM" }; // return ArraryLayer.Any(s => s.Equals(layer)); //} //#endregion } }