using System.Text.RegularExpressions; using WCTDataMiner.Core.Models; namespace WCTDataMiner.Core.Parsers; /// /// Qfod日志解析器 /// 格式: X#:Q A->B C D E /// 示例: 0#:Q 1->25 45.5 20.5 2 /// public class QfodParser : IParser { private static readonly Regex QfodPattern = new( @"^(\d)#:Q\s+(\d+)->(-?\d+)\s+([-\d.]+)\s+([-\d.]+)\s+(\d+)$", RegexOptions.Compiled ); public bool CanParse(string line) => QfodPattern.IsMatch(line); public ParseResult Parse(string line, Guid scenarioId) { var match = QfodPattern.Match(line); if (!match.Success) { return ParseResult.Failure( "FORMAT_MISMATCH", "Line does not match Qfod pattern" ); } try { var record = new QfodRecord { ScenarioId = scenarioId, ChargerIndex = byte.Parse(match.Groups[1].Value), CoilIndex = byte.Parse(match.Groups[2].Value), DeltaQ = int.Parse(match.Groups[3].Value), CurrentQ = float.Parse(match.Groups[4].Value), RawQ = float.Parse(match.Groups[5].Value), FodType = byte.Parse(match.Groups[6].Value) }; return ParseResult.Success(record); } catch (Exception ex) { return ParseResult.Failure( "TYPE_CONVERSION", $"Failed to convert field: {ex.Message}" ); } } }