1246 lines
55 KiB
C#
Raw Normal View History

using JiShe.IoT.DeviceAggregation.Dto;
using JiShe.ServicePro;
2025-09-19 11:36:17 +08:00
using JiShe.ServicePro.ApacheIoTDB.Provider.Options;
using JiShe.ServicePro.Core;
2025-09-19 11:36:17 +08:00
using JiShe.ServicePro.DataChannelManages;
2025-07-25 17:27:21 +08:00
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
using JiShe.ServicePro.DeviceManagement.DeviceInfos.Dto;
using JiShe.ServicePro.DeviceManagement.Permissions;
using JiShe.ServicePro.DeviceManagement.ThingModels;
2025-08-04 17:29:47 +08:00
using JiShe.ServicePro.Dto;
2025-12-31 16:46:36 +08:00
using JiShe.ServicePro.Encrypt;
2025-08-04 11:59:07 +08:00
using JiShe.ServicePro.Enums;
using JiShe.ServicePro.FileManagement.Files;
using JiShe.ServicePro.FreeRedisProvider;
2025-09-19 11:36:17 +08:00
using JiShe.ServicePro.IoTDBManagement.DataChannels;
using JiShe.ServicePro.IoTDBManagement.TableModels;
2025-12-31 16:46:36 +08:00
using JiShe.ServicePro.OneNET.Provider.OpenApiModels.Commands;
using JiShe.ServicePro.OneNETManagement.OneNETDevices;
using JiShe.ServicePro.OneNETManagement.OneNETProducts;
using Mapster;
using Microsoft.Extensions.Logging;
2025-09-19 11:36:17 +08:00
using Microsoft.Extensions.Options;
2026-01-04 16:22:49 +08:00
using StackExchange.Redis;
using Volo.Abp;
using Volo.Abp.Content;
namespace JiShe.IoT.DeviceAggregation
{
/// <summary>
/// 设备聚合服务
/// </summary>
/// <param name="logger"></param>
/// <param name="deviceAppService">设备服务</param>
/// <param name="oneNETDeviceService">OneNET设备服务</param>
2025-08-21 11:34:16 +08:00
/// <param name="redisPubSubService">Redis发布订阅服务</param>
2025-09-19 11:36:17 +08:00
/// <param name="ioTDBDataChannelManageService">数据通道</param>
/// <param name="_ioTDBOptions">IoTDBOptions</param>
/// <param name="oneNETProductService">OneNET产品服务</param>
/// <param name="deviceThingModelService">设备端物模型服务</param>
/// <param name="platformThingModelInfoAppService">平台端端物模型服务</param>
/// <param name="deviceFirmwareInfoService">设备固件服务</param>
/// <param name="deviceUpgradeRecordService">设备升级记录服务</param>
/// <param name="fileAppService">文件管理服务</param>
2025-12-31 16:46:36 +08:00
/// <param name="_serverOptions">应用服务配置</param>
public class DeviceAggregationService(ILogger<DeviceAggregationService> logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IReliableRedisPubSubService redisPubSubService, IIoTDBDataChannelManageService ioTDBDataChannelManageService, IOptions<IoTDBOptions> _ioTDBOptions, IOptions<ServerApplicationOptions> _serverOptions, IOneNETProductService oneNETProductService, IDeviceThingModelManagementAppService deviceThingModelService, IIoTPlatformThingModelInfoAppService platformThingModelInfoAppService, IDeviceFirmwareInfoService deviceFirmwareInfoService, IDeviceUpgradeRecordService deviceUpgradeRecordService, IFileAppService fileAppService) : IoTAppService, IDeviceAggregationService
{
2025-09-19 11:36:17 +08:00
IoTDBOptions ioTDBOptions = _ioTDBOptions.Value;
2025-12-31 16:46:36 +08:00
ServerApplicationOptions serverApplicationOptions = _serverOptions.Value;
2025-09-19 11:36:17 +08:00
/// <summary>
2025-08-01 14:59:11 +08:00
/// 管理后台创建设备信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2025-07-25 11:54:19 +08:00
[Authorize(DeviceManagementPermissions.DeviceInfoManagement.Create)]
2025-08-01 14:59:11 +08:00
public async Task<bool> CreateDeviceForApiAsync(CreateDeviceAggregationInput input)
{
try
{
2025-08-04 11:59:07 +08:00
input.DeviceSourceTypeEnum = ServicePro.Enums.DeviceSourceTypeEnum.AdminSystem;
2025-08-01 14:59:11 +08:00
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;
}
}
2025-08-04 11:59:07 +08:00
/// <summary>
/// 管理后台批量创建设备信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2025-10-17 17:25:33 +08:00
[Authorize(DeviceManagementPermissions.DeviceInfoManagement.Create)]
2025-08-04 11:59:07 +08:00
public async Task<bool> BatchCreateDeviceForApiAsync(BatchCreateDeviceAggregationInput input)
{
try
{
if (input.AddressList == null || input.AddressList.Count <= 0)
{
throw new UserFriendlyException($"批量创建设备失败,设备信息不能为空。");
}
if (input.AddressList.Count > 100)
{
throw new UserFriendlyException($"批量创建设备失败设备信息不能超过100个。");
}
input.DeviceSourceTypeEnum = ServicePro.Enums.DeviceSourceTypeEnum.AdminSystem;
if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.CTWing)
{
return await CTWingDeviceBatchCreateAsync(input);
}
else if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.OneNET)
{
return await OneNETDeviceBatchCreateAsync(input);
}
throw new UserFriendlyException($"不支持的物联网平台");
}
catch (Exception)
{
throw;
}
}
2025-08-01 14:59:11 +08:00
/// <summary>
/// 车间创建设备信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<bool> CreateDeviceWorkshopAsync(CreateDeviceAggregationInput input)
{
try
{
2025-08-04 11:59:07 +08:00
input.DeviceSourceTypeEnum = DeviceSourceTypeEnum.Workshop;
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;
}
}
/// <summary>
2025-08-04 11:59:07 +08:00
/// 车间批量创建设备信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2025-08-04 11:59:07 +08:00
public async Task<bool> BatchCreateDeviceWorkshopAsync(BatchCreateDeviceAggregationInput input)
{
try
{
2025-08-04 11:59:07 +08:00
if (input.AddressList == null || input.AddressList.Count <= 0)
{
2025-08-04 11:59:07 +08:00
throw new UserFriendlyException($"批量创建设备失败,设备信息不能为空。");
}
2025-08-04 11:59:07 +08:00
if (input.AddressList.Count > 100)
{
2025-08-04 11:59:07 +08:00
throw new UserFriendlyException($"批量创建设备失败设备信息不能超过100个。");
}
2025-08-04 11:59:07 +08:00
input.DeviceSourceTypeEnum = DeviceSourceTypeEnum.Workshop;
2025-08-04 11:59:07 +08:00
if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.CTWing)
{
2025-08-04 11:59:07 +08:00
return await CTWingDeviceBatchCreateAsync(input);
}
2025-08-04 11:59:07 +08:00
else if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.OneNET)
{
2025-08-04 11:59:07 +08:00
return await OneNETDeviceBatchCreateAsync(input);
}
2025-08-04 11:59:07 +08:00
throw new UserFriendlyException($"不支持的物联网平台");
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 删除设备信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
2025-07-25 11:54:19 +08:00
[Authorize(DeviceManagementPermissions.DeviceInfoManagement.Delete)]
public async Task<bool> DeleteAsync(IdInput input)
{
2025-08-04 11:59:07 +08:00
//检查设备信息是不是已经存在
var existsDeviceInfo = await deviceAppService.FindByIdAsync(input);
if (existsDeviceInfo == null)
{
throw new UserFriendlyException($"删除设备失败,未找到对应设备信息。");
}
//根据设备平台调用删除接口
if (existsDeviceInfo.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.OneNET)
{
return await DeleteDeviceInfoToOneNET(existsDeviceInfo);
}
else if (existsDeviceInfo.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.CTWing)
{
return await DeleteDeviceInfoToCTWing(existsDeviceInfo);
}
2025-08-04 11:59:07 +08:00
throw new UserFriendlyException($"不支持的物联网平台");
}
/// <summary>
/// 根据设备ID查询设备信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task<DeviceManagementInfoDto> FindByIdAsync(IdInput input)
{
return await deviceAppService.FindByIdAsync(input);
}
/// <summary>
/// 重新推送设备信息到物联网平台
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task<DeviceManagementInfoDto> RepushDeviceInfoToIoTPlatform(IdInput input)
{
try
{
var entityDevice = await FreeSqlDbContext.Instance.Select<DeviceManagementInfo>().Where(f => f.Id == input.Id).FirstAsync<DeviceManagementInfoDto>();
if (entityDevice == null)
{
throw new UserFriendlyException($"推送失败,未找到设备数据");
}
if (entityDevice.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.CTWing)
{
return await RepushDeviceInfoToCTWing(entityDevice);
}
else if (entityDevice.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.OneNET)
{
return await RepushDeviceInfoToOneNET(entityDevice);
}
throw new UserFriendlyException($"推送失败,异常的物联网平台");
}
catch (Exception)
{
throw;
}
}
/// <summary>
2025-08-04 11:59:07 +08:00
/// 更新设备信息并处理缓存
/// </summary>
/// <param name="input"></param>
/// <param name="pushResult">推送结果原始信息</param>
/// <param name="securityKey">设备接入鉴权key</param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
private async Task<DeviceManagementInfoDto> DeviceUpdateHandler(DeviceManagementInfoDto input, HttpDataResult pushResult, string securityKey = null)
2025-08-04 11:59:07 +08:00
{
UpdateDeviceInput updateDeviceInput = input.Adapt<UpdateDeviceInput>();
updateDeviceInput.IoTPlatformResponse = pushResult.Serialize();
updateDeviceInput.IsPlatformPushSuccess = true;
updateDeviceInput.SecurityKey = securityKey;
2025-08-04 11:59:07 +08:00
var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput);
if (updateResult == null)
{
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 更新设备信息失败:{input.Serialize()}");
throw new UserFriendlyException($"推送结果更新失败。");
}
//设备数据缓存到Redis
DeviceCacheInfos deviceCacheInfos = input.Adapt<DeviceCacheInfos>();
deviceCacheInfos.PlatformPassword = null;
FreeRedisProvider.Instance.HSet<DeviceCacheInfos>(RedisConst.CacheAllDeviceInfoHashKey, input.DeviceAddress, deviceCacheInfos);
2025-08-04 11:59:07 +08:00
2025-08-04 11:59:07 +08:00
return input.Adapt<DeviceManagementInfoDto>();
}
2025-08-04 17:29:47 +08:00
/// <summary>
/// 发送设备指令信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<bool> DeviceCommandForApiAsync(DeviceCommandForApiInput input)
{
try
{
if (input.CommandContent == null || input.CommandContent.Keys.Count <= 0)
{
throw new UserFriendlyException($"指令参数异常");
}
var deviceInfo = await deviceAppService.FindByIdAsync(input);
2025-09-19 11:36:17 +08:00
if (deviceInfo == null)
{
throw new UserFriendlyException($"设备不存在");
}
2025-12-09 17:24:27 +08:00
//将指令存储
var receiveCommandInfoDto = new ReceiveCommandInfoDto()
{
DeviceAddress = deviceInfo.DeviceAddress,
Commands = input.CommandContent,
DeviceType = deviceInfo.DeviceType,
SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
IoTPlatform = deviceInfo.IoTPlatform,
};
2025-10-29 15:19:18 +08:00
//数据写入遥测任务数据存储通道
2025-08-04 17:29:47 +08:00
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
2025-12-09 17:24:27 +08:00
return await DeviceCommandInfoToOneNET(deviceInfo, receiveCommandInfoDto);
2025-08-04 17:29:47 +08:00
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
//await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName,commandRequest);
//return true;
throw new UserFriendlyException($"发送设备指令信息失败CTWing暂未实现。");
2025-08-04 17:29:47 +08:00
}
else
{
throw new UserFriendlyException($"发送设备指令信息失败,未找到对应的产品配置信息。");
2025-08-04 17:29:47 +08:00
}
}
catch (Exception)
{
throw;
}
}
2026-01-04 17:06:41 +08:00
/// <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>
/// <param name="input"></param>
/// <returns></returns>
public async Task<bool> DeviceUpgradeForApiAsync(DeviceUpgradeForApiInput input)
{
try
{
var deviceInfo = await deviceAppService.FindByIdAsync(input);
if (deviceInfo == null)
{
2025-12-31 16:46:36 +08:00
throw new UserFriendlyException($"设备{input.Id}不存在");
}
2026-01-04 17:06:41 +08:00
var receiveCommandInfoDto = CreateReceiveCommandInfoDto(deviceInfo);
2025-12-31 16:46:36 +08:00
2026-01-04 17:06:41 +08:00
var (deviceFirmwareVersionInfo, fileInfo) = await ValidateAndGetFirmwareInfoAsync(input.NowFirmwareVersionDataId, input.IoTPlatformProductId);
2026-01-04 17:06:41 +08:00
return await ProcessDeviceUpgradeByPlatformAsync(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo, fileInfo, input);
}
catch (Exception)
{
throw;
}
}
2026-01-04 16:22:49 +08:00
/// <summary>
2026-01-04 16:47:54 +08:00
/// 批量发送设备升级指令信息,只对同一个平台下,同一个产品下的设备进行固件升级信息
2026-01-04 16:22:49 +08:00
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2026-01-04 16:47:54 +08:00
public async Task<List<string>> DeviceBatchUpgradeForApiAsync(DeviceBatchUpgradeForApiInput input)
2026-01-04 16:22:49 +08:00
{
try
{
if(input.AddressList == null || input.AddressList.Count <= 0)
{
throw new UserFriendlyException($"{nameof(DeviceBatchUpgradeForApiAsync)} 设备批量升级时地址列表不能为空","-101");
}
2026-01-04 16:47:54 +08:00
if (input.AddressList.Count >200)
{
throw new UserFriendlyException($"{nameof(DeviceBatchUpgradeForApiAsync)} 设备批量升级时地址列表数量不能超过200", "-102");
}
2026-01-04 17:06:41 +08:00
var (deviceFirmwareVersionInfo, fileInfo) = await ValidateAndGetFirmwareInfoAsync(input.NowFirmwareVersionDataId, input.IoTPlatformProductId, "-103", "-104");
2026-01-04 16:22:49 +08:00
2026-01-04 17:06:41 +08:00
var deviceInfoList = await deviceAppService.FindByDeviceAddressAsync(new FindByDeviceAddressInput() { AddressList = input.AddressList ,IoTPlatform = input.IoTPlatform,IoTPlatformProductId = input.IoTPlatformProductId});
2026-01-04 16:22:49 +08:00
2026-01-04 17:06:41 +08:00
//如果是OneNET平台提前获取产品信息和物模型信息避免在循环中重复调用
dynamic oneNETProductInfo = null;
dynamic oneNETPlatformThingModelInfo = null;
if (input.IoTPlatform == IoTPlatformTypeEnum.OneNET && deviceInfoList != null && deviceInfoList.Count > 0)
2026-01-04 16:22:49 +08:00
{
2026-01-04 17:06:41 +08:00
var firstDevice = deviceInfoList.FirstOrDefault();
if (firstDevice != null)
{
var (productInfo, platformThingModelInfo) = await GetOneNETProductAndThingModelInfoAsync(firstDevice.IoTPlatformProductId, firstDevice.DeviceAddress);
oneNETProductInfo = productInfo;
oneNETPlatformThingModelInfo = platformThingModelInfo;
}
2026-01-04 16:22:49 +08:00
}
2026-01-04 16:47:54 +08:00
List<string> deviceAddress = new List<string>();//异常的设备地址集合
2026-01-04 16:22:49 +08:00
foreach (var item in input.AddressList)
{
2026-01-04 16:47:54 +08:00
var deviceInfo = deviceInfoList.Where(d=>d.DeviceAddress== item).FirstOrDefault();
2026-01-04 16:22:49 +08:00
if (deviceInfo == null)
{
2026-01-04 16:47:54 +08:00
deviceAddress.Add(item);
continue;
2026-01-04 16:22:49 +08:00
}
2026-01-04 17:06:41 +08:00
var receiveCommandInfoDto = CreateReceiveCommandInfoDto(deviceInfo);
2026-01-04 16:22:49 +08:00
var deviceUpgradeRecordInput = input.Adapt<DeviceUpgradeForApiInput>();
deviceUpgradeRecordInput.Id = deviceInfo.Id;
2026-01-04 17:06:41 +08:00
try
2026-01-04 16:22:49 +08:00
{
2026-01-04 17:06:41 +08:00
var oneNETHandleResult = await ProcessDeviceUpgradeByPlatformAsync(deviceInfo, receiveCommandInfoDto, deviceFirmwareVersionInfo, fileInfo, deviceUpgradeRecordInput, oneNETProductInfo, oneNETPlatformThingModelInfo);
2026-01-04 16:47:54 +08:00
if (!oneNETHandleResult)
{
deviceAddress.Add(item);
}
2026-01-04 16:22:49 +08:00
}
2026-01-04 17:06:41 +08:00
catch (Exception)
2026-01-04 16:22:49 +08:00
{
2026-01-04 17:06:41 +08:00
deviceAddress.Add(item);
2026-01-04 16:22:49 +08:00
}
}
2026-01-04 16:47:54 +08:00
return deviceAddress;
2026-01-04 16:22:49 +08:00
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 下载设备固件文件
/// </summary>
[AllowAnonymous]
public async Task<RemoteStreamContent> DownloadFirmwareInfoAsync(IdInput input)
{
2026-01-04 16:22:49 +08:00
try
{
// Lua 脚本:获取并删除键(原子操作)
string cacheKey = string.Format($"{RedisConst.CacheDeviceUpgradeRecordDataKey}", input.Id);
var redisResult = await FreeRedisProvider.Instance.GetDelAsync(cacheKey);
if (string.IsNullOrWhiteSpace(redisResult))
{
throw new UserFriendlyException($"{nameof(DownloadFirmwareInfoAsync)} 设备升级记录信息 {input.Id} 不存在");
}
var downLoadResult = await fileAppService.AllowDownloadAsync(input);
if (downLoadResult.ContentLength.HasValue && downLoadResult.ContentLength.Value > 0)
{
//更新状态为文件下载中
await deviceUpgradeRecordService.UpdateStatusAsync(new UpdateStatusInput()
{
Id = input.Id,
UpgradeStatus = DeviceUpgradeStatusTypeEnum.Downloading,
});
}
return downLoadResult;
}
catch (Exception ex)
{
logger.LogError($"{nameof(DownloadFirmwareInfoAsync)}{input.Id}下载设备固件文件发生异常,{ex.Message}");
throw;
}
}
/// <summary>
/// 获取设备属性最新值
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<Dictionary<string, object>> 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;
}
}
2025-10-11 17:06:32 +08:00
/// <summary>
/// 业务系统批量创建设备信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<bool> BatchCreateDeviceBusinessSystemAsync(BatchCreateDeviceAggregationInput input)
{
try
{
if (input.AddressList == null || input.AddressList.Count <= 0)
{
throw new UserFriendlyException($"业务系统批量创建设备信息,设备信息不能为空。");
}
if (input.AddressList.Count > 100)
{
throw new UserFriendlyException($"业务系统批量创建设备信息设备信息不能超过100个。");
}
if (input.DeviceSourceTypeEnum != DeviceSourceTypeEnum.Prepay && input.DeviceSourceTypeEnum != DeviceSourceTypeEnum.Energy)
{
throw new UserFriendlyException($"业务系统批量创建设备信息,设备来源异常。");
}
if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.CTWing)
{
return await CTWingDeviceBatchCreateAsync(input);
}
else if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.OneNET)
{
return await OneNETDeviceBatchCreateAsync(input);
}
throw new UserFriendlyException($"不支持的物联网平台");
}
catch (Exception)
{
throw;
}
}
2025-08-04 11:59:07 +08:00
#region OneNET
/// <summary>
/// OneNET设备创建
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected async Task<bool> OneNETDeviceCreateAsync(CreateDeviceAggregationInput input)
{
try
{
2025-08-04 11:59:07 +08:00
CreateDeviceInput createDeviceInput = input.Adapt<CreateDeviceInput>();
var productInfo = await oneNETProductService.GetProductInfoAsync(new IdInput<string>() { Id = input.IoTPlatformProductId });
2025-08-04 11:59:07 +08:00
if (productInfo == null)
{
throw new UserFriendlyException($"OneNET创建设备失败未找到对应的产品配置信息。");
}
if (input.DeviceSourceTypeEnum == DeviceSourceTypeEnum.Workshop && !productInfo.IsEnabled) //车间生产推送,必须是已经启用的产品才可以
{
throw new UserFriendlyException($"车间生产推送OneNET创建设备失败产品未启用。");
2025-08-04 11:59:07 +08:00
}
createDeviceInput.DeviceName = input.DeviceAddress;
createDeviceInput.IoTPlatformAccountId = productInfo.OneNETAccountId;
createDeviceInput.IoTPlatformDeviceOpenInfo = $"{input.IoTPlatformProductId}{input.DeviceAddress}";
createDeviceInput.PlatformPassword = productInfo.ProductAccesskey;
createDeviceInput.IoTPlatformProductName = productInfo.ProductName;
createDeviceInput.AccountPhoneNumber = productInfo.AccountPhoneNumber;
var insertResult = await deviceAppService.CreateAsync(createDeviceInput);
if (insertResult == null)
{
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 添加设备信息失败:{input.Serialize()}");
return false;
}
//推送至OneNET平台
var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
{
DeviceName = createDeviceInput.IoTPlatformDeviceOpenInfo,
ProductId = productInfo.IoTPlatformProductId,
OneNETAccountId = productInfo.OneNETAccountId,
Description = input.DeviceAddress,
});
if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success || pushResult.Data == null)
2025-08-04 11:59:07 +08:00
{
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}");
return false;
}
await DeviceUpdateHandler(insertResult, pushResult, pushResult.Data.SecurityKey);
2025-08-04 11:59:07 +08:00
return true;
}
catch (Exception)
{
throw;
}
}
2025-10-31 14:13:22 +08:00
2025-08-04 11:59:07 +08:00
/// <summary>
/// OneNET设备批量创建
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected async Task<bool> OneNETDeviceBatchCreateAsync(BatchCreateDeviceAggregationInput input)
{
try
{
var productInfo = await FreeSqlDbContext.Instance.Select<OneNETProductInfos>()
.Where(e => e.IoTPlatformProductId == input.IoTPlatformProductId)//此处不需要过滤产品状态,方便测试产品配置信息是否准确,避免跟车间生产搞混
.WhereIf(input.DeviceSourceTypeEnum == DeviceSourceTypeEnum.Workshop, e => e.IsEnabled == true)
.FirstAsync();
if (productInfo == null)
{
throw new UserFriendlyException($"批量创建设备失败,未找到对应的产品配置信息。");
}
BatchCreateDeviceInput batchCreateDeviceInput = new BatchCreateDeviceInput()
{
IoTPlatform = input.IoTPlatform,
AddressList = input.AddressList,
DeviceInputs = new List<CreateDeviceInput>()
};
foreach (var item in input.AddressList)
{
CreateDeviceInput createDeviceInput = input.Adapt<CreateDeviceInput>();
createDeviceInput.DeviceName = item;
createDeviceInput.DeviceAddress = item;
2025-08-04 11:59:07 +08:00
createDeviceInput.IoTPlatformAccountId = productInfo.OneNETAccountId;
createDeviceInput.IoTPlatformDeviceOpenInfo = $"{input.IoTPlatformProductId}{item}";
createDeviceInput.PlatformPassword = productInfo.ProductAccesskey;
createDeviceInput.IoTPlatformProductName = productInfo.ProductName;
createDeviceInput.AccountPhoneNumber = productInfo.AccountPhoneNumber;
createDeviceInput.DeviceSourceTypeEnum = input.DeviceSourceTypeEnum.Value;
batchCreateDeviceInput.DeviceInputs.Add(createDeviceInput);
2025-08-04 11:59:07 +08:00
}
var insertResult = await deviceAppService.BatchCreateAsync(batchCreateDeviceInput);
if (insertResult == null)
{
logger.LogError($"{nameof(OneNETDeviceBatchCreateAsync)} OneNET设备批量创建添加设备信息失败{input.Serialize()}");
return false;
}
//推送至OneNET平台
var oneNETBatchCreateDeviceInfoInput = new BatchCreateDeviceInfoInput()
{
ProductId = productInfo.IoTPlatformProductId,
OneNETAccountId = productInfo.OneNETAccountId,
DeviceList = new List<string>()
2025-08-04 11:59:07 +08:00
};
oneNETBatchCreateDeviceInfoInput.DeviceList = batchCreateDeviceInput.DeviceInputs.Select(d => d.IoTPlatformDeviceOpenInfo).ToList();
2025-08-04 11:59:07 +08:00
var pushResult = await oneNETDeviceService.BatchCreateDeviceInfoAsync(oneNETBatchCreateDeviceInfoInput);
if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
{
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}");
return false;
}
foreach (var item in insertResult)
{
var successEntity = pushResult.Data.Successlist.Where(d => d.DeviceName == item.IoTPlatformDeviceOpenInfo).FirstOrDefault();
if (successEntity != null)
{
await DeviceUpdateHandler(item, HttpDataResultExtensions.Success(successEntity), successEntity.SecurityKey);
2025-08-04 11:59:07 +08:00
}
}
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 重新推送设备信息到OneNET物联网平台
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task<DeviceManagementInfoDto> RepushDeviceInfoToOneNET(DeviceManagementInfoDto input)
{
try
{
//检查OneNET平台设备是否已经存在
var oneNETDeviceInfoResult = await oneNETDeviceService.DeviceInfoDetailAsync(new DeviceInfoDetailInput()
{
DeviceName = input.IoTPlatformDeviceOpenInfo,
ProductId = input.IoTPlatformProductId,
OneNETAccountId = input.IoTPlatformAccountId,
});
if (oneNETDeviceInfoResult != null && oneNETDeviceInfoResult.Code == ServicePro.Enums.ResponeResultEnum.Success)
{
throw new UserFriendlyException($"OneNET推送失败账号产品下{input.IoTPlatformProductId}已经存在该设备{input.DeviceAddress}。");
}
//推送至OneNET平台
var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
{
DeviceName = input.IoTPlatformDeviceOpenInfo,
ProductId = input.IoTPlatformProductId,
OneNETAccountId = input.IoTPlatformAccountId,
Description = input.DeviceAddress,
});
if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success || pushResult.Data == null)
{
2025-08-01 14:59:11 +08:00
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}");
throw new UserFriendlyException($"平台请求失败。");
}
return await DeviceUpdateHandler(input, pushResult, pushResult.Data.SecurityKey);
}
catch (Exception)
{
throw;
}
}
/// <summary>
2025-08-04 11:59:07 +08:00
/// 删除OneNET平台设备
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
2025-08-04 11:59:07 +08:00
public async Task<bool> DeleteDeviceInfoToOneNET(DeviceManagementInfoDto input)
{
2025-08-04 11:59:07 +08:00
try
{
2025-08-04 11:59:07 +08:00
//删除OneNET平台设备信息
var deleteResult = await oneNETDeviceService.DeleteDeviceInfoAsync(new DeleteDeviceInfoInput()
{
DeviceName = input.IoTPlatformDeviceOpenInfo,
ProductId = input.IoTPlatformProductId,
OneNETAccountId = input.IoTPlatformAccountId,
2025-08-04 11:59:07 +08:00
});
if (deleteResult == null || deleteResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
{
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 删除设备信息失败:{deleteResult.Serialize()}");
throw new UserFriendlyException($"删除设备平台请求失败。");
}
var localDeleteReult = await deviceAppService.DeleteAsync(new IdInput() { Id = input.Id });
if (localDeleteReult == false)
{
throw new UserFriendlyException($"平台设备信息删除成功,系统删除设备失败。");
}
return localDeleteReult;
}
catch (Exception)
{
2025-08-04 11:59:07 +08:00
throw;
}
2025-08-04 11:59:07 +08:00
}
/// <summary>
/// 发送OneNET平台设备指令
/// </summary>
/// <param name="deviceInfo"></param>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
protected async Task<bool> DeviceCommandInfoToOneNET(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto input)
{
try
{
//获取设备对应的平台端物模型信息,校验前端传入的属性标识集合是否存在不合法的属性标识符
var platformThingModelInfo = await platformThingModelInfoAppService.FindByPlatformProductIdAsync(new IdInput<string>() { 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 的特别指令
2025-12-09 17:24:27 +08:00
if (deviceInfo.IsNeedConfigDevicMdoel && deviceInfo.DeviceThingModelDataId.HasValue && input.Commands.ContainsKey(ThingModelFixedTypeConst.SpecialCommand))
{
var propertyInfo = await oneNETProductService.GetProductThingModelSpecialCommandDataTypeListAsync(new IdInput<string>() { Id = deviceInfo.IoTPlatformProductId });
if (propertyInfo == null)
{
throw new UserFriendlyException($"{nameof(DeviceCommandInfoToOneNET)} OneNET设备属性设置失败产品Id{deviceInfo.IoTPlatformProductId}未找到对应的属性信息。");
}
Dictionary<string, string> tempSpecialCommand = await deviceThingModelService.BuildThingModelSpecialCommandAsync(propertyInfo, deviceInfo.DeviceThingModelDataId.Value);
2025-12-09 17:24:27 +08:00
input.Commands[ThingModelFixedTypeConst.SpecialCommand] = tempSpecialCommand;
}
var commandRequest = new OpenApiRequest()
{
2025-12-09 17:24:27 +08:00
Message = input.Serialize(),
};
var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, commandRequest, deviceInfo.Adapt<DeviceCacheInfos>(), input.Commands.Serialize());
await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo));
2026-01-04 16:22:49 +08:00
////检查下设备是否在线
//var deviceOnlineStatus = await oneNETDeviceService.DeviceInfoDetailAsync(new DeviceInfoDetailInput()
//{
// DeviceName = deviceInfo.IoTPlatformDeviceOpenInfo,
// OneNETAccountId = deviceInfo.IoTPlatformAccountId,
// ProductId = deviceInfo.IoTPlatformProductId,
//});
2026-01-04 16:22:49 +08:00
//if (deviceOnlineStatus == null || deviceOnlineStatus.Code != ResponeResultEnum.Success)
//{
// throw new UserFriendlyException("获取平台设备信息失败");
//}
2026-01-04 16:22:49 +08:00
//if (deviceOnlineStatus.Data.Status != 1)
//{
// throw new UserFriendlyException("设备不在线");
//}
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo);
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 发送OneNET平台设备升级指令
/// </summary>
/// <param name="deviceInfo"></param>
2025-12-31 16:46:36 +08:00
/// <param name="taskInput">设置属性请求内容相关</param>
/// <param name="deviceFirmwareInfo">固件数据Id</param>
2025-12-31 16:46:36 +08:00
/// <param name="fileObject">固件文件信息</param>
/// <param name="input">入参</param>
2026-01-04 17:06:41 +08:00
/// <param name="productInfo">产品信息(如果已获取则传入以避免重复调用)</param>
/// <param name="platformThingModelInfo">平台端物模型信息(如果已获取则传入以避免重复调用)</param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
2026-01-04 17:06:41 +08:00
protected async Task<bool> DeviceUpgradeCommandToOneNET(DeviceManagementInfoDto deviceInfo, ReceiveCommandInfoDto taskInput, DeviceFirmwareInfoDto deviceFirmwareInfo, FileObjectDto fileObject, DeviceUpgradeForApiInput input, dynamic productInfo = null, dynamic platformThingModelInfo = null)
{
try
{
2025-12-31 16:46:36 +08:00
if (deviceInfo == null || deviceFirmwareInfo == null || fileObject == null)
{
throw new UserFriendlyException($"{nameof(DeviceUpgradeCommandToOneNET)}设备或固件信息不能为空");
}
2025-12-31 16:46:36 +08:00
if ((deviceInfo.IoTPlatformProductId != deviceFirmwareInfo.IoTPlatformProductId) || (deviceInfo.IoTPlatform != deviceFirmwareInfo.IoTPlatform))
{
2025-12-31 16:46:36 +08:00
throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}平台产品信息固件中不一致");
}
2026-01-04 17:06:41 +08:00
//如果未传入产品信息和物模型信息,则获取
if (productInfo == null || platformThingModelInfo == null)
2025-12-31 16:46:36 +08:00
{
2026-01-04 17:06:41 +08:00
var (productInfoResult, platformThingModelInfoResult) = await GetOneNETProductAndThingModelInfoAsync(deviceInfo.IoTPlatformProductId, deviceInfo.DeviceAddress);
productInfo = productInfoResult;
platformThingModelInfo = platformThingModelInfoResult;
}
2026-01-04 17:06:41 +08:00
var upgradeProperty = ((List<IoTPlatformThingModelInfoDto>)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(),
2025-12-31 16:46:36 +08:00
UpgradeDescription = input.UpgradeDescription,
UpgradeStatus = DeviceUpgradeStatusTypeEnum.NotUpgrade,
Id = GuidGenerator.Create()
2025-12-31 16:46:36 +08:00
};
var md5HashRaw = $"{fileObject.Md5Hash}{productInfo.ProductAccesskey}{upgradeRecordInput.UpgradeIdentifier}{fileObject.FileSize}";
var upgradeRequest = new DeviceUpgradeCommandRequest()
{
Length = fileObject.FileSize,
UpgradeIdentifier = upgradeRecordInput.UpgradeIdentifier,
SignatureValue = md5HashRaw.Md5Fun(),
FirmwareUrl = $"{serverApplicationOptions.DownloadDeviceFirmwareBasicUrl}{upgradeRecordInput.Id}",
2025-12-31 16:46:36 +08:00
TimeOut = serverApplicationOptions.DownloadDeviceFirmwareTimeOut,
};
2025-12-31 16:46:36 +08:00
upgradeRecordInput.UpgradeMessage = upgradeRequest.Serialize();
upgradeRecordInput.FirmwareSignature = upgradeRequest.SignatureValue;
var insertResult = await deviceUpgradeRecordService.CreateAsync(upgradeRecordInput);
2025-12-31 16:46:36 +08:00
if (insertResult == null)
{
throw new UserFriendlyException($"设备{deviceInfo.DeviceAddress}升级记录失败");
}
//发送OneNET平台设备升级指令HEX格式字符串
2026-01-04 16:22:49 +08:00
taskInput.Commands = new Dictionary<string, object>()
2025-12-31 16:46:36 +08:00
{
{ upgradeProperty.IoTPlatformRawFieldName, upgradeRecordInput.UpgradeMessage.ToHexString() }
};
var commandRequest = new OpenApiRequest()
{
2025-12-31 16:46:36 +08:00
Message = taskInput.Serialize(),
};
2025-12-31 16:46:36 +08:00
var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, commandRequest, deviceInfo.Adapt<DeviceCacheInfos>(), taskInput.Commands.Serialize());
2026-01-04 16:22:49 +08:00
await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETUpgradeCommandIssuedEventName, packetTaskInfo));
2026-01-04 16:22:49 +08:00
//将升级记录数据Id存入Redis缓存中。
string cacheKey = string.Format($"{RedisConst.CacheDeviceUpgradeRecordDataKey}", upgradeRecordInput.Id);
await FreeRedisProvider.Instance.SetAsync(cacheKey, upgradeRecordInput.Id);
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo);
//更新状态为升级中
2026-01-04 16:22:49 +08:00
await deviceUpgradeRecordService.UpdateStatusAsync(new UpdateStatusInput()
{
2026-01-04 16:22:49 +08:00
Id = upgradeRecordInput.Id.Value,
UpgradeStatus = DeviceUpgradeStatusTypeEnum.Upgrading,
});
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 抄读OneNET平台设备属性数据
/// </summary>
/// <param name="deviceInfo"></param>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
protected async Task<Dictionary<string, object>> 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;
}
}
2025-08-04 11:59:07 +08:00
#endregion
2025-08-04 11:59:07 +08:00
#region CTWing
/// <summary>
/// CTWing 设备创建
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected async Task<bool> CTWingDeviceCreateAsync(CreateDeviceAggregationInput input)
{
throw new UserFriendlyException($"CTWing 设备创建失败,功能未实现。");
}
2025-08-04 11:59:07 +08:00
/// <summary>
/// CTWing 批量设备创建
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected async Task<bool> CTWingDeviceBatchCreateAsync(BatchCreateDeviceAggregationInput input)
{
try
{
throw new UserFriendlyException($"CTWing 批量设备创建失败CTWing暂未实现。");
}
catch (Exception)
{
2025-08-04 11:59:07 +08:00
throw;
}
}
/// <summary>
/// 重新推送设备信息到CTWing物联网平台
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task<DeviceManagementInfoDto> RepushDeviceInfoToCTWing(DeviceManagementInfoDto input)
{
try
{
throw new UserFriendlyException($"推送失败CTWing暂未实现。");
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 删除CTWing平台设备
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public async Task<bool> DeleteDeviceInfoToCTWing(DeviceManagementInfoDto input)
{
try
{
throw new UserFriendlyException($"删除失败CTWing暂未实现。");
}
catch (Exception)
{
throw;
}
}
2025-08-04 11:59:07 +08:00
#endregion
}
}