From 6da12cbf2dd5f87078fedf609e51472281d5ee6a Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Tue, 17 Mar 2026 16:23:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=B9=B3=E5=8F=B0=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dto/CallDeviceServiceForApiInput.cs | 26 ++++++++ .../IDeviceAggregationService.cs | 7 ++ .../DeviceAggregationService.cs | 54 +++++++++++++++ .../IoTDeviceBasicAppService.cs | 66 ++++++++++++++++++- .../IoTPlatformAggregationService.cs | 8 +-- 5 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CallDeviceServiceForApiInput.cs diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CallDeviceServiceForApiInput.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CallDeviceServiceForApiInput.cs new file mode 100644 index 0000000..de606fc --- /dev/null +++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CallDeviceServiceForApiInput.cs @@ -0,0 +1,26 @@ +using JiShe.ServicePro.Core; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace JiShe.IoT.DeviceAggregation +{ + /// + /// 调用设备服务入参 + /// + public class CallDeviceServiceForApiInput : IdInput + { + /// + /// 服务名称 + /// + [Required] + public string ServiceName { get; set; } + + /// + /// 服务参数 + /// + [Required] + public Dictionary ServiceParams { get; set; } + } +} diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs index 69b127b..5390bc1 100644 --- a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs +++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs @@ -106,5 +106,12 @@ namespace JiShe.IoT.DeviceAggregation /// /// Task BindingDeviceThingModel(BindingDeviceThingModelInput input); + + /// + /// 调用OneNET平台设备服务 + /// + /// + /// + Task> CallDeviceServiceToOneNETForApiAsync(CallDeviceServiceForApiInput input); } } diff --git a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs index 83d7cab..87d865e 100644 --- a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs +++ b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs @@ -638,6 +638,60 @@ namespace JiShe.IoT.DeviceAggregation } + /// + /// 调用OneNET平台设备服务 + /// + /// + /// + public async Task> CallDeviceServiceToOneNETForApiAsync(CallDeviceServiceForApiInput input) + { + try + { + if (string.IsNullOrWhiteSpace(input.ServiceName) || input.ServiceParams == null || input.ServiceParams.Count <= 0) + { + throw new UserFriendlyException($"服务名称参数异常"); + } + + var deviceInfo = await deviceAppService.FindByIdAsync(input); + if (deviceInfo == null) + { + throw new UserFriendlyException($"设备不存在"); + } + + + //数据写入遥测任务数据存储通道 + var commandRequest = new OpenApiRequest() + { + Message = input.Serialize(), + }; + var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, commandRequest, deviceInfo.Adapt(), input.Serialize()); + packetTaskInfo.TelemetryType = (int)DeviceTelemetryCommandTypeEnum.GetAttributeData; + + + if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET) + { + await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETUpgradeCommandIssuedEventName, packetTaskInfo)); + + return await CallDeviceServiceToOneNET(deviceInfo, input); + } + else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing) + { + //await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName,commandRequest); + //return true; + throw new UserFriendlyException($"发送设备指令信息失败,CTWing暂未实现。"); + } + else + { + throw new UserFriendlyException($"发送设备指令信息失败,未找到对应的产品配置信息。"); + } + } + catch (Exception) + { + throw; + } + } + + #region CTWing 设备操作 /// /// CTWing 设备创建 diff --git a/src/JiShe.IoT.Application/IoTDeviceBasicAppService.cs b/src/JiShe.IoT.Application/IoTDeviceBasicAppService.cs index d711828..7a5974d 100644 --- a/src/JiShe.IoT.Application/IoTDeviceBasicAppService.cs +++ b/src/JiShe.IoT.Application/IoTDeviceBasicAppService.cs @@ -34,12 +34,12 @@ namespace JiShe.IoT { protected readonly ILogger logger; protected readonly IDeviceAppService deviceAppService; + protected readonly IOneNETProductService oneNETProductService; protected readonly IOneNETDeviceService oneNETDeviceService; protected readonly IReliableRedisPubSubService redisPubSubService; protected readonly IIoTDBDataChannelManageService ioTDBDataChannelManageService; protected readonly IoTDBOptions ioTDBOptions; protected readonly ServerApplicationOptions serverApplicationOptions; - protected readonly IOneNETProductService oneNETProductService; protected readonly IDeviceThingModelManagementAppService deviceThingModelService; protected readonly IIoTPlatformThingModelInfoAppService platformThingModelInfoAppService; protected readonly IDeviceUpgradeRecordService deviceUpgradeRecordService; @@ -802,6 +802,70 @@ namespace JiShe.IoT throw; } } + + /// + /// 调用OneNET平台设备服务 + /// + /// + /// + /// + /// + protected async Task> CallDeviceServiceToOneNET(DeviceManagementInfoDto deviceInfo, CallDeviceServiceForApiInput input) + { + try + { + //检查下设备是否在线 + var deviceOnlineStatus = await oneNETDeviceService.DeviceInfoDetailAsync(new DeviceInfoDetailInput() + { + DeviceName = deviceInfo.IoTPlatformDeviceOpenInfo, + OneNETAccountId = deviceInfo.IoTPlatformAccountId, + ProductId = deviceInfo.IoTPlatformProductId, + }); + + if (deviceOnlineStatus == null || deviceOnlineStatus.Code != ResponeResultEnum.Success) + { + throw new UserFriendlyException("获取平台设备信息失败"); + } + + if (deviceOnlineStatus.Data.Status != 1) + { + throw new UserFriendlyException("设备不在线"); + } + + var deviceDataResult = await oneNETDeviceService.CallDeviceService(new CallDeviceServiceRequestInput() + { + DeviceName = deviceInfo.IoTPlatformDeviceOpenInfo, + OneNETAccountId = deviceInfo.IoTPlatformAccountId, + ProductId = deviceInfo.IoTPlatformProductId, + ThingModelIdentifier = input.ServiceName, + ServiceParams = input.ServiceParams + }); + if (deviceDataResult == null || deviceDataResult.Success == false || deviceDataResult.Data == null) + { + throw new UserFriendlyException($"设备{deviceInfo.DeviceName}获取数据失败"); + } + + //调用平台设备服务 + var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput() { Id = deviceInfo.IoTPlatformProductId }); + if (platformThingModelInfo == null || platformThingModelInfo.Count <= 0) + { + return deviceDataResult.Data; + } + + List updateKeys = new List() + { + ThingModelFixedTypeConst.FIRMWARE_VERSION.ToLowerInvariant(), + ThingModelFixedTypeConst.ReadingMode.ToLowerInvariant() + }; + + + return deviceDataResult.Data; + } + catch (Exception ex) + { + throw; + } + } #endregion #region CTWing 设备操作 diff --git a/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs b/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs index f214b46..8f8ac46 100644 --- a/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs +++ b/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs @@ -35,7 +35,7 @@ namespace JiShe.IoT.IoTPlatformAggregation private readonly ICTWingAccountService _ctwingAccountService; private readonly IOneNETAccountService _oneNETAccountService; private readonly IIoTPlatformThingModelInfoAppService _ioTPlatformThingModelInfoAppService; - private readonly IOneNETSubscriberBasic _oneNETSubscriberBasicService; + private readonly IOneNETPulsarServiceSubscriptionService _oneNETPulsarServiceSubscriptionService; private readonly ITableModelService _tableModelService; @@ -45,7 +45,7 @@ namespace JiShe.IoT.IoTPlatformAggregation IOneNETProductService oneNetProductService, ICTWingAccountService ctwingAccountService, IOneNETAccountService oneNETAccountService, - IOneNETSubscriberBasic oneNETSubscriberBasicService, + IOneNETPulsarServiceSubscriptionService oneNETPulsarServiceSubscriptionService, ITableModelService tableModelService, IIoTPlatformThingModelInfoAppService ioTPlatformThingModelInfoAppService) { @@ -55,7 +55,7 @@ namespace JiShe.IoT.IoTPlatformAggregation _ctwingAccountService = ctwingAccountService; _oneNETAccountService = oneNETAccountService; _ioTPlatformThingModelInfoAppService = ioTPlatformThingModelInfoAppService; - _oneNETSubscriberBasicService = oneNETSubscriberBasicService; + _oneNETPulsarServiceSubscriptionService = oneNETPulsarServiceSubscriptionService; _tableModelService = tableModelService; } @@ -383,7 +383,7 @@ namespace JiShe.IoT.IoTPlatformAggregation { string topicName = string.Equals(item.MessageType, IoTDBDataTypeConst.Data, StringComparison.OrdinalIgnoreCase) ? DistributedMessageCenterConst.OneNETThingModelPropertyChangeReceivedEventName : DistributedMessageCenterConst.OneNETThingModelEventChangeReceivedEventName; var tempOneNETReceiveBasicModel = item.RawMessage.Deserialize(); - await _oneNETSubscriberBasicService.OneNETReceiveThingModelHandlerAsync(topicName, item.IoTDataType, tempOneNETReceiveBasicModel, true); + await _oneNETPulsarServiceSubscriptionService.OneNETReceiveThingModelHandlerAsync(topicName, item.IoTDataType, tempOneNETReceiveBasicModel, true); } } catch (Exception)