diff --git a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs new file mode 100644 index 0000000..e66a278 --- /dev/null +++ b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs @@ -0,0 +1,22 @@ +using JiShe.ServicePro; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.IoT.BusinessSystemAggregation +{ + /// + /// 业务系统聚合服务 + /// + public interface IBusinessSystemAggregationService + { + /// + /// 接收业务系统指令信息 + /// + /// + /// + Task ReceiveCommandInfoAsync(OpenApiRequest input); + } +} diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CreateDeviceAggregationInput.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CreateDeviceAggregationInput.cs new file mode 100644 index 0000000..5f53812 --- /dev/null +++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CreateDeviceAggregationInput.cs @@ -0,0 +1,44 @@ +using JiShe.ServicePro.Enums; +using System.ComponentModel.DataAnnotations; + +namespace JiShe.IoT.DeviceAggregation.Dto +{ + /// + /// 设备聚合新增设备 + /// + public class CreateDeviceAggregationInput + { + /// + /// 表通信地址 + /// + [Required(ErrorMessage = "设备地址不能为空")] + public string DeviceAddress { get; set; } + + /// + /// 物联网平台类型 + /// + [Required(ErrorMessage = "物联网平台类型不能为空")] + public IoTPlatformTypeEnum IoTPlatform { get; set; } + + /// + /// 电表密码 + /// + [Required(ErrorMessage = "电表密码不能为空")] + public string PlatformPassword { get; set; } + + /// + /// 集中器在物联网平台中对应的产品Id + /// + public string IoTPlatformProductId { get; set; } + + /// + /// 集中器在物联网平台中对应的设备Id或者名称 + /// + public string IoTPlatformDeviceOpenInfo { get; set; } + + /// + /// 物联网平台中对应的账号Id + /// + public string IoTPlatformAccountId { get; set; } + } +} \ No newline at end of file diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs new file mode 100644 index 0000000..c9723d1 --- /dev/null +++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs @@ -0,0 +1,42 @@ +using JiShe.IoT.DeviceAggregation.Dto; +using JiShe.ServicePro.Core; +using JiShe.ServicePro.DeviceManagement.DeivceInfos.Dto; +using JiShe.ServicePro.DeviceManagement.Meters.Dto; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp; + +namespace JiShe.IoT.DeviceAggregation +{ + /// + /// 设备聚合服务 + /// + public interface IDeviceAggregationService + { + /// + /// 创建设备信息 + /// + /// + /// + Task CreateAsync(CreateDeviceAggregationInput input); + + /// + /// 删除设备信息 + /// + /// + /// + /// + Task DeleteAsync(IdInput input); + + /// + /// 根据设备ID查询设备信息 + /// + /// + /// + /// + Task FindByIdAsync(IdInput input); + } +} diff --git a/src/JiShe.IoT.Application.Contracts/OneNETAggregation/IOneNETAggregationService.cs b/src/JiShe.IoT.Application.Contracts/OneNETAggregation/IOneNETAggregationService.cs index adfa228..d41e4a6 100644 --- a/src/JiShe.IoT.Application.Contracts/OneNETAggregation/IOneNETAggregationService.cs +++ b/src/JiShe.IoT.Application.Contracts/OneNETAggregation/IOneNETAggregationService.cs @@ -24,13 +24,6 @@ namespace JiShe.IoT.OneNETAggregation /// /// 获取OneNET产品列表 /// - Task>> GetProductListAsync(OpenApiRequest input); - - /// - /// 接收业务系统指令信息 - /// - /// - /// - Task ReceiveBusinessSystemCommandInfoAsync(OpenApiRequest input); + Task>> GetProductListAsync(OpenApiRequest input); } } diff --git a/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs new file mode 100644 index 0000000..341e7a9 --- /dev/null +++ b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs @@ -0,0 +1,57 @@ +using JiShe.IoT.OneNETAggregation.Dto; +using JiShe.ServicePro; +using JiShe.ServicePro.Encrypt; +using JiShe.ServicePro.Enums; +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 +{ + /// + /// 业务系统聚合服务 + /// + public class BusinessSystemAggregationService(IOptions options, IKafkaProducerService _producerService) :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.VerifySignatureToken); + if (verifySignatureReult == false)//签名校验失败 + { + return HttpDataResultExtensions.Failed>("签名校验失败", -101, ResponeResultEnum.NotAllowed); + } + + if (string.IsNullOrWhiteSpace(input.Message)) + { + return HttpDataResultExtensions.Failed>("指令下发内容不能为空", -102, ResponeResultEnum.Fail); + } + + //将指令存储Kafka中 + await _producerService.ProduceAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, $"{GuidGenerator.Create()}", input); + + return HttpDataResultExtensions.Success("指令下发Kafka成功"); + } + catch (Exception) + { + + throw; + } + } + } +} diff --git a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs new file mode 100644 index 0000000..34d3253 --- /dev/null +++ b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs @@ -0,0 +1,106 @@ +using JiShe.IoT.DeviceAggregation.Dto; +using JiShe.IoT.OneNETAggregation.Dto; +using JiShe.ServicePro.Core; +using JiShe.ServicePro.DeviceManagement.DeivceInfos; +using JiShe.ServicePro.DeviceManagement.DeivceInfos.Dto; +using JiShe.ServicePro.DeviceManagement.Meters.Dto; +using JiShe.ServicePro.DeviceManagement.Permissions; +using JiShe.ServicePro.OneNETManagement.OneNETDevices; +using Mapster; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp; + +namespace JiShe.IoT.DeviceAggregation +{ + /// + /// 设备聚合服务 + /// + /// + /// 设备服务 + /// OneNET设备服务 + public class DeviceAggregationService(ILogger logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService) : IoTAppService, IDeviceAggregationService + { + /// + /// 创建设备信息 + /// + /// + /// + [Authorize(DeviceManagementPermissions.MeterManagement.Create)] + public async Task CreateAsync(CreateDeviceAggregationInput input) + { + + try + { + CreateDeviceInput createDeviceInput = input.Adapt(); + + var insertResult = await deviceAppService.CreateAsync(createDeviceInput); + if (insertResult == null) + { + logger.LogError($"{nameof(CreateAsync)} 添加设备信息失败:{input.Serialize()}"); + return false; + } + + //推送至OneNET平台 + var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput() + { + DeviceName = $"{input.IoTPlatformProductId}{input.DeviceAddress}", + ProductId = input.IoTPlatformProductId, + OneNETAccountId = input.IoTPlatformAccountId, + Description = input.DeviceAddress, + }); + + if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success) + { + logger.LogError($"{nameof(CreateAsync)} 推送设备信息失败:{pushResult.Serialize()}"); + return false; + } + + UpdateDeviceInput updateDeviceInput = insertResult.Adapt(); + updateDeviceInput.IoTPlatformResponse = pushResult.Serialize(); + + var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput); + if (updateResult == null) + { + logger.LogError($"{nameof(CreateAsync)} 更新设备信息失败:{input.Serialize()}"); + return false; + } + + return true; + } + catch (Exception) + { + + throw; + } + } + + /// + /// 删除设备信息 + /// + /// + /// + /// + [Authorize(DeviceManagementPermissions.MeterManagement.Delete)] + public async Task DeleteAsync(IdInput input) + { + return await deviceAppService.DeleteAsync(input); + + } + + /// + /// 根据设备ID查询设备信息 + /// + /// + /// + /// + public async Task FindByIdAsync(IdInput input) + { + return await deviceAppService.FindByIdAsync(input); + } + } +} diff --git a/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs b/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs index cb55f01..0b3bc6a 100644 --- a/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs +++ b/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs @@ -16,14 +16,13 @@ using Microsoft.Extensions.Options; namespace JiShe.IoT.OneNETAggregation { /// - /// 生产车间聚合服务 + /// OneNET聚合服务 /// /// 设备管理信息服务 - /// OneNET设备操作服务 - /// Kafka数据生产服务 + /// OneNET设备操作服务 /// 服务配置 /// - public class OneNETAggregationService(IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IKafkaProducerService _producerService, IOptions options, ILogger logger) : IoTAppService, IOneNETAggregationService + public class OneNETAggregationService(IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IOptions options, ILogger logger) : IoTAppService, IOneNETAggregationService { ServerApplicationOptions srverOptions = options.Value; @@ -84,7 +83,7 @@ namespace JiShe.IoT.OneNETAggregation CreateDeviceInput meterInfoEntity = new CreateDeviceInput() { DeviceAddress = productionEquipmentMessageBody.DeviceAddress, - Password = productInfo.ProductAccesskey, + PlatformPassword = productInfo.ProductAccesskey, IoTPlatformProductId = productionEquipmentMessageBody.IoTPlatformProductId, IoTPlatformDeviceOpenInfo = productionEquipmentMessageBody.DeviceOpenInfo, DeviceName = productionEquipmentMessageBody.DeviceOpenInfo @@ -141,38 +140,6 @@ namespace JiShe.IoT.OneNETAggregation throw; } - } - - /// - /// 接收业务系统指令信息,缓存进Kafka - /// - [AllowAnonymous] - public async Task ReceiveBusinessSystemCommandInfoAsync(OpenApiRequest input) - { - - try - { - bool verifySignatureReult = EncryptUtil.OpenApiVerifySignature(input.Message, input.Nonce, input.Timestamp, input.Signature, srverOptions.VerifySignatureToken); - if (verifySignatureReult == false)//签名校验失败 - { - return HttpDataResultExtensions.Failed>("签名校验失败", -101, ResponeResultEnum.NotAllowed); - } - - if (string.IsNullOrWhiteSpace(input.Message)) - { - return HttpDataResultExtensions.Failed>("指令下发内容不能为空", -102, ResponeResultEnum.Fail); - } - - //将指令存储Kafka中 - await _producerService.ProduceAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, $"{GuidGenerator.Create()}", input); - - return HttpDataResultExtensions.Success("指令下发Kafka成功"); - } - catch (Exception) - { - - throw; - } - } + } } } diff --git a/src/JiShe.IoT.HttpApi/Controllers/BusinessSystemAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/BusinessSystemAggregationController.cs new file mode 100644 index 0000000..4f2e288 --- /dev/null +++ b/src/JiShe.IoT.HttpApi/Controllers/BusinessSystemAggregationController.cs @@ -0,0 +1,44 @@ +using JiShe.IoT.BusinessSystemAggregation; +using JiShe.IoT.CommonServices; +using JiShe.IoT.CommonServices.Dto; +using JiShe.IoT.DeviceAggregation; +using JiShe.IoT.DeviceAggregation.Dto; +using JiShe.IoT.OneNETAggregation; +using JiShe.IoT.OneNETAggregation.Dto; +using JiShe.ServicePro; +using JiShe.ServicePro.Commons; +using JiShe.ServicePro.Core; +using JiShe.ServicePro.DeviceManagement.DeivceInfos.Dto; +using JiShe.ServicePro.DeviceManagement.Meters.Dto; +using JiShe.ServicePro.OneNETManagement.OneNETProducts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.IoT.Controllers +{ + /// + /// 设备聚合服务 + /// + [Route("/Aggregation/Business")] + public class BusinessSystemAggregationController : IoTController + { + private readonly IBusinessSystemAggregationService _businessSystemAggregationService; + public BusinessSystemAggregationController(IBusinessSystemAggregationService businessSystemAggregationService) + { + _businessSystemAggregationService = businessSystemAggregationService; + } + + /// + /// 接收业务系统指令信息 + /// + [HttpPost(nameof(ReceiveCommandInfoAsync))] + [SwaggerOperation(summary: "接收业务系统指令信息", Tags = new[] { "AggregationBusiness" })] + public async Task ReceiveCommandInfoAsync(OpenApiRequest input) + { + return await _businessSystemAggregationService.ReceiveCommandInfoAsync(input); + } + } +} diff --git a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs new file mode 100644 index 0000000..9c991f6 --- /dev/null +++ b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs @@ -0,0 +1,65 @@ +using JiShe.IoT.CommonServices; +using JiShe.IoT.CommonServices.Dto; +using JiShe.IoT.DeviceAggregation; +using JiShe.IoT.DeviceAggregation.Dto; +using JiShe.IoT.OneNETAggregation; +using JiShe.IoT.OneNETAggregation.Dto; +using JiShe.ServicePro; +using JiShe.ServicePro.Commons; +using JiShe.ServicePro.Core; +using JiShe.ServicePro.DeviceManagement.DeivceInfos.Dto; +using JiShe.ServicePro.DeviceManagement.Meters.Dto; +using JiShe.ServicePro.OneNETManagement.OneNETProducts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.IoT.Controllers +{ + /// + /// 设备聚合服务 + /// + [Route("/Aggregation/Device")] + public class DeviceAggregationController : IoTController + { + private readonly IDeviceAggregationService _deviceAggregationService; + public DeviceAggregationController(IDeviceAggregationService deviceAggregationService) + { + _deviceAggregationService = deviceAggregationService; + } + + /// + /// 创建设备信息 + /// + /// + /// + [HttpPost(nameof(CreateAsync))] + [SwaggerOperation(summary: "创建设备信息", Tags = new[] { "AggregationDevice" })] + public async Task CreateAsync(CreateDeviceAggregationInput input) + { + return await _deviceAggregationService.CreateAsync(input); + } + + /// + /// 删除设备信息 + /// + [HttpPost(nameof(DeleteAsync))] + [SwaggerOperation(summary: "删除设备信息", Tags = new[] { "AggregationDevice" })] + public async Task DeleteAsync(IdInput input) + { + return await _deviceAggregationService.DeleteAsync(input); + } + + /// + /// 根据设备ID查询设备信息 + /// + [HttpPost(nameof(FindByIdAsync))] + [SwaggerOperation(summary: "根据设备ID查询设备信息", Tags = new[] { "AggregationDevice" })] + public async Task FindByIdAsync(IdInput input) + { + return await _deviceAggregationService.FindByIdAsync(input); + } + } +} diff --git a/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs index 8df9d2d..bb951f6 100644 --- a/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs +++ b/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs @@ -45,16 +45,6 @@ namespace JiShe.IoT.Controllers public async Task>> GetProductListAsync(OpenApiRequest input) { return await _oneNETAggregationServiceService.GetProductListAsync(input); - } - - /// - /// 接收业务系统指令信息 - /// - [HttpPost(nameof(ReceiveBusinessSystemCommandInfoAsync))] - [SwaggerOperation(summary: "接收业务系统指令信息", Tags = new[] { "AggregationOneNET" })] - public async Task ReceiveBusinessSystemCommandInfoAsync(OpenApiRequest input) - { - return await _oneNETAggregationServiceService.ReceiveBusinessSystemCommandInfoAsync(input); - } + } } }