2025-10-11 15:22:25 +08:00
|
|
|
|
using JiShe.IoT.BusinessSystemAggregation.Dto;
|
2025-10-11 17:06:32 +08:00
|
|
|
|
using JiShe.IoT.DeviceAggregation;
|
2025-10-11 15:22:25 +08:00
|
|
|
|
using JiShe.ServicePro;
|
|
|
|
|
|
using JiShe.ServicePro.ApacheIoTDB.Provider.Model;
|
2025-09-19 11:36:17 +08:00
|
|
|
|
using JiShe.ServicePro.ApacheIoTDB.Provider.Options;
|
2025-08-04 17:29:47 +08:00
|
|
|
|
using JiShe.ServicePro.Core;
|
2025-09-19 11:36:17 +08:00
|
|
|
|
using JiShe.ServicePro.DataChannelManages;
|
2025-08-04 17:29:47 +08:00
|
|
|
|
using JiShe.ServicePro.DeviceManagement.DeviceInfos;
|
|
|
|
|
|
using JiShe.ServicePro.Dto;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using JiShe.ServicePro.Encrypt;
|
|
|
|
|
|
using JiShe.ServicePro.Enums;
|
2025-08-21 11:34:16 +08:00
|
|
|
|
using JiShe.ServicePro.FreeRedisProvider;
|
2025-09-19 11:36:17 +08:00
|
|
|
|
using JiShe.ServicePro.IoTDBManagement.DataChannels;
|
2025-10-11 15:22:25 +08:00
|
|
|
|
using JiShe.ServicePro.IoTDBManagement.TreeModels;
|
2025-09-19 11:36:17 +08:00
|
|
|
|
using Mapster;
|
2025-10-11 15:22:25 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2025-10-11 15:22:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
|
|
|
|
|
|
namespace JiShe.IoT.BusinessSystemAggregation
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 业务系统聚合服务
|
|
|
|
|
|
/// </summary>
|
2025-10-11 17:06:32 +08:00
|
|
|
|
public class BusinessSystemAggregationService(IOptions<ServerApplicationOptions> options, IReliableRedisPubSubService redisPubSubService, IDeviceAppService deviceAppService, IIoTDBDataChannelManageService ioTDBDataChannelManageService, IOptions<IoTDBOptions> _ioTDBOptions, ITreeModelService treeModelService, IDeviceAggregationService deviceAggregationService, ILogger<BusinessSystemAggregationService> _logger) : IoTAppService, IBusinessSystemAggregationService
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
ServerApplicationOptions srverOptions = options.Value;
|
2025-09-19 11:36:17 +08:00
|
|
|
|
IoTDBOptions ioTDBOptions = _ioTDBOptions.Value;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-11 15:22:25 +08:00
|
|
|
|
/// 接收业务系统指令信息
|
2025-07-24 17:06:54 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public async Task<HttpDataResult> ReceiveCommandInfoAsync(OpenApiRequest input)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2025-10-11 15:22:25 +08:00
|
|
|
|
var handleResult = HandleOpenApiRequest<ReceiveCommandInfoDto>(input);
|
|
|
|
|
|
if (handleResult.Success == false)
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
2025-10-11 15:22:25 +08:00
|
|
|
|
return handleResult;
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
2025-10-11 15:22:25 +08:00
|
|
|
|
var messageBody = handleResult.Data;
|
2025-09-19 11:36:17 +08:00
|
|
|
|
string tempMessageBody = null;
|
2025-08-21 11:34:16 +08:00
|
|
|
|
|
2025-10-11 15:22:25 +08:00
|
|
|
|
//bool verifySignatureReult = EncryptUtil.OpenApiVerifySignature(input.Message, input.Nonce, input.Timestamp, input.Signature, srverOptions.SignatureToken);
|
|
|
|
|
|
//if (verifySignatureReult == false)//签名校验失败
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return HttpDataResultExtensions.Failed("签名校验失败", -101, ResponeResultEnum.NotAllowed);
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
//if (string.IsNullOrWhiteSpace(input.Message))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return HttpDataResultExtensions.Failed("指令下发内容不能为空", -102, ResponeResultEnum.Fail);
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
////判断是否需要解密
|
|
|
|
|
|
//ReceiveCommandInfoDto messageBody = null;
|
|
|
|
|
|
//string tempMessageBody = null;
|
|
|
|
|
|
//if (srverOptions.IsAesEncrypted && !string.IsNullOrWhiteSpace(srverOptions.AesSecurityKey))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// tempMessageBody = EncryptUtil.OpenApiDecrypto(input.Message, srverOptions.AesSecurityKey);
|
|
|
|
|
|
// messageBody = tempMessageBody.Deserialize<ReceiveCommandInfoDto>();
|
|
|
|
|
|
//}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// tempMessageBody = input.Message;
|
|
|
|
|
|
// messageBody = input.Message.Deserialize<ReceiveCommandInfoDto>();
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
//if (messageBody == null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return HttpDataResultExtensions.Failed("指令下发内容不能为空", -103, ResponeResultEnum.Fail);
|
|
|
|
|
|
//}
|
2025-08-04 17:29:47 +08:00
|
|
|
|
|
|
|
|
|
|
//限定来源类型必须为业务系统
|
|
|
|
|
|
if (messageBody.SourceType != DeviceTelemetrySourceTypeEnum.BusinessSystem)
|
|
|
|
|
|
{
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed("设备指令来源类型错误,业务系统传固定值2", -104, ResponeResultEnum.Fail);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var deviceInfo = await deviceAppService.FindByDeviceAddressAsync(messageBody.DeviceAddress);
|
|
|
|
|
|
|
2025-09-19 11:36:17 +08:00
|
|
|
|
if (deviceInfo == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed("设备不存在", -1041, ResponeResultEnum.Fail);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var packetTaskInfo = GetDeviceTelemetryPacketTaskInfo(ioTDBOptions, input, deviceInfo.Adapt<DeviceCacheInfos>(), tempMessageBody);
|
|
|
|
|
|
|
2025-08-04 17:29:47 +08:00
|
|
|
|
//将指令存储Kafka的OneNET主题中
|
|
|
|
|
|
if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.OneNET)
|
|
|
|
|
|
{
|
2025-09-19 11:36:17 +08:00
|
|
|
|
//数据写入遥测任务数据存储通道
|
|
|
|
|
|
await ioTDBDataChannelManageService.DeviceTelemetryTaskWriterAsync(DataChannelManage.DeviceTelemetryTaskDataChannel.Writer, (DistributedMessageCenterConst.OneNETCommandIssuedEventName, packetTaskInfo));
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-22 17:32:04 +08:00
|
|
|
|
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.OneNETCommandIssuedEventName, input);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (deviceInfo.IoTPlatform == IoTPlatformTypeEnum.CTWing)
|
|
|
|
|
|
{
|
2025-08-22 17:32:04 +08:00
|
|
|
|
await redisPubSubService.PublishReliableAsync(DistributedMessageCenterConst.CTWingAepCommandIssuedEventName, input);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed("指令处理失败,当前设备平台类型异常", -105);
|
2025-08-04 17:29:47 +08:00
|
|
|
|
}
|
2025-07-24 17:06:54 +08:00
|
|
|
|
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Success("指令下发成功");
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
2025-08-21 11:34:16 +08:00
|
|
|
|
catch (Exception ex)
|
2025-07-24 17:06:54 +08:00
|
|
|
|
{
|
2025-08-21 11:34:16 +08:00
|
|
|
|
return HttpDataResultExtensions.Failed($"指令处理失败,发送异常:{ex.Message}", -106);
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-11 15:22:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 业务系统批量查询设备数据,Msg 字段为BatchQueryDeviceInfoInput实体
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[AllowAnonymous]
|
2025-10-11 17:06:32 +08:00
|
|
|
|
public async Task<HttpDataResult<List<IoTDBDynamicObject>>> BatchQueryDeviceDataInfoAsync(OpenApiRequest input)
|
2025-10-11 15:22:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var handleResult = HandleOpenApiRequest<BatchQueryDeviceInfoInput>(input);
|
|
|
|
|
|
if (handleResult.Success == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed<List<IoTDBDynamicObject>>(null, handleResult.Message, handleResult.LocationCode);
|
|
|
|
|
|
}
|
|
|
|
|
|
var messageBody = handleResult.Data;
|
|
|
|
|
|
|
|
|
|
|
|
if (messageBody.DeviceAddresses == null || messageBody.DeviceAddresses.Count <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed<List<IoTDBDynamicObject>>(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 },
|
|
|
|
|
|
messageBody.DeviceAddresses.ToArray()
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
List<DeviceCacheInfos> deviceCacheInfos = new List<DeviceCacheInfos>();
|
|
|
|
|
|
|
|
|
|
|
|
// 处理返回结果
|
|
|
|
|
|
if (result is object[] values)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var value in values)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tempFocusInfo = ServiceProJsonSerializer.Deserialize<DeviceCacheInfos>(value as string);
|
|
|
|
|
|
deviceCacheInfos.Add(tempFocusInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<IoTDBDynamicObject> queryResult = new List<IoTDBDynamicObject>();
|
|
|
|
|
|
foreach (var deviceAddress in messageBody.DeviceAddresses)
|
|
|
|
|
|
{
|
|
|
|
|
|
var deviceCacheInfo = deviceCacheInfos.FirstOrDefault(x => x.DeviceAddress == deviceAddress);
|
|
|
|
|
|
|
|
|
|
|
|
if (deviceCacheInfo == null)
|
|
|
|
|
|
{
|
2025-10-11 17:06:32 +08:00
|
|
|
|
_logger.LogError($"{nameof(BatchQueryDeviceDataInfoAsync)} 业务系统批量查询设备数据,设备地址:{deviceAddress}未找到设备地址缓存信息,消息体为:{input.Serialize()}");
|
2025-10-11 15:22:25 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var pageResult = await treeModelService.DeviceDataInfoPageAsync(new DeviceTreeModelDataInfoInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceAddress = deviceAddress,
|
|
|
|
|
|
DeviceType = messageBody.DeviceType,
|
|
|
|
|
|
IoTDataType = messageBody.IoTDataType,
|
|
|
|
|
|
IsNeedPaging = false,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//todo 根据业务系统时间间隔要求进行过滤
|
|
|
|
|
|
if (pageResult.Items != null && pageResult.Items.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
queryResult.AddRange(pageResult.Items);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-11 17:06:32 +08:00
|
|
|
|
return HttpDataResultExtensions.Success(queryResult, "查询成功");
|
2025-10-11 15:22:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
return HttpDataResultExtensions.Failed<List<IoTDBDynamicObject>>(null, $"查询设备数据失败,发送异常:{ex.Message}", -106);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-11 17:06:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 业务系统批量新增设备数据,Msg 字段为 BatchCreateDeviceInfoInput 实体
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
|
public async Task<HttpDataResult> BatchCreateDeviceInfoAsync(OpenApiRequest input)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var handleResult = HandleOpenApiRequest<BatchCreateDeviceInfoInput>(input);
|
|
|
|
|
|
if (handleResult.Success == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed(handleResult.Message, handleResult.LocationCode);
|
|
|
|
|
|
}
|
|
|
|
|
|
var messageBody = handleResult.Data;
|
|
|
|
|
|
|
|
|
|
|
|
if (messageBody.DeviceAddresses == null || messageBody.DeviceAddresses.Count <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed("设备地址不能为空", -101);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var createResult = await deviceAggregationService.BatchCreateDeviceBusinessSystemAsync(new BatchCreateDeviceAggregationInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceSourceTypeEnum = messageBody.DeviceSourceType,
|
|
|
|
|
|
IoTPlatform = messageBody.IoTPlatform,
|
|
|
|
|
|
IoTPlatformProductId = messageBody.IoTPlatformProductId,
|
|
|
|
|
|
AddressList = messageBody.DeviceAddresses
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (createResult == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError($"{nameof(BatchCreateDeviceInfoAsync)} 业务系统批量新增设备数据没有成功,消息体为:{input.Serialize()}");
|
|
|
|
|
|
return HttpDataResultExtensions.Failed("新增设备失败", -102);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return HttpDataResultExtensions.Success("新增设备成功");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed($"查询设备数据失败,发送异常:{ex.Message}", -106);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-11 15:22:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理开放接口请求
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private HttpDataResult<T> HandleOpenApiRequest<T>(OpenApiRequest input) where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.Message) || string.IsNullOrWhiteSpace(input.Message) || string.IsNullOrWhiteSpace(input.Signature))
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed<T>(null, "请求参数不能为空", -1101);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (input.Timestamp <= 946656000000)//时间戳小于2000年,视为错误
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed<T>(null, "时间戳异常", -1102);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool verifySignatureReult = EncryptUtil.OpenApiVerifySignature(input.Message, input.Nonce, input.Timestamp, input.Signature, srverOptions.SignatureToken);
|
|
|
|
|
|
if (verifySignatureReult == false)//签名校验失败
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed<T>(null, "签名校验失败", -1103);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//判断是否需要解密
|
|
|
|
|
|
T messageBody = default;
|
|
|
|
|
|
string tempMessageBody = null;
|
|
|
|
|
|
if (srverOptions.IsAesEncrypted && !string.IsNullOrWhiteSpace(srverOptions.AesSecurityKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
tempMessageBody = EncryptUtil.OpenApiDecrypto(input.Message, srverOptions.AesSecurityKey);
|
|
|
|
|
|
messageBody = tempMessageBody.Deserialize<T>();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
tempMessageBody = input.Message;
|
|
|
|
|
|
messageBody = input.Message.Deserialize<T>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (messageBody == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HttpDataResultExtensions.Failed<T>(null, "获取数据体失败", -1104);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return HttpDataResultExtensions.Success(messageBody, tempMessageBody);
|
|
|
|
|
|
}
|
2025-07-24 17:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|