kazelee
2025-06-27 d20ce230b49932d39ee4ce25e39fd78368c3b28a
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.IO;
 
using HH.WCS.Mobox3.DSZSH.util;
 
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace HH.WCS.Mobox3.DSZSH {
    public class Settings
    {
        public static string WebApiUrl { get; set; }
        public static string NdcApiUrl { get; set; }
        public static string ErpApiUrl { get; set; } // ERP 反馈接口URL
        public static string SqlServer { get; set; }
        public static string TcpServerIp { get; set; }
        public static int TcpServerPort { get; set; }
        public static List<Config.ProductionLine> ProductionLines { get; set; } = new List<Config.ProductionLine>();
 
        public static List<Config.TaskInfo> TaskInfos { get; set; } = new List<Config.TaskInfo> { };
 
        public static Dictionary<string, int> LocProdIdMap { get; set; } = new Dictionary<string, int> { };
 
        public static void Init() {
            // 加载配置文件
            LoadJson();
        }
 
        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 对象
                var root = JsonConvert.DeserializeObject<Config.Root>(jsonContent);
 
                WebApiUrl = root.WebApiUrl;
                NdcApiUrl = root.NdcApiUrl;
                ErpApiUrl = root.ErpApiUrl;
                SqlServer = root.SqlServer;
                TcpServerIp = root.TcpServerIp;
                TcpServerPort = root.TcpServerPort;
                ProductionLines = root.ProductionLines;
                TaskInfos = root.TaskInfos;
 
                for (var i = 0; i < ProductionLines.Count; i++) {
                    foreach (var onLoc in ProductionLines[i].OnLoc) {
                        LocProdIdMap.Add(onLoc, int.Parse(ProductionLines[i].Id));
                    }
                    foreach (var offLoc in ProductionLines[i].OffLoc) {
                        LocProdIdMap.Add(offLoc, int.Parse(ProductionLines[i].Id));
                    }
                }
            }   
            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 LoadProdLines() {
            //var db = new SqlHelper<object>().GetInstance();
            //for (int  i = 0;  i < ProductionLines.Count;  i++) {
            //    var line = ProductionLines[i];
            //    // 通过OnLoc OffLoc找到AGVsite然后写入字典
            //}
        }
 
        public static Config.TaskInfo GetTaskInfo(ETask eTask) {
            return TaskInfos[(int)eTask];
        }
    }
 
    // [Convert JSON to C# Classes Online - Json2CSharp Toolkit](https://json2csharp.com/)
 
    public class Config {
 
        // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
        public class ProductionLine {
            public string Id { get; set; }
            public string Name { get; set; }
            public string PlcIp { get; set; }
            public int PlcPort { get; set; }
            public int SlaveId { get; set; }
            public List<string> OnLoc { get; set; }
            public List<string> OffLoc { get; set; }
        }
 
        public class Root {
            public string WebApiUrl { get; set; }
            public string NdcApiUrl { get; set; }
            public string ErpApiUrl { get; set; }
            public string SqlServer { get; set; }
            public string TcpServerIp { get; set; }
            public int TcpServerPort { get; set; }
            public List<TaskInfo> TaskInfos { get; set; }
            public List<ProductionLine> ProductionLines { get; set; }
        }
 
        public class TaskInfo {
            public string TaskName { get; set; }
            public List<string> StartAreas { get; set; }
            public List<string> EndAreas { get; set; }
            public List<string> EndAreas_Pallet { get; set; }
            public List<string> EndAreas_Goodpack { get; set; }
        }
    }
 
    public enum ETask {
        M满托下线入库,
        M满箱下线入库,
        C成品胶出库,
        K空托上线出库,
        K空箱上线出库,
        K空托入库,
        K空箱入库,
        C抽检出库,
        C抽检合格回库,
        C抽检不合格移库,
        W尾料回库,
        Y移库,
    }
 
    public static class ETaskExtensions {
        public static string Name(this ETask eTask) {
            return Settings.TaskInfos[(int)eTask].TaskName;
        }
    }
}