This commit is contained in:
nutchayut
2024-05-31 00:38:31 +07:00
commit 88ddddd7c2
234 changed files with 37557 additions and 0 deletions

102
OCPP.Core.Server/Startup.cs Normal file
View File

@@ -0,0 +1,102 @@
/*
* OCPP.Core - https://github.com/dallmann-consulting/OCPP.Core
* Copyright (C) 2020-2021 dallmann consulting GmbH.
* All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using OCPP.Core.Database;
namespace OCPP.Core.Server
{
public class Startup
{
/// <summary>
/// ILogger object
/// </summary>
private ILoggerFactory LoggerFactory { get; set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOCPPDbContext(Configuration);
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
//public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env,
ILoggerFactory loggerFactory,
IServiceScopeFactory serviceScopeFactory)
{
LoggerFactory = loggerFactory;
ILogger logger = loggerFactory.CreateLogger(typeof(Startup));
logger.LogTrace("Startup => Configure(...)");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Migrate database
using var scope = serviceScopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<OCPPCoreContext>();
// but only when not disabled (needs admin permissions in SQL-Server!)
bool dbMigrate = Configuration.GetValue<bool>("AutoMigrateDB", true);
if (dbMigrate)
{
dbContext.Database.Migrate();
}
// Set WebSocketsOptions
var webSocketOptions = new WebSocketOptions()
{
};
// Accept WebSocket
app.UseWebSockets(webSocketOptions);
// Integrate custom OCPP middleware for message processing
app.UseOCPPMiddleware();
}
}
}