/// <summary> /// Create request wrapper /// </summary> /// <param name="request"></param> /// <param name="networkClass"></param> public DiscoveryRequest(DiscoveryRequestModel request, NetworkClass networkClass = NetworkClass.Wired) { Request = request?.Clone() ?? throw new ArgumentNullException(nameof(request)); NetworkClass = networkClass; if (!string.IsNullOrEmpty(request.Configuration?.AddressRangesToScan)) { if (AddressRange.TryParse(request.Configuration?.AddressRangesToScan, out var addresses)) { AddressRanges = addresses; } } if (AddressRanges == null) { if (request.Discovery == DiscoveryMode.Fast) { var interfaces = NetworkInformationEx.GetAllNetInterfaces(NetworkClass.Wired); AddressRanges = interfaces.Select(t => new AddressRange(t, false, 24)); AddressRanges = AddressRanges.Concat(interfaces .Where(t => t.Gateway != null && !t.Gateway.Equals(System.Net.IPAddress.Any) && !t.Gateway.Equals(System.Net.IPAddress.None)) .Select(i => new AddressRange(i.Gateway, 32))); } } if (!string.IsNullOrEmpty(request.Configuration?.PortRangesToScan)) { if (PortRange.TryParse(request.Configuration?.PortRangesToScan, out var ports)) { PortRanges = ports; } } if (PortRanges == null) { switch (request.Discovery) { case DiscoveryMode.Local: PortRanges = PortRange.All; break; case DiscoveryMode.Fast: PortRanges = PortRange.WellKnown; break; case DiscoveryMode.Scan: PortRanges = PortRange.Unassigned; break; default: PortRanges = PortRange.OpcUa; break; } } }
/// <summary> /// Create request wrapper /// </summary> /// <param name="request"></param> /// <param name="networkClass"></param> /// <param name="isScan"></param> public DiscoveryRequest(DiscoveryRequestModel request, NetworkClass networkClass = NetworkClass.Wired, bool isScan = false) { Request = request?.Clone() ?? throw new ArgumentNullException(nameof(request)); _cts = new CancellationTokenSource(); NetworkClass = networkClass; IsScan = isScan; if (request == null) { request = new DiscoveryRequestModel { Discovery = DiscoveryMode.Off }; } if (request.Configuration == null) { request.Configuration = new DiscoveryConfigModel(); } if (!string.IsNullOrEmpty(request.Configuration.AddressRangesToScan)) { if (AddressRange.TryParse(request.Configuration.AddressRangesToScan, out var addresses)) { AddressRanges = addresses; } } if (AddressRanges == null) { switch (request.Discovery) { case DiscoveryMode.Local: AddressRanges = NetworkInformationEx.GetAllNetInterfaces(NetworkClass) .Select(t => new AddressRange(t, true)).Distinct(); break; case DiscoveryMode.Fast: var interfaces = NetworkInformationEx.GetAllNetInterfaces(NetworkClass.Wired); AddressRanges = interfaces.Select(t => new AddressRange(t, false, 24)); AddressRanges = AddressRanges.Concat(interfaces .Where(t => t.Gateway != null && !t.Gateway.Equals(System.Net.IPAddress.Any) && !t.Gateway.Equals(System.Net.IPAddress.None)) .Select(i => new AddressRange(i.Gateway, 32))); break; case DiscoveryMode.Off: AddressRanges = Enumerable.Empty <AddressRange>(); break; case DiscoveryMode.Scan: AddressRanges = NetworkInformationEx.GetAllNetInterfaces(NetworkClass) .Select(t => new AddressRange(t, false)).Distinct(); break; default: AddressRanges = Enumerable.Empty <AddressRange>(); break; } } request.Configuration.AddressRangesToScan = AddressRange.Format(AddressRanges); if (!string.IsNullOrEmpty(request.Configuration.PortRangesToScan)) { if (PortRange.TryParse(request.Configuration.PortRangesToScan, out var ports)) { PortRanges = ports; } } if (PortRanges == null) { switch (request.Discovery) { case DiscoveryMode.Local: PortRanges = PortRange.All; break; case DiscoveryMode.Fast: PortRanges = PortRange.WellKnown; break; case DiscoveryMode.Scan: PortRanges = PortRange.Unassigned; break; case DiscoveryMode.Off: PortRanges = Enumerable.Empty <PortRange>(); break; default: PortRanges = PortRange.OpcUa; break; } } request.Configuration.PortRangesToScan = PortRange.Format(PortRanges); request.Configuration.IdleTimeBetweenScans ??= kDefaultIdleTime; request.Configuration.PortProbeTimeout ??= kDefaultPortProbeTimeout; request.Configuration.NetworkProbeTimeout ??= kDefaultNetworkProbeTimeout; TotalAddresses = AddressRanges?.Sum(r => r.Count) ?? 0; TotalPorts = PortRanges?.Sum(r => r.Count) ?? 0; }