Config.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using System.IO;
  8. namespace Bird_tool
  9. {
  10. public class ExcelColumnConfig
  11. {
  12. // Excel中的原始列名
  13. public string OriginalName { get; set; }
  14. // 映射后的别名(程序中使用)
  15. public string Alias { get; set; }
  16. // 是否必需
  17. public bool IsRequired { get; set; }
  18. // 默认值(当列为空时使用)
  19. public string DefaultValue { get; set; }
  20. }
  21. public class AppConfig
  22. {
  23. // 是否启用硬件测试功能, 该功能涉及到部分配置码所以需要根据配置文件调整
  24. public bool enable_hw_test { get; set; } = true;
  25. // 测试工具密码, 用于给部分客户时隐藏硬件测试功能
  26. public string test_tool_passwd { get; set; } = "szhfy";
  27. public bool enable_local_server { get; set; } = true;
  28. public string api_address { get; set; } = "hofuniot.cn";
  29. public int web_port = 8080;
  30. public string wifi_ssid { get; set; } = "h_y_ap";
  31. public string wifi_passwd { get; set; } = "51388513";
  32. public string pir { get; set; } = "90";
  33. // 是否加载excel文件
  34. public bool enable_excel_load { get; set; } = true;
  35. // 工具针对的设备类型 lw:乐微 fy:梵悦
  36. public string c_dev_key { get; set; } = "lw";
  37. public string excel_path { get; set; } = "test.xlsx";
  38. // excel 表格中主键的列名
  39. public string excel_primary_key { get; set; } = "uuid";
  40. // todo excel 中需要的列名 用于表示excel中列名对应的别名, 例如 key 对应authorKey. 用于在其它地方进行功能映射, 并且允许某些值为空, 配置默认值
  41. public List<ExcelColumnConfig> excel_columns { get; set; } = new List<ExcelColumnConfig>
  42. {
  43. new ExcelColumnConfig { OriginalName = "uuid", Alias = "uuid", IsRequired = true },
  44. new ExcelColumnConfig { OriginalName = "key", Alias = "authkey", IsRequired = true },
  45. // 添加其他列配置示例
  46. // new ExcelColumnConfig { OriginalName = "device_id", Alias = "deviceId", IsRequired = true, DefaultValue = "DEFAULT_001" }
  47. };
  48. }
  49. public static class ConfigManager
  50. {
  51. private static string ConfigName = "config.json";
  52. private static string ConfigPath =
  53. Path.Combine(Environment.CurrentDirectory, ConfigName);
  54. public static string getConfigPath()
  55. {
  56. return ConfigPath;
  57. }
  58. public static AppConfig LoadConfig(string ConfigName = "config.json")
  59. {
  60. ConfigPath = Path.Combine(Environment.CurrentDirectory, ConfigName);
  61. if (!File.Exists(ConfigPath))
  62. {
  63. var defaultConfig = new AppConfig();
  64. SaveConfig(defaultConfig);
  65. return defaultConfig;
  66. }
  67. //string json = File.ReadAllText(ConfigPath);
  68. try
  69. {
  70. using (var streamReader = new StreamReader(ConfigPath))
  71. using (var jsonReader = new JsonTextReader(streamReader))
  72. {
  73. var serializer = new JsonSerializer();
  74. return serializer.Deserialize<AppConfig>(jsonReader);
  75. }
  76. }
  77. catch (JsonException ex)
  78. {
  79. Console.WriteLine($"配置文件加载失败: {ex.Message}");
  80. // 处理损坏的配置文件
  81. File.Delete(ConfigPath);
  82. return new AppConfig();
  83. }
  84. catch (IOException ex)
  85. {
  86. Console.WriteLine($"文件访问错误: {ex.Message}");
  87. return new AppConfig();
  88. }
  89. }
  90. public static void SaveConfig(AppConfig config)
  91. {
  92. try
  93. {
  94. // 确保目录存在
  95. Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath));
  96. // 使用临时文件写入,然后替换原文件,避免占用问题
  97. string tempFile = Path.GetTempFileName();
  98. string json = JsonConvert.SerializeObject(config, Formatting.Indented);
  99. File.WriteAllText(tempFile, json);
  100. // 替换原文件
  101. File.Copy(tempFile, ConfigPath, true);
  102. File.Delete(tempFile);
  103. }
  104. catch (Exception ex)
  105. {
  106. Console.WriteLine($"保存配置失败: {ex.Message}");
  107. }
  108. }
  109. }
  110. }