FIC_Solar/SolarPower/Startup.cs

189 lines
8.0 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 Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using SolarPower.Helper;
using SolarPower.Models;
using SolarPower.Quartz;
using SolarPower.Quartz.Jobs;
using SolarPower.Repository.Implement;
using SolarPower.Repository.Interface;
using SolarPower.Services;
using SolarPower.Services.Implement;
using SolarPower.Services.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace SolarPower
{
public class Startup
{
public IConfiguration Configuration { get; }
public DBConfig dBConfig = new DBConfig();
private IWebHostEnvironment CurrentEnvironment { get; set; }
public Startup(IWebHostEnvironment env, IConfiguration configuration)
{
CurrentEnvironment = env;
Configuration = configuration;
dBConfig.Server = Configuration.GetValue<string>("DBConfig:Server");
dBConfig.Port = Configuration.GetValue<string>("DBConfig:Port");
dBConfig.Database = Configuration.GetValue<string>("DBConfig:Database");
dBConfig.Root = Configuration.GetValue<string>("DBConfig:Root");
dBConfig.Password = Configuration.GetValue<string>("DBConfig:Password");
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
string envName = CurrentEnvironment.EnvironmentName; //¨ú±o¥Ø«eÀô¹Ò Development, Staging, Production
services.AddLogging(
builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("NToastNotify", LogLevel.Warning)
.AddConsole();
});
#region SMPT°t¸m
services.AddTransient<ISendEmailService, SendEmailService>();
services.Configure<SMTPConfig>(Configuration.GetSection("SMTPConfig"));
#endregion
#region DBHelper ª`¤J
services.AddTransient<IDatabaseHelper>(x => new DatabaseHelper(dBConfig));
#endregion
#region Repository ª`¤J
services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<ICompanyRepository, CompanyRepository>();
services.AddTransient<IRoleRepository, RoleRepository>();
services.AddTransient<IPowerStationRepository, PowerStationRepository>();
services.AddTransient<IOperatorLogRepository, OperatorLogRepository>();
services.AddTransient<IOperationRepository, OperationRepository>();
services.AddTransient<IOverviewRepository, OverviewRepository>();
services.AddTransient<IAnalysisStationCombineRepository,AnalysisStationCombineRepository>();
services.AddTransient<IStationReportRepository, StationReportRepository>();
services.AddTransient<INoticeScheduleRepository, NoticeScheduleRepository>();
services.AddTransient<IElectricitySoldRecordRepository, ElectricitySoldRecordRepository>();
services.AddTransient<ISensorTypeRepository, SensorTypeRepository>();
services.AddTransient<ISensorDetailRepository, SensorDetailRepository>();
#endregion
double loginExpireMinute = this.Configuration.GetValue<double>("LoginExpireMinute");
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(loginExpireMinute);
});
#region ¥[¤J­I´º°õ¦æ
//services.AddHostedService<OperationScheduleBackgroundService>();
//if (envName == "Production")
if (envName == "Production")
{
////²K¥[QuartzªA°È
services.AddSingleton<IJobFactory, SingletonJobFactory>();
services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
////²K¥[Job
//#region ©w´Á­pµe
services.AddSingleton<OperationScheduleJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(OperationScheduleJob), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:OperationScheduleJob"))
);
//#endregion
//#region ­pºâ¹q¯¸°fÅܾ¹¸ê°T(¨C¾ãÂI2¤À¶}©l°õ¦æ15¤ÀÄÁ¤@­Ó´`Àô)
services.AddSingleton<CalcInverter15minJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(CalcInverter15minJob), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:CalcInverter15minJob"))
);
//#endregion
//#region ­pºâ¹q¯¸µo¹q¶qµ¥¸ê°T(¨C¾ãÂI5¤À°õ¦æ)
services.AddSingleton<CalcPowerStationJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(CalcPowerStationJob), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:CalcPowerStationJob"))
//new JobSchedule(jobType: typeof(CalcPowerStationJob), cronExpression: "0/10 * * * * ?")
);
//#endregion
//#region ­pºâ¹q¯¸¤é·Ó¶q¡BPR¡BkWP 30¤é¥­§¡¡B(¨C¤Ñ­â±á2ÂI°õ¦æ)
services.AddSingleton<CalcAvgPowerStationJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(CalcAvgPowerStationJob), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:CalcAvgPowerStationJob"))
//new JobSchedule(jobType: typeof(CalcAvgPowerStationJob), cronExpression: "0 03 7 ? * * *")
);
//#endregion
//#region ±H°eEmail(¨C¤Ñ­â±á2ÂI°õ¦æ)
services.AddSingleton<SendEmailJob>();
services.AddSingleton(
new JobSchedule(jobType: typeof(SendEmailJob), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:SendEmailJob"))
);
//#endregion
//#region ¬d¸ß²§±`·s¼W¦ÜEmail
services.AddSingleton<ExceptionSchedule>();
services.AddSingleton(
new JobSchedule(jobType: typeof(ExceptionSchedule), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:ExceptionSchedule"))
);
//#endregion
services.AddHostedService<QuartzHostedService>();
}
#endregion
services.AddApplicationInsightsTelemetry();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddFile("Logs/log-{Date}.txt");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
});
}
}
}