2025-08-22 09:12:19 +08:00
|
|
|
|
using JiShe.ServicePro;
|
2025-08-04 17:29:47 +08:00
|
|
|
|
using JiShe.ServicePro.Core;
|
|
|
|
|
|
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
|
|
|
|
|
|
using JiShe.ServicePro.Dto;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using JiShe.ServicePro.Encrypt;
|
|
|
|
|
|
using JiShe.ServicePro.Enums;
|
2025-08-21 11:34:16 +08:00
|
|
|
|
using JiShe.ServicePro.FreeRedisProvider;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.IoT.BusinessSystemAggregation
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 业务系统聚合服务
|
|
|
|
|
|
/// </summary>
|
2025-08-21 11:34:16 +08:00
|
|
|
|
public class BusinessSystemAggregationService(IOptions<ServerApplicationOptions> options, IReliableRedisPubSubService redisPubSubService, IDeviceAppService deviceAppService) : IoTAppService, IBusinessSystemAggregationService
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
ServerApplicationOptions srverOptions = options.Value;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 接收业务系统指令信息,缓存进Kafka
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public async Task<HttpDataResult> ReceiveCommandInfoAsync(OpenApiRequest input)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-21 11:34:16 +08:00
|
|
|
|
bool verifySignatureReult = EncryptUtil.OpenApiVerifySignature(input.Message, input.Nonce, input.Timestamp, input.Signature, srverOptions.SignatureToken);
|
2025-07-24 17:06:54 +08:00
|
|
|
|
if (verifySignatureReult == false)//签名校验失败
|
|
|
|
|
|
{
|
2025-08-04 17:29:47 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed("签名校验失败", -101, ResponeResultEnum.NotAllowed);
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.Message))
|
|
|
|
|
|
{
|
2025-08-04 17:29:47 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed("指令下发内容不能为空", -102, ResponeResultEnum.Fail);
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-21 11:34:16 +08:00
|
|
|
|
//判断是否需要解密
|
|
|
|
|
|
ReceiveCommandInfoDto messageBody = null;
|
|
|
|
|
|
if (srverOptions.IsAesEncrypted && !string.IsNullOrWhiteSpace(srverOptions.AesSecurityKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
string tempRaw = EncryptUtil.OpenApiDecrypto(input.Message, srverOptions.AesSecurityKey);
|
|
|
|
|
|
messageBody = tempRaw.Deserialize<ReceiveCommandInfoDto>();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
messageBody = input.Message.Deserialize<ReceiveCommandInfoDto>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (messageBody == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed("指令下发内容不能为空", -103, ResponeResultEnum.Fail);
|
|
|
|
|
|
}
|
2025-08-04 17:29:47 +08:00
|
|
|
|
|
|
|
|
|
|
//限定来源类型必须为业务系统
|
|
|
|
|
|
if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem)
|
|
|
|
|
|
{
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed("设备指令来源类型错误,业务系统传固定值2", -104, ResponeResultEnum.Fail);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(messageBody.DeviceAddress);
|
|
|
|
|
|
|
|
|
|
|
|
//将指令存储Kafka的OneNET主题中
|
|
|
|
|
|
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
|
|
|
|
|
|
{
|
2025-08-22 17:32:04 +08:00
|
|
|
|
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, input);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
|
|
|
|
|
|
{
|
2025-08-22 17:32:04 +08:00
|
|
|
|
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName, input);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed("指令处理失败,当前设备平台类型异常", -105);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
2025-07-24 17:06:54 +08:00
|
|
|
|
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Success("指令下发成功");
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
2025-08-21 11:34:16 +08:00
|
|
|
|
catch (Exception ex)
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed($"指令处理失败,发送异常:{ex.Message}", -106);
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|