feat: 实现拨动开关的数字孪生
This commit is contained in:
127
server/src/Controllers/SwitchController.cs
Normal file
127
server/src/Controllers/SwitchController.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Peripherals.SwitchClient;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class SwitchController : ControllerBase
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
private readonly Database.UserManager _userManager = new();
|
||||
|
||||
/// <summary>
|
||||
/// 获取示波器实例
|
||||
/// </summary>
|
||||
private SwitchCtrl? GetSwitchCtrl()
|
||||
{
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return null;
|
||||
|
||||
var userRet = _userManager.GetUserByName(userName);
|
||||
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||
return null;
|
||||
|
||||
var user = userRet.Value.Value;
|
||||
if (user.BoardID == Guid.Empty)
|
||||
return null;
|
||||
|
||||
var boardRet = _userManager.GetBoardByID(user.BoardID);
|
||||
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||
return null;
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
return new SwitchCtrl(board.IpAddr, board.Port, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 启用或禁用 Switch 外设
|
||||
/// </summary>
|
||||
/// <param name="enable">是否启用</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("enable")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> SetEnable([FromQuery] bool enable)
|
||||
{
|
||||
var switchCtrl = GetSwitchCtrl();
|
||||
if (switchCtrl == null)
|
||||
return BadRequest("Can't get user or board info");
|
||||
|
||||
var result = await switchCtrl.SetEnable(enable);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error(result.Error, "SetEnable failed");
|
||||
return StatusCode(500, result.Error);
|
||||
}
|
||||
return Ok(result.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 控制指定编号的 Switch 开关
|
||||
/// </summary>
|
||||
/// <param name="num">开关编号</param>
|
||||
/// <param name="onOff">开/关</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("switch")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ArgumentException), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> SetSwitchOnOff([FromQuery] int num, [FromQuery] bool onOff)
|
||||
{
|
||||
if (num <= 0 || num > 6)
|
||||
return BadRequest(new ArgumentException($"Switch num should be 1~5, instead of {num}"));
|
||||
|
||||
var switchCtrl = GetSwitchCtrl();
|
||||
if (switchCtrl == null)
|
||||
return BadRequest("Can't get user or board info");
|
||||
|
||||
var result = await switchCtrl.SetSwitchOnOff(num, onOff);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error(result.Error, $"SetSwitchOnOff({num}, {onOff}) failed");
|
||||
return StatusCode(500, result.Error);
|
||||
}
|
||||
return Ok(result.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 控制 Switch 开关
|
||||
/// </summary>
|
||||
/// <param name="keyStatus">开关状态</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("MultiSwitch")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ArgumentException), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> SetMultiSwitchsOnOff(bool[] keyStatus)
|
||||
{
|
||||
if (keyStatus.Length == 0 || keyStatus.Length > 6) return BadRequest(
|
||||
new ArgumentException($"Switch num should be 1~5, instead of {keyStatus.Length}"));
|
||||
|
||||
var switchCtrl = GetSwitchCtrl();
|
||||
if (switchCtrl == null)
|
||||
return BadRequest("Can't get user or board info");
|
||||
|
||||
for (int i = 0; i < keyStatus.Length; i++)
|
||||
{
|
||||
var result = await switchCtrl.SetSwitchOnOff(i, keyStatus[i]);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error(result.Error, $"SetSwitchOnOff({i}, {keyStatus[i]}) failed");
|
||||
return StatusCode(500, result.Error);
|
||||
}
|
||||
if (!result.Value) return Ok(false);
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
65
server/src/Peripherals/SwitchClient.cs
Normal file
65
server/src/Peripherals/SwitchClient.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Collections;
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
|
||||
namespace Peripherals.SwitchClient;
|
||||
|
||||
class SwitchCtrlAddr
|
||||
{
|
||||
public const UInt32 BASE = 0xB0_00_00_20;
|
||||
|
||||
public const UInt32 ENABLE = BASE;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 矩阵键盘外设类,用于控制和管理矩阵键盘的功能。
|
||||
/// </summary>
|
||||
public class SwitchCtrl
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
readonly int timeout = 500;
|
||||
readonly int taskID;
|
||||
readonly int port;
|
||||
readonly string address;
|
||||
private IPEndPoint ep;
|
||||
|
||||
public SwitchCtrl(string address, int port, int taskID, int timeout = 500)
|
||||
{
|
||||
if (timeout < 0)
|
||||
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
|
||||
this.taskID = taskID;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public async ValueTask<Result<bool>> SetEnable(bool enable)
|
||||
{
|
||||
if (MsgBus.IsRunning)
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
else return new(new Exception("Message Bus not work!"));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, this.taskID, SwitchCtrlAddr.ENABLE, enable ? 0x1U : 0x0U, this.timeout);
|
||||
if (!ret.IsSuccessful) return new(ret.Error);
|
||||
return ret.Value;
|
||||
}
|
||||
|
||||
public async ValueTask<Result<bool>> SetSwitchOnOff(int num, bool onOff)
|
||||
{
|
||||
if (MsgBus.IsRunning)
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
else return new(new Exception("Message Bus not work!"));
|
||||
|
||||
var ret = await UDPClientPool.WriteAddr(
|
||||
this.ep, this.taskID, SwitchCtrlAddr.BASE + (UInt32)num, onOff ? 0x1U : 0x0U, this.timeout);
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Set Switch {onOff} failed: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
return ret.Value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user