using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.Enums; using JiShe.CollectBus.Common.Extensions; using JiShe.CollectBus.IotSystems.Ammeters; using JiShe.CollectBus.IotSystems.Devices; using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto; using JiShe.CollectBus.Protocol.Dto; using JiShe.CollectBus.Protocol.Interfaces; using JiShe.CollectBus.Protocol.T37612012.AnalysisData; using JiShe.CollectBus.Protocol.T37612012.Appendix; using JiShe.CollectBus.Protocol3761; using Microsoft.Extensions.Logging; using System; using static FreeSql.Internal.GlobalFilter; namespace JiShe.CollectBus.Protocol.T37612012.AFN_0CH { /// /// 5.12.2.4.30 F33:当前正向有/无功电能示值、一/四象限无功电能示值(总、费率 1~M,1≤M≤12) /// public class AFN12_F33_Analysis : IAnalysisStrategy { private List DataUnitHexList { get; set; }=new List(); private readonly ILogger _logger; private readonly AnalysisStrategyContext _analysisStrategyContext; private readonly DataStorage _dataStorage; public AFN12_F33_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.A.Code); ArgumentNullException.ThrowIfNull(input.UnitData.HexMessageList); int rationgCount = input.UnitData.HexMessageList!.GetRatingCount(23, 1); DataUnitHexList = input.UnitData.HexMessageList.GetRange(24, (5 * (rationgCount + 1)) + (4 * (rationgCount + 1)) * 3); GetDataUnitHexString(input.UnitData.HexMessageList, rationgCount); var data = new AnalysisBaseDto() { FiledDesc = "当前正向有/无功电能示值、一/四象限无功电能示值", DataValue = await AnalysisDataUnit(input.UnitData.HexMessageList, rationgCount), ItemType= $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}" }; // 查询设备信息 DeviceCacheInfo? deviceInfo = await _dataStorage.GetDeviceInfoAsync(input.A.Code, input.DA.Pn); if (deviceInfo != null) { data.ProjectId = deviceInfo.ProjectId; data.DeviceId = deviceInfo.MeterId; data.DatabaseBusiID = deviceInfo.DatabaseBusiID; data.DeviceAddress = deviceInfo.MeterAddress; data.FocusId = deviceInfo.FocusId; } UnitDataAnalysis> unitDataAnalysis = 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.Second, TimeDensity = 0, DataType = IOTDBDataTypeConst.Data, }; result?.Invoke(unitDataAnalysis); await _dataStorage.SaveDataToIotDbAsync(unitDataAnalysis); return await Task.FromResult(true); } catch (Exception ex) { _logger.LogError(ex, $"0C_33解析失败:{input.A?.Code}-{input.DT?.Fn ?? 0}-{input?.BaseHexMessage?.HexMessageString},{ex.Message}"); } return await Task.FromResult(false); } #region 获取当前正向有/无功电能示值数据单元格式 private void GetDataUnitHexString(int rationgCount) private void GetDataUnitHexString(List hexMessageList, int rationgCount) { //(5*(x+1))+(4*(x+1))*3 var lenght = (5 * (rationgCount + 1)) + (4 * (rationgCount + 1)) * 3;//(rationgCount * 5 + 5) + (4 * 3) + (rationgCount * 4) * 2;//费率数(R):4 公式:5+5+(费率数*5)+ (4+(费率数*4))*2 DataUnitHexList = hexMessageList.GetRange(24, lenght); } #endregion #region 单元数据值解析 private F33Entity AnalysisDataUnit() private async Task AnalysisDataUnit(List hexMessageList, int rationgCount) { AFN12_F33_AnalysisDto f33Entity = new AFN12_F33_AnalysisDto(); f33Entity.ReadTime = hexMessageList.GetReadTime(18, 5); // 终端抄表时间 f33Entity.RatingCount = hexMessageList.GetRatingCount(23, 1); // 费率数 M(1≤M≤12) f33Entity.F_A_Kwh = await GetDataAsync(f33Entity.RatingCount, 0, 5, nameof(Appendix_A14));//当前正向有功总电能示值 f33Entity.R_R_Kvarh = await GetDataAsync(f33Entity.RatingCount, 25, 4, nameof(Appendix_A11));//当前正向无功(组合无功1)总电能示值 0+5*5 f33Entity.Q1_R_Kvarh = await GetDataAsync(f33Entity.RatingCount, 45, 4, nameof(Appendix_A11));//当前一象限无功总电能示值 25+4*5 f33Entity.Q4_R_Kvarh = await GetDataAsync(f33Entity.RatingCount, 65, 4, nameof(Appendix_A11));//当前四象限无功总电能示值 45+4*5 return f33Entity; } #region 有功/无功、费率总电能示值 /// /// 有功/无功、费率总电能示值 /// /// 费率数 /// 开始位置 /// 长度 /// private async Task GetDataAsync(int ratingCount, int startIndex, int len, string appendxName) { ParentNodes parent = new ParentNodes(); var arr = DataUnitHexList.GetRange(startIndex, len); await _analysisStrategyContext.ExecuteAsync>(appendxName, arr, (value) => { parent.Total_Value = value; }); parent.childNodes = await GetChildDataAsync(ratingCount, startIndex, len, appendxName); return parent; } /// /// 费率总电能示值 /// /// /// /// /// /// private async Task> GetChildDataAsync(int RatingCount, int startIndex, int len, string appendxName) { List children = new List(); for (int i = 0; i < RatingCount; i++) { var arr = DataUnitHexList.GetRange((i * len) + (startIndex + len), len); await _analysisStrategyContext.ExecuteAsync>(appendxName, arr, (value) => { ChildNodes model = new ChildNodes() { Value = value }; children.Add(model); }); } return children; } #endregion #endregion } }