2025-04-26 19:04:50 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Enums;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Extensions;
|
|
|
|
|
|
using JiShe.CollectBus.IotSystems.Ammeters;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Dto;
|
2025-04-25 14:35:59 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Interfaces;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.T37612012.AnalysisData;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.T37612012.AnalysisData.Appendix;
|
2025-04-25 14:35:59 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol3761;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
2025-04-26 19:04:50 +08:00
|
|
|
|
namespace JiShe.CollectBus.Protocol.T37612012.AFN_0CH
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 5.12.2.4.105 F149:上月(上一结算日)正向有功最大需量及发生时间(总、费率 1~M)
|
|
|
|
|
|
/// </summary>
|
2025-04-26 19:04:50 +08:00
|
|
|
|
public class AFN12_F149_Analysis : IAnalysisStrategy<TB3761, UnitDataAnalysis<AnalysisBaseDto<decimal>>>
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<AFN12_F149_Analysis> _logger;
|
|
|
|
|
|
private readonly AnalysisStrategyContext _analysisStrategyContext;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
private readonly DataStorage _dataStorage;
|
|
|
|
|
|
public AFN12_F149_Analysis(ILogger<AFN12_F149_Analysis> logger, AnalysisStrategyContext analysisStrategyContext, DataStorage dataStorage)
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_analysisStrategyContext = analysisStrategyContext;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
_dataStorage= dataStorage;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-26 19:04:50 +08:00
|
|
|
|
public async Task<UnitDataAnalysis<AnalysisBaseDto<decimal>>> ExecuteAsync(TB3761 input)
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(input);
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
|
|
|
|
|
|
List<string> datas = await AnalysisDataUnit(input.UnitData.HexMessageList);
|
2025-04-27 09:31:12 +08:00
|
|
|
|
string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
|
|
|
|
|
|
|
|
|
|
|
|
AnalysisBaseDto<decimal> data = GenerateFinalResult(datas, dataType,"上月(上一结算日)正向有功最大需量及发生时间");
|
2025-04-26 19:04:50 +08:00
|
|
|
|
// 查询电表信息
|
|
|
|
|
|
AmmeterInfo ammeterInfo = await _dataStorage.GetMeterInfoAsync(data.MeterType.ToString(), "15");
|
|
|
|
|
|
if (ammeterInfo != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
data.ProjectId = ammeterInfo.ProjectID;
|
|
|
|
|
|
data.MeterId = ammeterInfo.MeterId;
|
|
|
|
|
|
data.DatabaseBusiID=ammeterInfo.DatabaseBusiID;
|
|
|
|
|
|
data.MeterAddress= ammeterInfo.AmmerterAddress;
|
|
|
|
|
|
}
|
|
|
|
|
|
UnitDataAnalysis<AnalysisBaseDto<decimal>> unitDataAnalysis = new UnitDataAnalysis<AnalysisBaseDto<decimal>>
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
2025-04-26 19:04:50 +08:00
|
|
|
|
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.Month
|
2025-04-24 19:31:28 +08:00
|
|
|
|
};
|
2025-04-26 19:04:50 +08:00
|
|
|
|
await _dataStorage.SaveDataToIotDbAsync<decimal>(unitDataAnalysis);
|
2025-04-24 19:31:28 +08:00
|
|
|
|
return await Task.FromResult(unitDataAnalysis);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(ex, $"0C_149解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private async Task<List<string>> AnalysisDataUnit(List<string> hexMessageList)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> values = new List<string>();
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
var value = await _analysisStrategyContext.ExecuteAsync<List<string>, decimal>(nameof(Appendix_A23), arr);
|
|
|
|
|
|
values.Add(value.ToString());//正向有功总最大需量
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
arr = hexMessageList.GetRange(13 + i * 7, 4);
|
|
|
|
|
|
|
|
|
|
|
|
errorCode = arr.CheckErrorCode();
|
|
|
|
|
|
if (errorCode != null)
|
|
|
|
|
|
values.Add(errorCode.Item1);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var value = await _analysisStrategyContext.ExecuteAsync<List<string>, string>(nameof(Appendix_A17), arr);//正向有功总最大需量发生时间
|
|
|
|
|
|
values.Add(value);//正向有功总最大需量
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
2025-04-27 09:31:12 +08:00
|
|
|
|
public AnalysisBaseDto<decimal> GenerateFinalResult(List<string> data,string dataType, string filedDesc = "")
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
2025-04-26 19:04:50 +08:00
|
|
|
|
AnalysisBaseDto<decimal> dto = new AnalysisBaseDto<decimal>();
|
2025-04-24 19:31:28 +08:00
|
|
|
|
|
|
|
|
|
|
var errorCodeInfo = data[2].CheckErrorCode();
|
|
|
|
|
|
if (errorCodeInfo != null)
|
2025-04-26 19:04:50 +08:00
|
|
|
|
{
|
2025-04-24 19:31:28 +08:00
|
|
|
|
dto.ValidData = false;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
dto.ErrorCodeMsg= errorCodeInfo.Item2;
|
|
|
|
|
|
}
|
2025-04-24 19:31:28 +08:00
|
|
|
|
decimal.TryParse(data[2], out decimal value);
|
|
|
|
|
|
dto.DataValue = value;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
dto.MeterType= MeterTypeEnum.Ammeter;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
//TODO:最大需量发生时间
|
|
|
|
|
|
errorCodeInfo = data[3].CheckErrorCode();
|
|
|
|
|
|
if (data[3].Length != 8 && errorCodeInfo != null)
|
2025-04-27 09:31:12 +08:00
|
|
|
|
{
|
2025-04-24 19:31:28 +08:00
|
|
|
|
dto.ValidData = false;
|
2025-04-27 09:31:12 +08:00
|
|
|
|
dto.ErrorCodeMsg = errorCodeInfo.Item2;
|
|
|
|
|
|
}
|
2025-04-24 19:31:28 +08:00
|
|
|
|
else
|
2025-04-26 19:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
|
|
//TODO:时间标
|
|
|
|
|
|
if (!DateTime.TryParse(timeSpan, out DateTime dataTime))
|
|
|
|
|
|
dto.ValidData = false;
|
|
|
|
|
|
dto.DataTime = dataTime;
|
|
|
|
|
|
}
|
2025-04-24 19:31:28 +08:00
|
|
|
|
if (DateTime.Now.Month.Equals(1))//如果为1月份,则日期减去1年
|
|
|
|
|
|
{
|
2025-04-26 19:04:50 +08:00
|
|
|
|
dto.DataTime = dto.DataTime.AddYears(-1);
|
|
|
|
|
|
dto.TimeSpan= dto.DataTime;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
2025-04-26 19:04:50 +08:00
|
|
|
|
|
2025-04-27 09:31:12 +08:00
|
|
|
|
dto.FiledDesc = "上月(上一结算日)正向有功最大需量及发生时间";
|
|
|
|
|
|
dto.FiledName = dataType.GetDataFieldByGatherDataType() ?? string.Empty;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
return dto;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|