2025-11-02 00:41:36 +08:00

133 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using JiShe.ServicePro;
using JiShe.ServicePro.ApacheIoTDB.Provider.Options;
using JiShe.ServicePro.Consts;
using JiShe.ServicePro.Core;
using JiShe.ServicePro.Dto;
using JiShe.ServicePro.Encrypt;
using JiShe.ServicePro.Enums;
using JiShe.ServicePro.FreeRedisProvider;
using JiShe.ServicePro.FreeSqlProvider;
using JiShe.ServicePro.IoTDBManagement.TableModels;
using System.Xml.Linq;
using Volo.Abp;
namespace JiShe.IoT
{
/* Inherit your application services from this class.
*/
public abstract class IoTAppService : ApplicationService
{
protected IFreeSqlProvider FreeSqlDbContext => LazyServiceProvider.LazyGetRequiredService<IFreeSqlProvider>();
protected IFreeRedisProvider FreeRedisProvider => LazyServiceProvider.LazyGetRequiredService<IFreeRedisProvider>();
protected IoTAppService()
{
LocalizationResource = typeof(IoTResource);
}
/// <summary>
/// 获取设备遥测指令信息
/// </summary>
/// <param name="iotDBOptions">IoTDBOptions</param>
/// <param name="input">请求原始对象</param>
/// <param name="deviceInfo">设备信息</param>
/// <param name="messageBody">明文消息体</param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
protected DeviceTelemetryPacketTaskInfo GetDeviceTelemetryPacketTaskInfo(IoTDBOptions iotDBOptions, OpenApiRequest input, DeviceCacheInfos deviceInfo,string messageBody)
{
try
{
if (iotDBOptions == null || string.IsNullOrWhiteSpace(iotDBOptions.DataBaseName) || input == null || deviceInfo == null || string.IsNullOrWhiteSpace(messageBody))
{
throw new UserFriendlyException($"设备遥测指令创建失败,参数信息异常。");
}
//反序列化消息数据,得到数据实体
ReceiveCommandInfoDto commandIssueInfo = input.Message.Deserialize<ReceiveCommandInfoDto>();
var oneNETIssueMessageEntity = new DeviceTelemetryPacketTaskInfo()
{
DataBaseName = iotDBOptions.DataBaseName,
DeviceType = $"{commandIssueInfo.DeviceType}",
DeviceAddress = commandIssueInfo.DeviceAddress,
IssueRawMessage = input.Serialize(),
IoTDataType = IoTDBDataTypeConst.Command,
TelemetryType = (int)commandIssueInfo.TelemetryType,
TelemetrySource = (int)commandIssueInfo.SourceType,
IoTPlatform = (int)commandIssueInfo.IoTPlatform,
IoTPlatformProductId = deviceInfo.IoTPlatformProductId,
IoTPlatformDeviceOpenInfo = deviceInfo.IoTPlatformDeviceOpenInfo,
IoTPlatformAccountId = deviceInfo.IoTPlatformAccountId,
AccountPhoneNumber = deviceInfo.AccountPhoneNumber,
IoTPlatformProductName = deviceInfo.IoTPlatformProductName,
IssuePayload = messageBody,
RetryCount = 0,
IssueStatus = (int)DeviceCommandIssueStatusEnum.Unprocessed,
LastIssueTime = DateTime.Now
};
return oneNETIssueMessageEntity;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 处理开放接口请求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">接口请求体</param>
/// <param name="serverOptions">服务配置</param>
/// <returns></returns>
protected HttpDataResult<T> HandleOpenApiRequest<T>(OpenApiRequest input, ServerApplicationOptions serverOptions) where T : class
{
if (input == null || serverOptions == null || string.IsNullOrWhiteSpace(input.Message) || string.IsNullOrWhiteSpace(input.Nonce) || 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, serverOptions.SignatureToken);
if (verifySignatureReult == false)//签名校验失败
{
return HttpDataResultExtensions.Failed<T>(null, "签名校验失败", -1103);
}
//判断是否需要解密
T messageBody = default;
string tempMessageBody = null;
if (string.IsNullOrWhiteSpace(input.Message))
{
return HttpDataResultExtensions.Success(messageBody, "签名校验成功,没有请求体");
}
if (serverOptions.IsAesEncrypted && !string.IsNullOrWhiteSpace(serverOptions.AesSecurityKey))
{
tempMessageBody = EncryptUtil.OpenApiDecrypto(input.Message, serverOptions.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);
}
}
}