114 lines
5.1 KiB
C#
Raw Normal View History

using JiShe.CollectBus.Common.Consts;
using JiShe.CollectBus.Common.Enums;
2025-04-27 10:20:22 +08:00
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.IotSystems.Ammeters;
2025-05-13 17:49:12 +08:00
using JiShe.CollectBus.IotSystems.Devices;
using JiShe.CollectBus.Protocol.Contracts.Protocol.Dto;
using JiShe.CollectBus.Protocol.Dto;
using JiShe.CollectBus.Protocol.Interfaces;
2025-04-27 10:20:22 +08:00
using JiShe.CollectBus.Protocol.T37612012.Appendix;
using JiShe.CollectBus.Protocol3761;
using Microsoft.Extensions.Logging;
2025-05-13 17:49:12 +08:00
using System.Collections.Generic;
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0DH
{
/// <summary>
/// 5.13.2.4.122 F167日冻结三象限无功电能示值总、费率 1M
/// </summary>
public class AFN13_F167_Analysis : IAnalysisStrategy<TB3761>
{
private readonly ILogger<AFN13_F167_Analysis> _logger;
private readonly AnalysisStrategyContext _analysisStrategyContext;
private readonly DataStorage _dataStorage;
public AFN13_F167_Analysis(ILogger<AFN13_F167_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);
2025-05-13 17:49:12 +08:00
ArgumentNullException.ThrowIfNull(input.A.Code);
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
List<string> datas = await AnalysisDataUnitAsync(input.UnitData.HexMessageList);
string timeSpan = $"{datas[0].Substring(0, 4)}-{datas[0].Substring(4, 2)}-{datas[0].Substring(6, 2)} 00:00:00";
string dataType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
2025-05-13 17:49:12 +08:00
List<AnalysisBaseDto<decimal?>> list = datas.GenerateFinalResultTd_d(3, dataType, timeSpan, "日冻结三象限无功电能示值").IsValidData(new List<string>() { "0D_167", "0D_167_1", "0D_167_2", "0D_167_3", "0D_167_4" });
if (list.Count > 0)
2025-04-27 09:31:12 +08:00
{
2025-05-13 17:49:12 +08:00
// 查询设备信息
DeviceCacheInfo? deviceInfo = await _dataStorage.GetDeviceInfoAsync(input.A.Code, input.DA.Pn);
2025-05-13 17:49:12 +08:00
if (deviceInfo != null)
2025-04-27 09:31:12 +08:00
{
2025-05-13 17:49:12 +08:00
list.ForEach(item =>
2025-04-27 09:31:12 +08:00
{
2025-05-13 17:49:12 +08:00
item.ProjectId = deviceInfo.ProjectID;
item.DeviceId = deviceInfo.MeterId;
item.DatabaseBusiID = deviceInfo.DatabaseBusiID;
item.DeviceAddress = deviceInfo.MeterAddress;
item.FocusId = deviceInfo.FocusId;
2025-04-27 09:31:12 +08:00
});
}
}
2025-05-08 15:12:37 +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,
2025-05-13 17:49:12 +08:00
Data = list,
2025-05-12 14:02:22 +08:00
ReceivedHexMessage = input.BaseHexMessage.HexMessageString,
MessageId = input.MessageId,
TimeDensity = 1,//密度-间隔,
2025-04-29 11:43:16 +08:00
DensityUnit = DensityUnit.Day,
ReceivedTime = input.ReceivedTime,
DataType = IOTDBDataTypeConst.Data,
};
2025-05-08 15:12:37 +08:00
await _dataStorage.SaveMultipleDataToIotDbAsync<decimal?>(unitDataAnalysis);
result?.Invoke(unitDataAnalysis);
return await Task.FromResult(true);
}
catch (Exception ex)
{
_logger.LogError(ex, $"0D_167解析失败:{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>
{
hexMessageList.GetReadTime(4, 3),
hexMessageList.GetReadTime(7, 5)
};
int ratingCount = hexMessageList.GetRatingCount(12, 1);
values.Add(ratingCount.ToString());
for (int i = 0; i < ratingCount + 1; i++)
{
var arr = hexMessageList.GetRange(13 + (i * 4), 4);
var errorCode = arr.CheckErrorCode();
if (errorCode != null)
values.Add(errorCode.Item1);
else
{
await _analysisStrategyContext.ExecuteAsync<List<string>>(nameof(Appendix_A11), arr, (value) =>
{
values.Add(value.ToString());
});
}
}
return values;
}
}
}