Browse Source

dd

feature/MultiClientLog
root 1 month ago
parent
commit
ed133ea14d
  1. 44
      LTEMvcApp/Controllers/ConfigController.cs
  2. 26
      LTEMvcApp/Services/WebSocketManagerService.cs
  3. 71
      LTEMvcApp/Views/Statistics/Config.cshtml

44
LTEMvcApp/Controllers/ConfigController.cs

@ -35,19 +35,19 @@ namespace LTEMvcApp.Controllers
/// <summary> /// <summary>
/// 获取指定客户端的统计配置 /// 获取指定客户端的统计配置
/// </summary> /// </summary>
/// <param name="clientName">客户端名称</param> /// <param name="ipAddress">客户端IP地址</param>
/// <returns>统计配置</returns> /// <returns>统计配置</returns>
[HttpGet("statistics/{clientName}")] [HttpGet("statistics/{ipAddress}")]
public ActionResult<StatisticsConfig?> GetClientStatisticsConfig(string clientName) public ActionResult<StatisticsConfig?> GetClientStatisticsConfig(string ipAddress)
{ {
try try
{ {
var config = _webSocketManager.GetClientStatisticsConfig(clientName); var config = _webSocketManager.GetClientStatisticsConfig(ipAddress);
return Ok(new { success = true, data = config }); return Ok(new { success = true, data = config });
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "获取客户端统计配置时出错: {ClientName}", clientName); _logger.LogError(ex, "获取客户端统计配置时出错: {IpAddress}", ipAddress);
return BadRequest(new { success = false, message = ex.Message }); return BadRequest(new { success = false, message = ex.Message });
} }
} }
@ -55,21 +55,21 @@ namespace LTEMvcApp.Controllers
/// <summary> /// <summary>
/// 设置指定客户端的统计配置 /// 设置指定客户端的统计配置
/// </summary> /// </summary>
/// <param name="clientName">客户端名称</param> /// <param name="ipAddress">客户端IP地址</param>
/// <param name="config">统计配置</param> /// <param name="config">统计配置</param>
/// <returns>操作结果</returns> /// <returns>操作结果</returns>
[HttpPost("statistics/{clientName}")] [HttpPost("statistics/{ipAddress}")]
public ActionResult SetClientStatisticsConfig(string clientName, [FromBody] StatisticsConfig config) public ActionResult SetClientStatisticsConfig(string ipAddress, [FromBody] StatisticsConfig config)
{ {
try try
{ {
config.ClientName = clientName; // 确保客户端名称正确 config.IpAddress = ipAddress; // 确保IP地址正确
_webSocketManager.SetClientStatisticsConfig(config); _webSocketManager.SetClientStatisticsConfig(config);
return Ok(new { success = true, message = "客户端统计配置已更新" }); return Ok(new { success = true, message = "客户端统计配置已更新" });
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "设置客户端统计配置时出错: {ClientName}", clientName); _logger.LogError(ex, "设置客户端统计配置时出错: {IpAddress}", ipAddress);
return BadRequest(new { success = false, message = ex.Message }); return BadRequest(new { success = false, message = ex.Message });
} }
} }
@ -77,21 +77,21 @@ namespace LTEMvcApp.Controllers
/// <summary> /// <summary>
/// 更新客户端的Samples参数 /// 更新客户端的Samples参数
/// </summary> /// </summary>
/// <param name="clientName">客户端名称</param> /// <param name="ipAddress">客户端IP地址</param>
/// <param name="enableSamples">是否启用Samples</param> /// <param name="enableSamples">是否启用Samples</param>
/// <returns>操作结果</returns> /// <returns>操作结果</returns>
[HttpPost("statistics/{clientName}/samples")] [HttpPost("statistics/{ipAddress}/samples")]
public ActionResult UpdateClientSamples(string clientName, [FromBody] bool enableSamples) public ActionResult UpdateClientSamples(string ipAddress, [FromBody] bool enableSamples)
{ {
try try
{ {
var config = _webSocketManager.GetClientStatisticsConfig(clientName); var config = _webSocketManager.GetClientStatisticsConfig(ipAddress);
if (config == null) if (config == null)
{ {
// 创建新配置 // 创建新配置
config = new StatisticsConfig config = new StatisticsConfig
{ {
ClientName = clientName, IpAddress = ipAddress,
EnableSamples = enableSamples, EnableSamples = enableSamples,
EnableRf = false, EnableRf = false,
IsEnabled = true IsEnabled = true
@ -107,7 +107,7 @@ namespace LTEMvcApp.Controllers
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "更新客户端Samples参数时出错: {ClientName}", clientName); _logger.LogError(ex, "更新客户端Samples参数时出错: {IpAddress}", ipAddress);
return BadRequest(new { success = false, message = ex.Message }); return BadRequest(new { success = false, message = ex.Message });
} }
} }
@ -115,21 +115,21 @@ namespace LTEMvcApp.Controllers
/// <summary> /// <summary>
/// 更新客户端的RF参数 /// 更新客户端的RF参数
/// </summary> /// </summary>
/// <param name="clientName">客户端名称</param> /// <param name="ipAddress">客户端IP地址</param>
/// <param name="enableRf">是否启用RF</param> /// <param name="enableRf">是否启用RF</param>
/// <returns>操作结果</returns> /// <returns>操作结果</returns>
[HttpPost("statistics/{clientName}/rf")] [HttpPost("statistics/{ipAddress}/rf")]
public ActionResult UpdateClientRf(string clientName, [FromBody] bool enableRf) public ActionResult UpdateClientRf(string ipAddress, [FromBody] bool enableRf)
{ {
try try
{ {
var config = _webSocketManager.GetClientStatisticsConfig(clientName); var config = _webSocketManager.GetClientStatisticsConfig(ipAddress);
if (config == null) if (config == null)
{ {
// 创建新配置 // 创建新配置
config = new StatisticsConfig config = new StatisticsConfig
{ {
ClientName = clientName, IpAddress = ipAddress,
EnableSamples = false, EnableSamples = false,
EnableRf = enableRf, EnableRf = enableRf,
IsEnabled = true IsEnabled = true
@ -145,7 +145,7 @@ namespace LTEMvcApp.Controllers
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "更新客户端RF参数时出错: {ClientName}", clientName); _logger.LogError(ex, "更新客户端RF参数时出错: {IpAddress}", ipAddress);
return BadRequest(new { success = false, message = ex.Message }); return BadRequest(new { success = false, message = ex.Message });
} }
} }

26
LTEMvcApp/Services/WebSocketManagerService.cs

@ -215,8 +215,8 @@ namespace LTEMvcApp.Services
client.Start(); client.Start();
_clients[address] = client; _clients[address] = client;
// 应用统计配置 // 应用统计配置 - 使用IP地址
UpdateClientStatisticsConfig(config.Name); UpdateClientStatisticsConfig(address);
return true; return true;
} }
@ -852,11 +852,11 @@ namespace LTEMvcApp.Services
/// <summary> /// <summary>
/// 获取指定客户端的统计配置 /// 获取指定客户端的统计配置
/// </summary> /// </summary>
/// <param name="clientName">客户端名称</param> /// <param name="ipAddress">客户端IP地址</param>
/// <returns>统计配置</returns> /// <returns>统计配置</returns>
public StatisticsConfig? GetClientStatisticsConfig(string clientName) public StatisticsConfig? GetClientStatisticsConfig(string ipAddress)
{ {
return _globalStatisticsConfig.ClientConfigs.FirstOrDefault(c => c.ClientName == clientName); return _globalStatisticsConfig.ClientConfigs.FirstOrDefault(c => c.IpAddress == ipAddress);
} }
/// <summary> /// <summary>
@ -865,7 +865,7 @@ namespace LTEMvcApp.Services
/// <param name="config">统计配置</param> /// <param name="config">统计配置</param>
public void SetClientStatisticsConfig(StatisticsConfig config) public void SetClientStatisticsConfig(StatisticsConfig config)
{ {
var existingConfig = _globalStatisticsConfig.ClientConfigs.FirstOrDefault(c => c.ClientName == config.ClientName); var existingConfig = _globalStatisticsConfig.ClientConfigs.FirstOrDefault(c => c.IpAddress == config.IpAddress);
if (existingConfig != null) if (existingConfig != null)
{ {
_globalStatisticsConfig.ClientConfigs.Remove(existingConfig); _globalStatisticsConfig.ClientConfigs.Remove(existingConfig);
@ -873,8 +873,8 @@ namespace LTEMvcApp.Services
_globalStatisticsConfig.ClientConfigs.Add(config); _globalStatisticsConfig.ClientConfigs.Add(config);
SaveStatisticsConfig(); SaveStatisticsConfig();
// 更新指定客户端的统计配置 // 更新指定客户端的统计配置 - 使用IP地址作为键
UpdateClientStatisticsConfig(config.ClientName); UpdateClientStatisticsConfig(config.IpAddress);
} }
/// <summary> /// <summary>
@ -894,19 +894,19 @@ namespace LTEMvcApp.Services
{ {
foreach (var client in _clients.Values) foreach (var client in _clients.Values)
{ {
UpdateClientStatisticsConfig(client.Config.Name); UpdateClientStatisticsConfig(client.Config.Address);
} }
} }
/// <summary> /// <summary>
/// 更新指定客户端的统计配置 /// 更新指定客户端的统计配置
/// </summary> /// </summary>
/// <param name="clientName">客户端名称</param> /// <param name="ipAddress">客户端IP地址</param>
private void UpdateClientStatisticsConfig(string clientName) private void UpdateClientStatisticsConfig(string ipAddress)
{ {
if (_clients.TryGetValue(clientName, out var client)) if (_clients.TryGetValue(ipAddress, out var client))
{ {
var config = GetClientStatisticsConfig(clientName); var config = GetStatisticsConfigByIp(ipAddress);
if (config != null && config.IsEnabled) if (config != null && config.IsEnabled)
{ {
client.SetStatisticsConfig(config.EnableSamples, config.EnableRf); client.SetStatisticsConfig(config.EnableSamples, config.EnableRf);

71
LTEMvcApp/Views/Statistics/Config.cshtml

@ -322,25 +322,25 @@
<td><code>${client.address}</code></td> <td><code>${client.address}</code></td>
<td> <td>
<div class="form-check d-flex justify-content-center"> <div class="form-check d-flex justify-content-center">
<input class="form-check-input" type="checkbox" id="samples_${client.name}" <input class="form-check-input" type="checkbox" id="samples_${client.address}"
onchange="updateConfig('${client.name}', 'samples', this.checked)"> onchange="updateConfig('${client.address}', 'samples', this.checked)">
</div> </div>
</td> </td>
<td> <td>
<div class="form-check d-flex justify-content-center"> <div class="form-check d-flex justify-content-center">
<input class="form-check-input" type="checkbox" id="rf_${client.name}" <input class="form-check-input" type="checkbox" id="rf_${client.address}"
onchange="updateConfig('${client.name}', 'rf', this.checked)"> onchange="updateConfig('${client.address}', 'rf', this.checked)">
</div> </div>
</td> </td>
<td> <td>
<div class="form-check d-flex justify-content-center"> <div class="form-check d-flex justify-content-center">
<input class="form-check-input" type="checkbox" id="enabled_${client.name}" checked <input class="form-check-input" type="checkbox" id="enabled_${client.address}" checked
onchange="updateConfig('${client.name}', 'enabled', this.checked)"> onchange="updateConfig('${client.address}', 'enabled', this.checked)">
</div> </div>
</td> </td>
<td> <td>
<div class="btn-group btn-group-sm"> <div class="btn-group btn-group-sm">
<button class="btn btn-success" onclick="saveClientConfig('${client.name}')" title="保存"> <button class="btn btn-success" onclick="saveClientConfig('${client.address}')" title="保存">
<i class="fas fa-save"></i> <i class="fas fa-save"></i>
</button> </button>
</div> </div>
@ -365,19 +365,19 @@
} }
// 更新配置(实时保存到服务器) // 更新配置(实时保存到服务器)
function updateConfig(clientName, field, value) { function updateConfig(ipAddress, field, value) {
console.log(`更新配置: ${clientName} - ${field} = ${value}`); console.log(`更新配置: ${ipAddress} - ${field} = ${value}`);
if (field === 'samples') { if (field === 'samples') {
// 更新Samples参数 // 更新Samples参数
$.ajax({ $.ajax({
url: `/api/config/statistics/${encodeURIComponent(clientName)}/samples`, url: `/api/config/statistics/${encodeURIComponent(ipAddress)}/samples`,
type: 'POST', type: 'POST',
contentType: 'application/json', contentType: 'application/json',
data: JSON.stringify(value), data: JSON.stringify(value),
success: function(response) { success: function(response) {
if (response.success) { if (response.success) {
console.log(`客户端 ${clientName} Samples参数已更新`); console.log(`客户端 ${ipAddress} Samples参数已更新`);
} else { } else {
console.error('更新Samples参数失败:', response.message); console.error('更新Samples参数失败:', response.message);
showAlert('danger', `更新Samples参数失败: ${response.message}`); showAlert('danger', `更新Samples参数失败: ${response.message}`);
@ -391,13 +391,13 @@
} else if (field === 'rf') { } else if (field === 'rf') {
// 更新RF参数 // 更新RF参数
$.ajax({ $.ajax({
url: `/api/config/statistics/${encodeURIComponent(clientName)}/rf`, url: `/api/config/statistics/${encodeURIComponent(ipAddress)}/rf`,
type: 'POST', type: 'POST',
contentType: 'application/json', contentType: 'application/json',
data: JSON.stringify(value), data: JSON.stringify(value),
success: function(response) { success: function(response) {
if (response.success) { if (response.success) {
console.log(`客户端 ${clientName} RF参数已更新`); console.log(`客户端 ${ipAddress} RF参数已更新`);
} else { } else {
console.error('更新RF参数失败:', response.message); console.error('更新RF参数失败:', response.message);
showAlert('danger', `更新RF参数失败: ${response.message}`); showAlert('danger', `更新RF参数失败: ${response.message}`);
@ -410,29 +410,28 @@
}); });
} else if (field === 'enabled') { } else if (field === 'enabled') {
// 更新启用状态 // 更新启用状态
saveClientConfig(clientName); saveClientConfig(ipAddress);
} }
} }
// 保存客户端配置 // 保存客户端配置
function saveClientConfig(clientName) { function saveClientConfig(ipAddress) {
const config = { const config = {
clientName: clientName, ipAddress: ipAddress,
ipAddress: '', // 将从客户端列表获取 enableSamples: document.getElementById(`samples_${ipAddress}`).checked,
enableSamples: document.getElementById(`samples_${clientName}`).checked, enableRf: document.getElementById(`rf_${ipAddress}`).checked,
enableRf: document.getElementById(`rf_${clientName}`).checked, isEnabled: document.getElementById(`enabled_${ipAddress}`).checked
isEnabled: document.getElementById(`enabled_${clientName}`).checked
}; };
// 从已加载的客户端列表中获取IP地址 // 从已加载的客户端列表中获取客户端名称
const tbody = document.getElementById('clientConfigTableBody'); const tbody = document.getElementById('clientConfigTableBody');
const rows = tbody.getElementsByTagName('tr'); const rows = tbody.getElementsByTagName('tr');
for (let row of rows) { for (let row of rows) {
const nameCell = row.querySelector('td:first-child strong'); const ipCell = row.querySelector('td:nth-child(2) code');
if (nameCell && nameCell.textContent === clientName) { if (ipCell && ipCell.textContent === ipAddress) {
const ipCell = row.querySelector('td:nth-child(2) code'); const nameCell = row.querySelector('td:first-child strong');
if (ipCell) { if (nameCell) {
config.ipAddress = ipCell.textContent; config.clientName = nameCell.textContent;
} }
break; break;
} }
@ -440,14 +439,14 @@
console.log('保存客户端统计配置:', config); console.log('保存客户端统计配置:', config);
$.ajax({ $.ajax({
url: `/api/config/statistics/${encodeURIComponent(clientName)}`, url: `/api/config/statistics/${encodeURIComponent(ipAddress)}`,
type: 'POST', type: 'POST',
contentType: 'application/json', contentType: 'application/json',
data: JSON.stringify(config), data: JSON.stringify(config),
success: function(response) { success: function(response) {
console.log('客户端配置保存响应:', response); console.log('客户端配置保存响应:', response);
if (response.success) { if (response.success) {
showAlert('success', `客户端 ${clientName} 配置已保存`); showAlert('success', `客户端 ${ipAddress} 配置已保存`);
} else { } else {
showAlert('danger', `保存失败: ${response.message}`); showAlert('danger', `保存失败: ${response.message}`);
} }
@ -470,7 +469,7 @@
// 将配置转换为字典,方便查找 // 将配置转换为字典,方便查找
configs.forEach(function(config) { configs.forEach(function(config) {
configDict[config.clientName] = config; configDict[config.ipAddress] = config;
}); });
// 更新表格中的配置 // 更新表格中的配置
@ -478,15 +477,15 @@
const rows = tbody.getElementsByTagName('tr'); const rows = tbody.getElementsByTagName('tr');
for (let row of rows) { for (let row of rows) {
const nameCell = row.querySelector('td:first-child strong'); const ipCell = row.querySelector('td:nth-child(2) code');
if (nameCell) { if (ipCell) {
const clientName = nameCell.textContent; const ipAddress = ipCell.textContent;
const config = configDict[clientName]; const config = configDict[ipAddress];
if (config) { if (config) {
row.querySelector(`#samples_${clientName}`).checked = config.enableSamples; row.querySelector(`#samples_${ipAddress}`).checked = config.enableSamples;
row.querySelector(`#rf_${clientName}`).checked = config.enableRf; row.querySelector(`#rf_${ipAddress}`).checked = config.enableRf;
row.querySelector(`#enabled_${clientName}`).checked = config.isEnabled; row.querySelector(`#enabled_${ipAddress}`).checked = config.isEnabled;
} }
} }
} }

Loading…
Cancel
Save