Compare commits
6 Commits
e5f2be616c
...
dpp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51b39cee07 | ||
|
|
0bd1ad8a0e | ||
|
|
f2c7c78b64 | ||
|
|
2f23ffe482 | ||
|
|
9904fecbee | ||
| cb229c2a30 |
@@ -303,8 +303,11 @@ async function generateApiClient(): Promise<void> {
|
||||
async function generateSignalRClient(): Promise<void> {
|
||||
console.log("Generating SignalR TypeScript client...");
|
||||
try {
|
||||
// TypedSignalR.Client.TypeScript.Analyzer 会在编译时自动生成客户端
|
||||
// 我们只需要确保服务器项目构建一次即可生成 TypeScript 客户端
|
||||
const { stdout, stderr } = await execAsync(
|
||||
"dotnet tsrts --project ./server/server.csproj --output ./src",
|
||||
"dotnet build --configuration Release",
|
||||
{ cwd: "./server" }
|
||||
);
|
||||
if (stdout) console.log(stdout);
|
||||
if (stderr) console.error(stderr);
|
||||
|
||||
@@ -95,6 +95,12 @@ try
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
);
|
||||
options.AddPolicy("SignalR", policy => policy
|
||||
.WithOrigins("http://localhost:5173")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials()
|
||||
);
|
||||
});
|
||||
|
||||
// Use SignalR
|
||||
@@ -201,12 +207,15 @@ try
|
||||
};
|
||||
});
|
||||
app.UseSwaggerUi();
|
||||
|
||||
// SignalR
|
||||
app.UseWebSockets();
|
||||
app.UseSignalRHubSpecification();
|
||||
app.UseSignalRHubDevelopmentUI();
|
||||
|
||||
// Router
|
||||
app.MapControllers();
|
||||
app.MapHub<server.Hubs.JtagHub.JtagHub>("hubs/JtagHub").RequireCors("Users");
|
||||
app.MapHub<server.Hubs.JtagHub.JtagHub>("hubs/JtagHub");
|
||||
|
||||
// Setup Program
|
||||
MsgBus.Init();
|
||||
|
||||
@@ -101,22 +101,6 @@ public class ExamController : ControllerBase
|
||||
public bool IsVisibleToUsers { get; set; } = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源信息类
|
||||
/// </summary>
|
||||
public class ResourceInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源ID
|
||||
/// </summary>
|
||||
public int ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源名称
|
||||
/// </summary>
|
||||
public required string Name { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建实验请求类
|
||||
/// </summary>
|
||||
@@ -304,151 +288,4 @@ public class ExamController : ControllerBase
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"创建实验失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加实验资源
|
||||
/// </summary>
|
||||
/// <param name="examId">实验ID</param>
|
||||
/// <param name="resourceType">资源类型</param>
|
||||
/// <param name="file">资源文件</param>
|
||||
/// <returns>添加结果</returns>
|
||||
[Authorize("Admin")]
|
||||
[HttpPost("{examId}/resources/{resourceType}")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(ResourceInfo), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> AddExamResource(string examId, string resourceType, IFormFile file)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(examId) || string.IsNullOrWhiteSpace(resourceType) || file == null)
|
||||
return BadRequest("实验ID、资源类型和文件不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
|
||||
// 读取文件数据
|
||||
using var memoryStream = new MemoryStream();
|
||||
await file.CopyToAsync(memoryStream);
|
||||
var fileData = memoryStream.ToArray();
|
||||
|
||||
var result = db.AddExamResource(examId, resourceType, file.FileName, fileData);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
if (result.Error.Message.Contains("不存在"))
|
||||
return NotFound(result.Error.Message);
|
||||
if (result.Error.Message.Contains("已存在"))
|
||||
return Conflict(result.Error.Message);
|
||||
|
||||
logger.Error($"添加实验资源时出错: {result.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"添加实验资源失败: {result.Error.Message}");
|
||||
}
|
||||
|
||||
var resource = result.Value;
|
||||
var resourceInfo = new ResourceInfo
|
||||
{
|
||||
ID = resource.ID,
|
||||
Name = resource.ResourceName
|
||||
};
|
||||
|
||||
logger.Info($"成功添加实验资源: {examId}/{resourceType}/{file.FileName}");
|
||||
return CreatedAtAction(nameof(GetExamResourceById), new { resourceId = resource.ID }, resourceInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"添加实验资源 {examId}/{resourceType}/{file.FileName} 时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"添加实验资源失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定实验ID的指定资源类型的所有资源的ID和名称
|
||||
/// </summary>
|
||||
/// <param name="examId">实验ID</param>
|
||||
/// <param name="resourceType">资源类型</param>
|
||||
/// <returns>资源列表</returns>
|
||||
[Authorize]
|
||||
[HttpGet("{examId}/resources/{resourceType}")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(ResourceInfo[]), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetExamResourceList(string examId, string resourceType)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(examId) || string.IsNullOrWhiteSpace(resourceType))
|
||||
return BadRequest("实验ID和资源类型不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var result = db.GetExamResourceList(examId, resourceType);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取实验资源列表时出错: {result.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验资源列表失败: {result.Error.Message}");
|
||||
}
|
||||
|
||||
var resources = result.Value.Select(r => new ResourceInfo
|
||||
{
|
||||
ID = r.ID,
|
||||
Name = r.Name
|
||||
}).ToArray();
|
||||
|
||||
logger.Info($"成功获取实验资源列表: {examId}/{resourceType},共 {resources.Length} 个资源");
|
||||
return Ok(resources);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取实验资源列表 {examId}/{resourceType} 时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取实验资源列表失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据资源ID下载资源
|
||||
/// </summary>
|
||||
/// <param name="resourceId">资源ID</param>
|
||||
/// <returns>资源文件</returns>
|
||||
[HttpGet("resources/{resourceId}")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetExamResourceById(int resourceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var result = db.GetExamResourceById(resourceId);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取资源时出错: {result.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {result.Error.Message}");
|
||||
}
|
||||
|
||||
if (!result.Value.HasValue)
|
||||
{
|
||||
logger.Warn($"资源不存在: {resourceId}");
|
||||
return NotFound($"资源 {resourceId} 不存在");
|
||||
}
|
||||
|
||||
var resource = result.Value.Value;
|
||||
logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})");
|
||||
return File(resource.Data, resource.MimeType, resource.ResourceName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取资源 {resourceId} 时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Database;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
@@ -14,8 +15,6 @@ public class JtagController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
private const string BITSTREAM_PATH = "bitstream/Jtag";
|
||||
|
||||
/// <summary>
|
||||
/// 控制器首页信息
|
||||
/// </summary>
|
||||
@@ -112,64 +111,12 @@ public class JtagController : ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上传比特流文件到服务器
|
||||
/// </summary>
|
||||
/// <param name="address">目标设备地址</param>
|
||||
/// <param name="file">比特流文件</param>
|
||||
/// <returns>上传结果</returns>
|
||||
[HttpPost("UploadBitstream")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async ValueTask<IResult> UploadBitstream(string address, IFormFile file)
|
||||
{
|
||||
logger.Info($"User {User.Identity?.Name} uploading bitstream for device {address}");
|
||||
|
||||
if (file == null || file.Length == 0)
|
||||
{
|
||||
logger.Warn($"User {User.Identity?.Name} attempted to upload empty file for device {address}");
|
||||
return TypedResults.BadRequest("未选择文件");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 生成安全的文件名(避免路径遍历攻击)
|
||||
var fileName = Path.GetRandomFileName();
|
||||
var uploadsFolder = Path.Combine(Environment.CurrentDirectory, $"{BITSTREAM_PATH}/{address}");
|
||||
|
||||
// 如果存在文件,则删除原文件再上传
|
||||
if (Directory.Exists(uploadsFolder))
|
||||
{
|
||||
Directory.Delete(uploadsFolder, true);
|
||||
logger.Info($"User {User.Identity?.Name} removed existing bitstream folder for device {address}");
|
||||
}
|
||||
Directory.CreateDirectory(uploadsFolder);
|
||||
|
||||
var filePath = Path.Combine(uploadsFolder, fileName);
|
||||
|
||||
using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
logger.Info($"User {User.Identity?.Name} successfully uploaded bitstream for device {address}, file size: {file.Length} bytes");
|
||||
return TypedResults.Ok(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, $"User {User.Identity?.Name} failed to upload bitstream for device {address}");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过 JTAG 下载比特流文件到 FPGA 设备
|
||||
/// </summary>
|
||||
/// <param name="address">JTAG 设备地址</param>
|
||||
/// <param name="port">JTAG 设备端口</param>
|
||||
/// <param name="bitstreamId">比特流ID</param>
|
||||
/// <returns>下载结果</returns>
|
||||
[HttpPost("DownloadBitstream")]
|
||||
[EnableCors("Users")]
|
||||
@@ -177,50 +124,75 @@ public class JtagController : ControllerBase
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
public async ValueTask<IResult> DownloadBitstream(string address, int port)
|
||||
public async ValueTask<IResult> DownloadBitstream(string address, int port, int bitstreamId)
|
||||
{
|
||||
logger.Info($"User {User.Identity?.Name} initiating bitstream download to device {address}:{port}");
|
||||
|
||||
// 检查文件
|
||||
var fileDir = Path.Combine(Environment.CurrentDirectory, $"{BITSTREAM_PATH}/{address}");
|
||||
if (!Directory.Exists(fileDir))
|
||||
{
|
||||
logger.Warn($"User {User.Identity?.Name} attempted to download non-existent bitstream for device {address}");
|
||||
return TypedResults.BadRequest("Empty bitstream, Please upload it first");
|
||||
}
|
||||
logger.Info($"User {User.Identity?.Name} initiating bitstream download to device {address}:{port} using bitstream ID: {bitstreamId}");
|
||||
|
||||
try
|
||||
{
|
||||
// 读取文件
|
||||
var filePath = Directory.GetFiles(fileDir)[0];
|
||||
logger.Info($"User {User.Identity?.Name} reading bitstream file: {filePath}");
|
||||
|
||||
using (var fileStream = System.IO.File.Open(filePath, System.IO.FileMode.Open))
|
||||
// 获取当前用户名
|
||||
var username = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
if (fileStream is null || fileStream.Length <= 0)
|
||||
{
|
||||
logger.Warn($"User {User.Identity?.Name} found invalid bitstream file for device {address}");
|
||||
return TypedResults.BadRequest("Wrong bitstream, Please upload it again");
|
||||
logger.Warn("Anonymous user attempted to download bitstream");
|
||||
return TypedResults.Unauthorized();
|
||||
}
|
||||
|
||||
logger.Info($"User {User.Identity?.Name} processing bitstream file of size: {fileStream.Length} bytes");
|
||||
// 从数据库获取用户信息
|
||||
using var db = new Database.AppDataConnection();
|
||||
var userResult = db.GetUserByName(username);
|
||||
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||
{
|
||||
logger.Error($"User {username} not found in database");
|
||||
return TypedResults.BadRequest("用户不存在");
|
||||
}
|
||||
|
||||
var user = userResult.Value.Value;
|
||||
|
||||
// 从数据库获取比特流
|
||||
var bitstreamResult = db.GetResourceById(bitstreamId);
|
||||
|
||||
if (!bitstreamResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"User {username} failed to get bitstream from database: {bitstreamResult.Error}");
|
||||
return TypedResults.InternalServerError($"数据库查询失败: {bitstreamResult.Error?.Message}");
|
||||
}
|
||||
|
||||
if (!bitstreamResult.Value.HasValue)
|
||||
{
|
||||
logger.Warn($"User {username} attempted to download non-existent bitstream ID: {bitstreamId}");
|
||||
return TypedResults.BadRequest("比特流不存在");
|
||||
}
|
||||
|
||||
var bitstream = bitstreamResult.Value.Value;
|
||||
|
||||
// 处理比特流数据
|
||||
var fileBytes = bitstream.Data;
|
||||
if (fileBytes == null || fileBytes.Length == 0)
|
||||
{
|
||||
logger.Warn($"User {username} found empty bitstream data for ID: {bitstreamId}");
|
||||
return TypedResults.BadRequest("比特流数据为空,请重新上传");
|
||||
}
|
||||
|
||||
logger.Info($"User {username} processing bitstream file of size: {fileBytes.Length} bytes");
|
||||
|
||||
// 定义缓冲区大小: 32KB
|
||||
byte[] buffer = new byte[32 * 1024];
|
||||
byte[] revBuffer = new byte[32 * 1024];
|
||||
long totalBytesRead = 0;
|
||||
long totalBytesProcessed = 0;
|
||||
|
||||
// 使用异步流读取文件
|
||||
using (var memoryStream = new MemoryStream())
|
||||
// 使用内存流处理文件
|
||||
using (var inputStream = new MemoryStream(fileBytes))
|
||||
using (var outputStream = new MemoryStream())
|
||||
{
|
||||
int bytesRead;
|
||||
while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
||||
while ((bytesRead = await inputStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
// 反转 32bits
|
||||
var retBuffer = Common.Number.ReverseBytes(buffer, 4);
|
||||
if (!retBuffer.IsSuccessful)
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} failed to reverse bytes: {retBuffer.Error}");
|
||||
logger.Error($"User {username} failed to reverse bytes: {retBuffer.Error}");
|
||||
return TypedResults.InternalServerError(retBuffer.Error);
|
||||
}
|
||||
revBuffer = retBuffer.Value;
|
||||
@@ -230,34 +202,33 @@ public class JtagController : ControllerBase
|
||||
revBuffer[i] = Common.Number.ReverseBits(revBuffer[i]);
|
||||
}
|
||||
|
||||
await memoryStream.WriteAsync(revBuffer, 0, bytesRead);
|
||||
totalBytesRead += bytesRead;
|
||||
await outputStream.WriteAsync(revBuffer, 0, bytesRead);
|
||||
totalBytesProcessed += bytesRead;
|
||||
}
|
||||
|
||||
// 将所有数据转换为字节数组(注意:如果文件非常大,可能不适合完全加载到内存)
|
||||
var fileBytes = memoryStream.ToArray();
|
||||
logger.Info($"User {User.Identity?.Name} processed {totalBytesRead} bytes for device {address}");
|
||||
// 获取处理后的数据
|
||||
var processedBytes = outputStream.ToArray();
|
||||
logger.Info($"User {username} processed {totalBytesProcessed} bytes for device {address}");
|
||||
|
||||
// 下载比特流
|
||||
var jtagCtrl = new Peripherals.JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.DownloadBitstream(fileBytes);
|
||||
var ret = await jtagCtrl.DownloadBitstream(processedBytes);
|
||||
|
||||
if (ret.IsSuccessful)
|
||||
{
|
||||
logger.Info($"User {User.Identity?.Name} successfully downloaded bitstream to device {address}");
|
||||
logger.Info($"User {username} successfully downloaded bitstream '{bitstream.ResourceName}' to device {address}");
|
||||
return TypedResults.Ok(ret.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error($"User {User.Identity?.Name} failed to download bitstream to device {address}: {ret.Error}");
|
||||
logger.Error($"User {username} failed to download bitstream to device {address}: {ret.Error}");
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, $"User {User.Identity?.Name} encountered exception while downloading bitstream to device {address}");
|
||||
logger.Error(ex, $"User encountered exception while downloading bitstream to device {address}");
|
||||
return TypedResults.InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
377
server/src/Controllers/ResourceController.cs
Normal file
377
server/src/Controllers/ResourceController.cs
Normal file
@@ -0,0 +1,377 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using DotNext;
|
||||
using Database;
|
||||
|
||||
namespace server.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// 资源控制器 - 提供统一的资源管理API
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ResourceController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 资源信息类
|
||||
/// </summary>
|
||||
public class ResourceInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源ID
|
||||
/// </summary>
|
||||
public int ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源名称
|
||||
/// </summary>
|
||||
public required string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源类型
|
||||
/// </summary>
|
||||
public required string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源用途(template/user)
|
||||
/// </summary>
|
||||
public required string Purpose { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上传时间
|
||||
/// </summary>
|
||||
public DateTime UploadTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所属实验ID(可选)
|
||||
/// </summary>
|
||||
public string? ExamID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// MIME类型
|
||||
/// </summary>
|
||||
public string? MimeType { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加资源请求类
|
||||
/// </summary>
|
||||
public class AddResourceRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源类型
|
||||
/// </summary>
|
||||
public required string ResourceType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源用途(template/user)
|
||||
/// </summary>
|
||||
public required string ResourcePurpose { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所属实验ID(可选)
|
||||
/// </summary>
|
||||
public string? ExamID { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加资源(文件上传)
|
||||
/// </summary>
|
||||
/// <param name="request">添加资源请求</param>
|
||||
/// <param name="file">资源文件</param>
|
||||
/// <returns>添加结果</returns>
|
||||
[Authorize]
|
||||
[HttpPost]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(ResourceInfo), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> AddResource([FromForm] AddResourceRequest request, IFormFile file)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.ResourceType) || string.IsNullOrWhiteSpace(request.ResourcePurpose) || file == null)
|
||||
return BadRequest("资源类型、资源用途和文件不能为空");
|
||||
|
||||
// 验证资源用途
|
||||
if (request.ResourcePurpose != Resource.ResourcePurposes.Template && request.ResourcePurpose != Resource.ResourcePurposes.User)
|
||||
return BadRequest($"无效的资源用途: {request.ResourcePurpose}");
|
||||
|
||||
// 模板资源需要管理员权限
|
||||
if (request.ResourcePurpose == Resource.ResourcePurposes.Template && !User.IsInRole("Admin"))
|
||||
return Forbid("只有管理员可以添加模板资源");
|
||||
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
|
||||
// 获取当前用户ID
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return Unauthorized("无法获取用户信息");
|
||||
|
||||
var userResult = db.GetUserByName(userName);
|
||||
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||
return Unauthorized("用户不存在");
|
||||
|
||||
var user = userResult.Value.Value;
|
||||
|
||||
// 读取文件数据
|
||||
using var memoryStream = new MemoryStream();
|
||||
await file.CopyToAsync(memoryStream);
|
||||
var fileData = memoryStream.ToArray();
|
||||
|
||||
var result = db.AddResource(user.ID, request.ResourceType, request.ResourcePurpose, file.FileName, fileData, request.ExamID);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
if (result.Error.Message.Contains("不存在"))
|
||||
return NotFound(result.Error.Message);
|
||||
|
||||
logger.Error($"添加资源时出错: {result.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"添加资源失败: {result.Error.Message}");
|
||||
}
|
||||
|
||||
var resource = result.Value;
|
||||
var resourceInfo = new ResourceInfo
|
||||
{
|
||||
ID = resource.ID,
|
||||
Name = resource.ResourceName,
|
||||
Type = resource.ResourceType,
|
||||
Purpose = resource.ResourcePurpose,
|
||||
UploadTime = resource.UploadTime,
|
||||
ExamID = resource.ExamID,
|
||||
MimeType = resource.MimeType
|
||||
};
|
||||
|
||||
logger.Info($"成功添加资源: {request.ResourceType}/{request.ResourcePurpose}/{file.FileName}");
|
||||
return CreatedAtAction(nameof(GetResourceById), new { resourceId = resource.ID }, resourceInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"添加资源 {request.ResourceType}/{request.ResourcePurpose}/{file.FileName} 时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"添加资源失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源列表
|
||||
/// </summary>
|
||||
/// <param name="examId">实验ID(可选)</param>
|
||||
/// <param name="resourceType">资源类型(可选)</param>
|
||||
/// <param name="resourcePurpose">资源用途(可选)</param>
|
||||
/// <returns>资源列表</returns>
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(ResourceInfo[]), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetResourceList([FromQuery] string? examId = null, [FromQuery] string? resourceType = null, [FromQuery] string? resourcePurpose = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
|
||||
// 获取当前用户ID
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return Unauthorized("无法获取用户信息");
|
||||
|
||||
var userResult = db.GetUserByName(userName);
|
||||
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||
return Unauthorized("用户不存在");
|
||||
|
||||
var user = userResult.Value.Value;
|
||||
|
||||
// 普通用户只能查看自己的资源和模板资源
|
||||
Guid? userId = null;
|
||||
if (!User.IsInRole("Admin"))
|
||||
{
|
||||
// 如果指定了用户资源用途,则只查看自己的资源
|
||||
if (resourcePurpose == Resource.ResourcePurposes.User)
|
||||
{
|
||||
userId = user.ID;
|
||||
}
|
||||
// 如果指定了模板资源用途,则不限制用户ID
|
||||
else if (resourcePurpose == Resource.ResourcePurposes.Template)
|
||||
{
|
||||
userId = null;
|
||||
}
|
||||
// 如果没有指定用途,则查看自己的用户资源和所有模板资源
|
||||
else
|
||||
{
|
||||
// 这种情况下需要分别查询并合并结果
|
||||
var userResourcesResult = db.GetFullResourceList(examId, resourceType, Resource.ResourcePurposes.User, user.ID);
|
||||
var templateResourcesResult = db.GetFullResourceList(examId, resourceType, Resource.ResourcePurposes.Template, null);
|
||||
|
||||
if (!userResourcesResult.IsSuccessful || !templateResourcesResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取资源列表时出错");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "获取资源列表失败");
|
||||
}
|
||||
|
||||
var allResources = userResourcesResult.Value.Concat(templateResourcesResult.Value)
|
||||
.OrderByDescending(r => r.UploadTime);
|
||||
var mergedResourceInfos = allResources.Select(r => new ResourceInfo
|
||||
{
|
||||
ID = r.ID,
|
||||
Name = r.ResourceName,
|
||||
Type = r.ResourceType,
|
||||
Purpose = r.ResourcePurpose,
|
||||
UploadTime = r.UploadTime,
|
||||
ExamID = r.ExamID,
|
||||
MimeType = r.MimeType
|
||||
}).ToArray();
|
||||
|
||||
logger.Info($"成功获取资源列表,共 {mergedResourceInfos.Length} 个资源");
|
||||
return Ok(mergedResourceInfos);
|
||||
}
|
||||
}
|
||||
|
||||
var result = db.GetFullResourceList(examId, resourceType, resourcePurpose, userId);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取资源列表时出错: {result.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源列表失败: {result.Error.Message}");
|
||||
}
|
||||
|
||||
var resources = result.Value.Select(r => new ResourceInfo
|
||||
{
|
||||
ID = r.ID,
|
||||
Name = r.ResourceName,
|
||||
Type = r.ResourceType,
|
||||
Purpose = r.ResourcePurpose,
|
||||
UploadTime = r.UploadTime,
|
||||
ExamID = r.ExamID,
|
||||
MimeType = r.MimeType
|
||||
}).ToArray();
|
||||
|
||||
logger.Info($"成功获取资源列表,共 {resources.Length} 个资源");
|
||||
return Ok(resources);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取资源列表时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源列表失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据资源ID下载资源
|
||||
/// </summary>
|
||||
/// <param name="resourceId">资源ID</param>
|
||||
/// <returns>资源文件</returns>
|
||||
[HttpGet("{resourceId}")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult GetResourceById(int resourceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
var result = db.GetResourceById(resourceId);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取资源时出错: {result.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {result.Error.Message}");
|
||||
}
|
||||
|
||||
if (!result.Value.HasValue)
|
||||
{
|
||||
logger.Warn($"资源不存在: {resourceId}");
|
||||
return NotFound($"资源 {resourceId} 不存在");
|
||||
}
|
||||
|
||||
var resource = result.Value.Value;
|
||||
logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})");
|
||||
return File(resource.Data, resource.MimeType ?? "application/octet-stream", resource.ResourceName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取资源 {resourceId} 时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除资源
|
||||
/// </summary>
|
||||
/// <param name="resourceId">资源ID</param>
|
||||
/// <returns>删除结果</returns>
|
||||
[Authorize]
|
||||
[HttpDelete("{resourceId}")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public IActionResult DeleteResource(int resourceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = new Database.AppDataConnection();
|
||||
|
||||
// 获取当前用户信息
|
||||
var userName = User.Identity?.Name;
|
||||
if (string.IsNullOrEmpty(userName))
|
||||
return Unauthorized("无法获取用户信息");
|
||||
|
||||
var userResult = db.GetUserByName(userName);
|
||||
if (!userResult.IsSuccessful || !userResult.Value.HasValue)
|
||||
return Unauthorized("用户不存在");
|
||||
|
||||
var user = userResult.Value.Value;
|
||||
|
||||
// 先获取资源信息以验证权限
|
||||
var resourceResult = db.GetResourceById(resourceId);
|
||||
if (!resourceResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"获取资源时出错: {resourceResult.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"获取资源失败: {resourceResult.Error.Message}");
|
||||
}
|
||||
|
||||
if (!resourceResult.Value.HasValue)
|
||||
{
|
||||
logger.Warn($"资源不存在: {resourceId}");
|
||||
return NotFound($"资源 {resourceId} 不存在");
|
||||
}
|
||||
|
||||
var resource = resourceResult.Value.Value;
|
||||
|
||||
// 权限检查:管理员可以删除所有资源,普通用户只能删除自己的用户资源
|
||||
if (!User.IsInRole("Admin"))
|
||||
{
|
||||
if (resource.ResourcePurpose == Resource.ResourcePurposes.Template)
|
||||
return Forbid("普通用户不能删除模板资源");
|
||||
|
||||
if (resource.UserID != user.ID)
|
||||
return Forbid("只能删除自己的资源");
|
||||
}
|
||||
|
||||
var deleteResult = db.DeleteResource(resourceId);
|
||||
if (!deleteResult.IsSuccessful)
|
||||
{
|
||||
logger.Error($"删除资源时出错: {deleteResult.Error.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"删除资源失败: {deleteResult.Error.Message}");
|
||||
}
|
||||
|
||||
logger.Info($"成功删除资源: {resourceId} ({resource.ResourceName})");
|
||||
return NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"删除资源 {resourceId} 时出错: {ex.Message}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"删除资源失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,9 +229,9 @@ public class Exam
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 实验资源表(图片等)
|
||||
/// 资源类,统一管理实验资源、用户比特流等各类资源
|
||||
/// </summary>
|
||||
public class ExamResource
|
||||
public class Resource
|
||||
{
|
||||
/// <summary>
|
||||
/// 资源的唯一标识符
|
||||
@@ -240,17 +240,29 @@ public class ExamResource
|
||||
public int ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所属实验ID
|
||||
/// 上传资源的用户ID
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public required string ExamID { get; set; }
|
||||
public required Guid UserID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源类型(images, markdown, bitstream, diagram, project)
|
||||
/// 所属实验ID(可选,如果不属于特定实验则为空)
|
||||
/// </summary>
|
||||
[Nullable]
|
||||
public string? ExamID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源类型(images, markdown, bitstream, diagram, project等)
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public required string ResourceType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源用途:template(模板)或 user(用户上传)
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public required string ResourcePurpose { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源名称(包含文件扩展名)
|
||||
/// </summary>
|
||||
@@ -264,10 +276,10 @@ public class ExamResource
|
||||
public required byte[] Data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 资源创建时间
|
||||
/// 资源创建/上传时间
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public DateTime CreatedTime { get; set; } = DateTime.Now;
|
||||
public DateTime UploadTime { get; set; } = DateTime.Now;
|
||||
|
||||
/// <summary>
|
||||
/// 资源的MIME类型
|
||||
@@ -305,6 +317,22 @@ public class ExamResource
|
||||
/// </summary>
|
||||
public const string Project = "project";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源用途枚举
|
||||
/// </summary>
|
||||
public static class ResourcePurposes
|
||||
{
|
||||
/// <summary>
|
||||
/// 模板资源,通常由管理员上传,供用户参考
|
||||
/// </summary>
|
||||
public const string Template = "template";
|
||||
|
||||
/// <summary>
|
||||
/// 用户上传的资源
|
||||
/// </summary>
|
||||
public const string User = "user";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -355,7 +383,7 @@ public class AppDataConnection : DataConnection
|
||||
this.CreateTable<User>();
|
||||
this.CreateTable<Board>();
|
||||
this.CreateTable<Exam>();
|
||||
this.CreateTable<ExamResource>();
|
||||
this.CreateTable<Resource>();
|
||||
logger.Info("数据库表创建完成");
|
||||
}
|
||||
|
||||
@@ -368,7 +396,7 @@ public class AppDataConnection : DataConnection
|
||||
this.DropTable<User>();
|
||||
this.DropTable<Board>();
|
||||
this.DropTable<Exam>();
|
||||
this.DropTable<ExamResource>();
|
||||
this.DropTable<Resource>();
|
||||
logger.Warn("所有数据库表已删除");
|
||||
}
|
||||
|
||||
@@ -828,9 +856,9 @@ public class AppDataConnection : DataConnection
|
||||
public ITable<Exam> ExamTable => this.GetTable<Exam>();
|
||||
|
||||
/// <summary>
|
||||
/// 实验资源表
|
||||
/// 资源表(统一管理实验资源、用户比特流等)
|
||||
/// </summary>
|
||||
public ITable<ExamResource> ExamResourceTable => this.GetTable<ExamResource>();
|
||||
public ITable<Resource> ResourceTable => this.GetTable<Resource>();
|
||||
|
||||
/// <summary>
|
||||
/// 创建新实验
|
||||
@@ -933,34 +961,44 @@ public class AppDataConnection : DataConnection
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加实验资源
|
||||
/// 添加资源
|
||||
/// </summary>
|
||||
/// <param name="examId">所属实验ID</param>
|
||||
/// <param name="userId">上传用户ID</param>
|
||||
/// <param name="resourceType">资源类型</param>
|
||||
/// <param name="resourcePurpose">资源用途(template 或 user)</param>
|
||||
/// <param name="resourceName">资源名称</param>
|
||||
/// <param name="data">资源二进制数据</param>
|
||||
/// <param name="examId">所属实验ID(可选)</param>
|
||||
/// <param name="mimeType">MIME类型(可选,将根据文件扩展名自动确定)</param>
|
||||
/// <returns>创建的资源</returns>
|
||||
public Result<ExamResource> AddExamResource(string examId, string resourceType, string resourceName, byte[] data, string? mimeType = null)
|
||||
public Result<Resource> AddResource(Guid userId, string resourceType, string resourcePurpose, string resourceName, byte[] data, string? examId = null, string? mimeType = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 验证实验是否存在
|
||||
// 验证用户是否存在
|
||||
var user = this.UserTable.Where(u => u.ID == userId).FirstOrDefault();
|
||||
if (user == null)
|
||||
{
|
||||
logger.Error($"用户不存在: {userId}");
|
||||
return new(new Exception($"用户不存在: {userId}"));
|
||||
}
|
||||
|
||||
// 如果指定了实验ID,验证实验是否存在
|
||||
if (!string.IsNullOrEmpty(examId))
|
||||
{
|
||||
var exam = this.ExamTable.Where(e => e.ID == examId).FirstOrDefault();
|
||||
if (exam == null)
|
||||
{
|
||||
logger.Error($"实验不存在: {examId}");
|
||||
return new(new Exception($"实验不存在: {examId}"));
|
||||
}
|
||||
}
|
||||
|
||||
// 检查资源是否已存在
|
||||
var existingResource = this.ExamResourceTable
|
||||
.Where(r => r.ExamID == examId && r.ResourceType == resourceType && r.ResourceName == resourceName)
|
||||
.FirstOrDefault();
|
||||
if (existingResource != null)
|
||||
// 验证资源用途
|
||||
if (resourcePurpose != Resource.ResourcePurposes.Template && resourcePurpose != Resource.ResourcePurposes.User)
|
||||
{
|
||||
logger.Error($"资源已存在: {examId}/{resourceType}/{resourceName}");
|
||||
return new(new Exception($"资源已存在: {examId}/{resourceType}/{resourceName}"));
|
||||
logger.Error($"无效的资源用途: {resourcePurpose}");
|
||||
return new(new Exception($"无效的资源用途: {resourcePurpose}"));
|
||||
}
|
||||
|
||||
// 如果未指定MIME类型,根据文件扩展名自动确定
|
||||
@@ -970,49 +1008,126 @@ public class AppDataConnection : DataConnection
|
||||
mimeType = GetMimeTypeFromExtension(extension, resourceName);
|
||||
}
|
||||
|
||||
var resource = new ExamResource
|
||||
var resource = new Resource
|
||||
{
|
||||
UserID = userId,
|
||||
ExamID = examId,
|
||||
ResourceType = resourceType,
|
||||
ResourcePurpose = resourcePurpose,
|
||||
ResourceName = resourceName,
|
||||
Data = data,
|
||||
MimeType = mimeType,
|
||||
CreatedTime = DateTime.Now
|
||||
UploadTime = DateTime.Now
|
||||
};
|
||||
|
||||
this.Insert(resource);
|
||||
logger.Info($"新资源已添加: {examId}/{resourceType}/{resourceName} ({data.Length} bytes)");
|
||||
var insertedId = this.InsertWithIdentity(resource);
|
||||
resource.ID = Convert.ToInt32(insertedId);
|
||||
|
||||
logger.Info($"新资源已添加: {userId}/{resourceType}/{resourcePurpose}/{resourceName} ({data.Length} bytes)" +
|
||||
(examId != null ? $" [实验: {examId}]" : "") + $" [ID: {resource.ID}]");
|
||||
return new(resource);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"添加实验资源时出错: {ex.Message}");
|
||||
logger.Error($"添加资源时出错: {ex.Message}");
|
||||
return new(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定实验ID的指定资源类型的所有资源的ID和名称
|
||||
/// </summary>
|
||||
/// <param name="examId">实验ID</param>
|
||||
/// 获取资源信息列表(返回ID和名称)
|
||||
/// <param name="resourceType">资源类型</param>
|
||||
/// <param name="examId">实验ID(可选)</param>
|
||||
/// <param name="resourcePurpose">资源用途(可选)</param>
|
||||
/// <param name="userId">用户ID(可选)</param>
|
||||
/// </summary>
|
||||
/// <returns>资源信息列表</returns>
|
||||
public Result<(int ID, string Name)[]> GetExamResourceList(string examId, string resourceType)
|
||||
public Result<(int ID, string Name)[]> GetResourceList(string resourceType, string? examId = null, string? resourcePurpose = null, Guid? userId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var resources = this.ExamResourceTable
|
||||
.Where(r => r.ExamID == examId && r.ResourceType == resourceType)
|
||||
var query = this.ResourceTable.Where(r => r.ResourceType == resourceType);
|
||||
|
||||
if (examId != null)
|
||||
{
|
||||
query = query.Where(r => r.ExamID == examId);
|
||||
}
|
||||
|
||||
if (resourcePurpose != null)
|
||||
{
|
||||
query = query.Where(r => r.ResourcePurpose == resourcePurpose);
|
||||
}
|
||||
|
||||
if (userId != null)
|
||||
{
|
||||
query = query.Where(r => r.UserID == userId);
|
||||
}
|
||||
|
||||
var resources = query
|
||||
.Select(r => new { r.ID, r.ResourceName })
|
||||
.ToArray();
|
||||
|
||||
var result = resources.Select(r => (r.ID, r.ResourceName)).ToArray();
|
||||
logger.Info($"获取实验资源列表: {examId}/{resourceType},共 {result.Length} 个资源");
|
||||
logger.Info($"获取资源列表: {resourceType}" +
|
||||
(examId != null ? $"/{examId}" : "") +
|
||||
(resourcePurpose != null ? $"/{resourcePurpose}" : "") +
|
||||
(userId != null ? $"/{userId}" : "") +
|
||||
$",共 {result.Length} 个资源");
|
||||
return new(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取实验资源列表时出错: {ex.Message}");
|
||||
logger.Error($"获取资源列表时出错: {ex.Message}");
|
||||
return new(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取完整的资源列表
|
||||
/// </summary>
|
||||
/// <param name="examId">实验ID(可选)</param>
|
||||
/// <param name="resourceType">资源类型(可选)</param>
|
||||
/// <param name="resourcePurpose">资源用途(可选)</param>
|
||||
/// <param name="userId">用户ID(可选)</param>
|
||||
/// <returns>完整的资源对象列表</returns>
|
||||
public Result<List<Resource>> GetFullResourceList(string? examId = null, string? resourceType = null, string? resourcePurpose = null, Guid? userId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = this.ResourceTable.AsQueryable();
|
||||
|
||||
if (examId != null)
|
||||
{
|
||||
query = query.Where(r => r.ExamID == examId);
|
||||
}
|
||||
|
||||
if (resourceType != null)
|
||||
{
|
||||
query = query.Where(r => r.ResourceType == resourceType);
|
||||
}
|
||||
|
||||
if (resourcePurpose != null)
|
||||
{
|
||||
query = query.Where(r => r.ResourcePurpose == resourcePurpose);
|
||||
}
|
||||
|
||||
if (userId != null)
|
||||
{
|
||||
query = query.Where(r => r.UserID == userId);
|
||||
}
|
||||
|
||||
var resources = query.OrderByDescending(r => r.UploadTime).ToList();
|
||||
logger.Info($"获取完整资源列表" +
|
||||
(examId != null ? $" [实验: {examId}]" : "") +
|
||||
(resourceType != null ? $" [类型: {resourceType}]" : "") +
|
||||
(resourcePurpose != null ? $" [用途: {resourcePurpose}]" : "") +
|
||||
(userId != null ? $" [用户: {userId}]" : "") +
|
||||
$",共 {resources.Count} 个资源");
|
||||
return new(resources);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error($"获取完整资源列表时出错: {ex.Message}");
|
||||
return new(ex);
|
||||
}
|
||||
}
|
||||
@@ -1022,16 +1137,16 @@ public class AppDataConnection : DataConnection
|
||||
/// </summary>
|
||||
/// <param name="resourceId">资源ID</param>
|
||||
/// <returns>资源数据</returns>
|
||||
public Result<Optional<ExamResource>> GetExamResourceById(int resourceId)
|
||||
public Result<Optional<Resource>> GetResourceById(int resourceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var resource = this.ExamResourceTable.Where(r => r.ID == resourceId).FirstOrDefault();
|
||||
var resource = this.ResourceTable.Where(r => r.ID == resourceId).FirstOrDefault();
|
||||
|
||||
if (resource == null)
|
||||
{
|
||||
logger.Info($"未找到资源: {resourceId}");
|
||||
return new(Optional<ExamResource>.None);
|
||||
return new(Optional<Resource>.None);
|
||||
}
|
||||
|
||||
logger.Info($"成功获取资源: {resourceId} ({resource.ResourceName})");
|
||||
@@ -1045,15 +1160,15 @@ public class AppDataConnection : DataConnection
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除实验资源
|
||||
/// 删除资源
|
||||
/// </summary>
|
||||
/// <param name="resourceId">资源ID</param>
|
||||
/// <returns>删除的记录数</returns>
|
||||
public Result<int> DeleteExamResource(int resourceId)
|
||||
public Result<int> DeleteResource(int resourceId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = this.ExamResourceTable.Where(r => r.ID == resourceId).Delete();
|
||||
var result = this.ResourceTable.Where(r => r.ID == resourceId).Delete();
|
||||
logger.Info($"资源已删除: {resourceId},删除记录数: {result}");
|
||||
return new(result);
|
||||
}
|
||||
@@ -1132,29 +1247,20 @@ public class AppDataConnection : DataConnection
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除所有实验
|
||||
/// 根据文件扩展名获取比特流MIME类型
|
||||
/// </summary>
|
||||
/// <returns>删除的实验数量</returns>
|
||||
public int DeleteAllExams()
|
||||
/// <param name="extension">文件扩展名</param>
|
||||
/// <returns>MIME类型</returns>
|
||||
private string GetBitstreamMimeType(string extension)
|
||||
{
|
||||
// 先删除所有实验资源
|
||||
var resourceDeleteCount = this.DeleteAllExamResources();
|
||||
logger.Info($"已删除所有实验资源,共删除 {resourceDeleteCount} 个资源");
|
||||
|
||||
// 再删除所有实验
|
||||
var examDeleteCount = this.ExamTable.Delete();
|
||||
logger.Info($"已删除所有实验,共删除 {examDeleteCount} 个实验");
|
||||
return examDeleteCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除所有实验资源
|
||||
/// </summary>
|
||||
/// <returns>删除的资源数量</returns>
|
||||
public int DeleteAllExamResources()
|
||||
return extension.ToLowerInvariant() switch
|
||||
{
|
||||
var deleteCount = this.ExamResourceTable.Delete();
|
||||
logger.Info($"已删除所有实验资源,共删除 {deleteCount} 个资源");
|
||||
return deleteCount;
|
||||
".bit" => "application/octet-stream",
|
||||
".sbit" => "application/octet-stream",
|
||||
".bin" => "application/octet-stream",
|
||||
".mcs" => "application/octet-stream",
|
||||
".hex" => "text/plain",
|
||||
_ => "application/octet-stream"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,12 +25,19 @@ public interface IJtagReceiver
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[EnableCors("Users")]
|
||||
[EnableCors("SignalR")]
|
||||
public class JtagHub : Hub<IJtagReceiver>, IJtagHub
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
private ConcurrentDictionary<string, int> FreqTable = new();
|
||||
private ConcurrentDictionary<string, CancellationTokenSource> CancellationTokenSourceTable = new();
|
||||
private static ConcurrentDictionary<string, int> FreqTable = new();
|
||||
private static ConcurrentDictionary<string, CancellationTokenSource> CancellationTokenSourceTable = new();
|
||||
|
||||
private readonly IHubContext<JtagHub, IJtagReceiver> _hubContext;
|
||||
|
||||
public JtagHub(IHubContext<JtagHub, IJtagReceiver> hubContext)
|
||||
{
|
||||
_hubContext = hubContext;
|
||||
}
|
||||
|
||||
private Optional<Peripherals.JtagClient.Jtag> GetJtagClient(string userName)
|
||||
{
|
||||
@@ -92,33 +99,40 @@ public class JtagHub : Hub<IJtagReceiver>, IJtagHub
|
||||
}
|
||||
|
||||
await SetBoundaryScanFreq(freq);
|
||||
var cts = CancellationTokenSource.CreateLinkedTokenSource();
|
||||
var cts = new CancellationTokenSource();
|
||||
CancellationTokenSourceTable.AddOrUpdate(userName, cts, (key, value) => cts);
|
||||
|
||||
_ = Task
|
||||
.Run(
|
||||
() => BoundaryScanLogicPorts(
|
||||
Context.ConnectionId,
|
||||
userName,
|
||||
cts.Token),
|
||||
_ = Task.Run(
|
||||
() => BoundaryScanLogicPorts(Context.ConnectionId, userName, cts.Token),
|
||||
cts.Token)
|
||||
.ContinueWith((task) =>
|
||||
{
|
||||
if (!task.IsFaulted)
|
||||
if (task.IsFaulted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (task.Exception.InnerException is OperationCanceledException)
|
||||
// 遍历所有异常
|
||||
foreach (var ex in task.Exception.InnerExceptions)
|
||||
{
|
||||
if (ex is OperationCanceledException)
|
||||
{
|
||||
logger.Info($"Boundary scan operation cancelled for user {userName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error(task.Exception);
|
||||
logger.Error($"Boundary scan operation failed for user {userName}: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (task.IsCanceled)
|
||||
{
|
||||
logger.Info($"Boundary scan operation cancelled for user {userName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Info($"Boundary scan completed successfully for user {userName}");
|
||||
}
|
||||
});
|
||||
|
||||
logger.Info($"Boundary scan started for user {userName}");
|
||||
return true;
|
||||
}
|
||||
catch (Exception error)
|
||||
@@ -144,10 +158,12 @@ public class JtagHub : Hub<IJtagReceiver>, IJtagHub
|
||||
|
||||
cts.Cancel();
|
||||
cts.Token.WaitHandle.WaitOne();
|
||||
|
||||
logger.Info($"Boundary scan stopped for user {userName}");
|
||||
return true;
|
||||
}
|
||||
|
||||
private async void BoundaryScanLogicPorts(string connectionID, string userName, CancellationToken cancellationToken)
|
||||
private async Task BoundaryScanLogicPorts(string connectionID, string userName, CancellationToken cancellationToken)
|
||||
{
|
||||
var jtagCtrl = GetJtagClient(userName).OrThrow(() => new InvalidOperationException("JTAG client not found"));
|
||||
var cntFail = 0;
|
||||
@@ -161,9 +177,10 @@ public class JtagHub : Hub<IJtagReceiver>, IJtagHub
|
||||
{
|
||||
logger.Error($"User {userName} boundary scan failed for device {jtagCtrl.address}: {ret.Error}");
|
||||
cntFail++;
|
||||
continue;
|
||||
}
|
||||
|
||||
await this.Clients.Client(connectionID).OnReceiveBoundaryScanData(ret.Value);
|
||||
await _hubContext.Clients.Client(connectionID).OnReceiveBoundaryScanData(ret.Value);
|
||||
// logger.Info($"User {userName} successfully completed boundary scan for device {jtagCtrl.address}");
|
||||
|
||||
await Task.Delay(FreqTable.TryGetValue(userName, out var freq) ? 1000 / freq : 1000 / 100, cancellationToken);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
using Peripherals.PowerClient;
|
||||
using WebProtocol;
|
||||
|
||||
namespace Peripherals.CameraClient;
|
||||
|
||||
@@ -19,7 +20,7 @@ class Camera
|
||||
{
|
||||
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
readonly int timeout = 2000;
|
||||
readonly int timeout = 500;
|
||||
readonly int taskID;
|
||||
readonly int port;
|
||||
readonly string address;
|
||||
@@ -43,7 +44,7 @@ class Camera
|
||||
/// <param name="address">摄像头设备IP地址</param>
|
||||
/// <param name="port">摄像头设备端口</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
public Camera(string address, int port, int timeout = 2000)
|
||||
public Camera(string address, int port, int timeout = 500)
|
||||
{
|
||||
if (timeout < 0)
|
||||
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
|
||||
@@ -225,6 +226,7 @@ class Camera
|
||||
this.taskID, // taskID
|
||||
FrameAddr,
|
||||
(int)_currentFrameLength, // 使用当前分辨率的动态大小
|
||||
BurstType.ExtendBurst,
|
||||
this.timeout);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
@@ -462,6 +464,20 @@ class Camera
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置为960x540分辨率
|
||||
/// </summary>
|
||||
/// <returns>配置结果</returns>
|
||||
public async ValueTask<Result<bool>> ConfigureResolution960x540()
|
||||
{
|
||||
return await ConfigureResolution(
|
||||
hStart: 0, vStart: 0,
|
||||
dvpHo: 960, dvpVo: 540,
|
||||
hts: 1700, vts: 1500,
|
||||
hOffset: 16, vOffset: 4
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置为320x240分辨率
|
||||
/// </summary>
|
||||
@@ -543,6 +559,9 @@ class Camera
|
||||
case "640x480":
|
||||
result = await ConfigureResolution640x480();
|
||||
break;
|
||||
case "960x540":
|
||||
result = await ConfigureResolution960x540();
|
||||
break;
|
||||
case "1280x720":
|
||||
result = await ConfigureResolution1280x720();
|
||||
break;
|
||||
|
||||
118
server/src/Peripherals/HdmiInClient.cs
Normal file
118
server/src/Peripherals/HdmiInClient.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System.Net;
|
||||
using DotNext;
|
||||
using Peripherals.PowerClient;
|
||||
using WebProtocol;
|
||||
|
||||
namespace Peripherals.HdmiInClient;
|
||||
|
||||
static class HdmiInAddr
|
||||
{
|
||||
public const UInt32 BASE = 0xA000_0000;
|
||||
public const UInt32 HdmiIn_CTRL = BASE + 0x0; //[0]: rstn, 0 is reset.
|
||||
public const UInt32 HdmiIn_READFIFO = BASE + 0x1;
|
||||
}
|
||||
|
||||
class HdmiIn
|
||||
{
|
||||
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;
|
||||
|
||||
// 动态分辨率参数
|
||||
private UInt16 _currentWidth = 960;
|
||||
private UInt16 _currentHeight = 540;
|
||||
private UInt32 _currentFrameLength = 960 * 540 * 2 / 4; // RGB565格式,2字节/像素,按4字节对齐
|
||||
|
||||
/// <summary>
|
||||
/// 初始化HDMI输入客户端
|
||||
/// </summary>
|
||||
/// <param name="address">HDMI输入设备IP地址</param>
|
||||
/// <param name="port">HDMI输入设备端口</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
public HdmiIn(string address, int port, 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.timeout = timeout;
|
||||
}
|
||||
|
||||
public async ValueTask<Result<bool>> EnableTrans(bool isEnable)
|
||||
{
|
||||
var ret = await UDPClientPool.WriteAddr(this.ep, this.taskID, HdmiInAddr.HdmiIn_CTRL, (isEnable ? 0x00000001u : 0x00000000u));
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to write HdmiIn_CTRL to HdmiIn at {this.address}:{this.port}, error: {ret.Error}");
|
||||
return new(ret.Error);
|
||||
}
|
||||
if (!ret.Value)
|
||||
{
|
||||
logger.Error($"HdmiIn_CTRL write returned false for HdmiIn at {this.address}:{this.port}");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取一帧图像数据
|
||||
/// </summary>
|
||||
/// <returns>包含图像数据的字节数组</returns>
|
||||
public async ValueTask<Result<byte[]>> ReadFrame()
|
||||
{
|
||||
// 只在第一次或出错时清除UDP缓冲区,避免每帧都清除造成延迟
|
||||
// MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
|
||||
logger.Trace($"Reading frame from HdmiIn {this.address}");
|
||||
|
||||
// 使用UDPClientPool读取图像帧数据
|
||||
var result = await UDPClientPool.ReadAddr4BytesAsync(
|
||||
this.ep,
|
||||
this.taskID, // taskID
|
||||
HdmiInAddr.HdmiIn_READFIFO,
|
||||
(int)_currentFrameLength, // 使用当前分辨率的动态大小
|
||||
BurstType.FixedBurst,
|
||||
this.timeout);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"Failed to read frame from HdmiIn {this.address}:{this.port}, error: {result.Error}");
|
||||
// 读取失败时清除缓冲区,为下次读取做准备
|
||||
try
|
||||
{
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Warn($"Failed to clear UDP data after read error: {ex.Message}");
|
||||
}
|
||||
return new(result.Error);
|
||||
}
|
||||
|
||||
logger.Trace($"Successfully read frame from HdmiIn {this.address}:{this.port}, data length: {result.Value.Length} bytes");
|
||||
return result.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前分辨率
|
||||
/// </summary>
|
||||
/// <returns>当前分辨率(宽度, 高度)</returns>
|
||||
public (int Width, int Height) GetCurrentResolution()
|
||||
{
|
||||
return (_currentWidth, _currentHeight);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前帧长度
|
||||
/// </summary>
|
||||
/// <returns>当前帧长度</returns>
|
||||
public UInt32 GetCurrentFrameLength()
|
||||
{
|
||||
return _currentFrameLength;
|
||||
}
|
||||
}
|
||||
@@ -612,13 +612,10 @@ public class Jtag
|
||||
if (ret.Value)
|
||||
{
|
||||
var array = new UInt32[UInt32Num];
|
||||
for (int i = 0; i < UInt32Num; i++)
|
||||
{
|
||||
var retData = await ReadFIFO(JtagAddr.READ_DATA);
|
||||
var retData = await UDPClientPool.ReadAddr4Bytes(ep, 0, JtagAddr.READ_DATA, (int)UInt32Num);
|
||||
if (!retData.IsSuccessful)
|
||||
return new(new Exception("Read FIFO failed when Load DR"));
|
||||
array[i] = retData.Value;
|
||||
}
|
||||
Buffer.BlockCopy(retData.Value, 0, array, 0, (int)UInt32Num * 4);
|
||||
return array;
|
||||
}
|
||||
else
|
||||
@@ -788,7 +785,7 @@ public class Jtag
|
||||
{
|
||||
var paser = new BsdlParser.Parser();
|
||||
var portNum = paser.GetBoundaryRegsNum().Value;
|
||||
logger.Debug($"Get boundar scan registers number: {portNum}");
|
||||
logger.Debug($"Get boundary scan registers number: {portNum}");
|
||||
|
||||
// Clear Data
|
||||
MsgBus.UDPServer.ClearUDPData(this.address, 0);
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Collections;
|
||||
using System.Net;
|
||||
using Common;
|
||||
using DotNext;
|
||||
using WebProtocol;
|
||||
|
||||
namespace Peripherals.LogicAnalyzerClient;
|
||||
|
||||
@@ -475,6 +476,7 @@ public class Analyzer
|
||||
this.taskID,
|
||||
AnalyzerAddr.STORE_OFFSET_ADDR,
|
||||
capture_length,
|
||||
BurstType.ExtendBurst, // 使用扩展突发读取
|
||||
this.timeout
|
||||
);
|
||||
if (!ret.IsSuccessful)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Net;
|
||||
using Common;
|
||||
using DotNext;
|
||||
using WebProtocol;
|
||||
|
||||
namespace Peripherals.OscilloscopeClient;
|
||||
|
||||
@@ -319,6 +320,7 @@ class Oscilloscope
|
||||
this.taskID,
|
||||
OscilloscopeAddr.RD_DATA_ADDR,
|
||||
(int)OscilloscopeAddr.RD_DATA_LENGTH / 32,
|
||||
BurstType.ExtendBurst, // 使用扩展突发读取
|
||||
this.timeout
|
||||
);
|
||||
if (!ret.IsSuccessful)
|
||||
|
||||
@@ -1109,6 +1109,7 @@ public class HttpVideoStreamService : BackgroundService
|
||||
return new List<(int, int, string)>
|
||||
{
|
||||
(640, 480, "640x480 (VGA)"),
|
||||
(960, 540, "960x540 (qHD)"),
|
||||
(1280, 720, "1280x720 (HD)"),
|
||||
(1280, 960, "1280x960 (SXGA)"),
|
||||
(1920, 1080, "1920x1080 (Full HD)")
|
||||
|
||||
@@ -433,11 +433,12 @@ public class UDPClientPool
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="taskID">任务ID</param>
|
||||
/// <param name="devAddr">设备地址</param>
|
||||
/// <param name="burstType">突发类型</param>
|
||||
/// <param name="dataLength">要读取的数据长度(4字节)</param>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns>读取结果,包含接收到的字节数组</returns>
|
||||
public static async ValueTask<Result<byte[]>> ReadAddr4BytesAsync(
|
||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, int timeout = 1000)
|
||||
IPEndPoint endPoint, int taskID, UInt32 devAddr, int dataLength, BurstType burstType, int timeout = 1000)
|
||||
{
|
||||
var pkgList = new List<SendAddrPackage>();
|
||||
var resultData = new List<byte>();
|
||||
@@ -460,7 +461,7 @@ public class UDPClientPool
|
||||
|
||||
var opts = new SendAddrPackOptions
|
||||
{
|
||||
BurstType = BurstType.FixedBurst,
|
||||
BurstType = burstType,
|
||||
CommandID = Convert.ToByte(taskID),
|
||||
IsWrite = false,
|
||||
BurstLength = (byte)(currentSegmentSize - 1),
|
||||
|
||||
832
src/APIClient.ts
832
src/APIClient.ts
@@ -2942,277 +2942,6 @@ export class ExamClient {
|
||||
}
|
||||
return Promise.resolve<ExamInfo>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加实验资源
|
||||
* @param examId 实验ID
|
||||
* @param resourceType 资源类型
|
||||
* @param file (optional) 资源文件
|
||||
* @return 添加结果
|
||||
*/
|
||||
addExamResource(examId: string, resourceType: string, file: FileParameter | undefined, cancelToken?: CancelToken): Promise<ResourceInfo> {
|
||||
let url_ = this.baseUrl + "/api/Exam/{examId}/resources/{resourceType}";
|
||||
if (examId === undefined || examId === null)
|
||||
throw new Error("The parameter 'examId' must be defined.");
|
||||
url_ = url_.replace("{examId}", encodeURIComponent("" + examId));
|
||||
if (resourceType === undefined || resourceType === null)
|
||||
throw new Error("The parameter 'resourceType' must be defined.");
|
||||
url_ = url_.replace("{resourceType}", encodeURIComponent("" + resourceType));
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
const content_ = new FormData();
|
||||
if (file === null || file === undefined)
|
||||
throw new Error("The parameter 'file' cannot be null.");
|
||||
else
|
||||
content_.append("file", file.data, file.fileName ? file.fileName : "file");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
data: content_,
|
||||
method: "POST",
|
||||
url: url_,
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processAddExamResource(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processAddExamResource(response: AxiosResponse): Promise<ResourceInfo> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 201) {
|
||||
const _responseText = response.data;
|
||||
let result201: any = null;
|
||||
let resultData201 = _responseText;
|
||||
result201 = ResourceInfo.fromJS(resultData201);
|
||||
return Promise.resolve<ResourceInfo>(result201);
|
||||
|
||||
} else if (status === 400) {
|
||||
const _responseText = response.data;
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText;
|
||||
result400 = ProblemDetails.fromJS(resultData400);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
|
||||
} else if (status === 401) {
|
||||
const _responseText = response.data;
|
||||
let result401: any = null;
|
||||
let resultData401 = _responseText;
|
||||
result401 = ProblemDetails.fromJS(resultData401);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
|
||||
|
||||
} else if (status === 403) {
|
||||
const _responseText = response.data;
|
||||
let result403: any = null;
|
||||
let resultData403 = _responseText;
|
||||
result403 = ProblemDetails.fromJS(resultData403);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result403);
|
||||
|
||||
} else if (status === 404) {
|
||||
const _responseText = response.data;
|
||||
let result404: any = null;
|
||||
let resultData404 = _responseText;
|
||||
result404 = ProblemDetails.fromJS(resultData404);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result404);
|
||||
|
||||
} else if (status === 409) {
|
||||
const _responseText = response.data;
|
||||
let result409: any = null;
|
||||
let resultData409 = _responseText;
|
||||
result409 = ProblemDetails.fromJS(resultData409);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result409);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<ResourceInfo>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定实验ID的指定资源类型的所有资源的ID和名称
|
||||
* @param examId 实验ID
|
||||
* @param resourceType 资源类型
|
||||
* @return 资源列表
|
||||
*/
|
||||
getExamResourceList(examId: string, resourceType: string, cancelToken?: CancelToken): Promise<ResourceInfo[]> {
|
||||
let url_ = this.baseUrl + "/api/Exam/{examId}/resources/{resourceType}";
|
||||
if (examId === undefined || examId === null)
|
||||
throw new Error("The parameter 'examId' must be defined.");
|
||||
url_ = url_.replace("{examId}", encodeURIComponent("" + examId));
|
||||
if (resourceType === undefined || resourceType === null)
|
||||
throw new Error("The parameter 'resourceType' must be defined.");
|
||||
url_ = url_.replace("{resourceType}", encodeURIComponent("" + resourceType));
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
method: "GET",
|
||||
url: url_,
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processGetExamResourceList(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processGetExamResourceList(response: AxiosResponse): Promise<ResourceInfo[]> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 200) {
|
||||
const _responseText = response.data;
|
||||
let result200: any = null;
|
||||
let resultData200 = _responseText;
|
||||
if (Array.isArray(resultData200)) {
|
||||
result200 = [] as any;
|
||||
for (let item of resultData200)
|
||||
result200!.push(ResourceInfo.fromJS(item));
|
||||
}
|
||||
else {
|
||||
result200 = <any>null;
|
||||
}
|
||||
return Promise.resolve<ResourceInfo[]>(result200);
|
||||
|
||||
} else if (status === 400) {
|
||||
const _responseText = response.data;
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText;
|
||||
result400 = ProblemDetails.fromJS(resultData400);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
|
||||
} else if (status === 401) {
|
||||
const _responseText = response.data;
|
||||
let result401: any = null;
|
||||
let resultData401 = _responseText;
|
||||
result401 = ProblemDetails.fromJS(resultData401);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<ResourceInfo[]>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据资源ID下载资源
|
||||
* @param resourceId 资源ID
|
||||
* @return 资源文件
|
||||
*/
|
||||
getExamResourceById(resourceId: number, cancelToken?: CancelToken): Promise<FileResponse> {
|
||||
let url_ = this.baseUrl + "/api/Exam/resources/{resourceId}";
|
||||
if (resourceId === undefined || resourceId === null)
|
||||
throw new Error("The parameter 'resourceId' must be defined.");
|
||||
url_ = url_.replace("{resourceId}", encodeURIComponent("" + resourceId));
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
responseType: "blob",
|
||||
method: "GET",
|
||||
url: url_,
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processGetExamResourceById(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processGetExamResourceById(response: AxiosResponse): Promise<FileResponse> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 200 || status === 206) {
|
||||
const contentDisposition = response.headers ? response.headers["content-disposition"] : undefined;
|
||||
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
|
||||
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
|
||||
if (fileName) {
|
||||
fileName = decodeURIComponent(fileName);
|
||||
} else {
|
||||
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
|
||||
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
|
||||
}
|
||||
return Promise.resolve({ fileName: fileName, status: status, data: new Blob([response.data], { type: response.headers["content-type"] }), headers: _headers });
|
||||
} else if (status === 400) {
|
||||
const _responseText = response.data;
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText;
|
||||
result400 = ProblemDetails.fromJS(resultData400);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
|
||||
} else if (status === 404) {
|
||||
const _responseText = response.data;
|
||||
let result404: any = null;
|
||||
let resultData404 = _responseText;
|
||||
result404 = ProblemDetails.fromJS(resultData404);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result404);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<FileResponse>(null as any);
|
||||
}
|
||||
}
|
||||
|
||||
export class JtagClient {
|
||||
@@ -3427,98 +3156,14 @@ export class JtagClient {
|
||||
return Promise.resolve<void>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传比特流文件到服务器
|
||||
* @param address (optional) 目标设备地址
|
||||
* @param file (optional) 比特流文件
|
||||
* @return 上传结果
|
||||
*/
|
||||
uploadBitstream(address: string | undefined, file: FileParameter | undefined, cancelToken?: CancelToken): Promise<boolean> {
|
||||
let url_ = this.baseUrl + "/api/Jtag/UploadBitstream?";
|
||||
if (address === null)
|
||||
throw new Error("The parameter 'address' cannot be null.");
|
||||
else if (address !== undefined)
|
||||
url_ += "address=" + encodeURIComponent("" + address) + "&";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
const content_ = new FormData();
|
||||
if (file === null || file === undefined)
|
||||
throw new Error("The parameter 'file' cannot be null.");
|
||||
else
|
||||
content_.append("file", file.data, file.fileName ? file.fileName : "file");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
data: content_,
|
||||
method: "POST",
|
||||
url: url_,
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processUploadBitstream(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processUploadBitstream(response: AxiosResponse): Promise<boolean> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 200) {
|
||||
const _responseText = response.data;
|
||||
let result200: any = null;
|
||||
let resultData200 = _responseText;
|
||||
result200 = resultData200 !== undefined ? resultData200 : <any>null;
|
||||
|
||||
return Promise.resolve<boolean>(result200);
|
||||
|
||||
} else if (status === 400) {
|
||||
const _responseText = response.data;
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText;
|
||||
result400 = resultData400 !== undefined ? resultData400 : <any>null;
|
||||
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
|
||||
} else if (status === 401) {
|
||||
const _responseText = response.data;
|
||||
let result401: any = null;
|
||||
let resultData401 = _responseText;
|
||||
result401 = ProblemDetails.fromJS(resultData401);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<boolean>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 JTAG 下载比特流文件到 FPGA 设备
|
||||
* @param address (optional) JTAG 设备地址
|
||||
* @param port (optional) JTAG 设备端口
|
||||
* @param bitstreamId (optional) 比特流ID
|
||||
* @return 下载结果
|
||||
*/
|
||||
downloadBitstream(address: string | undefined, port: number | undefined, cancelToken?: CancelToken): Promise<boolean> {
|
||||
downloadBitstream(address: string | undefined, port: number | undefined, bitstreamId: number | undefined, cancelToken?: CancelToken): Promise<boolean> {
|
||||
let url_ = this.baseUrl + "/api/Jtag/DownloadBitstream?";
|
||||
if (address === null)
|
||||
throw new Error("The parameter 'address' cannot be null.");
|
||||
@@ -3528,6 +3173,10 @@ export class JtagClient {
|
||||
throw new Error("The parameter 'port' cannot be null.");
|
||||
else if (port !== undefined)
|
||||
url_ += "port=" + encodeURIComponent("" + port) + "&";
|
||||
if (bitstreamId === null)
|
||||
throw new Error("The parameter 'bitstreamId' cannot be null.");
|
||||
else if (bitstreamId !== undefined)
|
||||
url_ += "bitstreamId=" + encodeURIComponent("" + bitstreamId) + "&";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
@@ -6565,6 +6214,353 @@ export class RemoteUpdateClient {
|
||||
}
|
||||
}
|
||||
|
||||
export class ResourceClient {
|
||||
protected instance: AxiosInstance;
|
||||
protected baseUrl: string;
|
||||
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
|
||||
|
||||
constructor(baseUrl?: string, instance?: AxiosInstance) {
|
||||
|
||||
this.instance = instance || axios.create();
|
||||
|
||||
this.baseUrl = baseUrl ?? "http://127.0.0.1:5000";
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加资源(文件上传)
|
||||
* @param resourceType (optional) 资源类型
|
||||
* @param resourcePurpose (optional) 资源用途(template/user)
|
||||
* @param examID (optional) 所属实验ID(可选)
|
||||
* @param file (optional) 资源文件
|
||||
* @return 添加结果
|
||||
*/
|
||||
addResource(resourceType: string | undefined, resourcePurpose: string | undefined, examID: string | null | undefined, file: FileParameter | undefined, cancelToken?: CancelToken): Promise<ResourceInfo> {
|
||||
let url_ = this.baseUrl + "/api/Resource";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
const content_ = new FormData();
|
||||
if (resourceType === null || resourceType === undefined)
|
||||
throw new Error("The parameter 'resourceType' cannot be null.");
|
||||
else
|
||||
content_.append("ResourceType", resourceType.toString());
|
||||
if (resourcePurpose === null || resourcePurpose === undefined)
|
||||
throw new Error("The parameter 'resourcePurpose' cannot be null.");
|
||||
else
|
||||
content_.append("ResourcePurpose", resourcePurpose.toString());
|
||||
if (examID !== null && examID !== undefined)
|
||||
content_.append("ExamID", examID.toString());
|
||||
if (file === null || file === undefined)
|
||||
throw new Error("The parameter 'file' cannot be null.");
|
||||
else
|
||||
content_.append("file", file.data, file.fileName ? file.fileName : "file");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
data: content_,
|
||||
method: "POST",
|
||||
url: url_,
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processAddResource(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processAddResource(response: AxiosResponse): Promise<ResourceInfo> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 201) {
|
||||
const _responseText = response.data;
|
||||
let result201: any = null;
|
||||
let resultData201 = _responseText;
|
||||
result201 = ResourceInfo.fromJS(resultData201);
|
||||
return Promise.resolve<ResourceInfo>(result201);
|
||||
|
||||
} else if (status === 400) {
|
||||
const _responseText = response.data;
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText;
|
||||
result400 = ProblemDetails.fromJS(resultData400);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
|
||||
} else if (status === 401) {
|
||||
const _responseText = response.data;
|
||||
let result401: any = null;
|
||||
let resultData401 = _responseText;
|
||||
result401 = ProblemDetails.fromJS(resultData401);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
|
||||
|
||||
} else if (status === 404) {
|
||||
const _responseText = response.data;
|
||||
let result404: any = null;
|
||||
let resultData404 = _responseText;
|
||||
result404 = ProblemDetails.fromJS(resultData404);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result404);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<ResourceInfo>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取资源列表
|
||||
* @param examId (optional) 实验ID(可选)
|
||||
* @param resourceType (optional) 资源类型(可选)
|
||||
* @param resourcePurpose (optional) 资源用途(可选)
|
||||
* @return 资源列表
|
||||
*/
|
||||
getResourceList(examId: string | null | undefined, resourceType: string | null | undefined, resourcePurpose: string | null | undefined, cancelToken?: CancelToken): Promise<ResourceInfo[]> {
|
||||
let url_ = this.baseUrl + "/api/Resource?";
|
||||
if (examId !== undefined && examId !== null)
|
||||
url_ += "examId=" + encodeURIComponent("" + examId) + "&";
|
||||
if (resourceType !== undefined && resourceType !== null)
|
||||
url_ += "resourceType=" + encodeURIComponent("" + resourceType) + "&";
|
||||
if (resourcePurpose !== undefined && resourcePurpose !== null)
|
||||
url_ += "resourcePurpose=" + encodeURIComponent("" + resourcePurpose) + "&";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
method: "GET",
|
||||
url: url_,
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processGetResourceList(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processGetResourceList(response: AxiosResponse): Promise<ResourceInfo[]> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 200) {
|
||||
const _responseText = response.data;
|
||||
let result200: any = null;
|
||||
let resultData200 = _responseText;
|
||||
if (Array.isArray(resultData200)) {
|
||||
result200 = [] as any;
|
||||
for (let item of resultData200)
|
||||
result200!.push(ResourceInfo.fromJS(item));
|
||||
}
|
||||
else {
|
||||
result200 = <any>null;
|
||||
}
|
||||
return Promise.resolve<ResourceInfo[]>(result200);
|
||||
|
||||
} else if (status === 401) {
|
||||
const _responseText = response.data;
|
||||
let result401: any = null;
|
||||
let resultData401 = _responseText;
|
||||
result401 = ProblemDetails.fromJS(resultData401);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<ResourceInfo[]>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据资源ID下载资源
|
||||
* @param resourceId 资源ID
|
||||
* @return 资源文件
|
||||
*/
|
||||
getResourceById(resourceId: number, cancelToken?: CancelToken): Promise<FileResponse> {
|
||||
let url_ = this.baseUrl + "/api/Resource/{resourceId}";
|
||||
if (resourceId === undefined || resourceId === null)
|
||||
throw new Error("The parameter 'resourceId' must be defined.");
|
||||
url_ = url_.replace("{resourceId}", encodeURIComponent("" + resourceId));
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
responseType: "blob",
|
||||
method: "GET",
|
||||
url: url_,
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processGetResourceById(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processGetResourceById(response: AxiosResponse): Promise<FileResponse> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 200 || status === 206) {
|
||||
const contentDisposition = response.headers ? response.headers["content-disposition"] : undefined;
|
||||
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
|
||||
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
|
||||
if (fileName) {
|
||||
fileName = decodeURIComponent(fileName);
|
||||
} else {
|
||||
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
|
||||
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
|
||||
}
|
||||
return Promise.resolve({ fileName: fileName, status: status, data: new Blob([response.data], { type: response.headers["content-type"] }), headers: _headers });
|
||||
} else if (status === 400) {
|
||||
const _responseText = response.data;
|
||||
let result400: any = null;
|
||||
let resultData400 = _responseText;
|
||||
result400 = ProblemDetails.fromJS(resultData400);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result400);
|
||||
|
||||
} else if (status === 404) {
|
||||
const _responseText = response.data;
|
||||
let result404: any = null;
|
||||
let resultData404 = _responseText;
|
||||
result404 = ProblemDetails.fromJS(resultData404);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result404);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<FileResponse>(null as any);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除资源
|
||||
* @param resourceId 资源ID
|
||||
* @return 删除结果
|
||||
*/
|
||||
deleteResource(resourceId: number, cancelToken?: CancelToken): Promise<void> {
|
||||
let url_ = this.baseUrl + "/api/Resource/{resourceId}";
|
||||
if (resourceId === undefined || resourceId === null)
|
||||
throw new Error("The parameter 'resourceId' must be defined.");
|
||||
url_ = url_.replace("{resourceId}", encodeURIComponent("" + resourceId));
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: AxiosRequestConfig = {
|
||||
method: "DELETE",
|
||||
url: url_,
|
||||
headers: {
|
||||
},
|
||||
cancelToken
|
||||
};
|
||||
|
||||
return this.instance.request(options_).catch((_error: any) => {
|
||||
if (isAxiosError(_error) && _error.response) {
|
||||
return _error.response;
|
||||
} else {
|
||||
throw _error;
|
||||
}
|
||||
}).then((_response: AxiosResponse) => {
|
||||
return this.processDeleteResource(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processDeleteResource(response: AxiosResponse): Promise<void> {
|
||||
const status = response.status;
|
||||
let _headers: any = {};
|
||||
if (response.headers && typeof response.headers === "object") {
|
||||
for (const k in response.headers) {
|
||||
if (response.headers.hasOwnProperty(k)) {
|
||||
_headers[k] = response.headers[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status === 204) {
|
||||
const _responseText = response.data;
|
||||
return Promise.resolve<void>(null as any);
|
||||
|
||||
} else if (status === 401) {
|
||||
const _responseText = response.data;
|
||||
let result401: any = null;
|
||||
let resultData401 = _responseText;
|
||||
result401 = ProblemDetails.fromJS(resultData401);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result401);
|
||||
|
||||
} else if (status === 403) {
|
||||
const _responseText = response.data;
|
||||
let result403: any = null;
|
||||
let resultData403 = _responseText;
|
||||
result403 = ProblemDetails.fromJS(resultData403);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result403);
|
||||
|
||||
} else if (status === 404) {
|
||||
const _responseText = response.data;
|
||||
let result404: any = null;
|
||||
let resultData404 = _responseText;
|
||||
result404 = ProblemDetails.fromJS(resultData404);
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers, result404);
|
||||
|
||||
} else if (status === 500) {
|
||||
const _responseText = response.data;
|
||||
return throwException("A server side error occurred.", status, _responseText, _headers);
|
||||
|
||||
} else if (status !== 200 && status !== 204) {
|
||||
const _responseText = response.data;
|
||||
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||
}
|
||||
return Promise.resolve<void>(null as any);
|
||||
}
|
||||
}
|
||||
|
||||
export class TutorialClient {
|
||||
protected instance: AxiosInstance;
|
||||
protected baseUrl: string;
|
||||
@@ -8023,52 +8019,6 @@ export interface ICreateExamRequest {
|
||||
isVisibleToUsers: boolean;
|
||||
}
|
||||
|
||||
/** 资源信息类 */
|
||||
export class ResourceInfo implements IResourceInfo {
|
||||
/** 资源ID */
|
||||
id!: number;
|
||||
/** 资源名称 */
|
||||
name!: string;
|
||||
|
||||
constructor(data?: IResourceInfo) {
|
||||
if (data) {
|
||||
for (var property in data) {
|
||||
if (data.hasOwnProperty(property))
|
||||
(<any>this)[property] = (<any>data)[property];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init(_data?: any) {
|
||||
if (_data) {
|
||||
this.id = _data["id"];
|
||||
this.name = _data["name"];
|
||||
}
|
||||
}
|
||||
|
||||
static fromJS(data: any): ResourceInfo {
|
||||
data = typeof data === 'object' ? data : {};
|
||||
let result = new ResourceInfo();
|
||||
result.init(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
toJSON(data?: any) {
|
||||
data = typeof data === 'object' ? data : {};
|
||||
data["id"] = this.id;
|
||||
data["name"] = this.name;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/** 资源信息类 */
|
||||
export interface IResourceInfo {
|
||||
/** 资源ID */
|
||||
id: number;
|
||||
/** 资源名称 */
|
||||
name: string;
|
||||
}
|
||||
|
||||
/** 逻辑分析仪运行状态枚举 */
|
||||
export enum CaptureStatus {
|
||||
None = 0,
|
||||
@@ -8518,6 +8468,82 @@ export interface IOscilloscopeDataResponse {
|
||||
waveformData: string;
|
||||
}
|
||||
|
||||
/** 资源信息类 */
|
||||
export class ResourceInfo implements IResourceInfo {
|
||||
/** 资源ID */
|
||||
id!: number;
|
||||
/** 资源名称 */
|
||||
name!: string;
|
||||
/** 资源类型 */
|
||||
type!: string;
|
||||
/** 资源用途(template/user) */
|
||||
purpose!: string;
|
||||
/** 上传时间 */
|
||||
uploadTime!: Date;
|
||||
/** 所属实验ID(可选) */
|
||||
examID?: string | undefined;
|
||||
/** MIME类型 */
|
||||
mimeType?: string | undefined;
|
||||
|
||||
constructor(data?: IResourceInfo) {
|
||||
if (data) {
|
||||
for (var property in data) {
|
||||
if (data.hasOwnProperty(property))
|
||||
(<any>this)[property] = (<any>data)[property];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init(_data?: any) {
|
||||
if (_data) {
|
||||
this.id = _data["id"];
|
||||
this.name = _data["name"];
|
||||
this.type = _data["type"];
|
||||
this.purpose = _data["purpose"];
|
||||
this.uploadTime = _data["uploadTime"] ? new Date(_data["uploadTime"].toString()) : <any>undefined;
|
||||
this.examID = _data["examID"];
|
||||
this.mimeType = _data["mimeType"];
|
||||
}
|
||||
}
|
||||
|
||||
static fromJS(data: any): ResourceInfo {
|
||||
data = typeof data === 'object' ? data : {};
|
||||
let result = new ResourceInfo();
|
||||
result.init(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
toJSON(data?: any) {
|
||||
data = typeof data === 'object' ? data : {};
|
||||
data["id"] = this.id;
|
||||
data["name"] = this.name;
|
||||
data["type"] = this.type;
|
||||
data["purpose"] = this.purpose;
|
||||
data["uploadTime"] = this.uploadTime ? this.uploadTime.toISOString() : <any>undefined;
|
||||
data["examID"] = this.examID;
|
||||
data["mimeType"] = this.mimeType;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/** 资源信息类 */
|
||||
export interface IResourceInfo {
|
||||
/** 资源ID */
|
||||
id: number;
|
||||
/** 资源名称 */
|
||||
name: string;
|
||||
/** 资源类型 */
|
||||
type: string;
|
||||
/** 资源用途(template/user) */
|
||||
purpose: string;
|
||||
/** 上传时间 */
|
||||
uploadTime: Date;
|
||||
/** 所属实验ID(可选) */
|
||||
examID?: string | undefined;
|
||||
/** MIME类型 */
|
||||
mimeType?: string | undefined;
|
||||
}
|
||||
|
||||
/** Package options which to send address to read or write */
|
||||
export class SendAddrPackOptions implements ISendAddrPackOptions {
|
||||
/** 突发类型 */
|
||||
|
||||
@@ -88,17 +88,17 @@ export async function loadDiagramData(examId?: string): Promise<DiagramData> {
|
||||
// 如果提供了examId,优先从API加载实验的diagram
|
||||
if (examId) {
|
||||
try {
|
||||
const examClient = AuthManager.createAuthenticatedExamClient();
|
||||
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||
|
||||
// 获取diagram类型的资源列表
|
||||
const resources = await examClient.getExamResourceList(examId, 'canvas');
|
||||
const resources = await resourceClient.getResourceList(examId, 'canvas', 'template');
|
||||
|
||||
if (resources && resources.length > 0) {
|
||||
// 获取第一个diagram资源
|
||||
const diagramResource = resources[0];
|
||||
|
||||
// 使用动态API获取资源文件内容
|
||||
const response = await examClient.getExamResourceById(diagramResource.id);
|
||||
const response = await resourceClient.getResourceById(diagramResource.id);
|
||||
|
||||
if (response && response.data) {
|
||||
const text = await response.data.text();
|
||||
|
||||
@@ -6,8 +6,6 @@ import hljs from 'highlight.js';
|
||||
import 'highlight.js/styles/github.css'; // 亮色主题
|
||||
// 导入主题存储
|
||||
import { useThemeStore } from '@/stores/theme';
|
||||
// 导入ExamClient用于获取图片资源
|
||||
import { ExamClient } from '@/APIClient';
|
||||
import { AuthManager } from '@/utils/AuthManager';
|
||||
|
||||
const props = defineProps({
|
||||
@@ -36,8 +34,8 @@ const imageResourceCache = ref<Map<string, string>>(new Map());
|
||||
// 获取图片资源ID的函数
|
||||
async function getImageResourceId(examId: string, imagePath: string): Promise<string | null> {
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedExamClient();
|
||||
const resources = await client.getExamResourceList(examId, 'images');
|
||||
const client = AuthManager.createAuthenticatedResourceClient();
|
||||
const resources = await client.getResourceList(examId, 'images', 'template');
|
||||
|
||||
// 查找匹配的图片资源
|
||||
const imageResource = resources.find(r => r.name === imagePath || r.name.endsWith(imagePath));
|
||||
@@ -52,8 +50,8 @@ async function getImageResourceId(examId: string, imagePath: string): Promise<st
|
||||
// 通过资源ID获取图片数据URL
|
||||
async function getImageDataUrl(resourceId: string): Promise<string | null> {
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedExamClient();
|
||||
const response = await client.getExamResourceById(parseInt(resourceId));
|
||||
const client = AuthManager.createAuthenticatedResourceClient();
|
||||
const response = await client.getResourceById(parseInt(resourceId));
|
||||
|
||||
if (response && response.data) {
|
||||
return URL.createObjectURL(response.data);
|
||||
|
||||
@@ -127,12 +127,13 @@ onMounted(async () => {
|
||||
let thumbnail: string | undefined;
|
||||
|
||||
try {
|
||||
// 获取实验的封面资源
|
||||
const resourceList = await client.getExamResourceList(exam.id, 'cover');
|
||||
// 获取实验的封面资源(模板资源)
|
||||
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||
const resourceList = await resourceClient.getResourceList(exam.id, 'cover', 'template');
|
||||
if (resourceList && resourceList.length > 0) {
|
||||
// 使用第一个封面资源
|
||||
const coverResource = resourceList[0];
|
||||
const fileResponse = await client.getExamResourceById(coverResource.id);
|
||||
const fileResponse = await resourceClient.getResourceById(coverResource.id);
|
||||
// 创建Blob URL作为缩略图
|
||||
thumbnail = URL.createObjectURL(fileResponse.data);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<fieldset class="fieldset w-full">
|
||||
<legend class="fieldset-legend text-sm">示例比特流文件</legend>
|
||||
<div class="space-y-2">
|
||||
<div v-for="bitstream in availableBitstreams" :key="bitstream.id" class="flex items-center justify-between p-2 bg-base-200 rounded">
|
||||
<div v-for="bitstream in availableBitstreams" :key="bitstream.id" class="flex items-center justify-between p-2 border-2 border-base-300 rounded-lg">
|
||||
<span class="text-sm">{{ bitstream.name }}</span>
|
||||
<div class="flex gap-2">
|
||||
<button
|
||||
@@ -75,8 +75,8 @@ import { useDialogStore } from "@/stores/dialog";
|
||||
import { isNull, isUndefined } from "lodash";
|
||||
|
||||
interface Props {
|
||||
uploadEvent?: (file: File) => Promise<boolean>;
|
||||
downloadEvent?: () => Promise<boolean>;
|
||||
uploadEvent?: (file: File, examId: string) => Promise<number | null>;
|
||||
downloadEvent?: (bitstreamId: number) => Promise<boolean>;
|
||||
maxMemory?: number;
|
||||
examId?: string; // 新增examId属性
|
||||
}
|
||||
@@ -127,9 +127,9 @@ async function loadAvailableBitstreams() {
|
||||
}
|
||||
|
||||
try {
|
||||
const examClient = AuthManager.createAuthenticatedExamClient();
|
||||
// 使用新的API获取比特流资源列表
|
||||
const resources = await examClient.getExamResourceList(props.examId, 'bitstream');
|
||||
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||
// 使用新的ResourceClient API获取比特流模板资源列表
|
||||
const resources = await resourceClient.getResourceList(props.examId, 'bitstream', 'template');
|
||||
availableBitstreams.value = resources.map(r => ({ id: r.id, name: r.name })) || [];
|
||||
} catch (error) {
|
||||
console.error('加载比特流列表失败:', error);
|
||||
@@ -143,10 +143,10 @@ async function downloadExampleBitstream(bitstream: {id: number, name: string}) {
|
||||
|
||||
isDownloading.value = true;
|
||||
try {
|
||||
const examClient = AuthManager.createAuthenticatedExamClient();
|
||||
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||
|
||||
// 使用动态API获取资源文件
|
||||
const response = await examClient.getExamResourceById(bitstream.id);
|
||||
// 使用新的ResourceClient API获取资源文件
|
||||
const response = await resourceClient.getResourceById(bitstream.id);
|
||||
|
||||
if (response && response.data) {
|
||||
// 创建下载链接
|
||||
@@ -173,37 +173,21 @@ async function downloadExampleBitstream(bitstream: {id: number, name: string}) {
|
||||
|
||||
// 直接烧录示例比特流
|
||||
async function programExampleBitstream(bitstream: {id: number, name: string}) {
|
||||
if (isProgramming.value || !props.uploadEvent) return;
|
||||
if (isProgramming.value) return;
|
||||
|
||||
isProgramming.value = true;
|
||||
try {
|
||||
const examClient = AuthManager.createAuthenticatedExamClient();
|
||||
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||
|
||||
// 使用动态API获取比特流文件数据
|
||||
const response = await examClient.getExamResourceById(bitstream.id);
|
||||
|
||||
if (!response || !response.data) {
|
||||
throw new Error('获取比特流文件失败');
|
||||
}
|
||||
|
||||
const file = new File([response.data], response.fileName || bitstream.name, { type: response.data.type });
|
||||
|
||||
// 调用上传事件
|
||||
const uploadSuccess = await props.uploadEvent(file);
|
||||
if (uploadSuccess) {
|
||||
// 如果有下载事件(烧录),则执行
|
||||
if (props.downloadEvent) {
|
||||
const downloadSuccess = await props.downloadEvent();
|
||||
const downloadSuccess = await props.downloadEvent(bitstream.id);
|
||||
if (downloadSuccess) {
|
||||
dialog.info("示例比特流烧录成功");
|
||||
} else {
|
||||
dialog.error("烧录失败");
|
||||
}
|
||||
} else {
|
||||
dialog.info("示例比特流上传成功");
|
||||
}
|
||||
} else {
|
||||
dialog.error("上传失败");
|
||||
dialog.info("示例比特流props.downloadEvent未定义 无法烧录");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('烧录示例比特流失败:', error);
|
||||
@@ -234,6 +218,7 @@ function checkFile(file: File): boolean {
|
||||
}
|
||||
|
||||
async function handleClick(event: Event): Promise<void> {
|
||||
console.log("上传按钮被点击");
|
||||
if (isNull(bitstream.value) || isUndefined(bitstream.value)) {
|
||||
dialog.error(`未选择文件`);
|
||||
return;
|
||||
@@ -246,19 +231,21 @@ async function handleClick(event: Event): Promise<void> {
|
||||
}
|
||||
|
||||
isUploading.value = true;
|
||||
let uploadedBitstreamId: number | null = null;
|
||||
try {
|
||||
const ret = await props.uploadEvent(bitstream.value);
|
||||
console.log("开始上传比特流文件:", bitstream.value.name);
|
||||
const bitstreamId = await props.uploadEvent(bitstream.value, props.examId || '');
|
||||
console.log("上传结果,ID:", bitstreamId);
|
||||
if (isUndefined(props.downloadEvent)) {
|
||||
if (ret) {
|
||||
dialog.info("上传成功");
|
||||
emits("finishedUpload", bitstream.value);
|
||||
} else dialog.error("上传失败");
|
||||
return;
|
||||
}
|
||||
if (!ret) {
|
||||
console.log("上传成功,下载未定义");
|
||||
isUploading.value = false;
|
||||
return;
|
||||
}
|
||||
if (bitstreamId === null || bitstreamId === undefined) {
|
||||
isUploading.value = false;
|
||||
return;
|
||||
}
|
||||
uploadedBitstreamId = bitstreamId;
|
||||
} catch (e) {
|
||||
dialog.error("上传失败");
|
||||
console.error(e);
|
||||
@@ -267,9 +254,14 @@ async function handleClick(event: Event): Promise<void> {
|
||||
|
||||
// Download
|
||||
try {
|
||||
const ret = await props.downloadEvent();
|
||||
console.log("开始下载比特流,ID:", uploadedBitstreamId);
|
||||
if (uploadedBitstreamId === null || uploadedBitstreamId === undefined) {
|
||||
dialog.error("uploadedBitstreamId is null or undefined");
|
||||
} else {
|
||||
const ret = await props.downloadEvent(uploadedBitstreamId);
|
||||
if (ret) dialog.info("下载成功");
|
||||
else dialog.error("下载失败");
|
||||
}
|
||||
} catch (e) {
|
||||
dialog.error("下载失败");
|
||||
console.error(e);
|
||||
|
||||
@@ -22,8 +22,9 @@
|
||||
</div>
|
||||
<div class="divider"></div>
|
||||
<UploadCard
|
||||
:exam-id="props.examId"
|
||||
:upload-event="eqps.jtagUploadBitstream"
|
||||
:download-event="eqps.jtagDownloadBitstream"
|
||||
:download-event="handleDownloadBitstream"
|
||||
:bitstream-file="eqps.jtagBitstream"
|
||||
@update:bitstream-file="handleBitstreamChange"
|
||||
>
|
||||
@@ -127,6 +128,11 @@ function handleBitstreamChange(file: File | undefined) {
|
||||
eqps.jtagBitstream = file;
|
||||
}
|
||||
|
||||
async function handleDownloadBitstream(bitstreamId: number): Promise<boolean> {
|
||||
console.log("开始下载比特流,ID:", bitstreamId);
|
||||
return await eqps.jtagDownloadBitstream(bitstreamId);
|
||||
}
|
||||
|
||||
function handleSelectJtagSpeed(event: Event) {
|
||||
const target = event.target as HTMLSelectElement;
|
||||
eqps.jtagSetSpeed(target.selectedIndex);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ref, reactive, watchPostEffect, onMounted, onUnmounted } from "vue";
|
||||
import { defineStore } from "pinia";
|
||||
import { useLocalStorage } from "@vueuse/core";
|
||||
import { isString, toNumber, type Dictionary } from "lodash";
|
||||
import { isString, toNumber, isUndefined, type Dictionary } from "lodash";
|
||||
import z from "zod";
|
||||
import { isNumber } from "mathjs";
|
||||
import { Mutex, withTimeout } from "async-mutex";
|
||||
@@ -9,8 +9,10 @@ import { useConstraintsStore } from "@/stores/constraints";
|
||||
import { useDialogStore } from "./dialog";
|
||||
import { toFileParameterOrUndefined } from "@/utils/Common";
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
import { HubConnectionBuilder } from "@microsoft/signalr";
|
||||
import { HubConnection, HubConnectionBuilder } from "@microsoft/signalr";
|
||||
import { getHubProxyFactory, getReceiverRegister } from "@/TypedSignalR.Client";
|
||||
import type { ResourceInfo } from "@/APIClient";
|
||||
import type { IJtagHub } from "@/TypedSignalR.Client/server.Hubs.JtagHub";
|
||||
|
||||
export const useEquipments = defineStore("equipments", () => {
|
||||
// Global Stores
|
||||
@@ -23,27 +25,38 @@ export const useEquipments = defineStore("equipments", () => {
|
||||
// Jtag
|
||||
const jtagBitstream = ref<File>();
|
||||
const jtagBoundaryScanFreq = ref(100);
|
||||
const jtagUserBitstreams = ref<ResourceInfo[]>([]);
|
||||
const jtagClientMutex = withTimeout(
|
||||
new Mutex(),
|
||||
1000,
|
||||
new Error("JtagClient Mutex Timeout!"),
|
||||
);
|
||||
const jtagHubConnection = new HubConnectionBuilder()
|
||||
.withUrl("http://localhost:5000/hubs/JtagHub")
|
||||
.withAutomaticReconnect()
|
||||
.build();
|
||||
const jtagHubProxy =
|
||||
getHubProxyFactory("IJtagHub").createHubProxy(jtagHubConnection);
|
||||
const jtagHubSubscription = getReceiverRegister("IJtagReceiver").register(
|
||||
jtagHubConnection,
|
||||
{
|
||||
const jtagHubConnection = ref<HubConnection>();
|
||||
const jtagHubProxy = ref<IJtagHub>();
|
||||
|
||||
onMounted(async () => {
|
||||
// 每次挂载都重新创建连接
|
||||
jtagHubConnection.value =
|
||||
AuthManager.createAuthenticatedJtagHubConnection();
|
||||
jtagHubProxy.value = getHubProxyFactory("IJtagHub").createHubProxy(
|
||||
jtagHubConnection.value,
|
||||
);
|
||||
|
||||
getReceiverRegister("IJtagReceiver").register(jtagHubConnection.value, {
|
||||
onReceiveBoundaryScanData: async (msg) => {
|
||||
constrainsts.batchSetConstraintStates(msg);
|
||||
},
|
||||
},
|
||||
);
|
||||
onMounted(() => {
|
||||
});
|
||||
await jtagHubConnection.value.start();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
// 断开连接,清理资源
|
||||
if (jtagHubConnection.value) {
|
||||
jtagHubConnection.value.stop();
|
||||
jtagHubConnection.value = undefined;
|
||||
jtagHubProxy.value = undefined;
|
||||
}
|
||||
});
|
||||
|
||||
// Matrix Key
|
||||
@@ -87,34 +100,61 @@ export const useEquipments = defineStore("equipments", () => {
|
||||
}
|
||||
|
||||
async function jtagBoundaryScanSetOnOff(enable: boolean) {
|
||||
jtagHubConnection.start();
|
||||
enableJtagBoundaryScan.value = enable;
|
||||
if (enable) {
|
||||
jtagHubProxy.startBoundaryScan(jtagBoundaryScanFreq.value);
|
||||
} else {
|
||||
jtagHubProxy.stopBoundaryScan();
|
||||
}
|
||||
if (isUndefined(jtagHubProxy.value)) {
|
||||
console.error("JtagHub Not Initialize...");
|
||||
return;
|
||||
}
|
||||
|
||||
async function jtagUploadBitstream(bitstream: File): Promise<boolean> {
|
||||
if (enable) {
|
||||
const ret = await jtagHubProxy.value.startBoundaryScan(
|
||||
jtagBoundaryScanFreq.value,
|
||||
);
|
||||
if (!ret) {
|
||||
console.error("Failed to start boundary scan");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
const ret = await jtagHubProxy.value.stopBoundaryScan();
|
||||
if (!ret) {
|
||||
console.error("Failed to stop boundary scan");
|
||||
return;
|
||||
}
|
||||
}
|
||||
enableJtagBoundaryScan.value = enable;
|
||||
}
|
||||
|
||||
async function jtagUploadBitstream(bitstream: File, examId?: string): Promise<number | null> {
|
||||
try {
|
||||
// 自动开启电源
|
||||
await powerSetOnOff(true);
|
||||
|
||||
const jtagClient = AuthManager.createAuthenticatedJtagClient();
|
||||
const resp = await jtagClient.uploadBitstream(
|
||||
boardAddr.value,
|
||||
const resourceClient = AuthManager.createAuthenticatedResourceClient();
|
||||
const resp = await resourceClient.addResource(
|
||||
'bitstream',
|
||||
'user',
|
||||
examId || null,
|
||||
toFileParameterOrUndefined(bitstream),
|
||||
);
|
||||
return resp;
|
||||
|
||||
// 如果上传成功,设置为当前选中的比特流
|
||||
if (resp && resp.id !== undefined && resp.id !== null) {
|
||||
return resp.id;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (e) {
|
||||
dialog.error("上传错误");
|
||||
console.error(e);
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function jtagDownloadBitstream(): Promise<boolean> {
|
||||
async function jtagDownloadBitstream(bitstreamId?: number): Promise<boolean> {
|
||||
if (bitstreamId === null || bitstreamId === undefined) {
|
||||
dialog.error("请先选择要下载的比特流");
|
||||
return false;
|
||||
}
|
||||
|
||||
const release = await jtagClientMutex.acquire();
|
||||
try {
|
||||
// 自动开启电源
|
||||
@@ -124,10 +164,11 @@ export const useEquipments = defineStore("equipments", () => {
|
||||
const resp = await jtagClient.downloadBitstream(
|
||||
boardAddr.value,
|
||||
boardPort.value,
|
||||
bitstreamId,
|
||||
);
|
||||
return resp;
|
||||
} catch (e) {
|
||||
dialog.error("上传错误");
|
||||
dialog.error("下载错误");
|
||||
console.error(e);
|
||||
return false;
|
||||
} finally {
|
||||
@@ -256,6 +297,7 @@ export const useEquipments = defineStore("equipments", () => {
|
||||
jtagBoundaryScanSetOnOff,
|
||||
jtagBitstream,
|
||||
jtagBoundaryScanFreq,
|
||||
jtagUserBitstreams,
|
||||
jtagUploadBitstream,
|
||||
jtagDownloadBitstream,
|
||||
jtagGetIDCode,
|
||||
|
||||
@@ -14,8 +14,12 @@ import {
|
||||
OscilloscopeApiClient,
|
||||
DebuggerClient,
|
||||
ExamClient,
|
||||
ResourceClient,
|
||||
} from "@/APIClient";
|
||||
import router from "@/router";
|
||||
import { HubConnectionBuilder } from "@microsoft/signalr";
|
||||
import axios, { type AxiosInstance } from "axios";
|
||||
import { isNull } from "lodash";
|
||||
|
||||
// 支持的客户端类型联合类型
|
||||
type SupportedClient =
|
||||
@@ -33,7 +37,8 @@ type SupportedClient =
|
||||
| NetConfigClient
|
||||
| OscilloscopeApiClient
|
||||
| DebuggerClient
|
||||
| ExamClient;
|
||||
| ExamClient
|
||||
| ResourceClient;
|
||||
|
||||
export class AuthManager {
|
||||
// 存储token到localStorage
|
||||
@@ -117,7 +122,7 @@ export class AuthManager {
|
||||
if (!token) return null;
|
||||
|
||||
const instance = axios.create();
|
||||
instance.interceptors.request.use(config => {
|
||||
instance.interceptors.request.use((config) => {
|
||||
config.headers = config.headers || {};
|
||||
(config.headers as any)["Authorization"] = `Bearer ${token}`;
|
||||
return config;
|
||||
@@ -196,6 +201,25 @@ export class AuthManager {
|
||||
return AuthManager.createAuthenticatedClient(ExamClient);
|
||||
}
|
||||
|
||||
public static createAuthenticatedResourceClient(): ResourceClient {
|
||||
return AuthManager.createAuthenticatedClient(ResourceClient);
|
||||
}
|
||||
|
||||
public static createAuthenticatedJtagHubConnection() {
|
||||
const token = this.getToken();
|
||||
if (isNull(token)) {
|
||||
router.push("/login");
|
||||
throw Error("Token Null!");
|
||||
}
|
||||
|
||||
return new HubConnectionBuilder()
|
||||
.withUrl("http://127.0.0.1:5000/hubs/JtagHub", {
|
||||
accessTokenFactory: () => token,
|
||||
})
|
||||
.withAutomaticReconnect()
|
||||
.build();
|
||||
}
|
||||
|
||||
// 登录函数
|
||||
public static async login(
|
||||
username: string,
|
||||
|
||||
@@ -679,15 +679,15 @@ const downloadResources = async () => {
|
||||
downloadingResources.value = true
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedExamClient()
|
||||
const resourceClient = AuthManager.createAuthenticatedResourceClient()
|
||||
|
||||
// 获取资源包列表
|
||||
const resourceList = await client.getExamResourceList(selectedExam.value.id, 'resource')
|
||||
// 获取资源包列表(模板资源)
|
||||
const resourceList = await resourceClient.getResourceList(selectedExam.value.id, 'resource', 'template')
|
||||
|
||||
if (resourceList && resourceList.length > 0) {
|
||||
// 使用动态API获取第一个资源包
|
||||
// 使用新的ResourceClient API获取第一个资源包
|
||||
const resourceId = resourceList[0].id
|
||||
const fileResponse = await client.getExamResourceById(resourceId)
|
||||
const fileResponse = await resourceClient.getResourceById(resourceId)
|
||||
|
||||
// 创建Blob URL
|
||||
const blobUrl = URL.createObjectURL(fileResponse.data)
|
||||
@@ -925,7 +925,7 @@ const submitCreateExam = async () => {
|
||||
|
||||
// 上传实验资源
|
||||
const uploadExamResources = async (examId: string) => {
|
||||
const client = AuthManager.createAuthenticatedExamClient()
|
||||
const client = AuthManager.createAuthenticatedResourceClient()
|
||||
|
||||
try {
|
||||
// 上传MD文档
|
||||
@@ -934,7 +934,7 @@ const uploadExamResources = async (examId: string) => {
|
||||
data: uploadFiles.value.mdFile,
|
||||
fileName: uploadFiles.value.mdFile.name
|
||||
}
|
||||
await client.addExamResource(examId, 'doc', mdFileParam)
|
||||
await client.addResource('doc', 'template', examId, mdFileParam)
|
||||
console.log('MD文档上传成功')
|
||||
}
|
||||
|
||||
@@ -944,7 +944,7 @@ const uploadExamResources = async (examId: string) => {
|
||||
data: imageFile,
|
||||
fileName: imageFile.name
|
||||
}
|
||||
await client.addExamResource(examId, 'image', imageFileParam)
|
||||
await client.addResource('image', 'template', examId, imageFileParam)
|
||||
console.log('图片上传成功:', imageFile.name)
|
||||
}
|
||||
|
||||
@@ -954,7 +954,7 @@ const uploadExamResources = async (examId: string) => {
|
||||
data: bitstreamFile,
|
||||
fileName: bitstreamFile.name
|
||||
}
|
||||
await client.addExamResource(examId, 'bitstream', bitstreamFileParam)
|
||||
await client.addResource('bitstream', 'template', examId, bitstreamFileParam)
|
||||
console.log('比特流文件上传成功:', bitstreamFile.name)
|
||||
}
|
||||
|
||||
@@ -964,7 +964,7 @@ const uploadExamResources = async (examId: string) => {
|
||||
data: canvasFile,
|
||||
fileName: canvasFile.name
|
||||
}
|
||||
await client.addExamResource(examId, 'canvas', canvasFileParam)
|
||||
await client.addResource('canvas', 'template', examId, canvasFileParam)
|
||||
console.log('画布模板上传成功:', canvasFile.name)
|
||||
}
|
||||
|
||||
@@ -974,7 +974,7 @@ const uploadExamResources = async (examId: string) => {
|
||||
data: uploadFiles.value.resourceFile,
|
||||
fileName: uploadFiles.value.resourceFile.name
|
||||
}
|
||||
await client.addExamResource(examId, 'resource', resourceFileParam)
|
||||
await client.addResource('resource', 'template', examId, resourceFileParam)
|
||||
console.log('资源包上传成功')
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
<!-- 拖拽分割线 -->
|
||||
<SplitterResizeHandle
|
||||
id="splitter-group-h-resize-handle"
|
||||
class="w-2 bg-base-100 hover:bg-primary hover:opacity-70 transition-colors"
|
||||
class="w-1 bg-base-300"
|
||||
/>
|
||||
<!-- 右侧编辑区域 -->
|
||||
<SplitterPanel
|
||||
@@ -74,7 +74,7 @@
|
||||
<SplitterResizeHandle
|
||||
v-show="!isBottomBarFullscreen"
|
||||
id="splitter-group-v-resize-handle"
|
||||
class="h-2 bg-base-100 hover:bg-primary hover:opacity-70 transition-colors"
|
||||
class="h-1 bg-base-300"
|
||||
/>
|
||||
|
||||
<!-- 功能底栏 -->
|
||||
@@ -217,17 +217,17 @@ async function loadDocumentContent() {
|
||||
if (examId) {
|
||||
// 如果有实验ID,从API加载实验文档
|
||||
console.log('加载实验文档:', examId);
|
||||
const client = AuthManager.createAuthenticatedExamClient();
|
||||
const client = AuthManager.createAuthenticatedResourceClient();
|
||||
|
||||
// 获取markdown类型的资源列表
|
||||
const resources = await client.getExamResourceList(examId, 'doc');
|
||||
// 获取markdown类型的模板资源列表
|
||||
const resources = await client.getResourceList(examId, 'doc', 'template');
|
||||
|
||||
if (resources && resources.length > 0) {
|
||||
// 获取第一个markdown资源
|
||||
const markdownResource = resources[0];
|
||||
|
||||
// 使用动态API获取资源文件内容
|
||||
const response = await client.getExamResourceById(markdownResource.id);
|
||||
// 使用新的ResourceClient API获取资源文件内容
|
||||
const response = await client.getResourceById(markdownResource.id);
|
||||
|
||||
if (!response || !response.data) {
|
||||
throw new Error('获取markdown文件失败');
|
||||
|
||||
@@ -53,10 +53,6 @@ export default defineConfig({
|
||||
target: "http://localhost:5000",
|
||||
changeOrigin: true,
|
||||
},
|
||||
"/hubs": {
|
||||
target: "http://localhost:5000",
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
port: 5173,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user