feat: 配置板子网络时,更新动态mac

This commit is contained in:
2025-07-31 16:33:19 +08:00
parent bafd06162c
commit 2adeca3b99
7 changed files with 46 additions and 37 deletions

View File

@@ -19,8 +19,7 @@ public class NetConfigController : ControllerBase
// 固定的实验板IP,端口,MAC地址
private const string BOARD_IP = "169.254.109.0";
private const int BOARD_PORT = 1234;
private const string BOARD_MAC = "12:34:56:78:9a:bc";
// 本机网络信息
private readonly IPAddress _localIP;
private readonly byte[] _localMAC;
@@ -33,14 +32,14 @@ public class NetConfigController : ControllerBase
// 初始化本机IP地址
_localIP = GetLocalIPAddress();
_localIPString = _localIP?.ToString() ?? "未知";
// 初始化本机MAC地址
_localMAC = GetLocalMACAddress();
_localMACString = _localMAC != null ? BitConverter.ToString(_localMAC).Replace("-", ":") : "未知";
// 获取本机网络接口名称
_localInterface = GetLocalNetworkInterface();
logger.Info($"NetConfigController 初始化完成 - 本机IP: {_localIPString}, 本机MAC: {_localMACString}, 接口: {_localInterface}");
}
@@ -53,7 +52,7 @@ public class NetConfigController : ControllerBase
try
{
var boardIpSegments = BOARD_IP.Split('.').Take(3).ToArray();
// 优先选择与实验板IP前三段相同的IP
var sameSegmentIP = System.Net.NetworkInformation.NetworkInterface
.GetAllNetworkInterfaces()
@@ -130,7 +129,7 @@ public class NetConfigController : ControllerBase
{
try
{
return await Arp.CheckOrAddAsync(BOARD_IP, BOARD_MAC, _localInterface);
return await ArpClient.UpdateArpEntryAsync(BOARD_IP);
}
catch (Exception ex)
{