/// <summary> /// Test activation of endpoint /// </summary> /// <param name="endpoint"></param> /// <param name="options"></param> /// <returns></returns> private async Task TestBrowseAsync(EndpointRegistrationApiModel endpoint, CliOptions options) { EndpointInfoApiModel ep; Console.WriteLine($"Activating {endpoint.Id} for recursive browse..."); await _registry.ActivateEndpointAsync(endpoint.Id); var sw = Stopwatch.StartNew(); while (true) { ep = await _registry.GetEndpointAsync(endpoint.Id); if (ep.ActivationState == EndpointActivationState.ActivatedAndConnected && ep.EndpointState == EndpointConnectivityState.Ready) { break; } if (sw.ElapsedMilliseconds > 60000) { Console.WriteLine($"{endpoint.Id} could not be activated - skip!"); return; } } Console.WriteLine($"{endpoint.Id} activated - recursive browse."); var silent = !options.IsSet("-V", "--verbose"); var readDuringBrowse = options.IsProvidedOrNull("-v", "--readvalue"); var targetNodesOnly = options.IsProvidedOrNull("-t", "--targets"); var maxReferencesToReturn = options.GetValueOrDefault <uint>("-x", "--maxrefs", null); var workers = options.GetValueOrDefault("-w", "--workers", 1); // 1 worker per endpoint await Task.WhenAll(Enumerable.Range(0, workers).Select(i => BrowseAsync(i, endpoint.Id, silent, true, readDuringBrowse, null, targetNodesOnly, maxReferencesToReturn, null, options))); await _registry.DeactivateEndpointAsync(endpoint.Id); sw.Restart(); while (true) { ep = await _registry.GetEndpointAsync(endpoint.Id); if (ep.ActivationState == EndpointActivationState.Deactivated) { break; } if (sw.ElapsedMilliseconds > 60000) { throw new Exception($"{endpoint.Id} failed to deactivate!"); } } Console.WriteLine($"{endpoint.Id} deactivated."); }
/// <summary> /// Convert back to service model /// </summary> /// <param name="model"></param> /// <returns></returns> public static EndpointRegistrationModel ToServiceModel( this EndpointRegistrationApiModel model) { if (model == null) { return(null); } return(new EndpointRegistrationModel { Id = model.Id, Endpoint = model.Endpoint.ToServiceModel(), EndpointUrl = model.EndpointUrl, AuthenticationMethods = model.AuthenticationMethods? .Select(p => p.ToServiceModel()) .ToList(), SecurityLevel = model.SecurityLevel, SiteId = model.SiteId, DiscovererId = model.DiscovererId, SupervisorId = model.SupervisorId }); }
/// <summary> /// Test publish and unpublish /// </summary> /// <param name="endpoint"></param> /// <param name="options"></param> /// <returns></returns> private async Task TestPublishAsync(EndpointRegistrationApiModel endpoint, CliOptions options) { EndpointInfoApiModel ep; Console.WriteLine($"Activating {endpoint.Id} for publishing ..."); await _registry.ActivateEndpointAsync(endpoint.Id); var sw = Stopwatch.StartNew(); while (true) { ep = await _registry.GetEndpointAsync(endpoint.Id); if (ep.ActivationState == EndpointActivationState.ActivatedAndConnected && ep.EndpointState == EndpointConnectivityState.Ready) { break; } if (sw.ElapsedMilliseconds > 60000) { Console.WriteLine($"{endpoint.Id} could not be activated - skip!"); return; } } Console.WriteLine($"{endpoint.Id} activated - get all variables."); var nodes = new List <string>(); await BrowseAsync(0, endpoint.Id, true, true, false, null, true, 1000, null, options, nodes); Console.WriteLine($"{endpoint.Id} has {nodes.Count} variables."); sw.Restart(); await _publisher.NodePublishBulkAsync(endpoint.Id, new PublishBulkRequestApiModel { NodesToAdd = nodes.Select(n => new PublishedItemApiModel { NodeId = n }).ToList() }); Console.WriteLine($"{endpoint.Id} Publishing {nodes.Count} variables took {sw.Elapsed}."); sw.Restart(); await _publisher.NodePublishBulkAsync(endpoint.Id, new PublishBulkRequestApiModel { NodesToRemove = nodes.ToList() }); Console.WriteLine($"{endpoint.Id} Unpublishing {nodes.Count} variables took {sw.Elapsed}."); await _registry.DeactivateEndpointAsync(endpoint.Id); sw.Restart(); while (true) { ep = await _registry.GetEndpointAsync(endpoint.Id); if (ep.ActivationState == EndpointActivationState.Deactivated) { break; } if (sw.ElapsedMilliseconds > 60000) { throw new Exception($"{endpoint.Id} failed to deactivate!"); } } Console.WriteLine($"{endpoint.Id} deactivated."); }
/// <summary> /// Test activation of endpoint /// </summary> /// <param name="endpoint"></param> /// <param name="options"></param> /// <returns></returns> private async Task TestActivationAsync(EndpointRegistrationApiModel endpoint, CliOptions options) { EndpointInfoApiModel ep; var repeats = options.GetValueOrDefault("-r", "--repeat", 10); // 10 times for (var i = 0; i < repeats; i++) { await _registry.ActivateEndpointAsync(endpoint.Id); var sw = Stopwatch.StartNew(); while (true) { ep = await _registry.GetEndpointAsync(endpoint.Id); if (ep.ActivationState == EndpointActivationState.ActivatedAndConnected) { break; } if (sw.ElapsedMilliseconds > 60000) { throw new Exception($"{endpoint.Id} failed to activate!"); } } Console.WriteLine($"{endpoint.Id} activated."); while (options.IsSet("-b", "--browse") || options.IsSet("-w", "--waitstate")) { if (ep.EndpointState != EndpointConnectivityState.Connecting) { Console.WriteLine($"{endpoint.Id} now in {ep.EndpointState} state."); break; } if (sw.ElapsedMilliseconds > 60000) { throw new Exception($"{endpoint.Id} failed to get endpoint state!"); } ep = await _registry.GetEndpointAsync(endpoint.Id); } if (ep.EndpointState == EndpointConnectivityState.Ready && options.IsSet("-b", "--browse")) { var silent = !options.IsSet("-V", "--verbose"); var recursive = options.IsSet("-R", "--recursive"); var readDuringBrowse = options.IsProvidedOrNull("-v", "--readvalue"); var node = options.GetValueOrDefault <string>("-n", "--nodeid", null); var targetNodesOnly = options.IsProvidedOrNull("-t", "--targets"); var maxReferencesToReturn = options.GetValueOrDefault <uint>("-x", "--maxrefs", null); var direction = options.GetValueOrDefault <BrowseDirection>("-d", "--direction", null); await BrowseAsync(0, endpoint.Id, silent, recursive, readDuringBrowse, node, targetNodesOnly, maxReferencesToReturn, direction, options); } else { await Task.Delay(_rand.Next( options.GetValueOrDefault("-l", "--min-wait", 1000), // 1 seconds options.GetValueOrDefault("-h", "--max-wait", 20000))); // 20 seconds } await _registry.DeactivateEndpointAsync(endpoint.Id); sw.Restart(); while (true) { ep = await _registry.GetEndpointAsync(endpoint.Id); if (ep.ActivationState == EndpointActivationState.Deactivated) { break; } if (sw.ElapsedMilliseconds > 60000) { throw new Exception($"{endpoint.Id} failed to deactivate!"); } } Console.WriteLine($"{endpoint.Id} deactivated."); } }