2025-07-24 17:06:54 +08:00
|
|
|
|
using JiShe.IoT.DeviceAggregation.Dto;
|
2025-07-29 17:30:52 +08:00
|
|
|
|
using JiShe.ServicePro;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using JiShe.ServicePro.Core;
|
2025-07-25 17:27:21 +08:00
|
|
|
|
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
|
|
|
|
|
|
using JiShe.ServicePro.DeviceManagement.DeviceInfos.Dto;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using JiShe.ServicePro.DeviceManagement.Permissions;
|
2025-08-04 17:29:47 +08:00
|
|
|
|
using JiShe.ServicePro.Dto;
|
2025-08-04 11:59:07 +08:00
|
|
|
|
using JiShe.ServicePro.Enums;
|
2025-07-29 17:30:52 +08:00
|
|
|
|
using JiShe.ServicePro.FreeRedisProvider;
|
2025-08-04 17:29:47 +08:00
|
|
|
|
using JiShe.ServicePro.Kafka.Consts;
|
|
|
|
|
|
using JiShe.ServicePro.Kafka.Producer;
|
2025-08-04 11:59:07 +08:00
|
|
|
|
using JiShe.ServicePro.OneNET.Provider.OpenApiModels.Devices;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using JiShe.ServicePro.OneNETManagement.OneNETDevices;
|
2025-07-29 17:30:52 +08:00
|
|
|
|
using JiShe.ServicePro.OneNETManagement.OneNETProducts;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using Mapster;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Volo.Abp;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.IoT.DeviceAggregation
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备聚合服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
|
/// <param name="deviceAppService">设备服务</param>
|
|
|
|
|
|
/// <param name="oneNETDeviceService">OneNET设备服务</param>
|
2025-08-04 17:29:47 +08:00
|
|
|
|
/// <param name="producerService">KafKa生产服务</param>
|
|
|
|
|
|
public class DeviceAggregationService(ILogger<DeviceAggregationService> logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService, IKafkaProducerService producerService) : IoTAppService, IDeviceAggregationService
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-08-01 14:59:11 +08:00
|
|
|
|
/// 管理后台创建设备信息
|
2025-07-24 17:06:54 +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>
|
|
|
|
|
|
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)
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
2025-07-29 17:30:52 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
input.DeviceSourceTypeEnum = DeviceSourceTypeEnum.Workshop;
|
2025-07-29 17:30:52 +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-07-31 15:51:46 +08:00
|
|
|
|
|
2025-07-29 17:30:52 +08:00
|
|
|
|
}
|
2025-07-24 17:06:54 +08:00
|
|
|
|
|
2025-07-29 17:30:52 +08:00
|
|
|
|
/// <summary>
|
2025-08-04 11:59:07 +08:00
|
|
|
|
/// 车间批量创建设备信息
|
2025-07-29 17:30:52 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-08-04 11:59:07 +08:00
|
|
|
|
public async Task<bool> BatchCreateDeviceWorkshopAsync(BatchCreateDeviceAggregationInput input)
|
2025-07-29 17:30:52 +08:00
|
|
|
|
{
|
2025-07-24 17:06:54 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
if (input.AddressList == null || input.AddressList.Count <= 0)
|
2025-07-29 17:30:52 +08:00
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
throw new UserFriendlyException($"批量创建设备失败,设备信息不能为空。");
|
2025-07-29 17:30:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-04 11:59:07 +08:00
|
|
|
|
if (input.AddressList.Count > 100)
|
2025-07-29 17:30:52 +08:00
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
throw new UserFriendlyException($"批量创建设备失败,设备信息不能超过100个。");
|
2025-07-29 17:30:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-04 11:59:07 +08:00
|
|
|
|
input.DeviceSourceTypeEnum = DeviceSourceTypeEnum.Workshop;
|
2025-07-29 17:30:52 +08:00
|
|
|
|
|
2025-08-04 11:59:07 +08:00
|
|
|
|
if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.CTWing)
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
return await CTWingDeviceBatchCreateAsync(input);
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
2025-08-04 11:59:07 +08:00
|
|
|
|
else if (input.IoTPlatform == ServicePro.Enums.IoTPlatformTypeEnum.OneNET)
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
return await OneNETDeviceBatchCreateAsync(input);
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-04 11:59:07 +08:00
|
|
|
|
throw new UserFriendlyException($"不支持的物联网平台");
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
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)]
|
2025-07-24 17:06:54 +08:00
|
|
|
|
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-07-24 17:06:54 +08:00
|
|
|
|
|
2025-08-04 11:59:07 +08:00
|
|
|
|
throw new UserFriendlyException($"不支持的物联网平台");
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
2025-07-29 17:30:52 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重新推送设备信息到物联网平台
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
|
|
|
|
public async Task<DeviceManagementInfoDto> RepushDeviceInfoToIoTPlatform(IdInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-31 15:51:46 +08:00
|
|
|
|
var entityDevice = await FreeSqlDbContext.Instance.Select<DeviceManagementInfo>().Where(f => f.Id == input.Id).FirstAsync<DeviceManagementInfoDto>();
|
2025-07-29 17:30:52 +08:00
|
|
|
|
if (entityDevice == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"推送失败,未找到设备数据");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-31 15:51:46 +08:00
|
|
|
|
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
|
|
|
|
/// 更新设备信息并处理缓存
|
2025-07-31 15:51:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
2025-08-04 11:59:07 +08:00
|
|
|
|
/// <param name="pushResult"></param>
|
2025-07-31 15:51:46 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
2025-08-04 11:59:07 +08:00
|
|
|
|
private async Task<DeviceManagementInfoDto> DeviceUpdateHandler(DeviceManagementInfoDto input, HttpDataResult pushResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdateDeviceInput updateDeviceInput = input.Adapt<UpdateDeviceInput>();
|
|
|
|
|
|
updateDeviceInput.IoTPlatformResponse = pushResult.Serialize();
|
|
|
|
|
|
updateDeviceInput.IsPlatformPushSuccess = true;
|
|
|
|
|
|
|
|
|
|
|
|
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.IoTPlatformResponse = null;
|
|
|
|
|
|
deviceCacheInfos.PlatformPassword = null;
|
|
|
|
|
|
|
|
|
|
|
|
RedisProvider.Instance.HSet<DeviceCacheInfos>(RedisConst.CacheAllDeviceInfoHashKey, input.DeviceAddress, deviceCacheInfos);
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(input.DeviceAddress);
|
|
|
|
|
|
//将指令存储Kafka的OneNET主题中
|
|
|
|
|
|
var commandRequest = new OpenApiRequest()
|
|
|
|
|
|
{
|
|
|
|
|
|
Message = new ReceiveCommandInfoDto()
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceAddress = input.DeviceAddress,
|
|
|
|
|
|
Commands = input.CommandContent.Deserialize<Dictionary<string,object>>(),
|
|
|
|
|
|
DeviceType = DeviceTypeEnum.Focus,//todo 设备类型 需要跟设备统一什么情况下知道具体设备类型
|
|
|
|
|
|
SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
|
|
|
|
|
|
}.Serialize(),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
|
|
|
|
|
|
{
|
|
|
|
|
|
await producerService.ProduceAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, $"{GuidGenerator.Create()}", commandRequest);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
|
|
|
|
|
|
{
|
|
|
|
|
|
await producerService.ProduceAsync(KafkaTopicConsts.CTWingAepCommandIssuedEventName, $"{GuidGenerator.Create()}", commandRequest);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
2025-07-31 15:51:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
CreateDeviceInput createDeviceInput = input.Adapt<CreateDeviceInput>();
|
|
|
|
|
|
|
|
|
|
|
|
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($"创建设备失败,未找到对应的产品配置信息。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await DeviceUpdateHandler(insertResult, pushResult);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2025-07-31 15:51:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
2025-08-05 11:30:53 +08:00
|
|
|
|
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;
|
2025-08-05 11:30:53 +08:00
|
|
|
|
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,
|
2025-08-05 11:30:53 +08:00
|
|
|
|
DeviceList = new List<string>()
|
2025-08-04 11:59:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-05 11:30:53 +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));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-31 15:51:46 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重新推送设备信息到OneNET物联网平台
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
|
|
|
|
public async Task<DeviceManagementInfoDto> RepushDeviceInfoToOneNET(DeviceManagementInfoDto input)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2025-08-05 11:30:53 +08:00
|
|
|
|
{
|
2025-07-29 17:30:52 +08:00
|
|
|
|
var productInfo = await FreeSqlDbContext.Instance.Select<OneNETProductInfos>()
|
2025-08-05 11:30:53 +08:00
|
|
|
|
.Where(e => e.IoTPlatformProductId == input.IoTPlatformProductId)
|
2025-07-29 17:30:52 +08:00
|
|
|
|
.FirstAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (productInfo == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"推送失败,未找到对应的产品配置信息。");
|
|
|
|
|
|
}
|
2025-08-05 11:30:53 +08:00
|
|
|
|
|
|
|
|
|
|
//检查OneNET平台设备是否已经存在
|
|
|
|
|
|
var oneNETDeviceInfoResult = await oneNETDeviceService.DeviceInfoDetailAsync(new DeviceInfoDetailInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceName = input.IoTPlatformDeviceOpenInfo,
|
|
|
|
|
|
ProductId = productInfo.IoTPlatformProductId,
|
|
|
|
|
|
OneNETAccountId = productInfo.OneNETAccountId,
|
|
|
|
|
|
});
|
2025-07-29 17:30:52 +08:00
|
|
|
|
|
2025-08-05 11:30:53 +08:00
|
|
|
|
if (oneNETDeviceInfoResult != null && oneNETDeviceInfoResult.Code == ServicePro.Enums.ResponeResultEnum.Success)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"推送失败,OneNET账号{productInfo.AccountPhoneNumber}的产品下{productInfo.ProductName}已经存在该设备{input.DeviceAddress}。");
|
|
|
|
|
|
}
|
2025-07-29 17:30:52 +08:00
|
|
|
|
|
|
|
|
|
|
//推送至OneNET平台
|
|
|
|
|
|
var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
|
|
|
|
|
|
{
|
2025-07-31 15:51:46 +08:00
|
|
|
|
DeviceName = input.IoTPlatformDeviceOpenInfo,
|
2025-07-29 17:30:52 +08:00
|
|
|
|
ProductId = productInfo.IoTPlatformProductId,
|
|
|
|
|
|
OneNETAccountId = productInfo.OneNETAccountId,
|
2025-07-31 15:51:46 +08:00
|
|
|
|
Description = input.DeviceAddress,
|
2025-07-29 17:30:52 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
|
|
|
|
|
|
{
|
2025-08-01 14:59:11 +08:00
|
|
|
|
logger.LogError($"{nameof(CreateDeviceForApiAsync)} 推送设备信息失败:{pushResult.Serialize()}");
|
2025-07-29 17:30:52 +08:00
|
|
|
|
throw new UserFriendlyException($"平台请求失败。");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-31 15:51:46 +08:00
|
|
|
|
return await DeviceUpdateHandler(input, pushResult);
|
2025-07-29 17:30:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-31 15:51:46 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-04 11:59:07 +08:00
|
|
|
|
/// 删除OneNET平台设备
|
2025-07-31 15:51:46 +08:00
|
|
|
|
/// </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-07-31 15:51:46 +08:00
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var productInfo = await FreeSqlDbContext.Instance.Select<OneNETProductInfos>()
|
2025-08-04 17:29:47 +08:00
|
|
|
|
.Where(e => e.IoTPlatformProductId == input.IoTPlatformProductId)
|
2025-08-04 11:59:07 +08:00
|
|
|
|
.FirstAsync();
|
2025-07-31 15:51:46 +08:00
|
|
|
|
|
2025-08-04 11:59:07 +08:00
|
|
|
|
if (productInfo == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"删除失败,未找到对应的产品配置信息。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//删除OneNET平台设备信息
|
|
|
|
|
|
var deleteResult = await oneNETDeviceService.DeleteDeviceInfoAsync(new DeleteDeviceInfoInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceName = input.IoTPlatformDeviceOpenInfo,
|
|
|
|
|
|
ProductId = productInfo.IoTPlatformProductId,
|
2025-08-05 11:30:53 +08:00
|
|
|
|
OneNETAccountId = productInfo.OneNETAccountId,
|
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-07-31 15:51:46 +08:00
|
|
|
|
{
|
2025-08-04 11:59:07 +08:00
|
|
|
|
|
|
|
|
|
|
throw;
|
2025-07-31 15:51:46 +08:00
|
|
|
|
}
|
2025-08-04 11:59:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
2025-07-31 15:51:46 +08:00
|
|
|
|
|
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-07-31 15:51:46 +08:00
|
|
|
|
|
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-07-31 15:51:46 +08:00
|
|
|
|
|
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-07-31 15:51:46 +08:00
|
|
|
|
}
|
2025-08-04 11:59:07 +08:00
|
|
|
|
#endregion
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|