You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
7.4 KiB
204 lines
7.4 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using LTEMvcApp.Models;
|
|
using LTEMvcApp.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LTEMvcApp.Controllers
|
|
{
|
|
/// <summary>
|
|
/// IP组管理控制器 - 负责IP组网络启动、停止和Key管理
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class IpGroupController : ControllerBase
|
|
{
|
|
private readonly HttpClientService _httpClientService;
|
|
private readonly ILogger<IpGroupController> _logger;
|
|
|
|
public IpGroupController(HttpClientService httpClientService, ILogger<IpGroupController> logger)
|
|
{
|
|
_httpClientService = httpClientService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动IP组网络
|
|
/// </summary>
|
|
/// <param name="request">启动网络请求</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("start")]
|
|
public async Task<ActionResult> StartIpGroup([FromBody] StartIpGroupRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Ip))
|
|
return BadRequest("IP地址不能为空");
|
|
|
|
if (string.IsNullOrEmpty(request.Port))
|
|
return BadRequest("端口不能为空");
|
|
|
|
try
|
|
{
|
|
var apiUrl = $"http://{request.Ip}:{request.Port}/api/v1/CellularNetwork/start";
|
|
var command = new StartCellularNetworkCommand { Key = request.Key };
|
|
|
|
_logger.LogInformation("启动IP组网络: {Ip}:{Port}, Key: {Key}", request.Ip, request.Port, request.Key);
|
|
|
|
var response = await _httpClientService.PostJsonAsync(apiUrl, command);
|
|
|
|
_logger.LogInformation("IP组网络启动成功: {Response}", response);
|
|
return Ok(new { message = "网络启动成功", response });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "启动IP组网络失败: {Ip}:{Port}", request.Ip, request.Port);
|
|
return BadRequest($"启动网络失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止IP组网络
|
|
/// </summary>
|
|
/// <param name="request">停止网络请求</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("stop")]
|
|
public async Task<ActionResult> StopIpGroup([FromBody] StopIpGroupRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Ip))
|
|
return BadRequest("IP地址不能为空");
|
|
|
|
if (string.IsNullOrEmpty(request.Port))
|
|
return BadRequest("端口不能为空");
|
|
|
|
try
|
|
{
|
|
var apiUrl = $"http://{request.Ip}:{request.Port}/api/v1/CellularNetwork/stop";
|
|
var command = new StopCellularNetworkCommand { Key = request.Key };
|
|
|
|
_logger.LogInformation("停止IP组网络: {Ip}:{Port}, Key: {Key}", request.Ip, request.Port, request.Key);
|
|
|
|
var response = await _httpClientService.PostJsonAsync(apiUrl, command);
|
|
|
|
_logger.LogInformation("IP组网络停止成功: {Response}", response);
|
|
return Ok(new { message = "网络停止成功", response });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "停止IP组网络失败: {Ip}:{Port}", request.Ip, request.Port);
|
|
return BadRequest($"停止网络失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取网络配置列表
|
|
/// </summary>
|
|
/// <param name="ip">IP地址</param>
|
|
/// <param name="port">端口</param>
|
|
/// <returns>网络配置列表</returns>
|
|
[HttpGet("network-config")]
|
|
public async Task<ActionResult> GetNetworkConfigs([FromQuery] string ip, [FromQuery] string port)
|
|
{
|
|
try
|
|
{
|
|
var apiUrl = $"http://{ip}:{port}/api/v1/NetworkConfig";
|
|
_logger.LogInformation("获取网络配置: {ApiUrl}", apiUrl);
|
|
|
|
var response = await _httpClientService.GetAsync<NetworkConfigResponse>(apiUrl);
|
|
|
|
_logger.LogInformation("网络配置获取成功");
|
|
return Ok(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取网络配置失败: {Ip}:{Port}", ip, port);
|
|
return BadRequest($"获取网络配置失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加网络配置(转发)
|
|
/// </summary>
|
|
[HttpPost("network-config")]
|
|
public async Task<ActionResult> AddNetworkConfig([FromQuery] string ip, [FromQuery] string port, [FromBody] NetworkConfiguration config)
|
|
{
|
|
if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))
|
|
return BadRequest("ip和port不能为空");
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
try
|
|
{
|
|
var apiUrl = $"http://{ip}:{port}/api/v1/NetworkConfig";
|
|
var response = await _httpClientService.PostJsonAsync(apiUrl, config);
|
|
return Content(response, "application/json");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "添加网络配置失败: {Ip}:{Port}", ip, port);
|
|
return BadRequest($"添加网络配置失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除网络配置(转发)
|
|
/// </summary>
|
|
[HttpDelete("network-config/{configKey}")]
|
|
public async Task<ActionResult> DeleteNetworkConfig(string configKey, [FromQuery] string ip, [FromQuery] string port)
|
|
{
|
|
if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))
|
|
return BadRequest("ip和port不能为空");
|
|
try
|
|
{
|
|
var apiUrl = $"http://{ip}:{port}/api/v1/NetworkConfig/{configKey}";
|
|
var response = await _httpClientService.DeleteAsync(apiUrl);
|
|
return Content(response, "application/json");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "删除网络配置失败: {Ip}:{Port}", ip, port);
|
|
return BadRequest($"删除网络配置失败: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动IP组请求
|
|
/// </summary>
|
|
public class StartIpGroupRequest
|
|
{
|
|
/// <summary>
|
|
/// IP地址
|
|
/// </summary>
|
|
public string Ip { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 端口
|
|
/// </summary>
|
|
public string Port { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Key值
|
|
/// </summary>
|
|
public string Key { get; set; } = string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止IP组请求
|
|
/// </summary>
|
|
public class StopIpGroupRequest
|
|
{
|
|
/// <summary>
|
|
/// IP地址
|
|
/// </summary>
|
|
public string Ip { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 端口
|
|
/// </summary>
|
|
public string Port { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Key值
|
|
/// </summary>
|
|
public string Key { get; set; } = string.Empty;
|
|
}
|
|
}
|