diff --git a/JiShe.ServicePro b/JiShe.ServicePro
index 39fae25..e5f28b8 160000
--- a/JiShe.ServicePro
+++ b/JiShe.ServicePro
@@ -1 +1 @@
-Subproject commit 39fae252a272635ec7fd926b3e3d25b183b3fc7a
+Subproject commit e5f28b81d72a977d2e2b31d26aabf1a72ec1802d
diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CreateDeviceAggregationInput.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CreateDeviceAggregationInput.cs
index afb7b47..4da6926 100644
--- a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CreateDeviceAggregationInput.cs
+++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/CreateDeviceAggregationInput.cs
@@ -20,26 +20,11 @@ namespace JiShe.IoT.DeviceAggregation.Dto
[Required(ErrorMessage = "物联网平台类型不能为空")]
public IoTPlatformTypeEnum IoTPlatform { get; set; }
- ///
- /// 电表密码
- ///
- [Required(ErrorMessage = "电表密码不能为空")]
- public string PlatformPassword { get; set; }
-
///
/// 集中器在物联网平台中对应的产品Id
///
[Required(ErrorMessage = "产品Id不能为空")]
public string IoTPlatformProductId { get; set; }
- ///
- /// 集中器在物联网平台中对应的设备Id或者名称
- ///
- public string IoTPlatformDeviceOpenInfo { get; set; }
-
- ///
- /// 物联网平台中对应的账号Id
- ///
- public string IoTPlatformAccountId { get; set; }
}
}
\ No newline at end of file
diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs
index 2e3904d..810108d 100644
--- a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs
+++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/IDeviceAggregationService.cs
@@ -32,5 +32,14 @@ namespace JiShe.IoT.DeviceAggregation
///
///
Task FindByIdAsync(IdInput input);
+
+ ///
+ /// 重新推送设备信息到物联网平台
+ ///
+ ///
+ ///
+ ///
+ Task RepushDeviceInfoToIoTPlatform(IdInput input);
+
}
}
diff --git a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
index b386b28..eefbd8c 100644
--- a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
+++ b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
@@ -1,10 +1,13 @@
using JiShe.IoT.DeviceAggregation.Dto;
using JiShe.IoT.OneNETAggregation.Dto;
+using JiShe.ServicePro;
using JiShe.ServicePro.Core;
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
using JiShe.ServicePro.DeviceManagement.DeviceInfos.Dto;
using JiShe.ServicePro.DeviceManagement.Permissions;
+using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.OneNETManagement.OneNETDevices;
+using JiShe.ServicePro.OneNETManagement.OneNETProducts;
using Mapster;
using Microsoft.Extensions.Logging;
using System;
@@ -32,11 +35,60 @@ namespace JiShe.IoT.DeviceAggregation
[Authorize(DeviceManagementPermissions.DeviceInfoManagement.Create)]
public async Task CreateAsync(CreateDeviceAggregationInput input)
{
+ 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;
+ }
+
+ }
+
+ ///
+ /// OneNET设备创建
+ ///
+ ///
+ ///
+ public async Task OneNETDeviceCreateAsync(CreateDeviceAggregationInput input)
+ {
try
{
CreateDeviceInput createDeviceInput = input.Adapt();
+ var productInfo = await FreeSqlDbContext.Instance.Select()
+ .Where(e => e.IsEnabled == true && e.IoTPlatformProductId == input.IoTPlatformProductId)
+ .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;
+
var insertResult = await deviceAppService.CreateAsync(createDeviceInput);
if (insertResult == null)
{
@@ -47,9 +99,9 @@ namespace JiShe.IoT.DeviceAggregation
//推送至OneNET平台
var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
{
- DeviceName = $"{input.IoTPlatformProductId}{input.DeviceAddress}",
- ProductId = input.IoTPlatformProductId,
- OneNETAccountId = input.IoTPlatformAccountId,
+ DeviceName = createDeviceInput.IoTPlatformDeviceOpenInfo,
+ ProductId = productInfo.IoTPlatformProductId,
+ OneNETAccountId = productInfo.OneNETAccountId,
Description = input.DeviceAddress,
});
@@ -69,6 +121,11 @@ namespace JiShe.IoT.DeviceAggregation
return false;
}
+ //设备数据缓存到Redis
+ DeviceCacheInfos deviceCacheInfos = insertResult.Adapt();
+ deviceCacheInfos.IoTPlatformResponse = updateDeviceInput.IoTPlatformResponse;
+ RedisProvider.Instance.HSet(RedisConst.CacheAllDeviceInfoHashKey, insertResult.DeviceAddress, deviceCacheInfos);
+
return true;
}
catch (Exception)
@@ -78,6 +135,16 @@ namespace JiShe.IoT.DeviceAggregation
}
}
+ ///
+ /// CTWing 设备创建
+ ///
+ ///
+ ///
+ public async Task CTWingDeviceCreateAsync(CreateDeviceAggregationInput input)
+ {
+ throw new UserFriendlyException($"CTWing 设备创建失败,功能未实现。");
+ }
+
///
/// 删除设备信息
///
@@ -101,5 +168,71 @@ namespace JiShe.IoT.DeviceAggregation
{
return await deviceAppService.FindByIdAsync(input);
}
+
+ ///
+ /// 重新推送设备信息到物联网平台
+ ///
+ ///
+ ///
+ ///
+ public async Task RepushDeviceInfoToIoTPlatform(IdInput input)
+ {
+ try
+ {
+ var entityDevice = await FreeSqlDbContext.Instance.Select().Where(f => f.Id == input.Id).FirstAsync();
+ if (entityDevice == null)
+ {
+ throw new UserFriendlyException($"推送失败,未找到设备数据");
+ }
+
+ var productInfo = await FreeSqlDbContext.Instance.Select()
+ .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.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.IoTPlatformResponse = updateDeviceInput.IoTPlatformResponse;
+ RedisProvider.Instance.HSet(RedisConst.CacheAllDeviceInfoHashKey, entityDevice.DeviceAddress, deviceCacheInfos);
+
+ return entityDevice.Adapt();
+ }
+ catch (Exception)
+ {
+
+ throw;
+ }
+ }
}
}
diff --git a/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs b/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs
index 4cdc027..eff12cb 100644
--- a/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs
+++ b/src/JiShe.IoT.Application/OneNETAggregation/OneNETAggregationService.cs
@@ -5,13 +5,16 @@ using JiShe.ServicePro.DeviceManagement.DeviceInfos;
using JiShe.ServicePro.DeviceManagement.DeviceInfos.Dto;
using JiShe.ServicePro.Encrypt;
using JiShe.ServicePro.Enums;
+using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.Kafka.Consts;
using JiShe.ServicePro.Kafka.Producer;
using JiShe.ServicePro.OneNETManagement.OneNETDevices;
using JiShe.ServicePro.OneNETManagement.OneNETProducts;
using JiShe.ServicePro.ServerOptions;
+using Mapster;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
+using System.IO.Pipelines;
namespace JiShe.IoT.OneNETAggregation
{
@@ -90,10 +93,10 @@ namespace JiShe.IoT.OneNETAggregation
};
//创建本地设备信息
- await deviceAppService.CreateAsync(meterInfoEntity);
+ var insertResult = await deviceAppService.CreateAsync(meterInfoEntity);
//推送至OneNET平台
- await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
+ var pushResult = await oneNETDeviceService.CreateDeviceInfoAsync(new CreateDeviceInfoInput()
{
DeviceName = productionEquipmentMessageBody.DeviceOpenInfo,
ProductId = productionEquipmentMessageBody.IoTPlatformProductId,
@@ -101,6 +104,28 @@ namespace JiShe.IoT.OneNETAggregation
Description = productionEquipmentMessageBody.DeviceOpenInfo,
});
+ if (pushResult == null || pushResult.Code != ServicePro.Enums.ResponeResultEnum.Success)
+ {
+ logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} 推送设备信息失败:{pushResult.Serialize()}");
+ return HttpDataResultExtensions.Failed("推送设备信息OneNET失败", -109, ResponeResultEnum.Fail);
+ }
+
+ UpdateDeviceInput updateDeviceInput = insertResult.Adapt();
+ updateDeviceInput.IoTPlatformResponse = pushResult.Serialize();
+
+ var updateResult = await deviceAppService.UpdateAsync(updateDeviceInput);
+ if (updateResult == null)
+ {
+ logger.LogError($"{nameof(ReceiveWorkshopProductionInfoAsync)} OneNET返回的设备信息更新失败:{input.Serialize()}");
+ return HttpDataResultExtensions.Failed("OneNET返回的设备信息更新失败", -110, ResponeResultEnum.Fail);
+ }
+
+ //设备数据缓存到Redis
+ DeviceCacheInfos deviceCacheInfos = insertResult.Adapt();
+ deviceCacheInfos.IoTPlatformResponse = updateDeviceInput.IoTPlatformResponse;
+
+ RedisProvider.Instance.HSet(RedisConst.CacheAllDeviceInfoHashKey, insertResult.DeviceAddress, deviceCacheInfos);
+
return HttpDataResultExtensions.Failed("签名校验失败", -101, ResponeResultEnum.NotAllowed);
}
catch (Exception ex)
diff --git a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
index 26d0fd1..fa32133 100644
--- a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
+++ b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
@@ -48,5 +48,17 @@ namespace JiShe.IoT.Controllers
{
return await _deviceAggregationService.FindByIdAsync(input);
}
+
+ ///
+ /// 重新推送设备信息到物联网平台
+ ///
+ ///
+ ///
+ [HttpPost("RepushDeviceInfoToIoTPlatform")]
+ [SwaggerOperation(summary: "重新推送设备信息到物联网平台", Tags = new[] { "AggregationDevice" })]
+ public Task RepushDeviceInfoToIoTPlatform(IdInput input)
+ {
+ return _deviceAggregationService.RepushDeviceInfoToIoTPlatform(input);
+ }
}
}