using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.Common.Helpers;
using JiShe.CollectBus.IotSystems.Ammeters;
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 System.Collections.Generic;
using System.Diagnostics.Metrics;
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0CH
{
///
/// 5.12.2.4.101 F145:当月正向有功最大需量及发生时间(总、费率 1~M)
///
public class AFN12_F145_Analysis : IAnalysisStrategy
{
private readonly ILogger _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
private readonly DataStorage _dataStorage;
public AFN12_F145_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);
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);
// 查询电表信息
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;
}
UnitDataAnalysis> unitDataAnalysis = 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
};
result?.Invoke(unitDataAnalysis);
await _dataStorage.SaveDataToIotDbAsync(unitDataAnalysis);
return await Task.FromResult(true);
}
catch (Exception ex)
{
_logger.LogError(ex, $"0C_145解析失败:{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();
values.Add(hexMessageList.GetReadTime(4, 5));
int ratingCount = hexMessageList.GetRatingCount(9, 1);
values.Add(ratingCount.ToString());
for (int i = 0; i < ratingCount + 1; i++)
{
var arr = hexMessageList.GetRange(10 + i * 7, 3);
var errorCode = arr.CheckErrorCode();
if (errorCode != null)
values.Add(errorCode.Item1);
else
{
await _analysisStrategyContext.ExecuteAsync>(nameof(Appendix_A23), arr, (value) =>
{
values.Add(value.ToString());
}); //正向有功总最大需量
}
arr = hexMessageList.GetRange(10 + i * 7 + 3, 4);
errorCode = arr.CheckErrorCode();
if (errorCode != null)
values.Add(errorCode.Item1);
else
{
await _analysisStrategyContext.ExecuteAsync>(nameof(Appendix_A17), arr, (value) =>
{
values.Add(value.ToString());
});//正向有功总最大需量发生时间
}
}
return values;
}
public AnalysisBaseDto GenerateFinalResult(List data, string filedDesc,string dataType)
{
AnalysisBaseDto dto = new AnalysisBaseDto
{
DeviceType = MeterTypeEnum.Ammeter
};
var errorCode = data[2].CheckErrorCode();
if (errorCode != null)
{
dto.ErrorCodeMsg = errorCode.Item2;
dto.ValidData = false;
}
else
{
if(decimal.TryParse(data[2], out decimal value))
dto.DataValue = value;
}
string timeSpan = $"{DateTime.Now.Year}-{data[3].Substring(0, 2)}-{data[3].Substring(2, 2)} {data[3].Substring(4, 2)}:{data[3].Substring(6, 2)}:00";
if (DateTime.TryParse(timeSpan, out DateTime readingDate))
{
dto.TimeSpan = readingDate;
}
dto.DataType = dataType;
dto.FiledDesc = filedDesc;
dto.FiledName = dto.DataType.GetDataFieldByGatherDataType() ?? string.Empty;
return dto;
}
}
}