优化设备指令下发处理逻辑

This commit is contained in:
ChenYi 2025-08-04 17:29:47 +08:00
parent e99e853db3
commit f30074d7d9
7 changed files with 129 additions and 9 deletions

@ -1 +1 @@
Subproject commit f038a3c4239c680b512523d7b777c7e73cbc1ebf
Subproject commit 7e17249fcd7a289a03a75d2c59ddefd4b165a36a

View File

@ -21,7 +21,7 @@ namespace JiShe.IoT.DeviceAggregation.Dto
public IoTPlatformTypeEnum IoTPlatform { get; set; }
/// <summary>
/// 集中器在物联网平台中对应的产品Id
/// 设备在物联网平台中对应的产品Id
/// </summary>
[Required(ErrorMessage = "产品Id不能为空")]
public string IoTPlatformProductId { get; set; }

View File

@ -0,0 +1,36 @@
using JiShe.ServicePro.Enums;
using System.ComponentModel.DataAnnotations;
namespace JiShe.IoT.DeviceAggregation
{
/// <summary>
/// 设备命令
/// </summary>
public class DeviceCommandForApiInput
{
/// <summary>
/// 表通信地址
/// </summary>
[Required(ErrorMessage = "设备地址不能为空")]
public string DeviceAddress { get; set; }
/// <summary>
/// 物联网平台类型
/// </summary>
[Required(ErrorMessage = "物联网平台类型不能为空")]
public IoTPlatformTypeEnum IoTPlatform { get; set; }
/// <summary>
/// 设备在物联网平台中对应的产品Id
/// </summary>
[Required(ErrorMessage = "产品Id不能为空")]
public string IoTPlatformProductId { get; set; }
/// <summary>
/// 设备在物联网平台中发送的命令内容JSON格式
/// </summary>
[Required(ErrorMessage = "命令内容不能为空")]
public string CommandContent { get; set; }
}
}

View File

@ -38,6 +38,13 @@ namespace JiShe.IoT.DeviceAggregation
/// <returns></returns>
Task<bool> BatchCreateDeviceWorkshopAsync(BatchCreateDeviceAggregationInput input);
/// <summary>
/// 发送设备指令信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<bool> DeviceCommandForApiAsync(DeviceCommandForApiInput input);
/// <summary>
/// 删除设备信息
/// </summary>

View File

@ -43,4 +43,8 @@
<PackageReference Include="JiShe.ServicePro.OneNETManagement.Application.Contracts" />
</ItemGroup>
<ItemGroup>
<Folder Include="BusinessSystemAggregation\Dto\" />
</ItemGroup>
</Project>

View File

@ -1,5 +1,8 @@
using JiShe.IoT.OneNETAggregation.Dto;
using JiShe.ServicePro;
using JiShe.ServicePro.Core;
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
using JiShe.ServicePro.Dto;
using JiShe.ServicePro.Encrypt;
using JiShe.ServicePro.Enums;
using JiShe.ServicePro.Kafka.Consts;
@ -18,7 +21,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
/// <summary>
/// 业务系统聚合服务
/// </summary>
public class BusinessSystemAggregationService(IOptions<ServerApplicationOptions> options, IKafkaProducerService _producerService) :IoTAppService, IBusinessSystemAggregationService
public class BusinessSystemAggregationService(IOptions<ServerApplicationOptions> options, IKafkaProducerService _producerService, IDeviceAppService deviceAppService) : IoTAppService, IBusinessSystemAggregationService
{
ServerApplicationOptions srverOptions = options.Value;
@ -34,16 +37,38 @@ namespace JiShe.IoT.BusinessSystemAggregation
bool verifySignatureReult = EncryptUtil.OpenApiVerifySignature(input.Message, input.Nonce, input.Timestamp, input.Signature, srverOptions.VerifySignatureToken);
if (verifySignatureReult == false)//签名校验失败
{
return HttpDataResultExtensions.Failed<List<OneNetWorkshopProductListOutput>>("签名校验失败", -101, ResponeResultEnum.NotAllowed);
return HttpDataResultExtensions.Failed("签名校验失败", -101, ResponeResultEnum.NotAllowed);
}
if (string.IsNullOrWhiteSpace(input.Message))
{
return HttpDataResultExtensions.Failed<List<OneNetWorkshopProductListOutput>>("指令下发内容不能为空", -102, ResponeResultEnum.Fail);
return HttpDataResultExtensions.Failed("指令下发内容不能为空", -102, ResponeResultEnum.Fail);
}
//将指令存储Kafka中
//查询设备信息,判断设备在哪个平台
var messageBody = input.Message.Deserialize<ReceiveCommandInfoDto>();
//限定来源类型必须为业务系统
if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem)
{
return HttpDataResultExtensions.Failed("设备指令来源类型错误业务系统传固定值2", -103, ResponeResultEnum.Fail);
}
var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(messageBody.DeviceAddress);
//将指令存储Kafka的OneNET主题中
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
await _producerService.ProduceAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, $"{GuidGenerator.Create()}", input);
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
await _producerService.ProduceAsync(KafkaTopicConsts.CTWingAepCommandIssuedEventName, $"{GuidGenerator.Create()}", input);
}
else
{
return HttpDataResultExtensions.Failed("指令处理失败,当前设备平台类型异常",-104);
}
return HttpDataResultExtensions.Success("指令下发Kafka成功");
}

View File

@ -4,8 +4,11 @@ using JiShe.ServicePro.Core;
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
using JiShe.ServicePro.DeviceManagement.DeviceInfos.Dto;
using JiShe.ServicePro.DeviceManagement.Permissions;
using JiShe.ServicePro.Dto;
using JiShe.ServicePro.Enums;
using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.Kafka.Consts;
using JiShe.ServicePro.Kafka.Producer;
using JiShe.ServicePro.OneNET.Provider.OpenApiModels.Devices;
using JiShe.ServicePro.OneNETManagement.OneNETDevices;
using JiShe.ServicePro.OneNETManagement.OneNETProducts;
@ -21,7 +24,8 @@ namespace JiShe.IoT.DeviceAggregation
/// <param name="logger"></param>
/// <param name="deviceAppService">设备服务</param>
/// <param name="oneNETDeviceService">OneNET设备服务</param>
public class DeviceAggregationService(ILogger<DeviceAggregationService> logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService) : IoTAppService, IDeviceAggregationService
/// <param name="producerService">KafKa生产服务</param>
public class DeviceAggregationService(ILogger<DeviceAggregationService> logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IKafkaProducerService producerService) : IoTAppService, IDeviceAggregationService
{
/// <summary>
/// 管理后台创建设备信息
@ -266,6 +270,50 @@ namespace JiShe.IoT.DeviceAggregation
return input.Adapt<DeviceManagementInfoDto>();
}
/// <summary>
/// 发送设备指令信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<bool> DeviceCommandForApiAsync(DeviceCommandForApiInput input)
{
try
{
var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(input.DeviceAddress);
//将指令存储Kafka的OneNET主题中
var commandRequest = new OpenApiRequest()
{
Message = new ReceiveCommandInfoDto()
{
DeviceAddress = input.DeviceAddress,
Commands = input.CommandContent.Deserialize<Dictionary<string,object>>(),
DeviceType = DeviceTypeEnum.Focus,//todo 设备类型 需要跟设备统一什么情况下知道具体设备类型
SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
}.Serialize(),
};
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
await producerService.ProduceAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, $"{GuidGenerator.Create()}", commandRequest);
return true;
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
await producerService.ProduceAsync(KafkaTopicConsts.CTWingAepCommandIssuedEventName, $"{GuidGenerator.Create()}", commandRequest);
return true;
}
else
{
throw new UserFriendlyException($"创建设备失败,未找到对应的产品配置信息。");
}
}
catch (Exception)
{
throw;
}
}
#region OneNET
/// <summary>
@ -465,7 +513,7 @@ namespace JiShe.IoT.DeviceAggregation
try
{
var productInfo = await FreeSqlDbContext.Instance.Select<OneNETProductInfos>()
.Where(e => e.IsEnabled == true && e.IoTPlatformProductId == input.IoTPlatformProductId)
.Where(e => e.IoTPlatformProductId == input.IoTPlatformProductId)
.FirstAsync();
if (productInfo == null)