45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
using IntegrationGateway.Core.Infrastructure;
|
|
using IntegrationGateway.Adapters.Owl;
|
|
using IntegrationGateway.Host;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.AddSingleton<AdapterRegistry>();
|
|
builder.Services.AddSingleton<TokenManager>();
|
|
builder.Services.AddHttpClient("VolPro", c =>
|
|
{
|
|
c.BaseAddress = new Uri(builder.Configuration["VolProBaseUrl"] ?? "http://localhost:9100");
|
|
});
|
|
builder.Services.AddSingleton<GatewayClient>();
|
|
|
|
var app = builder.Build();
|
|
app.MapControllers();
|
|
|
|
// 启动时自动向 Vol.Pro 注册
|
|
app.Lifetime.ApplicationStarted.Register(() =>
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
var gw = app.Services.GetRequiredService<GatewayClient>();
|
|
var registry = app.Services.GetRequiredService<AdapterRegistry>();
|
|
var owlCfg = builder.Configuration.GetSection("Owl");
|
|
var owlHttp = app.Services.GetRequiredService<IHttpClientFactory>().CreateClient("VolPro");
|
|
var owlAdapter = new OwlAdapter("Owl:main", owlHttp, owlCfg["BaseUrl"] ?? "http://localhost:15123", owlCfg["Username"] ?? "admin", owlCfg["Password"] ?? "admin");
|
|
registry.Register(owlAdapter);
|
|
|
|
try
|
|
{
|
|
var result = await gw.RegisterAsync();
|
|
Console.WriteLine($"[Gateway] Registered as NodeId={result?.NodeId}, Devices={result?.Devices.Count ?? 0}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[Gateway] Registration failed: {ex.Message}");
|
|
}
|
|
});
|
|
});
|
|
|
|
app.Run();
|