From 1115f05da03db16d0295fa4bfcc2d743cfc1509e Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 14 Jul 2016 16:48:18 -0700 Subject: [PATCH 1/2] Added Make and Model properties to PrinterInfo class and made sure that when we add a new printer, the Make and Model are written to {user}.json --- SlicerConfiguration/Settings/ProfileManager.cs | 4 +++- SlicerConfiguration/Settings/SettingsProfile.cs | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index 404e19f61..9e66ba9ba 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -330,7 +330,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration Instance.Profiles.Add(new PrinterInfo { Name = printerName, - ID = guid + ID = guid, + Make = make, + Model = model }); // Update SHA1 diff --git a/SlicerConfiguration/Settings/SettingsProfile.cs b/SlicerConfiguration/Settings/SettingsProfile.cs index f2e2f8707..788f8dbc6 100644 --- a/SlicerConfiguration/Settings/SettingsProfile.cs +++ b/SlicerConfiguration/Settings/SettingsProfile.cs @@ -912,6 +912,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public string ComPort { get; set; } public string ID { get; set; } public string Name { get; set; } + public string Make { get; set; } + public string Model { get; set; } public string DeviceToken { get; set; } public bool IsDirty { get; set; } = false; public bool MarkedForDelete { get; set; } = false; From b1b88c01def2936adc2c7dda45cef52dd7751c79 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 15 Jul 2016 17:25:27 -0700 Subject: [PATCH 2/2] Added ability to recover a usable public profile in the event that we attempt to load a corrupt one. --- .../Settings/LayeredProfile.cs | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/SlicerConfiguration/Settings/LayeredProfile.cs b/SlicerConfiguration/Settings/LayeredProfile.cs index f027da49f..bf4a3528a 100644 --- a/SlicerConfiguration/Settings/LayeredProfile.cs +++ b/SlicerConfiguration/Settings/LayeredProfile.cs @@ -36,6 +36,8 @@ using System.IO; using Newtonsoft.Json.Linq; using System.Text; using System.Collections.ObjectModel; +using MatterHackers.MatterControl.DataStorage; +using MatterHackers.MatterControl.SettingsManagement; namespace MatterHackers.MatterControl.SlicerConfiguration { @@ -201,10 +203,19 @@ namespace MatterHackers.MatterControl.SlicerConfiguration /// User settings overrides /// public PrinterSettingsLayer UserLayer { get; } = new PrinterSettingsLayer(); - + // public static PrinterSettings LoadFile(string printerProfilePath) { - var jObject = JObject.Parse(File.ReadAllText(printerProfilePath)); + + JObject jObject; + try + { + jObject = JObject.Parse(File.ReadAllText(printerProfilePath)); + } + catch + { + return RecoverProfile(printerProfilePath); + } int documentVersion = jObject?.GetValue("DocumentVersion")?.Value() ?? 0; @@ -214,7 +225,33 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } // Reload the document with the new schema - return JsonConvert.DeserializeObject(File.ReadAllText(printerProfilePath)); + try + { + return JsonConvert.DeserializeObject(File.ReadAllText(printerProfilePath)); + } + catch + { + return RecoverProfile(printerProfilePath); + } + } + + public static PrinterSettings RecoverProfile(string printerProfilePath) + { + string profileKey = Path.GetFileNameWithoutExtension(printerProfilePath); + var profile = ProfileManager.Instance[profileKey]; + string publicProfileDeviceToken = OemSettings.Instance.OemProfiles[profile.Make][profile.Model]; + string publicProfileToLoad = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", "profiles") + "\\" + publicProfileDeviceToken + ".json"; + + var oemProfile = JsonConvert.DeserializeObject(File.ReadAllText(publicProfileToLoad)); + oemProfile.ID = profile.ID; + oemProfile.SetValue(SettingsKey.printer_name, profile.Name); + oemProfile.DocumentVersion = PrinterSettings.LatestVersion; + + var profileHelper = new SettingsProfile(oemProfile); + profileHelper.SetComPort(profile.ComPort); + profileHelper.SaveChanges(); + + return oemProfile; } // TODO: Hookup OEM layers