using System.Text.RegularExpressions; using WCTDataMiner.Core.Models; namespace WCTDataMiner.Core.Parsers; /// /// Ploss FOD日志解析器 /// 格式: FOD-> (1) X X X (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) /// 示例: FOD-> 4 2 1 127 10690 12013 35377 9904 1213 -2882 750 0 0 1 500 1 /// public class PlossParser : IParser { // 格式说明有3个X(忽略字段),正则需要3个 \d+ 来匹配 private static readonly Regex PlossPattern = new( @"^FOD->\s+(\d+)\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(-?\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$", RegexOptions.Compiled ); public bool CanParse(string line) => PlossPattern.IsMatch(line); public ParseResult Parse(string line, Guid scenarioId) { var match = PlossPattern.Match(line); if (!match.Success) { return ParseResult.Failure( "FORMAT_MISMATCH", "Line does not match Ploss pattern" ); } try { var record = new PlossRecord { ScenarioId = scenarioId, RxType = byte.Parse(match.Groups[1].Value), RxPower = int.Parse(match.Groups[2].Value), TxPower = int.Parse(match.Groups[3].Value), Vcoil = int.Parse(match.Groups[4].Value), Vin = int.Parse(match.Groups[5].Value), Isns = int.Parse(match.Groups[6].Value), Ploss = int.Parse(match.Groups[7].Value), Threshold = int.Parse(match.Groups[8].Value), TriggerCount = int.Parse(match.Groups[9].Value), FodResult = int.Parse(match.Groups[10].Value), ProtocolType = byte.Parse(match.Groups[11].Value), PwmDuty = int.Parse(match.Groups[12].Value), CoilIndex = byte.Parse(match.Groups[13].Value) }; return ParseResult.Success(record); } catch (Exception ex) { return ParseResult.Failure( "TYPE_CONVERSION", $"Failed to convert field: {ex.Message}" ); } } }