Files
Emmanuele Sassi 6e4645875b - Primo commit
- Tpa funzionante
2021-09-29 11:35:42 +02:00

334 lines
13 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
/// <summary>
/// Notify operative state change
/// </summary>
/// <param name="newOpState">New operative state</param>
void RemoteObject_OpStateUpdate(ISOCNC.Remoting.MachineOperatingState newOpState)
{
_opState = newOpState;
_MainWindowVM.SelOPState = _opState;
if (_MainWindowVM.SelOPState == ISOCNC.Remoting.MachineOperatingState.Pending)
remObject.RemoveAllProgramsFromList();
}
/// <summary>
/// Notify the execution of a command server side
/// </summary>
/// <param name="executedCommand">Command executed index</param>
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;
}
/// <summary>
/// Notify an error occured during the execution of a command server side
/// </summary>
/// <param name="sender"></param>
/// <param name="seEA">Error</param>
void RemoteObject_ServerError(object sender, ISOCNC.Remoting.ServerErrorEventArgs seEA)
{
MessageBoxResult dr = MessageBox.Show("Server Error: " + seEA.Error);
}
/// <summary>
/// Notify the update of an axis coordinate
/// </summary>
/// <param name="axisValue">New axis value</param>
/// <param name="axisIndex">Axis index</param>
void RemoteObject_AxisCoordinatesUpdate(double axisValue, int axisIndex)
{
_axesVal[axisIndex] = axisValue;
_MainWindowVM.AxisList[axisIndex].Value = axisValue;
_MainWindowVM.AxisList[axisIndex].NotifyPropertyChanged("Value");
}
/// <summary>
/// Notify the actual ISO line processed
/// </summary>
/// <param name="newRPC">Actual ISO Line</param>
void RemoteObject_RPCUpdate(uint newRPC)
{
_rpc = (int)newRPC;
}
/// <summary>
/// Receive program list information
/// </summary>
/// <param name="prgCount">Program count</param>
/// <param name="prgAtIndex">Program at index </param>
/// <param name="prgList">Program List</param>
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;
}
}
/// <summary>
/// Notify the adding or removal of an alarm
/// </summary>
/// <param name="alarmOperation">Operation: Add-Remove</param>
/// <param name="alarmType">Type: Cycle, Message, System, ISO</param>
/// <param name="alarmMessage">Alarm</param>
/// <param name="alarmCode">Alarm code</param>
/// <param name="alarmDateTime">Data</param>
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;
}
}
/// <summary>
/// Notify info on the executed command
/// </summary>
/// <param name="executedCommand">Indice di comando eseguito</param>
/// <param name="commandExecutedCorrectly">True se il comando è stato eseguito correttamente</param>
/// <param name="varName">Nome variabile ovvero percorso in Albatros della variabile</param>
/// <param name="varValue">Valore variabile</param>
/// <param name="varType">Tipologia variabile</param>
void RemoteObject_VariableCommandExecuted(int executedCommand, bool commandExecutedCorrectly, string varName, string varValue, int varType)
{
_MainWindowVM.SetVarValue(varValue);
}
/// <summary>
/// Notify the server alive
/// </summary>
/// <param name="newTick">Tick corrente per verifica server alive</param>
void RemoteObject_TickUpdate(ulong newTick)
{
}
#endregion Event management
#endregion Remoting obj events
}