From 295baad480338a25c354266f9ee3138e5f5e005c Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Fri, 6 Feb 2026 11:07:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E6=94=B6=E4=B8=9A=E5=8A=A1=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E8=8E=B7=E5=8F=96=E5=B1=9E=E6=80=A7=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IBusinessSystemAggregationService.cs | 2 +- .../BusinessSystemAggregationService.cs | 54 +++++++++++++------ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs index 8f25a52..4b9cc54 100644 --- a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs +++ b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs @@ -14,7 +14,7 @@ namespace JiShe.IoT.BusinessSystemAggregation public interface IBusinessSystemAggregationService : IApplicationService { /// - /// 接收业务系统设置指令信息,Msg 字段为 ReceiveCommandInfoDto 实体 + /// 接收业务系统设置指令信息,Msg 字段为 ReceiveCommandInfoDto 实体,操作类型必须有对应的事件上报,进行业务系统回调触发,不能直接使用同步操作。 /// /// /// diff --git a/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs index 1ecb907..b09099b 100644 --- a/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs +++ b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs @@ -73,7 +73,7 @@ namespace JiShe.IoT.BusinessSystemAggregation return redis.call('HMGET', hashKey, unpack(fieldKeys))"; /// - /// 接收业务系统设置指令信息 + /// 接收业务系统设置指令信息,Msg 字段为 ReceiveCommandInfoDto 实体,操作类型必须有对应的事件上报,进行业务系统回调触发,不能直接使用同步操作。 /// [AllowAnonymous] public async Task ReceiveSetCommandInfoAsync(OpenApiRequest input) @@ -116,9 +116,7 @@ namespace JiShe.IoT.BusinessSystemAggregation { return HttpDataResultExtensions.Failed("设备不存在", -1041, ResponeResultEnum.Fail); } - - var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, input, deviceInfo.Adapt(), messageBody.Commands.Serialize()); - + //将指令存储IoTDB数据库和Redis发布通道 if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET) { @@ -130,7 +128,7 @@ namespace JiShe.IoT.BusinessSystemAggregation { throw new UserFriendlyException($"业务系统推送指令时设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。"); } - + Dictionary commands = new Dictionary(); foreach (var item in messageBody.Commands) { var tempPlatformThingModelInfo = platformThingModelInfo.Where(d => d.IoTPlatformRawFieldName == item.Key).FirstOrDefault(); @@ -155,12 +153,21 @@ namespace JiShe.IoT.BusinessSystemAggregation messageBody.Commands.RemoveAll(d => d.Key == item.Key); continue; } + commands.Add(tempPlatformThingModelInfo.IoTPlatformRawFieldName, item.Value); } + if (commands == null || commands.Count <= 0) + { + _logger.LogError($"业务系统推送设置属性指令时设备{deviceInfo.DeviceAddress}未匹配到具体数据模型,被禁止。"); + return HttpDataResultExtensions.Failed>("设置属性失败,未匹配到具体数据模型", -108); + } + + + var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, input, deviceInfo.Adapt(), commands.Serialize()); + //数据写入遥测任务数据存储通道 await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo)); - await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, input); } else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing) @@ -169,7 +176,7 @@ namespace JiShe.IoT.BusinessSystemAggregation } else { - return HttpDataResultExtensions.Failed("指令处理失败,当前设备平台类型异常", -105); + return HttpDataResultExtensions.Failed("设置属性指令处理失败,当前设备平台类型异常", -105); } return HttpDataResultExtensions.Success("指令下发成功"); @@ -199,11 +206,20 @@ namespace JiShe.IoT.BusinessSystemAggregation return HttpDataResultExtensions.Failed>("获取数据失败,签名校验异常", -101); } var messageBody = handleResult.Data; - if (messageBody == null || messageBody.Commands == null || messageBody.Commands.Count <= 0) + if (messageBody == null || messageBody.Commands == null || messageBody.Commands.Count <= 0 || string.IsNullOrWhiteSpace(messageBody.DeviceAddress)) { - return HttpDataResultExtensions.Failed>("获取数据失败,设备属性值不能为空", -103, ResponeResultEnum.Fail); + return HttpDataResultExtensions.Failed>("获取数据失败,设备属性值不能为空", -102, ResponeResultEnum.Fail); } + var restrictionKey = string.Format(RedisConst.CacheDeviceOpenApiRestrictionKey,nameof(ReceiveGetCommandInfoAsync), messageBody.DeviceAddress); + var openApiDeviceRequest = FreeRedisProvider.Instance.Get(restrictionKey); + if (openApiDeviceRequest != null) + { + return HttpDataResultExtensions.Failed>("获取数据失败,设备请求被限制", -103, ResponeResultEnum.Fail); + } + + FreeRedisProvider.Instance.Set(restrictionKey,"1",TimeSpan.FromSeconds(5)); + //限定来源类型必须为业务系统 if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem) { @@ -235,7 +251,7 @@ namespace JiShe.IoT.BusinessSystemAggregation return HttpDataResultExtensions.Failed>($"业务系统推送指令时设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。", -107, ResponeResultEnum.Fail); } - Dictionary commands = new Dictionary(); + Dictionary commands = new Dictionary(); foreach (var item in messageBody.Commands) { var tempPlatformThingModelInfo = platformThingModelInfo.Where(d => d.StandardFieldName == item.Key).FirstOrDefault(); @@ -260,8 +276,7 @@ namespace JiShe.IoT.BusinessSystemAggregation messageBody.Commands.RemoveAll(d => d.Key == item.Key); continue; } - - commands.Add(item.Key,item.Value); + commands.Add(tempPlatformThingModelInfo.IoTPlatformRawFieldName,item.Key); } if (commands == null || commands.Count <= 0) @@ -283,7 +298,13 @@ namespace JiShe.IoT.BusinessSystemAggregation return HttpDataResultExtensions.Failed>("获取数据失败,OneNET平台未返回数据", -109); } - return HttpDataResultExtensions.Success>(queryResult); + Dictionary result = new Dictionary(); + foreach (var item in commands) + { + result.Add(item.Value, queryResult[item.Key]); + } + + return HttpDataResultExtensions.Success>(result); } else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing) @@ -297,14 +318,15 @@ namespace JiShe.IoT.BusinessSystemAggregation return HttpDataResultExtensions.Success>("指令下发成功"); } - catch (UserFriendlyException) + catch (UserFriendlyException ex) { - throw; // 重新抛出用户友好异常 + _logger.LogError(ex, "接收业务系统指令信息时发生异常"); + return HttpDataResultExtensions.Failed>("设备属性数据获取失败,发送异常", -111); } catch (Exception ex) { _logger.LogError(ex, "接收业务系统指令信息时发生异常"); - return HttpDataResultExtensions.Failed>("指令处理失败,发送异常", -111); + return HttpDataResultExtensions.Failed>("设备属性数据获取失败,发送异常", -112); } }