fix: 请求重试机制

master
Tongg 4 months ago
parent 62a7fa8b5e
commit 1a199f8d85
  1. 23
      Model/Services/SOCClientService.cs
  2. 18
      ViewModel/Grading/GradingLoadingVM.cs

@ -74,15 +74,30 @@ namespace SparkClient.Model.Services
// 提前读取内容并存储 // 提前读取内容并存储
string responseBody = await result.Content.ReadAsStringAsync(); string responseBody = await result.Content.ReadAsStringAsync();
int statusCode = (int)result.StatusCode; int statusCode = (int)result.StatusCode;
// 记录日志 // 记录日志
Logger.Info($"Response: Status={statusCode}, Body={responseBody}"); Logger.Info($"Response: Status={statusCode}, Body={responseBody}");
var tempEntity = JsonConvert.DeserializeObject<ResponseStatus>(responseBody);
if ("S007".Equals(tempEntity.Status))
{
Logger.Info($"S007 请求重试");
await Task.Delay(50);
return await SendGetRequestAsync(url);
}
result.Content = new StringContent(responseBody); result.Content = new StringContent(responseBody);
return result; return result;
} }
} }
private async Task<HttpResponseMessage> SendGetRequestImageAsync(string url)
{
using (var client = new HttpClient())
{
Logger.Info($"Request sent to URL: {url}");
client.DefaultRequestHeaders.Add("Authorization", "Basic " + _authToken);
return await client.GetAsync(url);
}
}
/// <summary> /// <summary>
/// 启动图片收集任务。 /// 启动图片收集任务。
@ -172,7 +187,7 @@ namespace SparkClient.Model.Services
string url = $"{_baseUrl}/retrieve_image/{imageIndex}"; string url = $"{_baseUrl}/retrieve_image/{imageIndex}";
try try
{ {
var response = await SendGetRequestAsync(url); var response = await SendGetRequestImageAsync(url);
int status = (int)response.StatusCode; int status = (int)response.StatusCode;
switch (status) switch (status)

@ -24,7 +24,6 @@ namespace SparkClient.ViewModel.Grading;
public class GradingLoadingVM : BaseViewModel,IDisposable public class GradingLoadingVM : BaseViewModel,IDisposable
{ {
private double _progress; private double _progress;
private SOCClientService _socClientService;
public AlgorithmResultEntity Parameter; public AlgorithmResultEntity Parameter;
/// <summary> /// <summary>
/// 进度 /// 进度
@ -134,6 +133,7 @@ public class GradingLoadingVM : BaseViewModel,IDisposable
/// <summary> /// <summary>
/// 开始检测 /// 开始检测
/// </summary> /// </summary>
//[Log]
public async Task<int> Start(int type = 0) public async Task<int> Start(int type = 0)
{ {
@ -168,15 +168,17 @@ public class GradingLoadingVM : BaseViewModel,IDisposable
if(type == 0) if(type == 0)
{ {
string openpupmStatus = await SOCClientService.Service.OpenPump(true);
if (!StatusCodes.Success.Equals(openpupmStatus))
{
new MessageBox().Show("气泵开启失败!");
return -1;
}
var processImage = _socClientService.ProcessImageCollectionAsync(); await SOCClientService.Service.OpenPump(true);
// if (!StatusCodes.Success.Equals(openpupmStatus))
// {
// new MessageBox().Show("气泵开启失败!");
// return -1;
// }
var processImage = SOCClientService.Service.ProcessImageCollectionAsync();
//通知页面可以播放图片 //通知页面可以播放图片
await processImage; await processImage;
if (!("ok".Equals(processImage.Result.Status) || "S000".Equals(processImage.Result.Status))) if (!("ok".Equals(processImage.Result.Status) || "S000".Equals(processImage.Result.Status)))

Loading…
Cancel
Save