Files
cms-core-active/CMS_CORE_Library/Demo/Nc_Demo.cs
T
Nicola Carminati afdbdbcbcd
2017-12-05 16:42:39 +00:00

907 lines
31 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using CMS_CORE.Demo.Models;
using CMS_CORE.Exceptions;
using CMS_CORE_Library.Demo.Models;
using Nc_Demo_Application.Server.Service;
using CMS_CORE.Utils;
namespace CMS_CORE.Demo
{
public class Nc_Demo : Nc
{
private EndpointAddress serverAddress;
private ChannelFactory<ILibraryService> cf;
private ILibraryService serverService;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region Contructor & global methods
public Nc_Demo(String IpAddress, ushort RemotePort)
{
Ip = IpAddress;
Port = RemotePort;
}
public override void 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();
Connected = true;
}
public override void NC_Disconnect()
{
cf.Close();
Connected = false;
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region High level methods
public override void NC_RDateTime(ref DateTime ActualTime)
{
//Check if the NC Demo is Connected
CheckConnection();
try
{
// Get datetime from Demo server
string demoDateTime = "";
serverService.GetDateTime(out demoDateTime);
ActualTime = Convert.ToDateTime(demoDateTime);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void NC_RSerialNumber(ref String SN)
{
//Check if the NC Demo is Connected
CheckConnection();
try
{
// Get serial number from Demo server
serverService.GetSerialNumber(out SN);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
//Get the NC model Name
public override void NC_RModelName(ref string ModelName)
{
//Check if the NC Demo is Connected
CheckConnection();
try
{
serverService.GetModel(out ModelName);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void NC_RSoftwareVersion(ref String SWV)
{
//Check if the NC Demo is Connected
CheckConnection();
try
{
// Get software version from Demo server
serverService.GetSoftwareVersion(out SWV);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void NC_RMachineNumber(ref string MachNumber)
{
//Check if the NC Demo is Connected
CheckConnection();
try
{
// Get machine number from Demo server
serverService.GetMachineNumber(out MachNumber);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void NC_RProcessesNum(ref ushort ProcNumber)
{
//Check if the NC Demo is Connected
CheckConnection();
try
{ // Get Process Cout from Demo server
serverService.GetProcessNumber(out ProcNumber);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void NC_RLanguage(ref CultureInfo Language)
{
//Check if the NC Demo is Connected
CheckConnection();
try
{
// Get language from Demo server
string demoLanguage = "";
serverService.GetLanguage(out demoLanguage);
Language = ConverToSTEPLanguage(demoLanguage);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void NC_RActiveAlarms(ref List<string> Alarms)
{
Alarms.Clear();
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PLC High-level data
public override void PLC_RActiveMessages(ref List<String> Alarms)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
List<NcAlarmModel> demoAlarms;
// Get Alarms from server
serverService.GetProcessesAlarms(out demoAlarms);
// Parse response
foreach (NcAlarmModel demoAlarm in demoAlarms)
{
Alarms.Add(demoAlarm.text);
}
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PROCESS (PATH) High-level data
public override void PROC_RStatus(ushort ProcNumber, ref PROC_Status Status)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
// Get status from server
serverService.GetProcessStatus(ProcNumber.ToString(), out int demoStatus);
Status = (PROC_Status)demoStatus;
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void PROC_RMode(ushort ProcNumber, ref PROC_Mode Mode)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
// Get mode from server
serverService.GetProcessMode(ProcNumber.ToString(), out int demoMode);
Mode = (PROC_Mode)demoMode;
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void PROC_RActiveAlarms(ushort ProcNumber, ref List<string> Alarms)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
serverService.GetProcessAlarms(ProcNumber.ToString(), out List<NcAlarmModel> demoAlarms);
foreach (NcAlarmModel demoAlarm in demoAlarms)
{
Alarms.Add(demoAlarm.text);
}
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void PROC_RPPLines(ushort ProcNumber, ref List<string> Lines)
{
ThrowNCException(new Nc_Exception(getError(FUNC_NOTALL_NC_ERROR)));
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PROCESS-AXES (PATH) High-level data
public override void AXES_RInterpPosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void AXES_RProgrPosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
// Get Axes Position from server
serverService.GetAxesPosition(ProcNumber.ToString(), out List<NcAxisModel> demoAxes);
// Parse server response and get programmed position
foreach (NcAxisModel demoAxis in demoAxes)
{
Axes.Add(demoAxis.name, demoAxis.programmed);
}
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void AXES_RMachinePosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
// Get Axes Position from server
serverService.GetAxesPosition(ProcNumber.ToString(), out List<NcAxisModel> demoAxes);
// Parse Server response and get machine position
foreach (NcAxisModel demoAxis in demoAxes)
{
Axes.Add(demoAxis.name, demoAxis.machinePosition);
}
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void AXES_RFollowingError(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
// Get Axes position from Demo server
serverService.GetAxesPosition(ProcNumber.ToString(), out List<NcAxisModel> demoAxes);
// Parse response get distance to go
foreach (NcAxisModel demoAxis in demoAxes)
{
Axes.Add(demoAxis.name, demoAxis.distanceToGo);
}
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void AXES_RDistanceToGo(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
serverService.GetAxesPosition(ProcNumber.ToString(), out List<NcAxisModel> demoAxes);
// Parse response get distance to go
foreach (NcAxisModel demoAxis in demoAxes)
{
Axes.Add(demoAxis.name, demoAxis.distanceToGo);
}
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region NC Low-level function: single valiable in memory
public override void MEM_RWBoolean(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, int MemBit, ref bool Value)
{
// Check if the NC Demo is Connected
CheckConnection();
CheckBitRange(MemBit);
try
{
// Read case: Read bit
if (bWrite == R)
{
serverService.GetBoolean(MemIndex.ToString(), MemBit.ToString(), out Value);
}
// Write case: write bit
else
{
}
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void MEM_RWByte(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, int MemByte, ref byte Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWWord(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, ref ushort Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWShort(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, ref short Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWDWord(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, ref uint Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWInteger(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, ref int Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
//--------------------------------------------------------------------------------------------------------------------------
// Siemens Version of Memory-Access Methods
public override void MEM_RWBoolean(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, int MemBit, ref bool Value)
{
MEM_RWBoolean(bWrite, Process, MemType, MemIndex, MemBit, ref Value);
}
public override void MEM_RWByte(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, int MemByte, ref byte Value)
{
MEM_RWByte(bWrite, Process, MemType, MemIndex, MemByte, ref Value);
}
public override void MEM_RWWord(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, ref ushort Value)
{
MEM_RWWord(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override void MEM_RWShort(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, ref short Value)
{
MEM_RWShort(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override void MEM_RWDWord(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, ref uint Value)
{
MEM_RWDWord(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override void MEM_RWInteger(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, ref int Value)
{
MEM_RWInteger(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override void MEM_RWByteList(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, int MemByteStart, int Number, ref List<byte> Value)
{
MEM_RWByteList(bWrite, Process, MemType, MemIndex, MemByteStart, Number, ref Value);
}
public override void MEM_RWWordList(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, int Number, ref List<ushort> Value)
{
MEM_RWWordList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
public override void MEM_RWShortList(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, int Number, ref List<short> Value)
{
MEM_RWShortList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
public override void MEM_RWDWordList(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, int Number, ref List<uint> Value)
{
MEM_RWDWordList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
public override void MEM_RWIntegerList(bool bWrite, int Process, MEMORY_Type MemType, int MemTable, int MemIndex, int Number, ref List<int> Value)
{
MEM_RWIntegerList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region NC Low-level function: variables List in memory
public override void 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
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWWordList(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, int Number, ref List<ushort> Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWShortList(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, int Number, ref List<short> Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWIntegerList(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, int Number, ref List<int> Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
public override void MEM_RWDWordList(bool bWrite, int Process, MEMORY_Type MemType, int MemIndex, int Number, ref List<uint> Value)
{
// Check if the NC Demo is Connected
CheckConnection();
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)
{
ThrowNCException(ex);
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region NC Low-level function: Parameters
public override void NC_RParam(short Index, short Bit, ref bool Value)
{
throw new Nc_Exception(getError(FUNC_NOTALL_NC_ERROR));
}
public override void NC_RParam(short Index, ref byte Value)
{
throw new Nc_Exception(getError(FUNC_NOTALL_NC_ERROR));
}
public override void NC_RParam(short Index, ref short Value)
{
throw new Nc_Exception(getError(FUNC_NOTALL_NC_ERROR));
}
public override void NC_RParam(short Index, ref int Value)
{
throw new Nc_Exception(getError(FUNC_NOTALL_NC_ERROR));
}
public override void NC_RParam(short Index, ref double Value)
{
throw new Nc_Exception(getError(FUNC_NOTALL_NC_ERROR));
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region File Management
public override void FILES_RProgramToFile(string partProgramPath, FileStream localFile)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
serverService.GetFile(partProgramPath, out string fileContent);
Nc_Utils.WriteLocalFile(fileContent, ref localFile);
}
catch(Exception ex)
{
ThrowNCException(ex);
}
}
public override void FILES_WProgramFromFile(string partProgramPath, FileStream localFile)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
FileModel fileData = new FileModel
{
fileContent = Nc_Utils.ReadLocalFile(localFile),
destFileName = partProgramPath
};
serverService.PostFile(fileData);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void FILES_CopyProgram(string partProgramPath, string newPartProgramPath, bool failIfExist)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
FileModel fileData = new FileModel
{
sourceFileName = partProgramPath,
destFileName = newPartProgramPath,
failIfExist = failIfExist
};
serverService.CopyFile(fileData);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
public override void FILES_DeleteProgram(string partProgramPath, string partProgramName)
{
// Check if the NC Demo is Connected
CheckConnection();
try
{
serverService.DeleteFile(partProgramName);
}
catch (Exception ex)
{
ThrowNCException(ex);
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region Subordinate Private Functions
//Check if NC is connected
private void CheckConnection()
{
if (!NC_IsConnected())
throw new Nc_Exception(getError(NOT_CONNECTED_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 void ThrowNCException(Exception ex)
{
if (!(ex is Nc_Exception))
Connected = false;
//Catch the .Net exceptions
if (ex is EndpointNotFoundException)
throw new Nc_Exception("NC not found: " + ex.Message);
else
throw new Nc_Exception("NC Comunication Error: " + ex.Message);
}
// Convert the internal error in a readable-String error
private String getError(uint CMSError)
{
String ErrororOwner = "";
if (CMSError != 0)
{
ErrororOwner = "CMS-Core-Error: ";
switch (CMSError)
{
case NOT_CONNECTED_ERROR: return ErrororOwner + "Nc not Connected";
case PROC_NOT_FOUND_ERROR: return ErrororOwner + "Process Id not found";
case FUNC_NOTALL_NC_ERROR: return ErrororOwner + "Function not allowed for this type of NC";
case BIT_NOT_IN_RANGE_ERROR: return ErrororOwner + "Bit-number must be between 0 and 7";
case INTERNAL_ERROR: return ErrororOwner + "Internal function error";
case INCORRECT_PARAMETERS_ERROR: return ErrororOwner + "Incorrect Parameters error";
}
}
return ErrororOwner + "Generic Error On Function";
}
//Check Bit In Range
private void CheckBitRange(int bitnum)
{
if (bitnum < 0 || bitnum > 7)
throw new Nc_Exception(getError(BIT_NOT_IN_RANGE_ERROR));
}
public override void PROC_RPPName(ushort ProcNumber, ref string Name)
{
throw new NotImplementedException();
}
#endregion
}
}