2621 lines
93 KiB
C#
2621 lines
93 KiB
C#
using CMS_CORE.Demo.Models;
|
|
using CMS_CORE.Utils;
|
|
using CMS_CORE_Library.Demo;
|
|
using CMS_CORE_Library.Demo.Models;
|
|
using Nc_Demo_Application.Server.Service;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.ServiceModel;
|
|
using System.ServiceModel.Description;
|
|
using System.Threading;
|
|
using static CMS_CORE.Demo.MEMORY_ADDRESS;
|
|
using static CMS_CORE.Nc;
|
|
using static CMS_CORE_Library.DataStructures;
|
|
using static CMS_CORE_Library.ToolConfigurations;
|
|
|
|
namespace CMS_CORE.Demo
|
|
{
|
|
public class Nc_Demo : Nc
|
|
{
|
|
private EndpointAddress serverAddress;
|
|
private ChannelFactory<ILibraryService> cf;
|
|
private ILibraryService serverService;
|
|
private DemoEdgesConfiguration DemoEdgesConfiguration = new DemoEdgesConfiguration();
|
|
|
|
// Magazine config parameters
|
|
List<MagazineConfigModel> MagazineConfig = new List<MagazineConfigModel>()
|
|
{
|
|
new MagazineConfigModel{Id = 1, Type = MAGAZINE_TYPE.CHAIN, LoadingIsActive = true, Name = "MAG1"},
|
|
new MagazineConfigModel{Id = 2, Type = MAGAZINE_TYPE.BOX_MAGAZINE, LoadingIsActive = true, Name = "MAG2"},
|
|
new MagazineConfigModel{Id = 3, Type = MAGAZINE_TYPE.BOX_MAGAZINE, LoadingIsActive = false, Name = "MAG3"},
|
|
};
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region Contructor & global methods
|
|
|
|
public Nc_Demo(String IpAddress, ushort RemotePort)
|
|
{
|
|
Ip = IpAddress;
|
|
Port = RemotePort;
|
|
}
|
|
|
|
public override CmsError NC_Connect()
|
|
{
|
|
// Create new connection to the demo server
|
|
String url = "http://" + Ip + ":" + Port + "/api";
|
|
serverAddress = new EndpointAddress(url);
|
|
|
|
cf = new ChannelFactory<ILibraryService>(new WebHttpBinding(), serverAddress);
|
|
cf.Endpoint.EndpointBehaviors.Add(new WebHttpBehavior());
|
|
|
|
serverService = cf.CreateChannel();
|
|
string serialNumber = "";
|
|
|
|
try
|
|
{
|
|
// Get serial number from Demo server
|
|
serverService.GetSerialNumber(out serialNumber);
|
|
Connected = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_Disconnect()
|
|
{
|
|
if (cf != null)
|
|
cf.Close();
|
|
|
|
Connected = false;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_GetTranslatedPlcMessages(string language, ref Dictionary<int, string> messages)
|
|
{
|
|
try
|
|
{
|
|
messages = new Dictionary<int, string>();
|
|
serverService.GetTranslations(language, out messages);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
#endregion Contructor & global methods
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region High level methods
|
|
|
|
public override CmsError NC_RDateTime(ref DateTime ActualTime)
|
|
{
|
|
//Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get datetime from Demo server
|
|
string demoDateTime = "";
|
|
serverService.GetDateTime(out demoDateTime);
|
|
|
|
ActualTime = Convert.ToDateTime(demoDateTime);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RSerialNumber(ref String SN)
|
|
{
|
|
//Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get serial number from Demo server
|
|
serverService.GetSerialNumber(out SN);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
//Get the NC model Name
|
|
public override CmsError NC_RModelName(ref string ModelName)
|
|
{
|
|
//Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.GetModel(out ModelName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RSoftwareVersion(ref String SWV)
|
|
{
|
|
//Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get software version from Demo server
|
|
serverService.GetSoftwareVersion(out SWV);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RMachineNumber(ref string MachNumber)
|
|
{
|
|
//Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get machine number from Demo server
|
|
serverService.GetMachineNumber(out MachNumber);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RProcessesNum(ref ushort ProcNumber)
|
|
{
|
|
//Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{ // Get Process Cout from Demo server
|
|
serverService.GetProcessNumber(out ProcNumber);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RLanguage(ref CultureInfo Language)
|
|
{
|
|
//Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get language from Demo server
|
|
string demoLanguage = "";
|
|
serverService.GetLanguage(out demoLanguage);
|
|
|
|
Language = ConverToSTEPLanguage(demoLanguage);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RActiveAlarms(ref List<AlarmModel> alarms)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
alarms.Clear();
|
|
|
|
List<NcAlarmModel> demoAlarms;
|
|
// Get Alarms from server
|
|
serverService.NcAlarms(out demoAlarms);
|
|
// Parse response
|
|
foreach (NcAlarmModel demoAlarm in demoAlarms)
|
|
{
|
|
AddNcAlarmToList((uint)demoAlarm.code, demoAlarm.text, DateTime.Now, alarms);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
// Check if the NC is in running state
|
|
public override CmsError NC_RIsRunning(ref bool running)
|
|
{
|
|
ushort nProcess = 0;
|
|
// Get the number of processes
|
|
CmsError cmsError = NC_RProcessesNum(ref nProcess);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
ushort i = 1;
|
|
running = false;
|
|
do
|
|
{
|
|
PROC_STATUS procStatus = PROC_STATUS.IDLE;
|
|
|
|
// Read process status
|
|
cmsError = PROC_RStatus(i, ref procStatus);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
// If one process is runninc, nc is running
|
|
if (procStatus == PROC_STATUS.RUN)
|
|
running = true;
|
|
i++;
|
|
} while (!running || i <= nProcess);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
#endregion High level methods
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region PLC High-level data
|
|
|
|
public override CmsError PLC_RActiveMessages(ref List<PlcAlarmModel> alarms)
|
|
{
|
|
alarms.Clear();
|
|
List<int> readValues = new List<int>();
|
|
|
|
// Read on data from memory
|
|
CmsError cmsError = MEM_RWIntegerList(R, 0,
|
|
ALARMS_STATUS.MemType,
|
|
ALARMS_STATUS.Address,
|
|
(ALARMS_STATUS.Size + ALARMS_DATA.Size) / 4,
|
|
ref readValues);
|
|
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
// Convert integer array into bit array
|
|
bool[] statusBits = new BitArray(readValues.ToArray()).Cast<bool>().ToArray();
|
|
|
|
for (int i = 0; i < ALARMS_STATUS.Size * 8; i++)
|
|
{
|
|
// If alarm is active
|
|
if (statusBits[i])
|
|
{
|
|
// Calculate matching alarm byte info address
|
|
int dataByteAddress = i * 8 + (ALARMS_STATUS.Size * 8);
|
|
|
|
var processList = new List<int>();
|
|
|
|
// Get processes on which alarm is active
|
|
for (int j = dataByteAddress; j < dataByteAddress + 6; j++)
|
|
{
|
|
if (statusBits[j])
|
|
processList.Add(j + 1 - dataByteAddress);
|
|
}
|
|
// Add alarm info into active alarms list
|
|
alarms.Add(new PlcAlarmModel()
|
|
{
|
|
Id = (uint)i + 1,
|
|
IsWarning = statusBits[dataByteAddress + 7],
|
|
RestorationIsActive = statusBits[dataByteAddress + 6],
|
|
Process = processList
|
|
});
|
|
}
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_WRefreshMessage(uint id)
|
|
{
|
|
// Check id range
|
|
if (id > ALARMS_NUMBER)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
CmsError cmsError;
|
|
bool readValue = false;
|
|
bool writeValue = true;
|
|
|
|
int ackByte = ALARM_ACK.Address + (((int)id - 1) / 8);
|
|
int strobeByte = ALARM_REFRESH_STROBE.Address + (((int)id - 1) / 8);
|
|
int alarmBitId = (((int)id - 1) % 8);
|
|
|
|
// Check ACK
|
|
cmsError = MEM_RWBoolean(R, 0, ALARM_ACK.MemType, ackByte, alarmBitId, ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// If PLC it's performing another request then return
|
|
if (readValue)
|
|
return NO_ERROR;
|
|
|
|
// Write data
|
|
cmsError = MEM_RWBoolean(W, 0, ALARM_REFRESH_STROBE.MemType, strobeByte, alarmBitId, ref writeValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Reset wait ack = 1 and reset the strobe
|
|
cmsError = ResetStrobe(alarmBitId, strobeByte, ackByte, ALARM_REFRESH_STROBE.MemType);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_WRestoreMessage(uint id)
|
|
{
|
|
// Call restore message
|
|
CmsError cmsError = PLC_WRefreshMessage(id);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_WRefreshAllMessages()
|
|
{
|
|
int numberOfInteger = (32 / 8) / 4;
|
|
List<uint> defaultValue = Enumerable.Repeat(uint.MaxValue, numberOfInteger).ToList();
|
|
|
|
CmsError cmsError = MEM_RWDWordList(W, 0, ALARM_REFRESH_STROBE.MemType, ALARM_REFRESH_STROBE.Address, numberOfInteger, ref defaultValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_RPowerOnData(ref PreAndPostPowerOnModel powerOnModel)
|
|
{
|
|
int readValue = 0;
|
|
// Read pre power on and post power on data from memory
|
|
CmsError cmsError = MEM_RWInteger(R, 0, PRE_POWER_ON.MemType, PRE_POWER_ON.Address, ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
bool[] bits = new bool[32];
|
|
|
|
// Convert int into to true/false array
|
|
bits = new BitArray(new int[] { readValue }).Cast<bool>().ToArray();
|
|
// Create adn set pre power on object
|
|
PrePowerOnModel prePowerOn = new PrePowerOnModel()
|
|
{
|
|
PowerOn = new PowerOnDataModel() { Id = 0, Active = bits[0], Clickable = bits[8] }, // id = N, bits = N, Clickable = N + 8 (Second bit)
|
|
AirPressure = new PowerOnDataModel() { Id = 1, Active = bits[1], Clickable = bits[9] },
|
|
ProtectionStatus = new PowerOnDataModel() { Id = 2, Active = bits[2], Clickable = bits[10] },
|
|
EmergencyButtons = new PowerOnDataModel() { Id = 3, Active = bits[3], Clickable = bits[11] },
|
|
SettingMode = new PowerOnDataModel() { Id = 4, Active = bits[4], Clickable = bits[12] },
|
|
StartingKey = new PowerOnDataModel() { Id = 5, Active = bits[5], Clickable = bits[13] }
|
|
};
|
|
// Create and set post power on object
|
|
PostPowerOnModel postPowerOn = new PostPowerOnModel()
|
|
{
|
|
AxisReset = new PowerOnDataModel() { Id = 8, Active = bits[16], Clickable = bits[24] }, // id = N - 8, bits = N, Clickable = N + 8 (Second bit)
|
|
WaterjetPump = new PowerOnDataModel() { Id = 9, Active = bits[17], Clickable = bits[25] }
|
|
};
|
|
|
|
powerOnModel = new PreAndPostPowerOnModel()
|
|
{
|
|
PostPowerOn = postPowerOn,
|
|
PrePowerOn = prePowerOn
|
|
};
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_WPowerOnData(uint id, bool value)
|
|
{
|
|
CmsError cmsError;
|
|
// Check bit range
|
|
if (id > 16)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
// Choose Pre-Power-On or Post-Power-Om
|
|
if (id < 8)
|
|
// Pre: memory bit = id
|
|
cmsError = MEM_RWBoolean(W, 0, PRE_POWER_ON_CLICKED.MemType, PRE_POWER_ON_CLICKED.Address, (int)id, ref value);
|
|
else
|
|
// Post: memory bit = id - 8
|
|
cmsError = MEM_RWBoolean(W, 0, POST_POWER_ON_CLICKED.MemType, POST_POWER_ON_CLICKED.Address, (int)id - 8, ref value);
|
|
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_RFunctionAccess(ref List<FunctionalityModel> functions)
|
|
{
|
|
List<int> readValues = new List<int>();
|
|
// Read functions access data from memory
|
|
CmsError cmsError = MEM_RWIntegerList(R, 0, FUNCTION_ACCESS.MemType, FUNCTION_ACCESS.Address, 4, ref readValues);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
functions = new List<FunctionalityModel>();
|
|
// Convert int into to true/false array
|
|
bool[] bits = new BitArray(readValues.ToArray()).Cast<bool>().ToArray();
|
|
// Convert array into structured data
|
|
for (int i = 0; i < bits.Count(); i++)
|
|
{
|
|
functions.Add(new FunctionalityModel()
|
|
{
|
|
Id = (uint)i,
|
|
IsActive = bits[i]
|
|
});
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_RAxesResetData(ref AxisResetDataModel axisResetData)
|
|
{
|
|
byte value = 0;
|
|
// Read byte from memory
|
|
CmsError cmsError = MEM_RWByte(R, 0, AXIS_RESET_PROCEDURE.MemType, AXIS_RESET_PROCEDURE.Address, 1, ref value);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Check last bit
|
|
if ((value & 128) == 128)
|
|
{
|
|
// Set to 0 last bit and 7 bit percentage and check if is higher than 100
|
|
byte percentage = value - 128 > 100 ? (byte)100 : (byte)(value - 128);
|
|
axisResetData = new AxisResetDataModel()
|
|
{
|
|
IsActive = true,
|
|
Percentage = percentage
|
|
};
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_RMachineCounters(ref List<CounterModel> counters)
|
|
{
|
|
counters = new List<CounterModel>();
|
|
List<int> readValues = new List<int>();
|
|
// Read on data from memory
|
|
CmsError cmsError = MEM_RWIntegerList(R, 0, COUNTERS.MemType, COUNTERS.Address, COUNTERS.Size / 4, ref readValues);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Foreach counters
|
|
for (int i = 0; i < readValues.Count; i++)
|
|
{
|
|
// Add counters value into returned value
|
|
counters.Add(new CounterModel()
|
|
{
|
|
Id = (uint)i,
|
|
Value = (uint)readValues[i]
|
|
});
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_RNcSoftKeys(ref List<SoftKeysModel> ncSoftKeys)
|
|
{
|
|
CmsError cmsError = PLC_RSoftKeys(NC_SOFTKEYS_VALUE, NC_SOFTKEYS_CLICKABLE, ref ncSoftKeys);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_RUserSoftKeys(ref List<SoftKeysModel> softKeys)
|
|
{
|
|
CmsError cmsError = PLC_RSoftKeys(USER_SOFTKEYS_VALUE, USER_SOFTKEYS_CLICKABLE, ref softKeys);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
private CmsError PLC_RSoftKeys(MEMORY_CELL softKeyStatusMemory, MEMORY_CELL softKeysClickableMemory, ref List<SoftKeysModel> softKeys)
|
|
{
|
|
softKeys = new List<SoftKeysModel>();
|
|
List<int> readValue = new List<int>();
|
|
|
|
int memorySizeToRead = (softKeyStatusMemory.Size + softKeysClickableMemory.Size);
|
|
// Offset between status and clicable data
|
|
int offset = softKeyStatusMemory.Size * 8;
|
|
|
|
// Read on data from memory
|
|
CmsError cmsError = MEM_RWIntegerList(R, 0,
|
|
softKeyStatusMemory.MemType,
|
|
softKeyStatusMemory.Address,
|
|
memorySizeToRead / 4,
|
|
ref readValue);
|
|
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
bool[] bits = new BitArray(readValue.ToArray()).Cast<bool>().ToArray();
|
|
|
|
// Convert array into structured data
|
|
for (ushort i = 0; i < bits.Count() / 2; i++)
|
|
{
|
|
softKeys.Add(new SoftKeysModel()
|
|
{
|
|
Id = (uint)i + 1,
|
|
Value = bits[i],
|
|
Active = bits[i + offset]
|
|
});
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_WNcSoftKey(uint id)
|
|
{
|
|
// Check id range
|
|
if (id > NC_SOFTKEYS_NUMBER)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
CmsError cmsError;
|
|
bool readValue = false;
|
|
bool writeValue = true;
|
|
|
|
int ackByte = NC_SOFT_KEYS_ACK.Address + (((int)id - 1) / 8);
|
|
int strobeByte = NC_SOFT_KEYS_CLICKED.Address + (((int)id - 1) / 8);
|
|
int softkeyBitId = (((int)id - 1) % 8);
|
|
|
|
// Check ACK
|
|
cmsError = MEM_RWBoolean(R, 0, NC_SOFT_KEYS_ACK.MemType, ackByte, softkeyBitId, ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// If PLC it's performing another request then return
|
|
if (readValue)
|
|
return NO_ERROR;
|
|
|
|
// Write data
|
|
cmsError = MEM_RWBoolean(W, 0, NC_SOFT_KEYS_CLICKED.MemType, strobeByte, softkeyBitId, ref writeValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Reset wait ack = 1 and reset the strobe
|
|
cmsError = ResetStrobe(softkeyBitId, strobeByte, ackByte, NC_SOFT_KEYS_CLICKED.MemType);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_WUserSoftKey(uint id)
|
|
{
|
|
// Check id range
|
|
if (id > USER_SOFTKEYS_NUMBER)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
CmsError cmsError;
|
|
bool readValue = false;
|
|
bool writeValue = true;
|
|
|
|
int ackByte = USER_SOFT_KEYS_ACK.Address + (((int)id - 1) / 8);
|
|
int strobeByte = USER_SOFT_KEYS_CLICKED.Address + (((int)id - 1) / 8);
|
|
int softkeyBitId = (((int)id - 1) % 8);
|
|
|
|
// Check ACK
|
|
cmsError = MEM_RWBoolean(R, 0, USER_SOFT_KEYS_ACK.MemType, ackByte, softkeyBitId, ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// If PLC it's performing another request then return
|
|
if (readValue)
|
|
return NO_ERROR;
|
|
|
|
// Write data
|
|
cmsError = MEM_RWBoolean(W, 0, USER_SOFT_KEYS_CLICKED.MemType, strobeByte, softkeyBitId, ref writeValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Reset wait ack = 1 and reset the strobe
|
|
cmsError = ResetStrobe(softkeyBitId, strobeByte, ackByte, USER_SOFT_KEYS_CLICKED.MemType);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_RHeadsData(List<HeadDataModel> heads, int number)
|
|
{
|
|
// Check parameters
|
|
if (number > 20)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
heads.Clear();
|
|
|
|
List<int> readInts = new List<int>();
|
|
List<byte> readValues = new List<byte>();
|
|
// Find the size of a single head
|
|
int headsByte = (HEADS_DATA.Size / HEADS_NUMBER);
|
|
double headsInts = Math.Ceiling((double)(headsByte / 4) + 1);
|
|
|
|
// Read data for N heads
|
|
CmsError cmsError = MEM_RWIntegerList(R, 0, HEADS_DATA.MemType, 0, HEADS_DATA.Address, (int)headsInts * number, ref readInts);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
readValues = readInts.SelectMany(BitConverter.GetBytes).ToList();
|
|
|
|
// Parse and format read data
|
|
for (int i = 0; i < number; i++)
|
|
{
|
|
// Head address = posizion * size of single head in memory
|
|
var headOffset = i * headsByte;
|
|
heads.Add(new HeadDataModel()
|
|
{
|
|
Id = (uint)i + 1,
|
|
Process = readValues[headOffset],
|
|
Override = readValues[headOffset + 1],
|
|
Load_Abrasive = BitConverter.ToUInt16(readValues.ToArray(), headOffset + 2), // Uint16 = 2 byte
|
|
ActualSpeed_Pressure = BitConverter.ToInt32(readValues.ToArray(), headOffset + 4), // Int32 = 4byte
|
|
MountedTool_Vacum = BitConverter.ToInt16(readValues.ToArray(), headOffset + 8), // Uint 16 = 2byte
|
|
IsActive = (readValues[headOffset + 10] & 1) != 0, // bit 0
|
|
IsSelected = (readValues[headOffset + 10] & 2) != 0, // bit 1
|
|
OverrideEditable = (readValues[headOffset + 10] & 4) != 0, // bit 2
|
|
AbrasiveIsActive = (readValues[headOffset + 10] & 8) != 0 // bit 3
|
|
});
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PLC_WHeadOverride(uint id, HEAD_OVERRIDE_SIGN sign)
|
|
{
|
|
if (id > HEADS_NUMBER)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
CmsError cmsError;
|
|
bool readValue = false;
|
|
bool writeValue = true;
|
|
|
|
MEMORY_CELL currentStrobeCell = sign == HEAD_OVERRIDE_SIGN.PLUS ? HEADS_STROBE_INCREMENT : HEADS_STROBE_DECREMENT;
|
|
|
|
int ackByte = HEADS_ACK.Address + (((int)id - 1) / 8);
|
|
int strobeByte = currentStrobeCell.Address + (((int)id - 1) / 8);
|
|
int headBit = (((int)id - 1) % 8);
|
|
|
|
// Check ACK
|
|
cmsError = MEM_RWBoolean(R, 0, HEADS_ACK.MemType, ackByte, headBit, ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// If PLC it's performing another request then return
|
|
if (readValue)
|
|
return NO_ERROR;
|
|
|
|
// Write strobe data
|
|
cmsError = MEM_RWBoolean(W, 0, currentStrobeCell.MemType, strobeByte, headBit, ref writeValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Reset wait ack = 1 and reset the strobe
|
|
cmsError = ResetStrobe(headBit, strobeByte, ackByte, currentStrobeCell.MemType);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
private CmsError ResetStrobe(int bitId, int strobeByte, int ackByte, MEMORY_TYPE memType)
|
|
{
|
|
int n = 5;
|
|
bool readValue = false;
|
|
bool writeValue = false;
|
|
CmsError cmsError;
|
|
do
|
|
{
|
|
// Check ACK
|
|
cmsError = MEM_RWBoolean(R, 0, memType, ackByte, bitId, ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// If true reset strobe
|
|
if (readValue == true)
|
|
{
|
|
// Reset strobe
|
|
cmsError = MEM_RWBoolean(W, 0, memType, strobeByte, bitId, ref writeValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
// Exit from cycle
|
|
n = 0;
|
|
}
|
|
else
|
|
{
|
|
// Decrement
|
|
n--;
|
|
// Wait befor next cycle
|
|
Thread.Sleep(20);
|
|
}
|
|
} while (n > 0);
|
|
|
|
return cmsError;
|
|
}
|
|
|
|
#endregion PLC High-level data
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region PROCESS (PATH) High-level data
|
|
|
|
public override CmsError PROC_RStatus(ushort ProcNumber, ref PROC_STATUS Status)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get status from server
|
|
serverService.GetProcessStatus(ProcNumber.ToString(), out int demoStatus);
|
|
|
|
Status = (PROC_STATUS)demoStatus;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PROC_RMode(ushort ProcNumber, ref PROC_MODE Mode)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get mode from server
|
|
serverService.GetProcessMode(ProcNumber.ToString(), out int demoMode);
|
|
|
|
Mode = (PROC_MODE)demoMode;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PROC_RActiveAlarms(ushort ProcNumber, ref List<AlarmModel> alarms)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
alarms.Clear();
|
|
serverService.GetProcessAlarms(ProcNumber.ToString(), out List<NcAlarmModel> demoAlarms);
|
|
|
|
// Parse response
|
|
foreach (NcAlarmModel demoAlarm in demoAlarms)
|
|
{
|
|
AddAlarmToList((uint)demoAlarm.code, demoAlarm.text, demoAlarm.processId, alarms);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PROC_RPPLines(ushort ProcNumber, ref List<string> Lines)
|
|
{
|
|
return FUNCTION_NOT_ALLOWED_ERROR;
|
|
}
|
|
|
|
// Get process part program data
|
|
public override CmsError PROC_RStatusAndData(ushort procNumber, ref ProcessDataModel processData)
|
|
{
|
|
processData.Id = procNumber;
|
|
// Read part program name
|
|
CmsError cmsError = PROC_RPPName(procNumber, ref processData.PartProgramName);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
ushort readValue = 0;
|
|
// Read processes status from memory
|
|
cmsError = MEM_RWWord(R, 0, PROCESS_STATUS.MemType, PROCESS_STATUS.Address + 2 * (procNumber - 1), ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
byte[] bytes = BitConverter.GetBytes(readValue);
|
|
|
|
// Convert Byte into true/false array
|
|
BitArray bits = new BitArray(new byte[] { bytes[0] }); //1st byte -> 0 : Proccess type (AUX-WORK), 1: Process in alarm status, 2: Run, 3: Hold, 4: Read, 5: PartProgramVisible
|
|
|
|
// Get part program info visibility
|
|
processData.Visible = bits[0];
|
|
processData.IsInAlarm = bits[1];
|
|
processData.IsSelected = bits[2];
|
|
// Populate response structure
|
|
processData.Type = bits[3] ? "AUX" : "WORK";
|
|
|
|
// Choose process status
|
|
if (bits[4])
|
|
processData.Status = "HOLD";
|
|
else
|
|
{
|
|
if (bits[5])
|
|
processData.Status = "RUN";
|
|
else
|
|
processData.Status = "READY";
|
|
}
|
|
|
|
// Get Reps from 2nd byte
|
|
processData.Reps = bytes[1];
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PROC_RSelectedProcess(ref ushort procNumber)
|
|
{
|
|
CmsError cmsError;
|
|
bool readValue = false;
|
|
|
|
for (int i = 0; i < PROCESS_NUMBER; i++)
|
|
{
|
|
int memoryAddress = SELECTED_PROCESS.Address + i * (2);
|
|
// Read memory from plc
|
|
cmsError = MEM_RWBoolean(R, 0, SELECTED_PROCESS.MemType, memoryAddress, SELECTED_PROCESS.SubAddress, ref readValue);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
if (readValue)
|
|
{
|
|
procNumber = (ushort)(i + 1);
|
|
return NO_ERROR;
|
|
}
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PROC_WSelectProcess(ushort procNumber)
|
|
{
|
|
CmsError cmsError;
|
|
byte processNum = (byte)procNumber;
|
|
|
|
// Check parameter
|
|
if (processNum > PROCESS_NUMBER)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
// Write process number
|
|
cmsError = MEM_RWByte(W, 0, SELECT_PROCESS.MemType, SELECT_PROCESS.Address, 1, ref processNum);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
#endregion PROCESS (PATH) High-level data
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region PROCESS-AXES (PATH) High-level data
|
|
|
|
public override CmsError AXES_RInterpPosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get axes position from Demo Server
|
|
serverService.GetAxesPosition(ProcNumber.ToString(), out List<NcAxisModel> demoAxes);
|
|
|
|
// Parse server response
|
|
foreach (NcAxisModel demoAxis in demoAxes)
|
|
{
|
|
Axes.Add(demoAxis.name, demoAxis.actual);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError AXES_RProgrPosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get Axes Position from server
|
|
serverService.GetAxesPosition(ProcNumber.ToString(), out List<NcAxisModel> demoAxes);
|
|
// Parse server response and retrieve programmed position
|
|
foreach (NcAxisModel demoAxis in demoAxes)
|
|
{
|
|
Axes.Add(demoAxis.name, demoAxis.programmed);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError AXES_RMachinePosition(ushort procNumber, ref Dictionary<string, double> Axes)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
try
|
|
{
|
|
// Get Axes Position from server
|
|
serverService.GetAxesPosition(procNumber.ToString(), out List<NcAxisModel> demoAxes);
|
|
|
|
// Parse Server response and retrieve machine position
|
|
foreach (NcAxisModel demoAxis in demoAxes)
|
|
{
|
|
Axes.Add(demoAxis.name, demoAxis.machinePosition);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError AXES_RFollowingError(ushort procNumber, ref Dictionary<string, double> Axes)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get Axes position from Demo server
|
|
serverService.GetAxesPosition(procNumber.ToString(), out List<NcAxisModel> demoAxes);
|
|
// Parse response retrieve following error
|
|
foreach (NcAxisModel demoAxis in demoAxes)
|
|
{
|
|
Axes.Add(demoAxis.name, demoAxis.distanceToGo);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError AXES_RDistanceToGo(ushort procNumber, ref Dictionary<string, double> Axes)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.GetAxesPosition(procNumber.ToString(), out List<NcAxisModel> demoAxes);
|
|
// Parse response and retrieve distance to go
|
|
foreach (NcAxisModel demoAxis in demoAxes)
|
|
{
|
|
Axes.Add(demoAxis.name, demoAxis.distanceToGo);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError AXES_RAxesNames(ushort process, ref List<AxisModel> axesData)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.GetAxesPosition(process.ToString(), out List<NcAxisModel> demoAxes);
|
|
int i = 1;
|
|
// Parse response get distance to go
|
|
foreach (NcAxisModel demoAxis in demoAxes)
|
|
{
|
|
axesData.Add(new AxisModel()
|
|
{
|
|
Id = i++,
|
|
Name = demoAxis.name
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError AXES_RSelectedAxis(ref byte axisId)
|
|
{
|
|
// Read byte from memory
|
|
CmsError cmsError = MEM_RWByte(R, 0, SELECTED_AXIS.MemType, SELECTED_AXIS.Address, 1, ref axisId);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError AXES_WSelectAxis(byte axisId)
|
|
{
|
|
// Write byte from memory
|
|
CmsError cmsError = MEM_RWByte(W, 0, SELECT_AXIS.MemType, SELECT_AXIS.Address, 1, ref axisId);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
#endregion PROCESS-AXES (PATH) High-level data
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region NC Low-level function: single valiable in memory
|
|
|
|
public override CmsError MEM_RWBoolean(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int MemBit, ref bool Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
cmsError = CheckBitRange(MemBit);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case: Read bit
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetBoolean(MemIndex.ToString(), MemBit.ToString(), out Value);
|
|
}
|
|
// Write case: write bit
|
|
else
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.boolean = Value;
|
|
serverService.PutBoolean(MemIndex.ToString(), MemBit.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWByte(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int MemByte, ref byte Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetByte(MemIndex.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.binary = Value;
|
|
serverService.PutByte(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref ushort Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetWord(MemIndex.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.uWord = Value;
|
|
serverService.PutWord(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWShort(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref short Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetShort(MemIndex.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.word = Value;
|
|
serverService.PutShort(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWDWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref uint Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetDWord(MemIndex.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.dWord = Value;
|
|
serverService.PutDWord(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWInteger(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref int Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetInteger(MemIndex.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.integer = Value;
|
|
serverService.PutInteger(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Siemens Version of Memory-Access Methods
|
|
|
|
public override CmsError MEM_RWBoolean(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemBit, ref bool Value)
|
|
{
|
|
return MEM_RWBoolean(bWrite, Process, MemType, MemIndex, MemBit, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWByte(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemByte, ref byte Value)
|
|
{
|
|
return MEM_RWByte(bWrite, Process, MemType, MemIndex, MemByte, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref ushort Value)
|
|
{
|
|
return MEM_RWWord(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWShort(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref short Value)
|
|
{
|
|
return MEM_RWShort(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWDWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref uint Value)
|
|
{
|
|
return MEM_RWDWord(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWInteger(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref int Value)
|
|
{
|
|
return MEM_RWInteger(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWByteList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemByteStart, int Number, ref List<byte> Value)
|
|
{
|
|
return MEM_RWByteList(bWrite, Process, MemType, MemIndex, MemByteStart, Number, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<ushort> Value)
|
|
{
|
|
return MEM_RWWordList(bWrite, Process, MemType, MemIndex, Number, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWShortList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<short> Value)
|
|
{
|
|
return MEM_RWShortList(bWrite, Process, MemType, MemIndex, Number, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWDWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<uint> Value)
|
|
{
|
|
return MEM_RWDWordList(bWrite, Process, MemType, MemIndex, Number, ref Value);
|
|
}
|
|
|
|
public override CmsError MEM_RWIntegerList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<int> Value)
|
|
{
|
|
return MEM_RWIntegerList(bWrite, Process, MemType, MemIndex, Number, ref Value);
|
|
}
|
|
|
|
#endregion NC Low-level function: single valiable in memory
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region NC Low-level function: variables List in memory
|
|
|
|
public override CmsError MEM_RWByteList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int MemByteStart, int Number, ref List<byte> Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetByteList(MemIndex.ToString(), Number.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.byteList = Value;
|
|
serverService.PutByteList(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<ushort> Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetWordList(MemIndex.ToString(), Number.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.wordList = Value;
|
|
serverService.PutWordList(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWShortList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<short> Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetShortList(MemIndex.ToString(), Number.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.shortList = Value;
|
|
serverService.PutShortList(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWIntegerList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<int> Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetIntegerList(MemIndex.ToString(), Number.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.integerList = Value;
|
|
serverService.PutIntegerList(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError MEM_RWDWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<uint> Value)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Read case
|
|
if (bWrite == R)
|
|
{
|
|
serverService.GetDWordList(MemIndex.ToString(), Number.ToString(), out Value);
|
|
}
|
|
else // Write case
|
|
{
|
|
BinaryMemoryModel binaryMemoryModel = new BinaryMemoryModel();
|
|
binaryMemoryModel.dWordList = Value;
|
|
serverService.PutDWordList(MemIndex.ToString(), binaryMemoryModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
#endregion NC Low-level function: variables List in memory
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region NC Low-level function: Parameters
|
|
|
|
public override CmsError NC_RParam(short Index, short Bit, ref bool Value)
|
|
{
|
|
return FUNCTION_NOT_ALLOWED_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RParam(short Index, ref byte Value)
|
|
{
|
|
return FUNCTION_NOT_ALLOWED_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RParam(short Index, ref short Value)
|
|
{
|
|
return FUNCTION_NOT_ALLOWED_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RParam(short Index, ref int Value)
|
|
{
|
|
return FUNCTION_NOT_ALLOWED_ERROR;
|
|
}
|
|
|
|
public override CmsError NC_RParam(short Index, ref double Value)
|
|
{
|
|
return FUNCTION_NOT_ALLOWED_ERROR;
|
|
}
|
|
|
|
#endregion NC Low-level function: Parameters
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region File Management
|
|
|
|
public override CmsError FILES_RProgramToFile(string partProgramPath, FileStream localFile)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.GetFile(partProgramPath, out string fileContent);
|
|
Nc_Utils.WriteLocalFile(fileContent, ref localFile);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError FILES_WProgramFromFile(string partProgramPath, FileStream localFile)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
FileModel fileData = new FileModel
|
|
{
|
|
fileContent = Nc_Utils.ReadLocalFile(localFile),
|
|
destFileName = partProgramPath
|
|
};
|
|
serverService.PostFile(fileData);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError FILES_CopyProgram(string partProgramPath, string newPartProgramPath, bool failIfExist)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
FileModel fileData = new FileModel
|
|
{
|
|
sourceFileName = partProgramPath,
|
|
destFileName = newPartProgramPath,
|
|
failIfExist = failIfExist
|
|
};
|
|
serverService.CopyFile(fileData);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError FILES_DeleteProgram(string partProgramPath, string partProgramName)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.DeleteFile(partProgramName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
#endregion File Management
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region Tools Management
|
|
|
|
public override CmsError TOOLS_RConfiguration(ref ToolTableConfiguration config)
|
|
{
|
|
try
|
|
{
|
|
config.ToolsConfiguration = SiemensToolFieldsConfig;
|
|
config.FamiliesConfiguration = SiemensFamilyFieldsConfig;
|
|
config.ShanksConfiguration = SiemensShankFieldsConfig;
|
|
config.MagazinePosConfiguration = SiemensMagazinePosFieldsConfig;
|
|
config.EdgesConfiguration = SiemensEdgesFieldsConfiguration;
|
|
|
|
// Get config from demo
|
|
serverService.GetToolManagerConfiguration(out DemoToolManagerConfig demoConf);
|
|
|
|
config.MaxTools = demoConf.MaxTools;
|
|
config.MaxEdgesPerTools = demoConf.MaxEdgePerTools;
|
|
config.MaxToolsPerFamily = demoConf.MaxMultitools;
|
|
config.MaxMultitools = demoConf.MaxMultitools;
|
|
config.MaxToolsPerMultitools = demoConf.MaxToolsPerMultitools;
|
|
// Options
|
|
config.MultitoolOptionActive = demoConf.MultitoolOption;
|
|
config.FamilyOptionActive = demoConf.FamilyOption;
|
|
config.MagPositionOptionActive = demoConf.MagPositionOption;
|
|
|
|
config.EdgesConfiguration.EdgesAdditionalParamsConfiguration = new Dictionary<int, List<string>>();
|
|
// Remove path and set only names
|
|
foreach (int key in DemoEdgesConfiguration.EdgesConfig.Keys)
|
|
{
|
|
List<string> tmpNames = new List<string>();
|
|
foreach (var conf in DemoEdgesConfiguration.EdgesConfig[key])
|
|
{
|
|
tmpNames.Add(conf.Name);
|
|
}
|
|
config.EdgesConfiguration.EdgesAdditionalParamsConfiguration.Add(key, tmpNames);
|
|
}
|
|
|
|
config.Magazines = MagazineConfig;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError TOOLS_RToolsData(ref List<SiemensToolModel> toolTable)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
toolTable = new List<SiemensToolModel>();
|
|
// Get tools table data
|
|
serverService.GetTools(out List<DemoToolsDataModel> demoToolsData);
|
|
|
|
DemoEdgesConfiguration toolsConfig = new DemoEdgesConfiguration();
|
|
|
|
serverService.GetMagazinePositionsWithTools(1.ToString(), out List<DemoMagPosDataModel> mag1);
|
|
|
|
// Parse and cast demo model
|
|
foreach (DemoToolsDataModel demoTool in demoToolsData)
|
|
{
|
|
int magazineId = 0;
|
|
int positionId = 0;
|
|
|
|
foreach(var tool in mag1)
|
|
{
|
|
if (tool.IsMultiTool)
|
|
{
|
|
if(tool.Shank != null)
|
|
if (tool.Shank.Id == demoTool.Id)
|
|
{
|
|
magazineId = tool.MagazineId;
|
|
positionId = tool.PositionId;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(tool.ChildTools != null)
|
|
if (tool.ChildTools.Id == demoTool.Id)
|
|
{
|
|
magazineId = tool.MagazineId;
|
|
positionId = tool.PositionId;
|
|
}
|
|
}
|
|
}
|
|
|
|
var toolData = new SiemensToolModel()
|
|
{
|
|
Id = demoTool.Id,
|
|
FamilyName = demoTool.FamilyName,
|
|
ChildId = demoTool.ChildId,
|
|
MagazinePositionType = demoTool.MagazinePositionType,
|
|
ToolType = demoTool.ToolType,
|
|
LeftSize = demoTool.LeftSize,
|
|
RightSize = demoTool.RightSize,
|
|
Rotation = (ROTATION)demoTool.Rotation,
|
|
Cooling1 = demoTool.Cooling1,
|
|
Cooling2 = demoTool.Cooling2,
|
|
MaxSpeed = demoTool.MaxSpeed,
|
|
MaxAcceleration = demoTool.MaxAcceleration,
|
|
IsEnabled = demoTool.IsEnabled,
|
|
IsActive = demoTool.IsActive,
|
|
InFixedPlace = demoTool.InFixedPlace,
|
|
IsInhibited = demoTool.IsInhibited,
|
|
IsMeasured = demoTool.IsMeasured,
|
|
InChangeTool = demoTool.InChangeTool,
|
|
IsInUse = demoTool.IsInUse,
|
|
PreAlarm = demoTool.PreAlarm,
|
|
MagazineId = magazineId,
|
|
PositionId = positionId,
|
|
MultitoolId = 0,
|
|
EdgesData = new List<EdgeModel>()
|
|
};
|
|
|
|
// Get tool type additional parameters
|
|
var toolTypeConfigs = toolsConfig.EdgesConfig[demoTool.ToolType];
|
|
|
|
//--- Read Edges
|
|
|
|
// For each demo edge
|
|
foreach (var demoEdge in demoTool.EdgesData)
|
|
{
|
|
// Create new edge model with static data
|
|
var tmpEdge = new EdgeModel()
|
|
{
|
|
Id = demoEdge.Id,
|
|
ResidualLife = demoEdge.ResidualLife,
|
|
NominalLife = demoEdge.NominalLife,
|
|
PreAlmLife = demoEdge.PreAlmLife,
|
|
EdgeAdditionalParams = new Dictionary<string, double>()
|
|
};
|
|
|
|
// Populate edges' additional params getting only configured fields for the selected tool type
|
|
foreach (var configItem in toolTypeConfigs)
|
|
{
|
|
tmpEdge.EdgeAdditionalParams.Add(
|
|
// Item name
|
|
configItem.Name,
|
|
// "param" + N; N is configured in the path
|
|
demoEdge.EdgeAdditionalParams["param" + configItem.Path]
|
|
);
|
|
}
|
|
// Add new edge to selected tool edges list
|
|
toolData.EdgesData.Add(tmpEdge);
|
|
}
|
|
|
|
// Add new tool to tool table
|
|
toolTable.Add(toolData);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WAddTool(ref SiemensToolModel tool)
|
|
{
|
|
CmsError cmsError = CheckToolData(tool);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.AddTool(ref tool);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WUpdateTool(ref SiemensToolModel tool)
|
|
{
|
|
CmsError cmsError = CheckToolData(tool);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
try
|
|
{
|
|
serverService.PutTool(ref tool);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WDeleteTool(int id)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.DeleteTool(id.ToString());
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
private CmsError CheckToolData(SiemensToolModel tool)
|
|
{
|
|
List<SiemensToolModel> tools = new List<SiemensToolModel>();
|
|
CmsError cmsError = TOOLS_RToolsData(ref tools);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
ToolTableConfiguration config = new ToolTableConfiguration();
|
|
cmsError = TOOLS_RConfiguration(ref config);
|
|
|
|
if (tools.Count >= config.MaxTools)
|
|
return MAX_TOOL_REACHED_ERROR;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
#endregion Tools Management
|
|
|
|
#region Edges
|
|
|
|
public override CmsError TOOLS_WAddEdge(int toolId, ref EdgeModel newEdge)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
ToolTableConfiguration config = new ToolTableConfiguration();
|
|
cmsError = TOOLS_RConfiguration(ref config);
|
|
|
|
try
|
|
{
|
|
// Get tools data in order to get tool type and its config
|
|
serverService.GetTools(out List<DemoToolsDataModel> demoToolsData);
|
|
|
|
// Find tool by id
|
|
var demoTool = demoToolsData
|
|
.Where(x => x.Id == toolId).FirstOrDefault();
|
|
|
|
if (demoTool == null)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
if (demoTool.EdgesData.Count >= config.MaxEdgesPerTools)
|
|
return MAX_EDGES_PER_TOOL_REACHED_ERROR;
|
|
|
|
// Get the configuration of the tool type
|
|
var toolTypeConfig = DemoEdgesConfiguration.EdgesConfig[demoTool.ToolType];
|
|
|
|
// Create tmp edge to be sended to the Demo
|
|
EdgeModel demoEdge = new EdgeModel
|
|
{
|
|
Id = newEdge.Id,
|
|
PreAlmLife = newEdge.PreAlmLife,
|
|
NominalLife = newEdge.NominalLife,
|
|
ResidualLife = newEdge.ResidualLife,
|
|
EdgeAdditionalParams = new Dictionary<string, double>(),
|
|
};
|
|
|
|
// Convert Siemens additional param names into Demo param names
|
|
foreach (var additionalParam in newEdge.EdgeAdditionalParams)
|
|
{
|
|
// Get path of the additional parameter from configuration
|
|
string pathNumber = toolTypeConfig
|
|
.Where(x => x.Name == additionalParam.Key)
|
|
.Select(x => x.Path).FirstOrDefault();
|
|
|
|
if (!string.IsNullOrEmpty(pathNumber))
|
|
{
|
|
// Add new Demo param to the temporary list
|
|
demoEdge.EdgeAdditionalParams.Add(
|
|
"param" + pathNumber,
|
|
additionalParam.Value
|
|
);
|
|
}
|
|
}
|
|
|
|
serverService.AddEdge(toolId.ToString(), ref demoEdge);
|
|
// Set id to the returned model
|
|
newEdge.Id = demoEdge.Id;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WUpdateEdge(int toolId, ref EdgeModel newEdge)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get tools data in order to get tool type and its config
|
|
serverService.GetTools(out List<DemoToolsDataModel> demoToolsData);
|
|
|
|
// Find tool by id
|
|
var demoTool = demoToolsData
|
|
.Where(x => x.Id == toolId).FirstOrDefault();
|
|
|
|
if (demoTool == null)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
// Get the configuration of the tool type
|
|
var toolTypeConfig = DemoEdgesConfiguration.EdgesConfig[demoTool.ToolType];
|
|
|
|
// Create tmp edge to be sended to the Demo
|
|
EdgeModel demoEdge = new EdgeModel
|
|
{
|
|
Id = newEdge.Id,
|
|
PreAlmLife = newEdge.PreAlmLife,
|
|
NominalLife = newEdge.NominalLife,
|
|
ResidualLife = newEdge.ResidualLife,
|
|
EdgeAdditionalParams = new Dictionary<string, double>(),
|
|
};
|
|
|
|
// Convert Siemens additional param names into Demo param names
|
|
foreach (var additionalParam in newEdge.EdgeAdditionalParams)
|
|
{
|
|
// Get path of the additional parameter from configuration
|
|
string pathNumber = toolTypeConfig
|
|
.Where(x => x.Name == additionalParam.Key)
|
|
.Select(x => x.Path).FirstOrDefault();
|
|
|
|
if (!string.IsNullOrEmpty(pathNumber))
|
|
{
|
|
// Add new Demo param to the temporary list
|
|
demoEdge.EdgeAdditionalParams.Add(
|
|
"param" + pathNumber,
|
|
additionalParam.Value
|
|
);
|
|
}
|
|
}
|
|
|
|
serverService.PutEdge(toolId.ToString(), ref demoEdge);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WDeleteEdge(int toolId, int edgeId)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.DeleteEdge(toolId.ToString(), edgeId.ToString());
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
#endregion Edges
|
|
|
|
#region ToolTable
|
|
|
|
public override CmsError TOOLS_RShanksData(ref List<ShankModel> shanks)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
shanks = new List<ShankModel>();
|
|
// Get tool table data
|
|
serverService.GetShanks(out shanks);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WAddShank(ref ShankModel shank)
|
|
{
|
|
List<ShankModel> shanks = new List<ShankModel>();
|
|
CmsError cmsError = TOOLS_RShanksData(ref shanks);
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Get configuration
|
|
ToolTableConfiguration config = new ToolTableConfiguration();
|
|
cmsError = TOOLS_RConfiguration(ref config);
|
|
|
|
if (shanks.Count >= config.MaxMultitools)
|
|
return MAX_MULTITOOL_REACHED_ERROR;
|
|
|
|
try
|
|
{
|
|
serverService.AddShank(ref shank);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WUpdateShank(ref ShankModel shank)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.PutShank(ref shank);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WDeleteShank(int id)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.DeleteShank(id.ToString());
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_RFamilyData(ref List<FamilyModel> families)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
families = new List<FamilyModel>();
|
|
try
|
|
{
|
|
// Get families data
|
|
serverService.GetFamilies(out List<DemoFamilyModel> demoFamilies);
|
|
// Populate output model
|
|
foreach (DemoFamilyModel family in demoFamilies)
|
|
{
|
|
families.Add(new FamilyModel()
|
|
{
|
|
Name = family.Name,
|
|
ChildTools = family.ChildTools.Select(x => new FamilyChildModel()
|
|
{
|
|
Id = x.Id,
|
|
ChildId = x.ChildId,
|
|
ToolType = x.Type
|
|
}).ToList()
|
|
});
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WAddFamily(ref FamilyModel family)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.AddFamily(ref family);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WUpdateFamilyData(string oldName, string newName)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
try
|
|
{
|
|
DemoFamilyModel demoFamilyData = new DemoFamilyModel()
|
|
{
|
|
Name = newName
|
|
};
|
|
|
|
// Get families data
|
|
serverService.PutFamily(oldName, ref demoFamilyData);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WDeleteFamily(string name)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.DeleteFamily(name);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_RMagazinePositions(ref List<PositionModel> positions)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
positions = new List<PositionModel>();
|
|
try
|
|
{
|
|
// Get position data from demo
|
|
serverService.GetPositions(out positions);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WUpdatePosition(PositionModel position)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Call demo update
|
|
serverService.PutPosition(ref position);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_RMagazineTools(int magazineId, ref List<MountedToolModel> magazinePos)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Get shanks data
|
|
List<ShankModel> shanks = new List<ShankModel>();
|
|
serverService.GetShanks(out shanks);
|
|
|
|
List<SiemensToolModel> tools = new List<SiemensToolModel>();
|
|
cmsError = TOOLS_RToolsData(ref tools);
|
|
|
|
try
|
|
{
|
|
magazinePos = new List<MountedToolModel>();
|
|
// Get tools equipped in the magazine
|
|
serverService.GetMagazinePositionsWithTools(magazineId.ToString(), out List<DemoMagPosDataModel> demoMagazinePositions);
|
|
|
|
foreach (DemoMagPosDataModel demoPos in demoMagazinePositions)
|
|
{
|
|
if (demoPos.IsMultiTool == true)
|
|
{
|
|
if (demoPos.Shank != null)
|
|
{
|
|
// Create multitool
|
|
MountedToolModel pos = new MountedToolModel()
|
|
{
|
|
PositionId = demoPos.PositionId,
|
|
MagazineId = magazineId,
|
|
ToolId = demoPos.Shank.Id,
|
|
ChildShank = shanks.Where(x => x.Id == demoPos.Shank.Id).First()
|
|
};
|
|
|
|
magazinePos.Add(pos);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (demoPos.ChildTools != null)
|
|
{
|
|
// Create tool
|
|
MountedToolModel pos = new MountedToolModel()
|
|
{
|
|
PositionId = demoPos.PositionId,
|
|
MagazineId = magazineId,
|
|
ToolId = demoPos.ChildTools.Id,
|
|
ChildTool = tools.Where(x => x.Id == demoPos.ChildTools.Id).First()
|
|
};
|
|
|
|
magazinePos.Add(pos);
|
|
}
|
|
}
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_RAvailableTools(ref List<ShankModel> multiTools, ref List<SiemensToolModel> tools)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.GetToolsAndMultiTools(out List<ShankModel> shanks, out List<SiemensToolModel> demoTools);
|
|
// Set multiTool Data
|
|
multiTools = shanks;
|
|
// Set tool data
|
|
tools = demoTools;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WLoadToolInMagazine(int magazineId, NewToolInMagazineModel newMagazineTool)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
// Get configuration
|
|
ToolTableConfiguration config = new ToolTableConfiguration();
|
|
cmsError = TOOLS_RConfiguration(ref config);
|
|
|
|
// Get mag config
|
|
MagazineConfigModel magConfig = config.Magazines.Where(x => x.Id == magazineId).FirstOrDefault();
|
|
if (magConfig == null || !magConfig.LoadingIsActive)
|
|
return INCORRECT_PARAMETERS_ERROR;
|
|
|
|
try
|
|
{
|
|
serverService.UpdateMagazine(magazineId.ToString(), new List<NewToolInMagazineModel> { newMagazineTool });
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WUnloadToolInMagazine(int magazineId, int positionId)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.UpdateMagazine(magazineId.ToString(), new List<NewToolInMagazineModel> {
|
|
new NewToolInMagazineModel
|
|
{
|
|
PositionId = positionId,
|
|
ToolId = 0
|
|
}
|
|
});
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
public override CmsError TOOLS_WLoadToolIntoShank(int shankId, int positionId, int toolId)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.UpdateToolShank(shankId.ToString(), positionId.ToString(), toolId.ToString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError TOOLS_WUnloadToolFromShank(int shankId, int positionId)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
serverService.UpdateToolShank(shankId.ToString(), positionId.ToString(), 0.ToString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError TOOLS_GetMagazinesStatus(ref MagazineActionModel magazineAction)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Call demo update
|
|
serverService.GetMagazinesAction(out magazineAction);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
}
|
|
|
|
#endregion ToolTable
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region Nc Tool Manager
|
|
public override CmsError TOOLS_ROffset(short offsetId, ref OffsetModel offset)
|
|
{
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError TOOLS_WOffset(short offsetId, OffsetModel offset)
|
|
{
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError TOOLS_WStartEdit()
|
|
{
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError TOOLS_WStopEdit()
|
|
{
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError TOOLS_WAddTool()
|
|
{
|
|
return NO_ERROR;
|
|
}
|
|
|
|
#endregion Nc Tool Manager
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region Subordinate Private Functions
|
|
|
|
//Check if NC is connected
|
|
private CmsError CheckConnection()
|
|
{
|
|
if (!NC_IsConnected())
|
|
return NOT_CONNECTED_ERROR;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
//Manage the Languages
|
|
private CultureInfo ConverToSTEPLanguage(String Lang)
|
|
{
|
|
CultureInfo Culture = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(X => X.EnglishName.ToLower() == Lang.ToLower());
|
|
|
|
if (Culture != null)
|
|
return Culture;
|
|
else
|
|
return new CultureInfo("en");
|
|
}
|
|
|
|
//Manage the Exception Launch
|
|
private CmsError ManageException(Exception ex)
|
|
{
|
|
//Catch the .Net exceptions
|
|
if (ex is EndpointNotFoundException)
|
|
{
|
|
Connected = false;
|
|
return NOT_CONNECTED_ERROR;
|
|
}
|
|
else
|
|
return CmsError.InternalError(ex.Message);
|
|
}
|
|
|
|
//Check Bit In Range
|
|
private CmsError CheckBitRange(int bitnum)
|
|
{
|
|
if (bitnum < 0 || bitnum > 7)
|
|
return BIT_NOT_IN_RANGE_ERROR;
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
public override CmsError PROC_RPPName(ushort ProcNumber, ref string Name)
|
|
{
|
|
// Check if the NC Demo is Connected
|
|
CmsError cmsError = CheckConnection();
|
|
if (cmsError.IsError())
|
|
return cmsError;
|
|
|
|
try
|
|
{
|
|
// Get status from server
|
|
serverService.GetProcessPartProgram(ProcNumber.ToString(), out Name);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ManageException(ex);
|
|
}
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
// Create and return CmsError object
|
|
private CmsError GetNcError(string message)
|
|
{
|
|
return CmsError.NcError(message);
|
|
}
|
|
|
|
#endregion Subordinate Private Functions
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#region Memory addresses
|
|
|
|
internal static class MEMORY_ADDRESS
|
|
{
|
|
internal const int STARTING_ADDRESS = 0;
|
|
|
|
internal static MEMORY_CELL FUNCTION_ACCESS = new MEMORY_CELL(MEMORY_TYPE.Demo, 13, 8);
|
|
|
|
internal static MEMORY_CELL PRE_POWER_ON = new MEMORY_CELL(MEMORY_TYPE.Demo, 21, 1);
|
|
internal static MEMORY_CELL PRE_POWER_ON_CLICKABLE = new MEMORY_CELL(MEMORY_TYPE.Demo, 22, 1);
|
|
|
|
internal static MEMORY_CELL POST_POWER_ON = new MEMORY_CELL(MEMORY_TYPE.Demo, 23, 1);
|
|
internal static MEMORY_CELL POST_POWER_ON_CLICKABLE = new MEMORY_CELL(MEMORY_TYPE.Demo, 24, 1);
|
|
|
|
internal static MEMORY_CELL PRE_POWER_ON_CLICKED = new MEMORY_CELL(MEMORY_TYPE.Demo, 25, 1);
|
|
internal static MEMORY_CELL POST_POWER_ON_CLICKED = new MEMORY_CELL(MEMORY_TYPE.Demo, 26, 1);
|
|
|
|
internal static MEMORY_CELL AXIS_RESET_PROCEDURE = new MEMORY_CELL(MEMORY_TYPE.Demo, 27, 7, 2);
|
|
|
|
internal static MEMORY_CELL SELECTED_PROCESS = new MEMORY_CELL(MEMORY_TYPE.Demo, 31, 2, 1);
|
|
internal static MEMORY_CELL SELECT_PROCESS = new MEMORY_CELL(MEMORY_TYPE.Demo, 30, 1);
|
|
internal static MEMORY_CELL PROCESS_STATUS = new MEMORY_CELL(MEMORY_TYPE.Demo, 31, 0, 12);
|
|
|
|
internal static MEMORY_CELL COUNTERS = new MEMORY_CELL(MEMORY_TYPE.Demo, 43, 0, 40);
|
|
|
|
internal static MEMORY_CELL SELECTED_AXIS = new MEMORY_CELL(MEMORY_TYPE.Demo, 83, 0, 1);
|
|
internal static MEMORY_CELL SELECT_AXIS = new MEMORY_CELL(MEMORY_TYPE.Demo, 84, 0, 1);
|
|
|
|
internal static MEMORY_CELL NC_SOFTKEYS_VALUE = new MEMORY_CELL(MEMORY_TYPE.Demo, 85, 0, 4);
|
|
internal static MEMORY_CELL NC_SOFTKEYS_CLICKABLE = new MEMORY_CELL(MEMORY_TYPE.Demo, 89, 0, 4);
|
|
internal static MEMORY_CELL NC_SOFT_KEYS_CLICKED = new MEMORY_CELL(MEMORY_TYPE.Demo, 93, 4);
|
|
internal static MEMORY_CELL NC_SOFT_KEYS_ACK = new MEMORY_CELL(MEMORY_TYPE.Demo, 97, 4);
|
|
|
|
internal static MEMORY_CELL USER_SOFTKEYS_VALUE = new MEMORY_CELL(MEMORY_TYPE.Demo, 101, 0, 16);
|
|
internal static MEMORY_CELL USER_SOFTKEYS_CLICKABLE = new MEMORY_CELL(MEMORY_TYPE.Demo, 117, 0, 16);
|
|
internal static MEMORY_CELL USER_SOFT_KEYS_CLICKED = new MEMORY_CELL(MEMORY_TYPE.Demo, 133, 16);
|
|
internal static MEMORY_CELL USER_SOFT_KEYS_ACK = new MEMORY_CELL(MEMORY_TYPE.Demo, 149, 16);
|
|
|
|
internal static MEMORY_CELL ALARMS_STATUS = new MEMORY_CELL(MEMORY_TYPE.Demo, 165, 4);
|
|
internal static MEMORY_CELL ALARMS_DATA = new MEMORY_CELL(MEMORY_TYPE.Demo, 169, 32);
|
|
|
|
internal static MEMORY_CELL ALARM_ACK = new MEMORY_CELL(MEMORY_TYPE.Demo, 201, 4);
|
|
internal static MEMORY_CELL ALARM_REFRESH_STROBE = new MEMORY_CELL(MEMORY_TYPE.Demo, 205, 4);
|
|
|
|
internal static MEMORY_CELL HEADS_DATA = new MEMORY_CELL(MEMORY_TYPE.Demo, 209, 220);
|
|
|
|
internal static MEMORY_CELL HEADS_ACK = new MEMORY_CELL(MEMORY_TYPE.Demo, 429, 4);
|
|
internal static MEMORY_CELL HEADS_STROBE_INCREMENT = new MEMORY_CELL(MEMORY_TYPE.Demo, 432, 4);
|
|
internal static MEMORY_CELL HEADS_STROBE_DECREMENT = new MEMORY_CELL(MEMORY_TYPE.Demo, 436, 4);
|
|
}
|
|
|
|
#endregion Memory addresses
|
|
} |