接收业务系统获取属性指令信息优化

This commit is contained in:
ChenYi 2026-02-06 11:07:59 +08:00
parent 7a5479f646
commit 295baad480
2 changed files with 39 additions and 17 deletions

View File

@ -14,7 +14,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
public interface IBusinessSystemAggregationService : IApplicationService public interface IBusinessSystemAggregationService : IApplicationService
{ {
/// <summary> /// <summary>
/// 接收业务系统设置指令信息Msg 字段为 ReceiveCommandInfoDto 实体 /// 接收业务系统设置指令信息Msg 字段为 ReceiveCommandInfoDto 实体,操作类型必须有对应的事件上报,进行业务系统回调触发,不能直接使用同步操作。
/// </summary> /// </summary>
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>

View File

@ -73,7 +73,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
return redis.call('HMGET', hashKey, unpack(fieldKeys))"; return redis.call('HMGET', hashKey, unpack(fieldKeys))";
/// <summary> /// <summary>
/// 接收业务系统设置指令信息 /// 接收业务系统设置指令信息Msg 字段为 ReceiveCommandInfoDto 实体,操作类型必须有对应的事件上报,进行业务系统回调触发,不能直接使用同步操作。
/// </summary> /// </summary>
[AllowAnonymous] [AllowAnonymous]
public async Task<HttpDataResult> ReceiveSetCommandInfoAsync(OpenApiRequest input) public async Task<HttpDataResult> ReceiveSetCommandInfoAsync(OpenApiRequest input)
@ -116,9 +116,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
{ {
return HttpDataResultExtensions.Failed("设备不存在", -1041, ResponeResultEnum.Fail); return HttpDataResultExtensions.Failed("设备不存在", -1041, ResponeResultEnum.Fail);
} }
var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, input, deviceInfo.Adapt<DeviceCacheInfos>(), messageBody.Commands.Serialize());
//将指令存储IoTDB数据库和Redis发布通道 //将指令存储IoTDB数据库和Redis发布通道
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET) if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{ {
@ -130,7 +128,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
{ {
throw new UserFriendlyException($"业务系统推送指令时设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。"); throw new UserFriendlyException($"业务系统推送指令时设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。");
} }
Dictionary<string, object> commands = new Dictionary<string, object>();
foreach (var item in messageBody.Commands) foreach (var item in messageBody.Commands)
{ {
var tempPlatformThingModelInfo = platformThingModelInfo.Where(d => d.IoTPlatformRawFieldName == item.Key).FirstOrDefault(); 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); messageBody.Commands.RemoveAll(d => d.Key == item.Key);
continue; continue;
} }
commands.Add(tempPlatformThingModelInfo.IoTPlatformRawFieldName, item.Value);
} }
if (commands == null || commands.Count <= 0)
{
_logger.LogError($"业务系统推送设置属性指令时设备{deviceInfo.DeviceAddress}未匹配到具体数据模型,被禁止。");
return HttpDataResultExtensions.Failed<Dictionary<string, object>>("设置属性失败,未匹配到具体数据模型", -108);
}
var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, input, deviceInfo.Adapt<DeviceCacheInfos>(), commands.Serialize());
//数据写入遥测任务数据存储通道 //数据写入遥测任务数据存储通道
await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo)); await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo));
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, input); await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, input);
} }
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing) else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
@ -169,7 +176,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
} }
else else
{ {
return HttpDataResultExtensions.Failed("指令处理失败,当前设备平台类型异常", -105); return HttpDataResultExtensions.Failed("设置属性指令处理失败,当前设备平台类型异常", -105);
} }
return HttpDataResultExtensions.Success("指令下发成功"); return HttpDataResultExtensions.Success("指令下发成功");
@ -199,11 +206,20 @@ namespace JiShe.IoT.BusinessSystemAggregation
return HttpDataResultExtensions.Failed<Dictionary<string, object>>("获取数据失败,签名校验异常", -101); return HttpDataResultExtensions.Failed<Dictionary<string, object>>("获取数据失败,签名校验异常", -101);
} }
var messageBody = handleResult.Data; 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<Dictionary<string, object>>("获取数据失败,设备属性值不能为空", -103, ResponeResultEnum.Fail); return HttpDataResultExtensions.Failed<Dictionary<string, object>>("获取数据失败,设备属性值不能为空", -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<Dictionary<string, object>>("获取数据失败,设备请求被限制", -103, ResponeResultEnum.Fail);
}
FreeRedisProvider.Instance.Set(restrictionKey,"1",TimeSpan.FromSeconds(5));
//限定来源类型必须为业务系统 //限定来源类型必须为业务系统
if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem) if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem)
{ {
@ -235,7 +251,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
return HttpDataResultExtensions.Failed<Dictionary<string, object>>($"业务系统推送指令时设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。", -107, ResponeResultEnum.Fail); return HttpDataResultExtensions.Failed<Dictionary<string, object>>($"业务系统推送指令时设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。", -107, ResponeResultEnum.Fail);
} }
Dictionary<string, object> commands = new Dictionary<string, object>(); Dictionary<string, string> commands = new Dictionary<string, string>();
foreach (var item in messageBody.Commands) foreach (var item in messageBody.Commands)
{ {
var tempPlatformThingModelInfo = platformThingModelInfo.Where(d => d.StandardFieldName == item.Key).FirstOrDefault(); 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); messageBody.Commands.RemoveAll(d => d.Key == item.Key);
continue; continue;
} }
commands.Add(tempPlatformThingModelInfo.IoTPlatformRawFieldName,item.Key);
commands.Add(item.Key,item.Value);
} }
if (commands == null || commands.Count <= 0) if (commands == null || commands.Count <= 0)
@ -283,7 +298,13 @@ namespace JiShe.IoT.BusinessSystemAggregation
return HttpDataResultExtensions.Failed<Dictionary<string, object>>("获取数据失败OneNET平台未返回数据", -109); return HttpDataResultExtensions.Failed<Dictionary<string, object>>("获取数据失败OneNET平台未返回数据", -109);
} }
return HttpDataResultExtensions.Success<Dictionary<string, object>>(queryResult); Dictionary<string, object> result = new Dictionary<string, object>();
foreach (var item in commands)
{
result.Add(item.Value, queryResult[item.Key]);
}
return HttpDataResultExtensions.Success<Dictionary<string, object>>(result);
} }
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing) else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
@ -297,14 +318,15 @@ namespace JiShe.IoT.BusinessSystemAggregation
return HttpDataResultExtensions.Success<Dictionary<string, object>>("指令下发成功"); return HttpDataResultExtensions.Success<Dictionary<string, object>>("指令下发成功");
} }
catch (UserFriendlyException) catch (UserFriendlyException ex)
{ {
throw; // 重新抛出用户友好异常 _logger.LogError(ex, "接收业务系统指令信息时发生异常");
return HttpDataResultExtensions.Failed<Dictionary<string, object>>("设备属性数据获取失败,发送异常", -111);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "接收业务系统指令信息时发生异常"); _logger.LogError(ex, "接收业务系统指令信息时发生异常");
return HttpDataResultExtensions.Failed<Dictionary<string, object>>("指令处理失败,发送异常", -111); return HttpDataResultExtensions.Failed<Dictionary<string, object>>("设备属性数据获取失败,发送异常", -112);
} }
} }