diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DevicePropertyValueForApiInput.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DevicePropertyValueForApiInput.cs new file mode 100644 index 0000000..f6b3b18 --- /dev/null +++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DevicePropertyValueForApiInput.cs @@ -0,0 +1,19 @@ +using JiShe.ServicePro.Core; +using JiShe.ServicePro.Enums; +using System.ComponentModel.DataAnnotations; + +namespace JiShe.IoT.DeviceAggregation +{ + + /// + /// 设备属性抄读 + /// + public class DevicePropertyValueForApiInput : IdInput + { + /// + /// 设备在物联网平台中属性标识符列表 + /// + [Required(ErrorMessage = "属性标识符不能为空")] + public List PropertyList { 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 index 14680c7..87ef6cf 100644 --- a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs +++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs @@ -46,6 +46,13 @@ namespace JiShe.IoT.DeviceAggregation /// Task DeviceCommandForApiAsync(DeviceCommandForApiInput input); + /// + /// 获取设备属性最新值 + /// + /// + /// + Task> GetDevicePropertyValueForApiAsync(DevicePropertyValueForApiInput input); + /// /// 删除设备信息 /// diff --git a/src/JiShe.IoT.Application.Contracts/IoTPlatformAggregation/Dto/UpdateIoTPlatformProductPropertyInfoInput.cs b/src/JiShe.IoT.Application.Contracts/IoTPlatformAggregation/Dto/UpdateIoTPlatformProductPropertyInfoInput.cs new file mode 100644 index 0000000..2f09bf4 --- /dev/null +++ b/src/JiShe.IoT.Application.Contracts/IoTPlatformAggregation/Dto/UpdateIoTPlatformProductPropertyInfoInput.cs @@ -0,0 +1,28 @@ +using JiShe.ServicePro.Enums; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.IoT.IoTPlatformAggregation.Dto +{ + /// + /// 平台产品属性更新输入 + /// + public class UpdateIoTPlatformProductPropertyInfoInput + { + /// + /// 物联网平台类型,默认没有指定 + /// + [Required] + public IoTPlatformTypeEnum IoTPlatformType { get; set; } + + /// + /// 物联网平台中对应的产品Id + /// + [Required] + public string IoTPlatformProductId { get; set; } + } +} diff --git a/src/JiShe.IoT.Application.Contracts/IoTPlatformAggregation/IIoTPlatformAggregationService.cs b/src/JiShe.IoT.Application.Contracts/IoTPlatformAggregation/IIoTPlatformAggregationService.cs index b5abc45..60c46e3 100644 --- a/src/JiShe.IoT.Application.Contracts/IoTPlatformAggregation/IIoTPlatformAggregationService.cs +++ b/src/JiShe.IoT.Application.Contracts/IoTPlatformAggregation/IIoTPlatformAggregationService.cs @@ -37,5 +37,13 @@ namespace JiShe.IoT.IoTPlatformAggregation /// Task> GetIoTPlatformProductPropertyInfoAsync(IoTPlatformProductPropertyInfoInput input ); + + /// + /// 更新平台产品物模型属性信息 + /// + /// + /// + Task UpdateIoTPlatformProductPropertyInfoAsync(UpdateIoTPlatformProductPropertyInfoInput input + ); } } diff --git a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs index 4135306..7c59dbc 100644 --- a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs +++ b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs @@ -334,6 +334,50 @@ namespace JiShe.IoT.DeviceAggregation } } + + /// + /// 获取设备属性最新值 + /// + /// + /// + public async Task> GetDevicePropertyValueForApiAsync(DevicePropertyValueForApiInput input) + { + try + { + if (input.PropertyList == null || input.PropertyList.Count <= 0) + { + throw new UserFriendlyException($"属性标识符参数异常"); + } + + var deviceInfo = await deviceAppService.FindByIdAsync(input); + if (deviceInfo == null) + { + throw new UserFriendlyException($"设备不存在"); + } + + + //数据写入遥测任务数据存储通道 + if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET) + { + return await DevicePropertyValueToOneNET(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; + } + } + /// /// 业务系统批量创建设备信息 /// @@ -678,6 +722,56 @@ namespace JiShe.IoT.DeviceAggregation throw; } } + + /// + /// 抄读OneNET平台设备属性数据 + /// + /// + /// + /// + /// + public async Task> DevicePropertyValueToOneNET(DeviceManagementInfoDto deviceInfo, DevicePropertyValueForApiInput 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.QueryDevicePropertyDetail(new QueryDevicePropertyDetailInput() + { + DeviceName = deviceInfo.IoTPlatformDeviceOpenInfo, + OneNETAccountId = deviceInfo.IoTPlatformAccountId, + ProductId = deviceInfo.IoTPlatformProductId, + PropertyInfos = input.PropertyList + }); + if (deviceDataResult == null || deviceDataResult.Success == false || deviceDataResult.Data == null) + { + throw new UserFriendlyException($"设备{deviceInfo.DeviceName}获取数据失败"); + } + + return deviceDataResult.Data; + } + catch (Exception) + { + + throw; + } + } #endregion #region CTWing 设备操作 diff --git a/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs b/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs index af47905..a259742 100644 --- a/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs +++ b/src/JiShe.IoT.Application/IoTPlatformAggregation/IoTPlatformAggregationService.cs @@ -4,6 +4,7 @@ using JiShe.ServicePro.Commons; using JiShe.ServicePro.Core; using JiShe.ServicePro.CTWingManagement.CTWingAccounts; using JiShe.ServicePro.CTWingManagement.CTWingProducts; +using JiShe.ServicePro.DeviceManagement.ThingModels; using JiShe.ServicePro.Enums; using JiShe.ServicePro.FreeRedisProvider; using JiShe.ServicePro.OneNETManagement.OneNETAccounts; @@ -29,18 +30,22 @@ namespace JiShe.IoT.IoTPlatformAggregation private readonly IOneNETProductService _oneNetProductService; private readonly ICTWingAccountService _ctwingAccountService; private readonly IOneNETAccountService _oneNETAccountService; + private readonly IIoTPlatformThingModelInfoAppService _ioTPlatformThingModelInfoAppService; + public IoTPlatformAggregationService(ILogger logger, ICTWingProductService ctwingProductService, IOneNETProductService oneNetProductService, ICTWingAccountService ctwingAccountService, - IOneNETAccountService oneNETAccountService) + IOneNETAccountService oneNETAccountService, + IIoTPlatformThingModelInfoAppService ioTPlatformThingModelInfoAppService) { _logger = logger; _ctwingProductService = ctwingProductService; _oneNetProductService = oneNetProductService; _ctwingAccountService = ctwingAccountService; _oneNETAccountService = oneNETAccountService; + _ioTPlatformThingModelInfoAppService = ioTPlatformThingModelInfoAppService; } /// @@ -244,5 +249,66 @@ namespace JiShe.IoT.IoTPlatformAggregation throw; } } + + + /// + /// 更新平台产品物模型属性信息 + /// + /// + /// + public async Task UpdateIoTPlatformProductPropertyInfoAsync(UpdateIoTPlatformProductPropertyInfoInput input + ) + + { + try + { + if (input == null) + { + throw new UserFriendlyException($"{nameof(UpdateIoTPlatformProductPropertyInfoAsync)} 平台产品聚合服务获取产品属性信息失败,参数异常"); + } + + if (input.IoTPlatformType == IoTPlatformTypeEnum.CTWing) + { + + _logger.LogError($"{nameof(UpdateIoTPlatformProductPropertyInfoAsync)}产品聚合服务暂不支持CTWing产品物模型属性信息更新"); + + return null; + } + + + if (input.IoTPlatformType == IoTPlatformTypeEnum.OneNET) + { + var oneNetProductInfos = await _oneNetProductService.UpdateThingModelAsync(new IdInput() { Id = input.IoTPlatformProductId }); + if (oneNetProductInfos == null || string.IsNullOrWhiteSpace(oneNetProductInfos.ThingModelInfos)) + { + _logger.LogError($"{nameof(UpdateIoTPlatformProductPropertyInfoAsync)}产品聚合服务OneNET产品{input.IoTPlatformProductId}物模型属性信息更新失败,-101"); + return null; + } + + var oneNETAllThingModel = oneNetProductInfos.ThingModelInfos.Deserialize(); + if (oneNETAllThingModel == null) + { + _logger.LogError($"{nameof(UpdateIoTPlatformProductPropertyInfoAsync)}产品聚合服务OneNET产品{input.IoTPlatformProductId}物模型属性信息更新失败,-102"); + return null; + } + + //将平台产品最新物模型信息更新到平台端物模型表中 + var selectResults = OneNETAllThingModel.GetAllPropertiesSelectResult(oneNETAllThingModel.Properties); + + await _ioTPlatformThingModelInfoAppService.UpdatePlatformThingModelAsync(input.IoTPlatformProductId, input.IoTPlatformType, selectResults); + + + return oneNetProductInfos; + } + + return null; + + } + catch (Exception) + { + + throw; + } + } } } diff --git a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs index 2b17916..5871a88 100644 --- a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs +++ b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs @@ -85,7 +85,19 @@ namespace JiShe.IoT.Controllers public Task DeviceCommandForApiAsync(DeviceCommandForApiInput input) { return _deviceAggregationService.DeviceCommandForApiAsync(input); - } + } + + /// + /// 获取设备属性最新值 + /// + /// + /// + [HttpPost(nameof(GetDevicePropertyValueForApiAsync))] + [SwaggerOperation(summary: "获取设备属性最新值", Tags = new[] { "AggregationDevice" })] + public Task> GetDevicePropertyValueForApiAsync(DevicePropertyValueForApiInput input) + { + return _deviceAggregationService.GetDevicePropertyValueForApiAsync(input); + } } } diff --git a/src/JiShe.IoT.HttpApi/Controllers/IoTPlatformAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/IoTPlatformAggregationController.cs index b96240f..42fa526 100644 --- a/src/JiShe.IoT.HttpApi/Controllers/IoTPlatformAggregationController.cs +++ b/src/JiShe.IoT.HttpApi/Controllers/IoTPlatformAggregationController.cs @@ -57,5 +57,17 @@ namespace JiShe.IoT.Controllers return await _iotPlatformAggregationService.GetIoTPlatformProductPropertyInfoAsync(input); } + /// + /// 更新平台产品物模型属性信息 + /// + /// + /// + [HttpPost(nameof(UpdateIoTPlatformProductPropertyInfoAsync))] + [SwaggerOperation(summary: "更新平台产品物模型属性信息", Tags = new[] { "AggregationIoTPlatform" })] + public async Task UpdateIoTPlatformProductPropertyInfoAsync(UpdateIoTPlatformProductPropertyInfoInput input) + { + return await _iotPlatformAggregationService.UpdateIoTPlatformProductPropertyInfoAsync(input); + } + } }