AacpsBusAlert/BinaryDad.AacpsBusAlert.Worker/Program.cs

60 lines
1.9 KiB
C#
Raw Normal View History

using BinaryDad.AacpsBusAlert;
using BinaryDad.Extensions;
using Microsoft.Extensions.Configuration;
2022-09-14 20:26:19 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2022-09-19 14:42:00 +00:00
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
2022-09-14 20:26:19 +00:00
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);
})
2022-09-19 14:42:00 +00:00
.ConfigureServices((context, services) =>
2022-09-14 20:26:19 +00:00
{
services.AddHttpClient();
2022-09-19 14:42:00 +00:00
services.Configure<List<User>>(context.Configuration.GetSection("Users"));
2022-09-14 20:26:19 +00:00
})
.Build();
var configuration = host.Services.GetService<IConfiguration>();
2022-09-19 14:42:00 +00:00
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);
2022-09-19 14:42:00 +00:00
var userRoutes = host.Services.GetService<IOptions<List<User>>>();
var busRoutes = JsonConvert.DeserializeObject<ICollection<BusRoute>>(busRoutesRaw);
2022-09-19 14:42:00 +00:00
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();
2022-09-14 20:26:19 +00:00
2022-09-19 14:42:00 +00:00
// 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}");
}
}
2022-09-14 20:26:19 +00:00
Console.ReadLine();