146 lines
6.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0DH
{
/// <summary>
/// 5.13.2.4.145 F190抄表日冻结正向无功最大需量及发生时间总、费率 1M
/// </summary>
public class AFN13_F190_Analysis : IAnalysisStrategy<TB3761>
{
private readonly ILogger<AFN13_F190_Analysis> _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
private readonly DataStorage _dataStorage;
public AFN13_F190_Analysis(ILogger<AFN13_F190_Analysis> logger, AnalysisStrategyContext analysisStrategyContext, DataStorage dataStorage)
{
_logger = logger;
_analysisStrategyContext = analysisStrategyContext;
_dataStorage = dataStorage;
}
public async Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
{
try
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
List<string> datas = await AnalysisDataUnitAsync(input.UnitData.HexMessageList);
AnalysisBaseDto<decimal> data = GenerateFinalResult(datas, "抄表日冻结正向无功最大需量及发生时间");
data.DataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
// 查询电表信息
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.MeterAddress = ammeterInfo.AmmerterAddress;
}
UnitDataAnalysis<AnalysisBaseDto<decimal>> unitDataAnalysis = new UnitDataAnalysis<AnalysisBaseDto<decimal>>
{
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,
HexMessage = input.BaseHexMessage.HexMessageString,
MessageId = input.MessageId,
TimeDensity = 1,//密度-间隔,
DensityUnit = DensityUnit.Day,
ReceivedTime = input.ReceivedTime
};
await _dataStorage.SaveDataToIotDbAsync<decimal>(unitDataAnalysis);
result?.Invoke(unitDataAnalysis);
return await Task.FromResult(true);
}
catch (Exception ex)
{
_logger.LogError(ex, $"0D_190解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}");
return await Task.FromResult(false);
}
}
private async Task<List<string>> AnalysisDataUnitAsync(List<string> hexMessageList)
{
List<string> values = new List<string>();
values.Add(hexMessageList.GetReadTime(4, 3));
values.Add(hexMessageList.GetReadTime(7, 5));
int ratingCount = hexMessageList.GetRatingCount(12, 1);
values.Add(ratingCount.ToString());
int handlerNum = 13;
values.AddRange(await GetDataAsync<decimal>(hexMessageList.GetRange(handlerNum, 3 * (ratingCount + 1)), ratingCount, 3, nameof(Appendix_A23)));//正向有功总最大需量
handlerNum += 3 * (ratingCount + 1);//12
values.AddRange(await GetDataAsync<string>(hexMessageList.GetRange(handlerNum, 4 * (ratingCount + 1)), ratingCount, 4, nameof(Appendix_A17)));//正向有功总最大需量发生时间
handlerNum += 4 * (ratingCount + 1);//28
values.AddRange(await GetDataAsync<decimal>(hexMessageList.GetRange(handlerNum, 3 * (ratingCount + 1)), ratingCount, 3, nameof(Appendix_A23)));//正向无功总最大需量
handlerNum += 3 * (ratingCount + 1);//48
values.AddRange(await GetDataAsync<string>(hexMessageList.GetRange(handlerNum, 4 * (ratingCount + 1)), ratingCount, 4, nameof(Appendix_A17)));//正向无功总最大需量发生时间
handlerNum += 4 * (ratingCount + 1);
return values;
}
private async Task<List<string>> GetDataAsync<T>(List<string> data, int ratingCount, int len, string appendixName)
{
List<string> values = new List<string>();
for (int i = 0; i < ratingCount + 1; i++)
{
var arr = data.GetRange(0 + (i * len), len);
var errorCode = arr.CheckErrorCode();
if (errorCode != null)
values.Add(errorCode.Item1);
else
{
await _analysisStrategyContext.ExecuteAsync<List<string>>(appendixName, arr, (value) =>
{
values.Add(value.ToString());
});
}
}
return values;
}
public AnalysisBaseDto<decimal> GenerateFinalResult(List<string> data, string filedDesc = "")
{
List<AnalysisBaseDto<decimal>> list = new List<AnalysisBaseDto<decimal>>();
AnalysisBaseDto<decimal> meter = new AnalysisBaseDto<decimal>
{
DeviceType = MeterTypeEnum.Ammeter
};
decimal value = 0;
var errorCode = data[3].CheckErrorCode();
if (errorCode != null)
{
meter.ValidData = false;
meter.ErrorCodeMsg = errorCode.Item2;
}
else
decimal.TryParse(data[3], out value);
meter.DataValue = value;
string timeSpan = $"{DateTime.Now.Year}-{data[4].Substring(0, 2)}-{data[4].Substring(2, 2)} {data[4].Substring(4, 2)}:{data[4].Substring(6, 2)}:00";//$"{data[0].Substring(0, 4)}-{data[0].Substring(4, 2)}-{data[0].Substring(6, 2)}";
DateTime readingDate = DateTime.Now;
if (DateTime.TryParse(timeSpan, out readingDate))
{
meter.TimeSpan = readingDate;
}
meter.FiledDesc = filedDesc;
meter.FiledName = meter.DataType.GetDataFieldByGatherDataType() ?? string.Empty;
return meter;
}
}
}