102 lines
4.1 KiB
C#
Raw Normal View History

2025-04-30 17:25:35 +08:00
using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Common.Extensions;
2025-05-06 19:25:15 +08:00
using JiShe.CollectBus.Common.Helpers;
2025-05-12 14:02:22 +08:00
using JiShe.CollectBus.IotSystems.Ammeters;
2025-04-24 19:31:28 +08:00
using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto;
using JiShe.CollectBus.Protocol.Dto;
2025-04-25 14:35:59 +08:00
using JiShe.CollectBus.Protocol.Interfaces;
2025-05-12 14:02:22 +08:00
using JiShe.CollectBus.Protocol.T37612012.AnalysisData;
2025-04-25 14:35:59 +08:00
using JiShe.CollectBus.Protocol3761;
2025-04-24 19:31:28 +08:00
using Microsoft.Extensions.Logging;
2025-05-08 15:12:37 +08:00
using YamlDotNet.Core.Tokens;
2025-04-24 19:31:28 +08:00
namespace JiShe.CollectBus.Protocol.T37612012.AFN_0CH
2025-04-24 19:31:28 +08:00
{
/// <summary>
/// 水表抄读取
/// </summary>
public class AFN12_F188_Analysis : IAnalysisStrategy<TB3761>
2025-04-24 19:31:28 +08:00
{
private readonly ILogger<AFN12_F188_Analysis> _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
2025-05-12 14:02:22 +08:00
private readonly DataStorage _dataStorage;
public AFN12_F188_Analysis(ILogger<AFN12_F188_Analysis> logger, AnalysisStrategyContext analysisStrategyContext, DataStorage dataStorage)
2025-04-24 19:31:28 +08:00
{
_logger = logger;
_analysisStrategyContext = analysisStrategyContext;
2025-05-12 14:02:22 +08:00
_dataStorage = dataStorage;
2025-04-24 19:31:28 +08:00
}
public async Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
2025-04-24 19:31:28 +08:00
{
try
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
ArgumentNullException.ThrowIfNull(input.AFN_FC.AFN);
ArgumentNullException.ThrowIfNull(input.DT.Fn);
2025-05-06 19:25:15 +08:00
string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
2025-05-08 15:12:37 +08:00
AnalysisBaseDto<decimal?> data = GenerateFinalResult(input.UnitData.HexMessageList, dataType);
2025-05-12 14:02:22 +08:00
// 查询电表信息
AmmeterInfo ammeterInfo = await _dataStorage.GetMeterInfoAsync(data.DeviceType.ToString(), "15");
if (ammeterInfo != null)
{
data.ProjectId = ammeterInfo.ProjectID;
data.DeviceId = ammeterInfo.MeterId;
data.DatabaseBusiID = ammeterInfo.DatabaseBusiID;
data.DeviceAddress = ammeterInfo.AmmerterAddress;
}
2025-05-08 15:12:37 +08:00
UnitDataAnalysis<AnalysisBaseDto<decimal?>> dto = new UnitDataAnalysis<AnalysisBaseDto<decimal?>>
2025-04-24 19:31:28 +08:00
{
Code = input.A.Code!,
2025-04-24 19:31:28 +08:00
AFN = input.AFN_FC.AFN,
Fn = input.DT.Fn,
Pn = input.DA.Pn,
2025-04-29 11:43:16 +08:00
Data = data,
2025-05-12 14:02:22 +08:00
ReceivedHexMessage = input.BaseHexMessage.HexMessageString,
2025-04-29 11:43:16 +08:00
MessageId = input.MessageId,
2025-04-30 17:25:35 +08:00
ReceivedTime = input.ReceivedTime,
DensityUnit = DensityUnit.Second,
TimeDensity = 0
2025-04-24 19:31:28 +08:00
};
result?.Invoke(dto);
2025-05-12 14:02:22 +08:00
await _dataStorage.SaveDataToIotDbAsync(dto);
return await Task.FromResult(true);
2025-04-24 19:31:28 +08:00
}
catch (Exception ex)
{
_logger.LogError(ex, $"0C_188解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage?.HexMessageString},{ex.Message}");
2025-04-24 19:31:28 +08:00
}
return await Task.FromResult(false);
2025-04-24 19:31:28 +08:00
}
2025-05-08 15:12:37 +08:00
public AnalysisBaseDto<decimal?> GenerateFinalResult(List<string> hexMessageList,string dataType)
2025-04-24 19:31:28 +08:00
{
2025-05-08 15:12:37 +08:00
AnalysisBaseDto<decimal?> dto = new AnalysisBaseDto<decimal?>
{
DeviceType = MeterTypeEnum.WaterMeter
};
2025-04-24 19:31:28 +08:00
var arr = hexMessageList.GetRange(11, 4);
var errorCodeInfo = arr.CheckErrorCode();
if (errorCodeInfo != null)
{
dto.ValidData = false;
dto.ErrorCodeMsg = errorCodeInfo.Item2;
}
else
2025-05-08 15:12:37 +08:00
{
if (decimal.TryParse($"{arr[11]}{arr[12]}{arr[13]}.{arr[14]}", out decimal value))
dto.DataValue = value;
}
2025-05-06 19:25:15 +08:00
dto.DataType = dataType;
dto.FiledDesc = "水示值";
dto.FiledName = dto.DataType.GetDataFieldByGatherDataType() ?? string.Empty;
2025-04-24 19:31:28 +08:00
return dto;
}
}
}