kazelee
2025-05-21 a8627a98b82d2364cbe849ca746e72fbab9916e5
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
 
using HH.WCS.Mobox3.AnGang;
 
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
// 根据 JSON 文件 生成对应的 C# 对象;网址:https://json2csharp.com/
 
namespace HH.WCS.Mobox3.AnGang {
    public class Settings {
        public static string WebApiUrl { get; set; }
        public static string RCSApiUrl { get; set; }
        public static string NDCApiUrl { get; set; }
        public static string SqlServer { get; set; }
        public static string TCPServerIP { get; set; }
        public static int TCPServerPort { get; set; }
        public static List<Config.Area> Areas { get; set; }
        public static List<Config.Snap> Snaps { get; set; }
        public static string CaptureUrl { get; set; }
 
        /// <summary>
        /// 库区字典(加载后就不变)
        /// </summary>
        public static Dictionary<string, List<string>> AreaMap { get; set; } = new Dictionary<string, List<string>>();
 
        public static void Init() {
            // 加载配置文件
            LoadJson();
 
            // 针对 Areas 进行转换:将 Config 的 List 加载到 Dict 中
            LoadAreas();
        }
 
        private static void LoadJson() {
            LogHelper.Info("加载配置文件信息 开始");
            // JSON 文件路径
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./config/config.json");
 
            try {
                // 读取 JSON 文件内容
                string jsonContent = File.ReadAllText(filePath);
 
                // 反序列化为 Root 对象
                var root = JsonConvert.DeserializeObject<Config.Root>(jsonContent);
 
                WebApiUrl = root.WebApiUrl;
                RCSApiUrl = root.RCSApiUrl;
                NDCApiUrl = root.NDCApiUrl;
                SqlServer = root.SqlServer;
                TCPServerIP = root.TCPServerIP;
                TCPServerPort = root.TCPServerPort;
                Areas = root.Areas;
                Snaps = root.Snaps;
                CaptureUrl = root.CaptureUrl;
 
            }
            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 Areas) {
                AreaMap.Add(area.Name, area.Codes);
            }
        }
    }
 
    public class Config {
        // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
        public class Area {
            public string Name { get; set; }
            public List<string> Codes { get; set; }
        }
 
        public class Root {
            public string WebApiUrl { get; set; }
            public string RCSApiUrl { get; set; }
            public string NDCApiUrl { get; set; }
            public string SqlServer { get; set; }
            public string TCPServerIP { get; set; }
            public int TCPServerPort { get; set; }
            public List<Area> Areas { get; set; }
            public List<Snap> Snaps { get; set; }
            public string CaptureUrl { get; set; }
        }
 
        public class Snap {
            public string Ip { get; set; }
            public int Port { get; set; }
            public string Name { get; set; }
            public string Pwd { get; set; }
        }
    }
 
    public class TaskName {
        public const string 产品入库 = "产品入库";
        public const string 产品部分出库 = "产品部分出库";
        public const string 产品部分回库 = "产品部分回库";
        public const string 盘点理货出库 = "盘点理货出库";
        public const string 盘点理货回库 = "盘点理货回库";
    }
}