ibms-dome/BackendWorkerService/Program.cs
jay.chang 5b43c7ab63 [BGService]新增燈控排程背景程式
[Webapi]搭配燈控排程背景程式進行修改
2024-04-30 16:40:48 +08:00

169 lines
7.7 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.IO;
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<Worker>();
//#region °T®§³qª¾
//#region SMPT°t¸m
//services.AddTransient<ISendEmailService, SendEmailService>();
//services.Configure<SMTPConfig>(configuration.GetSection("SMTPConfig"));
//#endregion
//#region SMS°t¸m
//services.AddTransient<ISendSMSService, SendSMSService>();
//services.Configure<SMSConfig>(configuration.GetSection("SMSConfig"));
//#endregion
//#region Line Notify°t¸m
//services.AddTransient<ISendLineNotifyService, SendLineNotifyService>();
//#endregion
//#endregion °T®§³qª¾
#region DBHelper ª`¤J
services.Configure<DBConfig>(configuration.GetSection("DBConfig"));
services.AddTransient<IDatabaseHelper, DatabaseHelper>();
#endregion DBHelper ª`¤J
#region BackEndConfigHelper ª`¤J
services.Configure<BackEndConfig>(configuration.GetSection("BackEndConfig"));
services.AddTransient<IBackEndConfigHelper, BackEndConfigHelper>();
#endregion BackEndConfigHelper ª`¤J
#region Repository ª`¤J
services.AddTransient<IBackendRepository, BackendRepository>();
services.AddTransient<IBackgroundServiceRepository, BackgroundServiceRepository>();
services.AddTransient<IBackgroundServiceMsSqlRepository, BackgroundServiceMsSqlRepository>();
services.AddTransient<IFrontendRepository, FrontendRepository>();
services.AddTransient<IBaseRepository, BaseRepository>();
#endregion Repository ª`¤J
//²K¥[QuartzªA°È
services.AddTransient<IJobFactory, SingletonJobFactory>();
services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
services.AddHostedService<QuartzHostedService>();
//#region ­I´º°õ¦æ­pµe(³]©w1min°õ¦æ¤@¦¸)
//services.AddSingleton<ExecutionBackgroundServicePlanJob>();
//services.AddSingleton(
//new JobSchedule(jobType: typeof(ExecutionBackgroundServicePlanJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ExecutionBackgroundServicePlanJob"))
//);
//#endregion
//#region °T®§³qª¾(³]©w¨C 30min °õ¦æ¤@¦¸)
//services.AddSingleton<MessageNotificationJob>();
//services.AddSingleton(
//new JobSchedule(jobType: typeof(MessageNotificationJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:MessageNotificationJob"))
//);
//#endregion
//#region ©w®É±N¯S©w¸ê®Æªí¥[¤J¦Ü¬£°e¥ô°È(³]©w¨C¤é 2AM °õ¦æ¤@¦¸)
//services.AddSingleton<RegularUpdateDBTableJob>();
//services.AddSingleton(
//new JobSchedule(jobType: typeof(RegularUpdateDBTableJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:RegularUpdateDBTableJob"))
//);
//#endregion
//#region ¸ê®Æ¬£°e(³]©w¨C 5min °õ¦æ¤@¦¸)
//services.AddSingleton<DataDeliveryJob>();
//services.AddSingleton(
//new JobSchedule(jobType: typeof(DataDeliveryJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:DataDeliveryJob"))
//);
//#endregion
#region °±¨®³õºÞ²z(³]©w¨C 5¬í °õ¦æ¤@¦¸)
services.AddSingleton<ParkingJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ParkingJob"))
);
#endregion
//#region ¹q¿öÂkÀÉ(³]©w¨C ¤p®É °õ¦æ¤@¦¸)
//services.AddSingleton<ArchiveElectricMeterHourJob>();
//services.AddSingleton(
//new JobSchedule(jobType: typeof(ArchiveElectricMeterHourJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ArchiveElectricMeterHourJob"))
//);
//#endregion
#region ¹q¿öÂkÀÉ(³]©w¨C ¤Ñ °õ¦æ¤@¦¸)
services.AddSingleton<ArchiveElectricMeterDayJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ArchiveElectricMeterDayJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ArchiveElectricMeterDayJob"))
);
#endregion
#region ©w®É¨ú±o®ðHAPI
services.AddSingleton<Quartz.Jobs.WeatherAPIJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:WeatherAPIJob"))
);
#endregion
#region ©w®ÉÀˬd¿O±±±Æµ{
services.AddSingleton< LightScheduleJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(LightScheduleJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:LightScheduleJob "))
);
#endregion
}).ConfigureLogging((hostContext, logFactory) => {
IConfiguration configuration = hostContext.Configuration;
//logFactory.AddFile("Logs/log-{Date}.txt");
string loggerPath = configuration.GetValue<string>("LoggerPath");
logFactory.AddFile($"{loggerPath}/log-{{Date}}.txt");
});
}
}