461 lines
18 KiB
C#
461 lines
18 KiB
C#
using CMS_CORE;
|
|
using CMS_CORE.Demo;
|
|
using CMS_CORE.Fanuc;
|
|
using CMS_CORE.Osai;
|
|
using CMS_CORE.Siemens;
|
|
using Step.Database.Controllers;
|
|
using Step.Model.DatabaseModels;
|
|
using Step.Model.DTOModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
using static CMS_CORE.Nc;
|
|
using static CMS_CORE_Library.DataStructures;
|
|
using static Step.Config.ServerConfig;
|
|
using static Step.Utils.Constants;
|
|
|
|
namespace Step.NC
|
|
{
|
|
public class NcHandler : IDisposable
|
|
{
|
|
public Nc numericalControl;
|
|
|
|
public NcHandler()
|
|
{
|
|
// Choose NC
|
|
numericalControl = SetNumericalControl();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
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;
|
|
}
|
|
|
|
public CmsError GetNcGenericData(out DTONcGenericDataModel genericData)
|
|
{
|
|
genericData = new DTONcGenericDataModel();
|
|
|
|
// 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
|
|
numericalControl.NC_RSoftwareVersion(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.NcSoftwareVersion = tmpInfo;
|
|
|
|
// Get model name
|
|
numericalControl.NC_RModelName(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.NcModel = tmpInfo;
|
|
|
|
// Get machine number
|
|
numericalControl.NC_RMachineNumber(ref tmpInfo);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
genericData.CmsMachineIdNumber = tmpInfo;
|
|
|
|
// Get max process number
|
|
ushort procNum = 0;
|
|
numericalControl.NC_RProcessesNum(ref procNum);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
// Max process number
|
|
genericData.ProcessNumber = procNum;
|
|
// Get Machine model from config
|
|
genericData.MachineModel = MachineConfig.Model;
|
|
// Get Installation Date
|
|
genericData.InstallationDate = DateTime.Now;
|
|
// Get PLC version
|
|
genericData.PlcVersion = "1.0.0";
|
|
// Get PLC version
|
|
genericData.UnitOfMeasurement = "mm";
|
|
// Get Server version
|
|
genericData.ServerVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
//Get Library Version
|
|
genericData.CoreLibraryVersion = "2.0.0";
|
|
|
|
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 strings
|
|
foreach (AlarmModel ncAlarm in tmpAlarms)
|
|
{
|
|
alarms.NcAlarms.Add(new GenericAlarmModel()
|
|
{
|
|
id = ncAlarm.Id,
|
|
message = ncAlarm.Message,
|
|
isWarning = ncAlarm.IsWarning,
|
|
dateTime = DateTime.Now
|
|
});
|
|
}
|
|
|
|
// 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 strings
|
|
foreach (AlarmModel processAlarm in tmpAlarms)
|
|
{
|
|
alarms.ProcessAlarms.Add(new ProcessAlarmModel()
|
|
{
|
|
id = processAlarm.Id,
|
|
message = processAlarm.Message,
|
|
process = processAlarm.Process,
|
|
isWarning = processAlarm.IsWarning,
|
|
dateTime = DateTime.Now
|
|
});
|
|
}
|
|
}
|
|
|
|
// Read PLC Active Messages
|
|
cmsError = numericalControl.PLC_RActiveMessages(ref tmpAlarms);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
// Formatting response list from library alarm model
|
|
foreach (AlarmModel plcAlarm in tmpAlarms)
|
|
{
|
|
alarms.PlcAlarms.Add(new GenericAlarmModel()
|
|
{
|
|
id = plcAlarm.Id,
|
|
message = plcAlarm.Message,
|
|
isWarning = plcAlarm.IsWarning,
|
|
dateTime = DateTime.Now
|
|
});
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
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 GetAxesPositionsByProcess(ushort processNum, out DTOAxesModel axes)
|
|
{
|
|
axes = new DTOAxesModel();
|
|
|
|
CmsError cmsError = numericalControl.AXES_RInterpPosition(processNum, ref axes.interpolated);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
numericalControl.AXES_RMachinePosition(processNum, ref axes.machine);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
numericalControl.AXES_RProgrPosition(processNum, ref axes.programmePos);
|
|
if (cmsError.errorCode != 0)
|
|
return 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 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 DTOProcessDataModel processesData)
|
|
{
|
|
processesData = new DTOProcessDataModel();
|
|
|
|
// Get NC max process number
|
|
ushort maxProcNumber = 0;
|
|
CmsError cmsError = numericalControl.NC_RProcessesNum(ref maxProcNumber);
|
|
if (cmsError.errorCode != 0)
|
|
return cmsError;
|
|
|
|
// 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(tmpProcPP);
|
|
|
|
// Check if there are running process
|
|
if (!processesData.isRunning)
|
|
{
|
|
PROC_STATUS status = PROC_STATUS.IDLE;
|
|
// Get process status
|
|
cmsError = numericalControl.PROC_RStatus(i, ref status);
|
|
if (status == PROC_STATUS.RUN)
|
|
processesData.isRunning = true;
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError PutPowerOnData(uint id)
|
|
{
|
|
// Set to true power on data by id
|
|
CmsError cmsError = numericalControl.PLC_WPowerOnData(id, true);
|
|
|
|
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 GetExpiredMaintenances(out List<DTOExpiredMaintenanceModel> expiredMaintenance)
|
|
{
|
|
// Return value
|
|
expiredMaintenance = new List<DTOExpiredMaintenanceModel>();
|
|
|
|
// List of PLC counters
|
|
List<CounterModel> counters = new List<CounterModel>();
|
|
// Populate counters
|
|
CmsError cmsError = numericalControl.PLC_RMachineCounters(ref counters);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Get the last performed maintenance for each maintenance
|
|
List<PerformedMaintenanceModel> performedMaintenance = maintenancesController.FindLastMaintenance();
|
|
|
|
// 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 = performedMaintenance.Find(x => x.MaintenanceId == currMaintenance.MaintenanceId);
|
|
|
|
switch (currMaintenance.Type)
|
|
{
|
|
case MAINT_MACHINE_INTERVAL_TYPE:
|
|
{
|
|
// 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 (currMaintenance.Interval <= 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
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
case MAINT_EXPIRATION_DATE_TYPE:
|
|
{
|
|
// Already performed
|
|
if (performed != null)
|
|
break;
|
|
// DEADLINE <= NOW
|
|
if (currMaintenance.Deadline <= DateTime.Now)
|
|
// Add item to return list
|
|
expiredMaintenance.Add(new DTOExpiredMaintenanceModel()
|
|
{
|
|
Id = currMaintenance.MaintenanceId,
|
|
ExpirationDate = currMaintenance.Deadline
|
|
});
|
|
}
|
|
break;
|
|
case MAINT_TIME_INTERVAL_TYPE:
|
|
{
|
|
// 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(currMaintenance.Interval <= minutesPassedFromLastMaint)
|
|
{
|
|
// Update last expiration date
|
|
if (currMaintenance.LastExpirationDate == null)
|
|
{
|
|
// Exp date = now - ( expiration intervall)
|
|
DateTime expirationDate = DateTime.Now.Subtract(TimeSpan.FromMinutes(minutesPassedFromLastMaint - currMaintenance.Interval));
|
|
|
|
maintenancesController.UpdateLastExpirationDate(currMaintenance.MaintenanceId, expirationDate);
|
|
currMaintenance.LastExpirationDate = expirationDate;
|
|
}
|
|
|
|
// Add item to return list
|
|
expiredMaintenance.Add(new DTOExpiredMaintenanceModel()
|
|
{
|
|
Id = currMaintenance.MaintenanceId,
|
|
ExpirationDate = (DateTime)currMaintenance.LastExpirationDate
|
|
});
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
public CmsError RefreshAllAlarms()
|
|
{
|
|
return new CmsError(0, "");
|
|
}
|
|
|
|
public CmsError RefreshAlarm(string id)
|
|
{
|
|
return new CmsError(0, "");
|
|
}
|
|
}
|
|
} |