2025-05-12 16:09:53 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Enums;
|
2025-04-30 17:25:35 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Extensions;
|
2025-05-06 19:25:15 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Helpers;
|
2025-05-12 14:02:22 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Ammeters;
|
2025-05-13 17:49:12 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Devices;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
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-27 09:16:37 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.T37612012.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-05-12 14:02:22 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-05-06 19:25:15 +08:00
|
|
|
|
using System.Diagnostics.Metrics;
|
2025-05-12 16:48:45 +08:00
|
|
|
|
using static FreeSql.Internal.GlobalFilter;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
|
2025-04-27 09:16:37 +08:00
|
|
|
|
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0CH
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 5.12.2.4.101 F145:当月正向有功最大需量及发生时间(总、费率 1~M)
|
|
|
|
|
|
/// </summary>
|
2025-04-29 09:16:48 +08:00
|
|
|
|
public class AFN12_F145_Analysis : IAnalysisStrategy<TB3761>
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly ILogger<AFN12_F145_Analysis> _logger;
|
|
|
|
|
|
private readonly AnalysisStrategyContext _analysisStrategyContext;
|
2025-05-12 14:02:22 +08:00
|
|
|
|
private readonly DataStorage _dataStorage;
|
|
|
|
|
|
public AFN12_F145_Analysis(ILogger<AFN12_F145_Analysis> logger, AnalysisStrategyContext analysisStrategyContext, DataStorage dataStorage)
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_analysisStrategyContext = analysisStrategyContext;
|
2025-05-12 14:02:22 +08:00
|
|
|
|
_dataStorage = dataStorage;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 09:16:48 +08:00
|
|
|
|
public async Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
ArgumentNullException.ThrowIfNull(input);
|
2025-05-13 17:49:12 +08:00
|
|
|
|
ArgumentNullException.ThrowIfNull(input.A.Code);
|
2025-04-24 19:31:28 +08:00
|
|
|
|
ArgumentNullException.ThrowIfNull(input.UnitData?.HexMessageList);
|
|
|
|
|
|
List<string> datas = await AnalysisDataUnitAsync(input.UnitData.HexMessageList);
|
2025-05-12 16:09:53 +08:00
|
|
|
|
string itemType = $"{input.AFN_FC.AFN.HexToDecStr().PadLeft(2, '0')}_{input.DT.Fn}";
|
2025-05-06 19:25:15 +08:00
|
|
|
|
|
2025-05-12 16:48:45 +08:00
|
|
|
|
AnalysisBaseDto<decimal?> data = GenerateFinalResult(datas, "当月正向有功最大需量及发生时间", itemType);
|
2025-05-13 17:49:12 +08:00
|
|
|
|
// 查询设备信息
|
2025-06-03 11:58:17 +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-05-12 14:02:22 +08:00
|
|
|
|
{
|
2025-06-03 23:01:46 +08:00
|
|
|
|
data.ProjectId = deviceInfo.ProjectId;
|
2025-05-13 17:49:12 +08:00
|
|
|
|
data.DeviceId = deviceInfo.MeterId;
|
|
|
|
|
|
data.DatabaseBusiID = deviceInfo.DatabaseBusiID;
|
|
|
|
|
|
data.DeviceAddress = deviceInfo.MeterAddress;
|
|
|
|
|
|
data.FocusId = deviceInfo.FocusId;
|
2025-05-12 14:02:22 +08:00
|
|
|
|
}
|
2025-05-13 17:49:12 +08:00
|
|
|
|
|
2025-05-08 15:12:37 +08:00
|
|
|
|
UnitDataAnalysis<AnalysisBaseDto<decimal?>> unitDataAnalysis = new UnitDataAnalysis<AnalysisBaseDto<decimal?>>
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
2025-04-29 09:16:48 +08:00
|
|
|
|
Code = input.A.Code!,
|
2025-04-24 19:31:28 +08:00
|
|
|
|
AFN = input.AFN_FC.AFN,
|
|
|
|
|
|
Fn = input.DT.Fn,
|
|
|
|
|
|
Pn = input.DA.Pn,
|
2025-04-29 11:43:16 +08:00
|
|
|
|
Data = data,
|
2025-05-12 14:02:22 +08:00
|
|
|
|
ReceivedHexMessage = input.BaseHexMessage.HexMessageString,
|
2025-04-29 11:43:16 +08:00
|
|
|
|
MessageId = input.MessageId,
|
2025-04-30 17:25:35 +08:00
|
|
|
|
ReceivedTime = input.ReceivedTime,
|
|
|
|
|
|
DensityUnit = DensityUnit.Second,
|
2025-05-12 16:09:53 +08:00
|
|
|
|
TimeDensity = 0,
|
|
|
|
|
|
DataType = IOTDBDataTypeConst.Data,
|
2025-04-24 19:31:28 +08:00
|
|
|
|
};
|
2025-04-29 09:16:48 +08:00
|
|
|
|
result?.Invoke(unitDataAnalysis);
|
2025-05-12 14:02:22 +08:00
|
|
|
|
await _dataStorage.SaveDataToIotDbAsync(unitDataAnalysis);
|
2025-04-29 09:16:48 +08:00
|
|
|
|
return await Task.FromResult(true);
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(ex, $"0C_145解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}");
|
2025-04-29 09:16:48 +08:00
|
|
|
|
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
2025-04-29 09:16:48 +08:00
|
|
|
|
return await Task.FromResult(false);
|
2025-04-24 19:31:28 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 * 7, 3);
|
|
|
|
|
|
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>>(nameof(Appendix_A23), arr, (value) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
values.Add(value.ToString());
|
|
|
|
|
|
}); //正向有功总最大需量
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
arr = hexMessageList.GetRange(10 + i * 7 + 3, 4);
|
|
|
|
|
|
|
|
|
|
|
|
errorCode = arr.CheckErrorCode();
|
|
|
|
|
|
if (errorCode != null)
|
|
|
|
|
|
values.Add(errorCode.Item1);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-04-29 09:16:48 +08:00
|
|
|
|
await _analysisStrategyContext.ExecuteAsync<List<string>>(nameof(Appendix_A17), arr, (value) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
values.Add(value.ToString());
|
|
|
|
|
|
});//正向有功总最大需量发生时间
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return values;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-12 16:09:53 +08:00
|
|
|
|
public AnalysisBaseDto<decimal?> GenerateFinalResult(List<string> data, string filedDesc,string itemType)
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
2025-05-08 15:12:37 +08:00
|
|
|
|
AnalysisBaseDto<decimal?> dto = new AnalysisBaseDto<decimal?>
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceType = MeterTypeEnum.Ammeter
|
|
|
|
|
|
};
|
2025-04-24 19:31:28 +08:00
|
|
|
|
var errorCode = data[2].CheckErrorCode();
|
|
|
|
|
|
if (errorCode != null)
|
2025-05-08 15:12:37 +08:00
|
|
|
|
{
|
|
|
|
|
|
dto.ErrorCodeMsg = errorCode.Item2;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
dto.ValidData = false;
|
2025-05-08 15:12:37 +08:00
|
|
|
|
}
|
2025-04-24 19:31:28 +08:00
|
|
|
|
else
|
2025-05-08 15:12:37 +08:00
|
|
|
|
{
|
|
|
|
|
|
if(decimal.TryParse(data[2], out decimal value))
|
|
|
|
|
|
dto.DataValue = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
2025-05-06 19:25:15 +08:00
|
|
|
|
if (DateTime.TryParse(timeSpan, out DateTime readingDate))
|
2025-04-24 19:31:28 +08:00
|
|
|
|
{
|
2025-05-06 19:25:15 +08:00
|
|
|
|
dto.TimeSpan = readingDate;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
}
|
2025-05-12 16:09:53 +08:00
|
|
|
|
dto.ItemType = itemType;
|
2025-05-06 19:25:15 +08:00
|
|
|
|
dto.FiledDesc = filedDesc;
|
2025-05-12 16:09:53 +08:00
|
|
|
|
dto.FiledName = dto.ItemType.GetDataFieldByGatherDataType() ?? string.Empty;
|
2025-04-24 19:31:28 +08:00
|
|
|
|
return dto;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|