You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.7 KiB
C#

using System.Reflection;
using Com.Ctrip.Framework.Apollo;
using Sln.Wcs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using NeoSmart.Caching.Sqlite;
using Newtonsoft.Json;
using Sln.Wcs.HikRoBotAdapter.Domain.Dto.GbTaskSubmit;
using Sln.Wcs.HikRoBotAdapter.Service;
using Sln.Wcs.HikRoBotDispatcher;
using Sln.Wcs.Model.Domain;
using Sln.Wcs.Repository;
using Sln.Wcs.Repository.service;
using Sln.Wcs.Serilog;
using ZiggyCreatures.Caching.Fusion;
using ZiggyCreatures.Caching.Fusion.Serialization.NewtonsoftJson;
var builder = WebApplication.CreateBuilder(args);
var basePath = AppContext.BaseDirectory;
// ---- 配置 ----
var localConfig = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var apolloConfigSection = localConfig.GetSection("apollo");
builder.Services.AddSingleton<IConfiguration>(localConfig);
var configProvider = new UpdateableConfigProvider();
configProvider.Set("PLC参数", "");
var apolloConfig = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddApollo(apolloConfigSection)
.AddDefault()
.Add(configProvider)
.Build();
builder.Services.Remove(new ServiceDescriptor(typeof(IConfiguration), localConfig));
builder.Services.AddSingleton<IConfiguration>(apolloConfig);
// ---- DI 扫描 ----
var assemblies = new[]
{
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.HikRoBotSdk.dll")),
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotAdapter.dll")),
Assembly.LoadFrom(Path.Combine(basePath, "Sln.Wcs.HikRoBotDispatcher.dll")),
};
builder.Services.Scan(scan => scan.FromAssemblies(assemblies)
.AddClasses().AsImplementedInterfaces().AsSelf().WithTransientLifetime());
builder.Services.AddSingleton(typeof(SerilogHelper));
builder.Services.AddSqlSugarSetup();
// builder.Services.AddFusionCache()
// .WithSerializer(new FusionCacheNewtonsoftJsonSerializer())
// .WithDistributedCache(new SqliteCache(new SqliteCacheOptions { CachePath = apolloConfig["cachePath"]! }));
// ---- Swagger ----
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
c.SwaggerDoc("v1", new OpenApiInfo { Title = "HikRobot Engine API", Version = "v1" }));
var app = builder.Build();
// ---- 启动初始化 ----
var sp = app.Services;
sp.UseSerilogExtensions();
var log = sp.GetRequiredService<SerilogHelper>();
log.Info($"HikRoBotServer 启动, 日志:{apolloConfig["logPath"]}");
app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "HikRobot Engine v1"); c.RoutePrefix = "swagger"; });
// ---- HikRoBotDispatchHub.ReciveTask ----
var hub = sp.GetRequiredService<HikRoBotDispatchHub>();
var api = app.MapGroup("/api");
// Hub 方法: ReciveTask
api.MapPost("/task/receive", (ReceiveTaskRequest req) =>
{
var detail = new LiveTaskDetail
{
taskCode = req.TaskCode, deviceType = 1, taskStatus = 1,
startPoint = req.StartPoint, endPoint = req.EndPoint
};
hub.ReciveTask(detail);
return Results.Ok(new { success = true });
});
api.MapGet("/health", () => Results.Ok(new { time = DateTime.Now, status = "ok" }));
log.Info("HikRoBotServer 就绪: http://localhost:5200/swagger");
app.Run();
record ReceiveTaskRequest(string TaskCode, string StartPoint, string EndPoint);