From 62a7fa8b5edf10623782b979daef502566b10562 Mon Sep 17 00:00:00 2001 From: Tongg Date: Fri, 7 Mar 2025 17:00:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=203.1=E6=8E=92=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Model/Services/SOCClientService.cs | 49 ++++++++++++++++++++++----- ViewModel/BaseWindow/BaseControlVM.cs | 5 ++- ViewModel/Grading/GradingLoadingVM.cs | 6 ++-- Views/UserControl/Viewport3D.xaml.cs | 3 +- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Model/Services/SOCClientService.cs b/Model/Services/SOCClientService.cs index 1f1f165..fa87ed4 100644 --- a/Model/Services/SOCClientService.cs +++ b/Model/Services/SOCClientService.cs @@ -35,12 +35,23 @@ namespace SparkClient.Model.Services /// private readonly string _authToken; + private static SOCClientService _service; + + public static SOCClientService Service { + get + { + if (_service == null) + _service = new SOCClientService(); + return _service; + } + + } private bool GenImage; /// /// 构造函数,初始化基础URL和认证令牌。 /// - public SOCClientService() + private SOCClientService() { _baseUrl = ConfigurationManager.AppSettings["BaseUrl"]; _authToken = "your_basic_auth_token"; @@ -55,8 +66,21 @@ namespace SparkClient.Model.Services { using (var client = new HttpClient()) { + Logger.Info($"Request sent to URL: {url}"); client.DefaultRequestHeaders.Add("Authorization", "Basic " + _authToken); - return await client.GetAsync(url); + + HttpResponseMessage result = await client.GetAsync(url); + + // 提前读取内容并存储 + string responseBody = await result.Content.ReadAsStringAsync(); + int statusCode = (int)result.StatusCode; + + // 记录日志 + Logger.Info($"Response: Status={statusCode}, Body={responseBody}"); + + result.Content = new StringContent(responseBody); + + return result; } } @@ -212,11 +236,12 @@ namespace SparkClient.Model.Services try { var response = await SendGetRequestAsync(url); - + Logger.Debug($" CollectStatusAsync url :{ url} "); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(jsonResponse); + Logger.Debug($" CollectStatusAsync result :{ result} "); return result.Status; } @@ -255,6 +280,7 @@ namespace SparkClient.Model.Services // 启动任务接口 SocResultEntity entity = await CollectImagesAsync(); // 成功 + Logger.Debug($"entity :{entity.Status} {entity.ToString()} "); if (entity.Status != StatusCodes.Success) { // 启动任务失败 @@ -268,11 +294,13 @@ namespace SparkClient.Model.Services // 成功 if (acquisitionStatus != StatusCodes.Success) { + Logger.Debug($"acquisitionStatus != StatusCodes.Success : {acquisitionStatus}"); // 采集状态失败 return new SocResultEntity { Status = acquisitionStatus, Images = new List() }; } if (imageNames.Count == 0) { + Logger.Debug("imageNames.Count == 0"); // 图片文件读取失败 return new SocResultEntity { Status = StatusCodes.ImageFileReadFailure, Images = new List() }; } @@ -311,18 +339,20 @@ namespace SparkClient.Model.Services public async Task CutRevolve(double angle) { + // await OpenPump(true); int param = (int)angle * 100; string url = $"{_baseUrl}/rotate_to?angle={param}"; - if(GenImage) return StatusCodes.DeviceNotFound; + // if(GenImage) return StatusCodes.DeviceNotFound; GenImage = true; try { var response = await SendGetRequestAsync(url); - + Logger.Debug(url); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(jsonResponse); + Logger.Debug(result); return result.Status; } @@ -337,14 +367,16 @@ namespace SparkClient.Model.Services finally { GenImage = false; + // await OpenPump(true); } } - public async Task OpenPump(bool isOpen = true) + public async Task OpenPump(bool isOpen) { - if(GenImage) return StatusCodes.DeviceNotFound; - int param = isOpen? 1 : 0 ; + // if(GenImage) return StatusCodes.DeviceNotFound; + int param = isOpen ? 1 : 0 ; string url = $"{_baseUrl}/set_pump?on={param}"; + Logger.Debug(url); try { var response = await SendGetRequestAsync(url); @@ -353,6 +385,7 @@ namespace SparkClient.Model.Services { var jsonResponse = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject(jsonResponse); + Logger.Debug(result.Status); return result.Status; } diff --git a/ViewModel/BaseWindow/BaseControlVM.cs b/ViewModel/BaseWindow/BaseControlVM.cs index 041a4f2..56b8bd4 100644 --- a/ViewModel/BaseWindow/BaseControlVM.cs +++ b/ViewModel/BaseWindow/BaseControlVM.cs @@ -143,14 +143,13 @@ public class BaseControlVM : BaseViewModel /// 关闭并退回至上一个页面 /// /// - public void CloseVM(object parameter) + public async void CloseVM(object parameter) { if(Content is GradingResultVM) { WindowManager.PreviousVM(); WindowManager.mainViewModel.Content = WindowManager.PreviousVM(); - var _socClientService = new SOCClientService(); - _socClientService.OpenPump(false); + await SOCClientService.Service.OpenPump(false); //bool isSaved = (Content as GradingResultVM).isSaved; // if (isSaved) // { diff --git a/ViewModel/Grading/GradingLoadingVM.cs b/ViewModel/Grading/GradingLoadingVM.cs index 2841fa4..a5933d8 100644 --- a/ViewModel/Grading/GradingLoadingVM.cs +++ b/ViewModel/Grading/GradingLoadingVM.cs @@ -166,9 +166,9 @@ public class GradingLoadingVM : BaseViewModel,IDisposable } } - if(type == 0){ - _socClientService = new SOCClientService(); - string openpupmStatus = await _socClientService.OpenPump(true); + if(type == 0) + { + string openpupmStatus = await SOCClientService.Service.OpenPump(true); if (!StatusCodes.Success.Equals(openpupmStatus)) { new MessageBox().Show("气泵开启失败!"); diff --git a/Views/UserControl/Viewport3D.xaml.cs b/Views/UserControl/Viewport3D.xaml.cs index d8b5f56..86b1d02 100644 --- a/Views/UserControl/Viewport3D.xaml.cs +++ b/Views/UserControl/Viewport3D.xaml.cs @@ -506,8 +506,7 @@ public partial class Viewport3D //获取当前选中的面,找到角度,*100,发送至切工仪 var res = ViewportManager.ViewportTriangle.Find(e => ViewportManager.ChooseTriangleCode.Equals(e.TriangleCode)); if (res == null) return; - SOCClientService service = new SOCClientService(); - service.CutRevolve(res.Theta); + SOCClientService.Service.CutRevolve(res.Theta); break; } }