From 0b3b4a7c706c18be09213699ee4d498ea7eb49f7 Mon Sep 17 00:00:00 2001
From: ChenYi <296215406@outlook.com>
Date: Tue, 30 Dec 2025 17:26:28 +0800
Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=9B=BA=E4=BB=B6=E4=BF=A1?=
=?UTF-8?q?=E6=81=AF=E6=9C=8D=E5=8A=A1=E3=80=81=E8=AE=BE=E5=A4=87=E5=8D=87?=
=?UTF-8?q?=E7=BA=A7=E8=AE=B0=E5=BD=95=E6=9C=8D=E5=8A=A1=EF=BC=8C=E8=AE=BE?=
=?UTF-8?q?=E5=A4=87=E8=81=9A=E5=90=88=E6=9C=8D=E5=8A=A1=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Dto/DeviceUpgradeForApiInput.cs | 31 +++
.../IDeviceAggregationService.cs | 14 ++
.../BusinessSystemAggregationService.cs | 41 +++-
.../DeviceAggregationService.cs | 229 ++++++++++++++++--
.../DeviceAggregationController.cs | 26 ++
5 files changed, 313 insertions(+), 28 deletions(-)
create mode 100644 src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceUpgradeForApiInput.cs
diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceUpgradeForApiInput.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceUpgradeForApiInput.cs
new file mode 100644
index 0000000..7b3663f
--- /dev/null
+++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceUpgradeForApiInput.cs
@@ -0,0 +1,31 @@
+using JiShe.ServicePro.Core;
+using JiShe.ServicePro.Enums;
+using System.ComponentModel.DataAnnotations;
+
+namespace JiShe.IoT.DeviceAggregation
+{
+ ///
+ /// 设备升级
+ ///
+ public class DeviceUpgradeForApiInput : IdInput
+ {
+ ///
+ /// 物联网平台类型,默认没有指定
+ ///
+ [Required]
+ public IoTPlatformTypeEnum IoTPlatform { get; set; }
+
+ ///
+ /// 物联网平台中对应的产品Id
+ ///
+ [Required]
+ public string IoTPlatformProductId { get; set; }
+
+ ///
+ /// 固件版本信息
+ ///
+ [Required]
+ public Guid NowFirmwareVersionDataId { 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 87ef6cf..236803f 100644
--- a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs
+++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs
@@ -2,7 +2,9 @@
using JiShe.ServicePro;
using JiShe.ServicePro.Core;
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
+using JiShe.ServicePro.FileManagement.Files;
using Volo.Abp;
+using Volo.Abp.Content;
namespace JiShe.IoT.DeviceAggregation
{
@@ -46,6 +48,18 @@ namespace JiShe.IoT.DeviceAggregation
///
Task DeviceCommandForApiAsync(DeviceCommandForApiInput input);
+ ///
+ /// 发送设备升级指令信息
+ ///
+ ///
+ ///
+ Task DeviceUpgradeForApiAsync(DeviceUpgradeForApiInput input);
+
+ ///
+ /// 下载设备固件文件
+ ///
+ Task DownloadFirmwareInfoAsync(IdInput input);
+
///
/// 获取设备属性最新值
///
diff --git a/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs
index 6fa8863..e459796 100644
--- a/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs
+++ b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs
@@ -6,6 +6,7 @@ using JiShe.ServicePro.ApacheIoTDB.Provider.Options;
using JiShe.ServicePro.Core;
using JiShe.ServicePro.DataChannelManages;
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
+using JiShe.ServicePro.DeviceManagement.ThingModels;
using JiShe.ServicePro.Dto;
using JiShe.ServicePro.Encrypt;
using JiShe.ServicePro.Enums;
@@ -16,6 +17,7 @@ using Mapster;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
+using Volo.Abp;
using Volo.Abp.Auditing;
namespace JiShe.IoT.BusinessSystemAggregation
@@ -24,7 +26,7 @@ namespace JiShe.IoT.BusinessSystemAggregation
/// 业务系统聚合服务
///
[DisableAuditing]
- public class BusinessSystemAggregationService(IOptions options, IReliableRedisPubSubService redisPubSubService, IDeviceAppService deviceAppService, IIoTDBDataChannelManageService ioTDBDataChannelManageService, IOptions _ioTDBOptions, ITreeModelService treeModelService, IDeviceAggregationService deviceAggregationService, ILogger _logger) : IoTAppService, IBusinessSystemAggregationService
+ public class BusinessSystemAggregationService(IOptions options, IReliableRedisPubSubService redisPubSubService, IDeviceAppService deviceAppService, IIoTDBDataChannelManageService ioTDBDataChannelManageService, IOptions _ioTDBOptions, ITreeModelService treeModelService, IDeviceAggregationService deviceAggregationService, ILogger _logger, IIoTPlatformThingModelInfoAppService platformThingModelInfoAppService) : IoTAppService, IBusinessSystemAggregationService
{
ServerApplicationOptions serverOptions = options.Value;
IoTDBOptions ioTDBOptions = _ioTDBOptions.Value;
@@ -45,11 +47,11 @@ namespace JiShe.IoT.BusinessSystemAggregation
return handleResult;
}
var messageBody = handleResult.Data;
- if(messageBody== null || messageBody.Commands == null || messageBody.Commands.Count <= 0)
+ if (messageBody == null || messageBody.Commands == null || messageBody.Commands.Count <= 0)
{
return HttpDataResultExtensions.Failed("设备指令不能为空", -103, ResponeResultEnum.Fail);
}
-
+
//限定来源类型必须为业务系统
if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem)
{
@@ -68,6 +70,37 @@ namespace JiShe.IoT.BusinessSystemAggregation
//将指令存储IoTDB数据库和Redis发布通道
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
+
+
+ //获取设备对应的平台端物模型信息,校验前端传入的属性标识集合是否存在不合法的属性标识符
+
+ var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput() { Id = deviceInfo.IoTPlatformProductId });
+
+ if (platformThingModelInfo == null)
+ {
+ throw new UserFriendlyException($"业务系统推送指令时设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。");
+ }
+
+ foreach (var item in messageBody.Commands)
+ {
+ var tempPlatformThingModelInfo = platformThingModelInfo.Where(d => d.IoTPlatformRawFieldName == item.Key).FirstOrDefault();
+ if (tempPlatformThingModelInfo == null)
+ {
+ throw new UserFriendlyException($"业务系统推送指令时设备设备{deviceInfo.DeviceAddress}平台端物模型信息不存在属性标识符{item.Key}。");
+ }
+
+ //排除升级指令
+ if (tempPlatformThingModelInfo.StandardFieldName.ToLowerInvariant() == ThingModelFixedTypeConst.FIRMWARE_UPGRADE.ToLowerInvariant())
+ {
+ throw new UserFriendlyException($"业务系统推送指令时设备{deviceInfo.DeviceAddress}平台端物模型属性标识符{item.Key}是升级指令操作,被禁止。");
+ }
+
+ if (deviceInfo.IsNeedConfigDevicMdoel && deviceInfo.DeviceThingModelDataId.HasValue && item.Key.ToLowerInvariant() == ThingModelFixedTypeConst.SpecialCommand.ToLowerInvariant())
+ {
+ throw new UserFriendlyException($"业务系统推送指令时设备{deviceInfo.DeviceAddress}平台端物模型属性标识符{item.Key}是特殊指令操作,被禁止。");
+ }
+ }
+
//数据写入遥测任务数据存储通道
await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo));
@@ -223,6 +256,6 @@ namespace JiShe.IoT.BusinessSystemAggregation
{
return HttpDataResultExtensions.Failed($"查询设备数据失败,发送异常:{ex.Message}", -106);
}
- }
+ }
}
}
diff --git a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
index 7c59dbc..93d08ff 100644
--- a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
+++ b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
@@ -9,6 +9,7 @@ using JiShe.ServicePro.DeviceManagement.Permissions;
using JiShe.ServicePro.DeviceManagement.ThingModels;
using JiShe.ServicePro.Dto;
using JiShe.ServicePro.Enums;
+using JiShe.ServicePro.FileManagement.Files;
using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.IoTDBManagement.DataChannels;
using JiShe.ServicePro.IoTDBManagement.TableModels;
@@ -18,6 +19,7 @@ using Mapster;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp;
+using Volo.Abp.Content;
namespace JiShe.IoT.DeviceAggregation
{
@@ -32,7 +34,11 @@ namespace JiShe.IoT.DeviceAggregation
/// IoTDBOptions
/// OneNET产品服务
/// 设备端物模型服务
- public class DeviceAggregationService(ILogger logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IReliableRedisPubSubService redisPubSubService, IIoTDBDataChannelManageService ioTDBDataChannelManageService,IOptions _ioTDBOptions, IOneNETProductService oneNETProductService, IDeviceThingModelManagementAppService deviceThingModelService) : IoTAppService, IDeviceAggregationService
+ /// 平台端端物模型服务
+ /// 设备固件服务
+ /// 设备升级记录服务
+ /// 文件管理服务
+ public class DeviceAggregationService(ILogger logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IReliableRedisPubSubService redisPubSubService, IIoTDBDataChannelManageService ioTDBDataChannelManageService, IOptions _ioTDBOptions, IOneNETProductService oneNETProductService, IDeviceThingModelManagementAppService deviceThingModelService, IIoTPlatformThingModelInfoAppService platformThingModelInfoAppService, IDeviceFirmwareInfoService deviceFirmwareInfoService, IDeviceUpgradeRecordService deviceUpgradeRecordService,IFileAppService fileAppService) : IoTAppService, IDeviceAggregationService
{
IoTDBOptions ioTDBOptions = _ioTDBOptions.Value;
@@ -255,14 +261,17 @@ namespace JiShe.IoT.DeviceAggregation
/// 更新设备信息并处理缓存
///
///
- ///
+ /// 推送结果原始信息
+ /// 设备接入鉴权key
///
///
- private async Task DeviceUpdateHandler(DeviceManagementInfoDto input, HttpDataResult pushResult)
+ private async Task DeviceUpdateHandler(DeviceManagementInfoDto input, HttpDataResult pushResult, string securityKey = null)
{
UpdateDeviceInput updateDeviceInput = input.Adapt();
updateDeviceInput.IoTPlatformResponse = pushResult.Serialize();
updateDeviceInput.IsPlatformPushSuccess = true;
+ updateDeviceInput.SecurityKey = securityKey;
+
var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput);
if (updateResult == null)
@@ -291,7 +300,7 @@ namespace JiShe.IoT.DeviceAggregation
{
try
{
- if (input.CommandContent == null || input.CommandContent.Keys.Count <=0)
+ if (input.CommandContent == null || input.CommandContent.Keys.Count <= 0)
{
throw new UserFriendlyException($"指令参数异常");
}
@@ -301,7 +310,6 @@ namespace JiShe.IoT.DeviceAggregation
{
throw new UserFriendlyException($"设备不存在");
}
-
//将指令存储
var receiveCommandInfoDto = new ReceiveCommandInfoDto()
{
@@ -314,7 +322,7 @@ namespace JiShe.IoT.DeviceAggregation
//数据写入遥测任务数据存储通道
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
- {
+ {
return await DeviceCommandInfoToOneNET(deviceInfo, receiveCommandInfoDto);
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
@@ -334,6 +342,68 @@ namespace JiShe.IoT.DeviceAggregation
}
}
+ ///
+ /// 发送设备升级指令信息
+ ///
+ ///
+ ///
+ public async Task DeviceUpgradeForApiAsync(DeviceUpgradeForApiInput input)
+ {
+ try
+ {
+ var deviceInfo = await deviceAppService.FindByIdAsync(input);
+ if (deviceInfo == null)
+ {
+ throw new UserFriendlyException($"设备不存在");
+ }
+
+ //将指令存储
+ var receiveCommandInfoDto = new ReceiveCommandInfoDto()
+ {
+ DeviceAddress = deviceInfo.DeviceAddress,
+ DeviceType = deviceInfo.DeviceType,
+ SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
+ IoTPlatform = deviceInfo.IoTPlatform,
+ };
+
+ //固件信息
+ var deviceFirmwareVersionInfo = await deviceFirmwareInfoService.FindByIdAsync(new IdInput() { Id = input.NowFirmwareVersionDataId });
+
+ var fileInfo = await fileAppService.DeleteAsync(new IdInput() { Id = fileInfo.Id });
+
+
+ //数据写入遥测任务数据存储通道
+ if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
+ {
+ return await DeviceUpgradeCommandToOneNET(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo);
+ }
+ 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;
+ }
+ }
+
+
+ ///
+ /// 下载设备固件文件
+ ///
+ [AllowAnonymous]
+ public async Task DownloadFirmwareInfoAsync(IdInput input)
+ {
+ return await fileAppService.AllowDownloadAsync(input);
+ }
+
///
/// 获取设备属性最新值
@@ -354,7 +424,7 @@ namespace JiShe.IoT.DeviceAggregation
{
throw new UserFriendlyException($"设备不存在");
}
-
+
//数据写入遥测任务数据存储通道
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
@@ -432,7 +502,7 @@ namespace JiShe.IoT.DeviceAggregation
try
{
CreateDeviceInput createDeviceInput = input.Adapt();
-
+
var productInfo = await oneNETProductService.GetProductInfoAsync(new IdInput() { Id = input.IoTPlatformProductId });
if (productInfo == null)
@@ -468,13 +538,13 @@ namespace JiShe.IoT.DeviceAggregation
Description = input.DeviceAddress,
});
- if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
+ if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success || pushResult.Data == null)
{
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}");
return false;
}
- await DeviceUpdateHandler(insertResult, pushResult);
+ await DeviceUpdateHandler(insertResult, pushResult, pushResult.Data.SecurityKey);
return true;
}
@@ -483,7 +553,7 @@ namespace JiShe.IoT.DeviceAggregation
throw;
}
- }
+ }
///
/// OneNET设备批量创建
@@ -541,7 +611,7 @@ namespace JiShe.IoT.DeviceAggregation
DeviceList = new List()
};
- oneNETBatchCreateDeviceInfoInput.DeviceList = batchCreateDeviceInput.DeviceInputs.Select(d => d.IoTPlatformDeviceOpenInfo).ToList();
+ oneNETBatchCreateDeviceInfoInput.DeviceList = batchCreateDeviceInput.DeviceInputs.Select(d => d.IoTPlatformDeviceOpenInfo).ToList();
var pushResult = await oneNETDeviceService.BatchCreateDeviceInfoAsync(oneNETBatchCreateDeviceInfoInput);
@@ -556,7 +626,7 @@ namespace JiShe.IoT.DeviceAggregation
var successEntity = pushResult.Data.Successlist.Where(d => d.DeviceName == item.IoTPlatformDeviceOpenInfo).FirstOrDefault();
if (successEntity != null)
{
- await DeviceUpdateHandler(item, HttpDataResultExtensions.Success(successEntity));
+ await DeviceUpdateHandler(item, HttpDataResultExtensions.Success(successEntity), successEntity.SecurityKey);
}
}
@@ -579,7 +649,7 @@ namespace JiShe.IoT.DeviceAggregation
public async Task RepushDeviceInfoToOneNET(DeviceManagementInfoDto input)
{
try
- {
+ {
//检查OneNET平台设备是否已经存在
var oneNETDeviceInfoResult = await oneNETDeviceService.DeviceInfoDetailAsync(new DeviceInfoDetailInput()
{
@@ -602,14 +672,14 @@ namespace JiShe.IoT.DeviceAggregation
Description = input.DeviceAddress,
});
- if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
+ if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success || pushResult.Data == null)
{
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}");
throw new UserFriendlyException($"平台请求失败。");
}
- return await DeviceUpdateHandler(input, pushResult);
+ return await DeviceUpdateHandler(input, pushResult, pushResult.Data.SecurityKey);
}
catch (Exception)
{
@@ -627,13 +697,13 @@ namespace JiShe.IoT.DeviceAggregation
public async Task DeleteDeviceInfoToOneNET(DeviceManagementInfoDto input)
{
try
- {
+ {
//删除OneNET平台设备信息
var deleteResult = await oneNETDeviceService.DeleteDeviceInfoAsync(new DeleteDeviceInfoInput()
{
DeviceName = input.IoTPlatformDeviceOpenInfo,
ProductId = input.IoTPlatformProductId,
- OneNETAccountId = input.IoTPlatformAccountId,
+ OneNETAccountId = input.IoTPlatformAccountId,
});
if (deleteResult == null || deleteResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
@@ -665,14 +735,39 @@ namespace JiShe.IoT.DeviceAggregation
///
///
///
- public async Task DeviceCommandInfoToOneNET(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto input)
+ protected async Task DeviceCommandInfoToOneNET(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto input)
{
try
- {
+ {
+
+ //获取设备对应的平台端物模型信息,校验前端传入的属性标识集合是否存在不合法的属性标识符
+
+ var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput() { Id = deviceInfo.IoTPlatformProductId });
+
+ if (platformThingModelInfo == null)
+ {
+ throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。");
+ }
+
+ foreach (var item in input.Commands)
+ {
+ var tempPlatformThingModelInfo = platformThingModelInfo.Where(d => d.IoTPlatformRawFieldName == item.Key).FirstOrDefault();
+ if (tempPlatformThingModelInfo == null)
+ {
+ throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}平台端物模型信息不存在属性标识符{item.Key}。");
+ }
+
+ //排除升级指令
+ if (tempPlatformThingModelInfo.StandardFieldName.ToLowerInvariant() == ThingModelFixedTypeConst.FIRMWARE_UPGRADE.ToLowerInvariant())
+ {
+ throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}平台端物模型属性标识符{item.Key}是升级指令,此处不允许下发。");
+ }
+ }
+
//检查设备是否有配置设备端物模型信息
//如果有配置,就检查指令字典中是否有SpecialCommand标识符
//如果有就需要构建 SpecialCommand 的特别指令
-
+
if (deviceInfo.IsNeedConfigDevicMdoel && deviceInfo.DeviceThingModelDataId.HasValue && input.Commands.ContainsKey(ThingModelFixedTypeConst.SpecialCommand))
{
var propertyInfo = await oneNETProductService.GetProductThingModelSpecialCommandDataTypeListAsync(new IdInput() { Id = deviceInfo.IoTPlatformProductId });
@@ -713,7 +808,93 @@ namespace JiShe.IoT.DeviceAggregation
throw new UserFriendlyException("设备不在线");
}
- await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo);
+ await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo);
+ return true;
+ }
+ catch (Exception)
+ {
+
+ throw;
+ }
+ }
+
+ ///
+ /// 发送OneNET平台设备升级指令
+ ///
+ ///
+ ///
+ /// 固件数据Id
+ ///
+ ///
+ protected async Task DeviceUpgradeCommandToOneNET(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto input, DeviceFirmwareInfoDto deviceFirmwareInfo)
+ {
+ try
+ {
+ if (deviceInfo == null || deviceFirmwareInfo == null)
+ {
+ throw new UserFriendlyException($"{nameof(DeviceUpgradeCommandToOneNET)}设备或固件信息不能为空");
+ }
+
+ if (deviceInfo.IoTPlatformProductId != deviceFirmwareInfo.IoTPlatformProductId)
+ {
+ throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}平台产品Id固件信息中不一致");
+ }
+
+ //获取设备对应的平台端物模型信息,校验前端传入的属性标识集合是否存在不合法的属性标识符
+ var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput() { Id = deviceInfo.IoTPlatformProductId });
+
+ if (platformThingModelInfo == null)
+ {
+ throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}的平台端物模型信息不存在。");
+ }
+
+ var upgradeProperty = platformThingModelInfo.Where(d => d.StandardFieldName == ThingModelFixedTypeConst.FIRMWARE_UPGRADE).FirstOrDefault();
+ if (upgradeProperty == null)
+ {
+ throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}平台端物模型信息不存在升级属性标识符{ThingModelFixedTypeConst.FIRMWARE_UPGRADE}。");
+ }
+
+ //构建升级指令,《文件MD5值+OneNET产品KEY+升级标识符+文件大小》=>MD5计算获得签名值
+
+ var upgradeRecordInput = new CreateDeviceUpgradeRecordInput()
+ {
+ DeviceName = deviceInfo.DeviceName,
+ DeviceAddress = deviceInfo.DeviceAddress,
+ OldFirmwareVersion = deviceInfo.FirmwareVersion,
+ NowFirmwareVersion = deviceFirmwareInfo.FirmwareVersion,
+ UpgradeSource = DeviceUpgradeSourceTypeEnum.AdminSystem,
+ UpgradeIdentifier = Yitter.IdGenerator.YitIdHelper.NextId(),
+ };
+
+ var insertResult = await deviceUpgradeRecordService.CreateAsync(upgradeRecordInput);
+
+ var commandRequest = new OpenApiRequest()
+ {
+ Message = input.Serialize(),
+ };
+ var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, commandRequest, deviceInfo.Adapt(), input.Commands.Serialize());
+
+ await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo));
+
+ //检查下设备是否在线
+ 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("设备不在线");
+ }
+
+ await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo);
return true;
}
catch (Exception)
@@ -730,10 +911,10 @@ namespace JiShe.IoT.DeviceAggregation
///
///
///
- public async Task> DevicePropertyValueToOneNET(DeviceManagementInfoDto deviceInfo, DevicePropertyValueForApiInput input)
+ protected async Task> DevicePropertyValueToOneNET(DeviceManagementInfoDto deviceInfo, DevicePropertyValueForApiInput input)
{
try
- {
+ {
//检查下设备是否在线
var deviceOnlineStatus = await oneNETDeviceService.DeviceInfoDetailAsync(new DeviceInfoDetailInput()
{
diff --git a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
index 5871a88..4dea2c6 100644
--- a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
+++ b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
@@ -3,6 +3,7 @@ using JiShe.IoT.DeviceAggregation.Dto;
using JiShe.ServicePro;
using JiShe.ServicePro.Core;
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
+using Volo.Abp.Content;
namespace JiShe.IoT.Controllers
{
@@ -99,5 +100,30 @@ namespace JiShe.IoT.Controllers
return _deviceAggregationService.GetDevicePropertyValueForApiAsync(input);
}
+ ///
+ /// 发送设备升级指令信息
+ ///
+ ///
+ ///
+ [HttpPost(nameof(DeviceUpgradeForApiAsync))]
+ [SwaggerOperation(summary: "发送设备升级指令信息", Tags = new[] { "AggregationDevice" })]
+ public Task DeviceUpgradeForApiAsync(DeviceUpgradeForApiInput input)
+ {
+ return _deviceAggregationService.DeviceUpgradeForApiAsync(input);
+ }
+
+ ///
+ /// 下载设备固件文件
+ ///
+ ///
+ ///
+ [HttpPost("DownloadFirmware")]
+ [HttpGet("DownloadFirmware")]
+ [SwaggerOperation(summary: "下载设备固件文件", Tags = new[] { "AggregationDevice" })]
+ public Task DownloadFirmwareInfoAsync(IdInput input)
+ {
+ return _deviceAggregationService.DownloadFirmwareInfoAsync(input);
+ }
+
}
}