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.Protocol3761;
using Microsoft.Extensions.Logging;
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_02H
{
///
/// 5.3.3.3 F3:心跳
///
public class AFN2_F3_Analysis : IAnalysisStrategy
{
private readonly ILogger _logger;
private readonly DataStorage _dataStorage;
public AFN2_F3_Analysis(ILogger logger, DataStorage dataStorage)
{
_logger = logger;
_dataStorage = dataStorage;
}
public async Task ExecuteAsync(TB3761 input, Action? result = null)
{
try
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(input.A.Code);
var data = new AnalysisBaseDto()
{
FiledDesc = "心跳",
FiledName = "Type",
DataValue = "Heartbeat",
ItemType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}"
};
// 查询设备信息
DeviceInfo? deviceInfo = await _dataStorage.GetDeviceInfoAsync(input.A.Code);
if (deviceInfo != null)
{
data.ProjectId = deviceInfo.ProjectID;
data.DeviceId = deviceInfo.FocusId;
data.DatabaseBusiID = deviceInfo.DatabaseBusiID;
data.DeviceAddress = deviceInfo.FocusAddress;
data.DeviceType = MeterTypeEnum.Focus;
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.None,
TimeDensity = -1,
DataType = IOTDBDataTypeConst.Status
};
result?.Invoke(dto);
await _dataStorage.SaveStatusToIotDbAsync(dto);
return await Task.FromResult(true);
}
catch (Exception ex)
{
_logger.LogError(ex, $"02_3解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}");
}
return await Task.FromResult(false);
}
}
}