From b2f0bb05b25952e328a74eb507a0d09c9eb52783 Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Fri, 1 Aug 2025 14:59:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=BC=93=E5=AD=98=E5=88=B7?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JiShe.ServicePro | 2 +- .../IDeviceAggregationService.cs | 11 ++- .../CTWingAggregationService.cs | 65 ++++------------- .../DeviceAggregationService.cs | 41 +++++++++-- .../OneNETAggregationService.cs | 71 ++++--------------- .../DeviceAggregationController.cs | 2 +- .../OneNETAggregationController.cs | 4 +- 7 files changed, 76 insertions(+), 120 deletions(-) diff --git a/JiShe.ServicePro b/JiShe.ServicePro index 7637921..7177852 160000 --- a/JiShe.ServicePro +++ b/JiShe.ServicePro @@ -1 +1 @@ -Subproject commit 76379211e6dd49d3c9ad10f7c4766f1fbbd4cde3 +Subproject commit 717785239911c98e2be567d49e57bcd9ff029516 diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs index 810108d..e63d33b 100644 --- a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs +++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs @@ -11,11 +11,18 @@ namespace JiShe.IoT.DeviceAggregation public interface IDeviceAggregationService : IApplicationService { /// - /// 创建设备信息 + /// 管理后台创建设备信息 /// /// /// - Task CreateAsync(CreateDeviceAggregationInput input); + Task CreateDeviceForApiAsync(CreateDeviceAggregationInput input); + + /// + /// 车间创建设备信息 + /// + /// + /// + Task CreateDeviceWorkshopAsync(CreateDeviceAggregationInput input); /// /// 删除设备信息 diff --git a/src/JiShe.IoT.Application/CTWingAggregation/CTWingAggregationService.cs b/src/JiShe.IoT.Application/CTWingAggregation/CTWingAggregationService.cs index fef4267..fdf87d9 100644 --- a/src/JiShe.IoT.Application/CTWingAggregation/CTWingAggregationService.cs +++ b/src/JiShe.IoT.Application/CTWingAggregation/CTWingAggregationService.cs @@ -1,4 +1,6 @@ using JiShe.IoT.CTWingAggregation.Dto; +using JiShe.IoT.DeviceAggregation; +using JiShe.IoT.DeviceAggregation.Dto; using JiShe.IoT.OneNETAggregation.Dto; using JiShe.IoT.Workshops; using JiShe.ServicePro; @@ -20,11 +22,10 @@ namespace JiShe.IoT.CTWingAggregation /// /// CTWing聚合服务 /// - /// - /// + /// 设备聚合服务 /// /// - public class CTWingAggregationService(IDeviceAppService deviceAppService, ICTWingDeviceService ctwingDeviceService, IOptions options, ILogger logger) : IoTAppService, ICTWingAggregationService + public class CTWingAggregationService(IDeviceAggregationService deviceAggregationService, IOptions options, ILogger logger) : IoTAppService, ICTWingAggregationService { ServerApplicationOptions srverOptions = options.Value; @@ -48,73 +49,35 @@ namespace JiShe.IoT.CTWingAggregation if (string.IsNullOrWhiteSpace(productionEquipmentMessageBody.IoTPlatformProductId)) { - return HttpDataResultExtensions.Failed("物联网平台产品Id不能为空", -102, ResponeResultEnum.Fail); + return HttpDataResultExtensions.Failed("CTWing平台产品Id不能为空", -102, ResponeResultEnum.Fail); } if (string.IsNullOrWhiteSpace(productionEquipmentMessageBody.DeviceAddress)) { return HttpDataResultExtensions.Failed("设备地址不能为空", -103, ResponeResultEnum.Fail); } - - var productInfo = await FreeSqlDbContext.Instance.Select() - .Where(e => e.IsEnabled == true && e.IoTPlatformProductId == productionEquipmentMessageBody.IoTPlatformProductId) - .FirstAsync(); - - if (productInfo == null) - { - return HttpDataResultExtensions.Failed("没有找到对应的产品信息", -104, ResponeResultEnum.Fail); - } - - CreateDeviceInput meterInfoEntity = new CreateDeviceInput() + CreateDeviceAggregationInput deviceInsertInput = new CreateDeviceAggregationInput() { DeviceAddress = productionEquipmentMessageBody.DeviceAddress, - PlatformPassword = productInfo.FeatureAccesskey, IoTPlatformProductId = productionEquipmentMessageBody.IoTPlatformProductId, - IoTPlatformDeviceOpenInfo = $"{productionEquipmentMessageBody.IoTPlatformProductId}{productionEquipmentMessageBody.DeviceAddress}", - DeviceName = productionEquipmentMessageBody.DeviceAddress + IoTPlatform = IoTPlatformTypeEnum.CTWing, }; - //创建本地设备信息 - var insertResult = await deviceAppService.CreateAsync(meterInfoEntity); - - //推送至CTWing平台 - var pushResult = await ctwingDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput() + //创建设备信息 + var insertResult = await deviceAggregationService.CreateDeviceWorkshopAsync(deviceInsertInput); + if (!insertResult) { - //DeviceName = productionEquipmentMessageBody.DeviceOpenInfo, - //ProductId = productionEquipmentMessageBody.IoTPlatformProductId, - //CTWingAccountId = productInfo.CTWingAccountId, - //Description = productionEquipmentMessageBody.DeviceOpenInfo, - }); - - if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success) - { - logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} 推送设备信息失败:{pushResult.Serialize()}"); - return HttpDataResultExtensions.Failed("推送设备信息CTWing失败", -105, ResponeResultEnum.Fail); + logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} 创建设备信息失败:{insertResult.Serialize()}"); + return HttpDataResultExtensions.Failed("CTWing推送设备失败!", -105); } - UpdateDeviceInput updateDeviceInput = insertResult.Adapt(); - updateDeviceInput.IoTPlatformResponse = pushResult.Serialize(); - - var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput); - if (updateResult == null) - { - logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} CTWing返回的设备信息更新失败:{input.Serialize()}"); - return HttpDataResultExtensions.Failed("CTWing返回的设备信息更新失败", -106, ResponeResultEnum.Fail); - } - - //设备数据缓存到Redis - DeviceCacheInfos deviceCacheInfos = insertResult.Adapt(); - deviceCacheInfos.IoTPlatformResponse = updateDeviceInput.IoTPlatformResponse; - - RedisProvider.Instance.HSet(RedisConst.CacheAllDeviceInfoHashKey, insertResult.DeviceAddress, deviceCacheInfos); - - return HttpDataResultExtensions.Success("推送设备成功!"); + return HttpDataResultExtensions.Success("CTWing推送设备成功!"); } catch (Exception ex) { logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} 生产车间数据推送发生异常:{ex.Serialize()}"); - return HttpDataResultExtensions.Failed("生产车间数据推送发生异常", -107, ResponeResultEnum.Exception); + return HttpDataResultExtensions.Failed("生产车间推送CTWing数据发生异常", -107, ResponeResultEnum.Exception); } } diff --git a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs index 124327f..1545892 100644 --- a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs +++ b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs @@ -29,12 +29,41 @@ namespace JiShe.IoT.DeviceAggregation public class DeviceAggregationService(ILogger logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService) : IoTAppService, IDeviceAggregationService { /// - /// 创建设备信息 + /// 管理后台创建设备信息 /// /// /// [Authorize(DeviceManagementPermissions.DeviceInfoManagement.Create)] - public async Task CreateAsync(CreateDeviceAggregationInput input) + public async Task CreateDeviceForApiAsync(CreateDeviceAggregationInput input) + { + try + { + + if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.CTWing) + { + return await CTWingDeviceCreateAsync(input); + } + else if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.OneNET) + { + return await OneNETDeviceCreateAsync(input); + } + + throw new UserFriendlyException($"不支持的物联网平台"); + } + catch (Exception) + { + + throw; + } + + } + + /// + /// 车间创建设备信息 + /// + /// + /// + public async Task CreateDeviceWorkshopAsync(CreateDeviceAggregationInput input) { try { @@ -95,7 +124,7 @@ namespace JiShe.IoT.DeviceAggregation var insertResult = await deviceAppService.CreateAsync(createDeviceInput); if (insertResult == null) { - logger.LogError($"{nameof(CreateAsync)} 添加设备信息失败:{input.Serialize()}"); + logger.LogError($"{nameof(CreateDeviceForApiAsync)} 添加设备信息失败:{input.Serialize()}"); return false; } @@ -110,7 +139,7 @@ namespace JiShe.IoT.DeviceAggregation if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success) { - logger.LogError($"{nameof(CreateAsync)} 推送设备信息失败:{pushResult.Serialize()}"); + logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}"); return false; } @@ -244,7 +273,7 @@ namespace JiShe.IoT.DeviceAggregation if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success) { - logger.LogError($"{nameof(CreateAsync)} 推送设备信息失败:{pushResult.Serialize()}"); + logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}"); throw new UserFriendlyException($"平台请求失败。"); } @@ -274,7 +303,7 @@ namespace JiShe.IoT.DeviceAggregation var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput); if (updateResult == null) { - logger.LogError($"{nameof(CreateAsync)} 更新设备信息失败:{input.Serialize()}"); + logger.LogError($"{nameof(CreateDeviceForApiAsync)} 更新设备信息失败:{input.Serialize()}"); throw new UserFriendlyException($"推送结果更新失败。"); } diff --git a/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs b/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs index 10d4bd3..a1634f9 100644 --- a/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs +++ b/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs @@ -1,32 +1,26 @@ -using JiShe.IoT.OneNETAggregation.Dto; +using JiShe.IoT.DeviceAggregation; +using JiShe.IoT.DeviceAggregation.Dto; +using JiShe.IoT.OneNETAggregation.Dto; using JiShe.IoT.Workshops; using JiShe.ServicePro; using JiShe.ServicePro.Core; -using JiShe.ServicePro.DeviceManagement.DeviceInfos; -using JiShe.ServicePro.DeviceManagement.DeviceInfos.Dto; using JiShe.ServicePro.Encrypt; using JiShe.ServicePro.Enums; -using JiShe.ServicePro.FreeRedisProvider; -using JiShe.ServicePro.Kafka.Consts; -using JiShe.ServicePro.Kafka.Producer; using JiShe.ServicePro.OneNETManagement.OneNETDevices; using JiShe.ServicePro.OneNETManagement.OneNETProducts; using JiShe.ServicePro.ServerOptions; -using Mapster; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using System.IO.Pipelines; namespace JiShe.IoT.OneNETAggregation { /// /// OneNET聚合服务 /// - /// 设备管理信息服务 - /// OneNET设备操作服务 + /// 设备聚合服务 /// 服务配置 /// - public class OneNETAggregationService(IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IOptions options, ILogger logger) : IoTAppService, IOneNETAggregationService + public class OneNETAggregationService(IDeviceAggregationService deviceAggregationService, IOptions options, ILogger logger) : IoTAppService, IOneNETAggregationService { ServerApplicationOptions srverOptions = options.Value; @@ -61,65 +55,28 @@ namespace JiShe.IoT.OneNETAggregation } - var productInfo = await FreeSqlDbContext.Instance.Select() - .Where(e => e.IsEnabled == true && e.IoTPlatformProductId == productionEquipmentMessageBody.IoTPlatformProductId) - .FirstAsync(); - - if (productInfo == null) - { - return HttpDataResultExtensions.Failed("没有找到对应的产品信息", -104, ResponeResultEnum.Fail); - } - - CreateDeviceInput deviceInsertInput = new CreateDeviceInput() + CreateDeviceAggregationInput deviceInsertInput = new CreateDeviceAggregationInput() { DeviceAddress = productionEquipmentMessageBody.DeviceAddress, - PlatformPassword = productInfo.ProductAccesskey, + IoTPlatform = IoTPlatformTypeEnum.OneNET, IoTPlatformProductId = productionEquipmentMessageBody.IoTPlatformProductId, - IoTPlatformDeviceOpenInfo = $"{productionEquipmentMessageBody.IoTPlatformProductId}{productionEquipmentMessageBody.DeviceAddress}", - DeviceName = productionEquipmentMessageBody.DeviceAddress }; - //创建本地设备信息 - var insertResult = await deviceAppService.CreateAsync(deviceInsertInput); - - //推送至OneNET平台 - var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput() + //创建设备信息 + var insertResult = await deviceAggregationService.CreateDeviceWorkshopAsync(deviceInsertInput); + if (!insertResult) { - DeviceName = deviceInsertInput.IoTPlatformDeviceOpenInfo, - ProductId = productionEquipmentMessageBody.IoTPlatformProductId, - OneNETAccountId = productInfo.OneNETAccountId, - Description = deviceInsertInput.IoTPlatformDeviceOpenInfo, - }); - - if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success) - { - logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} 推送设备信息失败:{pushResult.Serialize()}"); - return HttpDataResultExtensions.Failed("推送设备信息OneNET失败", -105, ResponeResultEnum.Fail); + logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} OneNET创建设备信息失败:{insertResult.Serialize()}"); + return HttpDataResultExtensions.Failed("OneNET推送设备失败!", -105); } - UpdateDeviceInput updateDeviceInput = insertResult.Adapt(); - updateDeviceInput.IoTPlatformResponse = pushResult.Serialize(); - - var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput); - if (updateResult == null) - { - logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} OneNET返回的设备信息更新失败:{input.Serialize()}"); - return HttpDataResultExtensions.Failed("OneNET返回的设备信息更新失败", -106, ResponeResultEnum.Fail); - } - - //设备数据缓存到Redis - DeviceCacheInfos deviceCacheInfos = insertResult.Adapt(); - deviceCacheInfos.IoTPlatformResponse = updateDeviceInput.IoTPlatformResponse; - - RedisProvider.Instance.HSet(RedisConst.CacheAllDeviceInfoHashKey, insertResult.DeviceAddress, deviceCacheInfos); - - return HttpDataResultExtensions.Success("推送设备成功!"); + return HttpDataResultExtensions.Success("OneNET推送设备成功!"); } catch (Exception ex) { logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} 生产车间数据推送发生异常:{ex.Serialize()}"); - return HttpDataResultExtensions.Failed("生产车间数据推送发生异常", -107, ResponeResultEnum.Exception); + return HttpDataResultExtensions.Failed("生产车间推送OneNET数据发生异常", -107, ResponeResultEnum.Exception); } } diff --git a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs index fa32133..8ec742c 100644 --- a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs +++ b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs @@ -26,7 +26,7 @@ namespace JiShe.IoT.Controllers [SwaggerOperation(summary: "创建设备信息", Tags = new[] { "AggregationDevice" })] public async Task CreateAsync(CreateDeviceAggregationInput input) { - return await _deviceAggregationService.CreateAsync(input); + return await _deviceAggregationService.CreateDeviceForApiAsync(input); } /// diff --git a/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs index f8375f9..73d3042 100644 --- a/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs +++ b/src/JiShe.IoT.HttpApi/Controllers/OneNETAggregationController.cs @@ -32,9 +32,9 @@ namespace JiShe.IoT.Controllers /// [HttpPost(nameof(ReceiveWorkshopProductionInfoAsync))] [SwaggerOperation(summary: "接收车间生产信息", Tags = new[] { "AggregationOneNET" })] - public async Task ReceiveWorkshopProductionInfoAsync(OpenApiRequest input) + public async Task ReceiveWorkshopProductionInfoAsync(OpenApiRequest input) { - await _oneNETAggregationServiceService.ReceiveWorkshopProductionInfoAsync(input); + return await _oneNETAggregationServiceService.ReceiveWorkshopProductionInfoAsync(input); } ///