2022-09-19 12:19:05 +00:00
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 ;
2022-09-19 12:19:05 +00:00
using Newtonsoft.Json ;
2022-09-14 20:26:19 +00:00
var host = Host . CreateDefaultBuilder ( args )
2022-09-19 12:19:05 +00:00
. 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 ( ) ;
2022-09-19 12:19:05 +00:00
var configuration = host . Services . GetService < IConfiguration > ( ) ;
2022-09-19 14:42:00 +00:00
2022-09-19 12:19:05 +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 > > > ( ) ;
2022-09-19 12:19:05 +00:00
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 )
{
2022-10-04 01:44:58 +00:00
Console . WriteLine ( $" {matchedRoute.BusRoute.BusNumber} ({matchedRoute.UserRoute.Label}): {matchedRoute.BusRoute.Impact} for {matchedRoute.BusRoute.Schedules} at {matchedRoute.BusRoute.Schools}" ) ;
2022-09-19 14:42:00 +00:00
}
}
2022-09-14 20:26:19 +00:00
Console . ReadLine ( ) ;