refactor: 修改webapi,修改实验板网络设置时不再需要实验板ip与端口
This commit is contained in:
@@ -15,12 +15,14 @@ namespace server.Controllers;
|
||||
public class NetConfigController : ControllerBase
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
// 固定的实验板IP和端口
|
||||
private const string BOARD_IP = "169.254.109.0";
|
||||
private const int BOARD_PORT = 1234;
|
||||
|
||||
/// <summary>
|
||||
/// 设置主机IP地址
|
||||
/// </summary>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="hostIp">主机IP地址</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetHostIP")]
|
||||
@@ -28,27 +30,18 @@ public class NetConfigController : ControllerBase
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetHostIP(string boardIp, int boardPort, string hostIp)
|
||||
public async Task<IActionResult> SetHostIP(string hostIp)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(boardIp))
|
||||
return BadRequest("板卡IP地址不能为空");
|
||||
|
||||
if (boardPort <= 0 || boardPort > 65535)
|
||||
return BadRequest("板卡端口号无效");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(hostIp))
|
||||
return BadRequest("主机IP地址不能为空");
|
||||
|
||||
if (!IPAddress.TryParse(boardIp, out _))
|
||||
return BadRequest("板卡IP地址格式不正确");
|
||||
|
||||
if (!IPAddress.TryParse(hostIp, out var hostIpAddress))
|
||||
return BadRequest("主机IP地址格式不正确");
|
||||
|
||||
try
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
var result = await netConfig.SetHostIP(hostIpAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -69,8 +62,6 @@ public class NetConfigController : ControllerBase
|
||||
/// <summary>
|
||||
/// 设置板卡IP地址
|
||||
/// </summary>
|
||||
/// <param name="currentBoardIp">当前板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="newBoardIp">新的板卡IP地址</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetBoardIP")]
|
||||
@@ -78,27 +69,18 @@ public class NetConfigController : ControllerBase
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetBoardIP(string currentBoardIp, int boardPort, string newBoardIp)
|
||||
public async Task<IActionResult> SetBoardIP(string newBoardIp)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(currentBoardIp))
|
||||
return BadRequest("当前板卡IP地址不能为空");
|
||||
|
||||
if (boardPort <= 0 || boardPort > 65535)
|
||||
return BadRequest("板卡端口号无效");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(newBoardIp))
|
||||
return BadRequest("新的板卡IP地址不能为空");
|
||||
|
||||
if (!IPAddress.TryParse(currentBoardIp, out _))
|
||||
return BadRequest("当前板卡IP地址格式不正确");
|
||||
|
||||
if (!IPAddress.TryParse(newBoardIp, out var newIpAddress))
|
||||
return BadRequest("新的板卡IP地址格式不正确");
|
||||
|
||||
try
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(currentBoardIp, boardPort, 0);
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
var result = await netConfig.SetBoardIP(newIpAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -119,8 +101,6 @@ public class NetConfigController : ControllerBase
|
||||
/// <summary>
|
||||
/// 设置主机MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="hostMac">主机MAC地址(格式:AA:BB:CC:DD:EE:FF)</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetHostMAC")]
|
||||
@@ -128,20 +108,11 @@ public class NetConfigController : ControllerBase
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetHostMAC(string boardIp, int boardPort, string hostMac)
|
||||
public async Task<IActionResult> SetHostMAC(string hostMac)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(boardIp))
|
||||
return BadRequest("板卡IP地址不能为空");
|
||||
|
||||
if (boardPort <= 0 || boardPort > 65535)
|
||||
return BadRequest("板卡端口号无效");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(hostMac))
|
||||
return BadRequest("主机MAC地址不能为空");
|
||||
|
||||
if (!IPAddress.TryParse(boardIp, out _))
|
||||
return BadRequest("板卡IP地址格式不正确");
|
||||
|
||||
// 解析MAC地址
|
||||
if (!TryParseMacAddress(hostMac, out var macBytes))
|
||||
return BadRequest("MAC地址格式不正确,请使用格式:AA:BB:CC:DD:EE:FF");
|
||||
@@ -149,7 +120,7 @@ public class NetConfigController : ControllerBase
|
||||
try
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
var result = await netConfig.SetHostMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -170,25 +141,14 @@ public class NetConfigController : ControllerBase
|
||||
/// <summary>
|
||||
/// 更新主机MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("UpdateHostMAC")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> UpdateHostMAC(string boardIp, int boardPort)
|
||||
public async Task<IActionResult> UpdateHostMAC()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(boardIp))
|
||||
return BadRequest("板卡IP地址不能为空");
|
||||
|
||||
if (boardPort <= 0 || boardPort > 65535)
|
||||
return BadRequest("板卡端口号无效");
|
||||
|
||||
if (!IPAddress.TryParse(boardIp, out _))
|
||||
return BadRequest("板卡IP地址格式不正确");
|
||||
|
||||
byte[]? macBytes = null;
|
||||
try
|
||||
{
|
||||
@@ -204,7 +164,7 @@ public class NetConfigController : ControllerBase
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "无法获取本机MAC地址");
|
||||
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
var result = await netConfig.SetHostMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -225,8 +185,6 @@ public class NetConfigController : ControllerBase
|
||||
/// <summary>
|
||||
/// 设置板卡MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="boardMac">板卡MAC地址(格式:AA:BB:CC:DD:EE:FF)</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetBoardMAC")]
|
||||
@@ -234,20 +192,11 @@ public class NetConfigController : ControllerBase
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetBoardMAC(string boardIp, int boardPort, string boardMac)
|
||||
public async Task<IActionResult> SetBoardMAC(string boardMac)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(boardIp))
|
||||
return BadRequest("板卡IP地址不能为空");
|
||||
|
||||
if (boardPort <= 0 || boardPort > 65535)
|
||||
return BadRequest("板卡端口号无效");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(boardMac))
|
||||
return BadRequest("板卡MAC地址不能为空");
|
||||
|
||||
if (!IPAddress.TryParse(boardIp, out _))
|
||||
return BadRequest("板卡IP地址格式不正确");
|
||||
|
||||
// 解析MAC地址
|
||||
if (!TryParseMacAddress(boardMac, out var macBytes))
|
||||
return BadRequest("MAC地址格式不正确,请使用格式:AA:BB:CC:DD:EE:FF");
|
||||
@@ -255,7 +204,7 @@ public class NetConfigController : ControllerBase
|
||||
try
|
||||
{
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
var result = await netConfig.SetBoardMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
|
Reference in New Issue
Block a user