2025-07-24 17:06:54 +08:00
|
|
|
|
using JiShe.IoT.DeviceAggregation.Dto;
|
|
|
|
|
|
using JiShe.IoT.OneNETAggregation.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-07-29 17:30:52 +08:00
|
|
|
|
using JiShe.ServicePro.FreeRedisProvider;
|
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 System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Volo.Abp;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.IoT.DeviceAggregation
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备聚合服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
|
/// <param name="deviceAppService">设备服务</param>
|
|
|
|
|
|
/// <param name="oneNETDeviceService">OneNET设备服务</param>
|
|
|
|
|
|
public class DeviceAggregationService(ILogger<DeviceAggregationService> logger, IDeviceAppService deviceAppService, IOneNETDeviceService oneNETDeviceService) : IoTAppService, IDeviceAggregationService
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建设备信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-07-25 11:54:19 +08:00
|
|
|
|
[Authorize(DeviceManagementPermissions.DeviceInfoManagement.Create)]
|
2025-07-24 17:06:54 +08:00
|
|
|
|
public async Task<bool> CreateAsync(CreateDeviceAggregationInput input)
|
|
|
|
|
|
{
|
2025-07-29 17:30:52 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
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-24 17:06:54 +08:00
|
|
|
|
|
2025-07-29 17:30:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// OneNET设备创建
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> OneNETDeviceCreateAsync(CreateDeviceAggregationInput input)
|
|
|
|
|
|
{
|
2025-07-24 17:06:54 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateDeviceInput createDeviceInput = input.Adapt<CreateDeviceInput>();
|
|
|
|
|
|
|
2025-07-29 17:30:52 +08:00
|
|
|
|
var productInfo = await FreeSqlDbContext.Instance.Select<OneNETProductInfos>()
|
2025-07-30 17:19:01 +08:00
|
|
|
|
.Where(e => e.IoTPlatformProductId == input.IoTPlatformProductId)//此处不需要过滤产品状态,方便测试产品配置信息是否准确,避免跟车间生产搞混
|
2025-07-29 17:30:52 +08:00
|
|
|
|
.FirstAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (productInfo == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"创建设备失败,未找到对应的产品配置信息。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//检查设备信息是不是已经存在
|
|
|
|
|
|
var existsDeviceInfo = await deviceAppService.FindDeviceInfosAsync(input.DeviceAddress, createDeviceInput.IoTPlatform);
|
|
|
|
|
|
if (existsDeviceInfo != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"创建设备失败,当前平台设备信息已经存在。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
createDeviceInput.DeviceName = input.DeviceAddress;
|
|
|
|
|
|
createDeviceInput.IoTPlatformAccountId = productInfo.OneNETAccountId;
|
|
|
|
|
|
createDeviceInput.IoTPlatformDeviceOpenInfo = $"{input.IoTPlatformProductId}{input.DeviceAddress}";
|
|
|
|
|
|
createDeviceInput.PlatformPassword = productInfo.ProductAccesskey;
|
2025-07-31 10:43:36 +08:00
|
|
|
|
createDeviceInput.IoTPlatformProductName = productInfo.ProductName;
|
|
|
|
|
|
createDeviceInput.AccountPhoneNumber = productInfo.AccountPhoneNumber;
|
2025-07-29 17:30:52 +08:00
|
|
|
|
|
2025-07-24 17:06:54 +08:00
|
|
|
|
var insertResult = await deviceAppService.CreateAsync(createDeviceInput);
|
|
|
|
|
|
if (insertResult == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"{nameof(CreateAsync)} 添加设备信息失败:{input.Serialize()}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//推送至OneNET平台
|
|
|
|
|
|
var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
|
|
|
|
|
|
{
|
2025-07-29 17:30:52 +08:00
|
|
|
|
DeviceName = createDeviceInput.IoTPlatformDeviceOpenInfo,
|
|
|
|
|
|
ProductId = productInfo.IoTPlatformProductId,
|
|
|
|
|
|
OneNETAccountId = productInfo.OneNETAccountId,
|
2025-07-24 17:06:54 +08:00
|
|
|
|
Description = input.DeviceAddress,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"{nameof(CreateAsync)} 推送设备信息失败:{pushResult.Serialize()}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UpdateDeviceInput updateDeviceInput = insertResult.Adapt<UpdateDeviceInput>();
|
|
|
|
|
|
updateDeviceInput.IoTPlatformResponse = pushResult.Serialize();
|
2025-07-30 17:19:01 +08:00
|
|
|
|
updateDeviceInput.IsPlatformPushSuccess = true;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
|
|
|
|
|
|
var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput);
|
|
|
|
|
|
if (updateResult == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"{nameof(CreateAsync)} 更新设备信息失败:{input.Serialize()}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-29 17:30:52 +08:00
|
|
|
|
//设备数据缓存到Redis
|
|
|
|
|
|
DeviceCacheInfos deviceCacheInfos = insertResult.Adapt<DeviceCacheInfos>();
|
|
|
|
|
|
deviceCacheInfos.IoTPlatformResponse = updateDeviceInput.IoTPlatformResponse;
|
2025-07-30 17:19:01 +08:00
|
|
|
|
|
2025-07-29 17:30:52 +08:00
|
|
|
|
RedisProvider.Instance.HSet<DeviceCacheInfos>(RedisConst.CacheAllDeviceInfoHashKey, insertResult.DeviceAddress, deviceCacheInfos);
|
|
|
|
|
|
|
2025-07-24 17:06:54 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-29 17:30:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// CTWing 设备创建
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> CTWingDeviceCreateAsync(CreateDeviceAggregationInput input)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"CTWing 设备创建失败,功能未实现。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 17:06:54 +08:00
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await deviceAppService.DeleteAsync(input);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
|
{
|
|
|
|
|
|
var entityDevice = await FreeSqlDbContext.Instance.Select<DeviceManagementInfo>().Where(f => f.Id == input.Id).FirstAsync();
|
|
|
|
|
|
if (entityDevice == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"推送失败,未找到设备数据");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var productInfo = await FreeSqlDbContext.Instance.Select<OneNETProductInfos>()
|
|
|
|
|
|
.Where(e => e.IsEnabled == true && e.IoTPlatformProductId == entityDevice.IoTPlatformProductId)
|
|
|
|
|
|
.FirstAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (productInfo == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException($"推送失败,未找到对应的产品配置信息。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//推送至OneNET平台
|
|
|
|
|
|
var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceName = entityDevice.IoTPlatformDeviceOpenInfo,
|
|
|
|
|
|
ProductId = productInfo.IoTPlatformProductId,
|
|
|
|
|
|
OneNETAccountId = productInfo.OneNETAccountId,
|
|
|
|
|
|
Description = entityDevice.DeviceAddress,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"{nameof(CreateAsync)} 推送设备信息失败:{pushResult.Serialize()}");
|
|
|
|
|
|
throw new UserFriendlyException($"平台请求失败。");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UpdateDeviceInput updateDeviceInput = entityDevice.Adapt<UpdateDeviceInput>();
|
|
|
|
|
|
updateDeviceInput.IoTPlatformResponse = pushResult.Serialize();
|
|
|
|
|
|
|
|
|
|
|
|
var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput);
|
|
|
|
|
|
if (updateResult == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError($"{nameof(CreateAsync)} 更新设备信息失败:{input.Serialize()}");
|
|
|
|
|
|
throw new UserFriendlyException($"推送结果更新失败。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//设备数据缓存到Redis
|
|
|
|
|
|
DeviceCacheInfos deviceCacheInfos = entityDevice.Adapt<DeviceCacheInfos>();
|
|
|
|
|
|
deviceCacheInfos.IoTPlatformResponse = updateDeviceInput.IoTPlatformResponse;
|
|
|
|
|
|
RedisProvider.Instance.HSet<DeviceCacheInfos>(RedisConst.CacheAllDeviceInfoHashKey, entityDevice.DeviceAddress, deviceCacheInfos);
|
|
|
|
|
|
|
|
|
|
|
|
return entityDevice.Adapt<DeviceManagementInfoDto>();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|