feat: 完成logicanalyzer的api
This commit is contained in:
358
server/src/Controllers/LogicAnalyzerController.cs
Normal file
358
server/src/Controllers/LogicAnalyzerController.cs
Normal file
@@ -0,0 +1,358 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Peripherals.LogicAnalyzerClient;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑分析仪控制器
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
public class LogicAnalyzerController : ControllerBase
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 信号触发配置
|
||||
/// </summary>
|
||||
public class SignalTriggerConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 信号索引 (0-7)
|
||||
/// </summary>
|
||||
public int SignalIndex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 操作符
|
||||
/// </summary>
|
||||
public SignalOperator Operator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 信号值
|
||||
/// </summary>
|
||||
public SignalValue Value { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 捕获配置
|
||||
/// </summary>
|
||||
public class CaptureConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局触发模式
|
||||
/// </summary>
|
||||
public GlobalCaptureMode GlobalMode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 信号触发配置列表
|
||||
/// </summary>
|
||||
public SignalTriggerConfig[] SignalConfigs { get; set; } = Array.Empty<SignalTriggerConfig>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取逻辑分析仪实例
|
||||
/// </summary>
|
||||
private Analyzer? GetAnalyzer()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return null;
|
||||
|
||||
using var db = new Database.AppDataConnection();
|
||||
var userRet = db.GetUserByName(userName);
|
||||
if (!userRet.IsSuccessful || !userRet.Value.HasValue)
|
||||
return null;
|
||||
|
||||
var user = userRet.Value.Value;
|
||||
if (user.BoardID == Guid.Empty)
|
||||
return null;
|
||||
|
||||
var boardRet = db.GetBoardByID(user.BoardID);
|
||||
if (!boardRet.IsSuccessful || !boardRet.Value.HasValue)
|
||||
return null;
|
||||
|
||||
var board = boardRet.Value.Value;
|
||||
return new Analyzer(board.IpAddr, board.Port, 2);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取逻辑分析仪实例时发生异常");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置捕获模式
|
||||
/// </summary>
|
||||
/// <param name="captureOn">是否开始捕获</param>
|
||||
/// <param name="force">是否强制捕获</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetCaptureMode")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> SetCaptureMode(bool captureOn, bool force = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
var analyzer = GetAnalyzer();
|
||||
if (analyzer == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await analyzer.SetCaptureMode(captureOn, force);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置捕获模式失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置捕获模式失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置捕获模式时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取捕获状态
|
||||
/// </summary>
|
||||
/// <returns>捕获状态</returns>
|
||||
[HttpGet("GetCaptureStatus")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(CaptureStatus), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> GetCaptureStatus()
|
||||
{
|
||||
try
|
||||
{
|
||||
var analyzer = GetAnalyzer();
|
||||
if (analyzer == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await analyzer.ReadCaptureStatus();
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"读取捕获状态失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "读取捕获状态失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "读取捕获状态时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置全局触发模式
|
||||
/// </summary>
|
||||
/// <param name="mode">全局触发模式</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetGlobalTrigMode")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> SetGlobalTrigMode(GlobalCaptureMode mode)
|
||||
{
|
||||
try
|
||||
{
|
||||
var analyzer = GetAnalyzer();
|
||||
if (analyzer == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await analyzer.SetGlobalTrigMode(mode);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置全局触发模式失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置全局触发模式失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置全局触发模式时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置信号触发模式
|
||||
/// </summary>
|
||||
/// <param name="signalIndex">信号索引 (0-7)</param>
|
||||
/// <param name="op">操作符</param>
|
||||
/// <param name="val">信号值</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetSignalTrigMode")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> SetSignalTrigMode(int signalIndex, SignalOperator op, SignalValue val)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (signalIndex < 0 || signalIndex > 7)
|
||||
return BadRequest("信号索引必须在0-7之间");
|
||||
|
||||
var analyzer = GetAnalyzer();
|
||||
if (analyzer == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await analyzer.SetSignalTrigMode(signalIndex, op, val);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置信号触发模式失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置信号触发模式失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置信号触发模式时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量配置捕获参数
|
||||
/// </summary>
|
||||
/// <param name="config">捕获配置</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("ConfigureCapture")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> ConfigureCapture([FromBody] CaptureConfig config)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (config == null)
|
||||
return BadRequest("配置参数不能为空");
|
||||
|
||||
var analyzer = GetAnalyzer();
|
||||
if (analyzer == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
// 设置全局触发模式
|
||||
var globalResult = await analyzer.SetGlobalTrigMode(config.GlobalMode);
|
||||
if (!globalResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置全局触发模式失败: {globalResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置全局触发模式失败");
|
||||
}
|
||||
|
||||
// 设置信号触发模式
|
||||
foreach (var signalConfig in config.SignalConfigs)
|
||||
{
|
||||
if (signalConfig.SignalIndex < 0 || signalConfig.SignalIndex > 7)
|
||||
return BadRequest($"信号索引{signalConfig.SignalIndex}超出范围0-7");
|
||||
|
||||
var signalResult = await analyzer.SetSignalTrigMode(
|
||||
signalConfig.SignalIndex, signalConfig.Operator, signalConfig.Value);
|
||||
if (!signalResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置信号{signalConfig.SignalIndex}触发模式失败: {signalResult.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError,
|
||||
$"设置信号{signalConfig.SignalIndex}触发模式失败");
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "配置捕获参数时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 强制捕获
|
||||
/// </summary>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("ForceCapture")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> ForceCapture()
|
||||
{
|
||||
try
|
||||
{
|
||||
var analyzer = GetAnalyzer();
|
||||
if (analyzer == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await analyzer.SetCaptureMode(true, true);
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"强制捕获失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "强制捕获失败");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "强制捕获时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取捕获数据
|
||||
/// </summary>
|
||||
/// <returns>捕获的波形数据(Base64编码)</returns>
|
||||
[HttpGet("GetCaptureData")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async Task<IActionResult> GetCaptureData()
|
||||
{
|
||||
try
|
||||
{
|
||||
var analyzer = GetAnalyzer();
|
||||
if (analyzer == null)
|
||||
return BadRequest("用户未绑定有效的实验板");
|
||||
|
||||
var result = await analyzer.ReadCaptureData();
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"读取捕获数据失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "读取捕获数据失败");
|
||||
}
|
||||
|
||||
// 将二进制数据编码为Base64字符串返回
|
||||
var base64Data = Convert.ToBase64String(result.Value);
|
||||
return Ok(base64Data);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "读取捕获数据时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "操作失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user