kazelee
8 天以前 2ca90a404fa1ab94eb6374f50c6ddd47a2b7f0e6
util/Settings.cs
@@ -7,15 +7,19 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace HH.WCS.Mobox3.DSZSH {
    public class Settings
    {
namespace HH.WCS.Mobox3.DSZSH.util {
    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 Config.ErpRoute ErpRoute { get; set; }
        public static SqlSugar.DbType DbType { get; set; } = SqlSugar.DbType.SqlServer;
        public static string DbConn { 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> { };
@@ -25,10 +29,12 @@
        public static void Init() {
            // 加载配置文件
            LoadJson();
            LoadProdLines();
        }
        private static void LoadJson() {
            LogHelper.Info("加载配置文件信息 开始");
            LogHelper.Info("加载配置文件信息:开始!!");
            // JSON 文件路径
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./config/config.json");
@@ -39,54 +45,53 @@
                // 反序列化为 Config 对象
                var root = JsonConvert.DeserializeObject<Config.Root>(jsonContent);
                #region Config.Root 赋值字段部分
                WebApiUrl = root.WebApiUrl;
                NdcApiUrl = root.NdcApiUrl;
                ErpApiUrl = root.ErpApiUrl;
                SqlServer = root.SqlServer;
                TcpServerIp = root.TcpServerIp;
                ErpRoute = root.ErpRoute;
                TcpServerPort = root.TcpServerPort;
                DbConn = 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));
                    }
                }
                #endregion
            }   
            catch (FileNotFoundException) {
                LogHelper.Info("JSON 文件未找到");
                LogHelper.Info("JSON文件未找到!!", "Exception");
            }
            catch (JsonException ex) {
                LogHelper.Info($"JSON 解析错误: {ex.Message}");
                LogHelper.Info($"JSON解析错误!!Error:{ex.Message}\n\n{ex.StackTrace}\n", "Exception");
            }
            catch (Exception ex) {
                LogHelper.Info($"发生错误: {ex.Message}");
                LogHelper.InfoEx(ex, "Settings:");
            }
            LogHelper.Info("加载配置文件信息 完成");
            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];
            for (var i = 0; i < ProductionLines.Count; i++) {
                if (ProductionLines[i].OnLoc == null) continue;
                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));
                }
            }
        }
    }
    // [Convert JSON to C# Classes Online - Json2CSharp Toolkit](https://json2csharp.com/)
    public class Config {
        // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
        public class ErpRoute {
            public string CreateTaskReturn { get; set; }
            public string PickUpReturn { get; set; }
        }
        public class ProductionLine {
            public string Id { get; set; }
            public string Name { get; set; }
@@ -101,9 +106,9 @@
            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 ErpRoute ErpRoute { get; set; }
            public string SqlServer { get; set; }
            public List<TaskInfo> TaskInfos { get; set; }
            public List<ProductionLine> ProductionLines { get; set; }
        }
@@ -136,5 +141,17 @@
        public static string Name(this ETask eTask) {
            return Settings.TaskInfos[(int)eTask].TaskName;
        }
        public static Config.TaskInfo Info(this ETask eTask) {
            return Settings.TaskInfos[(int)eTask];
        }
        public static List<string> StartAreas(this ETask eTask) {
            return Settings.TaskInfos[(int)(eTask)].StartAreas;
        }
        public static List<string> EndAreas(this ETask eTask) {
            return Settings.TaskInfos[((int)eTask)].EndAreas;
        }
    }
}