diff --git a/Model/Entity/ApiEntity/HttpSendResult.cs b/Model/Entity/ApiEntity/HttpSendResult.cs new file mode 100644 index 0000000..0821e1d --- /dev/null +++ b/Model/Entity/ApiEntity/HttpSendResult.cs @@ -0,0 +1,10 @@ +using SparkClient.Model.Services; + +namespace SparkClient.Model.Entity.ApiEntity; + +public class HttpSendResult +{ + public int StatusCode { get; set; } = 500; + public string Message { get; set; } + public ResponseStatus Content { get; set; } +} \ No newline at end of file diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index 8e85136..6526be2 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -11,6 +11,7 @@ using HandyControl.Tools.Extension; using log4net; using SparkClient.Model.Common; using SparkClient.Model.Entity.ApiEntity; +using SparkClient.Model.Extension; using SparkClient.Model.Helper; namespace SparkClient.Model.Services @@ -62,7 +63,7 @@ namespace SparkClient.Model.Services /// /// 请求的完整URL /// HTTP响应 - private async Task SendGetRequestAsync(string url) + private async Task SendGetRequestAsync(string url) { using (var client = new HttpClient()) { @@ -80,20 +81,23 @@ namespace SparkClient.Model.Services if (StatusCodes.PreviousTaskIncomplete.Equals(tempEntity.Status)) { Logger.Info($"S009 请求重试"); - await Task.Delay(50); + await Task.Delay(500); return await SendGetRequestAsync(url); } - result.Content = new StringContent(responseBody); - return result; + + return new HttpSendResult() + { + StatusCode = statusCode, + Content = tempEntity, + }; } } private async Task SendGetRequestImageAsync(string url) { using (var client = new HttpClient()) { - Logger.Info($"Request sent to URL: {url}"); + Logger.Info($"Request Image Download URL: {url}"); client.DefaultRequestHeaders.Add("Authorization", "Basic " + _authToken); - return await client.GetAsync(url); } @@ -139,19 +143,18 @@ namespace SparkClient.Model.Services var response = await SendGetRequestAsync(url); - if (!response.IsSuccessStatusCode) + if (response.StatusCode != 200) { return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List() }; } - var jsonResponse = await response.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject(jsonResponse); + var result = response.Content; if (result == null) { return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List() }; } - + return new SocResultEntity { Status = result.Status, Images = new List(), DeviceId = result.device_id }; @@ -171,13 +174,71 @@ namespace SparkClient.Model.Services GenImage = false; } } + /// + /// 启动图片收集任务。 + /// + /// 任务状态 + public Task CollectImagesAsyncNotAwait() + { + try + { + GenImage = true; + // 光照度和半圆 + int lightLevel = 0; + string halfCircle = string.Empty; + // 查询光照度和半圆配置 + string sql = $"SELECT KEY, VALUE FROM CUTTER_CONFIG WHERE KEY IN ('light_level', 'half_circle')"; + DataTable table = DataBaseHelper.ExecuteQuery(sql); + + if (table == null || table.Rows.Count == 0) + { + throw new Exception("No data found for the specified keys."); + } + + foreach (DataRow row in table.Rows) + { + string key = row["Key"].ToString() ?? string.Empty; + string value = row["Value"].ToString() ?? string.Empty; + + if (key == "light_level" && int.TryParse(value, out int parsedLightLevel)) + { + lightLevel = parsedLightLevel; // 光照度 + } + else if (key == "half_circle") + { + halfCircle = value; // 半圆 + } + } + + string url = $"{_baseUrl}/collect_images?light_level={lightLevel}&half_circle={halfCircle}"; + + var result = SendGetRequestAsync(url); + + return result; + + + } + catch (Exception ex) + { + // 记录日志或进行其他处理 + Console.WriteLine($"Error in DoSoc: {ex.Message}"); + Logger.Warn($"Error in DoSoc: {ex.Message}"); + // 或者使用日志框架记录日志 + // logger.LogError(ex, "Error in DoSoc method."); + return null; + } + finally + { + GenImage = false; + } + } /// /// 获取指定索引的图片。 /// /// 保存图片路径 /// 图片的字节数组 - public async Task> RetrieveImageAsync(string? savePath) + public async Task> RetrieveImageAsync(string? savePath, CancellationToken token = default) { List imageNames = new List(); // 读取图片接口 @@ -189,7 +250,7 @@ namespace SparkClient.Model.Services { var response = await SendGetRequestImageAsync(url); int status = (int)response.StatusCode; - + token.ThrowIfCancellationRequested(); switch (status) { case 200: @@ -208,7 +269,7 @@ namespace SparkClient.Model.Services break; case 425: // 请求被锁定,等待一段时间后重试 - await Task.Delay(1000); + await Task.Delay(500); break; case 410: // 资源已被永久删除,跳过当前索引 @@ -252,11 +313,11 @@ namespace SparkClient.Model.Services { var response = await SendGetRequestAsync(url); Logger.Debug($" CollectStatusAsync url :{ url} "); - if (response.IsSuccessStatusCode) + if (response.StatusCode == 200) { - var jsonResponse = await response.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject(jsonResponse); - Logger.Debug($" CollectStatusAsync result :{ result} "); + + ResponseStatus result = response.Content; + Logger.Debug($" CollectStatusAsync result :{ result.ToSafeAbundantString()} "); return result.Status; } @@ -292,18 +353,49 @@ namespace SparkClient.Model.Services Directory.Delete(savePath, true); } Directory.CreateDirectory(savePath); + + // 启动任务接口 - SocResultEntity entity = await CollectImagesAsync(); + // SocResultEntity entity = await CollectImagesAsync(); // 成功 - Logger.Debug($"entity :{entity.Status} {entity.ToString()} "); - if (entity.Status != StatusCodes.Success) + // Logger.Debug($"entity :{entity.Status} {entity.ToString()} "); + // if (entity.Status != StatusCodes.Success) + // { + // // 启动任务失败 + // return new SocResultEntity { Status = entity.Status, Images = new List() }; + // } + + // 读取图片接口 + // List imageNames = await RetrieveImageAsync(savePath); + var cts = new CancellationTokenSource(); + Task startImage = CollectImagesAsyncNotAwait(); + Task> downloadImage = RetrieveImageAsync(savePath, cts.Token); + await startImage; + var entity = startImage.Result; + if (entity.StatusCode != 200) { - // 启动任务失败 - return new SocResultEntity { Status = entity.Status, Images = new List() }; + //downloadImage怎么取消并终止 + cts.Cancel(); + return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List() }; + } + if (entity.Content == null) + { + //downloadImage怎么取消并终止 + cts.Cancel(); + return new SocResultEntity { Status = StatusCodes.DeviceNotFound, Images = new List() }; + } + + List imageNames = new List(); + try + { + await downloadImage; + imageNames = downloadImage.Result; + } + catch (Exception e) + { + Logger.Error($"Error in Await downloadImage: {e.Message}"); } - // 读取图片接口 - List imageNames = await RetrieveImageAsync(savePath); // 采集状态接口 string acquisitionStatus = await CollectStatusAsync(); // 成功 @@ -319,7 +411,7 @@ namespace SparkClient.Model.Services // 图片文件读取失败 return new SocResultEntity { Status = StatusCodes.ImageFileReadFailure, Images = new List() }; } - return new SocResultEntity { Status = acquisitionStatus, Images = imageNames, DeviceId = entity.DeviceId}; + return new SocResultEntity { Status = acquisitionStatus, Images = imageNames, DeviceId = entity.Content.device_id}; } catch (Exception e) { @@ -363,11 +455,10 @@ namespace SparkClient.Model.Services { var response = await SendGetRequestAsync(url); Logger.Debug(url); - if (response.IsSuccessStatusCode) + if (response.StatusCode == 200) { - var jsonResponse = await response.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject(jsonResponse); - Logger.Debug(result); + var result = response.Content; + Logger.Debug($"Rotate Angle : {result.ToSafeAbundantString()} "); return result.Status; } @@ -397,11 +488,10 @@ namespace SparkClient.Model.Services { var response = await SendGetRequestAsync(url); - if (response.IsSuccessStatusCode) + if (response.StatusCode == 200) { - var jsonResponse = await response.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject(jsonResponse); - Logger.Debug(result.Status); + var result = response.Content; + Logger.Debug($"Set Pump : {result.ToSafeAbundantString()} "); return result.Status; } Logger.Info($"气泵开关请求发起完毕"); diff --git a/SparkClient.sln.DotSettings.user b/SparkClient.sln.DotSettings.user index 018b7c3..bbe3669 100644 --- a/SparkClient.sln.DotSettings.user +++ b/SparkClient.sln.DotSettings.user @@ -1,9 +1,11 @@  True True + True ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -20,6 +22,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -28,6 +31,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -39,8 +43,12 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded + ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -54,11 +62,14 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded diff --git a/ViewModel/Grading/GradingResultVM.cs b/ViewModel/Grading/GradingResultVM.cs index 384a6a2..cac8c44 100644 --- a/ViewModel/Grading/GradingResultVM.cs +++ b/ViewModel/Grading/GradingResultVM.cs @@ -1674,7 +1674,7 @@ public class GradingResultVM : BaseViewModel result = RoundToPointTwo(value).ToString("f1"); // 保留1位小数 break; case Accuracy.IntegerFloor: - result = Math.Floor(value).ToString(); // 直接取整 + result = Math.Floor(value).ToString("F1"); // 直接取整 break; } } @@ -1689,11 +1689,9 @@ public class GradingResultVM : BaseViewModel case Accuracy.HalfStepRounding: case Accuracy.ForceOneDecimal: case Accuracy.DecimalTwoStepRounding: - result = (Math.Floor(value * 10) / 10).ToString("f1"); - break; case Accuracy.IntegerFloor: case Accuracy.MultipleOfFive: - result = Math.Floor(value).ToString(); // 直接取整 + result = (Math.Floor(value * 10) / 10).ToString("f1"); break; } }