AacpsBusAlert/BinaryDad.AacpsBusAlert.Worker/Program.cs

60 lines
2.0 KiB
C#

using BinaryDad.AacpsBusAlert;
using BinaryDad.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
builder.AddEnvironmentVariables();
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");// context.HostingEnvironment.EnvironmentName;
builder.AddJsonFile("appsettings.json");
builder.AddJsonFile($"appsettings.{env}.json", true);
})
.ConfigureServices((context, services) =>
{
services.AddHttpClient();
services.Configure<List<User>>(context.Configuration.GetSection("Users"));
})
.Build();
var configuration = host.Services.GetService<IConfiguration>();
var httpClientFactory = host.Services.GetService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
var apiBaseUrl = configuration["Urls:Api"];
var apiBusRouteUrl = UrlUtility.Combine(apiBaseUrl, "api/routes");
var busRoutesRaw = await httpClient.GetStringAsync(apiBusRouteUrl);
var userRoutes = host.Services.GetService<IOptions<List<User>>>();
var busRoutes = JsonConvert.DeserializeObject<ICollection<BusRoute>>(busRoutesRaw);
foreach (var userRoute in userRoutes.Value)
{
var matchedRoutes = busRoutes
.Join(userRoute.Routes, b => b.BusNumber, u => u.BusNumber, (b, u) => new
{
UserRoute = u,
BusRoute = b
})
.ToList();
// send alert for these buses
Console.WriteLine($"User {userRoute.Email} has {matchedRoutes.Count} delays:");
foreach (var matchedRoute in matchedRoutes)
{
Console.WriteLine($" {matchedRoute.BusRoute.BusNumber} ({matchedRoute.UserRoute.Label}): {matchedRoute.BusRoute.Impact} for {matchedRoute.BusRoute.Schedules} at {matchedRoute.BusRoute.Schools}");
}
}
Console.ReadLine();