using FrontendWorkerService.Models; using FrontendWorkerService.Quartz; using FrontendWorkerService.Quartz.Jobs; using Microsoft.AspNetCore.Hosting; 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.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 FrontendWorkerService { 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.Configure(configuration.GetSection("ObixApiConfig")); #region DBHelper 注入 services.Configure(configuration.GetSection("DBConfig")); services.AddTransient(); #endregion DBHelper 注入 #region Repository 注入 //services.AddTransient(); //services.AddTransient(); services.AddTransient(); services.AddTransient(); #endregion Repository 注入 //添加Quartz服務 services.AddTransient(); services.AddTransient(); services.AddHostedService(); #region 訂閱設備點位(設定每天01:00 AM 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(OnTimeDeviceSubscriptionJob), cronExpression: configuration.GetValue("BackgroundServiceCron:OnTimeDeviceSubscriptionJob")) ); #endregion #region 更新即時值(設定5秒執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(OnTimeDeviceRawDataJob), cronExpression: configuration.GetValue("BackgroundServiceCron:OnTimeDeviceRawDataJob")) ); #endregion #region 即時告警訂閱設備點位(設定每天01:00 AM 執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(OntimeAlarmDeviceSubscriptionJob), cronExpression: configuration.GetValue("BackgroundServiceCron:OntimeAlarmDeviceSubscriptionJob")) ); #endregion #region 更新告警設備狀態值(設定5秒執行一次) services.AddSingleton(); services.AddSingleton( new JobSchedule(jobType: typeof(OntimeAlarmDeviceRawDataJob), cronExpression: configuration.GetValue("BackgroundServiceCron:OntimeAlarmDeviceRawDataJob")) ); #endregion }).ConfigureLogging((hostContext, logFactory) => { IConfiguration configuration = hostContext.Configuration; //logFactory.AddFile("Logs/log-{Date}.txt"); logFactory.AddFile(configuration.GetValue("LoggerPath") + "/log-{Date}.txt"); }); } }