视觉定位提交保存
This commit is contained in:
@ -1,18 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DH.Commons.Base
|
||||
{
|
||||
[Serializable]
|
||||
public class VisualLocalization
|
||||
{
|
||||
// 必须包含公共无参构造函数
|
||||
public VisualLocalization() { }
|
||||
|
||||
// 配置属性
|
||||
public string CameraName { get; set; }
|
||||
public string ModelPath { get; set; }
|
||||
public string ImgPath { get; set; }
|
||||
@ -20,45 +16,62 @@ namespace DH.Commons.Base
|
||||
public string Direction { get; set; }
|
||||
public string Speed { get; set; }
|
||||
|
||||
// 保存到文件
|
||||
public void SaveToFile(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true, // 美化格式
|
||||
IgnoreNullValues = true // 忽略空值
|
||||
};
|
||||
// 配置文件路径
|
||||
private const string ConfigFile = "VisualConfigs.json";
|
||||
private static readonly object _fileLock = new object();
|
||||
|
||||
string json = JsonSerializer.Serialize(this, options);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
/// <summary>
|
||||
/// 保存当前配置(存在则更新,不存在则新增)
|
||||
/// </summary>
|
||||
public void Save()
|
||||
{
|
||||
lock (_fileLock)
|
||||
{
|
||||
throw new InvalidOperationException($"保存失败: {ex.Message}");
|
||||
var list = LoadAll();
|
||||
var existing = list.FirstOrDefault(c => c.CameraName == CameraName);
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
// 更新现有配置
|
||||
existing.ModelPath = ModelPath;
|
||||
existing.ImgPath = ImgPath;
|
||||
existing.Threshold = Threshold;
|
||||
existing.Direction = Direction;
|
||||
existing.Speed = Speed;
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(this);
|
||||
}
|
||||
|
||||
SaveAll(list);
|
||||
}
|
||||
}
|
||||
|
||||
// 从文件加载
|
||||
public static VisualLocalization LoadFromFile(string filePath)
|
||||
/// <summary>
|
||||
/// 获取全部配置列表
|
||||
/// </summary>
|
||||
public static List<VisualLocalization> LoadAll()
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
throw new FileNotFoundException("文件不存在", filePath);
|
||||
lock (_fileLock)
|
||||
{
|
||||
if (!File.Exists(ConfigFile)) return new List<VisualLocalization>();
|
||||
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(filePath);
|
||||
return JsonSerializer.Deserialize<VisualLocalization>(json);
|
||||
var json = File.ReadAllText(ConfigFile);
|
||||
return JsonSerializer.Deserialize<List<VisualLocalization>>(json)
|
||||
?? new List<VisualLocalization>();
|
||||
}
|
||||
catch (JsonException ex)
|
||||
}
|
||||
|
||||
private static void SaveAll(List<VisualLocalization> list)
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
throw new InvalidOperationException($"JSON解析错误: {ex.Message}");
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new InvalidOperationException($"文件读取失败: {ex.Message}");
|
||||
}
|
||||
WriteIndented = true,
|
||||
IgnoreNullValues = true
|
||||
};
|
||||
|
||||
File.WriteAllText(ConfigFile, JsonSerializer.Serialize(list, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user