123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using System.IO;
- namespace Bird_tool
- {
- public class ExcelColumnConfig
- {
- // Excel中的原始列名
- public string OriginalName { get; set; }
- // 映射后的别名(程序中使用)
- public string Alias { get; set; }
- // 是否必需
- public bool IsRequired { get; set; }
- // 默认值(当列为空时使用)
- public string DefaultValue { get; set; }
- }
- public class AppConfig
- {
- // 是否启用硬件测试功能, 该功能涉及到部分配置码所以需要根据配置文件调整
- public bool enable_hw_test { get; set; } = true;
- // 测试工具密码, 用于给部分客户时隐藏硬件测试功能
- public string test_tool_passwd { get; set; } = "szhfy";
- public bool enable_local_server { get; set; } = true;
- public string api_address { get; set; } = "hofuniot.cn";
- public int web_port = 8080;
- public string wifi_ssid { get; set; } = "h_y_ap";
- public string wifi_passwd { get; set; } = "51388513";
- public string pir { get; set; } = "90";
- // 是否加载excel文件
- public bool enable_excel_load { get; set; } = true;
-
- // 工具针对的设备类型 lw:乐微 fy:梵悦
- public string c_dev_key { get; set; } = "lw";
-
- public string excel_path { get; set; } = "test.xlsx";
- // excel 表格中主键的列名
- public string excel_primary_key { get; set; } = "uuid";
- // todo excel 中需要的列名 用于表示excel中列名对应的别名, 例如 key 对应authorKey. 用于在其它地方进行功能映射, 并且允许某些值为空, 配置默认值
- public List<ExcelColumnConfig> excel_columns { get; set; } = new List<ExcelColumnConfig>
- {
- new ExcelColumnConfig { OriginalName = "uuid", Alias = "uuid", IsRequired = true },
- new ExcelColumnConfig { OriginalName = "key", Alias = "authkey", IsRequired = true },
- // 添加其他列配置示例
- // new ExcelColumnConfig { OriginalName = "device_id", Alias = "deviceId", IsRequired = true, DefaultValue = "DEFAULT_001" }
- };
- }
- public static class ConfigManager
- {
- private static string ConfigName = "config.json";
- private static string ConfigPath =
- Path.Combine(Environment.CurrentDirectory, ConfigName);
- public static string getConfigPath()
- {
- return ConfigPath;
- }
- public static AppConfig LoadConfig(string ConfigName = "config.json")
- {
- ConfigPath = Path.Combine(Environment.CurrentDirectory, ConfigName);
- if (!File.Exists(ConfigPath))
- {
- var defaultConfig = new AppConfig();
- SaveConfig(defaultConfig);
- return defaultConfig;
- }
- //string json = File.ReadAllText(ConfigPath);
-
- try
- {
- using (var streamReader = new StreamReader(ConfigPath))
- using (var jsonReader = new JsonTextReader(streamReader))
- {
- var serializer = new JsonSerializer();
- return serializer.Deserialize<AppConfig>(jsonReader);
- }
- }
- catch (JsonException ex)
- {
- Console.WriteLine($"配置文件加载失败: {ex.Message}");
- // 处理损坏的配置文件
- File.Delete(ConfigPath);
- return new AppConfig();
- }
- catch (IOException ex)
- {
- Console.WriteLine($"文件访问错误: {ex.Message}");
- return new AppConfig();
- }
- }
- public static void SaveConfig(AppConfig config)
- {
- try
- {
- // 确保目录存在
- Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath));
- // 使用临时文件写入,然后替换原文件,避免占用问题
- string tempFile = Path.GetTempFileName();
- string json = JsonConvert.SerializeObject(config, Formatting.Indented);
- File.WriteAllText(tempFile, json);
- // 替换原文件
- File.Copy(tempFile, ConfigPath, true);
- File.Delete(tempFile);
- }
- catch (Exception ex)
- {
- Console.WriteLine($"保存配置失败: {ex.Message}");
- }
- }
- }
- }
|