refactor:修改api获取ip的方法
This commit is contained in:
parent
53eaac43e3
commit
2ff735e06a
|
@ -19,41 +19,37 @@ public class NetConfigController : ControllerBase
|
|||
/// <summary>
|
||||
/// 设置主机IP地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="hostIp">主机IP地址</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)
|
||||
public async Task<IActionResult> SetHostIP(string boardIp, int boardPort, string hostIp)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
if (string.IsNullOrWhiteSpace(boardIp))
|
||||
return BadRequest("板卡IP地址不能为空");
|
||||
|
||||
if (boardPort <= 0 || boardPort > 65535)
|
||||
return BadRequest("板卡端口号无效");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(hostIp))
|
||||
return BadRequest("主机IP地址不能为空");
|
||||
|
||||
if (!IPAddress.TryParse(hostIp, out var ipAddress))
|
||||
return BadRequest("IP地址格式不正确");
|
||||
if (!IPAddress.TryParse(boardIp, out _))
|
||||
return BadRequest("板卡IP地址格式不正确");
|
||||
|
||||
if (!IPAddress.TryParse(hostIp, out var hostIpAddress))
|
||||
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);
|
||||
var netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var result = await netConfig.SetHostIP(hostIpAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
|
@ -73,41 +69,37 @@ public class NetConfigController : ControllerBase
|
|||
/// <summary>
|
||||
/// 设置板卡IP地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="currentBoardIp">当前板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="newBoardIp">新的板卡IP地址</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)
|
||||
public async Task<IActionResult> SetBoardIP(string currentBoardIp, int boardPort, string newBoardIp)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
if (string.IsNullOrWhiteSpace(currentBoardIp))
|
||||
return BadRequest("当前板卡IP地址不能为空");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(boardIp))
|
||||
return BadRequest("板卡IP地址不能为空");
|
||||
if (boardPort <= 0 || boardPort > 65535)
|
||||
return BadRequest("板卡端口号无效");
|
||||
|
||||
if (!IPAddress.TryParse(boardIp, out var ipAddress))
|
||||
return BadRequest("IP地址格式不正确");
|
||||
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
|
||||
{
|
||||
// 获取板卡信息
|
||||
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);
|
||||
var netConfig = new NetConfig(currentBoardIp, boardPort, 0);
|
||||
var result = await netConfig.SetBoardIP(newIpAddress);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
|
@ -115,13 +107,6 @@ public class NetConfigController : ControllerBase
|
|||
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||
}
|
||||
|
||||
var ret = db.UpdateBoardIpAddr(boardId, ipAddress.ToString());
|
||||
if (ret <= 0)
|
||||
{
|
||||
logger.Error($"数据库更新失败");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"数据库更新失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -134,41 +119,37 @@ public class NetConfigController : ControllerBase
|
|||
/// <summary>
|
||||
/// 设置主机MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="hostMac">主机MAC地址(格式:AA:BB:CC:DD:EE:FF)</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)
|
||||
public async Task<IActionResult> SetHostMAC(string boardIp, int boardPort, string hostMac)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
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");
|
||||
|
||||
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 netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var result = await netConfig.SetHostMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
|
@ -189,18 +170,24 @@ public class NetConfigController : ControllerBase
|
|||
/// <summary>
|
||||
/// 更新主机MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <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.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> UpdateHostMAC(Guid boardId)
|
||||
public async Task<IActionResult> UpdateHostMAC(string boardIp, int boardPort)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
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
|
||||
|
@ -216,18 +203,8 @@ public class NetConfigController : ControllerBase
|
|||
if (macBytes == null)
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "无法获取本机MAC地址");
|
||||
|
||||
// 获取板卡信息
|
||||
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 netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var result = await netConfig.SetHostMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
|
@ -248,41 +225,37 @@ public class NetConfigController : ControllerBase
|
|||
/// <summary>
|
||||
/// 设置板卡MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardId">板卡ID</param>
|
||||
/// <param name="boardIp">板卡IP地址</param>
|
||||
/// <param name="boardPort">板卡端口</param>
|
||||
/// <param name="boardMac">板卡MAC地址(格式:AA:BB:CC:DD:EE:FF)</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)
|
||||
public async Task<IActionResult> SetBoardMAC(string boardIp, int boardPort, string boardMac)
|
||||
{
|
||||
if (boardId == Guid.Empty)
|
||||
return BadRequest("板卡ID不能为空");
|
||||
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");
|
||||
|
||||
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 netConfig = new NetConfig(boardIp, boardPort, 0);
|
||||
var result = await netConfig.SetBoardMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
|
|
|
@ -111,8 +111,8 @@ const boardManager = useBoardManager()!;
|
|||
|
||||
// 表单数据
|
||||
const form = reactive({
|
||||
name: '',
|
||||
ipAddr: '',
|
||||
name: 'Board1',
|
||||
ipAddr: '169.254.103.0',
|
||||
port: 1234
|
||||
});
|
||||
|
||||
|
@ -176,8 +176,8 @@ function validateForm(): boolean {
|
|||
|
||||
// 重置表单
|
||||
function resetForm() {
|
||||
form.name = '';
|
||||
form.ipAddr = '';
|
||||
form.name = 'Board1';
|
||||
form.ipAddr = '169.254.103.0';
|
||||
form.port = 1234;
|
||||
errors.name = '';
|
||||
errors.ipAddr = '';
|
||||
|
|
Loading…
Reference in New Issue