using System; using System.Collections.Generic; using System.IO; 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 SqlServer { get; set; } public static string TcpServerIp { get; set; } public static int TcpServerPort { get; set; } public static List Areas { get; set; } = new List(); public static List Tasks { get; set; } = new List(); public static List ProductionLines { get; set; } = new List(); /// /// 库区字典(加载后就不变) /// public static Dictionary> AreaMap { get; set; } = new Dictionary>(); /// /// 任务字典(加载后就不变) /// public static Dictionary TaskMap { get; set; } = new Dictionary(); public static Dictionary AgvSite_ProdLineCodeMap { get; set; } = new Dictionary(); public static void Init() { // 加载配置文件 LoadJson(); // 针对 Areas 进行转换:将 Config 的 List 加载到 Dict 中 LoadAreas(); // 针对 Tasks 进行转换 LoadTasks(); } 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(jsonContent); WebApiUrl = root.WebApiUrl; NdcApiUrl = root.NdcApiUrl; SqlServer = root.SqlServer; TcpServerIp = root.TcpServerIp; TcpServerPort = root.TcpServerPort; Areas = root.Areas; Tasks = root.Tasks; ProductionLines = root.ProductionLines; } 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); } } private static void LoadTasks() { foreach (var task in Tasks) { TaskMap.Add(task.Name, task); } } private static void LoadProdLines() { foreach (var prod in ProductionLines) { } } } public class Config { // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); public class Area { public string Name { get; set; } public List Codes { get; set; } } 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 OnLoc { get; set; } public List OffLoc { get; set; } } public class Root { public string WebApiUrl { get; set; } public string NdcApiUrl { get; set; } public string SqlServer { get; set; } public string TcpServerIp { get; set; } public int TcpServerPort { get; set; } public List Areas { get; set; } public List Tasks { get; set; } public List ProductionLines { get; set; } } public class Task { public string Name { get; set; } public List StartAreas { get; set; } public List EndAreas { get; set; } } } public class AreaName { public const string 包装区 = "包装区"; public const string 操作区 = "操作区"; public const string 空托存放区 = "空托存放区"; public const string 货架区 = "货架区"; public const string 空箱存放区 = "空箱存放区"; public const string 满托存放区 = "满托存放区"; public const string 满箱存放区 = "满箱存放区"; public const string 人工_AGV接驳区 = "人工-AGV接驳区"; public const string 空托盘接驳区 = "空托盘接驳区"; public const string 空箱接驳区 = "空箱接驳区"; } public class TaskName { public const string 好运箱_满箱下线入库 = "好运箱-满箱下线入库"; public const string 好运箱_空箱上线 = "好运箱-空箱上线"; public const string 好运箱_空箱入库 = "好运箱-空箱入库"; public const string 好运箱_空箱绑定 = "好运箱-空箱绑定"; public const string 成品胶出库 = "成品胶出库"; public const string 托盘_满托下线入库 = "托盘-满托下线入库"; public const string 托盘_空托上线 = "托盘-空托上线"; public const string 托盘_空托入库 = "托盘-空托入库"; public const string 托盘_空托绑定 = "托盘-空托绑定"; public const string 抽检_不合格移库 = "抽检-不合格移库"; public const string 抽检_出库 = "抽检-出库"; public const string 抽检_合格回库 = "抽检-合格回库"; public const string 移库 = "移库"; public const string 尾箱回库 = "尾箱回库"; } }