From 34f1e65179910f3c02f0ac6813dbfefb4244d4d7 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 15 四月 2026 19:23:24 +0800
Subject: [PATCH] feat(同步服务): 添加实例同步功能并优化更新逻辑

---
 Code/测试工具/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Application/InstanceSyncService.cs |   22 +++++++++++++++++++++-
 1 files changed, 21 insertions(+), 1 deletions(-)

diff --git "a/Code/\346\265\213\350\257\225\345\267\245\345\205\267/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Application/InstanceSyncService.cs" "b/Code/\346\265\213\350\257\225\345\267\245\345\205\267/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Application/InstanceSyncService.cs"
index d23bffd..3315faf 100644
--- "a/Code/\346\265\213\350\257\225\345\267\245\345\205\267/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Application/InstanceSyncService.cs"
+++ "b/Code/\346\265\213\350\257\225\345\267\245\345\205\267/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Application/InstanceSyncService.cs"
@@ -116,7 +116,27 @@
                 // 4.4 鍒涘缓鎴栨洿鏂板疄渚�
                 if (_instanceManager.InstanceExists(device.DeviceCode))
                 {
-                    _logger.LogInformation("瀹炰緥 {DeviceCode} 宸插瓨鍦紝璺宠繃鍒涘缓", device.DeviceCode);
+                    // 瀹炰緥宸插瓨鍦紝鏇存柊閰嶇疆
+                    var instance = _instanceManager.GetInstance(device.DeviceCode);
+                    if (instance != null)
+                    {
+                        var oldState = instance.GetState();
+                        var wasRunning = oldState.Status == InstanceStatus.Running;
+
+                        // 鍋滄鏃у疄渚�
+                        await _instanceManager.StopInstanceAsync(device.DeviceCode);
+
+                        // 鍒犻櫎鏃у疄渚嬶紙淇濈暀閰嶇疆锛�
+                        await _instanceManager.DeleteInstanceAsync(device.DeviceCode, deleteConfig: false);
+
+                        // 閲嶆柊鍒涘缓瀹炰緥
+                        var newInstance = await _instanceManager.CreateInstanceAsync(config);
+                        if (wasRunning)
+                        {
+                            await _instanceManager.StartInstanceAsync(device.DeviceCode);
+                        }
+                        _logger.LogInformation("宸叉洿鏂板疄渚� {DeviceCode} (绔彛:{Port})", device.DeviceCode, device.DevicePort);
+                    }
                 }
                 else
                 {

--
Gitblit v1.9.3