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 Areas { get; set; } public static List> Areas { get; set; } = new List>(); public static List Snaps { get; set; } = new List(); public static string CaptureUrl { get; set; } /// /// 库区字典(加载后就不变) /// //public static Dictionary> AreaMap { get; set; } = new Dictionary>(); 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(jsonContent); WebApiUrl = root.WebApiUrl; RCSApiUrl = root.RCSApiUrl; //NDCApiUrl = root.NDCApiUrl; SqlServer = root.SqlServer; //TCPServerIP = root.TCPServerIP; //TCPServerPort = root.TCPServerPort; //Areas = root.Areas; foreach (var item in root.Areas) { Areas.Add(item.Codes); } 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(myJsonResponse); public class Area { public string Name { get; set; } public List Codes { get; set; } } public class Root { public string WebApiUrl { get; set; } public string RCSApiUrl { get; set; } public string SqlServer { get; set; } public List Areas { get; set; } public List 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 盘点理货回库 = "盘点理货回库"; } public class AreaIndex { public const int Q取货区 = 0; public const int X卸货区 = 1; public const int H货架区 = 2; } }