/// <summary> /// Gets the file from the filepath, validates the file, and converts it to the master switch model /// </summary> /// <param name="filePath">File path for the master switch_config.json file to be used to convert</param> /// <returns>MasterSwitchModel if successful or null if unsuccessful</returns> public MasterSwitchModel GetMasterSwitchModelFromFile(string filePath) { MasterSwitchModel model = null; string json = null; try { //read master switch config file into string using (StreamReader sr = new StreamReader(filePath)) { json = sr.ReadToEnd(); } } catch (Exception e) { MessageBox.Show("The master switch config file could not be read from the file. Please check that it exists.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); _log.Error(e); return(model); } if (string.IsNullOrEmpty(json)) { MessageBox.Show("Master switch JSON file is empty. Please check that the master switch config is correct.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); _log.Warn("Master switch JSON file is empty after loading file."); return(model); } else { SchemaModel schemaModel = new SchemaModel(); if (ValidateJSON(json, schemaModel.GetMasterSwitchSchema())) { //if correct json format, write it into master switchModel try { model = JsonConvert.DeserializeObject <MasterSwitchModel>(json); } catch (Exception e) { MessageBox.Show("Could not convert master switch config file. Please be sure that master switch config file is correct.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); _log.Error(e); return(model); } } else { MessageBox.Show("Could not validate master switch config file. Please be sure that master switch config file is correct.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); _log.Warn("Could not validate master switch config file."); return(model); } } return(model); }