4bae9b4259
Fix OsaiFanuc delete tool
2599 lines
107 KiB
C#
2599 lines
107 KiB
C#
using CMS_CORE_Library;
|
|
using CMS_CORE_Library.Demo;
|
|
using CMS_CORE_Library.Fanuc;
|
|
using CMS_CORE_Library.Models;
|
|
using CMS_CORE_Library.Osai;
|
|
using CMS_CORE_Library.Siemens;
|
|
using Step.Database.Controllers;
|
|
using Step.Model.DatabaseModels;
|
|
using Step.Model.DTOModels;
|
|
using Step.Model.DTOModels.AlarmModels;
|
|
using Step.Model.DTOModels.JobModels;
|
|
using Step.Model.DTOModels.MaintenanceModels;
|
|
using Step.Model.DTOModels.Scada;
|
|
using Step.Model.DTOModels.ToolModels;
|
|
using Step.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using static CMS_CORE_Library.Models.DataStructures;
|
|
using static Step.Config.ServerConfig;
|
|
using static Step.Database.Controllers.QueueController;
|
|
using static Step.Model.Constants;
|
|
|
|
namespace Step.NC
|
|
{
|
|
public class NcHandler : IDisposable
|
|
{
|
|
public Nc numericalControl;
|
|
|
|
public NcHandler() =>
|
|
// Choose NC
|
|
numericalControl = SetNumericalControl();
|
|
|
|
public void Dispose()
|
|
{
|
|
if (NcConfig.NcVendor != NC_VENDOR.SIEMENS)
|
|
numericalControl.NC_Disconnect();
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
numericalControl.NC_Disconnect();
|
|
}
|
|
|
|
public CmsError Connect()
|
|
{
|
|
// Connect NC
|
|
if (!numericalControl.NC_IsConnected())
|
|
return numericalControl.NC_Connect();
|
|
|
|
return null;
|
|
}
|
|
|
|
public Nc SetNumericalControl()
|
|
{
|
|
// Return new Numerical control instance choosed from the configuration
|
|
switch (NcConfig.NcVendor)
|
|
{
|
|
case NC_VENDOR.DEMO:
|
|
return new Nc_Demo(NcConfig.NcIpAddress, NcConfig.NcPort);
|
|
|
|
case NC_VENDOR.FANUC:
|
|
return new Nc_Fanuc(NcConfig.NcIpAddress, NcConfig.NcPort, 2000);
|
|
|
|
case NC_VENDOR.SIEMENS:
|
|
return new Nc_Siemens(2000);
|
|
|
|
case NC_VENDOR.OSAI:
|
|
return new Nc_Osai(NcConfig.NcIpAddress, NcConfig.NcPort, 2000);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
#region File manager
|
|
|
|
public CmsError GetFileList(string path, out List<PreviewFileModel> fileList)
|
|
{
|
|
fileList = new List<PreviewFileModel>();
|
|
return numericalControl.FILES_RGetFileList(path, ref fileList);
|
|
}
|
|
|
|
public CmsError GetFileInfo(string path, out InfoFile fileInfo)
|
|
{
|
|
fileInfo = new InfoFile();
|
|
// CmsError cmsError = NO_ERROR;
|
|
|
|
// if (FileExistWithNc(path))
|
|
// cmsError = LocalPartProgramFileInfo(path, out fileInfo);
|
|
// else
|
|
// cmsError = numericalControl.FILES_RGetFileInfo(path, ref fileInfo);
|
|
|
|
return numericalControl.FILES_RGetFileInfo(path, ref fileInfo);
|
|
}
|
|
|
|
public bool FileExistWithNc(string path)
|
|
{
|
|
if (NcConfig.NcVendor != NC_VENDOR.SIEMENS && NcConfig.NcVendor != NC_VENDOR.FANUC)
|
|
{
|
|
// TODO Fix in the future
|
|
return File.Exists(path);
|
|
}
|
|
else
|
|
{
|
|
if (path.StartsWith("//"))
|
|
return false;
|
|
else
|
|
return File.Exists(path);
|
|
}
|
|
}
|
|
|
|
public CmsError GetActiveFileInfo(string path, out DTOActiveImageAndNameDataModel fileInfo)
|
|
{
|
|
fileInfo = new DTOActiveImageAndNameDataModel();
|
|
|
|
PROGRAM_TYPE_ENUM program = PROGRAM_TYPE_ENUM.NONE;
|
|
CmsError cmsError = numericalControl.FILES_RGetProgramType(ref program);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Get selected process
|
|
ushort selectedProcess = 0;
|
|
cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// JOB CASE
|
|
if (program == PROGRAM_TYPE_ENUM.JOB)
|
|
{
|
|
// Create folder path with process
|
|
string jobFolderPath = JOB_TMP_DIRECTORY + "\\" + selectedProcess + "\\";
|
|
// Get job data
|
|
DTOJobModel model = SupportFunctions.ReadExtractedJobMetadata(selectedProcess);
|
|
if (model == null)
|
|
return FILE_NOT_FOUND_ERROR;
|
|
|
|
string base64Img = model.Metadata.Generics.Images.Count() > 0 ? model.Metadata.Generics.Images[0].Base64 : "";
|
|
|
|
fileInfo = new DTOActiveImageAndNameDataModel()
|
|
{
|
|
Image = base64Img,
|
|
Name = Path.GetFileName(path) // TODO: Get From NC
|
|
};
|
|
|
|
return NO_ERROR;
|
|
}
|
|
// PART PROGRAM CASE
|
|
else
|
|
{
|
|
string name = "";
|
|
cmsError = numericalControl.PROC_RSelectedPPName(selectedProcess, ref name);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
fileInfo = new DTOActiveImageAndNameDataModel()
|
|
{
|
|
Image = SupportFunctions.FindImageBase64String(PART_PRG_IMAGES, name),
|
|
Name = Path.GetFileName(name) // TODO: Get From NC
|
|
};
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError LocalPartProgramFileInfo(string path, out InfoFile fileInfo)
|
|
{
|
|
FileInfo fileData = new FileInfo(path);
|
|
StreamReader fileRead = new StreamReader(path);
|
|
int count = 0;
|
|
string line = "";
|
|
|
|
fileInfo = new InfoFile()
|
|
{
|
|
Name = fileData.Name,
|
|
CreationDate = fileData.CreationTime,
|
|
LastModDate = fileData.LastAccessTime,
|
|
AbsolutePath = path
|
|
};
|
|
|
|
// Populate fileInfo content
|
|
fileInfo.Content = new List<string>();
|
|
while ((line = fileRead.ReadLine()) != null && count < 10)
|
|
{
|
|
fileInfo.Content.Add(line);
|
|
count++;
|
|
}
|
|
fileRead.Close();
|
|
|
|
// Set image base 64 string
|
|
foreach (string ext in VALID_IMAGE_EXTENSIONS)
|
|
{
|
|
string imagePath = PART_PRG_IMAGES + "/" + fileInfo.Name + ext;
|
|
if (File.Exists(imagePath))
|
|
{
|
|
fileInfo.PreviewBase64 = "data:image/" + ext + ";base64," + Convert.ToBase64String(File.ReadAllBytes(imagePath));
|
|
break;
|
|
}
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError GetActiveProgramInfo(out DTOActiveProgramDataModel dtoData)
|
|
{
|
|
dtoData = new DTOActiveProgramDataModel();
|
|
// Get selectedProcess process id
|
|
ushort selectedProcess = 0;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Read active program data
|
|
ActiveProgramDataModel data = new ActiveProgramDataModel();
|
|
cmsError = numericalControl.FILES_RActiveProgramData(selectedProcess, ref data);
|
|
|
|
dtoData = new DTOActiveProgramDataModel()
|
|
{
|
|
IsoLines = data.IsoLines,
|
|
Path = data.Path,
|
|
TimeLeft = data.TimeLeft
|
|
};
|
|
|
|
return cmsError; // TODO FIX OSAI
|
|
}
|
|
|
|
public CmsError SetActiveProgramInfo(string path, out DTOActiveProgramDataModel dtoData)
|
|
{
|
|
dtoData = new DTOActiveProgramDataModel();
|
|
// Get selectedProcess process id
|
|
ushort selectedProcess = 1;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
//if (cmsError.IsError())
|
|
// return cmsError;
|
|
|
|
ActiveProgramDataModel data = new ActiveProgramDataModel();
|
|
cmsError = numericalControl.FILES_WSetActiveProgram(selectedProcess, path, ref data);
|
|
|
|
dtoData = new DTOActiveProgramDataModel()
|
|
{
|
|
IsoLines = data.IsoLines,
|
|
Path = data.Path,
|
|
TimeLeft = data.TimeLeft
|
|
};
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError DeactivateProgram(out DTOActiveProgramDataModel dtoData)
|
|
{
|
|
dtoData = new DTOActiveProgramDataModel();
|
|
// Get selectedProcess process id
|
|
ushort selectedProcess = 0;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
ActiveProgramDataModel data = new ActiveProgramDataModel();
|
|
cmsError = numericalControl.FILES_WDeactivateProgram(selectedProcess);
|
|
|
|
dtoData = new DTOActiveProgramDataModel()
|
|
{
|
|
IsoLines = data.IsoLines,
|
|
Path = data.Path,
|
|
TimeLeft = data.TimeLeft
|
|
};
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError UploadPartProgram(string localPath, string fileName, out string newFilePath)
|
|
{
|
|
// Upload to NC
|
|
newFilePath = "";
|
|
CmsError cmsError = numericalControl.FILES_UploadPartProgram(localPath, fileName, ref newFilePath);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Delete local file
|
|
File.Delete(localPath + "\\" + fileName);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError UploadPartProgramAndActivate(string localPath, string fileName, out ActiveProgramDataModel programData)
|
|
{
|
|
programData = new ActiveProgramDataModel();
|
|
|
|
// Get selectedProcess id
|
|
ushort selectedProcess = 0;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
if (!JOB_EXTENSIONS.Contains(Path.GetExtension(fileName)))
|
|
{
|
|
// Upload to NC
|
|
cmsError = UploadPartProgram(localPath, fileName, out string newFilePath);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Activate updated program
|
|
return numericalControl.FILES_WSetActiveProgram(selectedProcess, newFilePath, ref programData);
|
|
}
|
|
else
|
|
{
|
|
DTOJobModel job = SupportFunctions.UnpackJobAndReadMetadata(localPath + fileName, selectedProcess);
|
|
return numericalControl.FILES_WUploadJobFilesAndActivate(selectedProcess, JOB_TMP_DIRECTORY, JOB_MAIN_FILENAME);
|
|
}
|
|
}
|
|
|
|
public CmsError UploadPartProgramAndAddToQueue(string localPath, string fileName, int reps, out DTOQueueModel queueItem)
|
|
{
|
|
queueItem = new DTOQueueModel();
|
|
|
|
// Get selectedProcess id
|
|
ushort selectedProcess = 0;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
queueItem = new DTOQueueModel()
|
|
{
|
|
PartProgramName = fileName,
|
|
Reps = reps,
|
|
RemainingReps = reps,
|
|
AbsolutePath = localPath + fileName,
|
|
Status = QUEUE_ITEM_STATUS.NOT_ACTIVE
|
|
};
|
|
|
|
// Check if there is already a pp queue
|
|
if (!PartProgramQueue.ContainsKey(selectedProcess))
|
|
PartProgramQueue.Add(selectedProcess, new List<DTOQueueModel>());
|
|
|
|
queueItem.Id = SupportFunctions.GetNextId(PartProgramQueue[selectedProcess].Select(x => x.Id));
|
|
|
|
// Add new process to
|
|
PartProgramQueue[selectedProcess].Add(queueItem);
|
|
|
|
using (QueueController queueController = new QueueController())
|
|
{
|
|
queueController.UpdateQueue();
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError UpdateQueue()
|
|
{
|
|
// Get queue data
|
|
List<QueueStatusModel> queueData = new List<QueueStatusModel>();
|
|
|
|
CmsError cmsError = numericalControl.FILES_RQueueData(ref queueData);
|
|
|
|
foreach (var item in queueData)
|
|
{
|
|
if (!QueueRunningIndexes.ContainsKey(item.ProcessId))
|
|
QueueRunningIndexes.Add(item.ProcessId, 0);
|
|
|
|
if (item.LoadNextProgram)
|
|
{
|
|
// Check if there is already a pp queue for the selected process
|
|
if (PartProgramQueue.ContainsKey(item.ProcessId))
|
|
{
|
|
// Check if runnig exist
|
|
if (PartProgramQueue[item.ProcessId].ElementAtOrDefault(QueueRunningIndexes[item.ProcessId]) != null)
|
|
{
|
|
var actualItem = PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]];
|
|
// Check if there are remaining reps before change part program
|
|
if (actualItem.RemainingReps > 1)
|
|
actualItem.RemainingReps -= 1;
|
|
else
|
|
{
|
|
// Set as finished
|
|
actualItem.RemainingReps = 0;
|
|
actualItem.Status = QUEUE_ITEM_STATUS.FINISHED;
|
|
|
|
// TODO set next
|
|
//while(PartProgramQueue[item.ProcessId].ElementAtOrDefault(QueueRunningIndexes[item.ProcessId] + 1) != null && !File.Exists(PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]].AbsolutePath))
|
|
//{
|
|
// QueueRunningIndexes[item.ProcessId] += 1;
|
|
//}
|
|
|
|
if (PartProgramQueue[item.ProcessId].ElementAtOrDefault(QueueRunningIndexes[item.ProcessId] + 1) != null)
|
|
// if next part program exists, set next pp as current pp
|
|
QueueRunningIndexes[item.ProcessId] += 1;
|
|
|
|
// Check if file is a valid job
|
|
if (SupportFunctions.IsValidJob(PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]].AbsolutePath))
|
|
{
|
|
// Unpack job
|
|
DTOJobModel job = SupportFunctions.UnpackJobAndReadMetadata(PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]].AbsolutePath, item.ProcessId);
|
|
// Load JOB to NC
|
|
return numericalControl.FILES_WUploadJobFilesAndActivate(item.ProcessId, JOB_TMP_DIRECTORY + "\\" + item.ProcessId + "\\", JOB_MAIN_FILENAME);
|
|
}
|
|
else
|
|
// Upload & activate new part program
|
|
cmsError = numericalControl
|
|
.FILES_WLoadNextPartProgram(
|
|
PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]].AbsolutePath,
|
|
QUEUE_FILE_NAME
|
|
);
|
|
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check if queue exists for the process
|
|
if (PartProgramQueue.ContainsKey(item.ProcessId) && PartProgramQueue[item.ProcessId].ElementAtOrDefault(QueueRunningIndexes[item.ProcessId]) != null)
|
|
{
|
|
// Set status based on queue status
|
|
if (item.Status == QUEUE_STATUS.RUNNING || item.Status == QUEUE_STATUS.CLOSING)
|
|
PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]].Status = QUEUE_ITEM_STATUS.RUNNING;
|
|
|
|
if (item.Status == QUEUE_STATUS.NOT_ACTIVE)
|
|
PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]].Status = QUEUE_ITEM_STATUS.NOT_ACTIVE;
|
|
|
|
if (item.Status == QUEUE_STATUS.WAITING_OPERATOR)
|
|
PartProgramQueue[item.ProcessId][QueueRunningIndexes[item.ProcessId]].Status = QUEUE_ITEM_STATUS.WAITING_OPERATOR;
|
|
}
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError GetSelectedProcessQueue(out List<DTOQueueModel> queueList)
|
|
{
|
|
queueList = new List<DTOQueueModel>();
|
|
// Get selected process
|
|
ushort selectedProcess = 0;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
queueList = new List<DTOQueueModel>();
|
|
// Check if there is already a pp queue
|
|
if (!PartProgramQueue.ContainsKey(selectedProcess))
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
// Add new process to
|
|
queueList = PartProgramQueue[selectedProcess];
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError MoveQueueItems(int processId, int itemId, int newIndex, out List<DTOQueueModel> queue)
|
|
{
|
|
itemId = itemId - 1;
|
|
|
|
var a = QueueRunningIndexes[processId];
|
|
// Update queue running index after move an item
|
|
if (itemId < QueueRunningIndexes[processId] && newIndex >= QueueRunningIndexes[processId])
|
|
QueueRunningIndexes[processId] -= 1;
|
|
if (newIndex <= QueueRunningIndexes[processId] && itemId > QueueRunningIndexes[processId])
|
|
QueueRunningIndexes[processId] += 1;
|
|
|
|
queue = new List<DTOQueueModel>();
|
|
|
|
// Check if there is a queue
|
|
if (!PartProgramQueue.ContainsKey(processId))
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
// Check if items exists
|
|
if (PartProgramQueue[processId].ElementAtOrDefault(itemId) == null)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
var item = PartProgramQueue[processId][itemId];
|
|
// Remove item in the old positions
|
|
PartProgramQueue[processId].RemoveAt(itemId);
|
|
// Add in the new position
|
|
PartProgramQueue[processId].Insert(newIndex, item);
|
|
|
|
// Fix new ids
|
|
for (int i = 0; i < PartProgramQueue[processId].Count(); i++)
|
|
PartProgramQueue[processId][i].Id = i + 1;
|
|
|
|
using (QueueController queueController = new QueueController())
|
|
queueController.UpdateQueueIdsAndSave(processId);
|
|
|
|
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))
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
// Check if items exists
|
|
if (PartProgramQueue[processId].ElementAtOrDefault(itemIndex) == null)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
// Check if Program is running
|
|
if (PartProgramQueue[processId][itemIndex].Status == QUEUE_ITEM_STATUS.RUNNING)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
PartProgramQueue[processId][itemIndex].Reps = reps;
|
|
PartProgramQueue[processId][itemIndex].RemainingReps = reps;
|
|
|
|
model = PartProgramQueue[processId][itemIndex];
|
|
|
|
// Update database
|
|
using (QueueController queueController = new QueueController())
|
|
queueController.UpdateReps(processId, itemIndex, reps);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError RemoveFromQueue(int processId, int id)
|
|
{
|
|
// Check if there is a queue
|
|
if (PartProgramQueue.ContainsKey(processId))
|
|
{
|
|
// Find item by id
|
|
DTOQueueModel tmpQueueItem = PartProgramQueue[processId]
|
|
.Where(x => x.Id == id)
|
|
.FirstOrDefault();
|
|
|
|
if (tmpQueueItem != null)
|
|
PartProgramQueue[processId].Remove(tmpQueueItem);
|
|
|
|
if (File.Exists(tmpQueueItem.AbsolutePath))
|
|
File.Delete(tmpQueueItem.AbsolutePath);
|
|
}
|
|
|
|
// Update database data
|
|
using (QueueController queueController = new QueueController())
|
|
queueController.UpdateQueueIdsAndSave(processId);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError EmptyQueue(int processId)
|
|
{
|
|
// Check if there is a queue
|
|
if (PartProgramQueue.ContainsKey(processId))
|
|
PartProgramQueue[processId] = new List<DTOQueueModel>();
|
|
|
|
// Update db
|
|
using (QueueController queueController = new QueueController())
|
|
queueController.UpdateQueue();
|
|
|
|
// Clean directory
|
|
SupportFunctions.EmptyFolder(JOB_TMP_DIRECTORY);
|
|
|
|
QueueRunningIndexes[processId] = 0;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError StartWorkingQueue(int processId)
|
|
{
|
|
return numericalControl.FILES_WStartQueue();
|
|
}
|
|
|
|
public CmsError StopWorkingQueue(int processId)
|
|
{
|
|
return numericalControl.FILES_WStopQueue();
|
|
}
|
|
|
|
#endregion File manager
|
|
|
|
#region Read Data
|
|
|
|
#region Axes
|
|
|
|
public CmsError GetAxesPositions(out List<DTOAxesModel> axes)
|
|
{
|
|
axes = new List<DTOAxesModel>();
|
|
|
|
// Get NC max process number
|
|
ushort maxProcNumber = 0;
|
|
CmsError cmsError = numericalControl.NC_RProcessesNum(ref maxProcNumber);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
// For each process
|
|
for (ushort i = 1; i <= maxProcNumber; i++)
|
|
{
|
|
cmsError = GetAxesPositionsByProcess(i, out DTOAxesModel axis);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
axes.Add(axis);
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetAxesPositionsBySelectedProcess(out DTOAxesModel axes)
|
|
{
|
|
axes = new DTOAxesModel();
|
|
|
|
// Get selectedProcess process number
|
|
ushort selectedProcess = 0;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
if (selectedProcess > 0)
|
|
cmsError = GetAxesPositionsByProcess(selectedProcess, out axes);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetAxesPositionsByProcess(ushort processNum, out DTOAxesModel axes)
|
|
{
|
|
axes = new DTOAxesModel();
|
|
|
|
CmsError cmsError = numericalControl.AXES_RInterpPosition(processNum, ref axes.interpolated);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
cmsError = numericalControl.AXES_RMachinePosition(processNum, ref axes.machine);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
cmsError = numericalControl.AXES_RProgrPosition(processNum, ref axes.programmePos);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
cmsError = numericalControl.AXES_RDistanceToGo(processNum, ref axes.toGo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
//numericalControl.AXES_RFollowingError(1, ref axes.followingErr);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError ReadAxisData(out List<DTOAxisNameModel> axesNames)
|
|
{
|
|
axesNames = new List<DTOAxisNameModel>();
|
|
List<AxisModel> plcAxes = new List<AxisModel>();
|
|
// Read selected process
|
|
ushort selectedProcess = 0;
|
|
CmsError cmsError = numericalControl.PROC_RSelectedProcess(ref selectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
if (selectedProcess != 0)
|
|
{
|
|
// Read axes names
|
|
cmsError = numericalControl.AXES_RAxesNames(selectedProcess, ref plcAxes);
|
|
|
|
axesNames = plcAxes.Select(x => new DTOAxisNameModel()
|
|
{
|
|
Id = x.Id,
|
|
Name = x.Name,
|
|
IsSelectable = x.IsSelectable,
|
|
Type = x.Type.ToString()
|
|
}).ToList();
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
#endregion Axes
|
|
|
|
public CmsError ManageWatchdog()
|
|
{
|
|
return numericalControl.PLC_RWManageWatchdog();
|
|
}
|
|
|
|
public CmsError GetNcGenericData(out DTONcGenericDataModel genericData)
|
|
{
|
|
genericData = new DTONcGenericDataModel(MachineConfig.Model);
|
|
|
|
// Get date time
|
|
DateTime dateTime = new DateTime();
|
|
CmsError cmsError = numericalControl.NC_RDateTime(ref dateTime);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.DateTime = dateTime;
|
|
|
|
// Get language
|
|
CultureInfo lang = new CultureInfo("en");
|
|
cmsError = numericalControl.NC_RLanguage(ref lang);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.Language = lang.TwoLetterISOLanguageName;
|
|
|
|
string tmpInfo = "";
|
|
// Get serial number
|
|
cmsError = numericalControl.NC_RSerialNumber(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.SerialNumber = tmpInfo;
|
|
|
|
// Get software version
|
|
cmsError = numericalControl.NC_RSoftwareVersion(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.NcSoftwareVersion = tmpInfo;
|
|
|
|
// Get model name
|
|
cmsError = numericalControl.NC_RModelName(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.NcModel = tmpInfo;
|
|
|
|
// Get machine number
|
|
cmsError = numericalControl.NC_RMachineNumber(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.CmsMachineIdNumber = tmpInfo;
|
|
|
|
// Get max process number
|
|
ushort procNum = 0;
|
|
cmsError = numericalControl.NC_RProcessesNum(ref procNum);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
// Max process number
|
|
genericData.ProcessNumber = procNum;
|
|
// Get Installation Date
|
|
genericData.InstallationDate = DateTime.Now;
|
|
// Get PLC version
|
|
genericData.PlcVersion = "1.0.0";
|
|
// Get PLC version
|
|
cmsError = numericalControl.NC_RUnitOfMeasure(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
genericData.UnitOfMeasurement = tmpInfo;
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetNcAlarms(out DTOAlarmsModel alarms)
|
|
{
|
|
alarms = new DTOAlarmsModel();
|
|
List<AlarmModel> tmpAlarms = new List<AlarmModel>();
|
|
|
|
// Read NC active alarms
|
|
CmsError cmsError = numericalControl.NC_RActiveAlarms(ref tmpAlarms);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
// Create response list from NC data
|
|
foreach (AlarmModel ncAlarm in tmpAlarms)
|
|
{
|
|
alarms.NcAlarms.Add(new GenericAlarmModel()
|
|
{
|
|
Id = ncAlarm.Id,
|
|
Message = ncAlarm.Message,
|
|
DateTime = DateTime.Now,
|
|
RestorationIsEnabled = false
|
|
});
|
|
}
|
|
// Select distints
|
|
//alarms.NcAlarms = alarms.NcAlarms?.GroupBy(x => x.Id).Select(x => x.First()).ToList();
|
|
|
|
// Get NC max process number
|
|
ushort maxProcNumber = 0;
|
|
cmsError = numericalControl.NC_RProcessesNum(ref maxProcNumber);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
// For each process
|
|
for (ushort i = 1; i <= maxProcNumber; i++)
|
|
{
|
|
// Get process active alarms
|
|
numericalControl.PROC_RActiveAlarms(i, ref tmpAlarms);
|
|
// Create response list from NC data
|
|
foreach (AlarmModel processAlarm in tmpAlarms)
|
|
{
|
|
alarms.ProcessAlarms.Add(new ProcessAlarmModel()
|
|
{
|
|
Id = processAlarm.Id,
|
|
Message = processAlarm.Message,
|
|
Process = processAlarm.Process,
|
|
DateTime = DateTime.Now,
|
|
RestorationIsEnabled = false
|
|
});
|
|
}
|
|
}
|
|
|
|
List<PlcAlarmModel> plcAlarms = new List<PlcAlarmModel>();
|
|
// Read PLC Active Messages
|
|
cmsError = numericalControl.PLC_RActiveMessages(ref plcAlarms);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
bool restorationIsEnabled = false;
|
|
foreach (PlcAlarmModel plcAlarm in plcAlarms)
|
|
{
|
|
var configuredAlarm = InitialAlarmsConfig.Where(x => x.PlcId == plcAlarm.Id).FirstOrDefault();
|
|
if (configuredAlarm == null)
|
|
restorationIsEnabled = false;
|
|
else
|
|
restorationIsEnabled = configuredAlarm.RestoreIsActive;
|
|
|
|
// Create response list from plc data
|
|
alarms.PlcAlarms.Add(new DTOPlcAlarmModel()
|
|
{
|
|
Id = plcAlarm.Id,
|
|
IsWarning = plcAlarm.IsWarning,
|
|
Process = plcAlarm.Process,
|
|
RestorationIsActive = plcAlarm.RestorationIsActive,
|
|
DateTime = DateTime.Now,
|
|
RestorationIsEnabled = restorationIsEnabled
|
|
});
|
|
}
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetPowerOnData(out DTOPowerOnDataModel resultPowerOnData)
|
|
{
|
|
resultPowerOnData = new DTOPowerOnDataModel();
|
|
|
|
// Init object
|
|
PreAndPostPowerOnModel prePostPowerOn = new PreAndPostPowerOnModel
|
|
{
|
|
PostPowerOn = new PostPowerOnModel(),
|
|
PrePowerOn = new PrePowerOnModel()
|
|
};
|
|
|
|
// Read pre and post power on data
|
|
CmsError cmsError = numericalControl.PLC_RPowerOnData(ref prePostPowerOn);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Set return object
|
|
resultPowerOnData.PrePowerOn = prePostPowerOn.PrePowerOn;
|
|
resultPowerOnData.PostPowerOn = prePostPowerOn.PostPowerOn;
|
|
|
|
// Read axes procedure data from
|
|
cmsError = numericalControl.PLC_RAxesResetData(ref resultPowerOnData.AxisReset);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetProcessesData(out DTOProcessesDataModel processesData)
|
|
{
|
|
processesData = new DTOProcessesDataModel();
|
|
|
|
// Get NC max process number
|
|
ushort maxProcNumber = 0;
|
|
CmsError cmsError = numericalControl.NC_RProcessesNum(ref maxProcNumber);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
PROC_STATUS status = PROC_STATUS.IDLE;
|
|
// For each process get info
|
|
for (ushort i = 1; i <= maxProcNumber; i++)
|
|
{
|
|
ProcessDataModel tmpProcPP = new ProcessDataModel();
|
|
// Get process status
|
|
cmsError = numericalControl.PROC_RStatusAndData(i, ref tmpProcPP);
|
|
// if at least one process is in run, NC is running
|
|
// Add to list
|
|
processesData.processes.Add(new ProcessModel()
|
|
{
|
|
Id = tmpProcPP.Id,
|
|
IsInAlarm = tmpProcPP.IsInAlarm,
|
|
PartProgramName = tmpProcPP.PartProgramName,
|
|
Reps = tmpProcPP.Reps,
|
|
Status = tmpProcPP.Status,
|
|
Type = tmpProcPP.Type,
|
|
Visible = tmpProcPP.Visible,
|
|
CanLoadProgram = tmpProcPP.CanLoadProgram
|
|
});
|
|
|
|
if (tmpProcPP.IsSelected && processesData.SelectedProcess == 0) // TODO remove with multi-process
|
|
processesData.SelectedProcess = (ushort)tmpProcPP.Id;
|
|
|
|
// Check if there are running process
|
|
if (!processesData.IsRunning)
|
|
{
|
|
// Get process status
|
|
cmsError = numericalControl.PROC_RStatus(i, ref status);
|
|
if (status == PROC_STATUS.RUN || status == PROC_STATUS.HOLD)
|
|
processesData.IsRunning = true;
|
|
else
|
|
status = PROC_STATUS.IDLE;
|
|
}
|
|
}
|
|
// Read selected axes
|
|
cmsError = numericalControl.AXES_RSelectedAxis(ref processesData.SelectedAxis);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
QueueStatusModel queueStatus = new QueueStatusModel();
|
|
// Get queue data
|
|
cmsError = numericalControl.FILES_RQueueDataByProcess(ref queueStatus, processesData.SelectedProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Set data
|
|
processesData.QueueStatus = queueStatus.Status;
|
|
processesData.StartStopQueueEnabled = queueStatus.StartStopQueueEnabled;
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetFunctionsMappedWithNC(out List<DTORuntimeFunctionalityModel> functionsAccessList)
|
|
{
|
|
functionsAccessList = new List<DTORuntimeFunctionalityModel>();
|
|
// Read plc functionality
|
|
List<FunctionalityModel> functionalityList = null;
|
|
CmsError cmsError = numericalControl.PLC_RFunctionAccess(ref functionalityList);
|
|
|
|
// If empty return
|
|
if (functionalityList == null || functionalityList.Count == 0)
|
|
return cmsError;
|
|
|
|
using (FunctionsAccessController functionsAccessController = new FunctionsAccessController())
|
|
{
|
|
// Map plc and database data
|
|
functionsAccessList = functionsAccessController.GetFunctionsMappedWithPlc(functionalityList);
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetMainenancesCounter(out List<CounterModel> counters)
|
|
{
|
|
// List of PLC counters
|
|
counters = new List<CounterModel>();
|
|
// Get counters values from PLC
|
|
CmsError cmsError = numericalControl.PLC_RMachineCounters(ref counters);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetMaintenanceDataById(int maintenanceId, int userId, out DTOMaintenanceModel dTOMaintenance)
|
|
{
|
|
dTOMaintenance = null;
|
|
|
|
CmsError cmsError = GetMaintenancesWithPermissions(out List<DTOMaintenanceModel> maintenance, userId);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
dTOMaintenance = maintenance.Where(x => x.Id == maintenanceId).FirstOrDefault();
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetMaintenancesWithPermissions(out List<DTOMaintenanceModel> dtoMaintenancesModel, int userId)
|
|
{
|
|
// Return value
|
|
dtoMaintenancesModel = new List<DTOMaintenanceModel>();
|
|
|
|
// Get counters values from PLC
|
|
CmsError cmsError = GetMainenancesCounter(out List<CounterModel> counters);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Get the last performed maintenance for each maintenance
|
|
List<PerformedMaintenanceModel> performedMaintenances = maintenancesController.FindLastPerformedMaintenances();
|
|
|
|
// Get all the active maintenances
|
|
List<MaintenanceModel> maintenances = maintenancesController.FindAll(); // TODO db or config file?
|
|
|
|
double percentage = 0;
|
|
TimeSpan missingDays = new TimeSpan();
|
|
CounterModel counter = new CounterModel();
|
|
|
|
bool canEdit = false;
|
|
bool canPerform = false;
|
|
foreach (var currMaintenance in maintenances)
|
|
{
|
|
// Get matching last performed maintenance for current maintenance
|
|
PerformedMaintenanceModel performed = performedMaintenances.Find(x => x.MaintenanceId == currMaintenance.MaintenanceId);
|
|
|
|
switch (currMaintenance.Type)
|
|
{
|
|
case MAINTENANCE_TYPE.MACHINE_INTERVAL:
|
|
{
|
|
// Get matching counter for the current maintenance
|
|
counter = counters.Find(x => x.Id == currMaintenance.CounterId);
|
|
|
|
int perfVal = 0;
|
|
if (performed != null)
|
|
perfVal = performed.CounterValue;
|
|
// Calc percentage = PLC - PERFORMED VALUE * 100 / interval
|
|
percentage = ((counter.Value - perfVal) * 100) / SupportFunctions.ConvertInMinutes(currMaintenance.Interval.Value, currMaintenance.UnitOfMeasure.Value);
|
|
missingDays = TimeSpan.FromMinutes(-1 * (counter.Value - perfVal - SupportFunctions.ConvertInMinutes(currMaintenance.Interval.Value, currMaintenance.UnitOfMeasure.Value)));
|
|
}
|
|
break;
|
|
|
|
case MAINTENANCE_TYPE.EXP_DATE:
|
|
{
|
|
// Already performed
|
|
if (performed != null)
|
|
{
|
|
percentage = 100;
|
|
missingDays = TimeSpan.Zero;
|
|
}
|
|
else
|
|
{
|
|
percentage = ((DateTime.Now - currMaintenance.CreationDate).TotalMinutes * 100) / (currMaintenance.Deadline - currMaintenance.CreationDate).TotalMinutes; // TimeSpan.FromTicks((currMaintenance.Deadline.Ticks)).TotalMinutes
|
|
missingDays = currMaintenance.Deadline - DateTime.Now;
|
|
counter = new CounterModel();
|
|
}
|
|
}
|
|
break;
|
|
|
|
case MAINTENANCE_TYPE.TIME_INTERVAL:
|
|
{
|
|
// Get last performed, default value is maintenance creation date
|
|
DateTime perfDate = currMaintenance.CreationDate;
|
|
if (performed != null)
|
|
perfDate = performed.Date;
|
|
|
|
// Now - last performed = elapsed interval since last maintenance
|
|
TimeSpan timePassedFromLastMaint = DateTime.Now.Subtract(perfDate);
|
|
// Convert in minutes
|
|
double minutesPassedFromLastMaint = timePassedFromLastMaint.TotalMinutes;
|
|
|
|
// percentage = Passed minutes since last maintenance * 100 / Interval
|
|
percentage = (minutesPassedFromLastMaint * 100) / SupportFunctions.ConvertInMinutes(currMaintenance.Interval.Value, currMaintenance.UnitOfMeasure.Value);
|
|
missingDays = TimeSpan.FromMinutes(-1 * (minutesPassedFromLastMaint - SupportFunctions.ConvertInMinutes(currMaintenance.Interval.Value, currMaintenance.UnitOfMeasure.Value)));
|
|
counter = new CounterModel();
|
|
}
|
|
break;
|
|
}
|
|
|
|
canEdit = false;
|
|
using (MachinesUsersController machineUsersController = new MachinesUsersController())
|
|
{
|
|
if (currMaintenance.UserId != null)
|
|
{
|
|
// Check if user can edit the maintenance -> caller id - maintenance user id
|
|
int comparision = machineUsersController.CompareUsersRole(userId, currMaintenance.UserId.Value, MachineConfig.MachineId);
|
|
if (comparision >= 0)
|
|
canPerform = canEdit = true;
|
|
}
|
|
else
|
|
{
|
|
canEdit = false;
|
|
canPerform = machineUsersController.UserIsCmsAdmin(MachineConfig.MachineId, userId);
|
|
}
|
|
}
|
|
|
|
dtoMaintenancesModel.Add(new DTOMaintenanceModel()
|
|
{
|
|
Id = currMaintenance.MaintenanceId,
|
|
Title = currMaintenance.UserId == null ? "" : currMaintenance.Title,
|
|
CreationDate = currMaintenance.CreationDate,
|
|
Deadline = currMaintenance.Deadline,
|
|
Description = currMaintenance.Description,
|
|
Interval = currMaintenance.Interval,
|
|
LastExpirationDate = currMaintenance.LastExpirationDate,
|
|
LastPerformedDate = performed?.Date,
|
|
Type = currMaintenance.Type.ToString(),
|
|
UnitOfMeasure = currMaintenance.UnitOfMeasure.ToString(),
|
|
PercentageOfCompletion = percentage >= 0 && percentage < 100 ? Convert.ToInt32(percentage) : 100,
|
|
MissingDays = missingDays,
|
|
PlcCounter = counter.Value,
|
|
CreatedByCms = currMaintenance.UserId == null,
|
|
CanEdit = canEdit,
|
|
CanPerform = canPerform,
|
|
Maintainer = performed?.Maintainer == null ? null : (DTOMessageUserModel)performed.Maintainer
|
|
});
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetExpiredMaintenances(out List<DTOExpiredMaintenanceModel> expiredMaintenance)
|
|
{
|
|
// Return value
|
|
expiredMaintenance = new List<DTOExpiredMaintenanceModel>();
|
|
|
|
// Get counters values from PLC
|
|
CmsError cmsError = GetMainenancesCounter(out List<CounterModel> counters);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Get the last performed maintenance for each maintenance
|
|
List<PerformedMaintenanceModel> performedMaintenances = maintenancesController.FindLastPerformedMaintenances();
|
|
|
|
// Get all the active maintenances
|
|
List<MaintenanceModel> maintenances = maintenancesController.FindAll(); // TODO db or config file?
|
|
|
|
foreach (var currMaintenance in maintenances)
|
|
{
|
|
// Get matching last performed maintenance for current maintenance
|
|
var performed = performedMaintenances.Find(x => x.MaintenanceId == currMaintenance.MaintenanceId);
|
|
|
|
switch (currMaintenance.Type)
|
|
{
|
|
case MAINTENANCE_TYPE.MACHINE_INTERVAL:
|
|
{
|
|
// Get matching counter for the current maintenance
|
|
var counter = counters.Find(x => x.Id == currMaintenance.CounterId);
|
|
|
|
int perfVal = 0;
|
|
if (performed != null)
|
|
perfVal = performed.CounterValue;
|
|
|
|
// MAINTENANCE_INTERVAL <= PLC - LASTPERFORMED
|
|
if (SupportFunctions.ConvertInMinutes(currMaintenance.Interval.Value, currMaintenance.UnitOfMeasure.Value) <= counter.Value - perfVal)
|
|
{
|
|
// Update last expiration date if null
|
|
if (currMaintenance.LastExpirationDate == null)
|
|
{
|
|
maintenancesController.UpdateLastExpirationDate(currMaintenance.MaintenanceId, DateTime.Now);
|
|
currMaintenance.LastExpirationDate = DateTime.Now;
|
|
}
|
|
|
|
// Add item to return list
|
|
expiredMaintenance.Add(new DTOExpiredMaintenanceModel()
|
|
{
|
|
Id = currMaintenance.MaintenanceId,
|
|
ExpirationDate = (DateTime)currMaintenance.LastExpirationDate,
|
|
CreatedByCms = currMaintenance.UserId == null,
|
|
Title = currMaintenance.UserId == null ? "" : currMaintenance.Title
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
|
|
case MAINTENANCE_TYPE.EXP_DATE:
|
|
{
|
|
// Already performed
|
|
if (performed != null)
|
|
break;
|
|
// DEADLINE <= NOW
|
|
if (currMaintenance.Deadline <= DateTime.Now)
|
|
{
|
|
maintenancesController.UpdateLastExpirationDate(currMaintenance.MaintenanceId, currMaintenance.Deadline);
|
|
currMaintenance.LastExpirationDate = currMaintenance.Deadline;
|
|
|
|
// Add item to return list
|
|
expiredMaintenance.Add(new DTOExpiredMaintenanceModel()
|
|
{
|
|
Id = currMaintenance.MaintenanceId,
|
|
ExpirationDate = currMaintenance.Deadline,
|
|
CreatedByCms = currMaintenance.UserId == null,
|
|
Title = currMaintenance.UserId == null ? "" : currMaintenance.Title,
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
|
|
case MAINTENANCE_TYPE.TIME_INTERVAL:
|
|
{
|
|
// Get last performed, default value is maintenance creation date
|
|
DateTime perfDate = currMaintenance.CreationDate;
|
|
if (performed != null)
|
|
perfDate = performed.Date;
|
|
|
|
// Now - last performed = elapsed interval since last maintenance
|
|
TimeSpan timePassedFromLastMaint = DateTime.Now.Subtract(perfDate);
|
|
// Convert in minutes
|
|
double minutesPassedFromLastMaint = timePassedFromLastMaint.TotalMinutes;
|
|
|
|
// Interval - passed minutes since last maintenance
|
|
if (SupportFunctions.ConvertInMinutes(currMaintenance.Interval.Value, currMaintenance.UnitOfMeasure.Value) <= minutesPassedFromLastMaint)
|
|
{
|
|
// Update last expiration date
|
|
if (currMaintenance.LastExpirationDate == null)
|
|
{
|
|
// Exp date = now - ( expiration intervall)
|
|
DateTime expirationDate = DateTime.Now.Subtract(TimeSpan.FromMinutes(minutesPassedFromLastMaint - currMaintenance.Interval.Value));
|
|
|
|
maintenancesController.UpdateLastExpirationDate(currMaintenance.MaintenanceId, expirationDate);
|
|
currMaintenance.LastExpirationDate = expirationDate;
|
|
}
|
|
|
|
// Add item to return list
|
|
expiredMaintenance.Add(new DTOExpiredMaintenanceModel()
|
|
{
|
|
Id = currMaintenance.MaintenanceId,
|
|
ExpirationDate = (DateTime)currMaintenance.LastExpirationDate,
|
|
CreatedByCms = currMaintenance.UserId == null,
|
|
Title = currMaintenance.UserId == null ? "" : currMaintenance.Title,
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetUserSoftKeys(out List<DTOUserSoftKeyModel> softKeys)
|
|
{
|
|
softKeys = new List<DTOUserSoftKeyModel>();
|
|
List<SoftKeysModel> plcSoftKeys = new List<SoftKeysModel>();
|
|
|
|
// Get softkeys data from PLC
|
|
CmsError cmsError = numericalControl.PLC_RUserSoftKeys(ref plcSoftKeys);
|
|
if (cmsError.IsError() || plcSoftKeys.Count == 0)
|
|
return cmsError;
|
|
|
|
foreach (var softKey in SoftKeysConfig)
|
|
{
|
|
// Check type of the selected softkey
|
|
if (softKey.Type == SOFTKEY_TYPE.GROUP)
|
|
{
|
|
// Foreach subkey create a softkey
|
|
foreach (var subkey in softKey.SubKeys)
|
|
{
|
|
// Find the plc matching data
|
|
var plcSoftKey = plcSoftKeys.Find(x => x.Id == subkey.PlcId);
|
|
// Create and add new user softkey model
|
|
softKeys.Add(new DTOUserSoftKeyModel()
|
|
{
|
|
Id = subkey.Id,
|
|
Active = plcSoftKey.Active,
|
|
Category = softKey.Category,
|
|
Value = plcSoftKey.Value
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Find the plc matching data
|
|
var plcSoftKey = plcSoftKeys.Find(x => x.Id == softKey.PlcId);
|
|
softKeys.Add(new DTOUserSoftKeyModel()
|
|
{
|
|
Id = softKey.Id,
|
|
Active = plcSoftKey.Active,
|
|
Category = softKey.Category,
|
|
Value = plcSoftKey.Value
|
|
});
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetNcSoftKeys(out List<DTONcSoftKeyModel> ncSoftKeys)
|
|
{
|
|
ncSoftKeys = new List<DTONcSoftKeyModel>();
|
|
List<SoftKeysModel> plcSoftKeys = new List<SoftKeysModel>();
|
|
|
|
// Get Nc softkeys data from PLC
|
|
CmsError cmsError = numericalControl.PLC_RNcSoftKeys(ref plcSoftKeys);
|
|
if (cmsError.IsError() || plcSoftKeys.Count == 0)
|
|
return cmsError;
|
|
|
|
foreach (var softKey in NcSoftKeysConfig)
|
|
{
|
|
// Find the plc matching data
|
|
var plcSoftKey = plcSoftKeys.Find(x => x.Id == softKey.Id);
|
|
|
|
// Create and add Nc softkey model
|
|
ncSoftKeys.Add(new DTONcSoftKeyModel()
|
|
{
|
|
Id = softKey.Id,
|
|
Active = plcSoftKey.Active,
|
|
Value = plcSoftKey.Value
|
|
});
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetHeadsData(out List<DTOHeadModel> heads)
|
|
{
|
|
// Returned value
|
|
heads = new List<DTOHeadModel>();
|
|
// Number of configured heads
|
|
int headsNumber = HeadsConfig.Count;
|
|
|
|
List<HeadDataModel> plcHeads = new List<HeadDataModel>();
|
|
// Read value from PLC
|
|
CmsError cmsError = numericalControl.PLC_RHeadsData(plcHeads, headsNumber);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
foreach (var head in plcHeads)
|
|
{
|
|
// Get current head config
|
|
var configuredHead = HeadsConfig.Find(x => x.Id == head.Id);
|
|
// Create different model according on type
|
|
switch (configuredHead.Type)
|
|
{
|
|
case HEAD_TYPE.SPINDLE:
|
|
{
|
|
heads.Add(new DTOSpindleModel()
|
|
{
|
|
Id = head.Id,
|
|
Process = head.Process,
|
|
Type = configuredHead.Type.ToString(),
|
|
inWarning = head.Load_Abrasive >= configuredHead.WarningLimit,
|
|
inAlarm = head.Load_Abrasive >= configuredHead.AlarmLimit,
|
|
OverrideEditable = head.OverrideEditable,
|
|
ActualSpeed = head.ActualSpeed_Pressure,
|
|
Load = head.Load_Abrasive,
|
|
MountedTool = (ushort)head.MountedTool_Vacum,
|
|
Override = head.Override,
|
|
IsActive = head.IsActive,
|
|
IsSelected = head.IsSelected,
|
|
AbrasiveIsActive = head.AbrasiveIsActive,
|
|
ToolData = head.Tool == null ? null : new DTOToolInSpindleModel()
|
|
{
|
|
ChildId = head.Tool.ChildId,
|
|
ToolName = head.Tool.ToolName
|
|
}
|
|
});
|
|
}
|
|
break;
|
|
|
|
case HEAD_TYPE.AWJ:
|
|
{
|
|
float vacumValue = (float)head.MountedTool_Vacum / 100;
|
|
heads.Add(new DTOAbrasiveWaterJet()
|
|
{
|
|
Id = head.Id,
|
|
Process = head.Process,
|
|
Type = configuredHead.Type.ToString(),
|
|
inWarning = vacumValue >= configuredHead.WarningLimit,
|
|
inAlarm = vacumValue >= configuredHead.AlarmLimit,
|
|
OverrideEditable = head.OverrideEditable,
|
|
ActualPressure = head.ActualSpeed_Pressure,
|
|
Abrasive = head.Load_Abrasive,
|
|
Vacum = vacumValue,
|
|
Override = head.Override,
|
|
IsActive = head.IsActive,
|
|
IsSelected = head.IsSelected,
|
|
AbrasiveIsActive = head.AbrasiveIsActive
|
|
});
|
|
}
|
|
break;
|
|
|
|
case HEAD_TYPE.WJ:
|
|
{
|
|
heads.Add(new DTOWaterJet()
|
|
{
|
|
Id = head.Id,
|
|
Process = head.Process,
|
|
Type = configuredHead.Type.ToString(),
|
|
inAlarm = false,
|
|
inWarning = false,
|
|
ActualPressure = head.ActualSpeed_Pressure,
|
|
OverrideEditable = head.OverrideEditable,
|
|
Override = head.Override,
|
|
IsActive = head.IsActive,
|
|
IsSelected = head.IsSelected,
|
|
AbrasiveIsActive = head.AbrasiveIsActive
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetM155Data(out List<DTOM155InputModel> data)
|
|
{
|
|
data = new List<DTOM155InputModel>();
|
|
List<M155InputIsNeededModel> ncData = new List<M155InputIsNeededModel>();
|
|
CmsError cmsError = numericalControl.PLC_ROperatorInputIsNeeded(ref ncData);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
data = ncData.Select(x => new DTOM155InputModel()
|
|
{
|
|
Buttons = x.Buttons,
|
|
IsNeeded = x.IsNeeded,
|
|
Message = x.Message,
|
|
Process = x.Process,
|
|
Type = x.Type.ToString()
|
|
}).ToList();
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError GetM154Data(out List<M154DataModel> data, out bool MTCStatus)
|
|
{
|
|
MTCStatus = false;
|
|
data = new List<M154DataModel>();
|
|
|
|
return numericalControl.PLC_RM154Data(ref data, ref MTCStatus);
|
|
}
|
|
|
|
public CmsError WriteM154Ack(int processId)
|
|
{
|
|
return numericalControl.PLC_W154ManageAck(processId);
|
|
}
|
|
|
|
#endregion Read Data
|
|
|
|
#region Write data
|
|
|
|
public CmsError PutNcSoftKeyClick(uint id)
|
|
{
|
|
// Write Nc softkey press to plc
|
|
CmsError cmsError = numericalControl.PLC_WNcSoftKey(id);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError PutUserSoftKeyClick(uint id)
|
|
{
|
|
// Write user softkey press to plc
|
|
return numericalControl.PLC_WUserSoftKey(id);
|
|
}
|
|
|
|
public CmsError RefreshAlarm(uint id)
|
|
{
|
|
// Write in memory the request to refresh the alarm
|
|
return numericalControl.PLC_WRefreshMessage(id);
|
|
}
|
|
|
|
public CmsError RestoreAlarm(uint id)
|
|
{
|
|
// Write in memory the request to restore the alarm
|
|
return numericalControl.PLC_WRestoreMessage(id);
|
|
}
|
|
|
|
public CmsError RefreshAllAlarms()
|
|
{
|
|
return numericalControl.PLC_WRefreshAllMessages();
|
|
}
|
|
|
|
public CmsError PutSelectProcess(ushort procNumber)
|
|
{
|
|
return numericalControl.PROC_WSelectProcess(procNumber);
|
|
}
|
|
|
|
public CmsError PutSelectAxis(byte axisId)
|
|
{
|
|
return numericalControl.AXES_WSelectAxis(axisId);
|
|
}
|
|
|
|
public CmsError PutOverride(uint id, string action)
|
|
{
|
|
HEAD_OVERRIDE_SIGN sign = HEAD_OVERRIDE_SIGN.MINUS;
|
|
if (action == "plus")
|
|
sign = HEAD_OVERRIDE_SIGN.PLUS;
|
|
|
|
CmsError cmsError = numericalControl.PLC_WHeadOverride(id, sign);
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError PutPowerOnData(uint id)
|
|
{
|
|
// Set to true power on data by id
|
|
return numericalControl.PLC_WPowerOnData(id, true);
|
|
}
|
|
|
|
public CmsError SetActiveLanguage(CultureInfo language)
|
|
{
|
|
// Set to true power on data by id
|
|
return numericalControl.NC_WLanguage(language);
|
|
}
|
|
|
|
public CmsError SetActiveScreen(short screen)
|
|
{
|
|
// Set to true power on data by id
|
|
return numericalControl.NC_SetScreenVisible((Nc.SCREEN_PAGE)screen);
|
|
}
|
|
|
|
public CmsError WriteM155Data(int process, double responseValue)
|
|
{
|
|
return numericalControl.PLC_WOperatorInputResponse(process, responseValue);
|
|
}
|
|
|
|
public CmsError WriteScada(string memIndex, SCADA_MEM_TYPE memType, object value)
|
|
{
|
|
return numericalControl.PLC_WScadaValue(memIndex, memType, value);
|
|
}
|
|
|
|
#endregion Write data
|
|
|
|
#region Siemens Tools
|
|
|
|
public CmsError GetToolTableConfiguration(out ToolTableConfiguration config)
|
|
{
|
|
config = new ToolTableConfiguration();
|
|
CmsError cmsError = numericalControl.TOOLS_RConfiguration(ref config);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
if (NcConfig.NcVendor != NC_VENDOR.SIEMENS && NcConfig.NcVendor != NC_VENDOR.DEMO)
|
|
{
|
|
config.FamilyOptionActive = true;
|
|
config.MultitoolOptionActive = true;
|
|
config.OffsetOptionActive = true;
|
|
|
|
List<string> categories = new List<string>();
|
|
if (!ToolManagerConfig.ShankOpt)
|
|
{
|
|
config.MultitoolOptionActive = false;
|
|
categories.Add("shankType");
|
|
config.ToolsConfiguration = config.ToolsConfiguration.Where(x => !(x.Name == "shankId")).ToList();
|
|
}
|
|
|
|
if (!ToolManagerConfig.FamilyOpt)
|
|
{
|
|
config.FamilyOptionActive = false;
|
|
categories.Add("family");
|
|
config.ToolsConfiguration = config.ToolsConfiguration.Where(x => !(x.Name == "familyId")).ToList();
|
|
config.ToolsConfiguration = config.FamiliesConfiguration.FamilyConfiguration.Concat(config.ToolsConfiguration).ToList();
|
|
}
|
|
else
|
|
{
|
|
config.ToolsConfiguration = config.ToolsConfiguration.Concat(config.FamiliesConfiguration.FamilyReadOnlyConfiguration).ToList();
|
|
}
|
|
|
|
if (!ToolManagerConfig.OffsetOpt)
|
|
config.OffsetOptionActive = false;
|
|
|
|
if (!ToolManagerConfig.OffsetOpt)
|
|
categories.Add("offset");
|
|
|
|
if (!ToolManagerConfig.TcpOpt)
|
|
categories.Add("tcp");
|
|
|
|
if (!ToolManagerConfig.CoolingOpt)
|
|
categories.Add("cooling");
|
|
|
|
if (!ToolManagerConfig.DynamicCompensationOpt)
|
|
categories.Add("dynamicCompensation");
|
|
|
|
if (!ToolManagerConfig.GammaOpt)
|
|
categories.Add("gamma");
|
|
|
|
if (!ToolManagerConfig.LifeOpt)
|
|
categories.Add("life");
|
|
|
|
if (!ToolManagerConfig.MagPosTypeOpt)
|
|
categories.Add("magPos");
|
|
|
|
if (!ToolManagerConfig.MultidimensionalShankOpt)
|
|
categories.Add("shankOpt");
|
|
|
|
if (!ToolManagerConfig.SelfAdaptivePathOpt)
|
|
categories.Add("selfAdaptive");
|
|
|
|
if (!ToolManagerConfig.BallufOpt)
|
|
categories.Add("balluf");
|
|
|
|
config.ToolsConfiguration = config.ToolsConfiguration.Where(x => !categories.Contains(x.Category)).ToList();
|
|
config.FamiliesConfiguration.FamilyConfiguration = config.FamiliesConfiguration.FamilyConfiguration.Where(x => !categories.Contains(x.Category)).ToList();
|
|
config.ShanksConfiguration.ShankConfiguration = config.ShanksConfiguration.ShankConfiguration.Where(x => !categories.Contains(x.Category)).ToList();
|
|
config.MagazinePosConfiguration = config.MagazinePosConfiguration.Where(x => !categories.Contains(x.Category)).ToList();
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError GetToolTableData(out List<SiemensToolModel> config)
|
|
{
|
|
config = new List<SiemensToolModel>();
|
|
return numericalControl.TOOLS_RToolsData(ref config);
|
|
}
|
|
|
|
public CmsError GetShanksData(out List<ShankModel> shanks)
|
|
{
|
|
shanks = new List<ShankModel>();
|
|
return numericalControl.TOOLS_RShanksData(ref shanks);
|
|
}
|
|
|
|
public CmsError GetFamiliesData(out List<FamilyModel> families)
|
|
{
|
|
families = new List<FamilyModel>();
|
|
return numericalControl.TOOLS_RFamilyData(ref families);
|
|
}
|
|
|
|
public CmsError GetMagazinesPositionsData(out List<PositionModel> magazinesPositions)
|
|
{
|
|
magazinesPositions = new List<PositionModel>();
|
|
return numericalControl.TOOLS_RMagazinePositions(ref magazinesPositions);
|
|
}
|
|
|
|
public CmsError GetMagazineStatus(out DTOMagazineActionModel magazineStatus)
|
|
{
|
|
// Set up models
|
|
magazineStatus = new DTOMagazineActionModel();
|
|
MagazineActionModel libModel = new MagazineActionModel();
|
|
// Read status from NC
|
|
CmsError cmsError = numericalControl.TOOLS_RMagazineAction(ref libModel);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
magazineStatus = (DTOMagazineActionModel)libModel;
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError AddTool(ref SiemensToolModel tool)
|
|
{
|
|
return numericalControl.TOOLS_WAddTool(ref tool);
|
|
}
|
|
|
|
public CmsError UpdateTool(ref SiemensToolModel tool)
|
|
{
|
|
return numericalControl.TOOLS_WUpdateTool(ref tool);
|
|
}
|
|
|
|
public CmsError AddFamily(string name, out FamilyModel family)
|
|
{
|
|
family = new FamilyModel()
|
|
{
|
|
Name = name
|
|
};
|
|
|
|
return numericalControl.TOOLS_WAddFamily(ref family);
|
|
}
|
|
|
|
public CmsError AddShank(ref ShankModel shank)
|
|
{
|
|
return numericalControl.TOOLS_WAddShank(ref shank);
|
|
}
|
|
|
|
public CmsError UpdateShank(ref ShankModel shank)
|
|
{
|
|
return numericalControl.TOOLS_WUpdateShank(ref shank);
|
|
}
|
|
|
|
public CmsError UpdateFamilyName(string oldName, string newName)
|
|
{
|
|
return numericalControl.TOOLS_WUpdateFamilyData(oldName, newName);
|
|
}
|
|
|
|
public CmsError UpdateMagazinePosition(PositionModel magazinePosition)
|
|
{
|
|
return numericalControl.TOOLS_WUpdatePosition(magazinePosition);
|
|
}
|
|
|
|
public CmsError DeleteTool(int id)
|
|
{
|
|
return numericalControl.TOOLS_WDeleteTool(id);
|
|
}
|
|
|
|
public CmsError DeleteShank(int id)
|
|
{
|
|
return numericalControl.TOOLS_WDeleteShank(id);
|
|
}
|
|
|
|
public CmsError DeleteFamily(string name)
|
|
{
|
|
return numericalControl.TOOLS_WDeleteFamily(name);
|
|
}
|
|
|
|
public CmsError AddEdge(int toolId, ref EdgeModel edge)
|
|
{
|
|
return numericalControl.TOOLS_WAddEdge(toolId, ref edge);
|
|
}
|
|
|
|
public CmsError UpdateEdge(int toolId, ref EdgeModel edge)
|
|
{
|
|
return numericalControl.TOOLS_WUpdateEdge(toolId, ref edge);
|
|
}
|
|
|
|
public CmsError DeleteEdge(int toolId, int edgeId)
|
|
{
|
|
return numericalControl.TOOLS_WDeleteEdge(toolId, edgeId);
|
|
}
|
|
|
|
public CmsError GetMagazinePositionsAndTools(int magazineId, out List<MountedToolModel> magazinePos)
|
|
{
|
|
magazinePos = new List<MountedToolModel>();
|
|
return numericalControl.TOOLS_RMountedTools(magazineId, ref magazinePos);
|
|
}
|
|
|
|
public CmsError GetNotInMagazinesTools(out List<ShankModel> multiTools, out List<SiemensToolModel> tools)
|
|
{
|
|
multiTools = new List<ShankModel>();
|
|
tools = new List<SiemensToolModel>();
|
|
return numericalControl.TOOLS_RAvailableTools(ref multiTools, ref tools);
|
|
}
|
|
|
|
public CmsError LoadToolInMagazine(int magazineId, NewToolInMagazineModel newMag, out MountedToolModel mounted)
|
|
{
|
|
mounted = new MountedToolModel();
|
|
return numericalControl.TOOLS_WLoadToolInMagazine(magazineId, newMag, ref mounted);
|
|
}
|
|
|
|
public CmsError UnloadToolInMagazine(int magazineId, int positionId)
|
|
{
|
|
return numericalControl.TOOLS_WUnloadToolFromMagazine(magazineId, positionId);
|
|
}
|
|
|
|
public CmsError LoadTooolIntoShank(int shankId, int positionId, int toolId)
|
|
{
|
|
return numericalControl.TOOLS_WLoadToolIntoShank(shankId, positionId, toolId);
|
|
}
|
|
|
|
public CmsError UnloadTooolFromShank(int shankId, int positionId)
|
|
{
|
|
return numericalControl.TOOLS_WUnloadToolFromShank(shankId, positionId);
|
|
}
|
|
|
|
#endregion Siemens Tools
|
|
|
|
// OSAI FANUC
|
|
|
|
#region Osai/Fanuc Tools
|
|
|
|
public CmsError GetToolsData(out List<DTONcToolModel> dtoTools)
|
|
{
|
|
dtoTools = new List<DTONcToolModel>();
|
|
|
|
CmsError cmsError = NO_ERROR;
|
|
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
List<DbNcToolModel> tools = toolsManager.FindToolsWithDependencies();
|
|
|
|
foreach (DbNcToolModel tool in tools)
|
|
{
|
|
DTONcToolModel dtoTool = (DTONcToolModel)tool;
|
|
|
|
cmsError = GetToolData(tool, ref dtoTool);
|
|
|
|
dtoTools.Add(dtoTool);
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
}
|
|
|
|
public CmsError GetToolData(DbNcToolModel tool, ref DTONcToolModel dtoTool)
|
|
{
|
|
dtoTool = (DTONcToolModel)tool;
|
|
|
|
OffsetModel offset = new OffsetModel();
|
|
CmsError cmsError = NO_ERROR;
|
|
|
|
if (tool.OffsetId1 != null && tool.OffsetId1 != 0)
|
|
{
|
|
// Read first offset data
|
|
cmsError = numericalControl.TOOLS_ROffset((short)tool.OffsetId1, ref offset);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
dtoTool.Offset1 = offset;
|
|
}
|
|
if (tool.OffsetId2 != null && tool.OffsetId2 != 0)
|
|
{
|
|
// Read second offset data
|
|
cmsError = numericalControl.TOOLS_ROffset((short)tool.OffsetId2, ref offset);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
dtoTool.Offset2 = offset;
|
|
}
|
|
if (tool.OffsetId3 != null && tool.OffsetId3 != 0)
|
|
{
|
|
// Read third offset data
|
|
cmsError = numericalControl.TOOLS_ROffset((short)tool.OffsetId3, ref offset);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
dtoTool.Offset3 = offset;
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError AddTool(DTONewNcToolModel tool, out DTONcToolModel dtoTool, short toolId)
|
|
{
|
|
dtoTool = new DTONcToolModel();
|
|
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
DbNcToolModel ncTool = toolsManager.AddTool(tool, toolId);
|
|
|
|
if (!ToolManagerConfig.OffsetOpt)
|
|
{
|
|
CmsError cmsError = UpdateToolOffsetId(ncTool.ToolId, 1, ncTool.ToolId, out DTONcToolModel toolWithOffset);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
ncTool.OffsetId1 = ncTool.ToolId;
|
|
}
|
|
|
|
GetToolData(ncTool, ref dtoTool);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
}
|
|
|
|
public CmsError UpdateTool(int toolId, DTONewNcToolModel newTool, out DTONcToolModel toolWithOffsets, DTONewNcToolWithFamilyModel dtoToolWithFamily)
|
|
{
|
|
toolWithOffsets = new DTONcToolModel();
|
|
CmsError cmsError = NO_ERROR;
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
// If option is active
|
|
if (!ToolManagerConfig.FamilyOpt)
|
|
{
|
|
// Copy only family
|
|
DTONewNcFamilyModel newFamily = new DTONewNcFamilyModel();
|
|
SupportFunctions.CopyProperties(dtoToolWithFamily, newFamily);
|
|
// Update only family data
|
|
newFamily = (DTONcFamilyModel)toolsManager.UpdateFamily(dtoToolWithFamily.FamilyId, newFamily);
|
|
|
|
// Update Nc families
|
|
cmsError = UpdateNcFamily(toolsManager);
|
|
if (cmsError.IsError())
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
|
}
|
|
|
|
// Update database tool Tool
|
|
DbNcToolModel dbTool = toolsManager.UpdateTool(toolId, newTool);
|
|
|
|
// Populate updated tool with offset data
|
|
cmsError = GetToolData(dbTool, ref toolWithOffsets);
|
|
if (cmsError.IsError())
|
|
{
|
|
dbContextTransaction.Rollback();
|
|
return cmsError;
|
|
}
|
|
|
|
bool startIsActive = false;
|
|
|
|
// Check if tool is loaded into a shank
|
|
if (dbTool.ShankId != null)
|
|
{
|
|
// Check if shank is loaded into magazine
|
|
DbNcShankModel shank = toolsManager.FindShankWithTools(dbTool.ShankId.Value);
|
|
if (shank.MagazineId != null)
|
|
{
|
|
startIsActive = true;
|
|
// Create backup of data stored on the NC and block file accesses
|
|
cmsError = numericalControl.TOOLS_WStartEditData();
|
|
if (cmsError.IsError())
|
|
{
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
return cmsError;
|
|
}
|
|
|
|
// Update tool
|
|
cmsError = UpdateNcTools(toolsManager);
|
|
}
|
|
}
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CmsError UpdateNcTools(NcToolManagerController toolsManager = null)
|
|
{
|
|
if (toolsManager == null)
|
|
toolsManager = new NcToolManagerController();
|
|
|
|
List<NcShankModel> shanks = toolsManager
|
|
.FindShanks()
|
|
.Where(x => x.MagazineId != null)
|
|
.Select(x => (NcShankModel)x)
|
|
.ToList();
|
|
|
|
// Get tools
|
|
List<NcToolModel> tools = toolsManager
|
|
.FindTools()
|
|
.Select(x => (NcToolModel)x)
|
|
.Where(x => shanks.Any(y => y.ShankId == x.ShankId)) // Find only mounted tools
|
|
.ToList();
|
|
|
|
// Update tools
|
|
return numericalControl.TOOLS_WUpdateTools(tools);
|
|
}
|
|
|
|
public CmsError UpdateOffset(short offsetId, OffsetModel offsetData)
|
|
{
|
|
return numericalControl.TOOLS_WOffset(offsetId, offsetData);
|
|
}
|
|
|
|
public CmsError UpdateToolOffsetId(int toolId, int positionId, short offsetId, out DTONcToolModel toolWithOffsets)
|
|
{
|
|
toolWithOffsets = new DTONcToolModel();
|
|
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
// Update database tool Tool
|
|
DbNcToolModel dbTool = toolsManager.UpdateToolOffset(toolId, positionId, offsetId);
|
|
|
|
// Populate updated tool with offset data
|
|
CmsError cmsError = GetToolData(dbTool, ref toolWithOffsets);
|
|
if (cmsError.IsError())
|
|
{
|
|
dbContextTransaction.Rollback();
|
|
return cmsError;
|
|
}
|
|
|
|
bool startIsActive = false;
|
|
|
|
// Check if tool is loaded into a shank
|
|
if (dbTool.ShankId != null)
|
|
{
|
|
// Check if shank is loaded into magazine
|
|
DbNcShankModel shank = toolsManager.FindShankWithTools(dbTool.ShankId.Value);
|
|
if (shank.MagazineId != null)
|
|
{
|
|
startIsActive = true;
|
|
// Create backup of data stored on the NC and block file accesses
|
|
cmsError = numericalControl.TOOLS_WStartEditData();
|
|
if (cmsError.IsError())
|
|
{
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
return cmsError;
|
|
}
|
|
|
|
// R tool
|
|
cmsError = UpdateNcTools(toolsManager);
|
|
}
|
|
}
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CmsError DeleteNcTool(DbNcToolModel tool)
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
// Delete db tool
|
|
toolsManager.DeleteTool(tool.ToolId);
|
|
|
|
if (!ToolManagerConfig.FamilyOpt)
|
|
{
|
|
DbNcFamilyModel fam = toolsManager.FindFamily(tool.FamilyId);
|
|
// Check if family is connected to other tools
|
|
if (fam != null && fam.Tools.Count() == 1)
|
|
{
|
|
toolsManager.DeleteFamily(fam.FamilyId);
|
|
}
|
|
}
|
|
DbNcShankModel shank = null;
|
|
if (!ToolManagerConfig.ShankOpt && tool.ShankId != null)
|
|
{
|
|
// If option is active find & delete tool shank
|
|
shank = toolsManager.FindShankWithTools(tool.ShankId.Value);
|
|
|
|
toolsManager.DeleteNcShank(shank.ShankId);
|
|
}
|
|
|
|
bool startIsActive = false;
|
|
CmsError cmsError = NO_ERROR;
|
|
|
|
if (tool.ShankId != null)
|
|
{
|
|
if (shank.MagazineId != null)
|
|
{
|
|
startIsActive = true;
|
|
// Create backup of data stored on the NC and block file accesses
|
|
cmsError = numericalControl.TOOLS_WStartEditData();
|
|
if (cmsError.IsError())
|
|
{
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
return cmsError;
|
|
}
|
|
|
|
// Update Nc data
|
|
cmsError = UpdateNcTools(toolsManager);
|
|
}
|
|
}
|
|
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
|
|
return cmsError;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CmsError UpdateShank(int shankId, DTONewNcShankModel dtoShank, out DTONcShankModel shank)
|
|
{
|
|
shank = new DTONcShankModel();
|
|
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
bool startIsActive = false;
|
|
CmsError cmsError = NO_ERROR;
|
|
|
|
// Update db data
|
|
shank = (DTONcShankModel)toolsManager.UpdateShank(shankId, dtoShank);
|
|
if (shank.MagazineId != null)
|
|
{
|
|
startIsActive = true;
|
|
// Create backup of data stored on the NC and block file accesses
|
|
cmsError = numericalControl.TOOLS_WStartEditData();
|
|
if (cmsError.IsError())
|
|
{
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
return cmsError;
|
|
}
|
|
|
|
// Update nc data
|
|
cmsError = UpdateNcShank(toolsManager);
|
|
}
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
|
|
return cmsError;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CmsError DeleteNcShank(int shankId)
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
CmsError cmsError = NO_ERROR;
|
|
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
// Delete database shank
|
|
DbNcShankModel deletedShank = toolsManager.DeleteNcShank(shankId);
|
|
bool startIsActive = false;
|
|
if (deletedShank.MagazineId != null)
|
|
{
|
|
startIsActive = true;
|
|
// Create backup of data stored on the NC and block file accesses
|
|
cmsError = numericalControl.TOOLS_WStartEditData();
|
|
if (cmsError.IsError())
|
|
{
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
return cmsError;
|
|
}
|
|
|
|
// Update nc data
|
|
cmsError = UpdateNcShank(toolsManager);
|
|
}
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
|
|
|
return cmsError;
|
|
}
|
|
}
|
|
}
|
|
|
|
private CmsError UpdateNcShank(NcToolManagerController toolsManager)
|
|
{
|
|
// Get mounted shanks
|
|
List<NcShankModel> shanks = toolsManager
|
|
.FindShanks()
|
|
.Where(x => x.MagazineId != null)
|
|
.Select(x => (NcShankModel)x)
|
|
.ToList();
|
|
|
|
// Update nc data
|
|
return numericalControl.TOOLS_WUpdateShanks(shanks);
|
|
}
|
|
|
|
public DTONcFamilyModel AddFamily(DTONewNcFamilyModel family)
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
return (DTONcFamilyModel)toolsManager.AddFamily(family);
|
|
}
|
|
}
|
|
|
|
public CmsError UpdateFamily(int familyId, DTONewNcFamilyModel family, out DTONcFamilyModel newFamily)
|
|
{
|
|
newFamily = new DTONcFamilyModel();
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
// Update db family
|
|
newFamily = (DTONcFamilyModel)toolsManager.UpdateFamily(familyId, family);
|
|
|
|
// Create backup of data stored on the NC and block file accesses
|
|
CmsError cmsError = numericalControl.TOOLS_WStartEditData();
|
|
if (cmsError.IsError())
|
|
{
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
|
return cmsError;
|
|
}
|
|
|
|
// Update Nc families
|
|
cmsError = UpdateNcFamily(toolsManager);
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
|
|
|
return cmsError;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CmsError DeleteNcFamily(int famId)
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
// Create backup of data stored on the NC and block file accesses
|
|
CmsError cmsError = numericalControl.TOOLS_WStartEditData();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
toolsManager.DeleteFamily(famId);
|
|
// Update Nc families
|
|
cmsError = UpdateNcFamily(toolsManager);
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
|
|
|
return cmsError;
|
|
}
|
|
}
|
|
}
|
|
|
|
private CmsError UpdateNcFamily(NcToolManagerController toolsManager)
|
|
{
|
|
// Get mounted shanks
|
|
List<NcShankModel> shanks = toolsManager
|
|
.FindShanks()
|
|
.Where(x => x.MagazineId != null)
|
|
.Select(x => (NcShankModel)x)
|
|
.ToList();
|
|
|
|
// Get tools
|
|
List<NcToolModel> tools = toolsManager
|
|
.FindTools()
|
|
.Select(x => (NcToolModel)x)
|
|
.Where(x => shanks.Any(y => y.ShankId == x.ShankId)) // Find only mounted tools
|
|
.ToList();
|
|
|
|
List<NcFamilyModel> families = toolsManager
|
|
.FindFamilies()
|
|
.Select(x => (NcFamilyModel)x)
|
|
.Where(x => tools.Any(y => y.FamilyId == x.FamilyId)) // Find only families of mounted tools
|
|
.ToList();
|
|
|
|
// Update nc families
|
|
return numericalControl.TOOLS_WUpdateFamilies(families);
|
|
}
|
|
|
|
public CmsError UpdateMagazinePosition(DbNcMagazinePositionModel dbPos, DTONcMagazinePositionModel dtoPos, out DTONcMagazinePositionModel magPos)
|
|
{
|
|
magPos = new DTONcMagazinePositionModel();
|
|
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
using (DbContextTransaction dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
|
{
|
|
// Create backup of data stored on the NC and block file accesses
|
|
CmsError cmsError = StartEditData();
|
|
if (cmsError.IsError())
|
|
{
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
|
return cmsError;
|
|
}
|
|
|
|
// Update database data
|
|
magPos = (DTONcMagazinePositionModel)toolsManager.UpdatePosition(dbPos, dtoPos);
|
|
|
|
// Get magazines positions
|
|
List<NcMagazinePositionModel> positions = toolsManager.FindMagazinesPositions()
|
|
.Select(x => (NcMagazinePositionModel)x)
|
|
.ToList();
|
|
// Update Nc data
|
|
cmsError = numericalControl.TOOLS_WUpdateMagazinePositions(positions);
|
|
|
|
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
|
|
|
return cmsError;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ManageErrorAndTransaction(CmsError cmsError, DbContextTransaction dbContextTransaction, bool ncStartIsActive)
|
|
{
|
|
// Check cmsError, manage transaction and if startIsActive = true manage Nc backup created with TOOLS_WStartEditData()
|
|
if (cmsError.IsError())
|
|
{
|
|
// Close transaction
|
|
dbContextTransaction.Rollback();
|
|
// Restore backup
|
|
if (ncStartIsActive)
|
|
numericalControl.TOOLS_WRestoreBackup();
|
|
}
|
|
else
|
|
{
|
|
// Close transaction
|
|
dbContextTransaction.Commit();
|
|
// Stop editing ncData
|
|
if (ncStartIsActive)
|
|
numericalControl.TOOLS_WStopEditData();
|
|
}
|
|
}
|
|
|
|
public DTONcShankModel LoadIntoShank(DbNcToolModel tool, short shankId)
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
return toolsManager.LoadToolIntoShank(tool, shankId);
|
|
}
|
|
}
|
|
|
|
public DTONcShankModel UnloadFromShank(DbNcToolModel tool)
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
return toolsManager.UnloadToolFromShank(tool);
|
|
}
|
|
}
|
|
|
|
public CmsError SetupNcToolManager()
|
|
{
|
|
CmsError cmsError = UpdateMagazinePositionConf();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
cmsError = WriteOption();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError UpdateMagazinePositionConf()
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
// Get database configured positions
|
|
List<DbNcMagazinePositionModel> dbPositions = toolsManager.FindMagazinesPositions();
|
|
if (dbPositions.Count <= 0)
|
|
{
|
|
CmsError cmsError = GetMagazineConfiguration(out List<NcMagazineConfigModel> config);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Setup new positions list in order to be saved in the database
|
|
List<DbNcMagazinePositionModel> fromConfigPosition = new List<DbNcMagazinePositionModel>();
|
|
foreach (var conf in config)
|
|
{
|
|
for (byte i = 1; i <= conf.MaxPositions; i++)
|
|
{
|
|
// Create new position
|
|
fromConfigPosition.Add(new DbNcMagazinePositionModel()
|
|
{
|
|
MagazineId = conf.Id,
|
|
PositionId = i,
|
|
Disabled = false,
|
|
Type = 0
|
|
});
|
|
}
|
|
}
|
|
|
|
// Update database
|
|
toolsManager.SetupMagazinePositions(fromConfigPosition);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
}
|
|
|
|
public CmsError WriteOption()
|
|
{
|
|
ToolManagerOptionsModel options = new ToolManagerOptionsModel();
|
|
SupportFunctions.CopyProperties(ToolManagerConfig, options);
|
|
|
|
return numericalControl.TOOLS_WOptions(options);
|
|
}
|
|
|
|
public CmsError GetMagazineConfiguration(out List<NcMagazineConfigModel> config)
|
|
{
|
|
config = new List<NcMagazineConfigModel>();
|
|
// Read configuration
|
|
return numericalControl.TOOLS_RMagazineConfig(ref config);
|
|
}
|
|
|
|
public CmsError StartEditData()
|
|
{
|
|
return numericalControl.TOOLS_WStartEditData();
|
|
}
|
|
|
|
public CmsError StopEditData()
|
|
{
|
|
return numericalControl.TOOLS_WStopEditData();
|
|
}
|
|
|
|
public CmsError StartEditTooling(int magazineId)
|
|
{
|
|
return numericalControl.TOOLS_WStartEditTooling(magazineId);
|
|
}
|
|
|
|
public CmsError StopEditTooling(int magazineId)
|
|
{
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
// Get mounted shanks
|
|
List<NcShankModel> shanks = toolsManager
|
|
.FindShanks()
|
|
.Where(x => x.MagazineId != null)
|
|
.Select(x => (NcShankModel)x)
|
|
.ToList();
|
|
// Update shanks
|
|
CmsError cmsError = numericalControl.TOOLS_WUpdateShanks(shanks);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Get magazine positions
|
|
List<NcMagazinePositionModel> positions = toolsManager.FindMagazinesPositions()
|
|
.Select(x => (NcMagazinePositionModel)x)
|
|
.ToList();
|
|
// Update positions
|
|
cmsError = numericalControl.TOOLS_WUpdateMagazinePositions(positions);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Get tools
|
|
List<NcToolModel> tools = toolsManager
|
|
.FindTools()
|
|
.Select(x => (NcToolModel)x)
|
|
.Where(x => shanks.Any(y => y.ShankId == x.ShankId)) // Find only mounted tools
|
|
.ToList();
|
|
|
|
// Update tools
|
|
cmsError = numericalControl.TOOLS_WUpdateTools(tools);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Get families
|
|
List<NcFamilyModel> families = toolsManager
|
|
.FindFamilies()
|
|
.Select(x => (NcFamilyModel)x)
|
|
.Where(x => tools.Any(y => y.FamilyId == x.FamilyId)) // Find only families of mounted tools
|
|
.ToList();
|
|
|
|
// Update families
|
|
cmsError = numericalControl.TOOLS_WUpdateFamilies(families);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
}
|
|
|
|
return numericalControl.TOOLS_WStopEditTooling(magazineId);
|
|
}
|
|
|
|
public CmsError GetUpdatedToolsData(out Dictionary<int, byte> updatedStatus, out Dictionary<int, uint> updatedLives)
|
|
{
|
|
updatedStatus = new Dictionary<int, byte>();
|
|
updatedLives = new Dictionary<int, uint>();
|
|
return numericalControl.TOOLS_RUpdatedToolsData(ref updatedStatus, ref updatedLives);
|
|
}
|
|
|
|
public CmsError CheckIfShankCanFit(int magazineId, int positionId, DbNcShankModel shankToBeLoaded)
|
|
{
|
|
CmsError cmsError = GetMagazineConfiguration(out List<NcMagazineConfigModel> magazinesConfig);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
NcMagazineConfigModel magConfig = magazinesConfig.Where(x => x.Id == magazineId).FirstOrDefault();// Get only current magazine config
|
|
|
|
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
|
{
|
|
List<DbNcToolModel> magazineMountedTools = toolsManager.GetMountedTools();
|
|
|
|
// Check if there is a tool already mounted in magazineId and positionId
|
|
var toolAlreadyMounted = magazineMountedTools.Where(x => x.Shank.MagazineId == magazineId && x.Shank.PositionId == positionId).FirstOrDefault();
|
|
if (toolAlreadyMounted != null)
|
|
return MAGAZINE_POSITION_OCCUPIED_ERROR;
|
|
|
|
// Get shank occupied space
|
|
toolsManager.GetShankMaxSpaceOccupied(shankToBeLoaded.ShankId, out int maxRight, out int maxLeft);
|
|
|
|
// Check if tool can fit in the position considering other tools
|
|
foreach (var mountedTool in magazineMountedTools)
|
|
{
|
|
// Check if is not the same position && if is different check if the position is on the same magazine
|
|
if (mountedTool.Shank.PositionId != positionId && mountedTool.Shank.MagazineId == magazineId)
|
|
{
|
|
int decPosition, incPosition;
|
|
decPosition = incPosition = mountedTool.Shank.PositionId.Value;
|
|
// if it is a circular magazine move position before ( for left case ) or after ( right case ) the location you want to mount the tool
|
|
if (magConfig.Type != NC_MAGAZINE_TYPE.BOX_MAGAZINE)
|
|
{
|
|
decPosition = decPosition < positionId ? decPosition + magConfig.MaxPositions : decPosition;
|
|
incPosition = incPosition > positionId ? incPosition - magConfig.MaxPositions : incPosition;
|
|
}
|
|
|
|
// Right check
|
|
if (positionId < decPosition)
|
|
{
|
|
if (!(magConfig.Type == NC_MAGAZINE_TYPE.BOX_MAGAZINE && mountedTool.Shank.PositionId == 1))
|
|
{
|
|
// Calculate left space occupied (with mounted shank)
|
|
double leftMounted = decPosition - (mountedTool.Family.LeftSize / 2.0);
|
|
// Calculate right space needed (with shank to be loaded)
|
|
double rightToBeMount = positionId + (maxRight / 2.0);
|
|
if (leftMounted < rightToBeMount)
|
|
return MAGAZINE_POSITION_OCCUPIED_ERROR;
|
|
}
|
|
}
|
|
|
|
// Left check
|
|
if (positionId > incPosition)
|
|
{
|
|
if (!(magConfig.Type == NC_MAGAZINE_TYPE.BOX_MAGAZINE && mountedTool.Shank.PositionId == magConfig.MaxPositions))
|
|
{
|
|
// Calculate right space occupied (with mounted shank)
|
|
double rightMounted = incPosition + (mountedTool.Family.RightSize / 2.0);
|
|
// Calculate left space needed (with shank to be loaded)
|
|
double leftToBeMounted = positionId - (maxLeft / 2.0);
|
|
if (rightMounted > leftToBeMounted)
|
|
return MAGAZINE_POSITION_OCCUPIED_ERROR;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public CmsError GetNcMagazineStatus(out Dictionary<int, bool> status)
|
|
{
|
|
status = new Dictionary<int, bool>();
|
|
return numericalControl.TOOLS_RMagazineStatus(ref status);
|
|
}
|
|
|
|
#endregion Osai/Fanuc Tools
|
|
|
|
public CmsError ReadScadasData(List<ScadaSchemaModel> schemasToRead, out List<DTOScadaModel> scadas)
|
|
{
|
|
CmsError cmsError = NO_ERROR;
|
|
scadas = new List<DTOScadaModel>();
|
|
|
|
List<int> alreadyReadScada = new List<int>();
|
|
|
|
foreach (var schema in schemasToRead)
|
|
{
|
|
// Avoid duplicated reads
|
|
if (!alreadyReadScada.Contains(schema.Id))
|
|
{
|
|
// Read one schema
|
|
cmsError = ReadScadaData(schema, out DTOScadaModel scadaValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Add value to return list
|
|
scadas.Add(scadaValue);
|
|
// Add id to read scada
|
|
alreadyReadScada.Add(schema.Id);
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError ReadScadaData(ScadaSchemaModel scadaSchema, out DTOScadaModel scadaValue)
|
|
{
|
|
CmsError cmsError = NO_ERROR;
|
|
scadaValue = new DTOScadaModel()
|
|
{
|
|
Id = scadaSchema.Id,
|
|
Name = scadaSchema.Name,
|
|
IsInProductionPage = scadaSchema.IsInProductionPage,
|
|
Buttons = new List<DTOScadaButtonModel>()
|
|
};
|
|
|
|
// Cycle inside layers
|
|
foreach (var layer in scadaSchema.Layers)
|
|
{
|
|
object val = null;
|
|
object val2 = null;
|
|
// Cycle through elements, use the max number of elements between layer components in order to be optimized
|
|
for (int i = 0; i < GetMaxCountBetweenLayerComponents(layer); i++)
|
|
{
|
|
if (layer.Buttons.ElementAtOrDefault(i) != null)
|
|
{
|
|
// Read enabled from PLC
|
|
cmsError = numericalControl.PLC_RScadaValue(layer.Buttons[i].Status.MemEnabledIndex, SCADA_MEM_TYPE.BOOL, ref val);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
// Populate & add new object into scada model
|
|
scadaValue.Buttons.Add(new DTOScadaButtonModel()
|
|
{
|
|
Id = layer.Buttons[i].Id,
|
|
Enabled = Convert.ToBoolean(val)
|
|
});
|
|
}
|
|
|
|
if (layer.Images.ElementAtOrDefault(i) != null)
|
|
{
|
|
// Read isVisible from PLC
|
|
cmsError = numericalControl.PLC_RScadaValue(layer.Images[i].Status.MemVisibleIndex, SCADA_MEM_TYPE.BOOL, ref val);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
// Populate & add new object into scada model
|
|
scadaValue.Images.Add(new DTOScadaImageModel()
|
|
{
|
|
Id = layer.Images[i].Id,
|
|
IsVisible = Convert.ToBoolean(val)
|
|
});
|
|
}
|
|
|
|
if (layer.ProgressBars.ElementAtOrDefault(i) != null)
|
|
{
|
|
// Read value from PLC
|
|
cmsError = numericalControl.PLC_RScadaValue(layer.ProgressBars[i].MemValueIndex, SCADA_MEM_TYPE.BYTE, ref val);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
// Populate & add new object into scada model
|
|
scadaValue.ProgressBars.Add(new DTOScadaProgressBarModel()
|
|
{
|
|
Id = layer.ProgressBars[i].Id,
|
|
Value = Convert.ToByte(val)
|
|
});
|
|
}
|
|
|
|
if (layer.Inputs.ElementAtOrDefault(i) != null && layer.Inputs[i].Status.Action == "read")
|
|
{
|
|
// Read isVisible from PLC
|
|
cmsError = numericalControl.PLC_RScadaValue(layer.Inputs[i].Status.MemEnabledIndex, SCADA_MEM_TYPE.BOOL, ref val);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Read value from PLC
|
|
cmsError = numericalControl.PLC_RScadaValue(layer.Inputs[i].Status.MemValueIndex, SupportFunctions.GetMemTypeFromString(layer.Inputs[i].Status.Type), ref val2);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Populate & add new object into scada model
|
|
scadaValue.Inputs.Add(new DTOScadaInputModel()
|
|
{
|
|
Id = layer.Inputs[i].Id,
|
|
Value = new DTOScadaValueModel()
|
|
{
|
|
IsEnabled = Convert.ToBoolean(val),
|
|
Value = val2
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
private int GetMaxCountBetweenLayerComponents(ScadaSchemaLayerModel layer)
|
|
{
|
|
// Choose which list contains the maximum number of elements
|
|
int max = layer.Buttons.Count();
|
|
|
|
if (layer.Images.Count() > max)
|
|
max = layer.Images.Count();
|
|
|
|
if (layer.Inputs.Count() > max)
|
|
max = layer.Inputs.Count();
|
|
|
|
if (layer.ProgressBars.Count() > max)
|
|
max = layer.ProgressBars.Count();
|
|
|
|
return max;
|
|
}
|
|
|
|
//DTOScadaModel scada = new DTOScadaModel
|
|
//{
|
|
// Layers = prodScada.Layers.Select(x => new DTOScadaLayerModel()
|
|
// {
|
|
// Id = x.Id,
|
|
// Buttons = x.Buttons.Select(y => new DTOScadaButtonModel()
|
|
// {
|
|
// Id = y.Id
|
|
// }).ToList(),
|
|
// Images = x.Images.Select(y => new DTOScadaImageModel()
|
|
// {
|
|
// Id = y.Id
|
|
// }).ToList(),
|
|
// ProgressBars = x.ProgressBars.Select(y => new DTOScadaProgressBarModel()
|
|
// {
|
|
// Id = y.Id
|
|
// }).ToList(),
|
|
// Inputs = x.Inputs.Select(y => new DTOScadaInputModel()
|
|
// {
|
|
// Id = y.Id
|
|
// }).ToList()
|
|
// }).ToList()
|
|
//};
|
|
}
|
|
} |