This commit is contained in:
Lars Brubaker 2016-07-21 15:07:02 -07:00
commit 3e32347c88
3 changed files with 41 additions and 16 deletions

View file

@ -125,7 +125,7 @@ namespace MatterHackers.MatterControl
/// </summary>
/// <param name="collector">The custom collector function to load the content</param>
/// <returns></returns>
internal static T LoadCacheable<T>(string cacheKey, string cacheScope, Func<string> collector) where T : class
internal static T LoadCacheable<T>(string cacheKey, string cacheScope, Func<string> 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<T>(File.ReadAllText(cachePath));
}
catch
{
//Fallback to Static Data
}
try
{
return JsonConvert.DeserializeObject<T>(StaticData.Instance.ReadAllText(staticDataFallbackPath));
}
catch
{
return default(T);
}
}
private MatterControlApplication(double width, double height)

View file

@ -124,10 +124,19 @@ namespace MatterHackers.MatterControl.SettingsManagement
[OnDeserialized]
private void Deserialized(StreamingContext context)
{
var oemProfiles = MatterControlApplication.LoadCacheable<Dictionary<string, Dictionary<string, string>>>(
"oemprofiles.json",
//Load from Static Data to prepopulate oemProfiles for when user create a printer before load cacheable is done
var staticDataList = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(StaticData.Instance.ReadAllText(Path.Combine("Profiles","allModels.json")));
var oemProfs = staticDataList.Select(x => new KeyValuePair<string,Dictionary<string,string>>(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<string, string>(m.Key, m.Key)).ToList();
SetManufacturers(manufacturesList);
//Attempt to update from online
Task.Run(() =>
{
var oemProfiles = MatterControlApplication.LoadCacheable<Dictionary<string, Dictionary<string, string>>>(
"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<string, string>(m.Key, m.Key)).ToList();
// sort by value (printer name)
manufactures.Sort(
delegate (KeyValuePair<string, string> pair1,
KeyValuePair<string, string> 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<string, string>(m.Key, m.Key)).ToList();
// sort by value (printer name)
manufactures.Sort(
delegate (KeyValuePair<string, string> pair1,
KeyValuePair<string, string> pair2)
{
return pair1.Value.CompareTo(pair2.Value);
}
);
SetManufacturers(manufactures);
Task.Run((Action)downloadMissingProfiles);
}
);
SetManufacturers(manufactures);
Task.Run((Action)downloadMissingProfiles);
});
}
private void downloadMissingProfiles()

View file

@ -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()