using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.Enums; using JiShe.CollectBus.Common.Extensions; 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; using System; namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0AH { /// /// 5.5.1.3.53 F66:定时上报 2 类数据任务设置 /// public class AFN10_F66_Analysis : IAnalysisStrategy { private readonly ILogger _logger; private readonly AnalysisStrategyContext _analysisStrategyContext; private readonly DataStorage _dataStorage; public AFN10_F66_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); var data = new AnalysisBaseDto() { FiledDesc = "终端电能表/交流采样装置配置参数", DataValue = await GenerateFinalResult(input.UnitData.HexMessageList), ItemType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}" }; // 查询电表信息 AmmeterInfo ammeterInfo = await _dataStorage.GetMeterInfoAsync(MeterTypeEnum.Focus.ToString(), "15"); if (ammeterInfo != null) { data.ProjectId = ammeterInfo.ProjectID; data.DeviceId = ammeterInfo.FocusId; data.DatabaseBusiID = ammeterInfo.DatabaseBusiID; data.DeviceAddress = ammeterInfo.Address; data.DeviceType = MeterTypeEnum.Focus; } UnitDataAnalysis> dto = new UnitDataAnalysis> { Code = input.A.Code!, AFN = input.AFN_FC.AFN, Fn = input.DT.Fn, Pn = input.DA.Pn, Data = data, ReceivedHexMessage = input.BaseHexMessage.HexMessageString, MessageId = input.MessageId, ReceivedTime = input.ReceivedTime, DensityUnit = DensityUnit.None, TimeDensity = -1, DataType = IOTDBDataTypeConst.Param }; result?.Invoke(dto); await _dataStorage.SaveDataToIotDbAsync(dto); return await Task.FromResult(true); } catch (Exception ex) { _logger.LogError(ex, $"0A_66解析失败:{input.A?.Code}-{input.DT?.Fn ?? 0}-{input?.BaseHexMessage?.HexMessageString},{ex.Message}"); } return await Task.FromResult(false); } public async Task GenerateFinalResult(List hexMessageList) { AFN10_F66_AnalysisDto entity = new AFN10_F66_AnalysisDto(); var cycleBin = hexMessageList[4].HexToBin().PadLeft(8, '0'); var cycleUnitBin = cycleBin.Substring(0, 2); var cycleValueBin = cycleBin.Substring(2, 6); entity.Cycle = cycleValueBin.BinToDec();//定时发送周期 entity.Unit = cycleUnitBin.BinToDec();//定时发送周期(单位) //TODO:发送基准时间 var arrBaseTime = hexMessageList.GetRange(5, 6); await _analysisStrategyContext.ExecuteAsync>(nameof(Appendix_A1), arrBaseTime, (value) => { var baseTimeArrStr = (string)value; var baseTimeArr = baseTimeArrStr.Split('_'); //entity.BaseTime = DateTime.Parse($"{DateTime.Now.Year.ToString().Substring(0, 2)}{arrBaseTime[0]}-{arrBaseTime[1]}-{arrBaseTime[2]} {arrBaseTime[3]}:{arrBaseTime[4]}:{arrBaseTime[5]}"); entity.BaseTime = Convert.ToDateTime(baseTimeArr[0]); }); entity.CurveRatio = hexMessageList[11].HexToDec(); var count = hexMessageList[12].HexToDec(); var dataArr = hexMessageList.GetRange(13, 4 * count); for (int i = 0; i < count; i++) { var pnfnArr = dataArr.GetRange(0, 4); var tempPn = T37612012ProtocolPlugin.CalculatePn(pnfnArr[0], pnfnArr[1]); var tempFn = T37612012ProtocolPlugin.CalculateFn(pnfnArr[2], pnfnArr[3]); entity.Details.Add(new SetAutoItemCodeDetails() { Fn = tempFn, Pn = tempPn }); dataArr.RemoveRange(0, 4); } return entity; } } }