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(); /// /// 获取示波器实例 /// 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); } /// /// 启用或禁用 Switch 外设 /// /// 是否启用 /// 操作结果 [HttpPost("enable")] [EnableCors("Users")] [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)] [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task 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); } /// /// 控制指定编号的 Switch 开关 /// /// 开关编号 /// 开/关 /// 操作结果 [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 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); } /// /// 控制 Switch 开关 /// /// 开关状态 /// 操作结果 [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 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 + 1, 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); } }