87 lines
3.7 KiB
C#
87 lines
3.7 KiB
C#
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.FreeRedisProvider;
|
||
using Microsoft.Extensions.Options;
|
||
|
||
namespace JiShe.IoT.BusinessSystemAggregation
|
||
{
|
||
/// <summary>
|
||
/// 业务系统聚合服务
|
||
/// </summary>
|
||
public class BusinessSystemAggregationService(IOptions<ServerApplicationOptions> options, IReliableRedisPubSubService redisPubSubService, IDeviceAppService deviceAppService) : IoTAppService, IBusinessSystemAggregationService
|
||
{
|
||
ServerApplicationOptions srverOptions = options.Value;
|
||
|
||
/// <summary>
|
||
/// 接收业务系统指令信息,缓存进Kafka
|
||
/// </summary>
|
||
[AllowAnonymous]
|
||
public async Task<HttpDataResult> ReceiveCommandInfoAsync(OpenApiRequest input)
|
||
{
|
||
|
||
try
|
||
{
|
||
bool verifySignatureReult = EncryptUtil.OpenApiVerifySignature(input.Message, input.Nonce, input.Timestamp, input.Signature, srverOptions.SignatureToken);
|
||
if (verifySignatureReult == false)//签名校验失败
|
||
{
|
||
return HttpDataResultExtensions.Failed("签名校验失败", -101, ResponeResultEnum.NotAllowed);
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(input.Message))
|
||
{
|
||
return HttpDataResultExtensions.Failed("指令下发内容不能为空", -102, ResponeResultEnum.Fail);
|
||
}
|
||
|
||
//判断是否需要解密
|
||
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);
|
||
}
|
||
|
||
//限定来源类型必须为业务系统
|
||
if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem)
|
||
{
|
||
return HttpDataResultExtensions.Failed("设备指令来源类型错误,业务系统传固定值2", -104, ResponeResultEnum.Fail);
|
||
}
|
||
|
||
var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(messageBody.DeviceAddress);
|
||
|
||
//将指令存储Kafka的OneNET主题中
|
||
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
|
||
{
|
||
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, input);
|
||
}
|
||
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
|
||
{
|
||
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName, input);
|
||
}
|
||
else
|
||
{
|
||
return HttpDataResultExtensions.Failed("指令处理失败,当前设备平台类型异常", -105);
|
||
}
|
||
|
||
return HttpDataResultExtensions.Success("指令下发成功");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return HttpDataResultExtensions.Failed($"指令处理失败,发送异常:{ex.Message}", -106);
|
||
}
|
||
}
|
||
}
|
||
}
|