From 2f540081614fd8d1448e4b2cf70afcf66e4a42b6 Mon Sep 17 00:00:00 2001 From: Adam Nagy Date: Thu, 7 May 2020 15:59:34 +0100 Subject: [PATCH] Add model caching --- .../Controllers/DesignAutomationController.cs | 179 +++++++++++++++--- forgesample/wwwroot/index.html | 30 ++- .../wwwroot/js/ForgeDesignAutomation.js | 8 +- 3 files changed, 178 insertions(+), 39 deletions(-) diff --git a/forgesample/Controllers/DesignAutomationController.cs b/forgesample/Controllers/DesignAutomationController.cs index 10b03df..5d64741 100644 --- a/forgesample/Controllers/DesignAutomationController.cs +++ b/forgesample/Controllers/DesignAutomationController.cs @@ -38,6 +38,8 @@ using Parameter = Autodesk.Forge.DesignAutomation.Model.Parameter; using WorkItem = Autodesk.Forge.DesignAutomation.Model.WorkItem; using WorkItemStatus = Autodesk.Forge.DesignAutomation.Model.WorkItemStatus; +using System.Security.Cryptography; +using System.Text; namespace forgeSample.Controllers @@ -109,8 +111,18 @@ public static string Base64Encode(string plainText) return System.Convert.ToBase64String(plainTextBytes); } + public static string MD5Encode(JObject obj) + { + using (MD5 md5 = MD5.Create()) + { + md5.Initialize(); + md5.ComputeHash(Encoding.UTF8.GetBytes(obj.ToString(Formatting.None))); + return BitConverter.ToString(md5.Hash).Replace("-", ""); + } + } + /// - /// Define a new appbundle + /// Upload sample files /// [HttpPost] [Route("api/forge/designautomation/files")] @@ -150,33 +162,66 @@ public async Task UploadOssFiles([FromBody]JObject appBundleSpecs { dynamic res = await objects.UploadObjectAsync(BucketKey, fileName, (int)streamReader.BaseStream.Length, streamReader.BaseStream, "application/octet-stream"); - // prepare the payload - List outputs = new List() - { - new JobPayloadItem( - JobPayloadItem.TypeEnum.Svf, - new List() - { - JobPayloadItem.ViewsEnum._2d, - JobPayloadItem.ViewsEnum._3d - } - ) - }; - JobPayload job; - string urn = Base64Encode(res.objectId); - job = new JobPayload(new JobPayloadInput(urn), new JobPayloadOutput(outputs)); - - // start the translation - DerivativesApi derivative = new DerivativesApi(); - derivative.Configuration.AccessToken = oauth.access_token; - - derivative.TranslateAsync(job); + TranslateFile(res.objectId, null); } } return Ok(); } + private async Task TranslateFile(string objectId, string rootFileName) + { + dynamic oauth = await OAuthController.GetInternalAsync(); + + // prepare the payload + List outputs = new List() + { + new JobPayloadItem( + JobPayloadItem.TypeEnum.Svf, + new List() + { + JobPayloadItem.ViewsEnum._2d, + JobPayloadItem.ViewsEnum._3d + } + ) + }; + JobPayload job; + string urn = Base64Encode(objectId); + if (rootFileName != null) + { + job = new JobPayload(new JobPayloadInput(urn, true, rootFileName), new JobPayloadOutput(outputs)); + } + else + { + job = new JobPayload(new JobPayloadInput(urn), new JobPayloadOutput(outputs)); + } + + // start the translation + DerivativesApi derivative = new DerivativesApi(); + derivative.Configuration.AccessToken = oauth.access_token; + + await derivative.TranslateAsync(job); + } + + /// + /// Get files in bucket + /// + [HttpGet] + [Route("api/forge/designautomation/files")] + public async Task GetOssFiles() + { + System.Diagnostics.Debug.WriteLine("GetOssFiles"); + // OAuth token + dynamic oauth = await OAuthController.GetInternalAsync(); + + ObjectsApi objects = new ObjectsApi(); + objects.Configuration.AccessToken = oauth.access_token; + + dynamic res = await objects.GetObjectsAsync(BucketKey); + + return Ok(res); + } + /// /// Define a new appbundle /// @@ -296,6 +341,24 @@ public async Task CreateActivity([FromBody]JObject activitySpecs) return Ok(new { Activity = "Activity already defined" }); } + /// + /// Define a new activity + /// + public static async Task IsInCache(string fileName) + { + dynamic oauth = await OAuthController.GetInternalAsync(); + ObjectsApi objects = new ObjectsApi(); + objects.Configuration.AccessToken = oauth.access_token; + + try + { + dynamic res = await objects.GetObjectDetailsAsync (BucketKey, fileName); + return true; + } catch {} + + return false; + } + /// /// Start a new workitem /// @@ -305,12 +368,16 @@ public async Task StartWorkitems([FromBody]JObject input) { System.Diagnostics.Debug.WriteLine("StartWorkitem"); string browerConnectionId = input["browerConnectionId"].Value(); + bool useCache = input["useCache"].Value(); + string pngWorkItemId = "skipped"; + string jsonWorkItemId = "skipped"; + string zipWorkItemId = "skipped"; // OAuth token dynamic oauth = await OAuthController.GetInternalAsync(); string pngFileName = browerConnectionId + ".png"; - string pngWorkItemId = await CreateWorkItem( + pngWorkItemId = await CreateWorkItem( input, new Dictionary() { { "Authorization", "Bearer " + oauth.access_token } }, browerConnectionId, @@ -319,8 +386,48 @@ public async Task StartWorkitems([FromBody]JObject input) string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", BucketKey, pngFileName) ); + if (useCache) { + string hash = MD5Encode(input["params"] as JObject); + string zipFileName = hash + ".zip"; + double [] cells = new double [] { + 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + }; + if (await IsInCache(zipFileName)) + { + JObject data = new JObject( + new JProperty("components", + new JArray( + new JObject( + new JProperty("fileName", zipFileName), + new JProperty("cells", cells) + ) + ) + ) + ); + await SendComponentsDataToClient(browerConnectionId, data); + + return Ok(new { + PngWorkItemId = pngWorkItemId, + JsonWorkItemId = jsonWorkItemId, + ZipWorkItemId = zipWorkItemId + }); + } else { + zipWorkItemId = await CreateWorkItem( + input, + new Dictionary() { { "Authorization", "Bearer " + oauth.access_token } }, + browerConnectionId, + "outputZip", + zipFileName, + string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", BucketKey, zipFileName) + ); + } + } + string jsonFileName = browerConnectionId + ".json"; - string jsonWorkItemId = await CreateWorkItem( + jsonWorkItemId = await CreateWorkItem( input, new Dictionary() { { "Content-Type", "application/json" } }, browerConnectionId, @@ -330,8 +437,9 @@ public async Task StartWorkitems([FromBody]JObject input) ); return Ok(new { - PngWorkItemId = "pngWorkItemId", - JsonWorkItemId = "jsonWorkItemId" + PngWorkItemId = pngWorkItemId, + JsonWorkItemId = jsonWorkItemId, + ZipWorkItemId = zipWorkItemId }); } private async Task CreateWorkItem(JObject input, Dictionary headers, string browerConnectionId, string outputName, string fileName, string url) @@ -370,6 +478,12 @@ private async Task CreateWorkItem(JObject input, Dictionary /// Define a new appbundle /// test with curl: @@ -379,13 +493,12 @@ private async Task CreateWorkItem(JObject input, Dictionary [HttpPut] [Route("api/forge/callback/ondata/json")] - public async Task OnData([FromRoute] string dataType, [FromQuery] string id, [FromBody] JObject data) + public async Task OnData([FromQuery] string id, [FromBody] JObject data) { - System.Diagnostics.Debug.WriteLine("OnData, dataType = " + dataType); + System.Diagnostics.Debug.WriteLine("OnData"); // urnBase, something like "urn:adsk.objects:os.object:rgm0mo9jvssd2ybedk9mrtxqtwsa61y0-designautomation/" - data["urnBase"] = "urn:adsk.objects:os.object:" + BucketKey + "/"; - await _hubContext.Clients.Client(id).SendAsync("onComponents", data.ToString(Formatting.None)); + await SendComponentsDataToClient(id, data); return Ok(); } @@ -419,6 +532,12 @@ public async Task OnComplete(string id, string outputFile, [FromB dynamic signedUrl = await objects.CreateSignedResourceAsyncWithHttpInfo(BucketKey, outputFile, new PostBucketsSigned(10), "read"); await _hubContext.Clients.Client(id).SendAsync("onPicture", (string)(signedUrl.Data.signedUrl)); } + + if (outputFile.EndsWith(".zip")) + { + string objectId = "urn:adsk.objects:os.object:" + BucketKey + "/" + outputFile; + TranslateFile(objectId, "shelves.iam"); + } } catch (Exception e) { diff --git a/forgesample/wwwroot/index.html b/forgesample/wwwroot/index.html index 4477c16..1918524 100644 --- a/forgesample/wwwroot/index.html +++ b/forgesample/wwwroot/index.html @@ -19,7 +19,7 @@