|
|
|
using System.Diagnostics;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using LTEMvcApp.Models;
|
|
|
|
using LTEMvcApp.Services;
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
namespace LTEMvcApp.Controllers;
|
|
|
|
|
|
|
|
public class HomeController : Controller
|
|
|
|
{
|
|
|
|
private readonly ILogger<HomeController> _logger;
|
|
|
|
private readonly WebSocketManagerService _webSocketManager;
|
|
|
|
private readonly HttpClientService _httpClientService;
|
|
|
|
|
|
|
|
public HomeController(ILogger<HomeController> logger, WebSocketManagerService webSocketManager, HttpClientService httpClientService)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_webSocketManager = webSocketManager;
|
|
|
|
_httpClientService = httpClientService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IActionResult Index()
|
|
|
|
{
|
|
|
|
_logger.LogInformation("访问首页");
|
|
|
|
// 获取连接统计信息
|
|
|
|
var stats = _webSocketManager.GetConnectionStatistics();
|
|
|
|
ViewBag.ConnectionStats = stats;
|
|
|
|
|
|
|
|
// 获取所有客户端配置
|
|
|
|
var configs = _webSocketManager.GetAllClientConfigs();
|
|
|
|
ViewBag.ClientConfigs = configs;
|
|
|
|
|
|
|
|
// 获取所有测试客户端配置和状态
|
|
|
|
var allTestClients = _webSocketManager.GetAllTestClientsWithState();
|
|
|
|
ViewBag.Clients = allTestClients;
|
|
|
|
|
|
|
|
// 处理IP分组数据
|
|
|
|
var ipGroups = ProcessIpGroups(allTestClients);
|
|
|
|
ViewBag.IpGroups = ipGroups;
|
|
|
|
|
|
|
|
// 网络配置将通过JavaScript异步加载,不阻塞页面渲染
|
|
|
|
ViewBag.NetworkConfigs = new List<NetworkConfig>();
|
|
|
|
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取网络配置
|
|
|
|
/// </summary>
|
|
|
|
private async Task<List<NetworkConfig>> GetNetworkConfigs(string ip, string port)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var apiUrl = $"http://{ip}:{port}/api/v1/NetworkConfig";
|
|
|
|
var response = await _httpClientService.GetAsync<NetworkConfigResponse>(apiUrl);
|
|
|
|
|
|
|
|
if (response.IsSuccess)
|
|
|
|
{
|
|
|
|
return response.Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogWarning("获取网络配置失败: {Response}", response);
|
|
|
|
return new List<NetworkConfig>();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "获取网络配置异常: {Ip}:{Port}", ip, port);
|
|
|
|
return new List<NetworkConfig>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理IP分组数据
|
|
|
|
/// </summary>
|
|
|
|
private List<dynamic> ProcessIpGroups(List<dynamic> allTestClients)
|
|
|
|
{
|
|
|
|
var ipGroups = new List<dynamic>();
|
|
|
|
var ipDict = new Dictionary<string, List<dynamic>>();
|
|
|
|
|
|
|
|
// 按IP地址分组
|
|
|
|
foreach (var client in allTestClients)
|
|
|
|
{
|
|
|
|
var address = client.Config.Address;
|
|
|
|
var ip = address.Split(':')[0]; // 根据:分割获取IP
|
|
|
|
|
|
|
|
if (!ipDict.ContainsKey(ip))
|
|
|
|
{
|
|
|
|
ipDict[ip] = new List<dynamic>();
|
|
|
|
}
|
|
|
|
ipDict[ip].Add(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 转换为动态对象列表
|
|
|
|
foreach (var kvp in ipDict)
|
|
|
|
{
|
|
|
|
var ip = kvp.Key;
|
|
|
|
var clients = kvp.Value;
|
|
|
|
|
|
|
|
// 计算该IP下客户端的状态
|
|
|
|
var connectedCount = clients.Count(c => (LTEMvcApp.Models.ClientState)c.State == LTEMvcApp.Models.ClientState.Connected);
|
|
|
|
var totalCount = clients.Count;
|
|
|
|
|
|
|
|
var groupState = connectedCount == 0 ? "停止" :
|
|
|
|
connectedCount == totalCount ? "运行" : "部分运行";
|
|
|
|
|
|
|
|
// 从TestConfigController获取已保存的Key
|
|
|
|
var savedKey = GetSavedIpGroupKey(ip);
|
|
|
|
|
|
|
|
ipGroups.Add(new
|
|
|
|
{
|
|
|
|
Ip = ip,
|
|
|
|
Clients = clients,
|
|
|
|
Port = 11003,
|
|
|
|
ConnectedCount = connectedCount,
|
|
|
|
State = groupState,
|
|
|
|
Key = savedKey
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipGroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取已保存的IP组Key
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ip">IP地址</param>
|
|
|
|
/// <returns>保存的Key值</returns>
|
|
|
|
private string GetSavedIpGroupKey(string ip)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 通过反射获取IpGroupController中的静态字典
|
|
|
|
var ipGroupControllerType = typeof(IpGroupController);
|
|
|
|
var ipGroupKeysField = ipGroupControllerType.GetField("_ipGroupKeys",
|
|
|
|
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
|
|
|
|
|
|
|
|
if (ipGroupKeysField != null)
|
|
|
|
{
|
|
|
|
var ipGroupKeys = ipGroupKeysField.GetValue(null) as Dictionary<string, string>;
|
|
|
|
if (ipGroupKeys != null && ipGroupKeys.TryGetValue(ip, out var key))
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogWarning(ex, "获取IP组Key失败: {Ip}", ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IActionResult Privacy()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
|
|
|
public IActionResult Error()
|
|
|
|
{
|
|
|
|
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// WebSocket测试页面
|
|
|
|
/// </summary>
|
|
|
|
public IActionResult WebSocketTest()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 添加测试客户端配置
|
|
|
|
/// </summary>
|
|
|
|
[HttpPost]
|
|
|
|
public IActionResult AddTestClient()
|
|
|
|
{
|
|
|
|
_logger.LogInformation("添加测试客户端配置");
|
|
|
|
var config = new ClientConfig
|
|
|
|
{
|
|
|
|
Name = "测试客户端",
|
|
|
|
Address = "192.168.13.12:9001",
|
|
|
|
Ssl = false,
|
|
|
|
Enabled = true,
|
|
|
|
ReconnectDelay = 5000,
|
|
|
|
Password = "test123",
|
|
|
|
Logs = new ClientLogsConfig
|
|
|
|
{
|
|
|
|
Layers = new Dictionary<string, LogLayerConfig>
|
|
|
|
{
|
|
|
|
["PHY"] = new LogLayerConfig { Level = "debug", Filter = "debug", MaxSize = 1000, Payload = false },
|
|
|
|
["RRC"] = new LogLayerConfig { Level = "debug", Filter = "debug", MaxSize = 1000, Payload = false }
|
|
|
|
},
|
|
|
|
Signal = true,
|
|
|
|
Cch = true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var success = _webSocketManager.AddClientConfig(config);
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
TempData["Message"] = "测试客户端配置已添加";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TempData["Error"] = "添加测试客户端配置失败";
|
|
|
|
}
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 启动测试客户端
|
|
|
|
/// </summary>
|
|
|
|
[HttpPost]
|
|
|
|
public IActionResult StartTestClient(string address)
|
|
|
|
{
|
|
|
|
_logger.LogInformation($"启动测试客户端: {address}");
|
|
|
|
var success = _webSocketManager.StartClient(address);
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
TempData["Message"] = "测试客户端已启动";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TempData["Error"] = "启动测试客户端失败";
|
|
|
|
}
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 停止测试客户端
|
|
|
|
/// </summary>
|
|
|
|
[HttpPost]
|
|
|
|
public IActionResult StopTestClient(string address)
|
|
|
|
{
|
|
|
|
_logger.LogInformation($"停止测试客户端: {address}");
|
|
|
|
var success = _webSocketManager.StopClient(address);
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
TempData["Message"] = "测试客户端已停止";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TempData["Error"] = "停止测试客户端失败";
|
|
|
|
}
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 测试客户端配置页面
|
|
|
|
/// </summary>
|
|
|
|
public IActionResult TestClientConfig(string address = null)
|
|
|
|
{
|
|
|
|
ClientConfig testConfig;
|
|
|
|
if (!string.IsNullOrEmpty(address))
|
|
|
|
{
|
|
|
|
testConfig = _webSocketManager.GetTestClientConfigByAddress(address);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
testConfig = _webSocketManager.GetDefaultTestClientConfig();
|
|
|
|
}
|
|
|
|
var allTestConfigs = _webSocketManager.GetAllTestClientConfigs();
|
|
|
|
|
|
|
|
ViewBag.TestConfig = testConfig;
|
|
|
|
ViewBag.AllTestConfigs = allTestConfigs;
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 客户端消息队列页面
|
|
|
|
/// </summary>
|
|
|
|
public IActionResult ClientMessages(string address = null)
|
|
|
|
{
|
|
|
|
ViewBag.Address = address;
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 返回 Logs.cshtml 视图
|
|
|
|
/// </summary>
|
|
|
|
public IActionResult Logs()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IActionResult NetworkConfig()
|
|
|
|
{
|
|
|
|
// 获取所有测试客户端配置和状态
|
|
|
|
var allTestClients = _webSocketManager.GetAllTestClientsWithState();
|
|
|
|
var ipGroups = ProcessIpGroups(allTestClients);
|
|
|
|
ViewBag.IpGroups = ipGroups;
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
}
|