public async Task <NpsClientOpenedOutput> OpenAsync(NpsClientOpenInput input) { _logger.LogInformation($"开始准备开通设备端口,{input.ToJson()}"); _logger.LogInformation($"检测用户是否存在"); if (!CurrentUser?.UserId.HasValue ?? false || CurrentUser?.UserId.Value == 0) { throw new NpsException("用户不存在,请先登录", StatusCode.AuthenticationFailed); } MiniProfiler.Current.Step("GetNpsAppSecretAsync"); var npsAppSecret = await GetNpsAppSecretAsync(input.DeviceUniqueId); MiniProfiler.Current.Step("CreateNpsAppSecretIfCheckNullAsync"); npsAppSecret = await CreateNpsAppSecretIfCheckNullAsync(input.DeviceUniqueId, npsAppSecret); MiniProfiler.Current.Step("CreateNpsClientIfCheckNullAsync"); npsAppSecret = await CreateNpsClientIfCheckNullAsync(input, npsAppSecret); MiniProfiler.Current.Step("UpdateNpsClientOrNpsServerIfCheckNotNullAsync"); npsAppSecret = await UpdateNpsClientOrNpsServerIfCheckNotNullAsync(npsAppSecret); MiniProfiler.Current.Step("CreateNpsChannelIfCheckNullAsync"); npsAppSecret = await CreateNpsChannelIfCheckNullAsync(input, npsAppSecret); MiniProfiler.Current.Step("UpdateNpsChannelsIfCheckNotNullAsync"); npsAppSecret = await UpdateNpsChannelsIfCheckNotNullAsync(npsAppSecret); _logger.LogInformation($"已成功开通设备端口,{input.ToJson()}"); var npsClientOpenedOutput = Mapper.Map <NpsClientOpenedOutput>(npsAppSecret); return(npsClientOpenedOutput); }
public async Task NpsClientOpenTestAsync() { Thread.CurrentPrincipal = new ClaimsPrincipal ( new ClaimsIdentity ( new List <Claim> { new Claim(ClaimTypes.NameIdentifier, "117084294703656960") } ) ); var input = new NpsClientOpenInput { DeviceUniqueId = "BBBBBBBBBB", OpenPorts = new List <string> { "1111", "2222", "3333", "4444", "5555", "6666" }, //, "4444", "5555", "6666", "7777", "8888", "9999" Remark = "开通端口单元测试" }; var openResult = await _npsClientService.OpenAsync(input); Assert.NotNull(openResult); }
/// <summary> /// 检测设备是否已创建过客户端,如果不存在,则创建 /// </summary> /// <param name="input">开通客户端输入参数</param> /// <param name="npsAppSecret">NpsAppSecret</param> /// <returns>返回NpsAppSecret</returns> private async Task <NpsAppSecret> CreateNpsClientIfCheckNullAsync(NpsClientOpenInput input, NpsAppSecret npsAppSecret) { Check.NotNull(npsAppSecret, nameof(npsAppSecret)); _logger.LogInformation($"检测设备是否已创建过客户端,设备唯一识别编码:{npsAppSecret.DeviceUniqueId}"); if (npsAppSecret.NpsClient == null) { //调用远程Api执行添加客户端 var baseAuthInput = await BeforeRequestNpsApiAsync(); var remoteApiResult = await _npsApi.AddClientAsync(new ClientAddInput { AuthKey = baseAuthInput.AuthKey, Timestamp = baseAuthInput.Timestamp, AppSecret = npsAppSecret.AppSecret, IsConfigConnectAllow = input.IsConfigConnectAllow.ToInt32OrDefault(1), IsCompress = input.IsCompress.ToInt32OrDefault(0), IsCrypt = input.IsCrypt.ToInt32OrDefault(1), Remark = input.Remark }); if (remoteApiResult.Status == 1) {//远程添加成功后,将数据写入本地Nps客户端表中 npsAppSecret.NpsClient = await _npsClientRepository.InsertAsync(new NpsClient { Id = npsAppSecret.Id, IsConfigConnectAllow = input.IsConfigConnectAllow, IsCompress = input.IsCompress, IsCrypt = input.IsCrypt, Remark = input.Remark }); } else { throw new NpsException(remoteApiResult.Message, StatusCode.Error); } } return(npsAppSecret); }
public async Task <IExecuteResult> OpenAsync(NpsClientOpenInput input) { return(ExecuteResult.Ok(await _npsClientService.OpenAsync(input))); }
/// <summary> /// 检查是否已创建服务器客户端隧道,如果不存在,则创建 /// </summary> /// <param name="input">设备开通服务输入参数</param> /// <param name="npsAppSecret">NpsAppSecret</param> /// <returns>返回NpsAppSecret</returns> private async Task <NpsAppSecret> CreateNpsChannelIfCheckNullAsync(NpsClientOpenInput input, NpsAppSecret npsAppSecret) { Check.NotNull(npsAppSecret, nameof(npsAppSecret)); Check.NotNull(npsAppSecret.NpsClient, nameof(npsAppSecret.NpsClient)); _logger.LogInformation($"检查是否已创建服务器客户端隧道,设备唯一识别编码:{npsAppSecret.DeviceUniqueId};开通端口列表:{input.OpenPorts.ToJson()}"); if (npsAppSecret.NpsClient.NpsChannels == null) { npsAppSecret.NpsClient.NpsChannels = new List <NpsChannel>(); } //查询已开通隧道列表 var openedNpsChannels = npsAppSecret.NpsClient?.NpsChannels.Where(x => input.OpenPorts.Contains(x.DeviceAddress)); //查询已开通隧道端口号列表 var openedNpsChannelPorts = openedNpsChannels?.Select(x => x.DeviceAddress); //取需要开通端口号集合与已开通端口号集合差集,得到正在需要开通的端口列表 var needOpenPorts = input.OpenPorts.Except(openedNpsChannelPorts).ToList(); if (needOpenPorts.Count > 0) { var needOpenNpsChannels = new List <NpsChannel>(); //取自定义服务器端口 var nowMaxServerPort = await _npsChannelRepository.Select.MaxAsync(x => x.ServerPort); var startServerPort = nowMaxServerPort == 0 ? _minServerPort : nowMaxServerPort; var baseAuthInput = await BeforeRequestNpsApiAsync(); for (int index = 0; index < needOpenPorts.Count; index++) { //向远程服务器添加隧道 var remark = $"{input.Remark}_隧道_{startServerPort += 1}"; var remoteApiResult = await _npsApi.AddChannelAsync(new ChannelAddInput { AuthKey = baseAuthInput.AuthKey, Timestamp = baseAuthInput.Timestamp, Remark = remark, ServerPort = startServerPort, TargetAddress = needOpenPorts[index], ClientId = npsAppSecret.NpsClient.RemoteClientId }); if (remoteApiResult.Status == 1) {//隧道添加成功后,写入本地数据库表 needOpenNpsChannels.Add(new NpsChannel { NpsClientId = npsAppSecret.NpsClient.Id, ServerPort = startServerPort, DeviceAddress = needOpenPorts[index], Remark = remark }); } else { _logger.LogError($"开通客户端{npsAppSecret.DeviceUniqueId},端口{needOpenPorts[index]},{remoteApiResult.Message}"); continue; } } if (needOpenNpsChannels.Count > 0) { //将本次已开通隧道列表写入本地数据库 needOpenNpsChannels = await _npsChannelRepository.InsertAsync(needOpenNpsChannels); //将本次已开通隧道列表附加至查询结果 npsAppSecret.NpsClient.NpsChannels.AddRange(needOpenNpsChannels); } } return(npsAppSecret); }