From 8f78a15cbcbf3902d4604efb00f6781cd79a8ac9 Mon Sep 17 00:00:00 2001
From: ChenYi <296215406@outlook.com>
Date: Tue, 5 Aug 2025 17:09:39 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0OneNET=E8=AE=BE=E5=A4=87?=
=?UTF-8?q?=E9=87=8D=E6=8E=A8=E3=80=81=E6=8C=87=E4=BB=A4=E4=B8=8B=E5=8F=91?=
=?UTF-8?q?=E8=BF=94=E5=9B=9E=E8=A7=A3=E6=9E=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
JiShe.ServicePro | 2 +-
.../Dto/DeviceCommandForApiInput.cs | 25 ++--------
.../DeviceAggregationService.cs | 48 +++++++++++++++++--
...ceCommunicationChannelSubscriberService.cs | 7 +++
.../DeviceAggregationController.cs | 16 ++++++-
5 files changed, 71 insertions(+), 27 deletions(-)
diff --git a/JiShe.ServicePro b/JiShe.ServicePro
index 8df0a54..6a5258a 160000
--- a/JiShe.ServicePro
+++ b/JiShe.ServicePro
@@ -1 +1 @@
-Subproject commit 8df0a54f551a1f894014c9b0e8eca5d7c5c4f51b
+Subproject commit 6a5258a04fbeec29f3dae96f375230204ab2047b
diff --git a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceCommandForApiInput.cs b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceCommandForApiInput.cs
index 2905958..e77046d 100644
--- a/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceCommandForApiInput.cs
+++ b/src/JiShe.IoT.Application.Contracts/DeviceAggregation/Dto/DeviceCommandForApiInput.cs
@@ -1,4 +1,5 @@
-using JiShe.ServicePro.Enums;
+using JiShe.ServicePro.Core;
+using JiShe.ServicePro.Enums;
using System.ComponentModel.DataAnnotations;
namespace JiShe.IoT.DeviceAggregation
@@ -7,26 +8,8 @@ namespace JiShe.IoT.DeviceAggregation
///
/// 设备命令
///
- public class DeviceCommandForApiInput
- {
- ///
- /// 表通信地址
- ///
- [Required(ErrorMessage = "设备地址不能为空")]
- public string DeviceAddress { get; set; }
-
- ///
- /// 物联网平台类型
- ///
- [Required(ErrorMessage = "物联网平台类型不能为空")]
- public IoTPlatformTypeEnum IoTPlatform { get; set; }
-
- ///
- /// 设备在物联网平台中对应的产品Id
- ///
- [Required(ErrorMessage = "产品Id不能为空")]
- public string IoTPlatformProductId { get; set; }
-
+ public class DeviceCommandForApiInput:IdInput
+ {
///
/// 设备在物联网平台中发送的命令内容,JSON格式
///
diff --git a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
index ba7a5b2..6d13f23 100644
--- a/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
+++ b/src/JiShe.IoT.Application/DeviceAggregation/DeviceAggregationService.cs
@@ -267,6 +267,8 @@ namespace JiShe.IoT.DeviceAggregation
RedisProvider.Instance.HSet(RedisConst.CacheAllDeviceInfoHashKey, input.DeviceAddress, deviceCacheInfos);
+
+
return input.Adapt();
}
@@ -279,13 +281,13 @@ namespace JiShe.IoT.DeviceAggregation
{
try
{
- var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(input.DeviceAddress);
+ var deviceInfo = await deviceAppService.FindByIdAsync(input);
//将指令存储Kafka的OneNET主题中
var commandRequest = new OpenApiRequest()
{
Message = new ReceiveCommandInfoDto()
{
- DeviceAddress = input.DeviceAddress,
+ DeviceAddress = deviceInfo.DeviceAddress,
Commands = input.CommandContent.Deserialize>(),
DeviceType = DeviceTypeEnum.Focus,//todo 设备类型 需要跟设备统一什么情况下知道具体设备类型
SourceType = DeviceTelemetrySourceTypeEnum.AdminSystem,
@@ -294,8 +296,7 @@ namespace JiShe.IoT.DeviceAggregation
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
{
- await producerService.ProduceAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, $"{GuidGenerator.Create()}", commandRequest);
- return true;
+ return await DeviceCommandInfoToOneNET(deviceInfo, commandRequest);
}
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
{
@@ -568,6 +569,45 @@ namespace JiShe.IoT.DeviceAggregation
throw;
}
}
+
+ ///
+ /// 发送OneNET平台设备指令
+ ///
+ ///
+ ///
+ ///
+ ///
+ public async Task DeviceCommandInfoToOneNET(DeviceManagementInfoDto deviceInfo, OpenApiRequest commandRequest)
+ {
+ 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("设备不在线");
+ }
+
+ await producerService.ProduceAsync(KafkaTopicConsts.OneNETCommandIssuedEventName, $"{GuidGenerator.Create()}", commandRequest);
+ return true;
+ }
+ catch (Exception)
+ {
+
+ throw;
+ }
+ }
#endregion
#region CTWing 设备操作
diff --git a/src/JiShe.IoT.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs b/src/JiShe.IoT.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs
index 1cbfaab..2a5b3f1 100644
--- a/src/JiShe.IoT.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs
+++ b/src/JiShe.IoT.Application/Subscribers/ServiceCommunicationChannelSubscriberService.cs
@@ -83,6 +83,13 @@ namespace JiShe.ServicePro.OneNETManagement.Subscribers
return SubscribeAck.Fail();
}
+ //更新设备数据缓存到Redis
+ DeviceCacheInfos deviceCacheInfos = deviceEntity.Adapt();
+ deviceCacheInfos.IoTPlatformResponse = null;
+ deviceCacheInfos.PlatformPassword = null;
+
+ RedisProvider.Instance.HSet(RedisConst.CacheAllDeviceInfoHashKey, deviceEntity.DeviceAddress, deviceCacheInfos);
+
return SubscribeAck.Success();
}
catch (Exception)
diff --git a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
index 62b6d1d..9f49886 100644
--- a/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
+++ b/src/JiShe.IoT.HttpApi/Controllers/DeviceAggregationController.cs
@@ -66,11 +66,25 @@ namespace JiShe.IoT.Controllers
///
///
///
- [HttpPost("RepushDeviceInfoToIoTPlatform")]
+ [HttpPost(nameof(RepushDeviceInfoToIoTPlatform))]
[SwaggerOperation(summary: "重新推送设备信息到物联网平台", Tags = new[] { "AggregationDevice" })]
public Task RepushDeviceInfoToIoTPlatform(IdInput input)
{
return _deviceAggregationService.RepushDeviceInfoToIoTPlatform(input);
}
+
+
+ ///
+ /// 发送设备指令信息
+ ///
+ ///
+ ///
+ [HttpPost(nameof(DeviceCommandForApiAsync))]
+ [SwaggerOperation(summary: "发送设备指令信息", Tags = new[] { "AggregationDevice" })]
+ public Task DeviceCommandForApiAsync(DeviceCommandForApiInput input)
+ {
+ return _deviceAggregationService.DeviceCommandForApiAsync(input);
+ }
+
}
}