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 { /// /// 业务系统聚合服务 /// public class BusinessSystemAggregationService(IOptions options, IReliableRedisPubSubService redisPubSubService, IDeviceAppService deviceAppService) : IoTAppService, IBusinessSystemAggregationService { ServerApplicationOptions srverOptions = options.Value; /// /// 接收业务系统指令信息,缓存进Kafka /// [AllowAnonymous] public async Task 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(); } else { messageBody = input.Message.Deserialize(); } 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); } } } }