feat: 使用静态arp处理通信

This commit is contained in:
2025-07-18 13:16:07 +08:00
parent e9ad1f0256
commit 9af2d3e87e
6 changed files with 372 additions and 75 deletions

View File

@@ -207,7 +207,7 @@ public class DataController : ControllerBase
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetAvailableBoard(int durationHours = 1)
public async ValueTask<IActionResult> GetAvailableBoard(int durationHours = 1)
{
try
{
@@ -227,7 +227,13 @@ public class DataController : ControllerBase
if (!boardOpt.HasValue)
return NotFound("没有可用的实验板");
return Ok(boardOpt.Value);
var boardInfo = boardOpt.Value;
if (!(await Arp.CheckOrAddAsync(boardInfo.IpAddr, boardInfo.MacAddr)))
{
logger.Error($"无法配置ARP实验板可能会无法连接");
}
return Ok(boardInfo);
}
catch (Exception ex)
{
@@ -278,7 +284,7 @@ public class DataController : ControllerBase
[ProducesResponseType(typeof(Database.Board), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetBoardByID(Guid id)
public async Task<IActionResult> GetBoardByID(Guid id)
{
try
{
@@ -288,7 +294,14 @@ public class DataController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, "数据库操作失败");
if (!ret.Value.HasValue)
return NotFound("未找到对应的实验板");
return Ok(ret.Value.Value);
var boardInfo = ret.Value.Value;
if (!(await Arp.CheckOrAddAsync(boardInfo.IpAddr, boardInfo.MacAddr)))
{
logger.Error($"无法配置ARP实验板可能会无法连接");
}
return Ok(boardInfo);
}
catch (Exception ex)
{
@@ -372,5 +385,59 @@ public class DataController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, "获取失败,请稍后重试");
}
}
/// <summary>
/// 更新板卡名称(管理员权限)
/// </summary>
[Authorize("Admin")]
[HttpPost("UpdateBoardName")]
[EnableCors("Users")]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult UpdateBoardName(Guid boardId, string newName)
{
if (boardId == Guid.Empty)
return BadRequest("板子Guid不能为空");
if (string.IsNullOrWhiteSpace(newName))
return BadRequest("新名称不能为空");
try
{
using var db = new Database.AppDataConnection();
var result = db.UpdateBoardName(boardId, newName);
return Ok(result);
}
catch (Exception ex)
{
logger.Error(ex, "更新板卡名称时发生异常");
return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试");
}
}
/// <summary>
/// 更新板卡状态(管理员权限)
/// </summary>
[Authorize("Admin")]
[HttpPost("UpdateBoardStatus")]
[EnableCors("Users")]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult UpdateBoardStatus(Guid boardId, Database.Board.BoardStatus newStatus)
{
if (boardId == Guid.Empty)
return BadRequest("板子Guid不能为空");
try
{
using var db = new Database.AppDataConnection();
var result = db.UpdateBoardStatus(boardId, newStatus);
return Ok(result);
}
catch (Exception ex)
{
logger.Error(ex, "更新板卡状态时发生异常");
return StatusCode(StatusCodes.Status500InternalServerError, "更新失败,请稍后重试");
}
}
}

View File

@@ -33,6 +33,10 @@ public class NetConfigController : ControllerBase
try
{
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.GetHostIP();
if (!result.IsSuccessful)
@@ -63,6 +67,10 @@ public class NetConfigController : ControllerBase
try
{
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.GetBoardIP();
if (!result.IsSuccessful)
@@ -93,6 +101,10 @@ public class NetConfigController : ControllerBase
try
{
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.GetHostMAC();
if (!result.IsSuccessful)
@@ -123,6 +135,10 @@ public class NetConfigController : ControllerBase
try
{
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.GetBoardMAC();
if (!result.IsSuccessful)
@@ -153,6 +169,10 @@ public class NetConfigController : ControllerBase
try
{
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var hostIPResult = await netConfig.GetHostIP();
var boardIPResult = await netConfig.GetBoardIP();
@@ -237,6 +257,10 @@ public class NetConfigController : ControllerBase
{
// 创建网络配置客户端
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.SetHostIP(hostIpAddress);
if (!result.IsSuccessful)
@@ -276,6 +300,10 @@ public class NetConfigController : ControllerBase
{
// 创建网络配置客户端
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.SetBoardIP(newIpAddress);
if (!result.IsSuccessful)
@@ -316,6 +344,10 @@ public class NetConfigController : ControllerBase
{
// 创建网络配置客户端
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.SetHostMAC(macBytes);
if (!result.IsSuccessful)
@@ -367,6 +399,10 @@ public class NetConfigController : ControllerBase
return StatusCode(StatusCodes.Status500InternalServerError, "无法获取本机IP地址");
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.SetHostIP(ip);
if (!result.IsSuccessful)
@@ -411,6 +447,10 @@ public class NetConfigController : ControllerBase
// 创建网络配置客户端
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.SetHostMAC(macBytes);
if (!result.IsSuccessful)
@@ -451,6 +491,10 @@ public class NetConfigController : ControllerBase
{
// 创建网络配置客户端
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
if (!(await netConfig.Init()))
{
throw new Exception($"无法配置ARP");
}
var result = await netConfig.SetBoardMAC(macBytes);
if (!result.IsSuccessful)