feat: 完成实验板动态ip与动态mac
This commit is contained in:
280
server/src/Controllers/NetConfigController.cs
Normal file
280
server/src/Controllers/NetConfigController.cs
Normal file
@@ -0,0 +1,280 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Peripherals.NetConfigClient;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 网络配置控制器(仅管理员权限)
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize("Admin")]
|
||||
public class NetConfigController : ControllerBase
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 设置主机IP地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="hostIp">主机IP地址</param>
|
||||
/// <param name="taskId">任务ID,默认为0</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetHostIP")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetHostIP(Guid boardId, string hostIp)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(hostIp))
|
||||
return BadRequest("主机IP地址不能为空");
|
||||
|
||||
if (!IPAddress.TryParse(hostIp, out var ipAddress))
|
||||
return BadRequest("IP地址格式不正确");
|
||||
|
||||
try
|
||||
{
|
||||
// 获取板卡信息
|
||||
using var db = new Database.AppDataConnection();
|
||||
var boardRet = db.GetBoardByID(boardId);
|
||||
if (!boardRet.IsSuccessful)
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
if (!boardRet.Value.HasValue)
|
||||
return NotFound("未找到对应的板卡");
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(board.IPAddr, board.Port, 0);
|
||||
var result = await netConfig.SetHostIP(ipAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置主机IP失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置主机IP时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置板卡IP地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="taskId">任务ID,默认为0</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetBoardIP")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetBoardIP(Guid boardId, string boardIp)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(boardIp))
|
||||
return BadRequest("板卡IP地址不能为空");
|
||||
|
||||
if (!IPAddress.TryParse(boardIp, out var ipAddress))
|
||||
return BadRequest("IP地址格式不正确");
|
||||
|
||||
try
|
||||
{
|
||||
// 获取板卡信息
|
||||
using var db = new Database.AppDataConnection();
|
||||
var boardRet = db.GetBoardByID(boardId);
|
||||
if (!boardRet.IsSuccessful)
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
if (!boardRet.Value.HasValue)
|
||||
return NotFound("未找到对应的板卡");
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(board.IPAddr, board.Port, 0);
|
||||
var result = await netConfig.SetBoardIP(ipAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置板卡IP失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置板卡IP时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置主机MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="hostMac">主机MAC地址(格式:AA:BB:CC:DD:EE:FF)</param>
|
||||
/// <param name="taskId">任务ID,默认为0</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetHostMAC")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetHostMAC(Guid boardId, string hostMac)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(hostMac))
|
||||
return BadRequest("主机MAC地址不能为空");
|
||||
|
||||
// 解析MAC地址
|
||||
if (!TryParseMacAddress(hostMac, out var macBytes))
|
||||
return BadRequest("MAC地址格式不正确,请使用格式:AA:BB:CC:DD:EE:FF");
|
||||
|
||||
try
|
||||
{
|
||||
// 获取板卡信息
|
||||
using var db = new Database.AppDataConnection();
|
||||
var boardRet = db.GetBoardByID(boardId);
|
||||
if (!boardRet.IsSuccessful)
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
if (!boardRet.Value.HasValue)
|
||||
return NotFound("未找到对应的板卡");
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(board.IPAddr, board.Port, 0);
|
||||
var result = await netConfig.SetHostMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置主机MAC地址失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置主机MAC地址时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置板卡MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="boardMac">板卡MAC地址(格式:AA:BB:CC:DD:EE:FF)</param>
|
||||
/// <param name="taskId">任务ID,默认为0</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetBoardMAC")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetBoardMAC(Guid boardId, string boardMac)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(boardMac))
|
||||
return BadRequest("板卡MAC地址不能为空");
|
||||
|
||||
// 解析MAC地址
|
||||
if (!TryParseMacAddress(boardMac, out var macBytes))
|
||||
return BadRequest("MAC地址格式不正确,请使用格式:AA:BB:CC:DD:EE:FF");
|
||||
|
||||
try
|
||||
{
|
||||
// 获取板卡信息
|
||||
using var db = new Database.AppDataConnection();
|
||||
var boardRet = db.GetBoardByID(boardId);
|
||||
if (!boardRet.IsSuccessful)
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
|
||||
if (!boardRet.Value.HasValue)
|
||||
return NotFound("未找到对应的板卡");
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(board.IPAddr, board.Port, 0);
|
||||
var result = await netConfig.SetBoardMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置板卡MAC地址失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置板卡MAC地址时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析MAC地址字符串为字节数组
|
||||
/// </summary>
|
||||
/// <param name="macAddress">MAC地址字符串</param>
|
||||
/// <param name="macBytes">解析后的字节数组</param>
|
||||
/// <returns>是否解析成功</returns>
|
||||
private static bool TryParseMacAddress(string macAddress, out byte[] macBytes)
|
||||
{
|
||||
macBytes = Array.Empty<byte>();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(macAddress))
|
||||
return false;
|
||||
|
||||
// 移除可能的分隔符并统一为冒号
|
||||
var cleanMac = macAddress.Replace("-", ":").Replace(" ", "").ToUpper();
|
||||
|
||||
// 验证格式
|
||||
if (cleanMac.Length != 17 || cleanMac.Count(c => c == ':') != 5)
|
||||
return false;
|
||||
|
||||
var parts = cleanMac.Split(':');
|
||||
if (parts.Length != 6)
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
macBytes = new byte[6];
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
macBytes[i] = Convert.ToByte(parts[i], 16);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
macBytes = Array.Empty<byte>();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user