feat: 更新网络配置后端及其api

This commit is contained in:
2025-07-17 20:46:37 +08:00
parent 08a9be543e
commit 0f4386457d
6 changed files with 1158 additions and 108 deletions

View File

@@ -92,11 +92,17 @@ public class Board
[NotNull]
public required string IpAddr { get; set; }
/// <summary>
/// FPGA 板子的MAC地址
/// </summary>
[NotNull]
public required string MacAddr { get; set; }
/// <summary>
/// FPGA 板子的通信端口
/// </summary>
[NotNull]
public required int Port { get; set; }
public int Port { get; set; } = 1234;
/// <summary>
/// FPGA 板子的当前状态
@@ -127,6 +133,11 @@ public class Board
/// </summary>
public enum BoardStatus
{
/// <summary>
/// 未启用状态,无法被使用
/// </summary>
Disabled,
/// <summary>
/// 繁忙状态,正在被用户使用
/// </summary>
@@ -371,25 +382,61 @@ public class AppDataConnection : DataConnection
return userResult + boardResult;
}
/// <summary>
/// 自动分配一个未被占用的IP地址
/// </summary>
/// <returns>分配的IP地址字符串</returns>
public string AllocateIpAddr()
{
var usedIps = this.BoardTable.Select(b => b.IpAddr).ToArray();
for (int i = 1; i <= 254; i++)
{
string ip = $"169.254.109.{i}";
if (!usedIps.Contains(ip))
return ip;
}
throw new Exception("没有可用的IP地址");
}
/// <summary>
/// 自动分配一个未被占用的MAC地址
/// </summary>
/// <returns>分配的MAC地址字符串</returns>
public string AllocateMacAddr()
{
var usedMacs = this.BoardTable.Select(b => b.MacAddr).ToArray();
// 以 02-00-00-xx-xx-xx 格式分配02 表示本地管理地址
for (int i = 1; i <= 0xFFFFFF; i++)
{
string mac = $"02-00-00-{(i >> 16) & 0xFF:X2}-{(i >> 8) & 0xFF:X2}-{i & 0xFF:X2}";
if (!usedMacs.Contains(mac))
return mac;
}
throw new Exception("没有可用的MAC地址");
}
/// <summary>
/// 添加一块新的 FPGA 板子到数据库
/// </summary>
/// <param name="name">FPGA 板子的名称</param>
/// <param name="ipAddr">FPGA 板子的IP地址</param>
/// <param name="port">FPGA 板子的通信端口</param>
/// <returns>插入的记录数</returns>
public int AddBoard(string name, string ipAddr, int port)
public Guid AddBoard(string name)
{
if (string.IsNullOrWhiteSpace(name) || name.Contains('\'') || name.Contains(';'))
{
logger.Error("实验板名称非法,包含不允许的字符");
throw new ArgumentException("实验板名称非法");
}
var board = new Board()
{
BoardName = name,
IpAddr = ipAddr,
Port = port,
Status = Database.Board.BoardStatus.Available,
IpAddr = AllocateIpAddr(),
MacAddr = AllocateMacAddr(),
Status = Database.Board.BoardStatus.Disabled,
};
var result = this.Insert(board);
logger.Info($"新实验板已添加: {name} ({ipAddr}:{port})");
return result;
logger.Info($"新实验板已添加: {name} ({board.IpAddr}:{board.MacAddr})");
return board.ID;
}
/// <summary>
@@ -543,18 +590,39 @@ public class AppDataConnection : DataConnection
}
/// <summary>
/// 更新实验板的IP地址
/// [TODO:description]
/// </summary>
/// <param name="boardId">实验板的唯一标识符</param>
/// <param name="newIpAddr">新的IP地址</param>
/// <returns>更新的记录数</returns>
public int UpdateBoardIpAddr(Guid boardId, string newIpAddr)
/// <param name="boardId">[TODO:parameter]</param>
/// <param name="newName">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public int UpdateBoardName(Guid boardId, string newName)
{
if (string.IsNullOrWhiteSpace(newName) || newName.Contains('\'') || newName.Contains(';'))
{
logger.Error("实验板名称非法,包含不允许的字符");
return 0;
}
var result = this.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.BoardName, newName)
.Update();
logger.Info($"实验板名称已更新: {boardId} -> {newName}");
return result;
}
/// <summary>
/// [TODO:description]
/// </summary>
/// <param name="boardId">[TODO:parameter]</param>
/// <param name="newStatus">[TODO:parameter]</param>
/// <returns>[TODO:return]</returns>
public int UpdateBoardStatus(Guid boardId, Board.BoardStatus newStatus)
{
var result = this.BoardTable
.Where(b => b.ID == boardId)
.Set(b => b.IpAddr, newIpAddr)
.Set(b => b.Status, newStatus)
.Update();
logger.Info($"实验板 {boardId} 的IP地址已更新为 {newIpAddr},更新记录数: {result}");
logger.Info($"TODO");
return result;
}