Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Threading.Tasks Imports System.Windows.Input Imports System.Collections.ObjectModel Imports System.Windows.Media Imports System.Windows Imports ISOCNC.Remoting Imports EgtUILib Imports System.Threading Imports System.Windows.Threading Class MachCommandMessagePanelVM Inherits VMBase Private m_AlarmTimer As New DispatcherTimer Private m_MachManagingThread As Thread Private _Connected As Boolean = False Public ReadOnly Property Connected As Boolean Get Return _Connected End Get End Property Public ReadOnly Property Connect_Background As SolidColorBrush Get Return (If(_Connected, Brushes.Green, Brushes.Red)) End Get End Property Private _ProgramPath As String Public ReadOnly Property ProgramPath As String Get Return _ProgramPath End Get End Property Private _ProgramIndex As Integer = 0 Public Property ProgramIndex As Integer Get Return _ProgramIndex End Get Set(ByVal value As Integer) _ProgramIndex = value End Set End Property Private _VarName As String Public Property VarName As String Get Return _VarName End Get Set(ByVal value As String) _VarName = value End Set End Property Private _VarValue As String = "" Public Property VarValue As String Get Return _VarValue End Get Set(ByVal value As String) _VarValue = value End Set End Property Public Sub SetVarValue(ByVal value As Object) _VarValue = CStr(value) NotifyPropertyChanged(NameOf(VarValue)) End Sub Private m_AxisList As ObservableCollection(Of Axis) Public ReadOnly Property AxisList As ObservableCollection(Of Axis) Get Return m_AxisList End Get End Property Private m_OPState As MachineOperatingState Public ReadOnly Property OPState As MachineOperatingState Get Return m_OPState End Get End Property Public ReadOnly Property sOPState As String Get Select Case m_OPState Case MachineOperatingState.Start Return "START" Case MachineOperatingState.Stop Return "STOP" Case MachineOperatingState.End Return "RESET" Case MachineOperatingState.SetPoint Return "SETPOINT" Case MachineOperatingState.Pending Return "PENDING" Case Else Return "" End Select End Get End Property Private m_ErrCycle As New List(Of Alarm) Private m_ErrCycleCount As Integer = 0 Public ReadOnly Property sErrCycle As String Get Dim ShownErr As Alarm = If(m_ErrCycleCount < m_ErrCycle.Count, m_ErrCycle(m_ErrCycleCount), Nothing) Return If(Not IsNothing(ShownErr), ShownErr.sCode & ": " & ShownErr.sMessage, "") End Get End Property Private m_Iso As New List(Of Alarm) Private m_IsoCount As Integer = 0 Public ReadOnly Property sIso As String Get Dim ShownErr As Alarm = If(m_IsoCount < m_Iso.Count, m_Iso(m_IsoCount), Nothing) Return If(Not IsNothing(ShownErr), ShownErr.sCode & ": " & ShownErr.sMessage, "") End Get End Property Private m_Message As New List(Of Alarm) Private m_MessageCount As Integer = 0 Public ReadOnly Property sMessage As String Get Dim ShownErr As Alarm = If(m_MessageCount < m_Message.Count, m_Message(m_MessageCount), Nothing) Return If(Not IsNothing(ShownErr), ShownErr.sCode & ": " & ShownErr.sMessage, "") End Get End Property Private m_ErrSystem As New List(Of Alarm) Private m_ErrSystemCount As Integer = 0 Public ReadOnly Property sErrSystem As String Get Dim ShownErr As Alarm = If(m_ErrSystemCount < m_ErrSystem.Count, m_ErrSystem(m_ErrSystemCount), Nothing) Return If(Not IsNothing(ShownErr), ShownErr.sCode & ": " & ShownErr.sMessage, "") End Get End Property Private m_cmdConnect As ICommand Private m_cmdDisconnect As ICommand Private m_cmdStart As ICommand Private m_cmdStop As ICommand Private m_cmdReset As ICommand Private m_cmdStep As ICommand Private m_cmdSearchFilePath As ICommand Private m_cmdAddProgram As ICommand Private m_cmdRemoveProgram As ICommand Private m_cmdReadVar As ICommand Private m_cmdWriteVar As ICommand Private m_cmdDeleteAlarms As ICommand Private m_cmdSetPoint As ICommand Public Sub New() Map.SetRefMachCommandMessagePanelVM(Me) ' creo thread gestione macchina m_MachManagingThread = New Thread(Sub() CommunicationThread.MachManagingThreadFunction(AddressOf ResultCallbackDlg, AddressOf CloseCallbackDlg, AddressOf UpdateCallbackDlg, AddressOf AlarmCallbackDlg, AddressOf AxisCoordinatesCallbackDlg, AddressOf OpStateCallbackDlg, AddressOf ReadVarCallbackDlg) End Sub) ' impostazioni timer degli allarmi m_AlarmTimer.Interval = New TimeSpan(1500) AddHandler m_AlarmTimer.Tick, AddressOf AlarmTimer_Tick ' impostazione assi (leggerli da conf macchina) m_AxisList = New ObservableCollection(Of Axis)({New Axis(ASSE_X), New Axis(ASSE_Y), New Axis(ASSE_Z), New Axis(ASSE_C), New Axis(ASSE_B)}) NotifyPropertyChanged(NameOf(Connect_Background)) End Sub Private Sub AlarmTimer_Tick() If m_ErrCycle.Count = 0 AndAlso m_Iso.Count = 0 AndAlso m_Message.Count = 0 AndAlso m_ErrSystem.Count = 0 Then NotifyPropertyChanged(NameOf(sErrCycle)) NotifyPropertyChanged(NameOf(sIso)) NotifyPropertyChanged(NameOf(sMessage)) NotifyPropertyChanged(NameOf(sErrSystem)) Return End If If m_ErrCycle.Count > 0 Then If m_ErrCycleCount < m_ErrCycle.Count Then m_ErrCycleCount += 1 Else m_ErrCycleCount = 0 End If End If NotifyPropertyChanged(NameOf(sErrCycle)) If m_Iso.Count > 0 Then If m_IsoCount < m_Iso.Count Then m_IsoCount += 1 Else m_IsoCount = 0 End If End If NotifyPropertyChanged(NameOf(sIso)) If m_Message.Count > 0 Then If m_MessageCount < m_Message.Count Then m_MessageCount += 1 Else m_MessageCount = 0 End If End If NotifyPropertyChanged(NameOf(sMessage)) If m_ErrSystem.Count > 0 Then If m_ErrSystemCount < m_ErrSystem.Count Then m_ErrSystemCount += 1 Else m_ErrSystemCount = 0 End If End If NotifyPropertyChanged(NameOf(sErrSystem)) End Sub Public ReadOnly Property Connect_Command As ICommand Get If m_cmdConnect Is Nothing Then m_cmdConnect = New Command(AddressOf Connect) Return m_cmdConnect End Get End Property Public Sub Connect(ByVal param As Object) ' avvio thread di gestione della macchina m_MachManagingThread.Start() MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.CONNECT)) NotifyPropertyChanged(NameOf(Connect_Background)) If _Connected Then Init() End Sub Private Sub Init() ' inizializzo tutte le variabili RWVariableManager.InitVar("AsseX", "0.TESTA.AsseX.X", CommVar.CommVarTypes.ONETIME) RWVariableManager.InitVar("AsseY", "0.TESTA.AsseY.Y", CommVar.CommVarTypes.ONETIME) RWVariableManager.InitVar("AsseZ", "0.TESTA.AsseZ.Z", CommVar.CommVarTypes.ONETIME) RWVariableManager.InitVar("AsseZ", "0.TESTA.AsseC.C", CommVar.CommVarTypes.ONETIME) RWVariableManager.InitVar("AsseZ", "0.TESTA.AsseB.B", CommVar.CommVarTypes.ONETIME) ' inizializzo valori assi leggendoli da variabili MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, "AsseX")) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, "AsseY")) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, "AsseZ")) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, "AsseC")) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, "AsseB")) End Sub Public ReadOnly Property Disconnect_Command As ICommand Get If m_cmdDisconnect Is Nothing Then m_cmdDisconnect = New Command(AddressOf Disconnect) Return m_cmdDisconnect End Get End Property Public Sub Disconnect() MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.DISCONNECT)) End Sub Public Sub SetOPState(OpState As Integer) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.SETOP)) End Sub Public ReadOnly Property Start_Command As ICommand Get If m_cmdStart Is Nothing Then m_cmdStart = New Command(AddressOf Start) Return m_cmdStart End Get End Property Public Sub Start(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.START)) End Sub Public ReadOnly Property Stop_Command As ICommand Get If m_cmdStop Is Nothing Then m_cmdStop = New Command(AddressOf [Stop]) Return m_cmdStop End Get End Property Public Sub [Stop](ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.STOP_)) End Sub Public ReadOnly Property Reset_Command As ICommand Get If m_cmdReset Is Nothing Then m_cmdReset = New Command(AddressOf Reset) Return m_cmdReset End Get End Property Public Sub Reset(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.RESET)) ' annullo stato di pronto ad essere lavorato di tutti i programmi Map.refProjectVM.SupervisorMachGroupPanelVM.ResetAllMachGroups() End Sub Public ReadOnly Property Step_Command As ICommand Get If m_cmdStep Is Nothing Then m_cmdStep = New Command(AddressOf [Step]) Return m_cmdStep End Get End Property Public Sub [Step](ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.STEP_)) End Sub Public ReadOnly Property SetPoint_Command As ICommand Get If m_cmdSetPoint Is Nothing Then m_cmdSetPoint = New Command(AddressOf SetPoint) Return m_cmdSetPoint End Get End Property Public Sub SetPoint(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.SETPOINT)) End Sub Public ReadOnly Property AddProgram_Command As ICommand Get If m_cmdAddProgram Is Nothing Then m_cmdAddProgram = New Command(AddressOf AddProgram) Return m_cmdAddProgram End Get End Property Public Sub AddProgram(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.SENDPROG)) End Sub Public ReadOnly Property RemoveProgram_Command As ICommand Get If m_cmdRemoveProgram Is Nothing Then m_cmdRemoveProgram = New Command(AddressOf RemoveProgram) Return m_cmdRemoveProgram End Get End Property Public Sub RemoveProgram(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.REMOVEPROG)) End Sub Public ReadOnly Property ReadVar_Command As ICommand Get If m_cmdReadVar Is Nothing Then m_cmdReadVar = New Command(AddressOf ReadVar) Return m_cmdReadVar End Get End Property Public Sub ReadVar(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ)) End Sub Public ReadOnly Property WriteVar_Command As ICommand Get If m_cmdWriteVar Is Nothing Then m_cmdWriteVar = New Command(AddressOf WriteVar) Return m_cmdWriteVar End Get End Property Public Sub WriteVar(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.WRITE)) End Sub Public ReadOnly Property DeleteAlarms_Command As ICommand Get If m_cmdDeleteAlarms Is Nothing Then m_cmdDeleteAlarms = New Command(AddressOf DeleteAlarms) Return m_cmdDeleteAlarms End Get End Property Public Sub DeleteAlarms(ByVal param As Object) MachManaging.m_CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.DELETEALARMS)) End Sub #Region "CALLBACKS" Friend Sub ResultCallbackDlg(CommandType As CommandTypes, CommandState As CommandStates, ResultType As ResultTypes, Params As String) ' DbControllers.m_LogMachineController.Create(m_SupervisorId,DateTime.Now(),nothing,Nothing,Nothing,) End Sub Friend Sub CloseCallbackDlg(ByRef bCancel As Boolean) ' serve? End Sub Friend Sub UpdateCallbackDlg(Param As String, Params As String) End Sub Friend Sub AlarmCallbackDlg(ByVal AlarmOperation As Integer, ByVal AlarmType As Integer, ByVal AlarmMessage As String, ByVal AlarmCode As String, ByVal AlarmDateTime As String) Select Case AlarmType Case CInt(ISOCNC.Remoting.AlarmType.Cycle) If AlarmOperation = CInt(ISOCNC.Remoting.AlarmOperation.Addition) Then m_ErrCycle.Add(Alarm.CreateAlarm(AlarmCode, AlarmMessage)) Else Dim ToRemove As Alarm = m_ErrCycle.FirstOrDefault(Function(x) x.sCode = AlarmCode) If Not IsNothing(ToRemove) Then m_ErrCycle.Remove(ToRemove) End If End If Case CInt(ISOCNC.Remoting.AlarmType.ISO) If AlarmOperation = CInt(ISOCNC.Remoting.AlarmOperation.Addition) Then m_Iso.Add(Alarm.CreateAlarm(AlarmCode, AlarmMessage)) Else Dim ToRemove As Alarm = m_Iso.FirstOrDefault(Function(x) x.sCode = AlarmCode) If Not IsNothing(ToRemove) Then m_Iso.Remove(ToRemove) End If End If Case CInt(ISOCNC.Remoting.AlarmType.Message) If AlarmOperation = CInt(ISOCNC.Remoting.AlarmOperation.Addition) Then m_Message.Add(Alarm.CreateAlarm(AlarmCode, AlarmMessage)) Else Dim ToRemove As Alarm = m_Message.FirstOrDefault(Function(x) x.sCode = AlarmCode) If Not IsNothing(ToRemove) Then m_Message.Remove(ToRemove) End If End If Case CInt(ISOCNC.Remoting.AlarmType.None) Case CInt(ISOCNC.Remoting.AlarmType.System) If AlarmOperation = CInt(ISOCNC.Remoting.AlarmOperation.Addition) Then m_ErrSystem.Add(Alarm.CreateAlarm(AlarmCode, AlarmMessage)) Else Dim ToRemove As Alarm = m_ErrSystem.FirstOrDefault(Function(x) x.sCode = AlarmCode) If Not IsNothing(ToRemove) Then m_ErrSystem.Remove(ToRemove) End If End If Case Else End Select ' se non ci sono errori If m_ErrCycle.Count = 0 AndAlso m_Iso.Count = 0 AndAlso m_Message.Count = 0 AndAlso m_ErrSystem.Count = 0 Then ' fermo timer degli allarmi m_AlarmTimer.Stop() Else ' altrimenti lo avvio m_AlarmTimer.Start() End If ' DbControllers.m_LogMachineController.Create(m_SupervisorId,DateTime.Now(),nothing,Nothing,Nothing,) End Sub Friend Sub AxisCoordinatesCallbackDlg(AxisValue As Double, AxisIndex As Integer) m_AxisList(AxisIndex).SetValue(AxisValue) End Sub Friend Sub OpStateCallbackDlg(newOpState As MachineOperatingState) m_OPState = newOpState NotifyPropertyChanged(NameOf(sOPState)) ' DbControllers.m_LogMachineController.Create(m_SupervisorId,DateTime.Now(),nothing,Nothing,Nothing,) End Sub Friend Sub ReadVarCallbackDlg(CommandExecutedCorrectly As Boolean, VarAddress As String, VarValue As String, VarType As Integer) Dim VarName As String = RWVariableManager.GetReadVarNameFromAddress(VarAddress) If Not String.IsNullOrEmpty(VarName) Then Select Case VarName Case "AsseX", "AsseY", "AsseZ", "AsseC", "AsseB" SetAxisValue(VarName, VarValue) End Select End If ' DbControllers.m_LogMachineController.Create(m_SupervisorId,DateTime.Now(),nothing,Nothing,Nothing,) End Sub Private Sub SetAxisValue(VarName As String, VarValue As String) Dim Axis As Axis = m_AxisList.FirstOrDefault(Function(x) x.sName = VarName) If Not IsNothing(Axis) Then Axis.SetValue(VarValue) End If End Sub #End Region ' CALLBACKS End Class Class Axis Inherits VMBase Private m_Name As String Public ReadOnly Property sName As String Get Return m_Name End Get End Property Private m_Value As Double Public ReadOnly Property sValue As Double Get Return m_Value End Get End Property Friend Sub SetValue(value As Double) m_Value = value NotifyPropertyChanged(NameOf(sValue)) End Sub Public Sub New(Name As String) m_Name = Name End Sub End Class Class Alarm Private m_sCode As String Public ReadOnly Property sCode As String Get Return m_sCode End Get End Property Private m_sMessage As String Public ReadOnly Property sMessage As String Get Return m_sMessage End Get End Property Protected Sub New() End Sub Public Shared Function CreateAlarm(Code As String, Message As String) As Alarm Dim NewAlarm As New Alarm NewAlarm.m_sCode = Code NewAlarm.m_sMessage = Message Return NewAlarm End Function End Class