示例#1
0
        /// <inheritdoc/>
        public async Task <DiscovererModel> GetDiscovererAsync(string id,
                                                               CancellationToken ct)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }
            var deviceId = DiscovererModelEx.ParseDeviceId(id, out var moduleId);
            var device   = await _iothub.GetAsync(deviceId, moduleId, ct);

            var registration = device.ToEntityRegistration()
                               as DiscovererRegistration;

            if (registration == null)
            {
                throw new ResourceNotFoundException(
                          $"{id} is not a discoverer registration.");
            }
            return(registration.ToServiceModel());
        }
        /// <inheritdoc/>
        public async Task UpdateDiscovererAsync(string discovererId,
                                                DiscovererUpdateModel request, CancellationToken ct)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrEmpty(discovererId))
            {
                throw new ArgumentException(nameof(discovererId));
            }

            // Get existing endpoint and compare to see if we need to patch.
            var deviceId = DiscovererModelEx.ParseDeviceId(discovererId, out var moduleId);

            while (true)
            {
                try {
                    var twin = await _iothub.GetAsync(deviceId, moduleId, ct);

                    if (twin.Id != deviceId && twin.ModuleId != moduleId)
                    {
                        throw new ArgumentException("Id must be same as twin to patch",
                                                    nameof(discovererId));
                    }

                    var registration = twin.ToEntityRegistration(true) as DiscovererRegistration;
                    if (registration == null)
                    {
                        throw new ResourceNotFoundException(
                                  $"{discovererId} is not a discoverer registration.");
                    }

                    // Update registration from update request
                    var patched = registration.ToServiceModel();
                    if (request.Discovery != null)
                    {
                        patched.RequestedMode = (DiscoveryMode)request.Discovery;
                    }

                    if (request.SiteId != null)
                    {
                        patched.SiteId = string.IsNullOrEmpty(request.SiteId) ?
                                         null : request.SiteId;
                    }

                    if (request.LogLevel != null)
                    {
                        patched.LogLevel = request.LogLevel == TraceLogLevel.Information ?
                                           null : request.LogLevel;
                    }

                    if (request.DiscoveryConfig != null)
                    {
                        if (patched.RequestedConfig == null)
                        {
                            patched.RequestedConfig = new DiscoveryConfigModel();
                        }
                        if (request.DiscoveryConfig.AddressRangesToScan != null)
                        {
                            patched.RequestedConfig.AddressRangesToScan =
                                string.IsNullOrEmpty(
                                    request.DiscoveryConfig.AddressRangesToScan.Trim()) ?
                                null : request.DiscoveryConfig.AddressRangesToScan;
                        }
                        if (request.DiscoveryConfig.PortRangesToScan != null)
                        {
                            patched.RequestedConfig.PortRangesToScan =
                                string.IsNullOrEmpty(
                                    request.DiscoveryConfig.PortRangesToScan.Trim()) ?
                                null : request.DiscoveryConfig.PortRangesToScan;
                        }
                        if (request.DiscoveryConfig.IdleTimeBetweenScans != null)
                        {
                            patched.RequestedConfig.IdleTimeBetweenScans =
                                request.DiscoveryConfig.IdleTimeBetweenScans.Value.Ticks < 0 ?
                                null : request.DiscoveryConfig.IdleTimeBetweenScans;
                        }
                        if (request.DiscoveryConfig.MaxNetworkProbes != null)
                        {
                            patched.RequestedConfig.MaxNetworkProbes =
                                request.DiscoveryConfig.MaxNetworkProbes <= 0 ?
                                null : request.DiscoveryConfig.MaxNetworkProbes;
                        }
                        if (request.DiscoveryConfig.NetworkProbeTimeout != null)
                        {
                            patched.RequestedConfig.NetworkProbeTimeout =
                                request.DiscoveryConfig.NetworkProbeTimeout.Value.Ticks <= 0 ?
                                null : request.DiscoveryConfig.NetworkProbeTimeout;
                        }
                        if (request.DiscoveryConfig.MaxPortProbes != null)
                        {
                            patched.RequestedConfig.MaxPortProbes =
                                request.DiscoveryConfig.MaxPortProbes <= 0 ?
                                null : request.DiscoveryConfig.MaxPortProbes;
                        }
                        if (request.DiscoveryConfig.MinPortProbesPercent != null)
                        {
                            patched.RequestedConfig.MinPortProbesPercent =
                                request.DiscoveryConfig.MinPortProbesPercent <= 0 ||
                                request.DiscoveryConfig.MinPortProbesPercent > 100 ?
                                null : request.DiscoveryConfig.MinPortProbesPercent;
                        }
                        if (request.DiscoveryConfig.PortProbeTimeout != null)
                        {
                            patched.RequestedConfig.PortProbeTimeout =
                                request.DiscoveryConfig.PortProbeTimeout.Value.Ticks <= 0 ?
                                null : request.DiscoveryConfig.PortProbeTimeout;
                        }
                        if (request.DiscoveryConfig.ActivationFilter != null)
                        {
                            patched.RequestedConfig.ActivationFilter =
                                request.DiscoveryConfig.ActivationFilter.SecurityMode == null &&
                                request.DiscoveryConfig.ActivationFilter.SecurityPolicies == null &&
                                request.DiscoveryConfig.ActivationFilter.TrustLists == null ?
                                null : request.DiscoveryConfig.ActivationFilter;
                        }
                    }
                    // Patch
                    await _iothub.PatchAsync(registration.Patch(
                                                 patched.ToDiscovererRegistration()), false, ct);

                    return;
                }
                catch (ResourceOutOfDateException ex) {
                    _logger.Debug(ex, "Retrying updating discoverer...");
                    continue;
                }
            }
        }