feat: 完成七段数码管后端

This commit is contained in:
2025-08-14 15:21:15 +08:00
parent 0e07a5996a
commit 7bfc362b1f
13 changed files with 189 additions and 58 deletions

View File

@@ -192,11 +192,6 @@ public class HttpHdmiVideoStreamService : BackgroundService
}
var hdmiInToken = _clientDict[boardId].CTS.Token;
if (hdmiInToken == null)
{
await SendErrorAsync(context.Response, "HDMI input is not available");
return;
}
if (path == "/snapshot")
{

View File

@@ -39,12 +39,12 @@ public class ProgressReporter : ProgressInfo, IProgress<int>
public ProgressReporter? Child { get; set; }
private ProgressStatus _status = ProgressStatus.Pending;
private string _errorMessage;
private string _errorMessage = string.Empty;
public string TaskId { get; set; } = Guid.NewGuid().ToString();
public int ProgressPercent => _progress * 100 / MaxProgress;
public ProgressStatus Status => _status;
public string ErrorMessage => _errorMessage;
public override string TaskId { get; } = Guid.NewGuid().ToString();
public override int ProgressPercent => _progress * 100 / MaxProgress;
public override ProgressStatus Status => _status;
public override string ErrorMessage => _errorMessage;
public ProgressReporter(Func<int, Task>? reporter = null, int initProgress = 0, int maxProgress = 100, int step = 1)
{
@@ -86,7 +86,7 @@ public class ProgressReporter : ProgressInfo, IProgress<int>
}
}
public async void Report(int value)
public void Report(int value)
{
if (this._status == ProgressStatus.Pending)
this._status = ProgressStatus.InProgress;
@@ -153,7 +153,7 @@ public class ProgressTrackerService : BackgroundService
private class TaskProgressInfo
{
public ProgressReporter Reporter { get; set; }
public ProgressReporter? Reporter { get; set; }
public string? ConnectionId { get; set; }
public required CancellationToken CancellationToken { get; set; }
public required CancellationTokenSource CancellationTokenSource { get; set; }
@@ -176,10 +176,15 @@ public class ProgressTrackerService : BackgroundService
{
var info = kvp.Value;
// 超过 1 分钟且任务已完成/失败/取消
if ((now - info.UpdatedAt).TotalMinutes > 1 &&
(info.Reporter.Status == ProgressStatus.Completed ||
info.Reporter.Status == ProgressStatus.Failed ||
info.Reporter.Status == ProgressStatus.Canceled))
if (
(now - info.UpdatedAt).TotalMinutes > 1 &&
info.Reporter != null &&
(
info.Reporter.Status == ProgressStatus.Completed ||
info.Reporter.Status == ProgressStatus.Failed ||
info.Reporter.Status == ProgressStatus.Canceled
)
)
{
_taskMap.TryRemove(kvp.Key, out _);
logger.Info($"Cleaned up task {kvp.Key}");
@@ -219,7 +224,7 @@ public class ProgressTrackerService : BackgroundService
cancellationTokenSource.Token.ThrowIfCancellationRequested();
// 通过 SignalR 推送进度
if (progressInfo.ConnectionId != null)
if (progressInfo.ConnectionId != null && progressInfo.Reporter != null)
await _hubContext.Clients.Client(progressInfo.ConnectionId).OnReceiveProgress(progressInfo.Reporter);
});
@@ -243,7 +248,8 @@ public class ProgressTrackerService : BackgroundService
{
if (_taskMap.TryGetValue(taskId, out var info))
{
return info.Reporter.Status;
if (info.Reporter != null)
return info.Reporter.Status;
}
return Optional<ProgressStatus>.None;
}
@@ -265,7 +271,7 @@ public class ProgressTrackerService : BackgroundService
{
try
{
if (_taskMap.TryGetValue(taskId, out var info) && info != null)
if (_taskMap.TryGetValue(taskId, out var info) && info != null && info.Reporter != null)
{
lock (info)
{