2025-04-26 19:04:50 +08:00
|
|
|
|
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 System.Data;
|
2025-04-27 10:20:22 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.T37612012.Appendix;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Extensions;
|
|
|
|
|
|
using Volo.Abp.Domain.Entities;
|
2025-04-27 09:31:12 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Ammeters;
|
2025-04-27 17:23:26 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Helpers;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0DH
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 5.13.2.4.3 F3:日冻结正向有/无功最大需量及发生时间(总、费率 1~M,1≤M≤12)
|
|
|
|
|
|
/// </summary>
|
2025-04-29 09:16:48 +08:00
|
|
|
|
public class AFN13_F3_Analysis : IAnalysisStrategy<TB3761>
|
2025-04-26 19:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<AFN13_F3_Analysis> _logger;
|
|
|
|
|
|
private readonly AnalysisStrategyContext _analysisStrategyContext;
|
|
|
|
|
|
private readonly DataStorage _dataStorage;
|
|
|
|
|
|
public AFN13_F3_Analysis(ILogger<AFN13_F3_Analysis> logger, AnalysisStrategyContext analysisStrategyContext, DataStorage dataStorage)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_analysisStrategyContext = analysisStrategyContext;
|
|
|
|
|
|
_dataStorage = dataStorage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 09:16:48 +08:00
|
|
|
|
public async Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
|
2025-04-26 19:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(input);
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
|
|
|
|
|
|
|
|
|
|
|
|
List<string> datas = await AnalysisDataUnitAsync(input.UnitData.HexMessageList);
|
|
|
|
|
|
string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
|
|
|
|
|
|
//冻结数据时标
|
|
|
|
|
|
var time = $"{datas[0].Substring(0, 4)}-{datas[0].Substring(4, 2)}-{datas[0].Substring(6, 2)} 00:00:00";
|
|
|
|
|
|
|
|
|
|
|
|
List<AnalysisBaseDto<decimal>> data = GenerateFinalResult(datas,dataType, "日冻结正向有/无功最大需量及发生时间");
|
2025-04-27 09:31:12 +08:00
|
|
|
|
if (data.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 查询电表信息
|
|
|
|
|
|
AmmeterInfo ammeterInfo = await _dataStorage.GetMeterInfoAsync(data[0].MeterType.ToString(), "15");
|
|
|
|
|
|
if (ammeterInfo != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
data.ForEach(item =>
|
|
|
|
|
|
{
|
|
|
|
|
|
item.ProjectId = ammeterInfo.ProjectID;
|
|
|
|
|
|
item.MeterId = ammeterInfo.MeterId;
|
|
|
|
|
|
item.DatabaseBusiID = ammeterInfo.DatabaseBusiID;
|
|
|
|
|
|
item.MeterAddress = ammeterInfo.AmmerterAddress;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-26 19:04:50 +08:00
|
|
|
|
UnitDataAnalysis<List<AnalysisBaseDto<decimal>>> unitDataAnalysis = new UnitDataAnalysis<List<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
|
|
|
|
|
|
};
|
2025-04-27 09:31:12 +08:00
|
|
|
|
await _dataStorage.SaveMultipleDataToIotDbAsync<decimal>(unitDataAnalysis);
|
2025-04-29 09:16:48 +08:00
|
|
|
|
result?.Invoke(unitDataAnalysis);
|
|
|
|
|
|
return await Task.FromResult(true);
|
2025-04-26 19:04:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(ex, $"0D_3解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}");
|
2025-04-29 09:16:48 +08:00
|
|
|
|
return await Task.FromResult(false);
|
2025-04-26 19:04:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private async Task<List<string>> AnalysisDataUnitAsync(List<string> hexMessageList)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> values = new List<string>
|
|
|
|
|
|
{
|
|
|
|
|
|
hexMessageList.GetReadTime(4, 3),//日冻结类数据时标 Td_d
|
|
|
|
|
|
hexMessageList.GetReadTime(7, 5)//终端抄表时间
|
|
|
|
|
|
};
|
|
|
|
|
|
int ratingCount = hexMessageList.GetRatingCount(12, 1);//费率数 M(1≤M≤12)
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
2025-04-29 09:16:48 +08:00
|
|
|
|
await _analysisStrategyContext.ExecuteAsync<List<string>>(appendixName, arr, (value) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
values.Add(value.ToString());
|
|
|
|
|
|
});
|
2025-04-26 19:04:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-27 09:31:12 +08:00
|
|
|
|
public List<AnalysisBaseDto<decimal>> GenerateFinalResult(List<string> data,string dataType, string filedDesc = "")
|
2025-04-26 19:04:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
List<AnalysisBaseDto<decimal>> list = new List<AnalysisBaseDto<decimal>>();
|
|
|
|
|
|
int fCount = Convert.ToInt32(data[2]) + 1;
|
|
|
|
|
|
for (int i = 1; i <= 2; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
AnalysisBaseDto<decimal> meter = new AnalysisBaseDto<decimal>
|
|
|
|
|
|
{
|
|
|
|
|
|
MeterType = MeterTypeEnum.Ammeter
|
|
|
|
|
|
};
|
|
|
|
|
|
int startIndex = i == 1 ? 3 : (3 + i * fCount);
|
|
|
|
|
|
|
|
|
|
|
|
var errorCode = data[startIndex].CheckErrorCode();
|
|
|
|
|
|
if (errorCode != null)
|
|
|
|
|
|
meter.ValidData = false;
|
|
|
|
|
|
decimal.TryParse(data[startIndex], out decimal value);//decimal.TryParse(data[startIndex], out value);
|
|
|
|
|
|
meter.DataValue = value;
|
|
|
|
|
|
|
|
|
|
|
|
//TODO:日冻结类数据时标Td 20210824
|
|
|
|
|
|
var dataTime = string.Empty;
|
|
|
|
|
|
errorCode = data[0].CheckErrorCode();
|
|
|
|
|
|
if (data[0].Length == 8 && errorCode is null)
|
|
|
|
|
|
dataTime = $"{data[0].Substring(0, 4)}-{data[0].Substring(4, 2)}-{data[0].Substring(6, 2)} 00:00:00";
|
|
|
|
|
|
if (!DateTime.TryParse(dataTime, out DateTime readingDate))
|
|
|
|
|
|
meter.ValidData = false;
|
|
|
|
|
|
meter.TimeSpan = readingDate;//
|
|
|
|
|
|
|
|
|
|
|
|
//TODO:最大需量发生时间
|
|
|
|
|
|
errorCode = data[startIndex + fCount].CheckErrorCode();
|
|
|
|
|
|
if (errorCode != null && data[startIndex + fCount].Length != 8)
|
|
|
|
|
|
meter.ValidData = false;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
string timeSpan = $"{data[0].Substring(0, 4)}-{data[startIndex + fCount].Substring(0, 2)}-{data[startIndex + fCount].Substring(2, 2)} {data[startIndex + fCount].Substring(4, 2)}:{data[startIndex + fCount].Substring(6, 2)}:00";
|
|
|
|
|
|
if (!DateTime.TryParse(timeSpan, out DateTime tsField))
|
|
|
|
|
|
meter.ValidData = false;
|
|
|
|
|
|
else
|
|
|
|
|
|
meter.TimeSpan=tsField;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
meter.DataType = i == 1 ? dataType : $"{dataType}_{ i - 1}";
|
2025-04-27 09:31:12 +08:00
|
|
|
|
filedDesc = i == 1 ? filedDesc : filedDesc.Replace("有功", "无功");
|
|
|
|
|
|
meter.FiledDesc = filedDesc;
|
|
|
|
|
|
meter.FiledName = meter.DataType.GetDataFieldByGatherDataType() ?? string.Empty;
|
2025-04-26 19:04:50 +08:00
|
|
|
|
list.Add(meter);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
|
throw new Exception("错误数据");
|
|
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|