135 lines
6.0 KiB
C#
135 lines
6.0 KiB
C#
using JiShe.CollectBus.Common.Consts;
|
||
using JiShe.CollectBus.Common.Enums;
|
||
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_0CH
|
||
{
|
||
/// <summary>
|
||
/// 5.12.2.4.86 F130:当前正向无功(组合无功 1)电能示值(总、费率 1~M)
|
||
/// </summary>
|
||
public class AFN12_F130_Analysis : IAnalysisStrategy<TB3761>
|
||
{
|
||
|
||
private readonly ILogger<AFN12_F130_Analysis> _logger;
|
||
private readonly AnalysisStrategyContext _analysisStrategyContext;
|
||
private readonly DataStorage _dataStorage;
|
||
public AFN12_F130_Analysis(ILogger<AFN12_F130_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);
|
||
List<AnalysisBaseDto<decimal?>> list = GenerateFinalResult(2, datas, "正向无功电能示值", input.AFN_FC.AFN, input.DT.Fn);
|
||
if (list.Count > 0)
|
||
{
|
||
// 查询电表信息
|
||
AmmeterInfo ammeterInfo = await _dataStorage.GetMeterInfoAsync(list[0].DeviceType.ToString(), "15");
|
||
if (ammeterInfo != null)
|
||
{
|
||
list.ForEach(item =>
|
||
{
|
||
item.ProjectId = ammeterInfo.ProjectID;
|
||
item.DeviceId = ammeterInfo.MeterId;
|
||
item.DatabaseBusiID = ammeterInfo.DatabaseBusiID;
|
||
item.DeviceAddress = ammeterInfo.AmmerterAddress;
|
||
});
|
||
}
|
||
}
|
||
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,
|
||
Data = list,
|
||
ReceivedHexMessage = input.BaseHexMessage.HexMessageString,
|
||
MessageId = input.MessageId,
|
||
ReceivedTime = input.ReceivedTime,
|
||
DensityUnit = DensityUnit.Second,
|
||
TimeDensity = 0,
|
||
DataType = IOTDBDataTypeConst.Data,
|
||
};
|
||
result?.Invoke(unitDataAnalysis);
|
||
await _dataStorage.SaveMultipleDataToIotDbAsync(unitDataAnalysis);
|
||
return await Task.FromResult(true);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, $"0C_130解析失败:{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, 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 * 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());
|
||
}); //从第10个开始,每加5个字节为下一个值的开始
|
||
}
|
||
}
|
||
return values;
|
||
}
|
||
public List<AnalysisBaseDto<decimal?>> GenerateFinalResult(int index, List<string> data, string filedDesc = "", int afn = 0, int fn = 0)
|
||
{
|
||
List<AnalysisBaseDto<decimal?>> list = new List<AnalysisBaseDto<decimal?>>();
|
||
for (int i = index; i < data.Count; i++)
|
||
{
|
||
AnalysisBaseDto<decimal?> meter = new AnalysisBaseDto<decimal?>
|
||
{
|
||
DeviceType = MeterTypeEnum.Ammeter
|
||
};
|
||
var errorCode = data[i].CheckErrorCode();
|
||
if (errorCode != null)
|
||
{
|
||
meter.ErrorCodeMsg = errorCode.Item2;
|
||
meter.ValidData = false;
|
||
}
|
||
else
|
||
{
|
||
if(decimal.TryParse(data[i], out decimal value))
|
||
meter.DataValue = value;
|
||
}
|
||
meter.ItemType = i - index==0 ? $"{afn.ToString().PadLeft(2, '0')}_{fn}": $"{afn.ToString().PadLeft(2, '0')}_{fn}_{i - index}";
|
||
string timeSpan = $"{data[0].Substring(0, 4)}-{data[0].Substring(4, 2)}-{data[0].Substring(6, 2)} {data[0].Substring(8, 2)}:{data[0].Substring(10, 2)}:00";
|
||
if(DateTime.TryParse(timeSpan, out DateTime readingDate))
|
||
meter.TimeSpan = readingDate;
|
||
meter.FiledDesc = filedDesc;
|
||
meter.FiledName = meter.ItemType.GetDataFieldByGatherDataType() ?? string.Empty;
|
||
list.Add(meter);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
|
||
}
|
||
}
|