98 lines
4.0 KiB
C#
Raw Normal View History

using JiShe.IoT.OneNETAggregation.Dto;
2025-08-21 11:34:16 +08:00
using JiShe.IoT.Workshops;
using JiShe.ServicePro;
2025-08-04 17:29:47 +08:00
using JiShe.ServicePro.Core;
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
using JiShe.ServicePro.Dto;
using JiShe.ServicePro.Encrypt;
using JiShe.ServicePro.Enums;
2025-08-21 11:34:16 +08:00
using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.Kafka.Consts;
using JiShe.ServicePro.Kafka.Producer;
using JiShe.ServicePro.ServerOptions;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
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
{
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);
if (verifySignatureReult == false)//签名校验失败
{
2025-08-04 17:29:47 +08:00
return HttpDataResultExtensions.Failed("签名校验失败", -101, ResponeResultEnum.NotAllowed);
}
if (string.IsNullOrWhiteSpace(input.Message))
{
2025-08-04 17:29:47 +08:00
return HttpDataResultExtensions.Failed("指令下发内容不能为空", -102, ResponeResultEnum.Fail);
}
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-21 11:34:16 +08:00
await redisPubSubService.PublishReliableAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, input);
2025-08-04 17:29:47 +08:00
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
2025-08-21 11:34:16 +08:00
await redisPubSubService.PublishReliableAsync(KafkaTopicConsts.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-08-21 11:34:16 +08:00
return HttpDataResultExtensions.Success("指令下发成功");
}
2025-08-21 11:34:16 +08:00
catch (Exception ex)
{
2025-08-21 11:34:16 +08:00
return HttpDataResultExtensions.Failed($"指令处理失败,发送异常:{ex.Message}", -106);
}
}
}
}