using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.Enums; using JiShe.CollectBus.Common.Extensions; 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; using System; using System.Collections.Generic; namespace JiShe.CollectBus.Protocol.T37612012.AnalysisData.AFN_0EH { /// /// 请求重要事件 /// Item1=停电事件 /// Item2=上电事件 /// public class AFN14_F1_Analysis : IAnalysisStrategy { private readonly ILogger _logger; private readonly DataStorage _dataStorage; public AFN14_F1_Analysis(ILogger logger, DataStorage dataStorage) { _logger = logger; _dataStorage= dataStorage; } public async Task ExecuteAsync(TB3761 input, Action? result = null) { try { 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) { // TODO:非终端停/上电事件直接确认 _logger.LogWarning($"0E_1解析ERC{erc}非终端停/上电事件:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString}"); return await Task.FromResult(true); } List> 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; item.FocusId = ammeterInfo.FocusId; }); } } UnitDataAnalysis>> dto = new UnitDataAnalysis>> { Code = input.A.Code!, AFN = input.AFN_FC.AFN, Fn = input.DT.Fn, Pn = input.DA.Pn, Data = data, ReceivedHexMessage = input.BaseHexMessage.HexMessageString, MessageId = input.MessageId, ReceivedTime = input.ReceivedTime, DensityUnit = DensityUnit.None, TimeDensity = -1, DataType = IOTDBDataTypeConst.Event, }; // meterData.DataType = "0E_1"; result?.Invoke(dto); await _dataStorage.SaveMultipleStatusToIotDbAsync(dto); return await Task.FromResult(true); } catch (Exception ex) { _logger.LogError(ex, $"0E_1解析失败:{input.A.Code}-{input.DT.Fn}-{input.BaseHexMessage.HexMessageString},{ex.Message}"); } return await Task.FromResult(false); } private List> AnalysisDataUnit(List hexMessageList) { /// Item1=停电事件 /// Item2=上电事件 List > values = new List> { new AnalysisBaseDto { FiledDesc = "停电事件", FiledName = "Type", DataValue = "PowerDownEvent", TimeSpan=HandlerTime(hexMessageList.GetRange(10, 5)) }, new AnalysisBaseDto { FiledDesc = "上电事件", FiledName = "Type", DataValue = "PowerOnEvent", TimeSpan=HandlerTime(hexMessageList.GetRange(15, 5)) } }; return values; } /// /// /解析时间 /// /// /// private DateTime? HandlerTime(List times) { try { times.Reverse(); 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 { } return null; } } }