131 lines
5.2 KiB
C#
Raw Normal View History

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-08 09:25:41 +08:00
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.Protocol3761;
using Microsoft.Extensions.Logging;
2025-05-08 09:25:41 +08:00
using System;
using System.Collections.Generic;
namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0EH
{
/// <summary>
/// 请求重要事件
/// Item1=停电事件
/// Item2=上电事件
/// </summary>
public class AFN14_F1_Analysis : IAnalysisStrategy<TB3761>
{
private readonly ILogger<AFN14_F1_Analysis> _logger;
2025-05-08 09:25:41 +08:00
private readonly DataStorage _dataStorage;
public AFN14_F1_Analysis(ILogger<AFN14_F1_Analysis> logger, DataStorage dataStorage)
{
_logger = logger;
2025-05-08 09:25:41 +08:00
_dataStorage= dataStorage;
}
2025-05-08 09:25:41 +08:00
public async Task<bool> ExecuteAsync(TB3761 input, Action<dynamic>? result = null)
{
try
{
2025-04-29 11:43:16 +08:00
ArgumentNullException.ThrowIfNull(nameof(input));
ArgumentNullException.ThrowIfNull(nameof(input.UnitData.HexMessageList));
int erc = input.UnitData.HexMessageList![8].HexToDec();
bool isOnOffPower = erc.Equals(14) ? true : false;
if (!isOnOffPower)
{
2025-05-08 09:25:41 +08:00
// TODO非终端停/上电事件直接确认
_logger.LogWarning($"0E_1解析ERC{erc}非终端停/上电事件:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString}");
return await Task.FromResult(true);
}
List<AnalysisBaseDto<string>> data = AnalysisDataUnit(input.UnitData.HexMessageList);
if (data.Count > 0)
{
// 查询电表信息
AmmeterInfo ammeterInfo = await _dataStorage.GetMeterInfoAsync(data[0].DeviceType.ToString(), "15");
if (ammeterInfo != null)
{
data.ForEach(item =>
{
item.ProjectId = ammeterInfo.ProjectID;
item.DeviceId = ammeterInfo.FocusId;
item.DatabaseBusiID = ammeterInfo.DatabaseBusiID;
item.DeviceAddress = ammeterInfo.Address;
item.DeviceType = MeterTypeEnum.Focus;
});
}
}
2025-05-08 09:25:41 +08:00
UnitDataAnalysis<List<AnalysisBaseDto<string>>> dto = new UnitDataAnalysis<List<AnalysisBaseDto<string>>>
{
2025-04-29 11:43:16 +08:00
Code = input.A.Code!,
AFN = input.AFN_FC.AFN,
Fn = input.DT.Fn,
Pn = input.DA.Pn,
2025-05-08 09:25:41 +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.None,
TimeDensity = -1,
DataType = IOTDBDataTypeConst.Event,
};
// meterData.DataType = "0E_1";
result?.Invoke(dto);
2025-05-08 09:25:41 +08:00
await _dataStorage.SaveMultipleStatusToIotDbAsync<string>(dto);
return await Task.FromResult(true);
}
catch (Exception ex)
{
2025-04-29 11:43:16 +08:00
_logger.LogError(ex, $"0E_1解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}");
}
2025-05-08 09:25:41 +08:00
return await Task.FromResult(false);
}
2025-05-08 09:25:41 +08:00
private List<AnalysisBaseDto<string>> AnalysisDataUnit(List<string> hexMessageList)
{
/// Item1=停电事件
/// Item2=上电事件
2025-05-08 09:25:41 +08:00
List <AnalysisBaseDto<string>> values = new List<AnalysisBaseDto<string>>
{
new AnalysisBaseDto<string>
{
FiledDesc = "停电事件",
FiledName = "Type",
DataValue = "PowerDownEvent",
TimeSpan=HandlerTime(hexMessageList.GetRange(10, 5))
},
new AnalysisBaseDto<string>
{
FiledDesc = "上电事件",
FiledName = "Type",
DataValue = "PowerOnEvent",
TimeSpan=HandlerTime(hexMessageList.GetRange(15, 5))
}
};
return values;
}
/// <summary>
/// /解析时间
/// </summary>
/// <param name="times"></param>
/// <returns></returns>
2025-05-08 09:25:41 +08:00
private DateTime? HandlerTime(List<string> times)
{
try
{
times.Reverse();
2025-05-08 09:25:41 +08:00
string time = $"{DateTime.Now.ToString("yyyy").Substring(0, 2)}{times[0]}-{times[1]}-{times[2]} {times[3]}:{times[4]}:00";
DateTime.TryParse(time, out DateTime dateTime);
return dateTime;
}
catch { }
2025-05-08 09:25:41 +08:00
return null;
}
}
}