diff --git a/MatterControlApplication.cs b/MatterControlApplication.cs index c8da885b6..f6a88fd0b 100644 --- a/MatterControlApplication.cs +++ b/MatterControlApplication.cs @@ -125,7 +125,7 @@ namespace MatterHackers.MatterControl /// /// The custom collector function to load the content /// - internal static T LoadCacheable(string cacheKey, string cacheScope, Func collector) where T : class + internal static T LoadCacheable(string cacheKey, string cacheScope, Func collector, string staticDataFallbackPath = null) where T : class { string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", cacheScope); string cachePath = Path.Combine(cacheDirectory, cacheKey); @@ -158,9 +158,19 @@ namespace MatterHackers.MatterControl return JsonConvert.DeserializeObject(File.ReadAllText(cachePath)); } catch + { + //Fallback to Static Data + } + + try + { + return JsonConvert.DeserializeObject(StaticData.Instance.ReadAllText(staticDataFallbackPath)); + } + catch { return default(T); } + } private MatterControlApplication(double width, double height) diff --git a/SettingsManagement/OemSettings.cs b/SettingsManagement/OemSettings.cs index 21516326d..071de84d7 100644 --- a/SettingsManagement/OemSettings.cs +++ b/SettingsManagement/OemSettings.cs @@ -124,10 +124,19 @@ namespace MatterHackers.MatterControl.SettingsManagement [OnDeserialized] private void Deserialized(StreamingContext context) { - var oemProfiles = MatterControlApplication.LoadCacheable>>( - "oemprofiles.json", + //Load from Static Data to prepopulate oemProfiles for when user create a printer before load cacheable is done + var staticDataList = JsonConvert.DeserializeObject>>(StaticData.Instance.ReadAllText(Path.Combine("Profiles","allModels.json"))); + var oemProfs = staticDataList.Select(x => new KeyValuePair>(x.Key ,x.Value.ToDictionary(y=>y,y=>y))).ToDictionary(x=>x.Key,x=>x.Value); + OemProfiles = oemProfs; + var manufacturesList = staticDataList.Select(m => new KeyValuePair(m.Key, m.Key)).ToList(); + SetManufacturers(manufacturesList); + //Attempt to update from online + Task.Run(() => + { + var oemProfiles = MatterControlApplication.LoadCacheable>>( + "oemprofiles.json", "profiles", - () => + () => { string responseText = null; @@ -143,19 +152,24 @@ namespace MatterHackers.MatterControl.SettingsManagement return responseText; }); - - OemProfiles = oemProfiles; - var manufactures = oemProfiles.Select(m => new KeyValuePair(m.Key, m.Key)).ToList(); - // sort by value (printer name) - manufactures.Sort( - delegate (KeyValuePair pair1, - KeyValuePair pair2) + //If we failed to get anything from load cacheable dont override potentally populated feilds + if(oemProfiles != default(Dictionary < string, Dictionary < string, string>>)) { - return pair1.Value.CompareTo(pair2.Value); + OemProfiles = oemProfiles; + var manufactures = oemProfiles.Select(m => new KeyValuePair(m.Key, m.Key)).ToList(); + // sort by value (printer name) + manufactures.Sort( + delegate (KeyValuePair pair1, + KeyValuePair pair2) + { + return pair1.Value.CompareTo(pair2.Value); + } + ); + SetManufacturers(manufactures); + Task.Run((Action)downloadMissingProfiles); } - ); - SetManufacturers(manufactures); - Task.Run((Action)downloadMissingProfiles); + + }); } private void downloadMissingProfiles() diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index f6bf5cf0c..ac7201de9 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -381,7 +381,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration responseText = RetrievePublicProfileRequest.DownloadPrinterProfile(deviceToken); } return responseText; - }); + }, + Path.Combine("Profiles",make,String.Format("{0}{1}",model,ProfileManager.ProfileExtension))); } public void EnsurePrintersImported()