using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.Enums; using JiShe.CollectBus.Common.Extensions; using JiShe.CollectBus.Common.Helpers; 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.Protocol3761; using Microsoft.Extensions.Logging; using YamlDotNet.Core.Tokens; using static FreeSql.Internal.GlobalFilter; namespace JiShe.CollectBus.Protocol.T37612012.AFN_0CH { /// /// 水表抄读取 /// public class AFN12_F188_Analysis : IAnalysisStrategy { private readonly ILogger _logger; private readonly AnalysisStrategyContext _analysisStrategyContext; private readonly DataStorage _dataStorage; public AFN12_F188_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); ArgumentNullException.ThrowIfNull(input.AFN_FC.AFN); ArgumentNullException.ThrowIfNull(input.DT.Fn); string itemType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}"; AnalysisBaseDto data = GenerateFinalResult(input.UnitData.HexMessageList, itemType); // 查询设备信息 DeviceInfo? 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> 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.Second, TimeDensity = 0, DataType = IOTDBDataTypeConst.Data, }; result?.Invoke(dto); await _dataStorage.SaveDataToIotDbAsync(dto); return await Task.FromResult(true); } catch (Exception ex) { _logger.LogError(ex, $"0C_188解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage?.HexMessageString},{ex.Message}"); } return await Task.FromResult(false); } public AnalysisBaseDto GenerateFinalResult(List hexMessageList,string itemType) { AnalysisBaseDto dto = new AnalysisBaseDto { DeviceType = MeterTypeEnum.WaterMeter }; var arr = hexMessageList.GetRange(11, 4); var errorCodeInfo = arr.CheckErrorCode(); if (errorCodeInfo != null) { dto.ValidData = false; dto.ErrorCodeMsg = errorCodeInfo.Item2; } else { if (decimal.TryParse($"{arr[11]}{arr[12]}{arr[13]}.{arr[14]}", out decimal value)) dto.DataValue = value; } dto.ItemType = itemType; dto.FiledDesc = "水示值"; dto.FiledName = dto.ItemType.GetDataFieldByGatherDataType() ?? string.Empty; return dto; } } }