|
|
using System.Reflection;
|
|
|
using Com.Ctrip.Framework.Apollo;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Identity.Client;
|
|
|
using NeoSmart.Caching.Sqlite;
|
|
|
using Newtonsoft.Json;
|
|
|
using Sln.Wcs.HikRoBotSdk;
|
|
|
using Sln.Wcs.Configs;
|
|
|
using Sln.Wcs.Repository;
|
|
|
using Sln.Wcs.Serilog;
|
|
|
using Sln.Wcs.ElevatorSdk.Dto;
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
|
|
using ZiggyCreatures.Caching.Fusion.Serialization.NewtonsoftJson;
|
|
|
using Microsoft.Extensions.Hosting.Internal;
|
|
|
using SqlSugar;
|
|
|
using Sln.Wcs.Plc;
|
|
|
using Serilog;
|
|
|
using Sln.Wcs.Model.Domain;
|
|
|
using Sln.Wcs.Common.Functions;
|
|
|
using Sln.Wcs.Function.Functions;
|
|
|
using Sln.Wcs.Rfid;
|
|
|
using System.Windows;
|
|
|
using Sln.Wcs.ElevatorSdk;
|
|
|
using Sln.Wcs.Repository.@base;
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
using Swashbuckle;
|
|
|
using Microsoft.OpenApi;
|
|
|
|
|
|
namespace Sln.Wcs
|
|
|
{
|
|
|
public class Program
|
|
|
{
|
|
|
private static IConfiguration _apolloConfiguration;
|
|
|
[STAThread]
|
|
|
|
|
|
static async Task Main(string[] args)
|
|
|
{
|
|
|
var services = new ServiceCollection();
|
|
|
|
|
|
ConfigureServices(services);
|
|
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
|
serviceProvider.UseSerilogExtensions();
|
|
|
var config = serviceProvider.GetService<IConfiguration>();
|
|
|
var log = serviceProvider.GetService<SerilogHelper>();
|
|
|
var sqlconfig = serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
|
|
|
#region 测试场地
|
|
|
var cts = new CancellationTokenSource();
|
|
|
var apiTask = StartSwaggerApiServer(_apolloConfiguration, serviceProvider, cts.Token);
|
|
|
|
|
|
Thread wpfThread = new Thread(() =>
|
|
|
{
|
|
|
Sln_Wpf.App app = new Sln_Wpf.App();
|
|
|
app.InitializeServices(serviceProvider);
|
|
|
Sln_Wpf.MainWindow mainWindow = new Sln_Wpf.MainWindow();
|
|
|
Sln_Wpf.App.ServiceProvider = serviceProvider;
|
|
|
app.Run(mainWindow);
|
|
|
|
|
|
});
|
|
|
|
|
|
wpfThread.SetApartmentState(ApartmentState.STA);
|
|
|
wpfThread.Start();
|
|
|
|
|
|
MainCentralControl mainCentralControl = new MainCentralControl(serviceProvider);
|
|
|
mainCentralControl.Start();
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
log.Info($"系统启动成功,日志存放位置:{config["logPath"]}");
|
|
|
await Task.WhenAny(apiTask);
|
|
|
cts.Cancel();
|
|
|
}
|
|
|
|
|
|
|
|
|
private static void ConfigureServices(IServiceCollection services)
|
|
|
{
|
|
|
var basePath = AppContext.BaseDirectory;
|
|
|
|
|
|
ApolloConfigureServices(services, ref basePath, out IConfiguration apolloConfiguration);
|
|
|
|
|
|
Assembly[] assemblies = {
|
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Common.dll")),
|
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Cache.dll")),
|
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.Repository.dll")),
|
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotApi.dll")),
|
|
|
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotSdk.dll")),
|
|
|
};
|
|
|
|
|
|
services.AddControllers();
|
|
|
|
|
|
services.Scan(scan => scan.FromAssemblies(assemblies)
|
|
|
.AddClasses()
|
|
|
.AsImplementedInterfaces()
|
|
|
.AsSelf()
|
|
|
.WithTransientLifetime());
|
|
|
|
|
|
services.AddSingleton<List<ElevatorInfo>>(x =>
|
|
|
{
|
|
|
var configuration = x.GetService<IConfiguration>();
|
|
|
return configuration.GetSection("ElevatorInfo").Get<List<ElevatorInfo>>();
|
|
|
});
|
|
|
|
|
|
services.AddSingleton(typeof(SerilogHelper));
|
|
|
|
|
|
services.AddSqlSugarSetup();
|
|
|
|
|
|
//注册AppConfig
|
|
|
services.AddSingleton<AppConfig>(provider =>
|
|
|
{
|
|
|
var configuration = provider.GetService<IConfiguration>();
|
|
|
return configuration.GetSection("AppConfig").Get<AppConfig>();
|
|
|
});
|
|
|
|
|
|
services.AddPlcFactorySetup();
|
|
|
|
|
|
services.AddRfidFactorySetup();
|
|
|
|
|
|
services.AddFusionCache()
|
|
|
.WithSerializer(
|
|
|
new FusionCacheNewtonsoftJsonSerializer()
|
|
|
)
|
|
|
.WithDistributedCache(new SqliteCache(new SqliteCacheOptions
|
|
|
{
|
|
|
CachePath = apolloConfiguration["cachePath"]
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
private static async Task StartSwaggerApiServer(IConfiguration configuration, IServiceProvider serviceProvider, CancellationToken cancellationToken)
|
|
|
{
|
|
|
// 需要安装: dotnet add package Microsoft.AspNetCore.App
|
|
|
var builder = WebApplication.CreateBuilder();
|
|
|
|
|
|
// 复用配置
|
|
|
builder.Services.AddSingleton(configuration);
|
|
|
|
|
|
// 如果有需要从 serviceProvider 复制的服务,可以在这里添加
|
|
|
// 例如:builder.Services.AddSingleton(serviceProvider.GetService<SomeService>());
|
|
|
builder.Services.AddSingleton(serviceProvider.GetService<ISqlSugarClient>());
|
|
|
builder.Services.AddSingleton(serviceProvider.GetService<SerilogHelper>());
|
|
|
|
|
|
builder.Services.AddControllers()
|
|
|
.AddApplicationPart(Assembly.LoadFrom(Path.Combine(AppContext.BaseDirectory, "Sln.Wcs.HikRoBotApi.dll")))
|
|
|
.AddControllersAsServices(); // 使用容器中的服务
|
|
|
|
|
|
// 添加 Swagger
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
|
{
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo
|
|
|
{
|
|
|
Title = "WCS AGV API",
|
|
|
Version = "v1",
|
|
|
Description = "海康AGV调度系统内部API"
|
|
|
});
|
|
|
});
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
// 配置 Swagger UI
|
|
|
app.UseSwagger();
|
|
|
app.UseSwaggerUI(c =>
|
|
|
{
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "WCS API V1");
|
|
|
c.RoutePrefix = "swagger"; // 访问地址:http://localhost:端口/swagger
|
|
|
});
|
|
|
|
|
|
// 路由配置
|
|
|
app.UseRouting();
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
{
|
|
|
endpoints.MapControllers();
|
|
|
});
|
|
|
|
|
|
// 从配置读取端口,默认 5000
|
|
|
var port = configuration["InternalApiPort"] ?? "5000";
|
|
|
var urls = $"http://*:{port}";
|
|
|
app.Urls.Add(urls);
|
|
|
|
|
|
Console.WriteLine($"[Swagger API] 启动,监听: {urls}");
|
|
|
Console.WriteLine($"[Swagger API] 文档地址: http://localhost:{port}/swagger");
|
|
|
|
|
|
await app.RunAsync(cancellationToken);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Apollo 配置中心
|
|
|
/// </summary>
|
|
|
/// <param name="services"></param>
|
|
|
/// <param name="basePath"></param>
|
|
|
/// <param name="apolloConfiguration"></param>
|
|
|
private static void ApolloConfigureServices(IServiceCollection services, ref string basePath, out IConfiguration apolloConfiguration)
|
|
|
{
|
|
|
var localConfiguration = new ConfigurationBuilder()
|
|
|
.SetBasePath(basePath)
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
.Build();
|
|
|
|
|
|
var apolloConfigSection = localConfiguration.GetSection("apollo");
|
|
|
|
|
|
services.AddSingleton<IConfiguration>(localConfiguration);
|
|
|
|
|
|
var configurationBuilder = new ConfigurationBuilder()
|
|
|
.SetBasePath(basePath)
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
.AddApollo(apolloConfigSection)
|
|
|
.AddDefault();
|
|
|
|
|
|
apolloConfiguration = configurationBuilder.Build();
|
|
|
|
|
|
_apolloConfiguration = configurationBuilder.Build();
|
|
|
|
|
|
services.Remove(new ServiceDescriptor(typeof(IConfiguration), localConfiguration));
|
|
|
services.AddSingleton<IConfiguration>(apolloConfiguration);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} |