76d617d633
Removed shanks from Siemens library Fix "isInUse" param etc
324 lines
11 KiB
C#
324 lines
11 KiB
C#
using CMS_CORE_Library.Models;
|
|
using Step.Model.DTOModels;
|
|
using Step.NC;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using static CMS_CORE_Library.Models.DataStructures;
|
|
using static Step.Config.ServerConfig;
|
|
using static Step.Model.Constants;
|
|
|
|
namespace Step.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/file_manager")]
|
|
public class NcFileController : ApiController
|
|
{
|
|
[Route("files"), HttpGet]
|
|
public IHttpActionResult GetFileList(string filePath = "")
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.GetFileList(filePath, out List<PreviewFileModel> fileList);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(fileList);
|
|
}
|
|
}
|
|
|
|
[Route("file/info"), HttpGet]
|
|
public IHttpActionResult GetFileInfo([FromUri]string filePath)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.GetFileInfo(filePath, out InfoFile fileInfo);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
}
|
|
|
|
[Route("file/active/info"), HttpGet]
|
|
public IHttpActionResult GetActiveFileInfo([FromUri]string filePath)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.GetActiveFileInfo(filePath, out InfoFile fileInfo);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
}
|
|
|
|
[Route("file/active"), HttpPut]
|
|
public IHttpActionResult SetActiveProgram([FromUri]string filePath)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.SetActiveProgramInfo(filePath, out DTOActiveProgramDataModel fileInfo);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
}
|
|
|
|
[Route("file/deactivate"), HttpPut]
|
|
public IHttpActionResult DeactivateProgram()
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.DeactivateProgram(out DTOActiveProgramDataModel fileInfo);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(fileInfo);
|
|
}
|
|
}
|
|
|
|
[Route("upload"), HttpPost]
|
|
public async Task<IHttpActionResult> UpdloadProgramWithImage()
|
|
{
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
{
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
}
|
|
|
|
List<FormItem> formItems = await MultipartHandler.ManageMultiPartFileAsync(Request);
|
|
|
|
foreach (FormItem item in formItems)
|
|
{
|
|
if (item.IsAFile)
|
|
{
|
|
if (item.ParameterName == "file")
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
File.WriteAllBytes(TEMP_PP_FOLDER + item.FileName, item.Data);
|
|
|
|
CmsError cmsError = ncHandler.UploadPartProgramAndActivate(TEMP_PP_FOLDER, item.FileName, out ActiveProgramDataModel programData);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
}
|
|
}
|
|
else if (item.ParameterName == "image")
|
|
{
|
|
File.WriteAllBytes(PART_PRG_IMAGES + item.FileName, item.Data);
|
|
}
|
|
}
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("shared_files"), HttpGet]
|
|
public IHttpActionResult GetSharedFolderFileList(string sharedPath)
|
|
{
|
|
List<object> filelist = new List<object>();
|
|
// NC - PC shared folder path
|
|
string sharedFullPath = NcConfig.SharedPath + sharedPath;
|
|
|
|
if (!Directory.Exists(sharedFullPath))
|
|
return NotFound();
|
|
// Create list of directories
|
|
foreach (string item in Directory.GetDirectories(sharedFullPath))
|
|
{
|
|
filelist.Add(new
|
|
{
|
|
Name = Path.GetFileName(item),
|
|
AbsolutePath = NcConfig.SharedPath + sharedPath,
|
|
Path = sharedPath,
|
|
IsDirectory = true
|
|
});
|
|
}
|
|
|
|
string sharedNcPath = NcConfig.SharedName + sharedPath.Replace('\\', '/');
|
|
// Add files to list
|
|
foreach (string item in Directory.GetFiles(sharedFullPath))
|
|
{
|
|
if (VALID_FILE_EXTENSIONS.Contains(Path.GetExtension(item).ToLower()))
|
|
filelist.Add(new
|
|
{
|
|
Name = Path.GetFileName(item),
|
|
AbsolutePath = sharedNcPath + "/" + Path.GetFileName(item),
|
|
Path = sharedPath + "\\" + Path.GetFileName(item),
|
|
IsDirectory = false
|
|
});
|
|
}
|
|
|
|
return Ok(filelist);
|
|
}
|
|
|
|
#region Queue Manager
|
|
|
|
[Route("queue/add"), HttpPost]
|
|
public async Task<IHttpActionResult> UploadToQueue()
|
|
{
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
{
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
}
|
|
|
|
List<FormItem> formItems = await MultipartHandler.ManageMultiPartFileAsync(Request);
|
|
DTOQueueModel queueItem = new DTOQueueModel();
|
|
foreach (FormItem item in formItems)
|
|
{
|
|
if (item.IsAFile)
|
|
{
|
|
// Get part program under "file" key
|
|
if (item.ParameterName == "file")
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
File.WriteAllBytes(QUEUE_TMP_FOLDER + item.FileName, item.Data);
|
|
|
|
// Get reps
|
|
var repsParam = formItems.Where(x => x.ParameterName == "reps").FirstOrDefault();
|
|
// Check if reps parameter is null or isn't a valid int
|
|
if (repsParam == null || !Int32.TryParse(repsParam.Value, out int reps))
|
|
return BadRequest();
|
|
// Upload
|
|
CmsError cmsError = ncHandler.UploadPartProgramAndAddToQueue(QUEUE_TMP_FOLDER, item.FileName, reps, out queueItem);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
}
|
|
}
|
|
else if (item.ParameterName == "image")
|
|
{
|
|
File.WriteAllBytes(PART_PRG_IMAGES + item.FileName, item.Data);
|
|
}
|
|
}
|
|
}
|
|
|
|
return Ok(queueItem);
|
|
}
|
|
|
|
[Route("queue/{processId:int}/remove/{itemId:int}"), HttpDelete]
|
|
public IHttpActionResult RemoveFromQueue(int processId, int itemId)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.RemoveFromQueue(processId, itemId);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[Route("queue/{processId:int}/move"), HttpPut]
|
|
public IHttpActionResult MoveQueueItems(int processId, MoveItems itemsPositions)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
CmsError cmsError = ncHandler.MoveQueueItems(processId, itemsPositions.ObjectId, itemsPositions.NewPosition, out List<DTOQueueModel> queue);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(queue);
|
|
}
|
|
}
|
|
|
|
[Route("queue/{processId:int}/edit/{itemId:int}"), HttpPut]
|
|
public IHttpActionResult EditQueueItem(int processId, int itemId, RepsModel reps)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
if (reps.Reps < 1)
|
|
return BadRequest(INCORRECT_PARAMETERS_ERROR.localizationKey);
|
|
|
|
CmsError cmsError = ncHandler.EditQueueItemReps(processId, itemId, reps.Reps, out DTOQueueModel queueItem);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(queueItem);
|
|
}
|
|
}
|
|
|
|
[Route("queue/{processId:int}/empty"), HttpDelete]
|
|
public IHttpActionResult EmptyQueue(int processId)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
CmsError cmsError = ncHandler.EmptyQueue(processId);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
[Route("queue/start")]
|
|
public IHttpActionResult StartWorkingQueue(int processId)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.StartWorkingQueue(processId);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
[Route("queue/stop")]
|
|
public IHttpActionResult StopWorkingQueue(int processId)
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
CmsError cmsError = ncHandler.StopWorkingQueue(processId);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
[Route("macros")]
|
|
public IHttpActionResult GetMacros()
|
|
{
|
|
return Ok(MacrosConfig);
|
|
}
|
|
|
|
public class MoveItems
|
|
{
|
|
public int ObjectId;
|
|
|
|
public int NewPosition;
|
|
}
|
|
|
|
public class RepsModel
|
|
{
|
|
public int Reps;
|
|
}
|
|
|
|
#endregion Queue Manager
|
|
}
|
|
} |