From bc5ef8a5b397a7a098cf3b6957945ecd84c58c54 Mon Sep 17 00:00:00 2001 From: Matt Moening Date: Wed, 22 Jun 2016 18:05:52 -0700 Subject: [PATCH] Implemented MC side receive of Public Profile list and public profiles --- MatterControl.csproj | 3 + .../SetupStepMakeModelName.cs | 6 +- SettingsManagement/OemSettings.cs | 52 +++++++------ .../Settings/ProfileManager.cs | 15 ++-- .../MatterControl.AutomationTests.csproj | 1 + .../RetrievePublicProfileTest.cs | 77 +++++++++++++++++++ VersionManagement/PublicProfilesRequest.cs | 44 +++++++++++ .../RetrievePublicProfileRequest.cs | 67 ++++++++++++++++ 8 files changed, 229 insertions(+), 36 deletions(-) create mode 100644 Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs create mode 100644 VersionManagement/PublicProfilesRequest.cs create mode 100644 VersionManagement/RetrievePublicProfileRequest.cs diff --git a/MatterControl.csproj b/MatterControl.csproj index 5b8b782ec..2db41c457 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -369,8 +369,10 @@ + + @@ -415,6 +417,7 @@ + diff --git a/PrinterControls/PrinterConnections/SetupStepMakeModelName.cs b/PrinterControls/PrinterConnections/SetupStepMakeModelName.cs index 7fc5b318b..c316c3eb5 100644 --- a/PrinterControls/PrinterConnections/SetupStepMakeModelName.cs +++ b/PrinterControls/PrinterConnections/SetupStepMakeModelName.cs @@ -181,13 +181,13 @@ namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections activeMake = ((Agg.UI.DropDownList)sender).SelectedValue; activeModel = null; - List printers; + Dictionary printers; if (!OemSettings.Instance.OemProfiles.TryGetValue(activeMake, out printers)) { - printers = new List(); + printers = new Dictionary(); } - printerModelSelector.ListSource = printers.Select(name => new KeyValuePair(name, name)).ToList(); + printerModelSelector.ListSource = printers.Select(profile => new KeyValuePair(profile.Key, profile.Value)).ToList(); contentRow.Invalidate(); diff --git a/SettingsManagement/OemSettings.cs b/SettingsManagement/OemSettings.cs index 47fb3ab8f..b14059126 100644 --- a/SettingsManagement/OemSettings.cs +++ b/SettingsManagement/OemSettings.cs @@ -29,6 +29,7 @@ either expressed or implied, of the FreeBSD Project. using MatterHackers.Agg.PlatformAbstract; using MatterHackers.MatterControl.DataStorage; +using MatterHackers.MatterControl.VersionManagement; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -114,7 +115,7 @@ namespace MatterHackers.MatterControl.SettingsManagement public List> AllOems { get; private set; } - public Dictionary> OemProfiles { get; private set; } + public Dictionary> OemProfiles { get; set; } [OnDeserialized] private void Deserialized(StreamingContext context) @@ -125,36 +126,39 @@ namespace MatterHackers.MatterControl.SettingsManagement // Request the latest content, passing along the ETAG // Refresh our cache if needed, otherwise stick with the cached data - // For now, refresh every time - string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", "profiles"); + PublicProfilesRequest profileRequest = new PublicProfilesRequest(); + profileRequest.Request(); - // Ensure directory exists - Directory.CreateDirectory(cacheDirectory); + //// For now, refresh every time + //string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", "profiles"); - // Cache file path - string cachePath = Path.Combine(cacheDirectory, "oemprofiles.json"); + //// Ensure directory exists + //Directory.CreateDirectory(cacheDirectory); - try - { - var fileInfo = new FileInfo(cachePath); - if (!fileInfo.Exists || (DateTime.Now - fileInfo.LastWriteTime).TotalHours > 1) - { - string url = "http://matterdata.azurewebsites.net/api/oemprofiles"; + //// Cache file path + //string cachePath = Path.Combine(cacheDirectory, "oemprofiles.json"); - var client = new WebClient(); + //try + //{ + // var fileInfo = new FileInfo(cachePath); + // if (!fileInfo.Exists || (DateTime.Now - fileInfo.LastWriteTime).TotalHours > 1) + // { + // string url = "http://matterdata.azurewebsites.net/api/oemprofiles"; - File.WriteAllText(cachePath, client.DownloadString(url)); - } - } - catch (Exception ex) - { - System.Diagnostics.Trace.WriteLine("An unexpected exception occurred while requesting the latest oem profiles: \r\n" + ex.Message); - } + // var client = new WebClient(); - string profilesText = File.ReadAllText(cachePath); - OemProfiles = JsonConvert.DeserializeObject>>(profilesText); + // File.WriteAllText(cachePath, client.DownloadString(url)); + // } + //} + //catch (Exception ex) + //{ + // System.Diagnostics.Trace.WriteLine("An unexpected exception occurred while requesting the latest oem profiles: \r\n" + ex.Message); + //} - SetManufacturers(OemProfiles.Select(m => new KeyValuePair(m.Key, m.Key)).ToList()); + //string profilesText = File.ReadAllText(cachePath); + //OemProfiles = JsonConvert.DeserializeObject>>(profilesText); + + //SetManufacturers(OemProfiles.Select(m => new KeyValuePair(m.Key, m.Key)).ToList()); } private OemSettings() diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index 74d822a08..9bd94a87f 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -38,6 +38,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { using Agg; using Localizations; + using VersionManagement; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Net; @@ -272,18 +273,14 @@ namespace MatterHackers.MatterControl.SlicerConfiguration private static OemProfile LoadHttpOemProfile(string make, string model) { - string url = string.Format( - "http://matterdata.azurewebsites.net/api/oemprofiles?manufacturer={0}&model={1}", - WebUtility.UrlEncode(make), - WebUtility.UrlEncode(model)); - - var client = new WebClient(); - - string profileText = client.DownloadString(url); - var printerProfile = JsonConvert.DeserializeObject(profileText); + RetrievePublicProfileRequest profileRequest = new RetrievePublicProfileRequest(); + string profText = profileRequest.getPrinterProfileByMakeModel(make, model); + var printerProfile = JsonConvert.DeserializeObject(profText); + return printerProfile; } + private static void Profiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // Any time the list changes, persist the updates to disk diff --git a/Tests/MatterControl.AutomationTests/MatterControl.AutomationTests.csproj b/Tests/MatterControl.AutomationTests/MatterControl.AutomationTests.csproj index 8acff0d3f..c92ddf86b 100644 --- a/Tests/MatterControl.AutomationTests/MatterControl.AutomationTests.csproj +++ b/Tests/MatterControl.AutomationTests/MatterControl.AutomationTests.csproj @@ -68,6 +68,7 @@ + diff --git a/Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs b/Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs new file mode 100644 index 000000000..8c8a83f2c --- /dev/null +++ b/Tests/MatterControl.AutomationTests/RetrievePublicProfileTest.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using MatterHackers.MatterControl.VersionManagement; +using System.IO; +using MatterHackers.MatterControl.DataStorage; +using MatterHackers.MatterControl.SettingsManagement; +using System.Threading; +using MatterHackers.MatterControl.Tests.Automation; +using MatterHackers.Agg.UI.Tests; +using MatterHackers.GuiAutomation; +using MatterHackers.Agg.UI; +using Newtonsoft.Json; +using MatterHackers.Agg.PlatformAbstract; + +namespace MatterControl.Tests.MatterControl +{ + [TestFixture, RunInApplicationDomain] + public class RetrievePublicProfileTest + { + private string deviceToken = null; + + [Test,RunInApplicationDomain] + public void RetrievePrinterProfileListWorking() + { + + StaticData.Instance = new MatterHackers.Agg.FileSystemStaticData(Path.Combine("..", "..", "..", "..", "StaticData")); + + string profilePath = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", "profiles", "oemprofiles.json"); + if(File.Exists(profilePath)) + { + File.Delete(profilePath); + } + //MatterControlUtilities.OverrideAppDataLocation(); + AutoResetEvent requestCompleteWaiter = new AutoResetEvent(false); + PublicProfilesRequest retrieveProfiles = new PublicProfilesRequest(); + retrieveProfiles.URI = "https://mattercontrol-test.appspot.com/api/1/device/get-public-profile-list"; + + + retrieveProfiles.RequestComplete += (sender, eArgs) => { requestCompleteWaiter.Set(); }; + + retrieveProfiles.Request(); + Assert.IsTrue(requestCompleteWaiter.WaitOne()); + + Assert.IsTrue(File.Exists(profilePath)); + + //Call Retrieve Profile next + RetrievePrinterProfileWorking(); + } + + //[Test,Category("CloudProfiles")] + public void RetrievePrinterProfileWorking() + { + string make = OemSettings.Instance.OemProfiles.First().Key; + string model = OemSettings.Instance.OemProfiles[make].First().Key; + deviceToken = OemSettings.Instance.OemProfiles[make][model]; + string expectedProfilePath = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "Profiles", string.Format("{0}.json", deviceToken)); + if (File.Exists(expectedProfilePath)) + { + File.Delete(expectedProfilePath); + } + RetrievePublicProfileRequest request = new RetrievePublicProfileRequest(); + RetrievePublicProfileRequest.DownloadBaseUrl = "https://mattercontrol-test.appspot.com/api/1/device/get-public-profile"; + string recievedPrinterProfile = request.getPrinterProfileByMakeModel(make,model); + RetrievePublicProfileRequest.DownloadBaseUrl = "https://mattercontrol.appspot.com/api/1/device/get-public-profile"; + + Assert.IsNotNullOrEmpty(recievedPrinterProfile); + //Assert.AreEqual(expectedProfilePath, recievedProfilePath,"Recieved Profile path does not match expected path."); + //Assert.IsTrue(File.Exists(expectedProfilePath)); + } + + + } +} diff --git a/VersionManagement/PublicProfilesRequest.cs b/VersionManagement/PublicProfilesRequest.cs new file mode 100644 index 000000000..6666b2c4a --- /dev/null +++ b/VersionManagement/PublicProfilesRequest.cs @@ -0,0 +1,44 @@ +using MatterHackers.MatterControl.DataStorage; +using MatterHackers.MatterControl.SettingsManagement; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MatterHackers.MatterControl.VersionManagement +{ + class PublicProfilesRequest : WebRequestBase + { + public PublicProfilesRequest() + { + //requestValues["ProjectToken"] = VersionInfo.Instance.ProjectToken; +#if DEBUG + uri = "https://mattercontrol-test.appspot.com/api/1/device/get-public-profile-list"; +#else + uri = "https://mattercontrol.appspot.com/api/1/device/get-public-profile-list"; +#endif + } + + internal string URI { get { return uri; } set { uri = value; } } + + public override void ProcessSuccessResponse(JsonResponseDictionary responseValues) + { + //For now, refresh every time + string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache", "profiles"); + //Ensure directory exists + Directory.CreateDirectory(cacheDirectory); + //Cache File Path + string cachePath = Path.Combine(cacheDirectory, "oemprofiles.json"); + File.WriteAllText(cachePath, responseValues["ProfileList"]); + + + OemSettings.Instance.OemProfiles = JsonConvert.DeserializeObject>>(responseValues["ProfileList"]); + OemSettings.Instance.SetManufacturers(OemSettings.Instance.OemProfiles.Select(m => new KeyValuePair(m.Key, m.Key)).ToList()); + } + + + } +} diff --git a/VersionManagement/RetrievePublicProfileRequest.cs b/VersionManagement/RetrievePublicProfileRequest.cs new file mode 100644 index 000000000..9349265b5 --- /dev/null +++ b/VersionManagement/RetrievePublicProfileRequest.cs @@ -0,0 +1,67 @@ +using MatterHackers.MatterControl.DataStorage; +using MatterHackers.MatterControl.SettingsManagement; +using System.IO; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +namespace MatterHackers.MatterControl.VersionManagement +{ + class RetrievePublicProfileRequest + { + internal static string DownloadBaseUrl { get; set; } + + public RetrievePublicProfileRequest() + { +#if DEBUG + DownloadBaseUrl = "https://mattercontrol-test.appspot.com/api/1/device/get-public-profile"; +#else + DownloadBaseUrl = "https://mattercontrol.appspot.com/api/1/device/get-public-profile"; +#endif + } + + public string getPrinterProfileByMakeModel(string make, string model) + { + string deviceToken = OemSettings.Instance.OemProfiles[make][model]; + string profiletext = DownloadPrinterProfile(deviceToken); + return profiletext; + } + + internal static string DownloadPrinterProfile(string deviceToken) + { + // TODO: Enable caching + //Keept track of version. When retrieving check version + + string url = DownloadBaseUrl + string.Format("/{0}",deviceToken); + + string profilePath = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath,"Profiles",string.Format("{0}.json",deviceToken)); + WebClient client = new WebClient(); + string profileText = client.DownloadString(url); + //File.WriteAllText(profileText, profilePath); + + return profileText; + //HttpClient client = new HttpClient(); + + //Get a pemporaty path to write to during download. If download completes without error we move this file to the proper path + //string tempFilePath = ApplicationDataStorage.Instance.GetTempFileName(".json"); + + //byte[] buffer = new byte[65536]; + //using (var writeStream = File.Create(tempFilePath)) + //using (var instream = await client.GetStreamAsync(url)) + //{ + // int bytesRead = await instream.ReadAsync(buffer, 0, buffer.Length); + // while(bytesRead != 0) + // { + // writeStream.Write(buffer, 0, bytesRead); + + // bytesRead = await instream.ReadAsync(buffer, 0, buffer.Length); + // } + //} + + //File.Move(tempFilePath, profilePath); + + //return profilePath; + } + //Used in test to access test server before changes go onto live server + } +}