From d216edd0e9931d71664f33e625cff6d8131a0fad Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 13 三月 2026 16:00:40 +0800
Subject: [PATCH] 重构: 实现前后端分离架构
---
Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Server/S7ServerInstance.cs | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 116 insertions(+), 1 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Server/S7ServerInstance.cs b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Server/S7ServerInstance.cs
index f29afa6..44e078b 100644
--- a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Server/S7ServerInstance.cs
+++ b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Server/S7ServerInstance.cs
@@ -1,4 +1,6 @@
using System.Collections.Concurrent;
+using System.Linq;
+using System.Net.NetworkInformation;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using HslCommunication.Reflection;
@@ -34,6 +36,11 @@
/// 瀹㈡埛绔繛鎺ヨ拷韪�
/// </summary>
private readonly ConcurrentDictionary<string, S7ClientConnection> _clients = new();
+
+ /// <summary>
+ /// 杩炴帴鐩戞帶瀹氭椂鍣�
+ /// </summary>
+ private System.Threading.Timer? _connectionMonitorTimer;
/// <summary>
/// 鏋勯�犲嚱鏁�
@@ -114,6 +121,13 @@
State.StartTime = DateTime.Now;
State.ErrorMessage = null;
+ // 鍚姩杩炴帴鐩戞帶瀹氭椂鍣紙姣�5绉掓鏌ヤ竴娆★級
+ _connectionMonitorTimer = new System.Threading.Timer(
+ _ => MonitorConnections(),
+ null,
+ TimeSpan.FromSeconds(5),
+ TimeSpan.FromSeconds(5));
+
_logger.LogInformation("S7鏈嶅姟鍣ㄥ疄渚� {InstanceId} 宸叉垚鍔熷惎鍔紝鐩戝惉绔彛: {Port}", Config.Id, Config.Port);
return true;
}
@@ -139,6 +153,10 @@
try
{
+ // 鍋滄杩炴帴鐩戞帶瀹氭椂鍣�
+ _connectionMonitorTimer?.Dispose();
+ _connectionMonitorTimer = null;
+
if (_server != null)
{
// 鍋滄鍓嶅悓姝ユ湇鍔″櫒鏁版嵁鍒癕emoryStore
@@ -183,7 +201,7 @@
State.ClientCount = _clients.Count;
State.Clients = _clients.Values.ToList();
- // 杩斿洖鐘舵�佸壇鏈�
+ // 杩斿洖鐘舵�佸壇鏈紝鍖呭惈閰嶇疆淇℃伅
return new InstanceState
{
InstanceId = State.InstanceId,
@@ -193,6 +211,34 @@
StartTime = State.StartTime,
LastActivityTime = State.LastActivityTime,
Clients = new List<S7ClientConnection>(State.Clients),
+ ErrorMessage = State.ErrorMessage
+ };
+ }
+ }
+
+ /// <summary>
+ /// 鑾峰彇瀹屾暣鐘舵�侊紙鍖呭惈閰嶇疆淇℃伅锛�
+ /// </summary>
+ public object GetFullState()
+ {
+ lock (_lock)
+ {
+ // 鏇存柊瀹㈡埛绔繛鎺ユ暟
+ State.ClientCount = _clients.Count;
+ State.Clients = _clients.Values.ToList();
+
+ return new
+ {
+ InstanceId = State.InstanceId,
+ Name = Config.Name,
+ PLCType = Config.PLCType.ToString(),
+ Port = Config.Port,
+ Status = State.Status.ToString(),
+ ClientCount = State.ClientCount,
+ TotalRequests = State.TotalRequests,
+ StartTime = State.StartTime,
+ LastActivityTime = State.LastActivityTime,
+ Clients = State.Clients,
ErrorMessage = State.ErrorMessage
};
}
@@ -407,6 +453,75 @@
}
/// <summary>
+ /// 鐩戞帶瀹㈡埛绔繛鎺ワ紙閫氳繃妫�鏌CP杩炴帴锛�
+ /// </summary>
+ private void MonitorConnections()
+ {
+ try
+ {
+ if (_server == null || State.Status != InstanceStatus.Running)
+ return;
+
+ var currentConnections = new HashSet<string>();
+ var now = DateTime.Now;
+
+ // 鑾峰彇褰撳墠鎵�鏈塗CP杩炴帴
+ var tcpConnections = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
+
+ // 绛涢�夊嚭鐩戝惉绔彛鐨勫凡寤虹珛杩炴帴
+ var connectionsOnPort = tcpConnections.Where(c =>
+ c.LocalEndPoint.Port == Config.Port &&
+ c.State == TcpState.Established);
+
+ foreach (var connection in connectionsOnPort)
+ {
+ var endPoint = connection.RemoteEndPoint.ToString();
+ currentConnections.Add(endPoint);
+
+ // 妫�鏌ユ槸鍚︽槸鏂拌繛鎺�
+ if (!_clients.ContainsKey(endPoint))
+ {
+ var clientConnection = new S7ClientConnection
+ {
+ ClientId = endPoint,
+ RemoteEndPoint = endPoint,
+ ConnectedTime = now,
+ LastActivityTime = now
+ };
+
+ _clients[endPoint] = clientConnection;
+ _logger.LogInformation("瀹炰緥 {InstanceId} 妫�娴嬪埌鏂板鎴风杩炴帴: {EndPoint}, 褰撳墠杩炴帴鏁�: {Count}",
+ Config.Id, endPoint, _clients.Count);
+ }
+ else
+ {
+ // 鏇存柊娲诲姩鏃堕棿
+ _clients[endPoint].LastActivityTime = now;
+ }
+ }
+
+ // 绉婚櫎鏂紑鐨勮繛鎺�
+ var disconnectedClients = _clients.Keys.Where(k => !currentConnections.Contains(k)).ToList();
+ foreach (var disconnected in disconnectedClients)
+ {
+ if (_clients.TryRemove(disconnected, out var client))
+ {
+ _logger.LogInformation("瀹炰緥 {InstanceId} 瀹㈡埛绔柇寮�: {EndPoint}, 褰撳墠杩炴帴鏁�: {Count}",
+ Config.Id, disconnected, _clients.Count);
+ }
+ }
+
+ // 鏇存柊鐘舵��
+ State.ClientCount = _clients.Count;
+ State.LastActivityTime = now;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogWarning(ex, "鐩戞帶瀹㈡埛绔繛鎺ユ椂鍙戠敓閿欒");
+ }
+ }
+
+ /// <summary>
/// 澧炲姞璇锋眰璁℃暟骞舵洿鏂版椿鍔ㄦ椂闂�
/// </summary>
private void IncrementRequestCount()
--
Gitblit v1.9.3