using JiShe.CollectBus.Common.Extensions; using JiShe.CollectBus.Protocol.Dto; using JiShe.CollectBus.Protocol.Interfaces; using JiShe.CollectBus.Protocol3761; using Microsoft.Extensions.Logging; namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0EH { /// /// 请求重要事件 /// Item1=停电事件 /// Item2=上电事件 /// public class AFN14_F1_Analysis : IAnalysisStrategy { private readonly ILogger _logger; public AFN14_F1_Analysis(ILogger logger) { _logger = logger; } public Task ExecuteAsync(TB3761 input, Action? result = null) { try { ArgumentNullException.ThrowIfNull(nameof(input)); ArgumentNullException.ThrowIfNull(nameof(input.UnitData.HexMessageList)); int erc = input.UnitData.HexMessageList![8].HexToDec(); bool isOnOffPower = erc.Equals(14) ? true : false; if (!isOnOffPower) { throw new Exception($"ERC{erc}非上掉电事件"); } UnitDataAnalysis> dto = new UnitDataAnalysis> { Code = input.A.Code!, AFN = input.AFN_FC.AFN, Fn = input.DT.Fn, Pn = input.DA.Pn, Data = AnalysisDataUnit(input.UnitData.HexMessageList), HexMessage = input.BaseHexMessage.HexMessageString, MessageId = input.MessageId, ReceivedTime = input.ReceivedTime }; // meterData.DataType = "0E_1"; result?.Invoke(dto); return Task.FromResult(true); } catch (Exception ex) { _logger.LogError(ex, $"0E_1解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}"); } return Task.FromResult(false); } private Tuple AnalysisDataUnit(List hexMessageList) { /// Item1=停电事件 /// Item2=上电事件 return Tuple.Create(HandlerTime(hexMessageList.GetRange(10, 5)), HandlerTime(hexMessageList.GetRange(15, 5))); } /// /// /解析时间 /// /// /// 时间验证失败是否默认为当前时间 /// private string HandlerTime(List times) { var time = string.Empty; try { times.Reverse(); time = $"{DateTime.Now.ToString("yyyy").Substring(0, 2)}{times[0]}-{times[1]}-{times[2]} {times[3]}:{times[4]}:00"; } catch { } return time; } } }