From 5c766d7e5c969b7530a014ded771973e242f25e0 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 18 三月 2026 14:51:07 +0800
Subject: [PATCH] refactor: 拆分 TaskService 任务流程并补充注释

---
 Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Program.cs |   42 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Program.cs b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Program.cs
index a006237..7ca0d53 100644
--- a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Program.cs
+++ b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Server/Program.cs
@@ -3,6 +3,9 @@
 using WIDESEAWCS_S7Simulator.Core.Persistence;
 using WIDESEAWCS_S7Simulator.Core.Memory;
 using WIDESEAWCS_S7Simulator.Core.Entities;
+using System.Text.Json.Serialization;
+using WIDESEAWCS_S7Simulator.Application.Protocol;
+using WIDESEAWCS_S7Simulator.Server.Services;
 
 var builder = WebApplication.CreateBuilder(args);
 
@@ -13,18 +16,23 @@
 {
     options.AddPolicy("AllowAll", policy =>
     {
-        policy.AllowAnyOrigin()
+        policy.WithOrigins("http://localhost:3000", "http://localhost:5173", "http://localhost:5174", "http://localhost:3001")
               .AllowAnyMethod()
               .AllowAnyHeader()
               .AllowCredentials();
     });
 });
 
-builder.Services.AddControllers()
-    .AddJsonOptions(options =>
-    {
-        options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
-    });
+builder.Services.AddControllers(options =>
+{
+    // 绂佺敤闅愬紡 [Required] 楠岃瘉
+    options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
+})
+.AddJsonOptions(options =>
+{
+    options.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
+    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
+});
 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
 builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen();
@@ -37,6 +45,13 @@
 var dataPath = Path.Combine(contentRoot, "Data");
 
 builder.Services.AddSingleton<IPersistenceService>(sp => new FilePersistenceService(dataPath));
+builder.Services.AddSingleton<IProtocolTemplateService>(sp => new FileProtocolTemplateService(dataPath));
+builder.Services.Configure<ProtocolMonitoringOptions>(builder.Configuration.GetSection("ProtocolMonitoring"));
+builder.Services.AddSingleton<MirrorAckProtocolHandler>();
+builder.Services.AddSingleton<IDeviceProtocolHandler, WcsLineProtocolHandler>();
+builder.Services.AddSingleton<IDeviceProtocolHandler, PlcLinkStackerProtocolHandler>();
+builder.Services.AddSingleton<IDeviceProtocolHandler, StackerInteractionProtocolHandler>();
+builder.Services.AddHostedService<ProtocolMonitoringHostedService>();
 builder.Services.AddSingleton<IMemoryStore>(sp =>
 {
     var config = new MemoryRegionConfig();
@@ -60,4 +75,19 @@
 
 app.MapControllers();
 
+// 鍚姩鏃跺姞杞藉凡淇濆瓨鐨勫疄渚嬶紙涓嶈嚜鍔ㄥ惎鍔級
+var instanceManager = app.Services.GetRequiredService<ISimulatorInstanceManager>();
+_ = Task.Run(async () =>
+{
+    try
+    {
+        await instanceManager.LoadSavedInstancesAsync(autoStart: false);
+        Console.WriteLine($"Loaded {instanceManager.GetAllInstances().Count()} saved instances.");
+    }
+    catch (Exception ex)
+    {
+        Console.WriteLine($"Error loading saved instances: {ex.Message}");
+    }
+});
+
 app.Run();

--
Gitblit v1.9.3