using JiShe.CollectBus.Common.Enums; using JiShe.CollectBus.Common.Extensions; using JiShe.CollectBus.Common.Helpers; using JiShe.CollectBus.IotSystems.Ammeters; using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto; using JiShe.CollectBus.Protocol.Dto; using JiShe.CollectBus.Protocol.Interfaces; using JiShe.CollectBus.Protocol.T37612012.Appendix; using JiShe.CollectBus.Protocol3761; using Microsoft.Extensions.Logging; namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0DH { /// /// 5.13.2.4.84 F108:测量点 C相功率因数曲线 /// public class AFN13_F108_Analysis : IAnalysisStrategy { private readonly ILogger _logger; private readonly AnalysisStrategyContext _analysisStrategyContext; private readonly DataStorage _dataStorage; public AFN13_F108_Analysis(ILogger logger, AnalysisStrategyContext analysisStrategyContext, DataStorage dataStorage) { _logger = logger; _analysisStrategyContext = analysisStrategyContext; _dataStorage = dataStorage; } public async Task ExecuteAsync(TB3761 input, Action? result = null) { try { ArgumentNullException.ThrowIfNull(input); ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList); List datas = await AnalysisDataUnitAsync(input.UnitData.HexMessageList); int density = Convert.ToInt32(Convert.ToInt32(datas[1]).GetEnumDescription(typeof(DensityEnums))); string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}"; List> data = datas.GenerateFinalResultTd_c(3, density, dataType, "C相功率因数"); if (data.Count > 0) { // 查询电表信息 AmmeterInfo ammeterInfo = await _dataStorage.GetMeterInfoAsync(data[0].DeviceType.ToString(), "15"); if (ammeterInfo != null) { data.ForEach(item => { item.ProjectId = ammeterInfo.ProjectID; item.DeviceId = ammeterInfo.MeterId; item.DatabaseBusiID = ammeterInfo.DatabaseBusiID; item.DeviceAddress = ammeterInfo.AmmerterAddress; }); } } UnitDataAnalysis>> unitDataAnalysis = new UnitDataAnalysis>> { Code = input.A.Code!, AFN = input.AFN_FC.AFN, Fn = input.DT.Fn, Pn = input.DA.Pn, MSA = input.A.A3!.D1_D7!, PSEQ = input.SEQ.PSEQ, Data = data, HexMessage = input.BaseHexMessage.HexMessageString, MessageId = input.MessageId, TimeDensity = density,//密度-间隔分钟数, DensityUnit = DensityUnit.Minute, ReceivedTime = input.ReceivedTime }; await _dataStorage.SaveMultipleDataToIotDbAsync(unitDataAnalysis); result?.Invoke(unitDataAnalysis); return await Task.FromResult(true); } catch (Exception ex) { _logger.LogError(ex, $"0D_107解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}"); return await Task.FromResult(false); } } private async Task> AnalysisDataUnitAsync(List hexMessageList) { List values = new List(); values.AddRange(hexMessageList.GetReadTimeTd_c(4, 7)); int.TryParse(values[values.Count - 1], out int n); for (int i = 0; i < n; i++) { var arr = hexMessageList.GetRange(11 + (i * 2), 2); var errorCode = arr.CheckErrorCode(); if (errorCode != null) values.Add(errorCode.Item1); else { await _analysisStrategyContext.ExecuteAsync>(nameof(Appendix_A5), arr, (value) => { values.Add(value.ToString()); }); } } return values; } } }