114 lines
4.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.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 JiShe.CollectBus.IotSystems.Devices;
using System.Collections.Generic;
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0DH
{
/// <summary>
/// 5.13.2.4.77 F99测量点反向有功总电能量曲线
/// </summary>
public class AFN13_F99_Analysis : IAnalysisStrategy<TB3761>
{
private readonly ILogger<AFN13_F99_Analysis> _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
private readonly DataStorage _dataStorage;
public AFN13_F99_Analysis(ILogger<AFN13_F99_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.A.Code);
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
List<string> datas = await AnalysisDataUnitAsync(input.UnitData.HexMessageList);
int density = Convert.ToInt32(Convert.ToInt32(datas[1]).GetEnumDescription(typeof(DensityEnums)));
string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
List<AnalysisBaseDto<decimal?>> list = datas.GenerateFinalResultTd_c(3, density, dataType, "反向有功总电能量曲线");
if (list.Count > 0)
{
// 查询设备信息
DeviceInfo? deviceInfo = await _dataStorage.GetDeviceInfoAsync(input.A.Code, input.DA.Pn);
if (deviceInfo != null)
{
list.ForEach(item =>
{
item.ProjectId = deviceInfo.ProjectID;
item.DeviceId = deviceInfo.MeterId;
item.DatabaseBusiID = deviceInfo.DatabaseBusiID;
item.DeviceAddress = deviceInfo.MeterAddress;
item.FocusId = deviceInfo.FocusId;
});
}
}
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 = list,
ReceivedHexMessage = input.BaseHexMessage.HexMessageString,
MessageId = input.MessageId,
TimeDensity = density,//密度-间隔分钟数,
DensityUnit = DensityUnit.Minute,
ReceivedTime = input.ReceivedTime,
DataType = IOTDBDataTypeConst.Data,
};
await _dataStorage.SaveMultipleDataToIotDbAsync<decimal?>(unitDataAnalysis);
result?.Invoke(unitDataAnalysis);
return await Task.FromResult(true);
}
catch (Exception ex)
{
_logger.LogError(ex, $"0D_99解析失败:{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.AddRange(hexMessageList.GetReadTimeTd_c(4, 7));
int.TryParse(values[values.Count - 1], out int n);
for (int i = 0; i < n; i++)
{
var arr = hexMessageList.GetRange(11 + (i * 4), 4);
var errorCode = arr.CheckErrorCode();
if (errorCode != null)
values.Add(errorCode.Item1);
else
{
await _analysisStrategyContext.ExecuteAsync<List<string>>(nameof(Appendix_A13), arr, (value) =>
{
values.Add(value.ToString());
});
}
}
return values;
}
}
}