diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index fc0dc885..0e754846 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.Database/Controllers/NcToolManagerController.cs b/Step.Database/Controllers/NcToolManagerController.cs index f8c6e17b..493c2bc2 100644 --- a/Step.Database/Controllers/NcToolManagerController.cs +++ b/Step.Database/Controllers/NcToolManagerController.cs @@ -228,7 +228,7 @@ namespace Step.Database.Controllers return dtoShanks; } - public List GetMountedTools() + public List GetMountedTools() { List tools = FindToolsWithDependencies() .Where(x => x.Shank != null && x.Shank.MagazineId != null) diff --git a/Step.Model/Constants.cs b/Step.Model/Constants.cs index 1788a138..d1606740 100644 --- a/Step.Model/Constants.cs +++ b/Step.Model/Constants.cs @@ -6,6 +6,8 @@ namespace Step.Model { public static class Constants { + public const double EPSILON = 0.001; + public enum ROLE_IDS { CMS_SERVICE_ONLY = 1, @@ -98,10 +100,6 @@ namespace Step.Model public const string UNDER_HOOD = "underHood"; } - // Filenames - - public const string MAINTENANCE_ATTACHMENT_PATH = "C:\\CMS\\STEP\\attachment\\"; - // Config File Names public static readonly string BASE_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); public const string CONFIG_DIRECTORY = "Config\\"; @@ -190,5 +188,11 @@ namespace Step.Model public const string TOOL_IS_MOUNTED = "error_tool_mounted"; public const string OPTION_NOT_ACTIVE = "error_option_not_active"; } + + + // File paths + public const string MAINTENANCE_ATTACHMENT_PATH = "C:\\CMS\\STEP\\attachment\\"; + public const string TEMP_FILE = @"C:/CMS/STEP/attachment/tmp/"; + public const string PART_PRG_IMAGES = @"C:/CMS/STEP/attachment/prg_images/"; } } diff --git a/Step.Model/DTOModels/DTOAxesModel.cs b/Step.Model/DTOModels/DTOAxesModel.cs index 11d758ad..ef18b788 100644 --- a/Step.Model/DTOModels/DTOAxesModel.cs +++ b/Step.Model/DTOModels/DTOAxesModel.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace Step.Model.DTOModels @@ -17,7 +18,6 @@ namespace Step.Model.DTOModels machine = new Dictionary(); programmePos = new Dictionary(); toGo = new Dictionary(); - followingErr = new Dictionary(); } public struct AxisModel @@ -32,17 +32,15 @@ namespace Step.Model.DTOModels if (item == null) return false; - if (!interpolated.SequenceEqual(item.interpolated)) + if (!CheckAxesDictionaries(interpolated, item.interpolated)) return false; - if (!machine.SequenceEqual(item.machine)) + if (!CheckAxesDictionaries(machine, item.machine)) return false; - if (!programmePos.SequenceEqual(item.programmePos)) + if (!CheckAxesDictionaries(programmePos , item.programmePos)) return false; - if (!toGo.SequenceEqual(item.toGo)) + if (!CheckAxesDictionaries(toGo, item.toGo)) return false; - if (!followingErr.SequenceEqual(item.followingErr)) - return false; - + return true; } @@ -50,5 +48,33 @@ namespace Step.Model.DTOModels { return base.GetHashCode(); } + + private bool CheckAxesDictionaries(IDictionary x, IDictionary y) + { + // early-exit checks + if (y == null) + return x == null; + if (x == null) + return false; + + if (x.Count != y.Count) + return false; + + // Check keys are the same + foreach (string k in x.Keys) + if (!y.ContainsKey(k)) + return false; + + // Check values are differen + foreach (string k in x.Keys) + { + var a = Math.Abs(y[k] - x[k]); + + if (Math.Round(a, 3) >= Constants.EPSILON) + return false; + } + + return true; + } } } \ No newline at end of file diff --git a/Step/Controllers/WebApi/NcFileController.cs b/Step/Controllers/WebApi/NcFileController.cs index aab82083..19d412f2 100644 --- a/Step/Controllers/WebApi/NcFileController.cs +++ b/Step/Controllers/WebApi/NcFileController.cs @@ -1,11 +1,13 @@ using Step.Model.DTOModels; using Step.NC; +using Step.Utils; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; -using System.Net.Http.Headers; +using static Step.Model.Constants; +using System.Text; using System.Threading.Tasks; using System.Web.Http; using static CMS_CORE_Library.DataStructures; @@ -76,37 +78,31 @@ namespace Step.Controllers.WebApi } [Route("upload"), HttpPost] - public async Task AddAttachment() + public async Task Post() { - // Check whether the POST operation is MultiPart? if (!Request.Content.IsMimeMultipartContent()) + { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); - - // Create CustomMultipartFormDataStreamProvider - CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider("C:\\CMS\\STEP\\"); - // MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH); - List files = new List(); - - // Read all contents of multipart message into CustomMultipartFormDataStreamProvider. - var result = await Request.Content.ReadAsMultipartAsync(provider); - - // Send OK Response along with saved file names to the client. - return Ok(); - } - - // Override MultipartFormDataStreamProvider to override the GetLocalFileName - public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider - { - public CustomMultipartFormDataStreamProvider(string path) : base(path) - { } - public override string GetLocalFileName(HttpContentHeaders headers) - { - var fileName = headers.ContentDisposition.FileName.Replace("\"", string.Empty); + List formItems = await MultipartHandler.ManageMultiPartFileAsync(Request); - return Path.GetFileNameWithoutExtension(fileName) + Guid.NewGuid() + Path.GetExtension(fileName); + foreach (FormItem item in formItems) + { + if (item.IsAFile) + { + if(item.ParameterName == "file") + { + File.WriteAllBytes(TEMP_FILE + item.FileName, item.Data); + } + else if(item.ParameterName == "image") + { + File.WriteAllBytes(PART_PRG_IMAGES + item.FileName, item.Data); + } + } } + + return Request.CreateResponse(HttpStatusCode.OK); } } } \ No newline at end of file diff --git a/Step/MultipartHandler.cs b/Step/MultipartHandler.cs new file mode 100644 index 00000000..57fb43e3 --- /dev/null +++ b/Step/MultipartHandler.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Step +{ + public class FormItem + { + public string ParameterName { get; set; } + public byte[] Data { get; set; } + public string FileName { get; set; } + public string MediaType { get; set; } + public string Value { get { return Encoding.Default.GetString(Data); } } + public bool IsAFile { get { return !String.IsNullOrEmpty(FileName); } } + } + + public static class MultipartHandler + { + public static async Task> ManageMultiPartFileAsync(HttpRequestMessage Request) + { + var provider = new MultipartMemoryStreamProvider(); + // Read multipart data + await Request.Content.ReadAsMultipartAsync(provider); + + var formItems = new List(); + + // Scan the Multiple Parts + foreach (HttpContent contentPart in provider.Contents) + { + // Populate list with form-data parameters + var formItem = new FormItem(); + var contentDisposition = contentPart.Headers.ContentDisposition; + formItem.ParameterName = contentDisposition.Name.Trim('"'); + formItem.Data = await contentPart.ReadAsByteArrayAsync(); + formItem.FileName = String.IsNullOrEmpty(contentDisposition.FileName) ? "" : contentDisposition.FileName.Trim('"'); + formItem.MediaType = contentPart.Headers.ContentType == null ? "" : String.IsNullOrEmpty(contentPart.Headers.ContentType.MediaType) ? "" : contentPart.Headers.ContentType.MediaType; + formItems.Add(formItem); + } + + return formItems; + } + } +} \ No newline at end of file diff --git a/Step/Step.csproj b/Step/Step.csproj index 51c29ffb..45a522c4 100644 --- a/Step/Step.csproj +++ b/Step/Step.csproj @@ -183,6 +183,7 @@ +