Пример #1
0
        /// <inheritdoc/>
        public async Task UpdateSupervisorAsync(string supervisorId,
                                                SupervisorUpdateModel request, CancellationToken ct)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrEmpty(supervisorId))
            {
                throw new ArgumentException(nameof(supervisorId));
            }

            // Get existing endpoint and compare to see if we need to patch.
            var deviceId = SupervisorModelEx.ParseDeviceId(supervisorId, 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(supervisorId));
                    }

                    var registration = twin.ToEntityRegistration(true) as SupervisorRegistration;
                    if (registration == null)
                    {
                        throw new ResourceNotFoundException(
                                  $"{supervisorId} is not a supervisor registration.");
                    }

                    // Update registration from update request
                    var patched = registration.ToServiceModel();

                    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;
                    }

                    // Patch
                    await _iothub.PatchAsync(registration.Patch(
                                                 patched.ToSupervisorRegistration()), false, ct);

                    return;
                }
                catch (ResourceOutOfDateException ex) {
                    _logger.Debug(ex, "Retrying updating supervisor...");
                    continue;
                }
            }
        }
 /// <summary>
 /// Create from service model
 /// </summary>
 /// <param name="model"></param>
 public SupervisorUpdateApiModel(SupervisorUpdateModel model)
 {
     SiteId          = model.SiteId;
     Discovery       = model.Discovery;
     LogLevel        = model.LogLevel;
     DiscoveryConfig = model.DiscoveryConfig == null ? null :
                       new DiscoveryConfigApiModel(model.DiscoveryConfig);
     DiscoveryCallbacks = model.DiscoveryCallbacks?
                          .Select(c => new CallbackApiModel(c)).ToList();
     RemoveDiscoveryCallbacks = model.RemoveDiscoveryCallbacks;
 }
        /// <inheritdoc/>
        public async Task UpdateSupervisorAsync(string supervisorId,
                                                SupervisorUpdateModel request, CancellationToken ct)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrEmpty(supervisorId))
            {
                throw new ArgumentException(nameof(supervisorId));
            }

            // Get existing endpoint and compare to see if we need to patch.
            var deviceId = SupervisorModelEx.ParseDeviceId(supervisorId, 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(supervisorId));
                    }

                    var registration = twin.ToRegistration(true) as SupervisorRegistration;
                    if (registration == null)
                    {
                        throw new ResourceNotFoundException(
                                  $"{supervisorId} is not a supervisor registration.");
                    }

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

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

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

                    if (request.DiscoveryConfig != null)
                    {
                        if (patched.DiscoveryConfig == null)
                        {
                            patched.DiscoveryConfig = new DiscoveryConfigModel();
                        }
                        if (request.DiscoveryConfig.AddressRangesToScan != null)
                        {
                            patched.DiscoveryConfig.AddressRangesToScan =
                                string.IsNullOrEmpty(
                                    request.DiscoveryConfig.AddressRangesToScan.Trim()) ?
                                null : request.DiscoveryConfig.AddressRangesToScan;
                        }
                        if (request.DiscoveryConfig.PortRangesToScan != null)
                        {
                            patched.DiscoveryConfig.PortRangesToScan =
                                string.IsNullOrEmpty(
                                    request.DiscoveryConfig.PortRangesToScan.Trim()) ?
                                null : request.DiscoveryConfig.PortRangesToScan;
                        }
                        if (request.DiscoveryConfig.IdleTimeBetweenScans != null)
                        {
                            patched.DiscoveryConfig.IdleTimeBetweenScans =
                                request.DiscoveryConfig.IdleTimeBetweenScans;
                        }
                        if (request.DiscoveryConfig.MaxNetworkProbes != null)
                        {
                            patched.DiscoveryConfig.MaxNetworkProbes =
                                request.DiscoveryConfig.MaxNetworkProbes <= 0 ?
                                null : request.DiscoveryConfig.MaxNetworkProbes;
                        }
                        if (request.DiscoveryConfig.NetworkProbeTimeout != null)
                        {
                            patched.DiscoveryConfig.NetworkProbeTimeout =
                                request.DiscoveryConfig.NetworkProbeTimeout.Value.Ticks == 0 ?
                                null : request.DiscoveryConfig.NetworkProbeTimeout;
                        }
                        if (request.DiscoveryConfig.MaxPortProbes != null)
                        {
                            patched.DiscoveryConfig.MaxPortProbes =
                                request.DiscoveryConfig.MaxPortProbes <= 0 ?
                                null : request.DiscoveryConfig.MaxPortProbes;
                        }
                        if (request.DiscoveryConfig.MinPortProbesPercent != null)
                        {
                            patched.DiscoveryConfig.MinPortProbesPercent =
                                request.DiscoveryConfig.MinPortProbesPercent <= 0 ||
                                request.DiscoveryConfig.MinPortProbesPercent > 100 ?
                                null : request.DiscoveryConfig.MinPortProbesPercent;
                        }
                        if (request.DiscoveryConfig.PortProbeTimeout != null)
                        {
                            patched.DiscoveryConfig.PortProbeTimeout =
                                request.DiscoveryConfig.PortProbeTimeout.Value.Ticks == 0 ?
                                null : request.DiscoveryConfig.PortProbeTimeout;
                        }
                        if (request.DiscoveryConfig.ActivationFilter != null)
                        {
                            patched.DiscoveryConfig.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.ToSupervisorRegistration()), false, ct);

                    return;
                }
                catch (ResourceOutOfDateException ex) {
                    _logger.Debug(ex, "Retrying updating supervisor...");
                    continue;
                }
            }
        }
 /// <inheritdoc/>
 public Task UpdateSupervisorAsync(string supervisorId,
                                   SupervisorUpdateModel request, CancellationToken ct)
 {
     return(_client.UpdateSupervisorAsync(supervisorId, request.ToApiModel(), ct));
 }
 /// <inheritdoc/>
 public Task UpdateSupervisorAsync(string supervisorId,
                                   SupervisorUpdateModel request)
 {
     return(_client.UpdateSupervisorAsync(supervisorId,
                                          Map <SupervisorUpdateApiModel>(request)));
 }