using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
class TPAComm
{
#region Fields
//VM
private MainWindowVM _MainWindowVM;
//Axes values
private double[] _axesVal = new double[24];
//Active command
private int _cmdActive = 0;
//Current machine operative state
private ISOCNC.Remoting.MachineOperatingState _opState = ISOCNC.Remoting.MachineOperatingState.Unspecified;
#region Remoting - Client
// Instance of the remote server object.
private ISOCNC.Remoting_Server _remObject;
public ISOCNC.Remoting_Server remObject
{
get => _remObject;
}
// Server IPC URI address
private string serverURI = "ipc://localhost:9090/IRemoteObject.rem";
//Remoting events proxy manager object
ISOCNC.Remoting.EventProxyManager eventProxy;
#endregion Remoting - Client
//RPC
private int _rpc;
//List Info
private int _prgCount;
private string _prgAtIndex;
private string[] _prgList;
private bool _prgListUpdated;
//Alarms
private string _errCycle;
private string _iso;
private string _message;
private string _errSystem;
#endregion Fields
#region Constructor
[SecurityPermission(SecurityAction.Demand)]
public TPAComm(MainWindowVM MainWindowVM)
{
//imposto VM
_MainWindowVM = MainWindowVM;
//Set up for remoting events properly
System.Collections.Hashtable properties = new System.Collections.Hashtable();
properties["name"] = "remotingClient";
properties["priority"] = "20";
properties["portName"] = "67";
BinaryClientFormatterSinkProvider clientProv = new
BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider serverProv = new
BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
//// Create the IPC channel.
IpcChannel channel = new IpcChannel(properties, clientProv, serverProv);
// Register the channel.
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel, false);
// Register as client for remote object.
System.Runtime.Remoting.WellKnownClientTypeEntry remoteType =
new System.Runtime.Remoting.WellKnownClientTypeEntry(typeof(ISOCNC.Remoting_Server), serverURI);
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownClientType(remoteType);
// Create a message sink.
string objectUri;
System.Runtime.Remoting.Messaging.IMessageSink messageSink =
channel.CreateMessageSink("ipc://localhost:9090/IRemoteObject.rem", null, out objectUri);
Console.WriteLine("The URI of the message sink is {0}.", objectUri);
if (messageSink != null)
{
Console.WriteLine("The type of the message sink is {0}.",
messageSink.GetType().ToString());
}
//Event Proxy management
eventProxy = new ISOCNC.Remoting.EventProxyManager();
eventProxy.CommandExecuted += new
ISOCNC.Remoting.CommandExecutedEventHandler(RemoteObject_CommandExecuted);
eventProxy.ServerError += new
ISOCNC.Remoting.ServerErrorEventHandler(RemoteObject_ServerError);
eventProxy.AxisCoordinatesUpdate += new
ISOCNC.Remoting.AxesCoordinatesUpdateEventHandler(RemoteObject_AxisCoordinatesUpdate);
eventProxy.OpStateUpdate += new
ISOCNC.Remoting.OpStateUpdateEventHandler(RemoteObject_OpStateUpdate);
eventProxy.AlarmNotification += new
ISOCNC.Remoting.AlarmNotificationEventHandler(RemoteObject_AlarmNotification);
eventProxy.ListInfoResponse += new
ISOCNC.Remoting.ListInfoEventHandler(RemoteObject_ListInfoResponse);
eventProxy.RPCUpdate += new
ISOCNC.Remoting.RPCUpdateEventHandler(RemoteObject_RPCUpdate);
eventProxy.VariableCommandExecuted += new
ISOCNC.Remoting.VariableCommandExecutedEventHandler(RemoteObject_VariableCommandExecuted);
eventProxy.TickUpdate += new
ISOCNC.Remoting.TickUpdateEventHandler(RemoteObject_TickUpdate);
//Connection to Remote Server instance
_remObject = (ISOCNC.Remoting_Server)Activator.GetObject(typeof(ISOCNC.Remoting_Server), serverURI);
try
{
//Event management
_remObject.CommandExecuted += new
ISOCNC.Remoting.CommandExecutedEventHandler(eventProxy.LocallyHandleCommandExecuted);
_remObject.ServerError += new
ISOCNC.Remoting.ServerErrorEventHandler(eventProxy.LocallyHandleServerError);
_remObject.AxisCoordinatesUpdate += new
ISOCNC.Remoting.AxesCoordinatesUpdateEventHandler(eventProxy.LocallyHandleAxisCoordinatesUpdate);
_remObject.OpStateUpdate += new
ISOCNC.Remoting.OpStateUpdateEventHandler(eventProxy.LocallyHandleOpStateUpdate);
_remObject.AlarmNotification += new
ISOCNC.Remoting.AlarmNotificationEventHandler(eventProxy.LocallyHandleAlarmNotification);
_remObject.ListInfoResponse += new
ISOCNC.Remoting.ListInfoEventHandler(eventProxy.LocallyHandleListInfo);
_remObject.RPCUpdate += new
ISOCNC.Remoting.RPCUpdateEventHandler(eventProxy.LocallyHandleRPCUpdate);
_remObject.VariableCommandExecuted += new
ISOCNC.Remoting.VariableCommandExecutedEventHandler(eventProxy.LocallyHandleVariableCommandExecuted);
//_remObject.TickUpdate += new
//ISOCNC.Remoting.TickUpdateEventHandler(eventProxy.LocallyHandleTickUpdate)
}
catch (System.Runtime.Remoting.RemotingException ex)
{
MessageBoxResult dR = MessageBox.Show(ex.Message);
}
}
#endregion Constructor
#region Event management
#region Remoting obj events
///
/// Notify operative state change
///
/// New operative state
void RemoteObject_OpStateUpdate(ISOCNC.Remoting.MachineOperatingState newOpState)
{
_opState = newOpState;
_MainWindowVM.SelOPState = _opState;
if (_MainWindowVM.SelOPState == ISOCNC.Remoting.MachineOperatingState.Pending)
remObject.RemoveAllProgramsFromList();
}
///
/// Notify the execution of a command – server side
///
/// Command executed index
void RemoteObject_CommandExecuted(int executedCommand)
{
switch (executedCommand)
{
case (int)ISOCNC.Remoting.Commands.NoCommand:
break;
case (int)ISOCNC.Remoting.Commands.End:
break;
case (int)ISOCNC.Remoting.Commands.Error:
break;
case (int)ISOCNC.Remoting.Commands.MDI_End:
break;
case (int)ISOCNC.Remoting.Commands.MDI_Start:
break;
case (int)ISOCNC.Remoting.Commands.MDI_Stop:
break;
case (int)ISOCNC.Remoting.Commands.SetPoint:
break;
case (int)ISOCNC.Remoting.Commands.Start:
break;
case (int)ISOCNC.Remoting.Commands.Step:
break;
case (int)ISOCNC.Remoting.Commands.Stop:
break;
case (int)ISOCNC.Remoting.Commands.Start_Program_Soft:
break;
default:
break;
}
//Update internal active executed command field
_cmdActive = (int)executedCommand;
}
///
/// Notify an error occured during the execution of a command – server side
///
///
/// Error
void RemoteObject_ServerError(object sender, ISOCNC.Remoting.ServerErrorEventArgs seEA)
{
MessageBoxResult dr = MessageBox.Show("Server Error: " + seEA.Error);
}
///
/// Notify the update of an axis coordinate
///
/// New axis value
/// Axis index
void RemoteObject_AxisCoordinatesUpdate(double axisValue, int axisIndex)
{
_axesVal[axisIndex] = axisValue;
_MainWindowVM.AxisList[axisIndex].Value = axisValue;
_MainWindowVM.AxisList[axisIndex].NotifyPropertyChanged("Value");
}
///
/// Notify the actual ISO line processed
///
/// Actual ISO Line
void RemoteObject_RPCUpdate(uint newRPC)
{
_rpc = (int)newRPC;
}
///
/// Receive program list information
///
/// Program count
/// Program at index
/// Program List
void RemoteObject_ListInfoResponse(int prgCount, string prgAtIndex, string[] prgList)
{
// Program count
if (prgCount >= 0)
{
_prgCount = prgCount;
}
//Program at index
if (prgAtIndex != "")
{
_prgAtIndex = prgAtIndex;
}
//Program List
if (prgList != null && prgList.Length > 0)
{
_prgList = prgList;
_prgListUpdated = true;
}
}
///
/// Notify the adding or removal of an alarm
///
/// Operation: Add-Remove
/// Type: Cycle, Message, System, ISO
/// Alarm
/// Alarm code
/// Data
void RemoteObject_AlarmNotification(int alarmOperation, int alarmType, string alarmMessage, string alarmCode, string alarmDateTime)
{
switch (alarmType)
{
case (int)ISOCNC.Remoting.AlarmType.Cycle:
if (alarmOperation == (int)ISOCNC.Remoting.AlarmOperation.Addition)
_errCycle = alarmCode + ": " + alarmMessage;
else
{
//Removal
if (_errCycle != null && _errCycle != "")
{
if (_errCycle.Split(':')[0] == alarmCode)
_errCycle = "";
}
}
_MainWindowVM.UpdateErrCycle(_errCycle);
break;
case (int)ISOCNC.Remoting.AlarmType.ISO:
if (alarmOperation == (int)ISOCNC.Remoting.AlarmOperation.Addition)
_iso = alarmCode + ": " + alarmMessage;
else
{
// Removal
if (_iso != null && _iso != "")
{
if (_iso.Split(':')[0] == alarmCode)
_iso = "";
}
}
_MainWindowVM.UpdateIso(_iso);
break;
case (int)ISOCNC.Remoting.AlarmType.Message:
if (alarmOperation == (int)ISOCNC.Remoting.AlarmOperation.Addition)
_message = alarmCode + ": " + alarmMessage;
else
{
// Removal
if (_message != null && _message != "")
{
if (_message.Split(':')[0] == alarmCode)
_message = "";
}
}
_MainWindowVM.UpdateMessage(_message);
break;
case (int)ISOCNC.Remoting.AlarmType.None:
break;
case (int)ISOCNC.Remoting.AlarmType.System:
if (alarmOperation == (int)ISOCNC.Remoting.AlarmOperation.Addition)
_errSystem = alarmCode + ": " + alarmMessage;
else
{
// Removal
if (_errSystem != null && _errSystem != "")
{
if (_errSystem.Split(':')[0] == alarmCode)
_errSystem = "";
}
}
_MainWindowVM.UpdateErrSystem(_errSystem);
break;
default:
break;
}
}
///
/// Notify info on the executed command
///
/// Indice di comando eseguito
/// True se il comando è stato eseguito correttamente
/// Nome variabile ovvero percorso in Albatros della variabile
/// Valore variabile
/// Tipologia variabile
void RemoteObject_VariableCommandExecuted(int executedCommand, bool commandExecutedCorrectly, string varName, string varValue, int varType)
{
_MainWindowVM.SetVarValue(varValue);
}
///
/// Notify the server alive
///
/// Tick corrente per verifica server alive
void RemoteObject_TickUpdate(ulong newTick)
{
}
#endregion Event management
#endregion Remoting obj events
}