From 376ce4a0a565dc78a03699fc7a39501738c65924 Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Wed, 14 Jan 2026 11:56:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=B8=9A=E5=8A=A1=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dto/BatchCreateDeviceInfoInput.cs | 21 ++++- .../Dto/QueryDeviceDataInfoInput.cs | 45 ++++++++++ .../IBusinessSystemAggregationService.cs | 7 ++ .../BusinessSystemAggregationService.cs | 85 +++++++++++++++++++ 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/QueryDeviceDataInfoInput.cs diff --git a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/BatchCreateDeviceInfoInput.cs b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/BatchCreateDeviceInfoInput.cs index aeca877..1311b62 100644 --- a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/BatchCreateDeviceInfoInput.cs +++ b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/BatchCreateDeviceInfoInput.cs @@ -17,7 +17,7 @@ namespace JiShe.IoT.BusinessSystemAggregation.Dto /// 设备来源类型,只接收 预付费业务系统推送、能耗业务系统推送 /// public DeviceSourceTypeEnum DeviceSourceType { get; set; } - + /// /// 物联网平台类型 /// @@ -33,4 +33,23 @@ namespace JiShe.IoT.BusinessSystemAggregation.Dto /// public List DeviceAddresses { get; set; } } + + public class SubDeviceInfoInput + { + /// + /// 设备地址 + /// + public string DeviceAddresses { get; set; } + + /// + /// 子设备地址 + /// + public string SubDeviceAddress { get; set; } + + /// + /// 子设备索引 + /// + public int SubDeviceIndex { get; set; } + + } } diff --git a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/QueryDeviceDataInfoInput.cs b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/QueryDeviceDataInfoInput.cs new file mode 100644 index 0000000..a1346cd --- /dev/null +++ b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/Dto/QueryDeviceDataInfoInput.cs @@ -0,0 +1,45 @@ +using JiShe.ServicePro.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JiShe.IoT.BusinessSystemAggregation.Dto +{ + /// + /// 单个查询设备信息输入 + /// + public class QueryDeviceDataInfoInput + { + /// + /// 设备类型 + /// + public DeviceTypeEnum DeviceType { get; set; } + + /// + /// 数据类型 + /// + public string IoTDataType { get; set; } + + /// + /// 开始时间,最终需要转换为纳秒级时间戳 + /// + public DateTime BeginTime { get; set; } + + /// + /// 结束时间,最终需要转换为纳秒级时间戳 + /// + public DateTime EndTime { get; set; } + + /// + /// 网关或者直连设备地址 + /// + public string DeviceAddress { get; set; } + + /// + /// 子设备地址 + /// + public string SubDeviceAddress { get; set; } + } +} diff --git a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs index b846e12..e03f1f6 100644 --- a/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs +++ b/src/JiShe.IoT.Application.Contracts/BusinessSystemAggregation/IBusinessSystemAggregationService.cs @@ -27,6 +27,13 @@ namespace JiShe.IoT.BusinessSystemAggregation /// Task>> BatchQueryDeviceDataInfoAsync(OpenApiRequest input); + /// + /// 业务系统查询单个设备数据,Msg 字段为 QueryDeviceDataInfoInput 实体 + /// + /// + /// + Task>> QueryDeviceDataInfoAsync(OpenApiRequest input); + /// /// 业务系统批量新增设备数据,Msg 字段为 BatchCreateDeviceInfoInput 实体 /// diff --git a/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs index e459796..0aebf36 100644 --- a/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs +++ b/src/JiShe.IoT.Application/BusinessSystemAggregation/BusinessSystemAggregationService.cs @@ -213,6 +213,91 @@ namespace JiShe.IoT.BusinessSystemAggregation } } + /// + /// 业务系统查询单个设备数据,Msg 字段为 QueryDeviceDataInfoInput 实体 + /// + /// + /// + [AllowAnonymous] + public async Task>> QueryDeviceDataInfoAsync(OpenApiRequest input) + { + try + { + var handleResult = HandleOpenApiRequest(input, serverOptions); + if (handleResult.Success == false) + { + return HttpDataResultExtensions.Failed>(null, handleResult.Message, handleResult.LocationCode); + } + var messageBody = handleResult.Data; + + if (string.IsNullOrWhiteSpace(messageBody.DeviceAddress)) + { + return HttpDataResultExtensions.Failed>(null, "设备地址不能为空", -103); + } + + // Lua脚本 + string luaScript = @" + local hashKey = KEYS[1] + local fieldKeys = ARGV + return redis.call('HMGET', hashKey, unpack(fieldKeys))"; + + //执行脚本 + var result = await FreeRedisProvider.Instance.EvalAsync + ( + luaScript, + new[] { RedisConst.CacheAllDeviceInfoHashKey }, + new List() { messageBody.DeviceAddress }.ToArray() + ); + + List deviceCacheInfos = new List(); + + // 处理返回结果 + if (result is object[] values) + { + foreach (var value in values) + { + var tempFocusInfo = ServiceProJsonSerializer.Deserialize(value as string); + deviceCacheInfos.Add(tempFocusInfo); + } + } + + List queryResult = new List(); + var deviceCacheInfo = deviceCacheInfos.FirstOrDefault(x => x.DeviceAddress == messageBody.DeviceAddress); + + if (deviceCacheInfo == null) + { + _logger.LogError($"{nameof(BatchQueryDeviceDataInfoAsync)} 业务系统单个查询设备数据,设备地址:{messageBody.DeviceAddress}未找到设备地址缓存信息,消息体为:{input.Serialize()}"); + } + + var pageResult = await treeModelService.OpenRequestDeviceDataInfoPageAsync(new DeviceTreeModelDataInfoInput() + { + DeviceAddress = messageBody.DeviceAddress, + DeviceType = messageBody.DeviceType, + IoTDataType = messageBody.IoTDataType, + IsNeedPaging = false, + StartCreationTime = messageBody.BeginTime, + EndCreationTime = messageBody.EndTime, + SubDeviceAddress = messageBody.SubDeviceAddress, + }); + + //todo 根据业务系统时间间隔要求进行过滤 + if (pageResult.Items != null && pageResult.Items.Count > 0) + { + queryResult.AddRange(pageResult.Items); + } + + return HttpDataResultExtensions.Success(queryResult, "查询成功"); + + + } + catch (Exception ex) + { + + return HttpDataResultExtensions.Failed>(null, $"查询设备数据失败,发送异常:{ex.Message}", -106); + + } + } + /// /// 业务系统批量新增设备数据,Msg 字段为 BatchCreateDeviceInfoInput 实体