wanshenmean
4 天以前 64a2aa2301946f777659239247233e47ad1e3076
Code/²âÊÔ¹¤¾ß/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Program.cs
@@ -5,6 +5,8 @@
using WIDESEAWCS_S7Simulator.Core.Entities;
using System.Text.Json.Serialization;
using WIDESEAWCS_S7Simulator.Application.Protocol;
using WIDESEAWCS_S7Simulator.Application;
using Microsoft.Extensions.Options;
using WIDESEAWCS_S7Simulator.Server.Services;
var builder = WebApplication.CreateBuilder(args);
@@ -39,6 +41,11 @@
// Register core services
builder.Services.AddSingleton<ISimulatorInstanceManager, SimulatorInstanceManager>();
// WCS æ•°æ®åº“配置
builder.Services.Configure<WcsDbOptions>(builder.Configuration.GetSection(WcsDbOptions.SectionName));
builder.Services.AddSingleton<DatabaseDeviceService>();
builder.Services.AddSingleton<InstanceSyncService>();
// Get content root path for data directory
var contentRoot = builder.Environment.ContentRootPath;
@@ -78,16 +85,26 @@
// å¯åŠ¨æ—¶åŠ è½½å·²ä¿å­˜çš„å®žä¾‹ï¼ˆä¸è‡ªåŠ¨å¯åŠ¨ï¼‰
var instanceManager = app.Services.GetRequiredService<ISimulatorInstanceManager>();
var syncService = app.Services.GetRequiredService<InstanceSyncService>();
_ = Task.Run(async () =>
{
    try
    {
        // å…ˆåŠ è½½å·²ä¿å­˜çš„å®žä¾‹
        await instanceManager.LoadSavedInstancesAsync(autoStart: false);
        Console.WriteLine($"Loaded {instanceManager.GetAllInstances().Count()} saved instances.");
        // å¦‚果启用了 WCS æ•°æ®åº“同步,则执行同步
        var wcsDbOptions = app.Services.GetRequiredService<IOptions<WcsDbOptions>>().Value;
        if (wcsDbOptions.Enabled)
        {
            await syncService.SyncInstancesAsync();
            Console.WriteLine($"WCS DB sync completed. Last sync: {syncService.LastSyncTime}");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error loading saved instances: {ex.Message}");
        Console.WriteLine($"Error during startup: {ex.Message}");
    }
});