Compare commits

..

No commits in common. "8ef2e83791af641d91e62ceb1a8182d77db28fc9" and "ecfc7d0b4fa868c2f116183984f4ec90730c38ec" have entirely different histories.

3 changed files with 89 additions and 160 deletions

View File

@ -56,11 +56,11 @@ namespace JiShe.IoT.DeviceAggregation
Task<bool> DeviceUpgradeForApiAsync(DeviceUpgradeForApiInput input);
/// <summary>
/// 批量发送设备升级指令信息,只对同一个平台下,同一个产品下的设备进行固件升级信息
/// 批量发送设备升级指令信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Task<List<string>> DeviceBatchUpgradeForApiAsync(DeviceBatchUpgradeForApiInput input);
Task<bool> DeviceBatchUpgradeForApiAsync(DeviceBatchUpgradeForApiInput input);
/// <summary>
/// 下载设备固件文件

View File

@ -347,115 +347,6 @@ namespace JiShe.IoT.DeviceAggregation
}
}
/// <summary>
/// 验证并获取固件信息和文件信息
/// </summary>
/// <param name="firmwareVersionDataId">固件版本数据Id</param>
/// <param name="iotPlatformProductId">物联网平台产品Id</param>
/// <param name="firmwareNotFoundErrorCode">固件信息不存在的错误代码(可选,用于批量操作时的错误代码)</param>
/// <param name="fileNotFoundErrorCode">文件信息不存在的错误代码(可选,用于批量操作时的错误代码)</param>
/// <returns>返回固件信息和文件信息的元组</returns>
private async Task<(DeviceFirmwareInfoDto FirmwareInfo, FileObjectDto FileInfo)> ValidateAndGetFirmwareInfoAsync(Guid firmwareVersionDataId, string iotPlatformProductId, string firmwareNotFoundErrorCode = null, string fileNotFoundErrorCode = null)
{
var deviceFirmwareVersionInfo = await deviceFirmwareInfoService.FindByIdAsync(new IdInput() { Id = firmwareVersionDataId });
if (deviceFirmwareVersionInfo == null)
{
var errorMsg = $"产品Id{iotPlatformProductId}的新固件信息{firmwareVersionDataId}不存在";
throw new UserFriendlyException(errorMsg, firmwareNotFoundErrorCode ?? string.Empty);
}
var fileInfo = await fileAppService.GetFileAsync(new IdInput() { Id = deviceFirmwareVersionInfo.FirmwareFileId });
if (fileInfo == null)
{
var errorMsg = $"产品Id{iotPlatformProductId}的新固件信息{deviceFirmwareVersionInfo.FirmwareFileName}固件文件不存在";
throw new UserFriendlyException(errorMsg, fileNotFoundErrorCode ?? string.Empty);
}
return (deviceFirmwareVersionInfo, fileInfo);
}
/// <summary>
/// 创建接收指令信息Dto
/// </summary>
/// <param name="deviceInfo">设备信息</param>
/// <returns>接收指令信息Dto</returns>
private ReceiveCommandInfoDto CreateReceiveCommandInfoDto(DeviceManagementInfoDto deviceInfo)
{
return new ReceiveCommandInfoDto()
{
DeviceAddress = deviceInfo.DeviceAddress,
DeviceType = deviceInfo.DeviceType,
SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
IoTPlatform = deviceInfo.IoTPlatform,
};
}
/// <summary>
/// 获取OneNET产品信息和平台端物模型信息
/// </summary>
/// <param name="iotPlatformProductId">物联网平台产品Id</param>
/// <param name="deviceAddress">设备地址(用于错误信息)</param>
/// <returns>返回产品信息和平台端物模型信息的元组</returns>
private async Task<(dynamic ProductInfo, dynamic PlatformThingModelInfo)> GetOneNETProductAndThingModelInfoAsync(string iotPlatformProductId, string deviceAddress)
{
//获取设备对应的产品信息
var productInfo = await oneNETProductService.GetProductInfoAsync(new IdInput<string>() { Id = iotPlatformProductId });
if (productInfo == null)
{
throw new UserFriendlyException($"{nameof(DeviceUpgradeCommandToOneNET)} OneNET设备升级属性设置失败产品Id{iotPlatformProductId}未找到对应的产品信息。");
}
//获取设备对应的平台端物模型信息,校验前端传入的属性标识集合是否存在不合法的属性标识符
var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput<string>() { Id = iotPlatformProductId });
if (platformThingModelInfo == null)
{
throw new UserFriendlyException($"设备{deviceAddress}的平台端物模型信息不存在。");
}
return (productInfo, platformThingModelInfo);
}
/// <summary>
/// 根据平台处理设备升级指令
/// </summary>
/// <param name="deviceInfo">设备信息</param>
/// <param name="receiveCommandInfoDto">接收指令信息Dto</param>
/// <param name="deviceFirmwareVersionInfo">固件版本信息</param>
/// <param name="fileInfo">文件信息</param>
/// <param name="input">升级输入参数</param>
/// <param name="oneNETProductInfo">OneNET产品信息可选如果已获取则传入以避免重复调用</param>
/// <param name="oneNETPlatformThingModelInfo">OneNET平台端物模型信息可选如果已获取则传入以避免重复调用</param>
/// <returns>处理结果</returns>
private async Task<bool> ProcessDeviceUpgradeByPlatformAsync(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto receiveCommandInfoDto, DeviceFirmwareInfoDto deviceFirmwareVersionInfo, FileObjectDto fileInfo, DeviceUpgradeForApiInput input, dynamic oneNETProductInfo = null, dynamic oneNETPlatformThingModelInfo = null)
{
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
//如果未传入产品信息和物模型信息,则获取
if (oneNETProductInfo == null || oneNETPlatformThingModelInfo == null)
{
var (productInfo, platformThingModelInfo) = await GetOneNETProductAndThingModelInfoAsync(deviceInfo.IoTPlatformProductId, deviceInfo.DeviceAddress);
oneNETProductInfo = productInfo;
oneNETPlatformThingModelInfo = platformThingModelInfo;
}
return await DeviceUpgradeCommandToOneNET(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo, fileInfo, input, oneNETProductInfo, oneNETPlatformThingModelInfo);
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
//await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName,commandRequest);
//return true;
throw new UserFriendlyException($"发送设备升级指令信息失败CTWing暂未实现。");
}
else
{
throw new UserFriendlyException($"发送设备升级指令信息失败,未找到对应的产品配置信息。");
}
}
/// <summary>
/// 发送设备升级指令信息
/// </summary>
@ -471,11 +362,45 @@ namespace JiShe.IoT.DeviceAggregation
throw new UserFriendlyException($"设备{input.Id}不存在");
}
var receiveCommandInfoDto = CreateReceiveCommandInfoDto(deviceInfo);
//将指令存储
var receiveCommandInfoDto = new ReceiveCommandInfoDto()
{
DeviceAddress = deviceInfo.DeviceAddress,
DeviceType = deviceInfo.DeviceType,
SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
IoTPlatform = deviceInfo.IoTPlatform,
};
var (deviceFirmwareVersionInfo, fileInfo) = await ValidateAndGetFirmwareInfoAsync(input.NowFirmwareVersionDataId, input.IoTPlatformProductId);
//固件信息
var deviceFirmwareVersionInfo = await deviceFirmwareInfoService.FindByIdAsync(new IdInput() { Id = input.NowFirmwareVersionDataId });
return await ProcessDeviceUpgradeByPlatformAsync(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo, fileInfo, input);
if (deviceFirmwareVersionInfo == null)
{
throw new UserFriendlyException($"产品Id{input.IoTPlatformProductId}的新固件信息{input.NowFirmwareVersionDataId}不存在");
}
var fileInfo = await fileAppService.GetFileAsync(new IdInput() { Id = deviceFirmwareVersionInfo.FirmwareFileId });
if (fileInfo == null)
{
throw new UserFriendlyException($"产品Id{input.IoTPlatformProductId}的新固件信息{deviceFirmwareVersionInfo.FirmwareFileName}固件文件不存在");
}
//数据写入遥测任务数据存储通道
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
return await DeviceUpgradeCommandToOneNET(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo, fileInfo, 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)
{
@ -484,11 +409,11 @@ namespace JiShe.IoT.DeviceAggregation
}
/// <summary>
/// 批量发送设备升级指令信息,只对同一个平台下,同一个产品下的设备进行固件升级信息
/// 批量发送设备升级指令信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<List<string>> DeviceBatchUpgradeForApiAsync(DeviceBatchUpgradeForApiInput input)
public async Task<bool> DeviceBatchUpgradeForApiAsync(DeviceBatchUpgradeForApiInput input)
{
try
{
@ -497,61 +422,59 @@ namespace JiShe.IoT.DeviceAggregation
throw new UserFriendlyException($"{nameof(DeviceBatchUpgradeForApiAsync)} 设备批量升级时地址列表不能为空","-101");
}
if (input.AddressList.Count >200)
//固件信息
var deviceFirmwareVersionInfo = await deviceFirmwareInfoService.FindByIdAsync(new IdInput() { Id = input.NowFirmwareVersionDataId });
if (deviceFirmwareVersionInfo == null)
{
throw new UserFriendlyException($"{nameof(DeviceBatchUpgradeForApiAsync)} 设备批量升级时地址列表数量不能超过200", "-102");
throw new UserFriendlyException($"产品Id{input.IoTPlatformProductId}的新固件信息{input.NowFirmwareVersionDataId}不存在");
}
var (deviceFirmwareVersionInfo, fileInfo) = await ValidateAndGetFirmwareInfoAsync(input.NowFirmwareVersionDataId, input.IoTPlatformProductId, "-103", "-104");
var fileInfo = await fileAppService.GetFileAsync(new IdInput() { Id = deviceFirmwareVersionInfo.FirmwareFileId });
var deviceInfoList = await deviceAppService.FindByDeviceAddressAsync(new FindByDeviceAddressInput() { AddressList = input.AddressList ,IoTPlatform = input.IoTPlatform,IoTPlatformProductId = input.IoTPlatformProductId});
//如果是OneNET平台提前获取产品信息和物模型信息避免在循环中重复调用
dynamic oneNETProductInfo = null;
dynamic oneNETPlatformThingModelInfo = null;
if (input.IoTPlatform == IoTPlatformTypeEnum.OneNET && deviceInfoList != null && deviceInfoList.Count > 0)
if (fileInfo == null)
{
var firstDevice = deviceInfoList.FirstOrDefault();
if (firstDevice != null)
{
var (productInfo, platformThingModelInfo) = await GetOneNETProductAndThingModelInfoAsync(firstDevice.IoTPlatformProductId, firstDevice.DeviceAddress);
oneNETProductInfo = productInfo;
oneNETPlatformThingModelInfo = platformThingModelInfo;
}
throw new UserFriendlyException($"产品Id{input.IoTPlatformProductId}的新固件信息{deviceFirmwareVersionInfo.FirmwareFileName}固件文件不存在");
}
List<string> deviceAddress = new List<string>();//异常的设备地址集合
foreach (var item in input.AddressList)
{
var deviceInfo = deviceInfoList.Where(d=>d.DeviceAddress== item).FirstOrDefault();
var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(item);
if (deviceInfo == null)
{
deviceAddress.Add(item);
continue;
throw new UserFriendlyException($"{nameof(DeviceBatchUpgradeForApiAsync)} 设备{item}不存在", "-102");
}
var receiveCommandInfoDto = CreateReceiveCommandInfoDto(deviceInfo);
//将指令存储
var receiveCommandInfoDto = new ReceiveCommandInfoDto()
{
DeviceAddress = deviceInfo.DeviceAddress,
DeviceType = deviceInfo.DeviceType,
SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
IoTPlatform = deviceInfo.IoTPlatform,
};
var deviceUpgradeRecordInput = input.Adapt<DeviceUpgradeForApiInput>();
deviceUpgradeRecordInput.Id = deviceInfo.Id;
try
//数据写入遥测任务数据存储通道
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
var oneNETHandleResult = await ProcessDeviceUpgradeByPlatformAsync(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo, fileInfo, deviceUpgradeRecordInput, oneNETProductInfo, oneNETPlatformThingModelInfo);
if (!oneNETHandleResult)
{
deviceAddress.Add(item);
}
return await DeviceUpgradeCommandToOneNET(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo, fileInfo, deviceUpgradeRecordInput);
}
catch (Exception)
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
deviceAddress.Add(item);
//await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName,commandRequest);
//return true;
throw new UserFriendlyException($"发送设备升级指令信息失败CTWing暂未实现。");
}
else
{
throw new UserFriendlyException($"发送设备升级指令信息失败,未找到对应的产品配置信息。");
}
}
return deviceAddress;
return true;
}
catch (Exception)
{
@ -1018,11 +941,9 @@ namespace JiShe.IoT.DeviceAggregation
/// <param name="deviceFirmwareInfo">固件数据Id</param>
/// <param name="fileObject">固件文件信息</param>
/// <param name="input">入参</param>
/// <param name="productInfo">产品信息(如果已获取则传入以避免重复调用)</param>
/// <param name="platformThingModelInfo">平台端物模型信息(如果已获取则传入以避免重复调用)</param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
protected async Task<bool> DeviceUpgradeCommandToOneNET(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto taskInput, DeviceFirmwareInfoDto deviceFirmwareInfo, FileObjectDto fileObject, DeviceUpgradeForApiInput input, dynamic productInfo = null, dynamic platformThingModelInfo = null)
protected async Task<bool> DeviceUpgradeCommandToOneNET(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto taskInput, DeviceFirmwareInfoDto deviceFirmwareInfo, FileObjectDto fileObject, DeviceUpgradeForApiInput input)
{
try
{
@ -1036,15 +957,23 @@ namespace JiShe.IoT.DeviceAggregation
throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}平台产品信息固件中不一致");
}
//如果未传入产品信息和物模型信息,则获取
if (productInfo == null || platformThingModelInfo == null)
//获取设备对应的产品信息
var productInfo = await oneNETProductService.GetProductInfoAsync(new IdInput<string>() { Id = deviceInfo.IoTPlatformProductId });
if (productInfo == null)
{
var (productInfoResult, platformThingModelInfoResult) = await GetOneNETProductAndThingModelInfoAsync(deviceInfo.IoTPlatformProductId, deviceInfo.DeviceAddress);
productInfo = productInfoResult;
platformThingModelInfo = platformThingModelInfoResult;
throw new UserFriendlyException($"{nameof(DeviceUpgradeCommandToOneNET)} OneNET设备升级属性设置失败产品Id{deviceInfo.IoTPlatformProductId}未找到对应的产品信息。");
}
var upgradeProperty = ((List<IoTPlatformThingModelInfoDto>)platformThingModelInfo).Where(d => d.StandardFieldName == ThingModelFixedTypeConst.FIRMWARE_UPGRADE).FirstOrDefault();
//获取设备对应的平台端物模型信息,校验前端传入的属性标识集合是否存在不合法的属性标识符
var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput<string>() { 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}。");

View File

@ -114,13 +114,13 @@ namespace JiShe.IoT.Controllers
}
/// <summary>
/// 批量发送设备升级指令信息,只对同一个平台下,同一个产品下的设备进行固件升级信息
/// 批量发送设备升级指令信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost(nameof(DeviceBatchUpgradeForApiAsync))]
[SwaggerOperation(summary: "批量发送设备升级指令信息,只对同一个平台下,同一个产品下的设备进行固件升级信息", Tags = new[] { "AggregationDevice" })]
public Task<List<string>> DeviceBatchUpgradeForApiAsync(DeviceBatchUpgradeForApiInput input)
[SwaggerOperation(summary: "批量发送设备升级指令信息", Tags = new[] { "AggregationDevice" })]
public Task<bool> DeviceBatchUpgradeForApiAsync(DeviceBatchUpgradeForApiInput input)
{
return _deviceAggregationService.DeviceBatchUpgradeForApiAsync(input);
}