diff --git a/Directory.Build.props b/Directory.Build.props index 0631838..d7090a1 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ - 1.0.5.23 + 1.0.5.27 9.1.1 diff --git a/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs b/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs index 6a89b38..8eb9819 100644 --- a/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs +++ b/services/JiShe.CollectBus.Application/Samples/SampleAppService.cs @@ -1,10 +1,14 @@ -using JiShe.CollectBus.Ammeters; +using Confluent.Kafka; +using JiShe.CollectBus.Ammeters; +using JiShe.CollectBus.Common.BuildSendDatas; using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.Extensions; using JiShe.CollectBus.Common.Helpers; +using JiShe.CollectBus.Common.Models; using JiShe.CollectBus.IotSystems.Devices; using JiShe.CollectBus.IotSystems.PrepayModel; using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.ServicePro.Core; using JiShe.ServicePro.FreeSqlProvider; using JiShe.ServicePro.Kafka.Internal; using JiShe.ServicePro.Kafka.Producer; @@ -14,6 +18,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using TouchSocket.Sockets; +using static FreeSql.Internal.GlobalFilter; namespace JiShe.CollectBus.Samples; @@ -26,8 +31,10 @@ public class SampleAppService : CollectBusAppService, ISampleAppService, IKafkaS private readonly IProducerService _producerService; private readonly ITcpService _tcpService; + + private readonly IFreeRedisProvider _freeRedisProvider; public SampleAppService(IoTDBSessionPoolProvider iotDBProvider, IOptions options, - ILogger logger, IRedisDataCacheService redisDataCacheService, IProducerService producerService, ITcpService tcpService) + ILogger logger, IRedisDataCacheService redisDataCacheService, IProducerService producerService, ITcpService tcpService, IFreeRedisProvider freeRedisProvider) { _iotDBProvider = iotDBProvider; _options = options.Value; @@ -35,6 +42,7 @@ public class SampleAppService : CollectBusAppService, ISampleAppService, IKafkaS _redisDataCacheService = redisDataCacheService; _producerService = producerService; _tcpService = tcpService; + _freeRedisProvider = freeRedisProvider; } /// @@ -400,5 +408,40 @@ public class SampleAppService : CollectBusAppService, ISampleAppService, IKafkaS await Task.CompletedTask; } + + + /// + /// 设置集中器采集项 + /// + /// + /// + /// 值可选(15,5) + /// + /// + [HttpPost] + public async Task AutoCollectionItemsSet(string focusAddress, string meterAddress,int cycle) + { + var key = $"{RedisConst.CacheAllDeviceInfoHashKey}"; + List deviceCacheInfos= await _freeRedisProvider.Instance.HGetAsync>(key, focusAddress); + if (deviceCacheInfos == null || deviceCacheInfos.Count <= 0) + { + throw new Exception($"未能在缓存中找到设备{focusAddress},缓存key:{key}"); + } + DeviceCacheInfo? deviceCacheInfo = deviceCacheInfos.FirstOrDefault(d => d.MeterAddress == meterAddress); + if (deviceCacheInfo == null) { + throw new Exception($"未能在缓存中找到设备{focusAddress}下的表{meterAddress},缓存key:{key}"); + } + + var details = deviceCacheInfo.ItemCodes.Where(n => n.Trim().Substring(0, 3) == "0D_").Select(it => new PnFn(1, Convert.ToInt32(it.Split('_')[1]))).ToList(); + var bytes = Build3761SendData.BuildAmmeterReportCollectionItemsSetSendCmd(focusAddress, 1, 0, cycle, DateTime.Now,1, details); + + string frame = bytes.ToHexString(); + await _producerService.ProduceAsync(KafkaTopicConsts.TESTSENDTOPIC, new KafkaSendDto() { + Address = focusAddress, + Frame = frame + }); + + return frame; + } } diff --git a/services/JiShe.CollectBus.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs b/services/JiShe.CollectBus.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs index 8ee60e6..22d6820 100644 --- a/services/JiShe.CollectBus.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs +++ b/services/JiShe.CollectBus.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs @@ -1,4 +1,9 @@ -using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.CollectBus.Common.BuildSendDatas; +using JiShe.CollectBus.Common.Models; +using JiShe.CollectBus.Protocol.Interfaces; +using JiShe.CollectBus.Samples; +using JiShe.ServicePro.Dto; +using JiShe.ServicePro.FreeRedisProvider; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -16,19 +21,21 @@ namespace JiShe.CollectBus.Subscribers private readonly IServiceProvider _serviceProvider; private readonly IoTDBSessionPoolProvider _dbProvider; private readonly IProtocolService _protocolService; - + private readonly IFreeRedisProvider _freeRedisProvider; public ServiceCommunicationChannelSubscriberService(ILogger logger, ITcpService tcpService, IServiceProvider serviceProvider, IoTDBSessionPoolProvider dbProvider, - IProtocolService protocolService) + IProtocolService protocolService, + IFreeRedisProvider freeRedisProvider) { _logger = logger; _tcpService = tcpService; _serviceProvider = serviceProvider; _dbProvider = dbProvider; _protocolService = protocolService; + _freeRedisProvider = freeRedisProvider; } @@ -44,7 +51,7 @@ namespace JiShe.CollectBus.Subscribers var tempFirstKeyInfo = issuedMessage.FirstOrDefault(); if (tempFirstKeyInfo.Value == null) { - return SubscribeAck.Fail(); + return SubscribeAck.Success(); } ServiceCommunicationTypeEnum serviceCommunication = tempFirstKeyInfo.Key; @@ -54,8 +61,31 @@ namespace JiShe.CollectBus.Subscribers case ServiceCommunicationTypeEnum.ArchivalDataIssued: if(!string.IsNullOrWhiteSpace(tempFirstKeyInfo.Value)) { - FocusCacheInfos focusCacheInfo = tempFirstKeyInfo.Value.Deserialize(); - tempResult = await SendArchivalDataIssued(focusCacheInfo); + // 解析数据 + // 判断电表,水表,气表 下发档案 + ArchivalDataIssuedInput? input = tempFirstKeyInfo.Value.Deserialize(); + if (input != null) + { + switch (input.MeterType) + { + case MeterTypeEnum.Ammeter: + + break; + case MeterTypeEnum.WaterMeter: + + break; + } + } + //tempResult = await SendArchivalDataIssued(focusCacheInfo); + } + break; + case ServiceCommunicationTypeEnum.SetItemCodeTask: + if(!string.IsNullOrWhiteSpace(tempFirstKeyInfo.Value)){ + SetItemCodeTaskInput? input = tempFirstKeyInfo.Value!.Deserialize(); + if(input != null) + { + await AutoCollectionItemsSetAsync(input); + } } break; default: @@ -87,5 +117,55 @@ namespace JiShe.CollectBus.Subscribers throw; } } + + + /// + /// 自动采集项目设置 + /// + /// + /// + protected async Task AutoCollectionItemsSetAsync(SetItemCodeTaskInput input) + { + try + { + var key = $"{RedisConst.CacheAllDeviceInfoHashKey}"; + List deviceCacheInfos = await _freeRedisProvider.Instance.HGetAsync>(key, input.FocusAddress); + if (deviceCacheInfos == null || deviceCacheInfos.Count <= 0) + { + throw new Exception($"未能在缓存中找到设备{input.FocusAddress},缓存key:{key}"); + } + DeviceCacheInfo? deviceCacheInfo = deviceCacheInfos.FirstOrDefault(d => d.MeterAddress == input.MeterAddress); + if (deviceCacheInfo == null) + { + throw new Exception($"未能在缓存中找到设备{input.FocusAddress}下的表{input.MeterAddress},缓存key:{key}"); + } + + var details = deviceCacheInfo.ItemCodes.Where(n => n.Trim().Substring(0, 3) == "0D_").Select(it => new PnFn(1, Convert.ToInt32(it.Split('_')[1]))).ToList(); + var bytes = Build3761SendData.BuildAmmeterReportCollectionItemsSetSendCmd(input.FocusAddress, 1, 0, input.Cycle, DateTime.Now, 1, details); + + string frame = bytes.ToHexString(); + + // TODO: 需要插入iotdb 日志 + + + var checkResult = _tcpService.ClientExists(input.FocusAddress); + if (checkResult) + { + await _tcpService.SendAsync(input.FocusAddress, bytes); + + return true; + } + else + { + return false; + } + } + catch (Exception) + { + throw; + } + + + } } } diff --git a/shared/JiShe.CollectBus.Common/BuildSendDatas/Build3761SendData.cs b/shared/JiShe.CollectBus.Common/BuildSendDatas/Build3761SendData.cs index 2d815da..b77ea2f 100644 --- a/shared/JiShe.CollectBus.Common/BuildSendDatas/Build3761SendData.cs +++ b/shared/JiShe.CollectBus.Common/BuildSendDatas/Build3761SendData.cs @@ -1731,7 +1731,7 @@ namespace JiShe.CollectBus.Common.BuildSendDatas list.AddRange(dA); list.AddRange(dT); - if (dataUnit != null) + if (dataUnit != null && dataUnit.Count > 0) { list.AddRange(dataUnit); } diff --git a/web/JiShe.CollectBus.Host/CollectBusHostModule.cs b/web/JiShe.CollectBus.Host/CollectBusHostModule.cs index 707dfcb..c1155ba 100644 --- a/web/JiShe.CollectBus.Host/CollectBusHostModule.cs +++ b/web/JiShe.CollectBus.Host/CollectBusHostModule.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Hosting.Internal; using Swashbuckle.AspNetCore.SwaggerUI; using Volo.Abp; using Volo.Abp.AspNetCore.Authentication.JwtBearer; +using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.Autofac; using Volo.Abp.Caching.StackExchangeRedis; @@ -57,6 +58,13 @@ namespace JiShe.CollectBus.Host .AddDataProtection() .PersistKeysToStackFreeRedis("JiSheCollectBus-Protection-Keys"); + // DOTO:abp swagger调用抽风 提示 The antiforgery token could not be decrypted. + Configure(options => + { + options.TokenCookie.Expiration = TimeSpan.Zero; + options.AutoValidate = false; //表示不验证防伪令牌 + }); + }