88 lines
3.4 KiB
C#

using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.Common.Helpers;
using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto;
using JiShe.CollectBus.Protocol.Dto;
using JiShe.CollectBus.Protocol.Interfaces;
using JiShe.CollectBus.Protocol3761;
using Microsoft.Extensions.Logging;
using YamlDotNet.Core.Tokens;
namespace JiShe.CollectBus.Protocol.T37612012.AFN_0CH
{
/// <summary>
/// 水表抄读取
/// </summary>
public class AFN12_F188_Analysis : IAnalysisStrategy<TB3761>
{
private readonly ILogger<AFN12_F188_Analysis> _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
public AFN12_F188_Analysis(ILogger<AFN12_F188_Analysis> logger, AnalysisStrategyContext analysisStrategyContext)
{
_logger = logger;
_analysisStrategyContext = analysisStrategyContext;
}
public async Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
{
try
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
ArgumentNullException.ThrowIfNull(input.AFN_FC.AFN);
ArgumentNullException.ThrowIfNull(input.DT.Fn);
string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
AnalysisBaseDto<decimal?> data = GenerateFinalResult(input.UnitData.HexMessageList, dataType);
UnitDataAnalysis<AnalysisBaseDto<decimal?>> dto = new UnitDataAnalysis<AnalysisBaseDto<decimal?>>
{
Code = input.A.Code!,
AFN = input.AFN_FC.AFN,
Fn = input.DT.Fn,
Pn = input.DA.Pn,
Data = data,
HexMessage = input.BaseHexMessage.HexMessageString,
MessageId = input.MessageId,
ReceivedTime = input.ReceivedTime,
DensityUnit = DensityUnit.Second,
TimeDensity = 0
};
result?.Invoke(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<decimal?> GenerateFinalResult(List<string> hexMessageList,string dataType)
{
AnalysisBaseDto<decimal?> dto = new AnalysisBaseDto<decimal?>
{
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.DataType = dataType;
dto.FiledDesc = "水示值";
dto.FiledName = dto.DataType.GetDataFieldByGatherDataType() ?? string.Empty;
return dto;
}
}
}