diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Abstracts/BaseProtocolPlugin_bak.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Abstracts/BaseProtocolPlugin_bak.cs deleted file mode 100644 index 923f9ba..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Abstracts/BaseProtocolPlugin_bak.cs +++ /dev/null @@ -1,1177 +0,0 @@ -using FreeRedis; -using JiShe.CollectBus.Common.Enums; -using JiShe.CollectBus.Common.Extensions; -using JiShe.CollectBus.Common.Models; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; -using Microsoft.Extensions.Logging; -using JiShe.CollectBus.Protocol.Contracts.Models; -using Volo.Abp.Domain.Repositories; -using JiShe.CollectBus.Common.BuildSendDatas; -using JiShe.CollectBus.Protocol.Contracts.AnalysisData; -using Microsoft.Extensions.DependencyInjection; -using JiShe.CollectBus.IotSystems.MessageReceiveds; -using JiShe.CollectBus.IotSystems.Protocols; -using JiShe.CollectBus.Kafka.Producer; -using JiShe.CollectBus.Common.Consts; -using JiShe.CollectBus.FreeRedis; - -namespace JiShe.CollectBus.Protocol.Contracts.Abstracts -{ - public abstract class BaseProtocolPlugin_bak //: IProtocolPlugin - { - private readonly IProducerService _producerService; - private readonly ILogger _logger; - private readonly IRepository _protocolInfoRepository; - private readonly IFreeRedisProvider _redisProvider; - - //头部字节长度 - public const int hearderLen = 6; - - public const int tPLen = 6; - - public const string errorData = "EE"; - - /// - /// Initializes a new instance of the class. - /// - /// The service provider. - protected BaseProtocolPlugin_bak(IServiceProvider serviceProvider) - { - - _logger = serviceProvider.GetRequiredService>(); - _protocolInfoRepository = serviceProvider.GetRequiredService>(); - _producerService = serviceProvider.GetRequiredService(); - _redisProvider = serviceProvider.GetRequiredService(); - } - - public abstract ProtocolInfo Info { get; } - - public virtual async Task GetAsync() => await Task.FromResult(Info); - - public virtual async Task LoadAsync() - { - if (Info == null) - { - throw new ArgumentNullException(nameof(Info)); - } - - await _protocolInfoRepository.DeleteDirectAsync(a => a.Name == Info.Name); - await _protocolInfoRepository.InsertAsync(Info); - await _redisProvider.Instance.HDelAsync($"{RedisConst.ProtocolKey}", Info.Name); - await _redisProvider.Instance.HSetAsync($"{RedisConst.ProtocolKey}", Info.Name, Info); - } - - public abstract Task AnalyzeAsync(MessageReceived messageReceived, Action? sendAction = null) where T : TB3761; - - /// - /// 登录帧解析 - /// - /// 报文 - /// - public virtual async Task LoginAsync(MessageReceivedLogin messageReceived) - { - var hexStringList = messageReceived.MessageHexString.StringToPairs(); - var aTuple = (Tuple)hexStringList.GetAnalyzeValue(CommandChunkEnum.A); - var seq = (Seq)hexStringList.GetAnalyzeValue(CommandChunkEnum.SEQ); - var reqParam = new ReqParameter2 - { - AFN = AFN.确认或否认, - FunCode = (int)CFromStationFunCode.链路数据, - PRM = PRM.从动站报文, - A = aTuple.Item1, - Seq = new Seq() - { - TpV = TpV.附加信息域中无时间标签, - FIRFIN = FIRFIN.单帧, - CON = CON.需要对该帧进行确认, - PRSEQ = seq.PRSEQ - }, - MSA = aTuple.Item2, - Pn = 0, - Fn = 1 - }; - var bytes = Build3761SendData.BuildSendCommandBytes(reqParam); - //await _producerBus.PublishAsync(ProtocolConst.SubscriberLoginIssuedEventName, new IssuedEventMessage { ClientId = messageReceived.ClientId, DeviceNo = messageReceived.DeviceNo, Message = bytes, Type = IssuedEventType.Login, MessageId = messageReceived.MessageId }); - - await _producerService.ProduceAsync(ProtocolConst.SubscriberLoginIssuedEventName, new IssuedEventMessage { ClientId = messageReceived.ClientId, DeviceNo = messageReceived.DeviceNo, Message = bytes, Type = IssuedEventType.Login, MessageId = messageReceived.MessageId }); - //await _producerBus.Publish(new IssuedEventMessage { ClientId = messageReceived.ClientId, DeviceNo = messageReceived.DeviceNo, Message = bytes, Type = IssuedEventType.Login, MessageId = messageReceived.MessageId }); - } - - /// - /// 心跳帧解析 - /// - /// 报文 - /// - public virtual async Task HeartbeatAsync(MessageReceivedHeartbeat messageReceived) - { - var hexStringList = messageReceived.MessageHexString.StringToPairs(); - var aTuple = (Tuple)hexStringList.GetAnalyzeValue(CommandChunkEnum.A); - var seq = (Seq)hexStringList.GetAnalyzeValue(CommandChunkEnum.SEQ); - if (seq.TpV == TpV.附加信息域中带时间标签) - { - //解析 - - } - if (seq.CON == CON.需要对该帧进行确认) - { - var reqParam = new ReqParameter2() - { - AFN = AFN.确认或否认, - FunCode = (int)CFromStationFunCode.链路数据, - PRM = PRM.从动站报文, - A = aTuple.Item1, - Seq = new Seq() - { - TpV = TpV.附加信息域中无时间标签, - FIRFIN = FIRFIN.单帧, - CON = CON.不需要对该帧进行确认, - PRSEQ = seq.PRSEQ, - }, - MSA = aTuple.Item2, - Pn = 0, - Fn = 1 - }; - var bytes = Build3761SendData.BuildSendCommandBytes(reqParam); - //await _producerBus.PublishAsync(ProtocolConst.SubscriberHeartbeatIssuedEventName, new IssuedEventMessage { ClientId = messageReceived.ClientId, DeviceNo = messageReceived.DeviceNo, Message = bytes, Type = IssuedEventType.Heartbeat, MessageId = messageReceived.MessageId }); - - await _producerService.ProduceAsync(ProtocolConst.SubscriberHeartbeatIssuedEventName, new IssuedEventMessage { ClientId = messageReceived.ClientId, DeviceNo = messageReceived.DeviceNo, Message = bytes, Type = IssuedEventType.Heartbeat, MessageId = messageReceived.MessageId }); - - //await _producerBus.Publish(new IssuedEventMessage { ClientId = messageReceived.ClientId, DeviceNo = messageReceived.DeviceNo, Message = bytes, Type = IssuedEventType.Heartbeat, MessageId = messageReceived.MessageId }); - } - } - - /// - /// 解析确认或否认数据 - /// - /// - /// - /// - public virtual async Task AnalyzeAnswerDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexStringList = messageReceived.MessageHexString.StringToPairs(); - var fn = hexStringList.GetAnalyzeValue(CommandChunkEnum.FN); - //1:全部确认 - //2:全部否认 - //3:按数据单元表示确认和否认 - //4 硬件安全认证错误应答 - } - - /// - /// 解析电表档案读取数据 - /// - /// - /// - /// - public virtual List AnalyzeAmmeterParameterReadingDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexData = GetHexData(messageReceived.MessageHexString); - - var meterList = new List(); - var count = (hexData[1] + hexData[0]).HexToDec(); - //if (2 + count * 27 != hexDatas.Count - pWLen - tPLen - 2) - // return; - var index = 2;//数量 - for (int i = 1; i <= count; i++) - { - var meterNumber = $"{hexData[index + 1]}{hexData[index]}".HexToDec(); - index += 2; - - var pn = $"{hexData[index + 1]}{hexData[index]}".HexToDec(); - index += 2; - - var baudRateAndPortBin = hexData[index].HexToBin().PadLeft(8, '0'); - var baudRate = baudRateAndPortBin.Substring(0, 3).BinToDec(); - var port = baudRateAndPortBin.Substring(3, 5).BinToDec(); - index += 1; - - var protocolType = (CommunicationProtocolType)hexData[index].HexToDec(); - index += 1; - - var addressHexList = hexData.Skip(index).Take(6).ToList(); - addressHexList.Reverse(); - var address = string.Join("", addressHexList); - index += 6; - - var pwdHexList = hexData.Skip(index).Take(6).ToList(); - pwdHexList.Reverse(); - var password = string.Join("", pwdHexList.Take(3).ToList()); - index += 6; - - var rateNumberBin = hexData[index].HexToBin().PadLeft(8, '0'); - var rateNumber = rateNumberBin.Substring(4).BinToDec(); - index += 1; - - var intBitAndDecBitNumberBin = hexData[index].HexToBin().PadLeft(8, '0'); - var intBitNumber = intBitAndDecBitNumberBin.Substring(4, 2).BinToDec() + 4; - var decBitNumber = intBitAndDecBitNumberBin.Substring(6, 2).BinToDec() + 1; - index += 1; - - // hexDatas.GetRange() - var collectorAddressHexList = hexData.Skip(index).Take(6).ToList(); - collectorAddressHexList.Reverse(); - var collectorAddress = string.Join("", collectorAddressHexList); - index += 6; - - var userClassNumberBin = hexData[index].HexToBin().PadLeft(8, '0'); - var userClass = userClassNumberBin.Substring(0, 4).BinToDec(); - var userSubClass = userClassNumberBin.Substring(4, 4).BinToDec(); - index += 1; - - meterList.Add(new AmmeterParameter() - { - Pn = pn, - BaudRate = baudRate, - Port = port, - ProtocolType = (int)protocolType, - Address = address, - Password = password, - RateNumber = rateNumber, - IntegerBitNumber = intBitNumber, - DecimalBitNumber = decBitNumber, - CollectorAddress = collectorAddress, - UserCategoryNumber = userClass, - UserSubclassNumber = userSubClass, - }); - } - - return meterList; - } - - /// - /// 解析当前正向有功电能示值抄读数据 - /// - /// 报文 - /// 发送委托 - /// - public virtual CurrentPositiveActiveEnergyAnalyze AnalyzeActivePowerIndicationReadingDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexData = GetHexData(messageReceived.MessageHexString); - - var minute = Convert.ToInt32(hexData[0]); // 获取当前分钟数 - var hour = Convert.ToInt32(hexData[1]); // 获取当前小时数 - var day = Convert.ToInt32(hexData[2]); // 获取当前日期的日数 - var month = Convert.ToInt32(hexData[3]); // 获取当前月份 - var year = Convert.ToInt32(hexData[4]); // 获取当前日期的年份 - var dateTime = new DateTime(year, month, day, hour, minute, 0); - // 转换为本地时间 - var localDateTime = dateTime.ToLocalTime(); - - var rateNumber = Convert.ToInt32(hexData[5]); - var kwhTotal = hexData.Skip(5).Take(5).ToList(); - var kwhList = new List(); - var index = 11; - for (int i = 0; i < rateNumber; i++) - { - var kwhHexList = hexData.Skip(index).Take(5).ToList(); - kwhHexList.Reverse(); - var integerStr = $"{kwhHexList.Take(0)}{kwhHexList.Take(1)}{kwhHexList.Take(2)}"; - var decimalValStr = $"{kwhHexList[3]}{kwhHexList[4]}"; - var val = decimal.Parse($"{integerStr}{decimalValStr}"); - kwhList.Add(new PositiveActiveEnergyItem() - { - Name = $"费率{i + 1}正向有功总电能示值", - Value = val - }); - index += 5; - } - - return new CurrentPositiveActiveEnergyAnalyze() - { - ReadingTime = localDateTime, - RateNumber = rateNumber, - Items = kwhList - }; - } - - /// - /// 解析日冻结正向有功电能示值抄读数据 - /// - /// - /// - /// - public virtual void AnalyzeDailyFrozenReadingDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexData = GetHexData(messageReceived.MessageHexString); - //附录A.20 日月年 - var td_dHex = hexData.Take(3).ToList(); - //附录A.15 分时日月年 - var readingTimeHex = hexData.Skip(3).Take(5).ToList(); - var rateNumberHex = hexData.Skip(8).Take(1).FirstOrDefault().HexToDec(); - - var datas = new List(); - //附录A.14 kWh 5字节 - for (int i = 0; i < rateNumberHex; i++) - { - var skipCount = 9 + i * 5; - var dataHexs = hexData.Skip(skipCount).Take(5).ToList(); - var data = AnalyzeDataAccordingToA14(dataHexs[0], dataHexs[1], dataHexs[2], dataHexs[3], dataHexs[4]); - datas.Add(data); - } - - } - - //接收<2024/11/7 17:34:42>: 68 3E 01 3E 01 68 - //控制域 88 - //地址域 20 32 90 26 1A - //AFN 0C - //Seq 62 - //pn 01 01 - //fn 01 03 - - //F0 16 - - //报文解析:计量点1数据 - //++++++++ F25:当前三相及总有/无功功率功率因数 三相电压电流 零序电流 ++++++++ - //终端抄表时间:2024年11月07日17时34分 34 17 07 11 24 - //当前总有功功率:-0.0028 kW 28 00 80 - //当前A相有功功率:0 kW 00 00 80 - //当前B相有功功率:0 kW 00 00 80 - //当前C相有功功率:-0.0027 kW 27 00 80 - //当前总无功功率:-0.0047 kW 47 00 80 - //当前A相无功功率:0 kW 00 00 00 - //当前B相无功功率:0 kW 00 00 00 - //当前C相无功功率:-0.0047 kW 47 00 80 - //当前总功率因数:-51.8 % 18 85 - //当前A相功率因数:0 % 00 80 - //当前B相功率因数:0 % 00 80 - //当前C相功率因数:-50 % 00 85 - //当前A相电压:0.1 V 01 00 - //当前B相电压:0.2 V 02 00 - //当前C相电压:239 V 90 23 - //当前A相电流:0 A 00 00 80 - //当前B相电流:0 A 00 00 80 - //当前C相电流:-0.024 A 24 00 80 - //当前零序电流:Error: 数据不符合BCD码格式 A EE EE EE - //当前总视在功率:Error: 数据不符合BCD码格式 kVA EE EE EE - //当前A相视在功率:Error: 数据不符合BCD码格式 kVA EE EE EE - //当前B相视在功率:Error: 数据不符合BCD码格式 kVA EE EE EE - //当前C相视在功率:Error: 数据不符合BCD码格式 kVA EE EE EE - //++++++++++++++++++++++++++++++++++++ - /// - /// 当前三相及总有/无功功率、功率因数、三相电压、电流、零序电流、视在功率 - /// - /// - /// - /// //F25ReadingAnalyze - public virtual Analyze3761Data AnalyzeF25ReadingDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexData = GetHexData(messageReceived.MessageHexString); - //A.15 分时日月年 - var readingTimeHex = hexData.Take(5).ToList(); - var readingTime = AnalyzeDataAccordingToA15(readingTimeHex[0], readingTimeHex[1], readingTimeHex[2], readingTimeHex[3], readingTimeHex[4]); - - //A.9 kW - var crntTotalActivePowerHexs = hexData.Skip((int)F25DataItemEnum.CrntTotalActivePower).Take(3).ToList(); - var crntTotalActivePower = AnalyzeDataAccordingToA09(crntTotalActivePowerHexs[0], crntTotalActivePowerHexs[1], crntTotalActivePowerHexs[2]); - - var crntActivePowerOfAHexs = hexData.Skip((int)F25DataItemEnum.CrntActivePowerOfA).Take(3).ToList(); - var crntActivePowerOfA = AnalyzeDataAccordingToA09(crntActivePowerOfAHexs[0], crntActivePowerOfAHexs[1], crntActivePowerOfAHexs[2]); - - var crntActivePowerOfBHexs = hexData.Skip((int)F25DataItemEnum.CrntActivePowerOfB).Take(3).ToList(); - var crntActivePowerOfB = AnalyzeDataAccordingToA09(crntActivePowerOfBHexs[0], crntActivePowerOfBHexs[1], crntActivePowerOfBHexs[2]); - - var crntActivePowerOfCHexs = hexData.Skip((int)F25DataItemEnum.CrntActivePowerOfC).Take(3).ToList(); - var crntActivePowerOfC = AnalyzeDataAccordingToA09(crntActivePowerOfCHexs[0], crntActivePowerOfCHexs[1], crntActivePowerOfCHexs[2]); - - var crntTotalReactivePowerHexs = hexData.Skip((int)F25DataItemEnum.CrntTotalReactivePower).Take(3).ToList(); - var crntTotalReactivePower = AnalyzeDataAccordingToA09(crntTotalReactivePowerHexs[0], crntTotalReactivePowerHexs[1], crntTotalReactivePowerHexs[2]); - - var crntReactivePowerOfAHexs = hexData.Skip((int)F25DataItemEnum.CrntReactivePowerOfA).Take(3).ToList(); - var crntReactivePowerOfA = AnalyzeDataAccordingToA09(crntReactivePowerOfAHexs[0], crntReactivePowerOfAHexs[1], crntReactivePowerOfAHexs[2]); - - var crntReactivePowerOfBHexs = hexData.Skip((int)F25DataItemEnum.CrntReactivePowerOfB).Take(3).ToList(); - var crntReactivePowerOfB = AnalyzeDataAccordingToA09(crntReactivePowerOfBHexs[0], crntReactivePowerOfBHexs[1], crntReactivePowerOfBHexs[2]); - - var crntReactivePowerOfCHexs = hexData.Skip((int)F25DataItemEnum.CrntReactivePowerOfC).Take(2).ToList(); - var crntReactivePowerOfC = AnalyzeDataAccordingToA09(crntReactivePowerOfCHexs[0], crntReactivePowerOfCHexs[1], crntReactivePowerOfCHexs[2]); - - //A.5 % - var crntTotalPowerFactorHexs = hexData.Skip((int)F25DataItemEnum.CrntTotalPowerFactor).Take(2).ToList(); - var crntTotalPowerFactor = AnalyzeDataAccordingToA05(crntTotalPowerFactorHexs[0], crntTotalPowerFactorHexs[1]); - - var crntPowerFactorOfAHexs = hexData.Skip((int)F25DataItemEnum.CrntPowerFactorOfA).Take(2).ToList(); - var crntPowerFactorOfA = AnalyzeDataAccordingToA05(crntPowerFactorOfAHexs[0], crntPowerFactorOfAHexs[1]); - - var crntPowerFactorOfBHexs = hexData.Skip((int)F25DataItemEnum.CrntPowerFactorOfB).Take(2).ToList(); - var crntPowerFactorOfB = AnalyzeDataAccordingToA05(crntPowerFactorOfBHexs[0], crntPowerFactorOfBHexs[1]); - - var crntPowerFactorOfCHexs = hexData.Skip((int)F25DataItemEnum.CrntPowerFactorOfC).Take(2).ToList(); - var crntPowerFactorOfC = AnalyzeDataAccordingToA05(crntPowerFactorOfCHexs[0], crntPowerFactorOfCHexs[1]); - - //A.7 V - var crntVoltageOfAHexs = hexData.Skip((int)F25DataItemEnum.CrntVoltageOfA).Take(2).ToList(); - var crntVoltageOfA = AnalyzeDataAccordingToA07(crntVoltageOfAHexs[0], crntVoltageOfAHexs[1]); - - var crntVoltageOfBHexs = hexData.Skip((int)F25DataItemEnum.CrntVoltageOfB).Take(2).ToList(); - var crntVoltageOfB = AnalyzeDataAccordingToA07(crntVoltageOfBHexs[0], crntVoltageOfBHexs[1]); - - var crntVoltageOfCHexs = hexData.Skip((int)F25DataItemEnum.CrntVoltageOfC).Take(2).ToList(); - var crntVoltageOfC = AnalyzeDataAccordingToA07(crntVoltageOfCHexs[0], crntVoltageOfCHexs[1]); - - //A.25 A - var crntCurrentOfAHexs = hexData.Skip((int)F25DataItemEnum.CrntCurrentOfA).Take(3).ToList(); - var crntCurrentOfA = AnalyzeDataAccordingToA25(crntCurrentOfAHexs[0], crntCurrentOfAHexs[1], crntCurrentOfAHexs[2]); - - var crntCurrentOfBHexs = hexData.Skip((int)F25DataItemEnum.CrntCurrentOfB).Take(3).ToList(); - var crntCurrentOfB = AnalyzeDataAccordingToA25(crntCurrentOfBHexs[0], crntCurrentOfBHexs[1], crntCurrentOfBHexs[2]); - - var crntCurrentOfCHexs = hexData.Skip((int)F25DataItemEnum.CrntCurrentOfC).Take(3).ToList(); - var crntCurrentOfC = AnalyzeDataAccordingToA25(crntCurrentOfCHexs[0], crntCurrentOfCHexs[1], crntCurrentOfCHexs[2]); - - var crntZeroSequenceCurrentHexs = hexData.Skip((int)F25DataItemEnum.CrntZeroSequenceCurrent).Take(3).ToList(); - var crntZeroSequenceCurrent = AnalyzeDataAccordingToA25(crntZeroSequenceCurrentHexs[0], crntZeroSequenceCurrentHexs[1], crntZeroSequenceCurrentHexs[2]); - - //A.9 kVA - var crntTotalApparentPowerHexs = hexData.Skip((int)F25DataItemEnum.CrntTotalApparentPower).Take(3).ToList(); - var crntTotalApparentPower = AnalyzeDataAccordingToA09(crntTotalApparentPowerHexs[0], crntTotalApparentPowerHexs[1], crntTotalApparentPowerHexs[2]); - - var crntApparentPowerOfAHexs = hexData.Skip((int)F25DataItemEnum.CrntApparentPowerOfA).Take(3).ToList(); - var crntApparentPowerOfA = AnalyzeDataAccordingToA09(crntApparentPowerOfAHexs[0], crntApparentPowerOfAHexs[1], crntApparentPowerOfAHexs[2]); - - var crntApparentPowerOfBHexs = hexData.Skip((int)F25DataItemEnum.CrntApparentPowerOfB).Take(3).ToList(); - var crntApparentPowerOfB = AnalyzeDataAccordingToA09(crntApparentPowerOfBHexs[0], crntApparentPowerOfBHexs[1], crntApparentPowerOfBHexs[2]); - - var crntApparentPowerOfCHexs = hexData.Skip((int)F25DataItemEnum.CrntApparentPowerOfC).Take(3).ToList(); - var crntApparentPowerOfC = AnalyzeDataAccordingToA09(crntApparentPowerOfCHexs[0], crntApparentPowerOfCHexs[1], crntApparentPowerOfCHexs[2]); - - return new Analyze3761Data() - { - AFN = 12, - FN = 25, - Text = "当前三相及总有/无功功率功率因数", - DataUpChilds = new List() - { - new Analyze3761DataUpChild(1,"终端抄表时间",readingTime.ToString(),1), - new Analyze3761DataUpChild(2,"当前总有功功率",crntTotalActivePower.ToString(),2), - new Analyze3761DataUpChild(3,"当前A相有功功率",crntActivePowerOfA.ToString(),3), - new Analyze3761DataUpChild(4,"当前B相有功功率",crntActivePowerOfB.ToString(),4), - new Analyze3761DataUpChild(5,"当前C相有功功率",crntActivePowerOfC.ToString(),5), - new Analyze3761DataUpChild(6,"当前总无功功率",crntTotalReactivePower.ToString(),6), - new Analyze3761DataUpChild(7,"当前A相无功功率",crntReactivePowerOfA.ToString(),7), - new Analyze3761DataUpChild(8,"当前B相无功功率",crntReactivePowerOfB.ToString(),8), - new Analyze3761DataUpChild(9,"当前C相无功功率",crntReactivePowerOfC.ToString(),9), - new Analyze3761DataUpChild(10,"当前总功率因数",crntTotalPowerFactor.ToString(),10), - new Analyze3761DataUpChild(11,"当前A相功率因数",crntPowerFactorOfA.ToString(),11), - new Analyze3761DataUpChild(12,"当前B相功率因数",crntPowerFactorOfB.ToString(),12), - new Analyze3761DataUpChild(13,"当前C相功率因数",crntPowerFactorOfC.ToString(),13), - new Analyze3761DataUpChild(14,"当前A相电压",crntVoltageOfA.ToString(),14), - new Analyze3761DataUpChild(15,"当前B相电压",crntVoltageOfB.ToString(),15), - new Analyze3761DataUpChild(16,"当前C相电压",crntVoltageOfC.ToString(),16), - new Analyze3761DataUpChild(17,"当前A相电流",crntCurrentOfA.ToString(),17), - new Analyze3761DataUpChild(18,"当前B相电流",crntCurrentOfB.ToString(),18), - new Analyze3761DataUpChild(19,"当前C相电流",crntCurrentOfC.ToString(),19), - new Analyze3761DataUpChild(20,"当前零序电流",crntZeroSequenceCurrent.ToString(),20), - new Analyze3761DataUpChild(21,"当前总视在功率",crntTotalApparentPower.ToString(),21), - new Analyze3761DataUpChild(22,"当前A相视在功率",crntApparentPowerOfA.ToString(),22), - new Analyze3761DataUpChild(23,"当前B相视在功率",crntApparentPowerOfB.ToString(),23), - new Analyze3761DataUpChild(24,"当前C相视在功率",crntApparentPowerOfC.ToString(),24), - } - }; - - //var f25ReadingAnalyze = new F25ReadingAnalyze() - //{ - // ReadingTime = readingTime.ToLocalTime(), - // CrntTotalActivePower = crntTotalActivePower, - // CrntActivePowerOfA = crntActivePowerOfA, - // CrntActivePowerOfB = crntActivePowerOfB, - // CrntActivePowerOfC = crntActivePowerOfC, - // CrntTotalReactivePower = crntTotalReactivePower, - // CrntReactivePowerOfA = crntReactivePowerOfA, - // CrntReactivePowerOfB = crntReactivePowerOfB, - // CrntReactivePowerOfC = crntReactivePowerOfC, - // CrntTotalPowerFactor = crntTotalPowerFactor, - // CrntPowerFactorOfA = crntPowerFactorOfA, - // CrntPowerFactorOfB = crntPowerFactorOfB, - // CrntPowerFactorOfC = crntPowerFactorOfC, - // CrntVoltageOfA = crntVoltageOfA, - // CrntVoltageOfB = crntVoltageOfB, - // CrntVoltageOfC = crntVoltageOfC, - // CrntCurrentOfA = crntCurrentOfA, - // CrntCurrentOfB = crntCurrentOfB, - // CrntCurrentOfC = crntCurrentOfC, - // CrntZeroSequenceCurrent = crntZeroSequenceCurrent, - // CrntTotalApparentPower = crntTotalApparentPower, - // CrntApparentPowerOfA = crntApparentPowerOfA, - // CrntApparentPowerOfB = crntApparentPowerOfB, - // CrntApparentPowerOfC = crntApparentPowerOfC - //}; - - //return f25ReadingAnalyze; - - } - - /// - /// 解析终端版本信息抄读 - /// - /// - /// - /// //TerminalVersionInfoAnalyze - public virtual Analyze3761Data AnalyzeTerminalVersionInfoReadingDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexData = GetHexData(messageReceived.MessageHexString); - - var makerNo = string.Join("",hexData.Take(4).Select(s => (char)s.HexToDec()));//厂商代码 - var deviceNo = string.Join("", hexData.Skip((int)TerminalVersionInfoEnum.DeviceNo).Take(8).Select(s => (char)s.HexToDec()));//设备编号 - var softwareVersionNo = string.Join("", hexData.Skip((int)TerminalVersionInfoEnum.SoftwareVersionNo).Take(4).Select(s => (char)s.HexToDec()));//软件版本号 - var softwareReleaseDateList = hexData.Skip((int)TerminalVersionInfoEnum.SoftwareReleaseDate).Take(3).ToList(); - var softwareReleaseDate = $"20{AnalyzeDataAccordingToA20(softwareReleaseDateList[0], softwareReleaseDateList[1], softwareReleaseDateList[2])}";//软件发布日期 - var capacityInformationCode = string.Join("", hexData.Skip((int)TerminalVersionInfoEnum.CapacityInformationCode).Take(11).Select(s => (char)s.HexToDec()));//容量信息码 - var protocolVersionNo = string.Join("", hexData.Skip((int)TerminalVersionInfoEnum.ProtocolVersionNo).Take(4).Select(s => (char)s.HexToDec()));//通信协议编号 - var hardwareVersionNo = string.Join("", hexData.Skip((int)TerminalVersionInfoEnum.HardwareVersionNo).Take(4).Select(s => (char)s.HexToDec()));//硬件版本号 - var hardwareReleaseDateList = hexData.Skip((int)TerminalVersionInfoEnum.HardwareReleaseDate).Take(3).ToList(); - var hardwareReleaseDate = $"20{AnalyzeDataAccordingToA20(hardwareReleaseDateList[0], hardwareReleaseDateList[1], hardwareReleaseDateList[2])}";//软件发布日期 - - return new Analyze3761Data() - { - AFN = 09, - FN = 1, - Text = "终端版本信息", - DataUpChilds = new List() - { - new Analyze3761DataUpChild(1,"厂商代码",makerNo.ToString(),1), - new Analyze3761DataUpChild(2,"设备编号",deviceNo.ToString(),2), - new Analyze3761DataUpChild(3,"软件版本号",softwareVersionNo.ToString(),3), - new Analyze3761DataUpChild(4,"软件发布日期",softwareReleaseDate.ToString(),4), - new Analyze3761DataUpChild(5,"容量信息码",capacityInformationCode.ToString(),5), - new Analyze3761DataUpChild(6,"通信协议编号",protocolVersionNo.ToString(),6), - new Analyze3761DataUpChild(7,"硬件版本号",hardwareVersionNo.ToString(),7), - new Analyze3761DataUpChild(8,"软件发布日期",hardwareReleaseDate.ToString(),8), - } - }; - //return new TerminalVersionInfoAnalyze() - //{ - // MakerNo = makerNo, - // DeviceNo = deviceNo, - // SoftwareVersionNo = softwareVersionNo, - // SoftwareReleaseDate = DateTime.Parse(softwareReleaseDate).ToLocalTime(), - // CapacityInformationCode = capacityInformationCode, - // ProtocolVersionNo = protocolVersionNo, - // HardwareVersionNo = hardwareVersionNo, - // HardwareReleaseDate = DateTime.Parse(hardwareReleaseDate).ToLocalTime() - //}; - } - - /// - /// 解析相位角 - /// - /// - /// - /// - public virtual Analyze3761Data AnalyzeATypeOfDataItems49ReadingDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexData = GetHexData(messageReceived.MessageHexString); - - var uabUaList = hexData.Take(2).ToList(); - var uabUa = AnalyzeDataAccordingToA05(uabUaList[0], uabUaList[1]); //单位 度 - - var ubList = hexData.Skip((int)ATypeOfDataItems49.Ub).Take(2).ToList(); - var ub = AnalyzeDataAccordingToA05(ubList[0], ubList[1]); - - var ucbUcList = hexData.Skip((int)ATypeOfDataItems49.UcbUc).Take(2).ToList(); - var ucbUc = AnalyzeDataAccordingToA05(ucbUcList[0], ucbUcList[1]); - - var iaList = hexData.Skip((int)ATypeOfDataItems49.Ia).Take(2).ToList(); - var ia = AnalyzeDataAccordingToA05(iaList[0], iaList[1]); - - var ibList = hexData.Skip((int)ATypeOfDataItems49.Ib).Take(2).ToList(); - var ib = AnalyzeDataAccordingToA05(ibList[0], ibList[1]); - - var icList = hexData.Skip((int)ATypeOfDataItems49.Ic).Take(2).ToList(); - var ic = AnalyzeDataAccordingToA05(icList[0], icList[1]); - - return new Analyze3761Data() - { - AFN = 12, - FN = 49, - Text = "相位角", - DataUpChilds = - [ - new Analyze3761DataUpChild(1, "UabUa相位角", uabUa.ToString(), 1), - new Analyze3761DataUpChild(2, "Ub相位角", ub.ToString(), 2), - new Analyze3761DataUpChild(3, "UcbUc相位角", ucbUc.ToString(), 3), - new Analyze3761DataUpChild(4, "Ia相位角", ia.ToString(), 4), - new Analyze3761DataUpChild(5, "Ib相位角", ib.ToString(), 5), - new Analyze3761DataUpChild(6, "Ic相位角", ic.ToString(), 6) - ] - }; - } - - /// - /// 解析终端时间抄读 - /// - /// - /// - - public virtual void AnalyzeTerminalTimeReadingDataAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexDatas = GetHexData(messageReceived.MessageHexString); - var time = Appendix.Appendix_A1(hexDatas.Take(6).ToList()); - } - - ///// - ///// 通用解析 - ///// - ///// - ///// - ///// - //public virtual TB3761 AnalyzeReadingDataAsync(MessageReceived messageReceived, - // Action? sendAction = null) - //{ - // var hexStringList = messageReceived.MessageHexString.StringToPairs(); - // var afn = (AFN)hexStringList.GetAnalyzeValue(CommandChunkEnum.AFN); - // var fn = (int)hexStringList.GetAnalyzeValue(CommandChunkEnum.FN); - - // var tb3761 = QGDW3761Config.CommandList.FirstOrDefault(it => it.Afn == afn); - // if (tb3761 == null) return null; - - // var tb3761Fn = tb3761.FnList.FirstOrDefault(it => it.Fn == fn); - // if (tb3761Fn == null) return null; - - // var analyzeValue = (List)hexStringList.GetAnalyzeValue(CommandChunkEnum.Data); - - // var m = 0; - // var rateNumberUpSort = -1; - // var rateNumberUp = tb3761Fn.UpList.FirstOrDefault(it => it.Name.Contains("费率数M")); - // if (rateNumberUp != null) - // { - // var rateNumber = analyzeValue.Skip(rateNumberUp.DataIndex).Take(rateNumberUp.DataCount).FirstOrDefault(); - // m = Convert.ToInt32(rateNumber); - // rateNumberUpSort = rateNumberUp.Sort; - // } - - // foreach (var up in tb3761Fn.UpList) - // { - // var dataIndex = up.DataIndex; - // if (dataIndex == 0 && up.Sort > rateNumberUpSort) - // { - // var sum1 = tb3761Fn.UpList.Where(it => it.Sort < up.Sort) - // .Sum(it => it.DataCount); - // var sum2 = tb3761Fn.UpList.Where(it => it.Sort < up.Sort && it.Tb3761UpChildlList.Count > 0) - // .Sum(it => it.Tb3761UpChildlList.Sum(c=> m * c.DataCount)); - // dataIndex = sum1 + sum2; - // } - - // var value = AnalyzeDataAccordingDataType(analyzeValue, dataIndex, up.DataCount, up.DataType); - // if (value != null) - // { - // up.Value = value.ToString(); - // } - // if (up.Tb3761UpChildlList.Count > 0) //复费率根据费率数来解析 - // { - // var repeatCount = m; - // foreach (var upChild in up.Tb3761UpChildlList) - // { - // for (var j = 0; j < repeatCount; j++) - // { - // var val = AnalyzeDataAccordingDataType(analyzeValue, dataIndex, upChild.DataCount, upChild.DataType); - // if (val != null) - // { - // upChild.Name = string.Format(upChild.Name, j + 1); - // upChild.Value = val.ToString(); - // } - // dataIndex += upChild.DataCount; - // } - // } - - // } - // } - - // return tb3761; - //} - - ///// - ///// 通用解析 日冻结曲线类 - ///// - ///// - ///// - ///// - //public virtual TB3761 AnalyzeReadingTdcDataAsync(MessageReceived messageReceived, - // Action? sendAction = null) - //{ - - // var hexStringList = messageReceived.MessageHexString.StringToPairs(); - // var afn = (AFN)hexStringList.GetAnalyzeValue(CommandChunkEnum.AFN); - // var fn = (int)hexStringList.GetAnalyzeValue(CommandChunkEnum.FN); - - // var tb3761 = QGDW3761Config.CommandTdcList.FirstOrDefault(it => it.Afn == afn); - // if (tb3761 == null) return null; - - // var tb3761Fn = tb3761.FnList.FirstOrDefault(it => it.Fn == fn); - // if (tb3761Fn == null) return null; - - // var analyzeValue = (List)hexStringList.GetAnalyzeValue(CommandChunkEnum.Data); - - // foreach (var up in tb3761Fn.UpList) - // { - // var value = AnalyzeDataAccordingDataType(analyzeValue, up.DataIndex, up.DataCount, up.DataType); - // if (value != null) - // { - // up.Value = value.ToString(); - - // if (up.Tb3761UpChildlList.Count > 0) - // { - // var dataIndex = up.DataIndex; - // var repeatCount = (int)value; - // foreach (var upChild in up.Tb3761UpChildlList) - // { - // for (var j = 0; j < repeatCount; j++) - // { - // var val = AnalyzeDataAccordingDataType(analyzeValue, dataIndex, upChild.DataCount, upChild.DataType); - // if (val != null) - // { - // upChild.Value = val.ToString(); - // upChild.Name = string.Format(upChild.Name, j + 1); - // } - // dataIndex += upChild.DataCount; - // } - // } - // } - // } - // } - - // return tb3761; - // //var freezeDensity = (FreezeDensity)Convert.ToInt32(hexDatas.Skip(5).Take(1)); - // //var addMinute = 0; - // //switch (freezeDensity) - // //{ - // // case FreezeDensity.No:break; - // // case FreezeDensity.Min15: - // // addMinute = 15; - // // break; - // // case FreezeDensity.Min30: - // // addMinute = 30; - // // break; - // // case FreezeDensity.Min60: - // // addMinute = 60; - // // break; - // // case FreezeDensity.Min5: break; - // // addMinute = 5; - // // case FreezeDensity.Min1: - // // addMinute = 1; - // // break; - // // } - //} - - private object? AnalyzeDataAccordingDataType(List analyzeValue, int dataIndex,int dataCount,string dataType) - { - var valueList = analyzeValue.Skip(dataIndex).Take(dataCount).ToList(); - object? value = null; - switch (dataType) - { - case "BIN": - value = Convert.ToInt32(valueList[0]); - break; - case "A05": - if (valueList.Count == 2) - { - value = AnalyzeDataAccordingToA05(valueList[0], valueList[1]); - } - break; - case "A09": - if (valueList.Count == 3) - { - value = AnalyzeDataAccordingToA09(valueList[0], valueList[1], valueList[2]); - } - break; - case "A14": - if (valueList.Count == 5) - { - value = AnalyzeDataAccordingToA15(valueList[0], valueList[1], valueList[2], valueList[3], valueList[4]); - } - break; - case "A15": - if (valueList.Count == 5) - { - //var minutes = Convert.ToInt32(analyzeValue[0]); // 获取当前分钟数 - //var hours = Convert.ToInt32(analyzeValue[1]); // 获取当前小时数 - //var day = Convert.ToInt32(analyzeValue[2]); // 获取当前日期的日数 - //var month = Convert.ToInt32(analyzeValue[3]); // 获取当前月份 - //var year = Convert.ToInt32(analyzeValue[4]); // 获取当前日期的年份 - value = AnalyzeDataAccordingToA15(valueList[0], valueList[1], valueList[2], valueList[3], valueList[4]); - } - break; - case "ATd_d": - var day = valueList[0]; - var month = valueList[1]; - var year = valueList[2]; - break; - case "ATd_m": - var tdmMonth = valueList[0]; - var tdmYear = valueList[1]; - break; - } - - return value; - } - - /// - /// 解析透明转发 应答 - /// - /// - /// - /// - public virtual async Task AnalyzeTransparentForwardingAnswerAsync(MessageReceived messageReceivedEvent, Action? sendAction = null) - { - var hexDatas = GetHexData(messageReceivedEvent.MessageHexString); - - var port = hexDatas[0].HexToDec(); - - var count = hexDatas[1].HexToDec(); - - var dataList = hexDatas.Skip(2).Take(count).ToList(); - - //645 - if (dataList[0].IsStartStr() && dataList[hearderLen - 1].IsStartStr()) - { - - } - else if (dataList[0].IsStartStr())//188 水表只有开头是68 - { - AnalyzeWaterMeterReadData(dataList); - } - } - - /// - /// 解析透明转发 应答结果 - /// - /// - /// - /// - public virtual async Task AnalyzeTransparentForwardingAnswerResultAsync(MessageReceived messageReceived, Action? sendAction = null) - { - var hexDatas = GetHexData(messageReceived.MessageHexString); - - var port = hexDatas[0].HexToDec(); - - //A.12 - var a = hexDatas.Skip(1).Take(6).ToList(); - - var result = hexDatas.Skip(7).Take(1).FirstOrDefault().HexToDec(); - var transparentForwardingFlag = (TransparentForwardingFlagEnum)result; - } - - /// - /// 获取命令数据单元-数据体 - /// - /// - /// - public static List GetHexData(string messageHexString) - { - var hexStringList = messageHexString.StringToPairs(); - var analyzeValue = (List)hexStringList.GetAnalyzeValue(CommandChunkEnum.Data); - return analyzeValue; - } - - /// - /// 解析时间标签 - /// - /// - public void AnalysisTp(List hexData) - { - var pFC = hexData[0].HexToDec();//启动帧帧序号计数器 - var seconds = Convert.ToInt32(hexData[1]); // 获取当前秒数 - var minutes = Convert.ToInt32(hexData[2]); // 获取当前分钟数 - var hours = Convert.ToInt32(hexData[3]); // 获取当前小时数 - var day = Convert.ToInt32(hexData[4]); // 获取当前日期的日数 - var delayTime = hexData[5].HexToDec();//延迟时间 min - } - - #region 报文指定的数据格式 - - /// - /// 根据A05解析数据 - /// - /// 个位和十分位 - /// 百位和十位 - /// - public decimal AnalyzeDataAccordingToA05(string singleDigitNumberAndDeciles, string hundredDigitNumbersAndTenDigitNumber) - { - if (singleDigitNumberAndDeciles.IsErrorData() && hundredDigitNumbersAndTenDigitNumber.IsErrorData()) - { - return 0; - } - var bin1 = hundredDigitNumbersAndTenDigitNumber.HexToBin().PadLeft(8, '0'); - var hundredDigitNumbers = bin1.Substring(1, 3).BinToDec();//百位 - var tenDigitNumber = bin1.Substring(4).BinToDec();//十位 - - var bin2 = singleDigitNumberAndDeciles.HexToBin().PadLeft(8, '0'); - var singleDigitNumber = bin1.Substring(0, 4).BinToDec();//个位 - var deciles = bin1.Substring(4).BinToDec();//十分位 - - var value = decimal.Parse($"{hundredDigitNumbers}{tenDigitNumber}{singleDigitNumber}.{deciles}"); - return value; - } - - /// - /// 数据格式07 - /// - /// 个位、十分位 - /// 百位、十位 - /// - public decimal AnalyzeDataAccordingToA07(string singleDigitNumberAndDeciles, string hundredDigitNumbersAndTenDigitNumber) - { - if (singleDigitNumberAndDeciles.IsErrorData() && hundredDigitNumbersAndTenDigitNumber.IsErrorData()) - { - return 0; - } - var bin1 = hundredDigitNumbersAndTenDigitNumber.HexToBin().PadLeft(8, '0'); - var hundredDigitNumbers = bin1.Substring(1, 3).BinToDec();//百位 - var tenDigitNumber = bin1.Substring(4).BinToDec();//十位 - - var bin2 = singleDigitNumberAndDeciles.HexToBin().PadLeft(8, '0'); - var singleDigitNumber = bin2.Substring(0, 4).BinToDec();//个位 - var deciles = bin2.Substring(4).BinToDec();//十分位 - - var value = decimal.Parse($"{hundredDigitNumbers}{tenDigitNumber}{singleDigitNumber}.{deciles}"); - return value; - } - - /// - /// 数据格式09 - /// - /// 千分位和万分位 - /// 十分位和百分位 - /// 十位和个位 - /// - public decimal AnalyzeDataAccordingToA09(string thousandthPercentileAndTenThousandPositions, string decilesAndPercentile, string tenAndSingleDigit) - { - if (thousandthPercentileAndTenThousandPositions.IsErrorData() && decilesAndPercentile.IsErrorData() && tenAndSingleDigit.IsErrorData()) - { - return 0; - } - - var bin3 = tenAndSingleDigit.HexToBin().PadLeft(8, '0'); - var tenDigitNumber = bin3.Substring(1, 3).BinToDec();//十位 - var singleDigitNumber = bin3.Substring(4).BinToDec();//个位 - - var value = decimal.Parse($"{tenDigitNumber}{singleDigitNumber}.{decilesAndPercentile}{thousandthPercentileAndTenThousandPositions}"); - return value; - - //var bin3 = hex3.HexToBin().PadLeft(8, '0'); - //var thousandthPercentile = bin3.Substring(0, 4).BinToDec();//千分位 - //var tenThousandPositions = bin3.Substring(4).BinToDec();//万分位 - - //var bin2 = hex2.HexToBin().PadLeft(8, '0'); - //var deciles = bin2.Substring(0, 4).BinToDec();//十分位 - //var percentile = bin2.Substring(4).BinToDec();//百分位 - - - } - - /// - /// 数据格式12 - /// - /// 十位 个位 - /// 千位 百位 - /// 十万位 万位 - /// 千万位 百万位 - /// 十亿位 亿位 - /// 千亿位 百亿位 - /// - public string AnalyzeDataAccordingToA12(string tenDigitAndSingleDigitNumber, string thousandAndHundredsOfPosition, string hundredThousandAndTenThousandOfPosition, - string millionsAndMillionOfPosition, string hundredMillionAndBillionOfPosition, string hundredBillionsAndBillionsOfPosition) - { - var value = $"{hundredBillionsAndBillionsOfPosition}{hundredMillionAndBillionOfPosition}{millionsAndMillionOfPosition}" + - $"{hundredThousandAndTenThousandOfPosition}{thousandAndHundredsOfPosition}{tenDigitAndSingleDigitNumber}"; - - return value; - } - - /// - /// 数据格式14 - /// - /// 千分,万分 - /// 十分、百分 - /// 十位、个位 - /// 千位、百位 - /// 十万位、万位 - /// - public decimal AnalyzeDataAccordingToA14(string tenThousandPositionsAndThousandthPercentile, string decilesAndPercentile, - string tenDigitNumberAndSingleDigitNumber, string thousandAndHundredsOfPosition, string hundredThousandAndTenThousandOfPosition) - { - if (tenThousandPositionsAndThousandthPercentile.IsErrorData() && decilesAndPercentile.IsErrorData() && tenDigitNumberAndSingleDigitNumber.IsErrorData() - && thousandAndHundredsOfPosition.IsErrorData() && hundredThousandAndTenThousandOfPosition.IsErrorData()) - return 0; - - var value = decimal.Parse($"{hundredThousandAndTenThousandOfPosition}{thousandAndHundredsOfPosition}" + - $"{tenDigitNumberAndSingleDigitNumber}.{decilesAndPercentile}{tenThousandPositionsAndThousandthPercentile}"); - - return value; - } - - /// - /// 数据格式A.15 - /// - /// - /// - /// - /// - /// - /// - public DateTime AnalyzeDataAccordingToA15(string minuteHex, string hourHex, string dayHex, string monthHex, string yearHex) - { - var centuryString = (DateTime.Now.Year / 100).ToString(); - var time = DateTime.Parse($"{centuryString}{yearHex}-{monthHex}-{dayHex} {hourHex}:{minuteHex}:00"); - return time; - //正常转换方式 - //var bin1 = hex1.HexToBin().PadLeft(8, '0'); - //var tenDigitNumberOfMinute = bin1.Substring(0, 4).BinToDec();//十位 - //var singleDigitNumberOfMinute = bin1.Substring(4).BinToDec();//个位 - //var minute = Convert.ToInt32($"{tenDigitNumberOfMinute}{singleDigitNumberOfMinute}"); - - //var bin2 = hex2.HexToBin().PadLeft(8, '0'); - //var tenDigitNumberOfHour = bin2.Substring(0, 4).BinToDec();//十位 - //var singleDigitNumberOfHour = bin2.Substring(4).BinToDec();//个位 - - //var bin3 = hex3.HexToBin().PadLeft(8, '0'); - //var tenDigitNumberOfDay = bin3.Substring(0, 4).BinToDec();//十位 - //var singleDigitNumberOfDay = bin3.Substring(4).BinToDec();//个位 - - //var bin4 = hex4.HexToBin().PadLeft(8, '0'); - //var tenDigitNumberOfMonth = bin4.Substring(0, 4).BinToDec();//十位 - //var singleDigitNumberOfMonth = bin4.Substring(4).BinToDec();//个位 - - //var bin5 = hex5.HexToBin().PadLeft(8, '0'); - //var tenDigitNumberOfYear = bin5.Substring(0, 4).BinToDec();//十位 - //var singleDigitNumberOfYear = bin5.Substring(4).BinToDec();//个位 - } - - - /// - /// 数据格式20 - /// - /// - /// - /// - /// - public DateTime AnalyzeDataAccordingToA20(string dayHex, string monthHex, string yearHex) - { - var centuryString = (DateTime.Now.Year / 100).ToString(); - var time = DateTime.Parse($"{centuryString}{yearHex}-{monthHex}-{dayHex}"); - return time; - - } - - /// - /// 数据格式25 - /// - /// 百分、千分位 - /// 个位、十分位 - /// 百位、十位 - /// - - public decimal AnalyzeDataAccordingToA25(string percentileAndThousandthPercentile, string singleDigitNumberAndDeciles, string hundredDigitNumbersAndTenDigitNumber) - { - if (percentileAndThousandthPercentile.IsErrorData() && singleDigitNumberAndDeciles.IsErrorData() && hundredDigitNumbersAndTenDigitNumber.IsErrorData()) - return 0; - - var bin1 = hundredDigitNumbersAndTenDigitNumber.HexToBin().PadLeft(8, '0'); - var hundredDigitNumbers = bin1.Substring(1, 3).BinToDec();//百位 - var tenDigitNumber = bin1.Substring(4).BinToDec();//十位 - - var bin2 = singleDigitNumberAndDeciles.HexToBin().PadLeft(8, '0'); - var singleDigitNumber = bin2.Substring(0, 4).BinToDec();//个位 - var deciles = bin1.Substring(4).BinToDec();//十分位 - - var value = decimal.Parse($"{hundredDigitNumbers}{tenDigitNumber}{singleDigitNumber}.{deciles}{percentileAndThousandthPercentile}"); - return value; - } - - #endregion - - #region 645 - /// - /// 解析读取表地址 - /// - /// - /// - public List AnalyzeAmmeterReadMeterAddress_10_105(List dataList) - { - var values = new List(); - //表地址 - var addressList = dataList.GetRange(1, 6); - addressList.Reverse(); - var address = string.Join("", addressList); - values.Add(address); - //控制码 - var controlCode = dataList.GetRange(8, 1)[0]; - values.Add(controlCode); - //长度 - var len = dataList.GetRange(9, 1)[0].HexToDec(); - //values.Add(len.ToString()); - //数据域 - var dataField = dataList.GetRange(10, len); - if (dataField.Count > 0) - { - string dataMark = string.Join("", dataField.GetRange(0, 4).AddHex33()); - values.Add(dataMark);//数据标识 - var readValue = dataField.GetRange(4, len - 4).ReduceHex33();//值 - readValue.Reverse(); - //var valueData = GetValue>(readValue, (AppendixEnums)Enum.Parse(typeof(AppendixEnums), dataMark)); - values.AddRange(readValue); - } - return values; - } - - - public void AnalyzeAmmeterReadMeterAddress_10_103(List dataList) - { - var data = AnalyzeAmmeterReadMeterAddress_10_105(dataList); - var validData = data[2].Equals("94") ? true : false; - var remark = validData ? "成功" : "失败"; - } - - public void AnalyzeAutoUpSwtichRead_0A_68(List dataList) - { - var isOpen = dataList[4].Equals("55"); - } - - #endregion - - #region 188 - - public void AnalyzeWaterMeter188Data(List dataList) - { - var waterMeterType = dataList[(int)WaterMeterReadEnum.Type]; - var addressList = dataList.Skip((int)WaterMeterReadEnum.Address).Take(6).ToList(); - addressList.Reverse(); - var address = string.Join("", addressList); - address = address.Substring(address.Length - 12); - var controlCode = dataList[(int)WaterMeterReadEnum.ControlCode]; - switch (controlCode) - { - case "81": - AnalyzeWaterMeterReadData(dataList); - break; - case "84": - AnalyzeWaterMeterValueResult(dataList); - break; - } - - } - - /// - /// 解析水表读数据 - /// - /// - public void AnalyzeWaterMeterReadData(List dataList) - { - var accumulativeWaterflowList = dataList.Skip((int)WaterMeterReadEnum.AccumulativeWaterflow).Take(5).ToList(); - //xxxxxx.xx - var accumulativeWaterflow = decimal.Parse( - $"{accumulativeWaterflowList[3]}{accumulativeWaterflowList[2]}{accumulativeWaterflowList[1]}." + - $"{accumulativeWaterflowList[0]}"); - } - - /// - /// 解析水表阀控结果 - /// - /// - public bool AnalyzeWaterMeterValueResult(List dataList) - { - return true; - } - - #endregion - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Abstracts/T37612012ProtocolPlugin.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Abstracts/T37612012ProtocolPlugin.cs deleted file mode 100644 index 772ab4e..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Abstracts/T37612012ProtocolPlugin.cs +++ /dev/null @@ -1,405 +0,0 @@ -using JiShe.CollectBus.Common.BuildSendDatas; -using JiShe.CollectBus.Common.Enums; -using System; -using System.Reflection; -using JiShe.CollectBus.Common.Consts; -using JiShe.CollectBus.Common.Extensions; -using JiShe.CollectBus.Common.Models; -using JiShe.CollectBus.FreeRedis; -using JiShe.CollectBus.IotSystems.Protocols; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; -using JiShe.CollectBus.Protocol.Contracts.Models; -using JiShe.CollectBus.Protocol.Contracts.SendData; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using TouchSocket.Sockets; -using Volo.Abp.Domain.Repositories; - -namespace JiShe.CollectBus.Protocol.Contracts.Abstracts -{ - public abstract class T37612012ProtocolPlugin : IProtocolPlugin - { - //头部字节长度 - public const int hearderLen = 6; - - public const int tPLen = 6; - - public const string errorData = "EE"; - - private readonly ILogger _logger; - private readonly IRepository _protocolInfoRepository; - private readonly IFreeRedisProvider _redisProvider; - - public T37612012ProtocolPlugin(IServiceProvider serviceProvider, ILogger logger) - { - _logger = logger; - _protocolInfoRepository = serviceProvider.GetRequiredService>(); - _redisProvider = serviceProvider.GetRequiredService(); - } - - - public abstract ProtocolInfo Info { get; } - - public virtual async Task GetAsync() => await Task.FromResult(Info); - - public virtual async Task LoadAsync() - { - if (Info == null) - { - throw new ArgumentNullException(nameof(Info)); - } - - await _protocolInfoRepository.DeleteDirectAsync(a => a.Name == Info.Name); - await _protocolInfoRepository.InsertAsync(Info); - await _redisProvider.Instance.HDelAsync($"{RedisConst.ProtocolKey}", Info.Name); - await _redisProvider.Instance.HSetAsync($"{RedisConst.ProtocolKey}", Info.Name, Info); - } - - public abstract Task AnalyzeAsync(ITcpSessionClient client, string messageReceived, Action? receivedAction = null) where T : class; - - - /// - /// 解析376.1帧 - /// - /// - /// - public virtual TB3761? Analysis3761(string messageReceived) - { - try - { - var hexStringList = messageReceived.StringToPairs(); - // 初步校验 - if (hexStringList.Count < 6 || hexStringList.FirstOrDefault() != "68" || hexStringList.Skip(5).Take(1).FirstOrDefault() != "68" || hexStringList.Count < 18 || hexStringList.LastOrDefault() != "16") - { - _logger.LogError($"解析Analysis3761校验不通过,报文:{messageReceived}"); - } - else - { - TB3761 tB3761 = new TB3761 - { - BaseHexMessage = new BaseHexMessage - { - HexMessageString = messageReceived, - HexMessageList = hexStringList - }, - C = Analysis_C(hexStringList), - A = Analysis_A(hexStringList), - AFN_FC = Analysis_AFN_FC(hexStringList), - SEQ = Analysis_SEQ(hexStringList), - UnitData = Analysis_UnitData(hexStringList), - DA = Analysis_DA(hexStringList), - DT = Analysis_DT(hexStringList) - }; - return tB3761; - } - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis3761错误,报文:{messageReceived},异常:{ex.Message}"); - } - return null; - } - - /// - /// 控制域C解析 - /// - /// - public virtual C Analysis_C(List hexStringList) - { - C c = new C(); - try - { - if (hexStringList.Count > 6) - { - BaseHexMessage baseHexMessage = new BaseHexMessage - { - HexMessageList = hexStringList.GetRange(6, 1) // 控制域 1字节 - }; - baseHexMessage.HexMessageString = string.Join("", baseHexMessage.HexMessageList); - if (baseHexMessage.HexMessageList.Count == 0) - return null; - string binStr = baseHexMessage.HexMessageString.HexTo4BinZero(); - c = new C - { - BaseHexMessage = baseHexMessage, - FC = binStr.Substring(binStr.Length - 4, 4).BinToDec(), - FCV = binStr.Substring(3, 1).BinToDec(), - FCB = binStr.Substring(2, 1).BinToDec(), - PRM = binStr.Substring(1, 1).BinToDec(), - DIR = binStr.Substring(0, 1).BinToDec() - }; - return c; - } - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_C错误,报文:{string.Join("", hexStringList)},异常:{ex.Message}"); - } - - return c; - } - - /// - /// 地址域A解析 - /// - /// - /// - public virtual A Analysis_A(List hexStringList) - { - A a = new A(); - try - { - if (hexStringList.Count > 7) - { - BaseHexMessage baseHexMessage = new BaseHexMessage - { - HexMessageList = hexStringList.GetRange(7, 5) // 地址域 5个字节 - }; - baseHexMessage.HexMessageString = string.Join("", baseHexMessage.HexMessageList); - a = new A - { - BaseHexMessage = baseHexMessage, - A1 = baseHexMessage.HexMessageList.ListReverseToStr(0, 2),//.DataConvert(10);//行政区划码A1 - A2 = baseHexMessage.HexMessageList.ListReverseToStr(2, 2).PadLeft(5, '0').HexToDec(),//终端地址A2 - A3 = Analysis_A3(baseHexMessage.HexMessageList) //主站地址和组地址标志A3 - }; - a.Code = $"{a.A1.PadLeft(4, '0')}{a.A2.ToString()!.PadLeft(5, '0')}"; - - } - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_A错误,报文:{string.Join("", hexStringList)},异常:{ex.Message}"); - } - - return a; - - } - - /// - /// 站地址和组地址标志A3 - /// - /// 地址域A集合 - /// - public virtual A3 Analysis_A3(List hexAList) - { - A3 a3 = new A3(); - try - { - if (hexAList.Count != 0) - { - BaseHexMessage baseHexMessage = new BaseHexMessage - { - HexMessageList = hexAList.GetRange(4, 1) // 站地址和组地址标志A3 1个字节 - }; - baseHexMessage.HexMessageString = string.Join("", baseHexMessage.HexMessageList); - var binStr = baseHexMessage.HexMessageString.HexTo4BinZero(); - a3 = new A3 - { - BaseHexMessage = baseHexMessage, - D0 = binStr.Substring(binStr.Length - 1, 1).BinToDec(), - D1_D7 = binStr.Substring(0, binStr.Length - 1).BinToDec() - }; - } - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_A3错误,报文:{string.Join("", hexAList)},异常:{ex.Message}"); - } - - return a3; - } - - /// - /// AFN_FC功能码 - /// - /// - public virtual AFN_FC Analysis_AFN_FC(List hexStringList) - { - AFN_FC aFN_FC = new AFN_FC(); - try - { - if (hexStringList.Count == 0) - { - - BaseHexMessage baseHexMessage = new BaseHexMessage - { - HexMessageList = hexStringList.GetRange(12, 1) //AFN功能码 1个字节 - }; - baseHexMessage.HexMessageString = string.Join("", baseHexMessage.HexMessageList); - - aFN_FC = new AFN_FC - { - BaseHexMessage = baseHexMessage, - AFN = baseHexMessage.HexMessageString.HexToDec(), - }; - } - - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_AFN_FC错误,报文:{string.Join("", hexStringList)},异常:{ex.Message}"); - } - - return aFN_FC; - } - - /// - /// 解析帧序列域SEQ - /// - /// - public virtual SEQ Analysis_SEQ(List hexStringList) - { - SEQ seq = new SEQ(); - try - { - if (hexStringList.Count != 0) - { - BaseHexMessage baseHexMessage = new BaseHexMessage - { - HexMessageList = hexStringList.GetRange(13, 1) //帧序列域 SEQ 1个字节 - }; - baseHexMessage.HexMessageString = string.Join("", baseHexMessage.HexMessageList); - var binStr = baseHexMessage.HexMessageString.HexTo4BinZero(); - seq = new SEQ - { - PSEQ = binStr.Substring(binStr.Length - 4, 4).BinToDec(), - CON = binStr.Substring(3, 1).BinToDec(), - FIN = binStr.Substring(2, 1).BinToDec(), - FIR = binStr.Substring(1, 1).BinToDec(), - TpV = binStr.Substring(0, 1).BinToDec() - }; - } - - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_SEQ错误,报文:{string.Join("", hexStringList)},异常:{ex.Message}"); - } - return seq; - } - - - /// - /// 数据单元标识及数据单元数据 - /// - public virtual UnitData Analysis_UnitData(List hexStringList) - { - UnitData unitData = new UnitData(); - try - { - if (hexStringList.Count != 0) - { - unitData = new UnitData - { - HexMessageList = hexStringList.GetRange(14, hexStringList.Count - 14 - 2) //总数字节数-固定长度报文头-控制域C-地址域A-校验和CS-结束字符(16H) - }; - unitData.HexMessageString = string.Join("", unitData.HexMessageList); - } - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_UnitData错误,报文:{string.Join("", hexStringList)},异常:{ex.Message}"); - } - return unitData; - } - - /// - /// 信息点DA Pn - /// - /// - public virtual DA Analysis_DA(List hexStringList) - { - DA da = new DA(); - try - { - if (hexStringList.Count != 0) - { - BaseHexMessage baseHexMessage = new BaseHexMessage - { - HexMessageList = hexStringList.GetRange(14, 2) //信息点DA Pn 2个字节 - }; - baseHexMessage.HexMessageString = string.Join("", baseHexMessage.HexMessageList); - var da1 = baseHexMessage.HexMessageList[0]; - var da2 = baseHexMessage.HexMessageList[1]; - da = new DA() - { - BaseHexMessage = baseHexMessage, - Pn = CalculatePn(da1, da2) - }; - } - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_DA错误,报文:{string.Join("", hexStringList)},异常:{ex.Message}"); - } - return da; - } - - /// - /// 信息类DT Fn - /// - /// - public virtual DT Analysis_DT(List hexStringList) - { - DT dt = new DT(); - try - { - if (hexStringList.Count != 0) - { - BaseHexMessage baseHexMessage = new BaseHexMessage - { - HexMessageList = hexStringList.GetRange(16, 2) //信息类DT Fn 2个字节 - }; - baseHexMessage.HexMessageString = string.Join("", baseHexMessage.HexMessageList); - var dt1 = baseHexMessage.HexMessageList[0]; - var dt2 = baseHexMessage.HexMessageList[1]; - dt = new DT() - { - BaseHexMessage = baseHexMessage, - Fn = CalculateFn(dt1, dt2) - }; - } - } - catch (Exception ex) - { - _logger.LogError($"解析Analysis_DT错误,报文:{string.Join("", hexStringList)},异常:{ex.Message}"); - } - - return dt; - - } - - - /// - /// 计算Pn - /// - /// - /// - /// - public static int CalculatePn(string da1, string da2) => (da2.HexToDec() - 1) * 8 + (8 - da1.HexTo4BinZero().IndexOf(da1.Equals("00") ? "0" : "1")); - - - /// - /// 计算Fn - /// - /// - /// - /// - public static int CalculateFn(string dt1, string dt2) => dt2.HexToDec() * 8 + (8 - dt1.HexTo4BinZero().IndexOf("1")); - - #region 下行命令构建 - - /// - /// 组装报文 - /// - /// - /// 设备数据实体 - /// 映射读取执行方法的Code,例如10_1,表示 10H_F1_00000,10H_F1_00001,统一英文下划线分隔 - /// - public abstract Task BuildAsync(ProtocolBuildRequest request); - - #endregion - - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Adapters/StandardFixedHeaderDataHandlingAdapter.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Adapters/StandardFixedHeaderDataHandlingAdapter.cs deleted file mode 100644 index 0984a8f..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Adapters/StandardFixedHeaderDataHandlingAdapter.cs +++ /dev/null @@ -1,27 +0,0 @@ -using JiShe.CollectBus.Protocol.Contracts.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TouchSocket.Core; - -namespace JiShe.CollectBus.Protocol.Contracts.Adapters -{ - public class StandardFixedHeaderDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter - { - /// - /// 接口实现,指示固定包头长度 - /// - public override int HeaderLength => 3; - - /// - /// 获取新实例 - /// - /// - protected override CustomFixedHeaderRequestInfo GetInstance() - { - return new CustomFixedHeaderRequestInfo(); - } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs similarity index 89% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs index d313bcd..406beb9 100644 --- a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs +++ b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs @@ -1,6 +1,6 @@ -using JiShe.CollectBus.Protocol.Contracts.Interfaces; -using JiShe.CollectBus.Protocol.Contracts.Models; -using JiShe.CollectBus.Protocol.Dto; +using JiShe.CollectBus.Protocol.Dto; +using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.CollectBus.Protocol3761; using Microsoft.Extensions.Logging; namespace JiShe.CollectBus.Protocol.AnalysisData.AFN_00H diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_00H/AFN0_F2_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_00H/AFN0_F2_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_00H/AFN0_F2_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_00H/AFN0_F2_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_09H/AFN9_F1_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_09H/AFN9_F1_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_09H/AFN9_F1_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_09H/AFN9_F1_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_09H/AFN9_F9_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_09H/AFN9_F9_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_09H/AFN9_F9_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_09H/AFN9_F9_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0AH/AFN10_F10_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0AH/AFN10_F10_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0AH/AFN10_F10_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0AH/AFN10_F10_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0AH/AFN10_F66_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0AH/AFN10_F66_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0AH/AFN10_F66_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0AH/AFN10_F66_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0AH/AFN10_F68_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0AH/AFN10_F68_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0AH/AFN10_F68_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0AH/AFN10_F68_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F129_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F129_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F129_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F129_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F130_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F130_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F130_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F130_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F131_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F131_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F131_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F131_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F132_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F132_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F132_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F132_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F145_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F145_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F145_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F145_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F149_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F149_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F149_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F149_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F188_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F188_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F188_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F188_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F25_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F25_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F25_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F25_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F2_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F2_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F2_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F2_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F33_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F33_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F33_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F33_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F49_Analysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F49_Analysis.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/AnalysisData/AFN_0CH/AFN12_F49_Analysis.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/AFN_0CH/AFN12_F49_Analysis.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/Appendix.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/Appendix.cs deleted file mode 100644 index f68268c..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisData/Appendix.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using JiShe.CollectBus.Common.Extensions; - -namespace JiShe.CollectBus.Protocol.Contracts.AnalysisData -{ - /// - /// 附录 - /// - public static class Appendix - { - /// - /// 附录1 A1格式 - /// - /// - public static string Appendix_A1(List data) - { - var seconds = data[0]; - var minutes = data[1]; - var hours = data[2]; - var day = data[3]; - var binString = data[4].HexToBin(); - var months = binString.Substring(3, 1).BinToDec() * 10 + Convert.ToInt32(binString.Substring(4, 4).BinToHex()); - var week = binString.Substring(0, 3).BinToHex(); - var year = $"{DateTime.Now.ToString("yyyy").Substring(0, 2)}{data[5]}"; - return $"{year}-{months.ToString().PadLeft(2, '0')}-{day} {hours}:{minutes}:{seconds}_{week}"; - } - - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisStrategyContext.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisStrategyContext.cs deleted file mode 100644 index 628934a..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/AnalysisStrategyContext.cs +++ /dev/null @@ -1,33 +0,0 @@ -using JiShe.CollectBus.Protocol.Contracts.Interfaces; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts -{ - - public class AnalysisStrategyContext(IServiceProvider provider) - { - private readonly IServiceProvider _provider = provider; - - /// - /// 执行策略 - /// - /// - /// - /// - /// - /// - public Task ExecuteAsync(string type, TInput input) - { - var factory = _provider.GetRequiredService>(); - var strategy = (IAnalysisStrategy)factory(type, typeof(TInput), typeof(TResult)); - return strategy.ExecuteAsync(input); - } - } - - -} diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_02800002.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_02800002.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_02800002.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_02800002.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000201.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000201.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000201.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000201.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000202.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000202.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000202.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000202.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000203.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000203.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000203.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000203.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000204.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000204.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04000204.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04000204.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010000.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010000.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010000.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010000.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010001.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010001.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010001.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010001.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010002.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010002.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010002.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010002.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010003.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010003.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010003.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010003.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010004.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010004.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010004.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010004.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010005.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010005.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010005.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010005.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010006.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010006.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010006.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010006.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010007.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010007.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010007.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010007.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010008.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010008.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_04010008.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_04010008.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A1.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A1.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A1.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A1.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A11.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A11.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A11.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A11.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A13.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A13.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A13.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A13.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A14.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A14.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A14.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A14.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A15.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A15.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A15.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A15.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A17.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A17.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A17.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A17.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A23.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A23.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A23.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A23.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A25.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A25.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A25.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A25.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A5.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A5.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A5.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A5.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A7.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A7.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A7.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A7.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A9.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A9.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Appendix/Appendix_A9.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Appendix/Appendix_A9.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Attributes/ProtocolNameAttribute.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Attributes/ProtocolNameAttribute.cs deleted file mode 100644 index a04af71..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Attributes/ProtocolNameAttribute.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace JiShe.CollectBus.Protocol.Contracts.Attributes -{ - [AttributeUsage(AttributeTargets.Class)] - public class ProtocolNameAttribute(string name) : Attribute - { - public string Name { get; set; } = name; - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/CollectBusProtocolT37612012Module.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/CollectBusProtocolT37612012Module.cs index ec4a12f..054ef2f 100644 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/CollectBusProtocolT37612012Module.cs +++ b/protocols/JiShe.CollectBus.Protocol.T37612012/CollectBusProtocolT37612012Module.cs @@ -1,4 +1,9 @@ -using Volo.Abp.Modularity; +using JiShe.CollectBus.Protocol.Interfaces; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System.Reflection; +using Volo.Abp; +using Volo.Abp.Modularity; namespace JiShe.CollectBus.Protocol.Contracts { @@ -6,6 +11,91 @@ namespace JiShe.CollectBus.Protocol.Contracts { public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.AddKeyedSingleton(nameof(StandardProtocolPlugin)); + //RegisterProtocolAnalysis(context.Services); + LoadAnalysisStrategy(context.Services); + } + + public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + Console.WriteLine("StandardProtocolPlugin OnApplicationInitializationAsync"); + var standardProtocol = context.ServiceProvider.GetRequiredKeyedService(nameof(StandardProtocolPlugin)); + await standardProtocol.LoadAsync(); + } + + public override void OnApplicationShutdown(ApplicationShutdownContext context) + { + Console.WriteLine("StandardProtocolPlugin OnApplicationShutdown"); + base.OnApplicationShutdown(context); + } + + public void LoadAnalysisStrategy(IServiceCollection services) + { + var assembly = Assembly.GetExecutingAssembly(); + var analysisStrategyTypes = assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAnalysisStrategy<,>))); + foreach (var analysisStrategyType in analysisStrategyTypes) + { + var service = analysisStrategyType.GetInterfaces().First(); + services.AddKeyedSingleton(service, analysisStrategyType.Name, analysisStrategyType); + } + } + + public void RegisterProtocolAnalysis(IServiceCollection services) + { + // 扫描并注册所有策略 + var strategyMetadata = new Dictionary<(string, Type, Type), Type>(); + services.AddTransient(); + + // 批量注册 + var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); + if (string.IsNullOrWhiteSpace(assemblyPath)) + { + return; + } + var dllFiles = Directory.GetFiles(Path.Combine(assemblyPath, "Plugins"), "*.dll"); + foreach (var file in dllFiles) + { + // 跳过已加载的程序集 + var assemblyName = AssemblyName.GetAssemblyName(file); + var existingAssembly = AppDomain.CurrentDomain.GetAssemblies() + .FirstOrDefault(a => a.GetName().FullName == assemblyName.FullName); + var assembly = existingAssembly ?? Assembly.LoadFrom(file); + // 实现IAnalysisStrategy接口 + var analysisStrategyTypes = assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAnalysisStrategy<,>))); + if (!analysisStrategyTypes.Any()) + continue; + foreach (var analysisStrategyType in analysisStrategyTypes) + { + // 通过反射获取静态元数据 + var strategyType = analysisStrategyType.Name; + var genericArgs = analysisStrategyType.GetInterface($"IAnalysisStrategy`2")!.GetGenericArguments(); + var inputType = genericArgs[0]; + var resultType = genericArgs[1]; + // 注册策略实现 + services.AddTransient(analysisStrategyType); + strategyMetadata[(strategyType, inputType, resultType)] = analysisStrategyType; + } + } + + // 注册元数据字典 + services.AddSingleton(strategyMetadata); + + // 注册策略解析工厂 + services.AddTransient>(provider => (name, inputType, resultType) => + { + var metadata = provider.GetRequiredService>(); + if (metadata.TryGetValue((name, inputType, resultType), out var strategyType)) + { + return provider.GetRequiredService(strategyType); + } + else + { + var logger = provider.GetRequiredService>(); + logger.LogWarning($"未能找到解析策略:{name}-{inputType}-{resultType}"); + return null; + } + }); + } } diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Interfaces/IAnalysisStrategy.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Interfaces/IAnalysisStrategy.cs deleted file mode 100644 index dcd12fb..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Interfaces/IAnalysisStrategy.cs +++ /dev/null @@ -1,15 +0,0 @@ -using JiShe.CollectBus.Protocol.Contracts.Models; -using JiShe.CollectBus.Protocol.Dto; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Interfaces -{ - public interface IAnalysisStrategy - { - Task ExecuteAsync(TInput input); - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/JiShe.CollectBus.Protocol.T37612012.csproj b/protocols/JiShe.CollectBus.Protocol.T37612012/JiShe.CollectBus.Protocol.T37612012.csproj index 2c074cd..e83c165 100644 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/JiShe.CollectBus.Protocol.T37612012.csproj +++ b/protocols/JiShe.CollectBus.Protocol.T37612012/JiShe.CollectBus.Protocol.T37612012.csproj @@ -12,8 +12,10 @@ + + diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Models/CustomFixedHeaderRequestInfo.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Models/CustomFixedHeaderRequestInfo.cs deleted file mode 100644 index 61ea887..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Models/CustomFixedHeaderRequestInfo.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TouchSocket.Core; - -namespace JiShe.CollectBus.Protocol.Contracts.Models -{ - public class CustomFixedHeaderRequestInfo : IFixedHeaderRequestInfo - { - /// - /// 报文开始前缀 - /// - public string StartPrefix { get; set; } = "68"; - - /// - /// 报文结束后缀 - /// - public string EndPrefix { get; set; } = "16"; - - /// - /// 头部长度 - /// - public int HeadLength = 5; - - /// - /// 固定长度 - /// - private const int FixedLength = 17; - - /// - /// 报文长度 - /// - public int PacketLength { get; set; } - - /// - /// 控制域 C - /// - public int ControlDomain { get; set; } - - public bool OnParsingHeader(ReadOnlySpan header) - { - //throw new NotImplementedException(); - return true; - } - - public bool OnParsingBody(ReadOnlySpan body) - { - //throw new NotImplementedException(); - return true; - - } - - public int BodyLength { get; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN10_F10_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN10_F10_AnalysisDto.cs deleted file mode 100644 index 46b7581..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN10_F10_AnalysisDto.cs +++ /dev/null @@ -1,95 +0,0 @@ -using JiShe.CollectBus.Protocol.Dto; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN10_F10_AnalysisDto - { - /// - /// 本次电能表/交流采样装置配置数量 n - /// - public int ConfigNum { get; set; } - - /// - /// 电能表/交流采样装置配置信息 - /// - public List AFN10F10Entitys { get; set; } = new List(); - } - - - public class AFN10F10Entity - { - /// - /// 电能表/交流采样装置序号 - /// - public int SerialNum { get; set; } - - /// - /// 所属测量点号 - /// - public int Point { get; set; } - - /// - /// 通信速率 - /// - public int BaudRate { get; set; } - - /// - /// 端口号 - /// - public int Port { get; set; } - - - /// - /// 通信协议类型 - /// - public string RuleType { get; set; } - - /// - /// 通信地址 - /// - public string ComAddress { get; set; } - - /// - /// 通信密码 - /// - public string ComPwd { get; set; } - - - /// - /// 电能费率个数 - /// - public int ElectricityRatesNum { get; set; } - - - /// - /// 有功电能示值整数位及小数位个数 - /// - public int IntegerBitsNum { get; set; } - - /// - /// 有功电能示值小数位个数 - /// - public int DecimalPlacesNum { get; set; } - - /// - /// 所属采集器通信地址 - /// - public string CollectorAddress{ get; set; } - - /// - /// 用户大类号 - /// - public int UserCategoryNum { get; set;} - - /// - /// 用户小类号 - /// - public int UserSubclassNum { get; set; } - } - -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN10_F66_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN10_F66_AnalysisDto.cs deleted file mode 100644 index c3c8306..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN10_F66_AnalysisDto.cs +++ /dev/null @@ -1,45 +0,0 @@ -using JiShe.CollectBus.Protocol.Dto; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN10_F66_AnalysisDto - { - /// - /// 定时发送周期 - /// 1:用 D6~D7 编码表示,取值 0~3 依次表示分、时、日、月 - /// 2:用 D0~D5 表示,为定时上报数据的时间周期 - /// - public int Cycle { get; set; } - /// - /// 定时发送周期(单位) - /// - public int Unit { get; set; } - /// - /// 发送基准时间 - /// - public DateTime BaseTime { get; set; } - - /// - /// 曲线数据提取倍率 - /// - public int CurveRatio { get; set; } - - /// - /// 任务号 - /// - public int Pn { get; set; } - - public List Details { get; set; } = new List(); - } - - public class SetAutoItemCodeDetails - { - public int Pn { get; set; } - public int Fn { get; set; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F129_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F129_AnalysisDto.cs deleted file mode 100644 index 30719e9..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F129_AnalysisDto.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F129_AnalysisDto : AnalysisBaseDto - { - public DateTime ReadTime { get; set; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F130_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F130_AnalysisDto.cs deleted file mode 100644 index 9a9259b..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F130_AnalysisDto.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F130_AnalysisDto : AnalysisBaseDto - { - public DateTime ReadTime { get; set; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F131_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F131_AnalysisDto.cs deleted file mode 100644 index edc5ca7..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F131_AnalysisDto.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F131_AnalysisDto: AnalysisBaseDto - { - public DateTime ReadTime { get; set; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F132_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F132_AnalysisDto.cs deleted file mode 100644 index 1625f15..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F132_AnalysisDto.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F132_AnalysisDto: AnalysisBaseDto - { - public DateTime ReadTime { get; set; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F145_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F145_AnalysisDto.cs deleted file mode 100644 index 96f0c37..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F145_AnalysisDto.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F145_AnalysisDto : AnalysisBaseDto - { - /// - /// 最大需量时标 - /// - public string TimeSpan { get; set; } - - public DateTime ReadingDate { get; set; } - } - -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F149_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F149_AnalysisDto.cs deleted file mode 100644 index fb9dafe..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F149_AnalysisDto.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F149_AnalysisDto : AnalysisBaseDto - { - /// - /// 最大需量时标 - /// - public string TimeSpan { get; set; } - - public DateTime ReadingDate { get; set; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F188_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F188_AnalysisDto.cs deleted file mode 100644 index 1afb8ff..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F188_AnalysisDto.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F188_AnalysisDto : AnalysisBaseDto - { - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F25_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F25_AnalysisDto.cs deleted file mode 100644 index ca8b8ff..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F25_AnalysisDto.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F25_AnalysisDto: AnalysisBaseDto - { - /// - /// 读取时间 - /// - public DateTime ReadTime { get; set; } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F2_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F2_AnalysisDto.cs deleted file mode 100644 index e069382..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F2_AnalysisDto.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F2_AnalysisDto: AnalysisBaseDto - { - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F33_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F33_AnalysisDto.cs deleted file mode 100644 index b917969..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F33_AnalysisDto.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F33_AnalysisDto - { - /// - /// 终端抄表时间 - /// - public string ReadTime { get; set; } - - /// - /// 费率数M(1≤M≤12) - /// - public int RatingCount { get; set; } - /// - /// 当前正向有功总电能示值 - /// - public ParentNodes F_A_Kwh { get; set; } - /// - /// 当前正向无功(组合无功1)总电能示值 - /// - public ParentNodes R_R_Kvarh { get; set; } - - /// - /// 当前一象限无功总电能示值 - /// - public ParentNodes Q1_R_Kvarh { get; set; } - /// - /// 当前四象限无功总电能示值 - /// - public ParentNodes Q4_R_Kvarh { get; set; } - } - - public class ParentNodes - { - /// - /// 总电能示值 - /// - public decimal Total_Value { get; set; } - public List childNodes { get; set; } - } - public class ChildNodes - { - /// - /// 费率总电能示值 - /// - public decimal Value { get; set; } - - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F49_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F49_AnalysisDto.cs deleted file mode 100644 index ce0aeb7..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN12_F49_AnalysisDto.cs +++ /dev/null @@ -1,15 +0,0 @@ -using JiShe.CollectBus.Protocol.Dto; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN12_F49_AnalysisDto: AnalysisBaseDto - { - } - - -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN9_F1_AnalysisDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN9_F1_AnalysisDto.cs deleted file mode 100644 index 7b7bc3a..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AFN9_F1_AnalysisDto.cs +++ /dev/null @@ -1,38 +0,0 @@ -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AFN9_F1_AnalysisDto - { - public int FocusID { get; set; } - public string? AreaCode { get; set; } - public string? Address { get; set; } - - /// - /// 厂商代号 - /// - public string? MakerNo { get; set; } - /// - /// 设备编号 - /// - public string? DeviceNo { get; set; } - /// - /// 终端软件版本号 - /// - public string? SoftwareVersion { get; set; } - /// - /// 终端软件发布日期:日月年 - /// - public string? SoftwareReleaseDate { get; set; } - - /// - /// 硬件软件版本号 - /// - public string? HardwareVersion { get; set; } - /// - /// 硬件软件发布日期:日月年 - /// - public string? HardwareReleaseDate { get; set; } - - public string? AddDate { get; set; } - } - -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AnalysisBaseDto.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AnalysisBaseDto.cs deleted file mode 100644 index 645c567..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/AnalysisBaseDto.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace JiShe.CollectBus.Protocol.Contracts.Protocol.Dto -{ - public class AnalysisBaseDto - { - /// - /// 是否有效数据 - /// - public bool ValidData { get; set; } = true; - - /// - /// 数据类型 - /// - public string PointDataType { get; set; } = null!; - - /// - /// 错误码信息 - /// - public string? ErrorCodeMsg { get; set; } - - /// - /// 字段名 - /// - public string FiledName { get; set; } = null!; - - /// - /// 字段描述 - /// - public string FiledDesc { get; set; } = null!; - } - - public class AnalysisBaseDto : AnalysisBaseDto - { - /// - /// 抄读值 - /// - public T? DataValue { get; set; } - } - -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/UnitDataAnalysis.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/UnitDataAnalysis.cs deleted file mode 100644 index d10594b..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol/Dto/UnitDataAnalysis.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace JiShe.CollectBus.Protocol.Dto -{ - public class UnitDataAnalysis - { - /// - /// 集中器地址 - /// - public string Code { get; set; } = null!; - - /// - /// AFN功能码 - /// - public int AFN { get; set; } - - /// - /// 信息点 - /// - public int Pn { get; set; } - - /// - /// 信息类 - /// - public int Fn { get; set; } - - /// - /// 主站地址 MSA - /// - public int MSA { get; set; } - - /// - /// 响应帧序号 - /// - public int PSEQ { get; set; } - - } - - public class UnitDataAnalysis: UnitDataAnalysis - { - /// - /// 数据 - /// - public T? Data { get; set; } - } - -} diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/Protocol3761Extensions.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/Protocol3761Extensions.cs similarity index 100% rename from protocols/JiShe.CollectBus.Protocol.T6452007/Protocol3761Extensions.cs rename to protocols/JiShe.CollectBus.Protocol.T37612012/Protocol3761Extensions.cs diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/QGDW3761Config.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/QGDW3761Config.cs deleted file mode 100644 index 88f6bcb..0000000 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/QGDW3761Config.cs +++ /dev/null @@ -1,70 +0,0 @@ -using JiShe.CollectBus.Protocol.Contracts.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using JiShe.CollectBus.Common.Extensions; - -namespace JiShe.CollectBus.Protocol.Contracts -{ - public class QGDW3761Config - { - private static List _commandList = null; - public static List CommandList - { - get - { - if (_commandList == null) - { - var filePath = AppDomain.CurrentDomain.BaseDirectory + "cmd3761Matching.txt"; - - try - { - var fileStr = ""; - if (File.Exists(filePath)) - fileStr = File.ReadAllText(filePath, Encoding.UTF8); - - if (!string.IsNullOrWhiteSpace(fileStr)) - { - _commandList = fileStr.FromJson>(); - } - } - catch (Exception) - { - - } - } - return _commandList; - } - } - - private static List _commandTdcList = null; - public static List CommandTdcList - { - get - { - if (_commandTdcList == null) - { - var filePath = AppDomain.CurrentDomain.BaseDirectory + "cmd3761TdcMatching.txt"; - - try - { - var fileStr = ""; - if (File.Exists(filePath)) - fileStr = File.ReadAllText(filePath, Encoding.UTF8); - - if (!string.IsNullOrWhiteSpace(fileStr)) - { - _commandTdcList = fileStr.FromJson>(); - } - } - catch (Exception) - { - - } - } - return _commandTdcList; - } - } - } -} diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/StandardProtocolPlugin.cs b/protocols/JiShe.CollectBus.Protocol.T37612012/StandardProtocolPlugin.cs new file mode 100644 index 0000000..55dbb92 --- /dev/null +++ b/protocols/JiShe.CollectBus.Protocol.T37612012/StandardProtocolPlugin.cs @@ -0,0 +1,508 @@ +using JiShe.CollectBus.Common.BuildSendDatas; +using JiShe.CollectBus.Common.Consts; +using JiShe.CollectBus.Common.Enums; +using JiShe.CollectBus.Common.Extensions; +using JiShe.CollectBus.Common.Helpers; +using JiShe.CollectBus.Common.Models; +using JiShe.CollectBus.Enums; +using JiShe.CollectBus.IotSystems.Devices; +using JiShe.CollectBus.IotSystems.MessageReceiveds; +using JiShe.CollectBus.IotSystems.Protocols; +using JiShe.CollectBus.Kafka.Producer; +using JiShe.CollectBus.Protocol.Contracts.Abstracts; +using JiShe.CollectBus.Protocol.Contracts.SendData; +using JiShe.CollectBus.Protocol3761; +using Mapster; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using TouchSocket.Sockets; +using Volo.Abp.Domain.Repositories; + +namespace JiShe.CollectBus.Protocol +{ + public class StandardProtocolPlugin : ProtocolPlugin + { + private readonly ILogger _logger; + + private readonly IProducerService _producerService; + + private readonly IRepository _deviceRepository; + private readonly ITcpService _tcpService; + + public readonly Dictionary T3761AFNHandlers; + + /// + /// Initializes a new instance of the class. + /// + /// The service provider. + public StandardProtocolPlugin(IServiceProvider serviceProvider, ILogger logger, ITcpService tcpService) : base(serviceProvider, logger) + { + _logger = logger; + //_logger = serviceProvider.GetRequiredService>(); + _producerService = serviceProvider.GetRequiredService(); + _deviceRepository = serviceProvider.GetRequiredService>(); + _tcpService = tcpService; + T3761AFNHandlers = Telemetry3761PacketBuilder.T3761AFNHandlers; + } + + public sealed override ProtocolInfo Info => new(nameof(StandardProtocolPlugin), "376.1", "TCP", "376.1协议", "DTS1980"); + + public override async Task AnalyzeAsync(ITcpSessionClient client, string messageReceived, Action? sendAction = null) + { + TB3761? tB3761 = Analysis3761(messageReceived); + if (tB3761 != null) + { + if (tB3761.AFN_FC?.AFN == (int)AFN.链路接口检测) + { + if (tB3761.A == null || tB3761.A.Code.IsNullOrWhiteSpace() || tB3761.A.A3?.D1_D7 == null || tB3761.SEQ?.PSEQ == null) + { + _logger.LogError($"解析AFN.链路接口检测报文失败,报文:{messageReceived},TB3761:{tB3761.Serialize()}"); + } + else + { + if (tB3761.DT?.Fn == (int)FN.登录) + { + // 登录回复 + if (tB3761.SEQ.CON == (int)CON.需要对该帧进行确认) + await LoginAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ); + } + else if (tB3761.DT?.Fn == (int)FN.心跳) + { + // 心跳回复 + //心跳帧有两种情况: + //1. 集中器先有登录帧,再有心跳帧 + //2. 集中器没有登录帧,只有心跳帧 + await HeartbeatAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ); + } + } + + } + await OnTcpNormalReceived(client, tB3761); + } + return (tB3761 as T)!; + } + + /// + /// 正常帧处理,将不同的AFN进行分发 + /// + /// + /// + /// + /// + private async Task OnTcpNormalReceived(ITcpSessionClient tcpSessionClient, TB3761 tB3761) + { + //string topicName = string.Format(ProtocolConst.AFNTopicNameFormat, aFn); + //todo 如何确定时标?目前集中器的采集频率,都是固定,数据上报的时候,根据当前时间,往后推测出应当采集的时间点作为时标。但是如果由于网络问题,数据一直没上报的情况改怎么计算? + //await _producerBus.PublishAsync(ProtocolConst.SubscriberReceivedEventName, new MessageReceived + //{ + // ClientId = client.Id, + // ClientIp = client.IP, + // ClientPort = client.Port, + // MessageHexString = messageHexString, + // DeviceNo = deviceNo, + // MessageId = NewId.NextGuid().ToString() + //}); + + if (tB3761.AFN_FC.BaseHexMessage == null || tB3761.DT.BaseHexMessage == null || tB3761.BaseHexMessage.HexMessageString==null) + { + _logger.LogError("376.1协议解析AFN失败"); + return; + } + // 登录心跳已做了处理,故需要忽略登录和心跳帧 + if (tB3761.DT.Fn == (int)FN.登录 || tB3761.DT.Fn == (int)FN.心跳) + return; + //TODO:根据AFN进行分流推送到kafka + string topicName = string.Format(ProtocolConst.AFNTopicNameFormat, tB3761.AFN_FC.AFN.ToString().PadLeft(2, '0')); + + MessageProtocolAnalysis messageReceivedAnalysis = new MessageProtocolAnalysis() + { + ClientId = tcpSessionClient.Id, + ClientIp = tcpSessionClient.IP, + ClientPort = tcpSessionClient.Port, + MessageHexString = tB3761.BaseHexMessage.HexMessageString!, + DeviceNo = tB3761.A.Code!, + MessageId = Guid.NewGuid().ToString(), + ReceivedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + Data = tB3761 + }; + + List topics = ProtocolConstExtensions.GetAllTopicNamesByReceived(); + if (topics.Contains(topicName)) + await _producerService.ProduceAsync(topicName, messageReceivedAnalysis); + else + { + _logger.LogError($"不支持的上报kafka主题:{topicName}"); + await _producerService.ProduceAsync(ProtocolConst.SubscriberReceivedEventName, messageReceivedAnalysis); + } + + } + + + /// + /// 登录回复 + /// + /// + /// + /// + /// + /// + public async Task LoginAsync(ITcpSessionClient client, string messageReceived, string code, int? msa, int? pseq) + { + string oldClientId = $"{client.Id}"; + await client.ResetIdAsync(code); + var deviceInfoList = await _deviceRepository.GetListAsync(a => a.Number == code); + if (deviceInfoList != null && deviceInfoList.Count > 1) + { + //todo 推送集中器编号重复预警 + _logger.LogError($"集中器编号:{code},存在多个集中器,请检查集中器编号是否重复"); + return; + } + + var entity = deviceInfoList?.FirstOrDefault(a => a.Number == code); + if (entity == null) + { + await _deviceRepository.InsertAsync(new Device(code, oldClientId, DateTime.Now, DateTime.Now, DeviceStatus.Online)); + } + else + { + entity.UpdateByLoginAndHeartbeat(oldClientId); + await _deviceRepository.UpdateAsync(entity); + } + + var messageReceivedLoginEvent = new MessageReceivedLogin + { + ClientId = code, + ClientIp = client.IP, + ClientPort = client.Port, + MessageHexString = messageReceived, + DeviceNo = code, + MessageId = Guid.NewGuid().ToString(), + ReceivedTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + }; + await _producerService.ProduceAsync(ProtocolConst.SubscriberLoginReceivedEventName, messageReceivedLoginEvent); + var reqParam = new ReqParameter2 + { + AFN = AFN.确认或否认, + FunCode = (int)CFromStationFunCode.链路数据, + PRM = PRM.从动站报文, + A = code, + Seq = new Seq() + { + TpV = TpV.附加信息域中无时间标签, + FIRFIN = FIRFIN.单帧, + CON = CON.需要对该帧进行确认, + PRSEQ = pseq!.Value + }, + MSA = msa!.Value, + Pn = 0, + Fn = 1 + }; + var bytes = Build3761SendData.BuildSendCommandBytes(reqParam); + var issuedEventMessage = new IssuedEventMessage + { + ClientId = messageReceivedLoginEvent.ClientId, + DeviceNo = messageReceivedLoginEvent.DeviceNo, + Message = bytes, + Type = IssuedEventType.Login, + MessageId = messageReceivedLoginEvent.MessageId + }; + if (_tcpService.ClientExists(issuedEventMessage.ClientId)) + { + await _tcpService.SendAsync(issuedEventMessage.ClientId, issuedEventMessage.Message); + _logger.LogInformation($"集中器地址{issuedEventMessage.ClientId} 登录回复下发内容:{Convert.ToHexString(bytes)}"); + await _producerService.ProduceAsync(ProtocolConst.SubscriberLoginIssuedEventName, issuedEventMessage); + } + } + + /// + /// 心跳帧解析 + /// + /// + /// + /// + /// + /// + public async Task HeartbeatAsync(ITcpSessionClient client, string messageReceived, string code, int? msa, int? pseq) + { + + string clientId = code; + string oldClientId = $"{client.Id}"; + var deviceInfoList = await _deviceRepository.GetListAsync(a => a.Number == code); + if (deviceInfoList != null && deviceInfoList.Count > 1) + { + //todo 推送集中器编号重复预警 + _logger.LogError($"集中器编号:{code},存在多个集中器,请检查集中器编号是否重复"); + return; + } + + var entity = deviceInfoList?.FirstOrDefault(a => a.Number == code); + if (entity == null) //没有登录帧的设备,只有心跳帧 + { + await client.ResetIdAsync(clientId); + await _deviceRepository.InsertAsync(new Device(code, oldClientId, DateTime.Now, DateTime.Now, DeviceStatus.Online)); + } + else + { + if (clientId != oldClientId) + { + entity.UpdateByLoginAndHeartbeat(oldClientId); + } + else + { + entity.UpdateByLoginAndHeartbeat(); + } + + await _deviceRepository.UpdateAsync(entity); + } + + var messageReceivedHeartbeatEvent = new MessageReceivedHeartbeat + { + ClientId = clientId, + ClientIp = client.IP, + ClientPort = client.Port, + MessageHexString = messageReceived, + DeviceNo = code, + MessageId = Guid.NewGuid().ToString(), + ReceivedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + }; + await _producerService.ProduceAsync(ProtocolConst.SubscriberHeartbeatReceivedEventName, messageReceivedHeartbeatEvent); + + var reqParam = new ReqParameter2() + { + AFN = AFN.确认或否认, + FunCode = (int)CFromStationFunCode.链路数据, + PRM = PRM.从动站报文, + A = code, + Seq = new Seq() + { + TpV = TpV.附加信息域中无时间标签, + FIRFIN = FIRFIN.单帧, + CON = CON.不需要对该帧进行确认, + PRSEQ = pseq!.Value, + }, + MSA = msa!.Value, + Pn = 0, + Fn = 1 + }; + var bytes = Build3761SendData.BuildSendCommandBytes(reqParam); + + IssuedEventMessage issuedEventMessage = new IssuedEventMessage + { + ClientId = messageReceivedHeartbeatEvent.ClientId, + DeviceNo = messageReceivedHeartbeatEvent.DeviceNo, + Message = bytes, + Type = IssuedEventType.Heartbeat, + MessageId = messageReceivedHeartbeatEvent.MessageId + }; + if (_tcpService.ClientExists(issuedEventMessage.ClientId)) + { + await _tcpService.SendAsync(issuedEventMessage.ClientId, bytes); + _logger.LogWarning($"集中器地址{issuedEventMessage.ClientId} 心跳回复下发内容:{Convert.ToHexString(bytes)}"); + await _producerService.ProduceAsync(ProtocolConst.SubscriberHeartbeatIssuedEventName, issuedEventMessage); + } + + + } + + + + ///// + ///// 组装报文 + ///// + ///// 报文构建参数 + ///// + //public override async Task BuildAsync(ProtocolBuildRequest request) + //{ + // if (request == null) + // { + // throw new Exception($"{nameof(StandardProtocolPlugin)} 报文构建失败,参数为空"); + // } + // var itemCodeArr = request.ItemCode.Split('_'); + // var aFNStr = itemCodeArr[0]; + // var aFN = (AFN)aFNStr.HexToDec(); + // var fn = int.Parse(itemCodeArr[1]); + // Telemetry3761PacketResponse builderResponse = null; + + // List dataUnit = new List(); + // //数据转发场景 10H_F1_1CH + // if (aFNStr == "10" && request.SubProtocolRequest != null && string.IsNullOrWhiteSpace(request.SubProtocolRequest.ItemCode) == false) + // { + // var t645PacketHandlerName = $"C{request.SubProtocolRequest.ItemCode}_Send"; + // Telemetry645PacketResponse t645PacketResponse = null; + + // if (T645ControlHandlers != null && T645ControlHandlers.TryGetValue(t645PacketHandlerName + // , out var t645PacketHandler)) + // { + // t645PacketResponse = t645PacketHandler(new Telemetry645PacketRequest() + // { + // MeterAddress = request.SubProtocolRequest.MeterAddress, + // Password = request.SubProtocolRequest.Password, + // ItemCode = request.SubProtocolRequest.ItemCode, + // }); + // } + + // if (t645PacketResponse != null) + // { + // dataUnit = t645PacketResponse.Data; + // } + // } + + // string afnMethonCode = $"AFN{aFNStr}_Fn_Send"; + // if (T3761AFNHandlers != null && T3761AFNHandlers.TryGetValue(afnMethonCode + // , out var handler)) + // { + // builderResponse = handler(new Telemetry3761PacketRequest() + // { + // FocusAddress = request.FocusAddress, + // Fn = fn, + // Pn = request.Pn, + // DataUnit = dataUnit, + // }); + // } + + // if (builderResponse == null) + // { + // return new ProtocolBuildResponse(); + // } + + // var result = builderResponse.Adapt(); + // result.IsSuccess = true; + + // return await Task.FromResult(result); + //} + + + #region 上行命令 + + //68 + //32 00 + //32 00 + //68 + //C9 1100'1001. 控制域C。 + // D7=1, (终端发送)上行方向。 + // D6=1, 此帧来自启动站。 + // D5=0, (上行方向)要求访问位。表示终端无事件数据等待访问。 + // D4=0, 保留 + // D3~D0=9, 功能码。链路测试 + + //20 32 行政区划码 + //90 26 终端地址 + //00 主站地址和组地址标志。终端为单地址。 //3220 09 87 2 + // 终端启动的发送帧的 MSA 应为 0, 其主站响应帧的 MSA 也应为 0. + //02 应用层功能码。AFN=2, 链路接口检测 + //70 0111'0000. 帧序列域。无时间标签、单帧、需要确认。 + //00 00 信息点。DA1和DA2全为“0”时,表示终端信息点。 + //01 00 信息类。F1, 登录。 + //44 帧尾,包含用户区数据校验和 + //16 帧结束标志 + + /// + /// 解析上行命令 + /// + /// + /// + public CommandReulst? AnalysisCmd(string cmd) + { + CommandReulst? commandReulst = null; + var hexStringList = cmd.StringToPairs(); + + if (hexStringList.Count < hearderLen) + { + return commandReulst; + } + //验证起始字符 + if (!hexStringList[0].IsStartStr() || !hexStringList[5].IsStartStr()) + { + return commandReulst; + } + + var lenHexStr = $"{hexStringList[2]}{hexStringList[1]}"; + var lenBin = lenHexStr.HexToBin(); + var len = lenBin.Remove(lenBin.Length - 2).BinToDec(); + //验证长度 + if (hexStringList.Count - 2 != hearderLen + len) + return commandReulst; + + var userDataIndex = hearderLen; + var c = hexStringList[userDataIndex];//控制域 1字节 + userDataIndex += 1; + + var aHexList = hexStringList.Skip(userDataIndex).Take(5).ToList();//地址域 5字节 + var a = AnalysisA(aHexList); + var a3Bin = aHexList[4].HexToBin().PadLeft(8, '0'); + var mSA = a3Bin.Substring(0, 7).BinToDec(); + userDataIndex += 5; + + var aFN = (AFN)hexStringList[userDataIndex].HexToDec();//1字节 + userDataIndex += 1; + + var seq = hexStringList[userDataIndex].HexToBin().PadLeft(8, '0'); + var tpV = (TpV)Convert.ToInt32(seq.Substring(0, 1)); + var fIRFIN = (FIRFIN)Convert.ToInt32(seq.Substring(1, 2)); + var cON = (CON)Convert.ToInt32(seq.Substring(3, 1)); + var prseqBin = seq.Substring(4, 4); + userDataIndex += 1; + + // (DA2 - 1) * 8 + DA1 = pn + var da1Bin = hexStringList[userDataIndex].HexToBin(); + var da1 = da1Bin == "0" ? 0 : da1Bin.Length; + userDataIndex += 1; + var da2 = hexStringList[userDataIndex].HexToDec(); + var pn = da2 == 0 ? 0 : (da2 - 1) * 8 + da1; + userDataIndex += 1; + //(DT2*8)+DT1=fn + var dt1Bin = hexStringList[userDataIndex].HexToBin(); + var dt1 = dt1Bin != "0" ? dt1Bin.Length : 0; + userDataIndex += 1; + var dt2 = hexStringList[userDataIndex].HexToDec(); + var fn = dt2 * 8 + dt1; + userDataIndex += 1; + + //数据单元 + var datas = hexStringList.Skip(userDataIndex).Take(len + hearderLen - userDataIndex).ToList(); + + //EC + //Tp + commandReulst = new CommandReulst() + { + A = a, + MSA = mSA, + AFN = aFN, + Seq = new Seq() + { + TpV = tpV, + FIRFIN = fIRFIN, + CON = cON, + PRSEQ = prseqBin.BinToDec(), + }, + CmdLength = len, + Pn = pn, + Fn = fn, + HexDatas = datas + }; + + return commandReulst; + } + + /// + /// 解析地址 + /// + /// + /// + private string AnalysisA(List aHexList) + { + var a1 = aHexList[1] + aHexList[0]; + var a2 = aHexList[3] + aHexList[2]; + var a2Dec = a2.HexToDec(); + var a3 = aHexList[4]; + var a = $"{a1}{a2Dec.ToString().PadLeft(5, '0')}"; + return a; + } + + public override Task BuildAsync(ProtocolBuildRequest request) + { + throw new NotImplementedException(); + } + #endregion + } +} diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/JiSheCollectBusProtocolT6452007Module.cs b/protocols/JiShe.CollectBus.Protocol.T6452007/JiSheCollectBusProtocolT6452007Module.cs index 767a2d3..826ae1e 100644 --- a/protocols/JiShe.CollectBus.Protocol.T6452007/JiSheCollectBusProtocolT6452007Module.cs +++ b/protocols/JiShe.CollectBus.Protocol.T6452007/JiSheCollectBusProtocolT6452007Module.cs @@ -1,15 +1,5 @@ -using JiShe.CollectBus.Kafka.Internal; -using JiShe.CollectBus.Protocol.AnalysisData; -using JiShe.CollectBus.Protocol.Contracts; -using JiShe.CollectBus.Protocol.Contracts.Abstracts; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; -using JiShe.CollectBus.Protocol.Contracts.Models; -using JiShe.CollectBus.Protocol.Dto; +using JiShe.CollectBus.Protocol.Interfaces; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Logging; -using Serilog.Core; -using System; using System.Reflection; using Volo.Abp; using Volo.Abp.Modularity; @@ -49,63 +39,5 @@ namespace JiShe.CollectBus.Protocol } } - public void RegisterProtocolAnalysis(IServiceCollection services) - { - // 扫描并注册所有策略 - var strategyMetadata = new Dictionary<(string, Type, Type), Type>(); - services.AddTransient(); - - // 批量注册 - var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); - if (string.IsNullOrWhiteSpace(assemblyPath)) - { - return; - } - var dllFiles = Directory.GetFiles(Path.Combine(assemblyPath, "Plugins") , "*.dll"); - foreach (var file in dllFiles) - { - // 跳过已加载的程序集 - var assemblyName = AssemblyName.GetAssemblyName(file); - var existingAssembly = AppDomain.CurrentDomain.GetAssemblies() - .FirstOrDefault(a => a.GetName().FullName == assemblyName.FullName); - var assembly = existingAssembly ?? Assembly.LoadFrom(file); - // 实现IAnalysisStrategy接口 - var analysisStrategyTypes = assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAnalysisStrategy<,>))); - if (!analysisStrategyTypes.Any()) - continue; - foreach (var analysisStrategyType in analysisStrategyTypes) - { - // 通过反射获取静态元数据 - var strategyType = analysisStrategyType.Name; - var genericArgs = analysisStrategyType.GetInterface($"IAnalysisStrategy`2")!.GetGenericArguments(); - var inputType = genericArgs[0]; - var resultType = genericArgs[1]; - // 注册策略实现 - services.AddTransient(analysisStrategyType); - strategyMetadata[(strategyType, inputType, resultType)] = analysisStrategyType; - } - } - - // 注册元数据字典 - services.AddSingleton(strategyMetadata); - - // 注册策略解析工厂 - services.AddTransient>(provider => (name, inputType, resultType) => - { - var metadata = provider.GetRequiredService>(); - if (metadata.TryGetValue((name, inputType, resultType), out var strategyType)) - { - return provider.GetRequiredService(strategyType); - } - else - { - var logger= provider.GetRequiredService>(); - logger.LogWarning($"未能找到解析策略:{name}-{inputType}-{resultType}"); - return null; - } - }); - - - } } } diff --git a/protocols/JiShe.CollectBus.Protocol.T6452007/T6452007ProtocolPlugin.cs b/protocols/JiShe.CollectBus.Protocol.T6452007/T6452007ProtocolPlugin.cs index e0e9fc3..2438093 100644 --- a/protocols/JiShe.CollectBus.Protocol.T6452007/T6452007ProtocolPlugin.cs +++ b/protocols/JiShe.CollectBus.Protocol.T6452007/T6452007ProtocolPlugin.cs @@ -52,263 +52,41 @@ namespace JiShe.CollectBus.Protocol public override async Task AnalyzeAsync(ITcpSessionClient client, string messageReceived, Action? sendAction = null) { - TB3761? tB3761 = Analysis3761(messageReceived); - if (tB3761 != null) - { - if (tB3761.AFN_FC?.AFN == (int)AFN.链路接口检测) - { - if (tB3761.A == null || tB3761.A.Code.IsNullOrWhiteSpace() || tB3761.A.A3?.D1_D7 == null || tB3761.SEQ?.PSEQ == null) - { - _logger.LogError($"解析AFN.链路接口检测报文失败,报文:{messageReceived},TB3761:{tB3761.Serialize()}"); - } - else - { - if (tB3761.DT?.Fn == (int)FN.登录) - { - // 登录回复 - if (tB3761.SEQ.CON == (int)CON.需要对该帧进行确认) - await LoginAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ); - } - else if (tB3761.DT?.Fn == (int)FN.心跳) - { - // 心跳回复 - //心跳帧有两种情况: - //1. 集中器先有登录帧,再有心跳帧 - //2. 集中器没有登录帧,只有心跳帧 - await HeartbeatAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ); - } - } - - } - await OnTcpNormalReceived(client, tB3761); - } - return (tB3761 as T)!; - } - - /// - /// 正常帧处理,将不同的AFN进行分发 - /// - /// - /// - /// - /// - private async Task OnTcpNormalReceived(ITcpSessionClient tcpSessionClient, TB3761 tB3761) - { - //string topicName = string.Format(ProtocolConst.AFNTopicNameFormat, aFn); - //todo 如何确定时标?目前集中器的采集频率,都是固定,数据上报的时候,根据当前时间,往后推测出应当采集的时间点作为时标。但是如果由于网络问题,数据一直没上报的情况改怎么计算? - //await _producerBus.PublishAsync(ProtocolConst.SubscriberReceivedEventName, new MessageReceived + //TODO:645解析报文 + //TB3761? tB3761 = Analysis3761(messageReceived); + //if (tB3761 != null) //{ - // ClientId = client.Id, - // ClientIp = client.IP, - // ClientPort = client.Port, - // MessageHexString = messageHexString, - // DeviceNo = deviceNo, - // MessageId = NewId.NextGuid().ToString() - //}); - - if (tB3761.AFN_FC.BaseHexMessage == null || tB3761.DT.BaseHexMessage == null || tB3761.BaseHexMessage.HexMessageString==null) - { - _logger.LogError("376.1协议解析AFN失败"); - return; - } - // 登录心跳已做了处理,故需要忽略登录和心跳帧 - if (tB3761.DT.Fn == (int)FN.登录 || tB3761.DT.Fn == (int)FN.心跳) - return; - //TODO:根据AFN进行分流推送到kafka - string topicName = string.Format(ProtocolConst.AFNTopicNameFormat, tB3761.AFN_FC.AFN.ToString().PadLeft(2, '0')); - - MessageProtocolAnalysis messageReceivedAnalysis = new MessageProtocolAnalysis() - { - ClientId = tcpSessionClient.Id, - ClientIp = tcpSessionClient.IP, - ClientPort = tcpSessionClient.Port, - MessageHexString = tB3761.BaseHexMessage.HexMessageString!, - DeviceNo = tB3761.A.Code!, - MessageId = Guid.NewGuid().ToString(), - ReceivedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), - Data = tB3761 - }; - - List topics = ProtocolConstExtensions.GetAllTopicNamesByReceived(); - if (topics.Contains(topicName)) - await _producerService.ProduceAsync(topicName, messageReceivedAnalysis); - else - { - _logger.LogError($"不支持的上报kafka主题:{topicName}"); - await _producerService.ProduceAsync(ProtocolConst.SubscriberReceivedEventName, messageReceivedAnalysis); - } + // if (tB3761.AFN_FC?.AFN == (int)AFN.链路接口检测) + // { + // if (tB3761.A == null || tB3761.A.Code.IsNullOrWhiteSpace() || tB3761.A.A3?.D1_D7 == null || tB3761.SEQ?.PSEQ == null) + // { + // _logger.LogError($"解析AFN.链路接口检测报文失败,报文:{messageReceived},TB3761:{tB3761.Serialize()}"); + // } + // else + // { + // if (tB3761.DT?.Fn == (int)FN.登录) + // { + // // 登录回复 + // if (tB3761.SEQ.CON == (int)CON.需要对该帧进行确认) + // await LoginAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ); + // } + // else if (tB3761.DT?.Fn == (int)FN.心跳) + // { + // // 心跳回复 + // //心跳帧有两种情况: + // //1. 集中器先有登录帧,再有心跳帧 + // //2. 集中器没有登录帧,只有心跳帧 + // await HeartbeatAsync(client, messageReceived, tB3761.A.Code, tB3761.A.A3?.D1_D7, tB3761.SEQ?.PSEQ); + // } + // } + // } + // await OnTcpNormalReceived(client, tB3761); + //} + //return (tB3761 as T)!; + return null; } - - /// - /// 登录回复 - /// - /// - /// - /// - /// - /// - public async Task LoginAsync(ITcpSessionClient client, string messageReceived, string code, int? msa, int? pseq) - { - string oldClientId = $"{client.Id}"; - await client.ResetIdAsync(code); - var deviceInfoList = await _deviceRepository.GetListAsync(a => a.Number == code); - if (deviceInfoList != null && deviceInfoList.Count > 1) - { - //todo 推送集中器编号重复预警 - _logger.LogError($"集中器编号:{code},存在多个集中器,请检查集中器编号是否重复"); - return; - } - - var entity = deviceInfoList?.FirstOrDefault(a => a.Number == code); - if (entity == null) - { - await _deviceRepository.InsertAsync(new Device(code, oldClientId, DateTime.Now, DateTime.Now, DeviceStatus.Online)); - } - else - { - entity.UpdateByLoginAndHeartbeat(oldClientId); - await _deviceRepository.UpdateAsync(entity); - } - - var messageReceivedLoginEvent = new MessageReceivedLogin - { - ClientId = code, - ClientIp = client.IP, - ClientPort = client.Port, - MessageHexString = messageReceived, - DeviceNo = code, - MessageId = Guid.NewGuid().ToString(), - ReceivedTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") - }; - await _producerService.ProduceAsync(ProtocolConst.SubscriberLoginReceivedEventName, messageReceivedLoginEvent); - var reqParam = new ReqParameter2 - { - AFN = AFN.确认或否认, - FunCode = (int)CFromStationFunCode.链路数据, - PRM = PRM.从动站报文, - A = code, - Seq = new Seq() - { - TpV = TpV.附加信息域中无时间标签, - FIRFIN = FIRFIN.单帧, - CON = CON.需要对该帧进行确认, - PRSEQ = pseq!.Value - }, - MSA = msa!.Value, - Pn = 0, - Fn = 1 - }; - var bytes = Build3761SendData.BuildSendCommandBytes(reqParam); - var issuedEventMessage = new IssuedEventMessage - { - ClientId = messageReceivedLoginEvent.ClientId, - DeviceNo = messageReceivedLoginEvent.DeviceNo, - Message = bytes, - Type = IssuedEventType.Login, - MessageId = messageReceivedLoginEvent.MessageId - }; - if (_tcpService.ClientExists(issuedEventMessage.ClientId)) - { - await _tcpService.SendAsync(issuedEventMessage.ClientId, issuedEventMessage.Message); - _logger.LogInformation($"集中器地址{issuedEventMessage.ClientId} 登录回复下发内容:{Convert.ToHexString(bytes)}"); - await _producerService.ProduceAsync(ProtocolConst.SubscriberLoginIssuedEventName, issuedEventMessage); - } - } - - /// - /// 心跳帧解析 - /// - /// - /// - /// - /// - /// - public async Task HeartbeatAsync(ITcpSessionClient client, string messageReceived, string code, int? msa, int? pseq) - { - - string clientId = code; - string oldClientId = $"{client.Id}"; - var deviceInfoList = await _deviceRepository.GetListAsync(a => a.Number == code); - if (deviceInfoList != null && deviceInfoList.Count > 1) - { - //todo 推送集中器编号重复预警 - _logger.LogError($"集中器编号:{code},存在多个集中器,请检查集中器编号是否重复"); - return; - } - - var entity = deviceInfoList?.FirstOrDefault(a => a.Number == code); - if (entity == null) //没有登录帧的设备,只有心跳帧 - { - await client.ResetIdAsync(clientId); - await _deviceRepository.InsertAsync(new Device(code, oldClientId, DateTime.Now, DateTime.Now, DeviceStatus.Online)); - } - else - { - if (clientId != oldClientId) - { - entity.UpdateByLoginAndHeartbeat(oldClientId); - } - else - { - entity.UpdateByLoginAndHeartbeat(); - } - - await _deviceRepository.UpdateAsync(entity); - } - - var messageReceivedHeartbeatEvent = new MessageReceivedHeartbeat - { - ClientId = clientId, - ClientIp = client.IP, - ClientPort = client.Port, - MessageHexString = messageReceived, - DeviceNo = code, - MessageId = Guid.NewGuid().ToString(), - ReceivedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") - }; - await _producerService.ProduceAsync(ProtocolConst.SubscriberHeartbeatReceivedEventName, messageReceivedHeartbeatEvent); - - var reqParam = new ReqParameter2() - { - AFN = AFN.确认或否认, - FunCode = (int)CFromStationFunCode.链路数据, - PRM = PRM.从动站报文, - A = code, - Seq = new Seq() - { - TpV = TpV.附加信息域中无时间标签, - FIRFIN = FIRFIN.单帧, - CON = CON.不需要对该帧进行确认, - PRSEQ = pseq!.Value, - }, - MSA = msa!.Value, - Pn = 0, - Fn = 1 - }; - var bytes = Build3761SendData.BuildSendCommandBytes(reqParam); - - IssuedEventMessage issuedEventMessage = new IssuedEventMessage - { - ClientId = messageReceivedHeartbeatEvent.ClientId, - DeviceNo = messageReceivedHeartbeatEvent.DeviceNo, - Message = bytes, - Type = IssuedEventType.Heartbeat, - MessageId = messageReceivedHeartbeatEvent.MessageId - }; - if (_tcpService.ClientExists(issuedEventMessage.ClientId)) - { - await _tcpService.SendAsync(issuedEventMessage.ClientId, bytes); - _logger.LogWarning($"集中器地址{issuedEventMessage.ClientId} 心跳回复下发内容:{Convert.ToHexString(bytes)}"); - await _producerService.ProduceAsync(ProtocolConst.SubscriberHeartbeatIssuedEventName, issuedEventMessage); - } - - - } - - - /// /// 组装报文 /// diff --git a/protocols/JiShe.CollectBus.Protocol/Abstracts/ProtocolPlugin.cs b/protocols/JiShe.CollectBus.Protocol/Abstracts/ProtocolPlugin.cs index 56320a1..8f0e944 100644 --- a/protocols/JiShe.CollectBus.Protocol/Abstracts/ProtocolPlugin.cs +++ b/protocols/JiShe.CollectBus.Protocol/Abstracts/ProtocolPlugin.cs @@ -4,6 +4,7 @@ using JiShe.CollectBus.FreeRedis; using JiShe.CollectBus.IotSystems.Protocols; using JiShe.CollectBus.Protocol.Contracts.SendData; using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.CollectBus.Protocol3761; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using TouchSocket.Sockets; diff --git a/protocols/JiShe.CollectBus.Protocol/AnalysisStrategyContext.cs b/protocols/JiShe.CollectBus.Protocol/AnalysisStrategyContext.cs index 628934a..8a524cc 100644 --- a/protocols/JiShe.CollectBus.Protocol/AnalysisStrategyContext.cs +++ b/protocols/JiShe.CollectBus.Protocol/AnalysisStrategyContext.cs @@ -1,10 +1,5 @@ -using JiShe.CollectBus.Protocol.Contracts.Interfaces; +using JiShe.CollectBus.Protocol.Interfaces; using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace JiShe.CollectBus.Protocol.Contracts { diff --git a/protocols/JiShe.CollectBus.Protocol/Interfaces/IProtocolPlugin.cs b/protocols/JiShe.CollectBus.Protocol/Interfaces/IProtocolPlugin.cs index 0153e69..91eba1a 100644 --- a/protocols/JiShe.CollectBus.Protocol/Interfaces/IProtocolPlugin.cs +++ b/protocols/JiShe.CollectBus.Protocol/Interfaces/IProtocolPlugin.cs @@ -17,8 +17,6 @@ namespace JiShe.CollectBus.Protocol.Interfaces Task AnalyzeAsync(ITcpSessionClient client, string messageReceived, Action? sendAction = null) where T : class; - TB3761? Analysis3761(string messageReceived); - /// /// 组装报文 /// @@ -26,9 +24,5 @@ namespace JiShe.CollectBus.Protocol.Interfaces /// 映射读取执行方法的Code,例如10_1,表示10H_F1 /// Task BuildAsync(ProtocolBuildRequest request); - - //Task LoginAsync(MessageReceivedLogin messageReceived); - - //Task HeartbeatAsync(MessageReceivedHeartbeat messageReceived); } } diff --git a/protocols/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj b/protocols/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj index 495dbf1..f75b5dd 100644 --- a/protocols/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj +++ b/protocols/JiShe.CollectBus.Protocol/JiShe.CollectBus.Protocol.csproj @@ -12,6 +12,11 @@ + + + + + diff --git a/protocols/JiShe.CollectBus.Protocol/Services/ProtocolService.cs b/protocols/JiShe.CollectBus.Protocol/Services/ProtocolService.cs index 054b5ab..c4cf931 100644 --- a/protocols/JiShe.CollectBus.Protocol/Services/ProtocolService.cs +++ b/protocols/JiShe.CollectBus.Protocol/Services/ProtocolService.cs @@ -1,18 +1,12 @@ -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using JiShe.CollectBus.Common.Consts; -using JiShe.CollectBus.Common.Extensions; +using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.FreeRedis; using JiShe.CollectBus.IotSystems.Protocols; -using Volo.Abp.DependencyInjection; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; +using JiShe.CollectBus.Protocol.Interfaces; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using System.Text.RegularExpressions; using Volo.Abp; -using Microsoft.Extensions.Logging; +using Volo.Abp.DependencyInjection; namespace JiShe.CollectBus.Protocol.Contracts.Services { diff --git a/services/JiShe.CollectBus.Application.Contracts/Subscribers/ISubscriberAppService.cs b/services/JiShe.CollectBus.Application.Contracts/Subscribers/ISubscriberAppService.cs index bdc6b78..63594c8 100644 --- a/services/JiShe.CollectBus.Application.Contracts/Subscribers/ISubscriberAppService.cs +++ b/services/JiShe.CollectBus.Application.Contracts/Subscribers/ISubscriberAppService.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using JiShe.CollectBus.Common.Models; using JiShe.CollectBus.IotSystems.MessageReceiveds; using JiShe.CollectBus.Kafka.Internal; +using JiShe.CollectBus.Protocol3761; using Volo.Abp.Application.Services; namespace JiShe.CollectBus.Subscribers @@ -11,7 +12,7 @@ namespace JiShe.CollectBus.Subscribers { Task LoginIssuedEvent(List issuedEventMessage); Task HeartbeatIssuedEvent(List issuedEventMessage); - Task ReceivedEvent(MessageReceived receivedMessage); + Task ReceivedEvent(MessageProtocolAnalysis receivedMessage); Task ReceivedHeartbeatEvent(List receivedHeartbeatMessage); Task ReceivedLoginEvent(List receivedLoginMessage); } diff --git a/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs b/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs index badf6de..6eadb0a 100644 --- a/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs +++ b/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs @@ -1,35 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using DeviceDetectorNET.Parser.Device; -using JiShe.CollectBus.Ammeters; -using JiShe.CollectBus.Common.Consts; -using JiShe.CollectBus.Common.Enums; -using JiShe.CollectBus.Common.Extensions; -using JiShe.CollectBus.Common.Helpers; -using JiShe.CollectBus.Enums; -using JiShe.CollectBus.Interceptors; -using JiShe.CollectBus.IotSystems.Ammeters; +using JiShe.CollectBus.IotSystems.Ammeters; using JiShe.CollectBus.IotSystems.Devices; -using JiShe.CollectBus.IotSystems.MessageReceiveds; using JiShe.CollectBus.Kafka.Producer; -using JiShe.CollectBus.Protocol.Contracts; -using JiShe.CollectBus.Protocol.Contracts.Abstracts; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; -using JiShe.CollectBus.Protocol.Contracts.Models; -using JiShe.CollectBus.Protocol.Contracts.Services; -using Microsoft.Extensions.DependencyInjection; +using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.CollectBus.Protocol3761; using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; using TouchSocket.Core; using TouchSocket.Sockets; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; -using static System.Formats.Asn1.AsnWriter; -using static FreeSql.Internal.GlobalFilter; namespace JiShe.CollectBus.Plugins { diff --git a/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs b/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs index 0a94586..31a3494 100644 --- a/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs +++ b/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs @@ -10,12 +10,11 @@ using JiShe.CollectBus.IoTDB.Context; using JiShe.CollectBus.IoTDB.Interface; using JiShe.CollectBus.IoTDB.Model; using JiShe.CollectBus.IoTDB.Options; -using JiShe.CollectBus.IoTDB.Provider; using JiShe.CollectBus.IotSystems.Ammeters; using JiShe.CollectBus.IotSystems.PrepayModel; using JiShe.CollectBus.Kafka.Attributes; using JiShe.CollectBus.Kafka.Internal; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; +using JiShe.CollectBus.Protocol.Interfaces; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; @@ -25,7 +24,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; -using JiShe.CollectBus.IotSystems.Ammeters; namespace JiShe.CollectBus.Samples; diff --git a/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs b/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs index 7173813..0cb8a2c 100644 --- a/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs +++ b/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs @@ -1,6 +1,5 @@ using JiShe.CollectBus.Application.Contracts; using JiShe.CollectBus.Common; -using JiShe.CollectBus.Common.BuildSendDatas; using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.DeviceBalanceControl; using JiShe.CollectBus.Common.Encrypt; @@ -8,7 +7,6 @@ using JiShe.CollectBus.Common.Enums; using JiShe.CollectBus.Common.Extensions; using JiShe.CollectBus.Common.Helpers; using JiShe.CollectBus.Common.Models; -using JiShe.CollectBus.EnergySystems.Entities; using JiShe.CollectBus.GatherItem; using JiShe.CollectBus.IoTDB.Context; using JiShe.CollectBus.IoTDB.Interface; @@ -20,19 +18,16 @@ using JiShe.CollectBus.IotSystems.MeterReadingRecords; using JiShe.CollectBus.IotSystems.Watermeter; using JiShe.CollectBus.Kafka.Internal; using JiShe.CollectBus.Kafka.Producer; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; using JiShe.CollectBus.Protocol.Contracts.Models; using JiShe.CollectBus.Protocol.Contracts.SendData; -using Microsoft.Extensions.DependencyInjection; +using JiShe.CollectBus.Protocol.Interfaces; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using Microsoft.Identity.Client; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; -using static System.Runtime.CompilerServices.RuntimeHelpers; namespace JiShe.CollectBus.ScheduledMeterReading { diff --git a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAnalysisAppService.cs b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAnalysisAppService.cs index 319792a..990ff51 100644 --- a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAnalysisAppService.cs +++ b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAnalysisAppService.cs @@ -5,18 +5,14 @@ using JiShe.CollectBus.IotSystems.MessageReceiveds; using JiShe.CollectBus.Kafka.Attributes; using JiShe.CollectBus.Kafka.Internal; using JiShe.CollectBus.Protocol.Contracts; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; -using JiShe.CollectBus.Protocol.Contracts.Models; +using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.CollectBus.Protocol3761; using JiShe.CollectBus.Repository.MeterReadingRecord; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using TouchSocket.Sockets; -using Volo.Abp.Domain.Repositories; namespace JiShe.CollectBus.Subscribers { diff --git a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs index d6a7e75..a082551 100644 --- a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs +++ b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs @@ -1,6 +1,5 @@ using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.Enums; -using JiShe.CollectBus.Common.Helpers; using JiShe.CollectBus.Common.Models; using JiShe.CollectBus.Interceptors; using JiShe.CollectBus.IoTDB.Interface; @@ -8,7 +7,8 @@ using JiShe.CollectBus.IotSystems.MessageReceiveds; using JiShe.CollectBus.IotSystems.MeterReadingRecords; using JiShe.CollectBus.Kafka.Attributes; using JiShe.CollectBus.Kafka.Internal; -using JiShe.CollectBus.Protocol.Contracts.Interfaces; +using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.CollectBus.Protocol3761; using JiShe.CollectBus.Repository.MeterReadingRecord; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -103,7 +103,7 @@ namespace JiShe.CollectBus.Subscribers } [KafkaSubscribe(ProtocolConst.SubscriberReceivedEventName)] - public async Task ReceivedEvent(MessageReceived receivedMessage) + public async Task ReceivedEvent(MessageProtocolAnalysis receivedMessage) { var currentTime = Clock.Now; @@ -115,24 +115,24 @@ namespace JiShe.CollectBus.Subscribers else { //todo 会根据不同的协议进行解析,然后做业务处理 - TB3761? tB3761 = protocolPlugin.Analysis3761(receivedMessage.MessageHexString); - if (tB3761 == null) - { - Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}"); - return SubscribeAck.Success(); - } - if (tB3761.DT == null || tB3761.AFN_FC == null) - { - Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}"); - return SubscribeAck.Success(); - } + //TB3761? tB3761 = protocolPlugin.Analysis3761(receivedMessage.MessageHexString); + //if (tB3761 == null) + //{ + // Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}"); + // return SubscribeAck.Success(); + //} + //if (tB3761.DT == null || tB3761.AFN_FC == null) + //{ + // Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}"); + // return SubscribeAck.Success(); + //} //报文入库 var entity = new MeterReadingRecords() { ReceivedMessageHexString = receivedMessage.MessageHexString, - AFN = (AFN)tB3761.AFN_FC.AFN, - Fn = tB3761.DT.Fn, + AFN = (AFN)receivedMessage.Data?.AFN_FC.AFN!, + Fn = receivedMessage.Data.DT.Fn, Pn = 0, FocusAddress = "", MeterAddress = "", diff --git a/services/JiShe.CollectBus.Domain/JiShe.CollectBus.Domain.csproj b/services/JiShe.CollectBus.Domain/JiShe.CollectBus.Domain.csproj index c5f49bc..a60ffe1 100644 --- a/services/JiShe.CollectBus.Domain/JiShe.CollectBus.Domain.csproj +++ b/services/JiShe.CollectBus.Domain/JiShe.CollectBus.Domain.csproj @@ -34,8 +34,4 @@ - - - - diff --git a/protocols/JiShe.CollectBus.Protocol.T37612012/Models/TB3761.cs b/services/JiShe.CollectBus.Domain/Protocol3761/TB3761.cs similarity index 98% rename from protocols/JiShe.CollectBus.Protocol.T37612012/Models/TB3761.cs rename to services/JiShe.CollectBus.Domain/Protocol3761/TB3761.cs index 75727d2..d5e647f 100644 --- a/protocols/JiShe.CollectBus.Protocol.T37612012/Models/TB3761.cs +++ b/services/JiShe.CollectBus.Domain/Protocol3761/TB3761.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace JiShe.CollectBus.Protocol.Contracts.Models +namespace JiShe.CollectBus.Protocol3761 { /// @@ -238,10 +238,7 @@ namespace JiShe.CollectBus.Protocol.Contracts.Models /// /// 数据单元标识和数据单元格式 /// - public class UnitData: BaseHexMessage - { - - } + public class UnitData: BaseHexMessage{ } #endregion