using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto;
using JiShe.CollectBus.Protocol.Contracts;
using JiShe.CollectBus.Protocol.Dto;
using JiShe.CollectBus.Protocol.Interfaces;
using JiShe.CollectBus.Protocol3761;
using Microsoft.Extensions.Logging;
using JiShe.CollectBus.Protocol.T37612012.Appendix;
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.IotSystems.Ammeters;
using JiShe.CollectBus.Common.Helpers;
using JiShe.CollectBus.Common.Consts;
using static FreeSql.Internal.GlobalFilter;
using JiShe.CollectBus.IotSystems.Devices;
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0DH
{
///
/// 5.13.2.4.150 F195:月冻结反向有功最大需量及发生时间(总、费率 1~M)
///
public class AFN13_F195_Analysis : IAnalysisStrategy
{
private readonly ILogger _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
private readonly DataStorage _dataStorage;
public AFN13_F195_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);
List datas = await AnalysisDataUnitAsync(input.UnitData.HexMessageList);
string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
AnalysisBaseDto data = GenerateFinalResult(datas, "月冻结反向有功最大需量及发生时间", dataType);
// 查询设备信息
DeviceCacheInfo? 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> unitDataAnalysis = new UnitDataAnalysis>
{
Code = input.A.Code!,
AFN = input.AFN_FC.AFN,
Fn = input.DT.Fn,
Pn = input.DA.Pn,
MSA = input.A.A3!.D1_D7!,
PSEQ = input.SEQ.PSEQ,
Data = data,
ReceivedHexMessage = input.BaseHexMessage.HexMessageString,
MessageId = input.MessageId,
TimeDensity = 1,//密度-间隔,
DensityUnit = DensityUnit.Month,
ReceivedTime = input.ReceivedTime,
DataType = IOTDBDataTypeConst.Data,
};
await _dataStorage.SaveDataToIotDbAsync(unitDataAnalysis);
result?.Invoke(unitDataAnalysis);
return await Task.FromResult(true);
}
catch (Exception ex)
{
_logger.LogError(ex, $"0D_195解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}");
return await Task.FromResult(false);
}
}
private async Task> AnalysisDataUnitAsync(List hexMessageList)
{
List values = new List
{
hexMessageList.GetReadTime(4, 2),
hexMessageList.GetReadTime(6, 5)
};
int ratingCount = hexMessageList.GetRatingCount(11, 1);
values.Add(ratingCount.ToString());
int handlerNum = 12;
values.AddRange(await GetDataAsync(hexMessageList.GetRange(handlerNum, 3), 3,nameof(Appendix_A23)));//正向有功总最大需量
handlerNum += 3;
values.AddRange(await GetDataAsync(hexMessageList.GetRange(handlerNum, 4), 4, nameof(Appendix_A17)));//正向有功总最大需量发生时间
return values;
}
private async Task> GetDataAsync(List data, int len, string appendixName)
{
List values = new List();
var errorCode = data.CheckErrorCode();
if (errorCode != null)
values.Add(errorCode.Item1);
else
{
await _analysisStrategyContext.ExecuteAsync>(appendixName, data, (value) =>
{
values.Add(value.ToString());
});
}
return values;
}
public AnalysisBaseDto GenerateFinalResult(List data, string filedDesc,string dataType)
{
AnalysisBaseDto meter = new AnalysisBaseDto
{
DeviceType = MeterTypeEnum.Ammeter
};
var errorCode = data[3].CheckErrorCode();
if (errorCode != null)
{
meter.ValidData = false;
meter.ErrorCodeMsg = errorCode.Item2;
}
else
{
if(decimal.TryParse(data[3], out decimal value))
{
meter.DataValue = value;
}
}
string timeSpan = $"{data[0].Substring(0, 4)}-{data[4].Substring(0, 2)}-{data[4].Substring(2, 2)} {data[4].Substring(4, 2)}:{data[4].Substring(6, 2)}:00";
if (DateTime.TryParse(timeSpan, out DateTime readingDate))
{
meter.TimeSpan = readingDate;
}
meter.ItemType = dataType;
meter.FiledDesc = filedDesc;
meter.FiledName = meter.ItemType.GetDataFieldByGatherDataType() ?? string.Empty;
return meter;
}
}
}