123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using Newtonsoft.Json.Converters;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace InitializerModel
- {
- public static class AppSettingConfig
- {
- private static AppSettingConfigModel _Instance;
- public static AppSettingConfigModel Instance
- {
- get
- {
- if (_Instance == null)
- {
- Reload();
- }
- return _Instance;
- }
- private set => _Instance = value;
- }
- //private string _FirmwareRoot, _Language, _MES, _MechineCode;
- //private bool _IsDisableAuthRequired;
- //private string _JohnSenLedComPort;
- private static string settingFileName = "config.ini";
- //public AppSettingConfig()
- //{
- // if (!File.Exists(settingFileName))
- // {
- // LoadDefaultSetting();
- // }
- // else
- // {
- // LoadSettingFromDefaultFile();
- // }
- //}
- public static void Reload()
- {
- //Instance = new AppSettingConfig();
- if (!File.Exists(settingFileName))
- {
- LoadDefaultSetting();
- }
- else
- {
- LoadSettingFromDefaultFile();
- }
- }
- public static void Save()
- {
- //var toSave = new AppSettingConfigModel();
- //toSave.FirmwareRoot = _FirmwareRoot;
- //toSave.Language = _Language;
- //toSave.MES = _MES;
- //toSave.MechineCode = _MechineCode;
- //toSave.IsDisableAuthRequired = _IsDisableAuthRequired;
- File.WriteAllText(
- settingFileName,
- JsonConvert.SerializeObject(
- Instance,
- Formatting.Indented,
- new StringEnumConverter()
- ));
- }
- private static void LoadSettingFromDefaultFile()
- {
- try
- {
- LoadDefaultSetting();
- var configStr = File.ReadAllText(settingFileName);
- var config = Newtonsoft.Json.JsonConvert.DeserializeObject<AppSettingConfigModel>(configStr);
- CompacityChnage(config);
- _Instance = config;
- //_Instance.FirmwareRoot = config.FirmwareRoot;
- //_Language = config.Language;
- //_MES = config.MES;
- //_MechineCode = config.MechineCode;
- //_IsDisableAuthRequired = config.IsDisableAuthRequired;
- //_JohnSenLedComPort = config.JohnSenLedComPort;
- }
- catch
- {
- }
- }
- private static void LoadDefaultSetting()
- {
- _Instance = new AppSettingConfigModel()
- {
- FirmwareRoot = "./Firmware",
- Language = "zh-chs",
- MES = "sajet2",
- MechineCode = "testMechineCode"
- };
- }
- private static void CompacityChnage(AppSettingConfigModel config)
- {
- if (config == null)
- {
- return;
- }
- if (config.Language.ToLower() == "zh-chs")
- {
- config.Language = "zh-CHS";
- }
- if (config.Language.ToLower() == "en-us")
- {
- config.Language = "en-US";
- }
- }
- }
- }
|