protected override async Task ExecuteAsync(CancellationToken cancellationToken) { _logger.LogInformation("Screen Background Service is starting."); using (var scope = _scopeFactory.CreateScope()) { _data = scope.ServiceProvider.GetRequiredService <IVolatileDataService>(); IEnumerable <Item> activeCustomItems = _data.GetAllActive().Where(i => !(i is RSSItem || i is ClockItem || i is WeatherItem)); IEnumerable <Item> activeRSSItems = _data.GetAllActive().Where(i => i is RSSItem); activeCustomItems.ToList().Shuffle(); activeRSSItems.ToList().Shuffle(); Item currentItem = activeCustomItems.FirstOrDefault(); Item screenItem = currentItem; Item currentRssItem = null; bool showClock = true; while (!cancellationToken.IsCancellationRequested) { activeCustomItems = _data.GetAllActive() .Where(i => !(i is RSSItem || i is ClockItem || i is WeatherItem)); activeRSSItems = _data.GetAllActive().Where(i => i is RSSItem); try { if (screenItem != null) { // Send item to clients via websockets _logger.LogInformation($"Send item: {(screenItem.Title)}."); //SEND TO CLIENTS await _hubContext.Clients.All.BroadcastSlide(new ScreenItemViewModel(screenItem)); await Task.Delay(TimeSpan.FromSeconds(screenItem.DisplayTime)); } else { await Task.Delay(TimeSpan.FromSeconds(3)); } // Obtain new item Random r = new Random(); ClockItem clock = _data.GetSingle <ClockItem>(); WeatherItem weather = _data.GetSingle <WeatherItem>(); // bool checkIfRSSFeedActive = _data.AnyRssFeedActive(); // bool checkIfRssItemsExist = activeRSSItems.ToList().Count > 0; // bool checkIfRssShow = r.NextBool(25); if (_data.AnyRssFeedActive() && activeRSSItems.ToList().Count > 0 && r.NextBool(25)) { // Toon RSS Item als er actieve RSS feeds zijn en met een (pseudo) kans van 25% currentRssItem = GetNextItem(currentRssItem, activeRSSItems); screenItem = currentRssItem ?? screenItem; } else if (!(screenItem is ClockItem || screenItem is WeatherItem) && r.NextBool(35) && ((showClock && clock.Active) || (!showClock && weather.Active))) { screenItem = showClock ? (Item)clock : weather; showClock = !showClock; } else { currentItem = GetNextItem(currentItem, activeCustomItems); screenItem = currentItem ?? screenItem; } } catch (Exception ex) { _logger.LogError(ex, $"An unexpected error occurred in ScreenBackgroundController."); } } } _logger.LogInformation("Screen Background Service is stopping."); }
protected async override Task ExecuteAsync(CancellationToken cancellationToken) { _logger.LogInformation("Renew Background Service is starting."); var counter = 0; using (var scope = _scopeFactory.CreateScope()) { _data = scope.ServiceProvider.GetRequiredService <IVolatileDataService>(); _rss = scope.ServiceProvider.GetRequiredService <IRSSService>(); while (!cancellationToken.IsCancellationRequested) { try { counter++; _logger.LogInformation("Checking state of items."); _data.CheckItemState(); if (counter >= CheckRSSRenewCount) { _logger.LogInformation("Renewing RSSFeed items and weather forecast."); var renewed = _rss.RenewActiveRssFeeds().Result; using (var client = new HttpClient()) { try { client.BaseAddress = new Uri("https://api.openweathermap.org"); var city = _data.GetSettingByName("WeatherLocation"); var response = await client.GetAsync($"/data/2.5/forecast?q={city}&appid=e9e81d9533487c8075cd2f47af1ef9ae&units=metric&lang=nl"); response.EnsureSuccessStatusCode(); var stringResult = await response.Content.ReadAsStringAsync(); WeatherItem weather = _data.GetSingle <WeatherItem>(); weather.Forecast = stringResult; _data.Edit(weather); _data.Commit(); } catch (HttpRequestException httpRequestException) { _logger.LogInformation($"Error getting weather from OpenWeather: {httpRequestException.Message}"); } } counter = 0; } await Task.Delay(TimeSpan.FromSeconds(CheckItemStateInterval)); } catch (Exception ex) { _logger.LogError(ex, $"An unexpected error occurred in RenewBackgroundController."); } } } _logger.LogInformation("Renew Background Service is stopping."); }