Imports System.Collections.ObjectModel Imports System.Threading Imports System.Windows.Threading Imports EgtWPFLib5 Public Class MachCommandMessagePanelVM Inherits VMBase Public Enum MachineOperatingState Start = 1 [Stop] = 2 [End] = 3 SetPoint = 4 Pending = 5 Unspecified = 100 End Enum Private m_AlarmTimer As New DispatcherTimer Private m_MachManagingThread As Thread Public ReadOnly Property MachManagingThread As Thread Get Return m_MachManagingThread End Get End Property 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_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 "UNSPECIFIED" ' 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_cmdDeleteAlarms As ICommand Private m_cmdSetPoint As ICommand 'Private m_cmdPrintLabel As ICommand Public Sub New() Map.SetRefMachCommandMessagePanelVM(Me) ' impostazioni timer degli allarmi m_AlarmTimer.Interval = TimeSpan.FromMilliseconds(1500) AddHandler m_AlarmTimer.Tick, AddressOf AlarmTimer_Tick 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 - 1 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 - 1 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 - 1 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 - 1 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) ' creo thread gestione macchina m_MachManagingThread = New Thread(Sub() MachineCommThread.MachManagingThreadFunction(AddressOf ResultCallbackDlg, AddressOf CloseCallbackDlg, AddressOf UpdateCallbackDlg, AddressOf AlarmCallbackDlg, AddressOf AxisCoordinatesCallbackDlg, AddressOf OpStateCallbackDlg, AddressOf ReadVarCallbackDlg) End Sub) ' avvio thread di gestione della macchina che avvia la connessione m_MachManagingThread.Start() End Sub Private Sub Init() ' inizializzo valori assi leggendoli da variabili MachManaging.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, ASSE_X)) MachManaging.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, ASSE_Y)) MachManaging.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, ASSE_Z)) MachManaging.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, ASSE_B)) MachManaging.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.READ, ASSE_C)) 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.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.DISCONNECT)) End Sub Public Sub SetOPState(OpState As Integer) MachManaging.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.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.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.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.RESET)) ' annullo stato di pronto ad essere lavorato di tutti i programmi ' If Not IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM) Then 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.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.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.SETPOINT)) End Sub 'Public ReadOnly Property PrintLabel_Command As ICommand ' Get ' If m_cmdPrintLabel Is Nothing Then m_cmdPrintLabel = New Command(AddressOf PrintLabel) ' Return m_cmdPrintLabel ' End Get 'End Property 'Public Sub PrintLabel() ' 'DbControllers.m_LogMachineController.Create(MachLog.CreateResultLog(LogCommandTypes.ALARM, CommandStates.ERROR_, ResultTypes.EXECUTED, "")) ' 'DbControllers.m_LogMachineController.Create(MachLog.CreateAlarmLog(5, 7, "Allaaaaaaaarmeeeeeeeeee!!", 7645, DateTime.Now())) ' 'DbControllers.m_LogMachineController.Create(MachLog.CreateOPStateLog(85)) ' 'DbControllers.m_LogMachineController.Create(MachLog.CreateReadLog(True, "E80048", "46")) ' 'LabelPrinter.SetTestPrintVariables(2, 34, 76) ' 'LabelPrinter.Print() '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.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) Select Case CommandType Case CommandTypes.CONNECT If CommandState = CommandStates.OK Then _Connected = True NotifyPropertyChanged(NameOf(Connect_Background)) Init() End If Case CommandTypes.DISCONNECT If CommandState = CommandStates.OK Then _Connected = False NotifyPropertyChanged(NameOf(Connect_Background)) End If End Select 'DbControllers.m_LogMachineController.Create(MachLog.CreateResultLog(CommandType, CommandState, ResultType, Params)) 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) ' se aggiungo allarme e non ce ne erano If AlarmOperation = CInt(ISOCNC.Remoting.AlarmOperation.Addition) AndAlso m_ErrCycle.Count = 0 AndAlso m_Iso.Count = 0 AndAlso m_Message.Count = 0 AndAlso m_ErrSystem.Count = 0 Then ' avvio il timer degli allarmi m_AlarmTimer.Start() End If ' analizzo errore 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 m_ErrCycle.RemoveAll(Function(x) x.sCode = AlarmCode) '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 m_Iso.RemoveAll(Function(x) x.sCode = AlarmCode) '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 m_Message.RemoveAll(Function(x) x.sCode = AlarmCode) '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 m_ErrSystem.RemoveAll(Function(x) x.sCode = AlarmCode) '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 ' forzo aggiornamento allarmi AlarmTimer_Tick() ' 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() End If 'DbControllers.m_LogMachineController.Create(MachLog.CreateAlarmLog(AlarmOperation, AlarmType, AlarmMessage, AlarmCode, AlarmDateTime)) End Sub Friend Sub AxisCoordinatesCallbackDlg(AxisValue As Double, AxisIndex As Integer) Map.refAxesPanelVM.AxisCoordinatesCallbackDlg(AxisValue, AxisIndex) End Sub Friend Sub OpStateCallbackDlg(newOpState As MachineOperatingState) If newOpState <> MachineOperatingState.Unspecified Then Map.refMainWindowVM.SetOPState(newOpState) End If ' Map.refLeftPanelVM.NotifyPropertyChanged(NameOf(Map.refLeftPanelVM.sOPState)) 'DbControllers.m_LogMachineController.Create(MachLog.CreateOPStateLog(newOpState)) 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(MachLog.CreateReadLog(CommandExecutedCorrectly, VarAddress, VarValue)) End Sub Private Sub SetAxisValue(VarName As String, VarValue As String) Dim Axis As Axis = Map.refAxesPanelVM.AxesList.FirstOrDefault(Function(x) x.sName = VarName) If Not IsNothing(Axis) Then Axis.SetValue(VarValue) End If End Sub #End Region ' CALLBACKS 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