fix: 修复示波器WebSocket的问题

This commit is contained in:
SikongJueluo 2025-08-20 15:28:58 +08:00
parent ec84eeeaa4
commit f23a8a9712
1 changed files with 37 additions and 19 deletions

View File

@ -117,7 +117,7 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
try
{
var board = TryGetBoard().OrThrow(() => new Exception("Board not found"));
var client = new OscilloscopeCtrl(board.IpAddr, board.Port, 0);
var client = new OscilloscopeCtrl(board.IpAddr, board.Port);
return client;
}
catch (Exception ex)
@ -148,6 +148,25 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
}
}
private Task ScanTask(OscilloscopeScanTaskInfo taskInfo, CancellationToken token)
{
return Task.Run(async () =>
{
while (!token.IsCancellationRequested)
{
var data = await GetCaptureData(taskInfo.Client);
if (data == null)
{
logger.Error("GetData failed");
continue;
}
await _hubContext.Clients.Client(Context.ConnectionId).OnDataReceived(data);
await Task.Delay(1000 / taskInfo.Frequency, token);
}
}, token);
}
public async Task<bool> StartCapture()
{
try
@ -168,21 +187,7 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
var scanTaskInfo = new OscilloscopeScanTaskInfo(client);
var token = scanTaskInfo.CTS.Token;
scanTaskInfo.ScanTask = Task.Run(async () =>
{
while (!token.IsCancellationRequested)
{
var data = await GetData();
if (data == null)
{
logger.Error("GetData failed");
continue;
}
await Clients.Client(Context.ConnectionId).OnDataReceived(data);
await Task.Delay(1000 / scanTaskInfo.Frequency, token);
}
}, token);
scanTaskInfo.ScanTask = ScanTask(scanTaskInfo, token);
_scanTasks[key] = scanTaskInfo;
@ -229,12 +234,10 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
}
}
public async Task<OscilloscopeDataResponse?> GetData()
private async Task<OscilloscopeDataResponse?> GetCaptureData(OscilloscopeCtrl oscilloscope)
{
try
{
var oscilloscope = GetOscilloscope().OrThrow(() => new Exception("用户未绑定有效的实验板"));
var freqResult = await oscilloscope.GetADFrequency();
var vppResult = await oscilloscope.GetADVpp();
var maxResult = await oscilloscope.GetADMax();
@ -296,6 +299,21 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
}
}
public async Task<OscilloscopeDataResponse?> GetData()
{
try
{
var oscilloscope = GetOscilloscope().OrThrow(() => new Exception("Oscilloscope not found"));
var response = await GetCaptureData(oscilloscope);
return response;
}
catch (Exception ex)
{
logger.Error(ex, "获取示波器数据时发生异常");
return null;
}
}
public async Task<bool> SetTrigger(byte level)
{
try