kazelee
2025-05-14 cd92df8b7b383a6a3218f50b3b62264db8332899
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.IO;
 
using HH.WCS.Mobox3.AnGang;
 
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace HH.WCS.Mobox3.AnGang.AppStart {
    public class Settings {
        /// <summary>
        /// 配置文件
        /// </summary>
        public static Config Config { get; set; }
 
        /// <summary>
        /// 库区字典(加载后就不变)
        /// </summary>
        public static Dictionary<string, List<string>> AreaMap { get; set; } = new Dictionary<string, List<string>>();
 
        //public static Snap Snap { get; set; }
        public static string CaptureUrl { get; set; }
 
        public static void Init() {
            // 加载配置文件
            LoadJson();
 
            // 针对 Areas 进行转换:将 Config 的 List 加载到 Dict 中
            LoadAreas();
 
            //Snap = Config.Snap[0]; // 目前本项目只有一个相机
            CaptureUrl = Config.CaptureUrl; // 保存图片的根目录
        }
 
        private static void LoadJson() {
            LogHelper.Info("加载配置文件信息 开始");
            // JSON 文件路径
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./config/config.json");
 
            try {
                // 读取 JSON 文件内容
                string jsonContent = File.ReadAllText(filePath);
 
                // 反序列化为 Config 对象
                Config = JsonConvert.DeserializeObject<Config>(jsonContent);
            }
            catch (FileNotFoundException) {
                LogHelper.Info("JSON 文件未找到");
            }
            catch (JsonException ex) {
                LogHelper.Info($"JSON 解析错误: {ex.Message}");
            }
            catch (Exception ex) {
                LogHelper.Info($"发生错误: {ex.Message}");
            }
            LogHelper.Info("加载配置文件信息 完成");
        }
 
        private static void LoadAreas() {
            foreach (var area in Config.Areas) {
                AreaMap.Add(area.Name, area.Codes);
            }
        }
    }
}