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(); protected IFreeRedisProvider FreeRedisProvider => LazyServiceProvider.LazyGetRequiredService(); protected IoTAppService() { LocalizationResource = typeof(IoTResource); } /// /// 获取设备遥测指令信息 /// /// IoTDBOptions /// 请求原始对象 /// 设备信息 /// 明文消息体 /// /// 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(); 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; } } /// /// 处理开放接口请求 /// /// /// 接口请求体 /// 服务配置 /// protected HttpDataResult HandleOpenApiRequest(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(null, "请求参数不能为空", -1101); } if (input.Timestamp <= 946656000000)//时间戳小于2000年,视为错误 { return HttpDataResultExtensions.Failed(null, "时间戳异常", -1102); } bool verifySignatureReult = EncryptUtil.OpenApiVerifySignature(input.Message, input.Nonce, input.Timestamp, input.Signature, serverOptions.SignatureToken); if (verifySignatureReult == false)//签名校验失败 { return HttpDataResultExtensions.Failed(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(); } else { tempMessageBody = input.Message; messageBody = input.Message.Deserialize(); } if (messageBody == null) { return HttpDataResultExtensions.Failed(null, "获取数据体失败", -1104); } return HttpDataResultExtensions.Success(messageBody, tempMessageBody); } } }