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

26
LTEMvcApp/Services/WebSocketManagerService.cs

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

71
LTEMvcApp/Views/Statistics/Config.cshtml

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

Loading…
Cancel
Save