diff --git a/Client.Utils/Constants.cs b/Client.Utils/Constants.cs index 21cd2b19..d32a31d7 100644 --- a/Client.Utils/Constants.cs +++ b/Client.Utils/Constants.cs @@ -72,6 +72,7 @@ namespace Client.Utils public const string StartingPage = "index.html"; public const string Uploadpage = "/api/file_manager/upload"; + public const string UPLOAD_ADD_QUEUE = "/api/file_manager/queue/add"; public const string ConfigPage = "api/configuration/client"; public const string errorPageUrl = @"error://error/"; diff --git a/Client/Browser_Tools/BrowserJSObject.cs b/Client/Browser_Tools/BrowserJSObject.cs index d8c1e139..bbc60955 100644 --- a/Client/Browser_Tools/BrowserJSObject.cs +++ b/Client/Browser_Tools/BrowserJSObject.cs @@ -61,6 +61,7 @@ namespace CMS_Client.Browser_Tools AddFunction("getProgramInfo").Execute += getProgramInfo; AddFunction("uploadAndActivateProgram").Execute += uploadAndActivateProgram; + AddFunction("uploadAndAddToQueue").Execute += uploadAndAddToQueue; } @@ -431,6 +432,80 @@ namespace CMS_Client.Browser_Tools } + //Upload and add to queue + public async void uploadAndAddToQueue(object sender, CfrV8HandlerExecuteEventArgs e) + { + string fileToUpload = ""; + Byte[] filecontent = null, imagecontent = null; + string fileNameNoExt; + string imageName = ""; + string fileName; + string imageDirectory; + int reps = 0; + + //Check the arguments numbers + if (e.Arguments.Length < 1 || e.Arguments[0] == null) + { + e.SetReturnValue(Constants.UPLOAD_ADD_QUEUE + "INVALID_ARGUMENTS"); + return; + } + + //Check if the file exists + fileToUpload = e.Arguments[0].StringValue; + reps = e.Arguments[1].IntValue; + + if (!System.IO.File.Exists(fileToUpload)) + { + e.SetReturnValue(Constants.UPLOAD_ADD_QUEUE + ";FILE_NOT_FOUND"); + return; + } + + //Get names and content of the file + fileNameNoExt = Path.GetFileNameWithoutExtension(fileToUpload); + fileName = Path.GetFileName(fileToUpload); ; + filecontent = System.IO.File.ReadAllBytes(fileToUpload); + + //Get all bytes of the image (if exists) + imageDirectory = Path.GetDirectoryName(fileToUpload); + foreach (string ext in _validImages) + { + if (System.IO.File.Exists(imageDirectory + "/" + fileNameNoExt + ext)) + { + imageName = fileNameNoExt + ext; + imagecontent = System.IO.File.ReadAllBytes(imageDirectory + "/" + fileNameNoExt + ext); + break; + } + } + + //Send all to the server + using (HttpClient httpClient = new HttpClient()) + { + //Create the new FORM + MultipartFormDataContent form = new MultipartFormDataContent(); + + //Add the content to the form + form.Add(new ByteArrayContent(filecontent), "file", fileName); + if (imagecontent != null) + form.Add(new ByteArrayContent(imagecontent), "image", imageName); + + form.Add(new StringContent(reps.ToString()), "reps"); + + //Send them + HttpResponseMessage response = httpClient + .PostAsync("http://" + Config.ConnectionConfig.ServerUrl + ":" + Config.ConnectionConfig.ServerPort + "/" + Constants.UPLOAD_ADD_QUEUE, + form) + .Result; + //Wait the answer + if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) + e.SetReturnValue(Constants.UPLOAD_ADD_QUEUE + ";" + await response.Content.ReadAsStringAsync()); + else if (response.StatusCode != System.Net.HttpStatusCode.OK) + e.SetReturnValue(Constants.UPLOAD_ADD_QUEUE + ";" + response.ReasonPhrase); + else + e.SetReturnValue(""); + } + + } + //Read info of a file public void getProgramInfo(object sender, CfrV8HandlerExecuteEventArgs e) diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index 85d8be42..74ac503e 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.NC/NcHandler.cs b/Step.NC/NcHandler.cs index 067e687c..c8564784 100644 --- a/Step.NC/NcHandler.cs +++ b/Step.NC/NcHandler.cs @@ -158,6 +158,8 @@ namespace Step.NC if (cmsError.IsError()) return cmsError; + programData.Path = newFilePath; + // Delete local file File.Delete(localPath + "\\" + fileName); @@ -219,7 +221,7 @@ namespace Step.NC // Set status if (PartProgramQueue.ContainsKey(selectedProcess) && PartProgramQueue[selectedProcess].ElementAtOrDefault(QueueRunningIndex) != null) - PartProgramQueue[selectedProcess][QueueRunningIndex].Status = queueData.Status; + PartProgramQueue[selectedProcess][QueueRunningIndex].Status = queueData.Status; return GetProcessQueue(selectedProcess, out queueList); } @@ -237,11 +239,11 @@ namespace Step.NC return NO_ERROR; } - public CmsError UploadPartProgramAddToQueue(string localPath, string fileName, int reps, out ActiveProgramDataModel programData) + public CmsError UploadPartProgramAddToQueue(string localPath, string fileName, int reps, out DTOQueueModel queueItem) { - programData = new ActiveProgramDataModel(); + queueItem = new DTOQueueModel(); // Upload to NC - CmsError cmsError = UploadPartProgram(localPath, fileName, out programData); + CmsError cmsError = UploadPartProgram(localPath, fileName, out ActiveProgramDataModel programData); // Get selectedProcess id ushort selectedProcess = 0; @@ -249,7 +251,7 @@ namespace Step.NC if (cmsError.IsError()) return cmsError; - DTOQueueModel tmpQueue = new DTOQueueModel() + queueItem = new DTOQueueModel() { PartProgramName = fileName, Reps = reps, @@ -262,10 +264,10 @@ namespace Step.NC if (!PartProgramQueue.ContainsKey(selectedProcess)) PartProgramQueue.Add(selectedProcess, new List()); - tmpQueue.Id = SupportFunctions.GetNextId(PartProgramQueue[selectedProcess].Select(x => x.Id)); + queueItem.Id = SupportFunctions.GetNextId(PartProgramQueue[selectedProcess].Select(x => x.Id)); // Add new process to - PartProgramQueue[selectedProcess].Add(tmpQueue); + PartProgramQueue[selectedProcess].Add(queueItem); return cmsError; } @@ -298,8 +300,38 @@ namespace Step.NC return NO_ERROR; } - public CmsError EditQueueItemReps(int processId, int itemIndex, int reps) + public CmsError MoveQueueItems(int processId, int oldIndex, int newIndex, out List queue) { + oldIndex = oldIndex - 1; + newIndex = newIndex - 1; + + queue = new List(); + + // Check if there is a queue + if (!PartProgramQueue.ContainsKey(processId)) + return INCORRECT_PARAMETERS_ERROR; + // Check if items exists + if (PartProgramQueue[processId].ElementAtOrDefault(oldIndex) == null) + return INCORRECT_PARAMETERS_ERROR; + + var item = PartProgramQueue[processId][oldIndex]; + // Remove item in the old positions + PartProgramQueue[processId].RemoveAt(oldIndex); + // Add in the new position + PartProgramQueue[processId].Insert(newIndex, item); + + // Insert new ids + for (int i = 0; i < PartProgramQueue[processId].Count(); i++) + PartProgramQueue[processId][i].Id = i + 1; + + queue = PartProgramQueue[processId]; + + return NO_ERROR; + } + + public CmsError EditQueueItemReps(int processId, int itemIndex, int reps, out DTOQueueModel model) + { + model = new DTOQueueModel(); itemIndex = itemIndex - 1; // Check if there is a queue if (!PartProgramQueue.ContainsKey(processId)) @@ -314,6 +346,8 @@ namespace Step.NC PartProgramQueue[processId][itemIndex].Reps = reps; PartProgramQueue[processId][itemIndex].RemainingReps = reps; + model = PartProgramQueue[processId][itemIndex]; + return NO_ERROR; } diff --git a/Step/Controllers/WebApi/NcFileController.cs b/Step/Controllers/WebApi/NcFileController.cs index 9a855aa3..a6f42699 100644 --- a/Step/Controllers/WebApi/NcFileController.cs +++ b/Step/Controllers/WebApi/NcFileController.cs @@ -170,7 +170,7 @@ namespace Step.Controllers.WebApi } List formItems = await MultipartHandler.ManageMultiPartFileAsync(Request); - + DTOQueueModel queueItem = new DTOQueueModel(); foreach (FormItem item in formItems) { if (item.IsAFile) @@ -189,7 +189,7 @@ namespace Step.Controllers.WebApi if (repsParam == null || !Int32.TryParse(repsParam.Value, out int reps)) return BadRequest(); - CmsError cmsError = ncHandler.UploadPartProgramAddToQueue(TEMP_FILE, item.FileName, reps, out ActiveProgramDataModel programData); + CmsError cmsError = ncHandler.UploadPartProgramAddToQueue(TEMP_FILE, item.FileName, reps, out queueItem); if (cmsError.IsError()) return BadRequest(cmsError.localizationKey); } @@ -201,7 +201,7 @@ namespace Step.Controllers.WebApi } } - return Ok(); + return Ok(queueItem); } [Route("queue/{processId:int}/remove/{itemId:int}"), HttpDelete] @@ -219,38 +219,55 @@ namespace Step.Controllers.WebApi return Ok(); } - [Route("queue/{processId:int}/swap"), HttpPut] - public IHttpActionResult SwapQueueItems(int processId, SwapItems itemsPositions) + [Route("queue/{processId:int}/move"), HttpPut] + public IHttpActionResult MoveQueueItems(int processId, MoveItems itemsPositions) { using (NcHandler ncHandler = new NcHandler()) { - CmsError cmsError = ncHandler.SwapQueueItems(processId, itemsPositions.Item1Position, itemsPositions.Item2Position); + CmsError cmsError = ncHandler.MoveQueueItems(processId, itemsPositions.OldPosition, itemsPositions.NewPosition, out List queue); if (cmsError.IsError()) return BadRequest(cmsError.localizationKey); - } + - return Ok(); + return Ok(queue); + } } [Route("queue/{processId:int}/edit/{positionId:int}"), HttpPut] - public IHttpActionResult EditQueueItem(int processId, int positionId, SwapItems reps) + public IHttpActionResult EditQueueItem(int processId, int positionId, RepsModel reps) { using (NcHandler ncHandler = new NcHandler()) { - CmsError cmsError = ncHandler.EditQueueItemReps(processId, positionId, reps.Reps ); + CmsError cmsError = ncHandler.EditQueueItemReps(processId, positionId, reps.Reps, out DTOQueueModel queueItem); if (cmsError.IsError()) return BadRequest(cmsError.localizationKey); + + return Ok(queueItem); } - - return Ok(); } - public class SwapItems + [Route("queue/{processId:int}/empty"), HttpPut] + public IHttpActionResult EmptyQueue(int processId) { - public int Item1Position; + using (NcHandler ncHandler = new NcHandler()) + { + CmsError cmsError = ncHandler.EmptyQueue(processId); + if (cmsError.IsError()) + return BadRequest(cmsError.localizationKey); - public int Item2Position; + return Ok(); + } + } + public class MoveItems + { + public int OldPosition; + + public int NewPosition; + } + + public class RepsModel + { public int Reps; }