/// <summary> /// SetScanAsync /// </summary> /// <param name="discoverer"></param> /// <returns></returns> public async Task <string> SetDiscoveryAsync(DiscovererInfo discoverer) { var model = discoverer.DiscovererModel.DiscoveryConfig; DiscoveryMode discoveryMode; if (model == null) { model = new DiscoveryConfigApiModel(); } if (discoverer.ScanStatus == true) { discoveryMode = DiscoveryMode.Fast; } else { discoveryMode = DiscoveryMode.Off; } try { await _registryService.SetDiscoveryModeAsync(discoverer.DiscovererModel.Id, discoveryMode, model); } catch (Exception exception) { var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); _logger.Error(errorMessageTrace); return(errorMessageTrace); } return(null); }
/// <summary> /// SetScanAsync /// </summary> /// <param name="discoverer"></param> /// <returns></returns> public async Task <string> SetDiscoveryAsync(DiscovererInfo discoverer) { try { var discoveryMode = DiscoveryMode.Off; if (discoverer.DiscovererModel.DiscoveryConfig?.DiscoveryUrls != null && discoverer.ScanStatus) { discoveryMode = DiscoveryMode.Url; } else { discoveryMode = discoverer.ScanStatus ? DiscoveryMode.Fast : DiscoveryMode.Off; } await _registryService.SetDiscoveryModeAsync(discoverer.DiscovererModel.Id, discoveryMode, discoverer.Patch); discoverer.Patch = new DiscoveryConfigApiModel(); } catch (UnauthorizedAccessException) { return("Unauthorized access: Bad User Access Denied."); } catch (Exception exception) { _logger.Error(exception, "Failed to set discovery mode."); var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); return(errorMessageTrace); } return(null); }
/// <summary> /// UpdateDiscovererAsync /// </summary> /// <param name="discoverer"></param> /// <param name="config"></param> /// <returns></returns> public async Task <string> UpdateDiscovererAsync(DiscovererInfo discoverer, DiscoveryConfigApiModel config) { var model = new DiscovererUpdateApiModel(); model.DiscoveryConfig = discoverer.DiscovererModel.DiscoveryConfig; if (config.AddressRangesToScan != null) { model.DiscoveryConfig.AddressRangesToScan = config.AddressRangesToScan; } if (config.PortRangesToScan != null) { model.DiscoveryConfig.PortRangesToScan = config.PortRangesToScan; } if (config.ActivationFilter != null) { model.DiscoveryConfig.ActivationFilter = config.ActivationFilter; } if (config.MaxNetworkProbes != null && config.MaxNetworkProbes != 0) { model.DiscoveryConfig.MaxNetworkProbes = config.MaxNetworkProbes; } if (config.MaxPortProbes != null && config.MaxPortProbes != 0) { model.DiscoveryConfig.MaxPortProbes = config.MaxPortProbes; } if (config.NetworkProbeTimeoutMs != null && config.NetworkProbeTimeoutMs != 0) { model.DiscoveryConfig.NetworkProbeTimeoutMs = config.NetworkProbeTimeoutMs; } if (config.PortProbeTimeoutMs != null && config.PortProbeTimeoutMs != 0) { model.DiscoveryConfig.PortProbeTimeoutMs = config.PortProbeTimeoutMs; } if (config.IdleTimeBetweenScansSec != null && config.IdleTimeBetweenScansSec != 0) { model.DiscoveryConfig.IdleTimeBetweenScansSec = config.IdleTimeBetweenScansSec; } else { model.DiscoveryConfig.IdleTimeBetweenScansSec = _5MINUTES; } if (config.DiscoveryUrls != null) { model.DiscoveryConfig.DiscoveryUrls = config.DiscoveryUrls; } try { await _registryService.UpdateDiscovererAsync(discoverer.DiscovererModel.Id, model); } catch (Exception exception) { var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); _logger.Error(errorMessageTrace); return(errorMessageTrace); } return(null); }
/// <summary> /// GetDiscovererListAsync /// </summary> /// <returns>DiscovererInfo</returns> public async Task <PagedResult <DiscovererInfo> > GetDiscovererListAsync() { var pageResult = new PagedResult <DiscovererInfo>(); try { var discovererModel = new DiscovererQueryApiModel(); var applicationModel = new ApplicationRegistrationQueryApiModel(); var discoverers = await _registryService.QueryAllDiscoverersAsync(discovererModel); if (discoverers != null) { if (discoverers.Count() > 0) { foreach (var discoverer in discoverers) { var info = new DiscovererInfo { DiscovererModel = discoverer, HasApplication = false, ScanStatus = (discoverer.Discovery == DiscoveryMode.Off) || (discoverer.Discovery == null) ? false : true }; applicationModel.DiscovererId = discoverer.Id; var applications = await _registryService.QueryAllApplicationsAsync(applicationModel); if (applications != null) { info.HasApplication = true; } pageResult.Results.Add(info); } } else { pageResult.Results.Add(new DiscovererInfo { DiscovererModel = new DiscovererApiModel { Id = "No Discoveres Found" } }); } } } catch (Exception e) { Trace.TraceWarning("Can not get discoverers as list"); var errorMessage = string.Concat(e.Message, e.InnerException?.Message ?? "--", e?.StackTrace ?? "--"); Trace.TraceWarning(errorMessage); pageResult.Results.Add(new DiscovererInfo { DiscovererModel = new DiscovererApiModel { Id = e.Message } }); } pageResult.PageSize = 10; pageResult.RowCount = pageResult.Results.Count; pageResult.PageCount = (int)Math.Ceiling((decimal)pageResult.RowCount / 10); return(pageResult); }
/// <summary> /// SetScanAsync /// </summary> /// <param name="discoverer"></param> /// <param name="ipMask"></param> /// <param name="portRange"></param> /// <param name="forceScan"></param> /// <returns></returns> public async Task SetScanAsync(DiscovererInfo discoverer, string ipMask, string portRange, bool forceScan) { var model = new DiscoveryConfigApiModel(); DiscoveryMode discoveryMode; if (forceScan == true) { model.AddressRangesToScan = string.Empty; model.PortRangesToScan = string.Empty; } else { if (discoverer.DiscovererModel.DiscoveryConfig != null) { model = discoverer.DiscovererModel.DiscoveryConfig; } } if (discoverer.ScanStatus == true && forceScan == true) { discoveryMode = DiscoveryMode.Fast; if (ipMask != null) { model.AddressRangesToScan = ipMask; } if (portRange != null) { model.PortRangesToScan = portRange; } model.IdleTimeBetweenScansSec = _5MINUTES; } else { discoveryMode = DiscoveryMode.Off; } try { if (discoveryMode == DiscoveryMode.Off) { await _registryService.SetDiscoveryModeAsync(discoverer.DiscovererModel.Id, discoveryMode, new DiscoveryConfigApiModel()); } else { await _registryService.SetDiscoveryModeAsync(discoverer.DiscovererModel.Id, discoveryMode, model); } } catch (Exception exception) { var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); Trace.TraceError(errorMessageTrace); } }
/// <summary> /// SetScanAsync /// </summary> /// <param name="discoverer"></param> /// <returns></returns> public async Task <string> SetDiscoveryAsync(DiscovererInfo discoverer) { try { var discoveryMode = discoverer.ScanStatus ? DiscoveryMode.Fast : DiscoveryMode.Off; await _registryService.SetDiscoveryModeAsync(discoverer.DiscovererModel.Id, discoveryMode, discoverer.Patch); discoverer.Patch = new DiscoveryConfigApiModel(); } catch (Exception exception) { var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); _logger.Error(errorMessageTrace); return(errorMessageTrace); } return(null); }
/// <summary> /// UpdateDiscovererAsync /// </summary> /// <param name="discoverer"></param> /// <param name="config"></param> /// <returns></returns> public async Task <string> UpdateDiscovererAsync(DiscovererInfo discoverer) { try { await _registryService.UpdateDiscovererAsync(discoverer.DiscovererModel.Id, new DiscovererUpdateApiModel { DiscoveryConfig = discoverer.Patch }); discoverer.Patch = new DiscoveryConfigApiModel(); } catch (Exception exception) { var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); _logger.Error(errorMessageTrace); return(errorMessageTrace); } return(null); }
/// <summary> /// UpdateDiscovererAsync /// </summary> /// <param name="discoverer"></param> /// <param name="config"></param> /// <returns></returns> public async Task <string> UpdateDiscovererAsync(DiscovererInfo discoverer) { try { await _registryService.UpdateDiscovererAsync(discoverer.DiscovererModel.Id, new DiscovererUpdateApiModel { DiscoveryConfig = discoverer.Patch }); discoverer.Patch = new DiscoveryConfigApiModel(); } catch (UnauthorizedAccessException) { return("Unauthorized access: Bad User Access Denied."); } catch (Exception exception) { _logger.Error(exception, "Failed to update discoverer"); var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); return(errorMessageTrace); } return(null); }
/// <summary> /// Discover servers /// </summary> /// <param name="discoverer"></param> /// <returns></returns> public async Task <string> DiscoverServersAsync(DiscovererInfo discoverer) { try { await _registryService.DiscoverAsync( new DiscoveryRequestApiModel { Id = discoverer.DiscoveryRequestId, Discovery = DiscoveryMode.Fast, Configuration = discoverer.Patch }); discoverer.Patch = new DiscoveryConfigApiModel(); } catch (UnauthorizedAccessException) { return("Unauthorized access: Bad User Access Denied."); } catch (Exception exception) { _logger.Error(exception, "Failed to discoverer servers."); var errorMessageTrace = string.Concat(exception.Message, exception.InnerException?.Message ?? "--", exception?.StackTrace ?? "--"); return(errorMessageTrace); } return(null); }
/// <summary> /// GetDiscovererListAsync /// </summary> /// <param name="previousPage"></param> /// <returns>DiscovererInfo</returns> public async Task <PagedResult <DiscovererInfo> > GetDiscovererListAsync(PagedResult <DiscovererInfo> previousPage = null) { var pageResult = new PagedResult <DiscovererInfo>(); try { var discovererModel = new DiscovererQueryApiModel(); var applicationModel = new ApplicationRegistrationQueryApiModel(); var discoverers = new DiscovererListApiModel(); if (string.IsNullOrEmpty(previousPage?.ContinuationToken)) { discoverers = await _registryService.QueryDiscoverersAsync(discovererModel, _commonHelper.PageLengthSmall); if (!string.IsNullOrEmpty(discoverers.ContinuationToken)) { pageResult.PageCount = 2; } } else { discoverers = await _registryService.ListDiscoverersAsync(previousPage.ContinuationToken, _commonHelper.PageLengthSmall); if (string.IsNullOrEmpty(discoverers.ContinuationToken)) { pageResult.PageCount = previousPage.PageCount; } else { pageResult.PageCount = previousPage.PageCount + 1; } } if (discoverers != null && discoverers.Items.Any()) { foreach (var disc in discoverers.Items) { var discoverer = await _registryService.GetDiscovererAsync(disc.Id); var info = new DiscovererInfo { DiscovererModel = discoverer, HasApplication = false, ScanStatus = (discoverer.Discovery == DiscoveryMode.Off) || (discoverer.Discovery == null) ? false : true }; applicationModel.DiscovererId = discoverer.Id; var applications = await _registryService.QueryApplicationsAsync(applicationModel, 1); if (applications != null) { info.HasApplication = true; } pageResult.Results.Add(info); } if (previousPage != null) { previousPage.Results.AddRange(pageResult.Results); pageResult.Results = previousPage.Results; } pageResult.ContinuationToken = discoverers.ContinuationToken; pageResult.PageSize = _commonHelper.PageLengthSmall; pageResult.RowCount = pageResult.Results.Count; } else { pageResult.Error = "No Discoveres Found"; } } catch (UnauthorizedAccessException) { pageResult.Error = "Unauthorized access: Bad User Access Denied."; } catch (Exception e) { _logger.Warning(e, "Can not get discoverers as list"); pageResult.Error = e.Message; } return(pageResult); }
/// <summary> /// GetDiscovererListAsync /// </summary> /// <param name="previousPage"></param> /// <returns>DiscovererInfo</returns> public async Task <PagedResult <DiscovererInfo> > GetDiscovererListAsync(PagedResult <DiscovererInfo> previousPage = null, bool getNextPage = false) { var pageResult = new PagedResult <DiscovererInfo>(); try { var discovererModel = new DiscovererQueryApiModel(); var applicationModel = new ApplicationRegistrationQueryApiModel(); var discoverers = new DiscovererListApiModel(); if (!getNextPage || string.IsNullOrEmpty(previousPage?.ContinuationToken)) { discoverers = await _registryService.QueryDiscoverersAsync(discovererModel, _commonHelper.PageLengthSmall); } else { discoverers = await _registryService.ListDiscoverersAsync(previousPage.ContinuationToken, _commonHelper.PageLengthSmall); } if (discoverers != null) { if (discoverers.Items != null && discoverers.Items.Any()) { foreach (var disc in discoverers.Items) { var discoverer = await _registryService.GetDiscovererAsync(disc.Id); var info = new DiscovererInfo { DiscovererModel = discoverer, HasApplication = false, ScanStatus = (discoverer.Discovery == DiscoveryMode.Off) || (discoverer.Discovery == null) ? false : true }; applicationModel.DiscovererId = discoverer.Id; var applications = await _registryService.QueryApplicationsAsync(applicationModel, 1); if (applications != null) { info.HasApplication = true; } pageResult.Results.Add(info); } } } if (previousPage != null && getNextPage) { previousPage.Results.AddRange(pageResult.Results); pageResult.Results = previousPage.Results; } pageResult.ContinuationToken = discoverers.ContinuationToken; } catch (UnauthorizedAccessException) { pageResult.Error = "Unauthorized access: Bad User Access Denied."; } catch (ResourceInvalidStateException) { pageResult.Error = "IotHubQuotaExceeded. Send and Receive operations are blocked for this hub until the next UTC day."; } catch (Exception e) { var message = "Cannot get discoverers as list"; _logger.Warning(e, message); pageResult.Error = message; } return(pageResult); }