using Backend.Models; using BackendWorkerService.Quartz; using BackendWorkerService.Quartz.Jobs; using BackendWorkerService.Services.Implement; using BackendWorkerService.Services.Interface; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Quartz; using Quartz.Impl; using Quartz.Spi; using Repository.BackendRepository.Implement; using Repository.BackendRepository.Interface; using Repository.BaseRepository.Implement; using Repository.BaseRepository.Interface; using Repository.FrontendRepository.Implement; using Repository.FrontendRepository.Interface; using Repository.Helper; using Repository.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace BackendWorkerService { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseWindowsService() .ConfigureServices((hostContext, services) => { IConfiguration configuration = hostContext.Configuration; services.AddLogging( builder => { builder.AddFilter("Microsoft", LogLevel.Warning) .AddFilter("System", LogLevel.Warning) .AddFilter("NToastNotify", LogLevel.Warning) .AddConsole(); }); //services.AddHostedService(); #region 訊息通知 #region SMPT配置 services.AddTransient(); services.Configure(configuration.GetSection("SMTPConfig")); #endregion #region SMS配置 services.AddTransient(); services.Configure(configuration.GetSection("SMSConfig")); #endregion #region Line Notify配置 services.AddTransient(); #endregion #endregion 訊息通知 #region DBHelper 注入 services.Configure(configuration.GetSection("DBConfig")); services.AddTransient(); #endregion DBHelper 注入 #region BackEndConfigHelper 注入 services.Configure(configuration.GetSection("BackEndConfig")); services.AddTransient(); #endregion BackEndConfigHelper 注入 #region Repository 注入 services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); #endregion Repository 注入 //添加Quartz服務 services.AddTransient(); services.AddTransient(); services.AddHostedService(); #region 背景執行計畫(設定1min執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(ExecutionBackgroundServicePlanJob), cronExpression: configuration.GetValue("BackgroundServiceCron:ExecutionBackgroundServicePlanJob")) ); #endregion #region 訊息通知(設定每 30min 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(MessageNotificationJob), cronExpression: configuration.GetValue("BackgroundServiceCron:MessageNotificationJob")) ); #endregion #region 定時將特定資料表加入至派送任務(設定每日 2AM 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(RegularUpdateDBTableJob), cronExpression: configuration.GetValue("BackgroundServiceCron:RegularUpdateDBTableJob")) ); #endregion #region 資料派送(設定每 5min 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(DataDeliveryJob), cronExpression: configuration.GetValue("BackgroundServiceCron:DataDeliveryJob")) ); #endregion #region 停車場管理(設定每 5秒 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue("BackgroundServiceCron:ParkingJob")) ); #endregion #region 電錶歸檔(設定每 小時 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(ArchiveElectricMeterHourJob), cronExpression: configuration.GetValue("BackgroundServiceCron:ArchiveElectricMeterHourJob")) ); #endregion #region 電錶歸檔(設定每 天 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(ArchiveElectricMeterDayJob), cronExpression: configuration.GetValue("BackgroundServiceCron:ArchiveElectricMeterDayJob")) ); #endregion #region 定時取得氣象API services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue("BackgroundServiceCron:WeatherAPIJob")) ); #endregion }).ConfigureLogging((hostContext, logFactory) => { IConfiguration configuration = hostContext.Configuration; //logFactory.AddFile("Logs/log-{Date}.txt"); logFactory.AddFile(configuration.GetValue("LoggerPath") + "/log-{Date}.txt"); }); } }