From d0ce7a56005e52a68d161008640b6ddf8f7832e7 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Wed, 10 Aug 2016 09:49:32 -0700 Subject: [PATCH] Move recovery orchestration into ProfileManager.LoadProfileAsync --- SetupWizard/ImportSettingsPage.cs | 1 + .../Settings/ProfileManager.cs | 47 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/SetupWizard/ImportSettingsPage.cs b/SetupWizard/ImportSettingsPage.cs index ebd3e5aeb..7bacd4a63 100644 --- a/SetupWizard/ImportSettingsPage.cs +++ b/SetupWizard/ImportSettingsPage.cs @@ -62,6 +62,7 @@ namespace MatterHackers.MatterControl this.destinationLayer = destinationLayer; this.sectionName = sectionName; + // TODO: Need to handle load failures for import attempts settingsToImport = PrinterSettings.LoadFile(settingsFilePath); this.headerLabel.Text = "Select What to Import".Localize(); diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index 175b1442c..697dc0ad3 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -124,7 +124,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration // Load the last selected printer profile or an empty profile ActiveSliceSettings.Instance = Instance.LoadLastProfile() ?? LoadEmptyProfile(); - // In either case, wire up the CollectionChanged event Instance.Profiles.CollectionChanged += Profiles_CollectionChanged; } @@ -206,7 +205,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public PrinterSettings LoadLastProfile() { - return LoadProfile(this.LastProfileID); + return LoadProfileAsync(this.LastProfileID); } public void SetLastProfile(string printerID) @@ -228,15 +227,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } /// - /// Loads the specified PrinterProfile + /// Loads the specified PrinterProfile, performing recovery options if required /// /// The profile ID to load /// Return the in memory instance if already loaded. Alternatively, reload from disk /// - public static PrinterSettings LoadProfile(string profileID, bool useActiveInstance = true) + public static async Task LoadProfileAsync(string profileID, bool useActiveInstance = true) { + var printerInfo = ProfileManager.Instance[profileID]; + // Only load profiles by ID that are defined in the profiles document - if (ProfileManager.Instance[profileID] == null) + if (printerInfo == null) { return null; } @@ -246,19 +247,37 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return ActiveSliceSettings.Instance; } - string profilePath = Path.Combine(ProfilesPath, profileID + ProfileManager.ProfileExtension); - return File.Exists(profilePath) ? LoadProfileFromDisk(profilePath) : null; - } + PrinterSettings printerSettings; - internal static PrinterSettings LoadProfileFromDisk(string profilePath) - { - if (File.Exists(profilePath)) + string profilePath = Path.Combine(ProfilesPath, profileID + ProfileManager.ProfileExtension); + if (!File.Exists(profilePath)) { - return PrinterSettings.LoadFile(profilePath); + // Attempt to load from MCWS if missing on disk + printerSettings = await ApplicationController.GetPrinterProfileAsync(printerInfo, null); + if (printerSettings != null) + { + // If successful, persist downloaded profile + printerSettings.Save(); + } + + return printerSettings; + } + + // LoadOrRecoverProfile - if exists on disk, attempt to load or fall back using recovery logic + printerSettings = PrinterSettings.LoadFile(profilePath, performMigrations: true); + if (printerSettings != null) + { + return printerSettings; } else { - return LoadEmptyProfile(); + int delayDuration = MatterControlApplication.IsLoading ? 4 : 0; + + // Schedule a recovery rather than blocking until the MCWS and/or OemProfile restore complete + UiThread.RunOnIdle(() => PrinterSettings.RecoverProfile(printerInfo), delayDuration); + + // Return a short lived profile which should be reset after recovery + return ProfileManager.LoadEmptyProfile(); } } @@ -279,7 +298,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration switch (importType) { case ProfileManager.ProfileExtension: - var profile = ProfileManager.LoadProfileFromDisk(settingsFilePath); + var profile = PrinterSettings.LoadFile(settingsFilePath); profile.ID = printerInfo.ID; profile.ClearValue(SettingsKey.device_token); printerInfo.DeviceToken = "";