|
|
|
@ -21,6 +21,7 @@ using Newtonsoft.Json.Linq; |
|
|
|
|
using SparkClient.Model.Common; |
|
|
|
|
using Color = System.Windows.Media.Color; |
|
|
|
|
using MessageBox = SparkClient.Views.Dialog.MessageBox; |
|
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
|
|
|
|
|
|
namespace SparkClient.ViewModel.Grading; |
|
|
|
|
|
|
|
|
@ -276,6 +277,11 @@ public class DiamondSelectVM : BaseViewModel |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (DirectoryNotFoundException ex) |
|
|
|
|
{ |
|
|
|
|
loading.Close(); |
|
|
|
|
new MessageBox().Show(MultilingualHelper.getString("SavePathIsnotExists")); |
|
|
|
|
} |
|
|
|
|
finally { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
@ -315,160 +321,172 @@ public class DiamondSelectVM : BaseViewModel |
|
|
|
|
/// <param name="param"></param> |
|
|
|
|
public async void DoStartGrading(object param) |
|
|
|
|
{ |
|
|
|
|
if (param != null) |
|
|
|
|
{ |
|
|
|
|
AlgorithmResultEntity parameter = new AlgorithmResultEntity(); |
|
|
|
|
JsonImport jsonImport = new JsonImport(); |
|
|
|
|
//jsonImport.FilePath.Text = "D:\\项目\\TEST240.json"; |
|
|
|
|
bool? a = jsonImport.ShowDialog(); |
|
|
|
|
if (a ?? false) |
|
|
|
|
LoadingDialog loading = new LoadingDialog(); |
|
|
|
|
try { |
|
|
|
|
if (param != null) |
|
|
|
|
{ |
|
|
|
|
string fileName = jsonImport.FilePath.Text; |
|
|
|
|
string[] lines = File.ReadAllLines(fileName); |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
AlgorithmResultEntity parameter = new AlgorithmResultEntity(); |
|
|
|
|
JsonImport jsonImport = new JsonImport(); |
|
|
|
|
//jsonImport.FilePath.Text = "D:\\项目\\TEST240.json"; |
|
|
|
|
bool? a = jsonImport.ShowDialog(); |
|
|
|
|
if (a ?? false) |
|
|
|
|
{ |
|
|
|
|
string fileName = jsonImport.FilePath.Text; |
|
|
|
|
string[] lines = File.ReadAllLines(fileName); |
|
|
|
|
StringBuilder stringBuilder = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
foreach (var line in lines) |
|
|
|
|
foreach (var line in lines) |
|
|
|
|
{ |
|
|
|
|
stringBuilder.Append(line); |
|
|
|
|
} |
|
|
|
|
parameter = JsonConvert.DeserializeObject<AlgorithmResultEntity>(stringBuilder.ToString()); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
stringBuilder.Append(line); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
parameter = JsonConvert.DeserializeObject<AlgorithmResultEntity>(stringBuilder.ToString()); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
LoadingDialog loading = new LoadingDialog(); |
|
|
|
|
var tcs = new TaskCompletionSource<bool>(); |
|
|
|
|
var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => { |
|
|
|
|
loading.Closed += (s, e) => tcs.SetResult(true); |
|
|
|
|
loading.ShowDialog(); |
|
|
|
|
} |
|
|
|
|
)); |
|
|
|
|
await Task.Run(async () => |
|
|
|
|
{ |
|
|
|
|
for (int i = 0; i <= 100; i++) |
|
|
|
|
|
|
|
|
|
var tcs = new TaskCompletionSource<bool>(); |
|
|
|
|
var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => { |
|
|
|
|
loading.Closed += (s, e) => tcs.SetResult(true); |
|
|
|
|
loading.ShowDialog(); |
|
|
|
|
} |
|
|
|
|
)); |
|
|
|
|
await Task.Run(async () => |
|
|
|
|
{ |
|
|
|
|
// 模拟耗时操作 |
|
|
|
|
//System.Threading.Thread.Sleep(50); // 休眠50毫秒 |
|
|
|
|
await Task.Delay(5); |
|
|
|
|
// 更新进度条的值(需要在UI线程上执行) |
|
|
|
|
loading.Dispatcher.Invoke(() => |
|
|
|
|
for (int i = 0; i <= 100; i++) |
|
|
|
|
{ |
|
|
|
|
loading.setValue(i); |
|
|
|
|
}); |
|
|
|
|
// 模拟耗时操作 |
|
|
|
|
//System.Threading.Thread.Sleep(50); // 休眠50毫秒 |
|
|
|
|
await Task.Delay(5); |
|
|
|
|
// 更新进度条的值(需要在UI线程上执行) |
|
|
|
|
loading.Dispatcher.Invoke(() => |
|
|
|
|
{ |
|
|
|
|
loading.setValue(i); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
// SocResultEntity socResolt = new SocResultEntity(); |
|
|
|
|
// AlgorithmResultEntity parameter = new AlgorithmResultEntity(); |
|
|
|
|
// // 启动soc |
|
|
|
|
// socResolt = DoSoc(); |
|
|
|
|
// // 启动算法 |
|
|
|
|
// parameter = DoAlgorithm(); |
|
|
|
|
} |
|
|
|
|
// SocResultEntity socResolt = new SocResultEntity(); |
|
|
|
|
// AlgorithmResultEntity parameter = new AlgorithmResultEntity(); |
|
|
|
|
// // 启动soc |
|
|
|
|
// socResolt = DoSoc(); |
|
|
|
|
// // 启动算法 |
|
|
|
|
// parameter = DoAlgorithm(); |
|
|
|
|
|
|
|
|
|
//parameter = DoSoc(); |
|
|
|
|
parameter.Standard = getStandardName(); |
|
|
|
|
string value = param.ToString() ?? ""; |
|
|
|
|
if (value != null && value.Split(" ").Length == 3) |
|
|
|
|
{ |
|
|
|
|
parameter.Shape = value.Split(" ")[0]; |
|
|
|
|
parameter.CrownType = value.Split(" ")[1]; |
|
|
|
|
parameter.PavType = value.Split(" ")[2]; |
|
|
|
|
} |
|
|
|
|
parameter.DiamondCode = DiamondCode; |
|
|
|
|
GradingResult(parameter); |
|
|
|
|
}); |
|
|
|
|
await Task.Delay(5); |
|
|
|
|
//parameter = DoSoc(); |
|
|
|
|
parameter.Standard = getStandardName(); |
|
|
|
|
string value = param.ToString() ?? ""; |
|
|
|
|
if (value != null && value.Split(" ").Length == 3) |
|
|
|
|
{ |
|
|
|
|
parameter.Shape = value.Split(" ")[0]; |
|
|
|
|
parameter.CrownType = value.Split(" ")[1]; |
|
|
|
|
parameter.PavType = value.Split(" ")[2]; |
|
|
|
|
} |
|
|
|
|
parameter.DiamondCode = DiamondCode; |
|
|
|
|
GradingResult(parameter); |
|
|
|
|
}); |
|
|
|
|
await Task.Delay(5); |
|
|
|
|
loading.Close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (DirectoryNotFoundException ex) |
|
|
|
|
{ |
|
|
|
|
loading.Close(); |
|
|
|
|
new MessageBox().Show(MultilingualHelper.getString("SavePathIsnotExists")); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
new MessageBox().Show($"{MultilingualHelper.getString("ApplicationError")}{ex.Message}"); |
|
|
|
|
Logger.Error($"全局异常捕获:{ex.Message}", ex); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 处理算法调用失败时的逻辑,包括保存图片到历史记录文件夹 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="image_files">图片文件路径的 JSON 字符串</param> |
|
|
|
|
/// |
|
|
|
|
private static readonly ILog Logger = LogManager.GetLogger(typeof(AlgorithmServer)); |
|
|
|
|
private void HandleAlgorithmFailure(string image_files) |
|
|
|
|
{ |
|
|
|
|
/// 处理算法调用失败时的逻辑,包括保存图片到历史记录文件夹 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="image_files">图片文件路径的 JSON 字符串</param> |
|
|
|
|
/// |
|
|
|
|
private static readonly ILog Logger = LogManager.GetLogger(typeof(AlgorithmServer)); |
|
|
|
|
private void HandleAlgorithmFailure(string image_files) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
// 从配置文件中读取 imageHistoryPath |
|
|
|
|
// 定义历史记录文件夹路径 |
|
|
|
|
string? imageHistoryPath = ConfigurationManager.AppSettings["ImageHistoryPath"]; |
|
|
|
|
if (string.IsNullOrEmpty(imageHistoryPath)) |
|
|
|
|
{ |
|
|
|
|
Logger.Error("ImageHistoryPath is not configured in AppSettings."); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// 从配置文件中读取 imageHistoryPath |
|
|
|
|
// 定义历史记录文件夹路径 |
|
|
|
|
string? imageHistoryPath = ConfigurationManager.AppSettings["ImageHistoryPath"]; |
|
|
|
|
if (string.IsNullOrEmpty(imageHistoryPath)) |
|
|
|
|
{ |
|
|
|
|
Logger.Error("ImageHistoryPath is not configured in AppSettings."); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 生成时间戳,格式为 yyyyMMddHHmmss |
|
|
|
|
string timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); |
|
|
|
|
// 组合新的文件夹路径 |
|
|
|
|
string newFolderPath = Path.Combine(imageHistoryPath, $"image-{timestamp}"); |
|
|
|
|
// 生成时间戳,格式为 yyyyMMddHHmmss |
|
|
|
|
string timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); |
|
|
|
|
// 组合新的文件夹路径 |
|
|
|
|
string newFolderPath = Path.Combine(imageHistoryPath, $"image-{timestamp}"); |
|
|
|
|
|
|
|
|
|
// 检查 D 盘内存空间 |
|
|
|
|
DriveInfo dDrive = new DriveInfo("D"); |
|
|
|
|
long requiredSpace = 10L * 1024 * 1024 * 1024; // 10GB |
|
|
|
|
if (dDrive.TotalFreeSpace < requiredSpace) |
|
|
|
|
// 检查 D 盘内存空间 |
|
|
|
|
DriveInfo dDrive = new DriveInfo("D"); |
|
|
|
|
long requiredSpace = 10L * 1024 * 1024 * 1024; // 10GB |
|
|
|
|
if (dDrive.TotalFreeSpace < requiredSpace) |
|
|
|
|
{ |
|
|
|
|
// 如果 D 盘空间不足 10GB,删除最早创建的10个文件夹 |
|
|
|
|
DirectoryInfo historyDir = new DirectoryInfo(imageHistoryPath); // 获取历史记录文件夹的信息 |
|
|
|
|
DirectoryInfo[] subDirs = historyDir.GetDirectories(); // 获取历史记录文件夹中的所有子文件夹 |
|
|
|
|
if (subDirs.Length > 0) |
|
|
|
|
{ |
|
|
|
|
// 如果 D 盘空间不足 10GB,删除最早创建的10个文件夹 |
|
|
|
|
DirectoryInfo historyDir = new DirectoryInfo(imageHistoryPath); // 获取历史记录文件夹的信息 |
|
|
|
|
DirectoryInfo[] subDirs = historyDir.GetDirectories(); // 获取历史记录文件夹中的所有子文件夹 |
|
|
|
|
if (subDirs.Length > 0) |
|
|
|
|
{ |
|
|
|
|
// 按创建时间排序子文件夹 |
|
|
|
|
var orderedSubDirs = subDirs.OrderBy(d => d.CreationTime).ToList(); |
|
|
|
|
int foldersToDelete = Math.Min(10, orderedSubDirs.Count); |
|
|
|
|
// 按创建时间排序子文件夹 |
|
|
|
|
var orderedSubDirs = subDirs.OrderBy(d => d.CreationTime).ToList(); |
|
|
|
|
int foldersToDelete = Math.Min(10, orderedSubDirs.Count); |
|
|
|
|
|
|
|
|
|
// 删除最早的 10 个文件夹,如果不够 10 个则全删掉 |
|
|
|
|
for (int i = 0; i < foldersToDelete; i++) |
|
|
|
|
{ |
|
|
|
|
orderedSubDirs[i].Delete(true); |
|
|
|
|
} |
|
|
|
|
// 删除最早的 10 个文件夹,如果不够 10 个则全删掉 |
|
|
|
|
for (int i = 0; i < foldersToDelete; i++) |
|
|
|
|
{ |
|
|
|
|
orderedSubDirs[i].Delete(true); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 创建新文件夹 |
|
|
|
|
Directory.CreateDirectory(newFolderPath); |
|
|
|
|
// 创建新文件夹 |
|
|
|
|
Directory.CreateDirectory(newFolderPath); |
|
|
|
|
|
|
|
|
|
// 保存图片到新文件夹 |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// 解析 image_files JSON 数组 |
|
|
|
|
JArray imageFilesArray = JArray.Parse(image_files); |
|
|
|
|
string[] imageFiles = imageFilesArray.Select(token => token.ToString()).ToArray(); |
|
|
|
|
|
|
|
|
|
string? imageBasePath = ConfigurationManager.AppSettings["ImageFileBasePath"]; // 图片根目录 |
|
|
|
|
if (string.IsNullOrEmpty(imageBasePath)) |
|
|
|
|
{ |
|
|
|
|
Logger.Error("ImageFileBasePath is not configured in AppSettings."); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// 保存图片到新文件夹 |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// 解析 image_files JSON 数组 |
|
|
|
|
JArray imageFilesArray = JArray.Parse(image_files); |
|
|
|
|
string[] imageFiles = imageFilesArray.Select(token => token.ToString()).ToArray(); |
|
|
|
|
|
|
|
|
|
foreach (string imageFile in imageFiles) |
|
|
|
|
{ |
|
|
|
|
// 获取文件名 |
|
|
|
|
string fileName = Path.GetFileName(imageFile); |
|
|
|
|
// 构建完整的源文件路径 |
|
|
|
|
string sourcePath = Path.Combine(imageBasePath, imageFile); |
|
|
|
|
// 组合目标路径 |
|
|
|
|
string destinationPath = Path.Combine(newFolderPath, fileName); |
|
|
|
|
// 复制文件到目标路径,如果目标文件已存在则覆盖 |
|
|
|
|
File.Copy(sourcePath, destinationPath, true); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (JsonException ex) |
|
|
|
|
string? imageBasePath = ConfigurationManager.AppSettings["ImageFileBasePath"]; // 图片根目录 |
|
|
|
|
if (string.IsNullOrEmpty(imageBasePath)) |
|
|
|
|
{ |
|
|
|
|
// 记录日志或处理异常 |
|
|
|
|
Logger.Error($"Error parsing image_files JSON: {ex.Message}"); |
|
|
|
|
Logger.Error($"Stack Trace: {ex.StackTrace}"); |
|
|
|
|
Logger.Error("ImageFileBasePath is not configured in AppSettings."); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
|
|
foreach (string imageFile in imageFiles) |
|
|
|
|
{ |
|
|
|
|
// 记录其他异常 |
|
|
|
|
Logger.Error($"Error saving images: {ex.Message}"); |
|
|
|
|
Logger.Error($"Stack Trace: {ex.StackTrace}"); |
|
|
|
|
// 获取文件名 |
|
|
|
|
string fileName = Path.GetFileName(imageFile); |
|
|
|
|
// 构建完整的源文件路径 |
|
|
|
|
string sourcePath = Path.Combine(imageBasePath, imageFile); |
|
|
|
|
// 组合目标路径 |
|
|
|
|
string destinationPath = Path.Combine(newFolderPath, fileName); |
|
|
|
|
// 复制文件到目标路径,如果目标文件已存在则覆盖 |
|
|
|
|
File.Copy(sourcePath, destinationPath, true); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (JsonException ex) |
|
|
|
|
{ |
|
|
|
|
// 记录日志或处理异常 |
|
|
|
|
Logger.Error($"Error parsing image_files JSON: {ex.Message}"); |
|
|
|
|
Logger.Error($"Stack Trace: {ex.StackTrace}"); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
// 记录其他异常 |
|
|
|
|
Logger.Error($"Error saving images: {ex.Message}"); |
|
|
|
|
Logger.Error($"Stack Trace: {ex.StackTrace}"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
/// <summary> |
|
|
|
|
/// 跳转至检测结果 |
|
|
|
|
/// </summary> |
|
|
|
|