using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Common.Extensions;
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 static System.Runtime.InteropServices.JavaScript.JSType;
using System.Diagnostics.Metrics;
using JiShe.CollectBus.Protocol.T37612012.AnalysisData;
using JiShe.CollectBus.IotSystems.Ammeters;
using JiShe.CollectBus.Common.Consts;
using static FreeSql.Internal.GlobalFilter;
namespace JiShe.CollectBus.Protocol.T37612012.AFN_0CH
{
///
/// 5.12.2.4.1 F2:终端日历时钟
///
public class AFN12_F2_Analysis : IAnalysisStrategy
{
private readonly ILogger _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
private readonly DataStorage _dataStorage;
public AFN12_F2_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);
string itemType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
var data = await GenerateFinalResultAsync(input.UnitData.HexMessageList, itemType);
// 查询电表信息
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;
data.FocusId = ammeterInfo.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.Param,
};
result?.Invoke(dto);
await _dataStorage.SaveDataToIotDbAsync(dto);
return await Task.FromResult(true);
}
catch (Exception ex)
{
_logger.LogError(ex, $"0C_2解析失败:{input.A?.Code}-{input.DT?.Fn ?? 0}-{input?.BaseHexMessage?.HexMessageString},{ex.Message}");
}
return await Task.FromResult(false);
}
public async Task> GenerateFinalResultAsync(List hexMessageList,string itemType)
{
AnalysisBaseDto dto = new AnalysisBaseDto();
var arr = hexMessageList.GetRange(4, 6);
var errorCodeInfo = arr.CheckErrorCode();
if (errorCodeInfo != null)
{
dto.ValidData = false;
dto.ErrorCodeMsg = errorCodeInfo.Item2;
}
else
{
await _analysisStrategyContext.ExecuteAsync>(nameof(Appendix_A1), arr, (value) =>
{
var data = (Tuple)value;
dto.DataValue = $"{data.Item1} {data.Item2}";
});
}
dto.ItemType = itemType;
dto.FiledName = "TerminalTime";
dto.FiledDesc = "召读终端时间";
return await Task.FromResult(dto);
}
}
}