Browse Source

12311

feature/MultiClientLog
root 1 month ago
parent
commit
7be44af3f1
  1. 30
      LTEMvcApp/Controllers/ClientController.cs
  2. 14
      LTEMvcApp/Controllers/HomeController.cs
  3. 4
      LTEMvcApp/Controllers/TestConfigController.cs
  4. 78
      LTEMvcApp/Services/WebSocketManagerService.cs

30
LTEMvcApp/Controllers/ClientController.cs

@ -36,38 +36,38 @@ namespace LTEMvcApp.Controllers
/// <summary>
/// 启动客户端
/// </summary>
/// <param name="clientName">客户端名称</param>
/// <param name="address">客户端地址</param>
/// <returns>操作结果</returns>
[HttpPost("{clientName}/start")]
public ActionResult StartClient(string clientName)
[HttpPost("{address}/start")]
public ActionResult StartClient(string address)
{
_logger.LogInformation($"API请求: 启动客户端 {clientName}");
var success = _webSocketManager.StartClient(clientName);
_logger.LogInformation($"API请求: 启动客户端 {address}");
var success = _webSocketManager.StartClient(address);
if (success)
{
_logger.LogInformation($"客户端 {clientName} 启动成功");
return Ok(new { message = $"客户端 '{clientName}' 已启动" });
_logger.LogInformation($"客户端 {address} 启动成功");
return Ok(new { message = $"客户端 '{address}' 已启动" });
}
else
{
_logger.LogWarning($"客户端 {clientName} 启动失败");
return BadRequest($"启动客户端 '{clientName}' 失败");
_logger.LogWarning($"客户端 {address} 启动失败");
return BadRequest($"启动客户端 '{address}' 失败");
}
}
/// <summary>
/// 停止客户端
/// </summary>
/// <param name="clientName">客户端名称</param>
/// <param name="address">客户端地址</param>
/// <returns>操作结果</returns>
[HttpPost("{clientName}/stop")]
public ActionResult StopClient(string clientName)
[HttpPost("{address}/stop")]
public ActionResult StopClient(string address)
{
var success = _webSocketManager.StopClient(clientName);
var success = _webSocketManager.StopClient(address);
if (success)
return Ok(new { message = $"客户端 '{clientName}' 已停止" });
return Ok(new { message = $"客户端 '{address}' 已停止" });
else
return BadRequest($"停止客户端 '{clientName}' 失败");
return BadRequest($"停止客户端 '{address}' 失败");
}
/// <summary>

14
LTEMvcApp/Controllers/HomeController.cs

@ -97,10 +97,10 @@ public class HomeController : Controller
/// 启动测试客户端
/// </summary>
[HttpPost]
public IActionResult StartTestClient()
public IActionResult StartTestClient(string address)
{
_logger.LogInformation("启动测试客户端");
var success = _webSocketManager.StartClient("测试客户端");
_logger.LogInformation($"启动测试客户端: {address}");
var success = _webSocketManager.StartClient(address);
if (success)
{
TempData["Message"] = "测试客户端已启动";
@ -109,7 +109,6 @@ public class HomeController : Controller
{
TempData["Error"] = "启动测试客户端失败";
}
return RedirectToAction(nameof(Index));
}
@ -117,10 +116,10 @@ public class HomeController : Controller
/// 停止测试客户端
/// </summary>
[HttpPost]
public IActionResult StopTestClient()
public IActionResult StopTestClient(string address)
{
_logger.LogInformation("停止测试客户端");
var success = _webSocketManager.StopClient("测试客户端");
_logger.LogInformation($"停止测试客户端: {address}");
var success = _webSocketManager.StopClient(address);
if (success)
{
TempData["Message"] = "测试客户端已停止";
@ -129,7 +128,6 @@ public class HomeController : Controller
{
TempData["Error"] = "停止测试客户端失败";
}
return RedirectToAction(nameof(Index));
}

4
LTEMvcApp/Controllers/TestConfigController.cs

@ -128,7 +128,7 @@ namespace LTEMvcApp.Controllers
if (config == null)
return NotFound($"未找到地址为 {address} 的测试客户端配置");
var success = _webSocketManager.StartClient(config.Name);
var success = _webSocketManager.StartClient(config.Address);
if (success)
return Ok(new { message = $"测试客户端 {config.Name} 已启动" });
else
@ -164,7 +164,7 @@ namespace LTEMvcApp.Controllers
return NotFound($"未找到地址为 {address} 的测试客户端配置");
_logger.LogInformation($"API 请求: 停止测试客户端 {config.Name}");
var success = _webSocketManager.StopClient(config.Name);
var success = _webSocketManager.StopClient(config.Address);
if (success)
return Ok(new { message = $"测试客户端 {config.Name} 停止成功" });
else

78
LTEMvcApp/Services/WebSocketManagerService.cs

@ -162,65 +162,53 @@ namespace LTEMvcApp.Services
/// <summary>
/// 启动客户端
/// </summary>
/// <param name="clientName">客户端名称</param>
/// <param name="address">客户端地址</param>
/// <returns>是否成功启动</returns>
public bool StartClient(string clientName)
public bool StartClient(string address)
{
_logger.LogInformation($"启动客户端: {clientName}");
_logger.LogInformation($"启动客户端: {address}");
ClientConfig config;
// 检查是否是测试客户端
var testConfig = _testClientConfigs.FirstOrDefault(c => c.Name == clientName);
var testConfig = _testClientConfigs.FirstOrDefault(c => c.Address == address);
if (testConfig != null)
{
config = testConfig;
_logger.LogInformation($"使用测试客户端配置: {config.Name}");
_logger.LogInformation($"使用测试客户端配置: {config.Address}");
}
else if (!_configs.TryGetValue(clientName, out config))
else if (!_configs.TryGetValue(address, out config))
{
_logger.LogWarning($"客户端配置不存在: {clientName}");
_logger.LogWarning($"客户端配置不存在: {address}");
return false;
}
// 如果客户端已存在,先停止
if (_clients.TryGetValue(clientName, out var existingClient))
if (_clients.TryGetValue(address, out var existingClient))
{
existingClient.Stop();
_clients.TryRemove(clientName, out _);
_clients.TryRemove(address, out _);
}
// 通过依赖注入获取ILogger<LTEClientWebSocket>
var logger = _serviceProvider.GetService(typeof(ILogger<LTEClientWebSocket>)) as ILogger<LTEClientWebSocket>;
var client = new LTEClientWebSocket(config, logger!);
// 订阅事件
client.ConnectionOpened += (sender, e) => OnClientConnected(client);
client.ConnectionClosed += (sender, e) => OnClientDisconnected(client);
client.LogsReceived += (sender, logs) => OnLogsReceived(clientName, logs);
client.StateChanged += (sender, state) => OnStateChanged(clientName, state);
// 启动客户端
client.LogsReceived += (sender, logs) => OnLogsReceived(address, logs);
client.StateChanged += (sender, state) => OnStateChanged(address, state);
client.Start();
// 添加到客户端列表
_clients[clientName] = client;
_clients[address] = client;
return true;
}
/// <summary>
/// 停止客户端
/// </summary>
/// <param name="clientName">客户端名称</param>
/// <param name="address">客户端地址</param>
/// <returns>是否成功停止</returns>
public bool StopClient(string clientName)
public bool StopClient(string address)
{
_logger.LogInformation($"停止客户端: {clientName}");
if (_clients.TryGetValue(clientName, out var client))
_logger.LogInformation($"停止客户端: {address}");
if (_clients.TryGetValue(address, out var client))
{
client.Stop();
_clients.TryRemove(clientName, out _);
_clients.TryRemove(address, out _);
return true;
}
return false;
@ -229,11 +217,11 @@ namespace LTEMvcApp.Services
/// <summary>
/// 获取客户端状态
/// </summary>
/// <param name="clientName">客户端名称</param>
/// <param name="address">客户端地址</param>
/// <returns>客户端状态</returns>
public ClientState? GetClientState(string clientName)
public ClientState? GetClientState(string address)
{
if (_clients.TryGetValue(clientName, out var client))
if (_clients.TryGetValue(address, out var client))
{
return client.State;
}
@ -384,7 +372,7 @@ namespace LTEMvcApp.Services
{
if (config.Enabled)
{
StartClient(config.Name);
StartClient(config.Address);
}
}
}
@ -392,11 +380,11 @@ namespace LTEMvcApp.Services
/// <summary>
/// 获取客户端实例
/// </summary>
/// <param name="clientName">客户端名称</param>
/// <param name="address">客户端地址</param>
/// <returns>客户端实例</returns>
public LTEClientWebSocket? GetClientInstance(string clientName)
public LTEClientWebSocket? GetClientInstance(string address)
{
_clients.TryGetValue(clientName, out var client);
_clients.TryGetValue(address, out var client);
return client;
}
@ -476,8 +464,8 @@ namespace LTEMvcApp.Services
public bool StartTestClient()
{
var defaultConfig = GetDefaultTestClientConfig();
_logger.LogInformation("启动测试客户端: " + defaultConfig.Name);
return StartClient(defaultConfig.Name);
_logger.LogInformation("启动测试客户端: " + defaultConfig.Address);
return StartClient(defaultConfig.Address);
}
/// <summary>
@ -487,8 +475,8 @@ namespace LTEMvcApp.Services
public bool StopTestClient()
{
var defaultConfig = GetDefaultTestClientConfig();
_logger.LogInformation("停止测试客户端: " + defaultConfig.Name);
return StopClient(defaultConfig.Name);
_logger.LogInformation("停止测试客户端: " + defaultConfig.Address);
return StopClient(defaultConfig.Address);
}
/// <summary>
@ -498,7 +486,7 @@ namespace LTEMvcApp.Services
public LTEClientWebSocket? GetTestClient()
{
var defaultConfig = GetDefaultTestClientConfig();
return GetClientInstance(defaultConfig.Name);
return GetClientInstance(defaultConfig.Address);
}
/// <summary>
@ -511,7 +499,7 @@ namespace LTEMvcApp.Services
foreach (var config in _testClientConfigs)
{
if (_clients.TryGetValue(config.Name, out var client))
if (_clients.TryGetValue(config.Address, out var client))
{
testClients.Add(client);
}
@ -530,7 +518,7 @@ namespace LTEMvcApp.Services
foreach (var config in _testClientConfigs)
{
var client = GetClientInstance(config.Name);
var client = GetClientInstance(config.Address);
var state = client?.State ?? ClientState.Stop;
result.Add(new { Config = config, State = state, Client = client });
@ -735,7 +723,7 @@ namespace LTEMvcApp.Services
/// </summary>
private void OnClientConnected(LTEClientWebSocket client)
{
_logger.LogInformation($"客户端已连接: {client.Client.Config.Name}");
_logger.LogInformation($"客户端已连接: {client.Client.Config.Address}");
ClientConnected?.Invoke(this, client);
}
@ -744,7 +732,7 @@ namespace LTEMvcApp.Services
/// </summary>
private void OnClientDisconnected(LTEClientWebSocket client)
{
_logger.LogWarning($"客户端已断开: {client.Client.Config.Name}");
_logger.LogWarning($"客户端已断开: {client.Client.Config.Address}");
ClientDisconnected?.Invoke(this, client);
}

Loading…
Cancel
Save