public static void BeginCachedConfiguration() { Debug.Assert(_initialized, "Not initialized."); Debug.Assert(_uncommittedZAMsettings == null, "Configuration already in a cached state. It must be rolled back or committed before calling BeginCachedConfiguration."); try { //_logger.LogInformation($"In BeginCachedConfiguration:\n{_committedJsonStr}"); _uncommittedZAMsettings = JsonConvert.DeserializeObject <ZAMsettings>(_committedJsonStr); _uncommittedZAMsettings.m_readOnly = false; // Because this value is not persisted in json file, set current user manually. _uncommittedZAMsettings.CurrentUserProfile = _committedZAMsettings.CurrentUserProfile; _logger.LogInformation($"Configuration cached."); } catch (Exception ex) { throw new ApplicationException($"Exception occurred trying to load configuration from JSON string.", ex); } }
public static void CommitCachedConfiguration() { Debug.Assert(_initialized, "Not initialized."); Debug.Assert(_uncommittedZAMsettings != null, "Configuration not in a cached state. It must be cached first using BeginCachedConfiguration."); try { string json = JsonConvert.SerializeObject(_uncommittedZAMsettings, Formatting.Indented); File.WriteAllText(FileName, json); //_logger.LogInformation($"In CommitCachedConfiguration:\n{json}"); _committedZAMsettings = _uncommittedZAMsettings; _committedJsonStr = json; _committedZAMsettings.m_readOnly = true; _uncommittedZAMsettings = null; _logger.LogInformation($"Cached configuration saved to file: {FileName}"); } catch (Exception ex) { throw new ApplicationException($"Exception occurred trying to save cached configuration to file: {FileName}", ex); } }
public static void RollbackCachedConfiguration() { Debug.Assert(_initialized, "Not initialized."); Debug.Assert(_uncommittedZAMsettings != null, "Configuration not in a cached state. It must be cached first using RollbackCachedConfiguration."); _uncommittedZAMsettings = null; _logger.LogInformation($"Cached configuration rolled back."); }
public static void Initialize(ILoggerFactory loggerFactory, ZPMonitorService zpMonitorService) { if (_initialized) { return; } _loggerFactory = loggerFactory; _logger = loggerFactory.CreateLogger <ZAMsettings>(); ZPMonitorService = zpMonitorService; JObject parsedJson = null; //bool userFileExists = false; try { try { // Try to load user .json file settings string jsonStr = File.ReadAllText(FileName); parsedJson = JObject.Parse(jsonStr); //userFileExists = true; _logger.LogInformation($"Configuration cached from user settings file {FileName}."); } catch (FileNotFoundException) { // User .json file not found. Try to load default .json file settings string jsonStr = File.ReadAllText(FileNameDefault); parsedJson = JObject.Parse(jsonStr); _logger.LogInformation($"Configuration cached from default settings file {FileNameDefault}. User settings file {FileName} not found."); } // Configuration has been loaded and .json is good. Now deserialize into the settings objects. _committedJsonStr = parsedJson.ToString(); _committedZAMsettings = JsonConvert.DeserializeObject <ZAMsettings>(_committedJsonStr); // this could throw if settings don't match the .json _committedZAMsettings.m_readOnly = true; // Set current user according to default selection. This value is not persisted in json file. _committedZAMsettings.CurrentUserProfile = _committedZAMsettings.DefaultUserProfile; _initialized = true; // Allow any configuration classes to do default initialization (if needed) BeginCachedConfiguration(); bool isInitialized = Settings.Laps.InitializeDefaultValues(); isInitialized = isInitialized || Settings.Splits.InitializeDefaultValues(); // If a Collector or UserProfile needs to be added manually for some reason, that will need to be coded separately foreach (Collector c in Settings.Collectors.Values) { isInitialized = isInitialized || c.InitializeDefaultValues(); } foreach (UserProfile p in Settings.UserProfiles.Values) { isInitialized = isInitialized || p.InitializeDefaultValues(); } if (isInitialized) { // Something changed CommitCachedConfiguration(); } else { // Nothing changed RollbackCachedConfiguration(); } //if (userFileExists) //{ // BeginCachedConfiguration(); // if (!Settings.Collectors.ContainsKey("6 min")) // { // Collector c = new Collector() // { // Name = "6 min", // DurationDesc = "SixMinute", // DurationSecs = 360, // FieldAvgDesc = "Watts", // FieldAvgMaxDesc = "Wkg", // FieldFtpDesc = "Hidden" // }; // Settings.Collectors.Add("6 min", c); // } // Settings.Laps.InitializeDefaultValues(); // CommitCachedConfiguration(); //} } catch (Exception ex) { throw new ApplicationException($"Exception occurred while trying to load configuration.", ex); } }