Imports System.Collections.ObjectModel Imports System.Collections.Specialized Imports ISOCNC.Remoting Imports EgtUILib Imports EgtWPFLib5 Imports System.IO Imports EgtCOMMTest.MainWindowVM Public Class MachManaging Private m_bConnected As Boolean = False Public ReadOnly Property bConnected As Boolean Get Return m_bConnected End Get End Property Friend Sub SetConnected(value As Boolean) m_bConnected = value End Sub Private m_CN As Object = Nothing Public ReadOnly Property CN As Object Get Return m_CN End Get End Property Public ReadOnly Property Tpa As TPAComm Get Return m_CN End Get End Property Public ReadOnly Property Num_Flexium As NUMFlexiumComm Get Return m_CN End Get End Property Private Shared WithEvents m_CommandList As New ObservableCollection(Of ThreadCommand) Public Shared ReadOnly Property CommandList As ObservableCollection(Of ThreadCommand) Get Return m_CommandList End Get End Property Private m_bStartPending As Boolean = False Public ReadOnly Property StartPending As Boolean Get Return m_bStartPending End Get End Property Friend Sub ResetStartPending() m_bStartPending = False End Sub Friend Sub SetStartPending(value As Boolean) m_bStartPending = value End Sub ' prossima barra di cui mando i parametri di carico Private m_NextBarId As Integer Sub New() ' imposto in Map Map.SetRefMachManaging(Me) AddHandler m_CommandList.CollectionChanged, AddressOf CommandList_CollectionChanged ' mi connetto alla macchina Connect() End Sub Private Sub OnDispose() RemoveHandler m_CommandList.CollectionChanged, AddressOf CommandList_CollectionChanged End Sub Friend Sub Timer_Tick() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA 'Dim bCancel As Boolean = False 'callback(0, "Init", bCancel) ' leggo tutte le variabili CN.RWVariableManager.RefreshAllVars() ' eseguo ciclo principale Dim nReset_State As Integer Dim nP_Prod As Integer Dim nP_Machgroup As Integer Dim nP_Part As Integer Dim nP_State As Integer CN.RWVariableManager.ReadVar(P_PROD, nP_Prod) CN.RWVariableManager.ReadVar(P_MACHGROUP, nP_Machgroup) CN.RWVariableManager.ReadVar(P_PART, nP_Part) CN.RWVariableManager.ReadVar(P_STATE, nP_State) CN.RWVariableManager.ReadVar(RESET_STATE, nReset_State) '' se non ancora fatto, preparo variabili barra successiva 'If m_NextBarId = 0 Then ' SetNextBarVars() 'End If 'Dim TestOpState As Integer = Tpa.remObject.MachineOperativeStatus '' verifico se scattato stato reset 'If nReset_State <> 0 Then ' ' resetto tutti i programmi ' If Not IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM) Then Map.refProjectVM.SupervisorMachGroupPanelVM.ResetAllMachGroups() ' ' resetto variabili P ' Tpa.RWVariableManager.WriteVar(P_PROD, 0) ' Tpa.RWVariableManager.WriteVar(P_MACHGROUP, 0) ' Tpa.RWVariableManager.WriteVar(P_PART, 0) ' Tpa.RWVariableManager.WriteVar(P_STATE, 0) ' ' cancello tutti i programmi da memoria CN ' 'RemoveAllProgram() toglie programma default per pending ' ' azzero variabile reset ' Tpa.remObject.SetVariableCommand(CInt(ISOCNC.Remoting.VariableCommands.WriteVar), ' RWVariableManager.GetReadVarFromName(RESET_STATE).sAddress, "0") ' ' resetto prossima barra e variabili V ' m_NextBarId = 0 ' For Index = 1 To 6 ' Tpa.RWVariableManager.WriteVar(VPAR & Index.ToString(), 0) ' Next ' Return ' ' se macchina pronta e non sta tagliando alcun pezzo 'ElseIf (nP_Prod = 0 AndAlso ' nP_Machgroup = 0 AndAlso ' nP_Part = 0 AndAlso ' nP_State = PartState.NULL) AndAlso ' Tpa.opState = MachineOperatingState.Pending AndAlso Tpa.remObject.MachineOperativeStatus = MachineOperatingState.Pending Then ' ' verifico se c'e' un programma da lanciare ' SendNextProgram() ' ' attesa per essere sicuro che abbia scritto e riletto variabili ' Threading.Thread.Sleep(300) ' ' verifico stati inizio e fine pezzi 'ElseIf nP_Prod <> 0 AndAlso ' nP_Machgroup <> 0 AndAlso ' nP_Part <> 0 Then ' If nP_State = PartState.START Then ' ' scrivo data start su Db pezzo ' Dim dtStart As DateTime = DateTime.Now() ' DbControllers.m_PartController.UpdateStart(nP_Prod, nP_Machgroup, nP_Part, dtStart) ' DbControllers.m_PartController.UpdateStatus(nP_Prod, nP_Machgroup, nP_Part, ItemState.WIP) ' ' recupero gruppo di lavorazione del pezzo ' Dim MachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.FirstOrDefault(Function(x) x.Id = nP_Machgroup) ' ' recupero pezzo ' Dim Part As PartVM = MachGroup.PartVMList.FirstOrDefault(Function(x) x.nPartId = nP_Part) ' ' scrivo stato start ' Part.nProduction_State = 1 ' Part.dtStartTime = dtStart ' Part.NotifyPropertyChanged(NameOf(Part.nCALC_State)) ' ' azzero variabile per far ripartire macchina ' Tpa.RWVariableManager.WriteVar(P_STATE, 0) ' ' se nessun pezzo della barra diverso da quello corrente e' in start ' If Not MachGroup.PartVMList.Any(Function(x) x.nPartId <> nP_Part AndAlso x.nCALC_State = 1) Then ' ' scrivo data start su Db barra ' DbControllers.m_MachGroupController.UpdateStart(nP_Prod, nP_Machgroup, dtStart) ' DbControllers.m_MachGroupController.UpdateStatus(nP_Prod, nP_Machgroup, ItemState.WIP) ' ' scrivo stato start ' MachGroup.MyMachGroupM.SetProductionState(1) ' MachGroup.dtStartTime = dtStart ' MachGroup.NotifyPropertyChanged(NameOf(MachGroup.nCALC_State)) ' End If ' ' attesa per essere sicuro che abbia scritto e riletto variabili ' Threading.Thread.Sleep(300) ' ElseIf nP_State = PartState.END_ Then ' ' scrivo data end su Db pezzo ' Dim dtEnd As DateTime = DateTime.Now() ' DbControllers.m_PartController.UpdateEnd(nP_Prod, nP_Machgroup, nP_Part, dtEnd) ' DbControllers.m_PartController.UpdateStatus(nP_Prod, nP_Machgroup, nP_Part, ItemState.Produced) ' ' recupero gruppo di lavorazione del pezzo ' Dim MachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.FirstOrDefault(Function(x) x.Id = nP_Machgroup) ' ' recupero pezzo ' Dim Part As PartVM = MachGroup.PartVMList.FirstOrDefault(Function(x) x.nPartId = nP_Part) ' ' scrivo stato end ' Part.nProduction_State = 2 ' Part.dtEndTime = dtEnd ' Part.NotifyPropertyChanged(NameOf(Part.nCALC_State)) ' ' resetto stato redo ' Part.bRedo = False ' ' azzero variabile per far ripartire macchina ' Tpa.RWVariableManager.WriteVar(P_STATE, 0) ' ' se tutti i pezzi della barra sono in end ' If MachGroup.PartVMList.All(Function(x) x.nCALC_State >= 2) Then ' ' scrivo data end su Db barra ' DbControllers.m_MachGroupController.UpdateEnd(nP_Prod, nP_Machgroup, dtEnd) ' DbControllers.m_MachGroupController.UpdateStatus(nP_Prod, nP_Machgroup, ItemState.Produced) ' ' scrivo stato end ' MachGroup.MyMachGroupM.SetProductionState(2) ' MachGroup.dtEndTime = dtEnd ' MachGroup.NotifyPropertyChanged(NameOf(MachGroup.nCALC_State)) ' ' azzero tutte le variabilli per iniziare barra successiva ' Tpa.RWVariableManager.WriteVar(P_PROD, 0) ' Tpa.RWVariableManager.WriteVar(P_MACHGROUP, 0) ' Tpa.RWVariableManager.WriteVar(P_PART, 0) ' Tpa.RWVariableManager.WriteVar(P_STATE, 0) ' End If ' If Map.refMachCommandMessagePanelVM.bPrinterLink Then ' LabelPrinter.SetTestPrintVariables(nP_Prod, nP_Machgroup, nP_Part) ' LabelPrinter.Print() ' End If ' ' attesa per essere sicuro che abbia scritto e riletto variabili ' Threading.Thread.Sleep(300) ' End If 'End If Case NCTypes.NUM_FLEXIUM End Select End Sub Private m_Lock_SendProgram As New Object Private Function SetNextBarVars() As Boolean '' verifico se c'e' un programma da lanciare 'If Not IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM) Then ' For Each MyMachGroup As MyMachGroupVM In Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList ' If Not MyMachGroup.bSentToMachine AndAlso MyMachGroup.dtStartTime = DateTime.MinValue AndAlso MyMachGroup.bReadyForMachining Then ' ' leggo valori parametri V da file ' Dim dVPars(10) As Double ' Dim Lines() As String = File.ReadAllLines(MyMachGroup.CnFilePath()) ' Dim sVs() As String = Lines(0).Split(";"c) ' For VParIndex = 1 To sVs.Length - 1 ' Dim VPar() As String = sVs(VParIndex).Split("="c) ' VPar(1) = VPar(1).Trim ' StringToDouble(VPar(1), dVPars(VParIndex)) ' Next ' ' li scrivo nel CN ' For Index = 1 To 10 ' Tpa.RWVariableManager.WriteVar(VPAR & Index.ToString(), dVPars(Index)) ' Next ' m_NextBarId = MyMachGroup.Id ' Return True ' End If ' Next 'End If 'Return False End Function Private Function SendNextProgram() As Boolean 'Dim bSent As Boolean = False 'If Not m_bStartPending AndAlso Tpa.opState = MachineOperatingState.Pending Then ' Or Tpa.opState = MachineOperatingState.Start) Then ' ' verifico se c'e' un programma da lanciare ' If Not IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM) Then ' EgtOutLog("Start " & DateTime.Now()) ' SyncLock m_Lock_SendProgram ' For Each MyMachGroup As MyMachGroupVM In Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList ' If Not MyMachGroup.bSentToMachine AndAlso MyMachGroup.dtStartTime = DateTime.MinValue AndAlso MyMachGroup.bReadyForMachining Then ' ' lo lancio ' bSent = SendProgram(MyMachGroup.CnFilePath()) ' EgtOutLog("Mandato " & DateTime.Now() & " " & MyMachGroup.Name & " " & bSent.ToString()) ' MyMachGroup.SetSentToMachine(bSent) ' m_NextBarId = 0 ' Exit For ' End If ' Next ' End SyncLock ' EgtOutLog("End " & DateTime.Now()) ' End If 'End If 'Return bSent End Function Private Sub CommandList_CollectionChanged(sender As Object, e As NotifyCollectionChangedEventArgs) If Not IsNothing(e.NewItems) Then For Each Command In e.NewItems ExecuteCommand(DirectCast(Command, ThreadCommand)) m_CommandList.Remove(Command) Next End If End Sub Private Sub ExecuteCommand(Command As ThreadCommand) Select Case Command.CommandType Case CommandTypes.CONNECT Connect() Case CommandTypes.DISCONNECT Disconnect() Case CommandTypes.START Start() Case CommandTypes.STOP_ Stop Case CommandTypes.RESET Reset() Case CommandTypes.STEP_ Step_() Case CommandTypes.SETPOINT SetPoint() Case CommandTypes.SENDPROG SendProgram(Command.sVariable, Command.sVariables(1)) Case CommandTypes.REMOVEPROG RemoveProgram(Command.nVariable, Command.sVariable, Command.sVariables(1)) Case CommandTypes.REMOVEALLPROG RemoveAllProgram() Case CommandTypes.READ_TPA Tpa.RWVariableManager.RefreshVar(Command.sVariable) Case CommandTypes.WRITE WriteVar(Command.sVariable, Command.sVariables(1), Command.nVariable) Case CommandTypes.DELETEALARMS DeleteAlarms() Case CommandTypes.SETOP SetOPState(Command.nVariable) Case CommandTypes.READ_NUMFLEXIUM Num_Flexium.StartReadList() Num_Flexium.StartReadELS() Case CommandTypes.STOPREAD_NUMFLEXIUM Num_Flexium.CloseReadList() Num_Flexium.CloseReadELS() Case CommandTypes.MDI Num_Flexium.MDI_Execute(Command.sVariable) End Select End Sub Public Sub Connect() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Try m_CN = New TPAComm(Me) m_bConnected = Tpa.remObject.OnConnect() m_ResultCallbackDlg(CommandTypes.CONNECT, CommandStates.OK, ResultTypes.EXECUTED, "") ' creo classe di gestione variabili Catch ex As Exception m_ResultCallbackDlg(CommandTypes.CONNECT, CommandStates.ERROR_, ResultTypes.EXECUTED, "Errore: impossibile connettersi!") Return End Try Case NCTypes.NUM_FLEXIUM Try m_CN = New NUMFlexiumComm(Me) Num_Flexium.InitFxServer() m_ResultCallbackDlg(CommandTypes.CONNECT, CommandStates.OK, ResultTypes.EXECUTED, "") ' creo classe di gestione variabili Catch ex As Exception m_ResultCallbackDlg(CommandTypes.CONNECT, CommandStates.ERROR_, ResultTypes.EXECUTED, "Errore: impossibile connettersi!") Return End Try End Select End Sub Public Sub Disconnect() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Dim bOk As Boolean = Tpa.remObject.OnClose() m_bConnected = Not bOk m_ResultCallbackDlg(CommandTypes.DISCONNECT, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") ' chiudo classe Tpa Me.OnDispose() Tpa.OnDispose() Case NCTypes.NUM_FLEXIUM m_bConnected = False m_ResultCallbackDlg(CommandTypes.DISCONNECT, CommandStates.OK, ResultTypes.EXECUTED, "") ' chiudo classe Num_Flexium Me.OnDispose() 'Num_Flexium.OnDispose() End Select ' termino thread di comunicazione MachineCommThread.StopThread() End Sub Public Sub SetOPState(CNMode As NUMFlexiumComm.CNMode) Select Case Map.refMainWindowVM.SelNCType Case NCTypes.NUM_FLEXIUM Num_Flexium.SetMode(CNMode) End Select End Sub Public Sub Start() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Dim bOk As Boolean = Tpa.remObject.SetCommand(CInt(ISOCNC.Remoting.Commands.Start)) m_bStartPending = bOk m_ResultCallbackDlg(CommandTypes.START, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") Case NCTypes.NUM_FLEXIUM Num_Flexium.Start() End Select End Sub Public Sub [Stop]() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Dim bOk As Boolean = Tpa.remObject.SetCommand(CInt(ISOCNC.Remoting.Commands.[Stop])) m_ResultCallbackDlg(CommandTypes.STOP_, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") Case NCTypes.NUM_FLEXIUM Num_Flexium.Stop_() End Select End Sub Public Sub Reset() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Dim bOk As Boolean = Tpa.remObject.SetCommand(CInt(ISOCNC.Remoting.Commands.[End])) m_ResultCallbackDlg(CommandTypes.RESET, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") Case NCTypes.NUM_FLEXIUM Num_Flexium.Reset() End Select End Sub Public Sub Step_() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Dim bOk As Boolean = Tpa.remObject.SetCommand(CInt(ISOCNC.Remoting.Commands.Step)) m_ResultCallbackDlg(CommandTypes.STEP_, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") Case NCTypes.NUM_FLEXIUM '' End Select End Sub Public Sub SetPoint() Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Dim bOk As Boolean = Tpa.remObject.SetCommand(CInt(ISOCNC.Remoting.Commands.SetPoint)) m_ResultCallbackDlg(CommandTypes.SETPOINT, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") Case NCTypes.NUM_FLEXIUM '' End Select End Sub Public Function SendProgram(ProgramPath As String, ProgramType As String) As Boolean Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA If Tpa.opState = MachineOperatingState.Pending Then Tpa.remObject.RemoveAllProgramsFromList() Dim bOk As Boolean = False ProgramPath = ProgramPath.Replace("\\", "\") bOk = Tpa.remObject.AddProgramToList(ProgramPath) Threading.Thread.Sleep(1000) bOk = Tpa.remObject.SetCommand(CInt(ISOCNC.Remoting.Commands.Start_Program_Soft)) m_ResultCallbackDlg(CommandTypes.SENDPROG, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") Return bOk End If Return False Case NCTypes.NUM_FLEXIUM Num_Flexium.FileDownload(ProgramType, ProgramPath) Num_Flexium.StartTransfer() End Select End Function Public Sub RemoveProgram(ProgramIndex As Integer, ProgramPath As String, ProgramType As String) Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Dim bOk As Boolean Dim sError As String = "" If ProgramIndex > 0 Then bOk = CN.remObject.RemoveProgramFromListAtIndex(ProgramIndex) ElseIf Not String.IsNullOrEmpty(ProgramPath) Then bOk = CN.remObject.RemoveProgramFromList(ProgramPath) Else bOk = False sError = "Errore: nessun parametro passato" End If m_ResultCallbackDlg(CommandTypes.REMOVEPROG, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, sError) Case NCTypes.NUM_FLEXIUM Num_Flexium.FileDelete(ProgramType, ProgramPath) Num_Flexium.StartTransfer() End Select End Sub Public Sub RemoveAllProgram() Dim bOk As Boolean = CN.remObject.RemoveAllProgramsFromList() m_ResultCallbackDlg(CommandTypes.REMOVEALLPROG, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") End Sub Public Sub DeleteAlarms() Dim bOk As Boolean = CN.remObject.DeleteAlarms(CInt(AlarmType.ISO)) bOk = CN.remObject.DeleteAlarms(CInt(AlarmType.Message)) bOk = CN.remObject.DeleteAlarms(CInt(AlarmType.Cycle)) bOk = CN.remObject.DeleteAlarms(CInt(AlarmType.System)) m_ResultCallbackDlg(CommandTypes.DELETEALARMS, If(bOk, CommandStates.OK, CommandStates.ERROR_), ResultTypes.EXECUTED, "") End Sub Public Sub WriteVar(Address As String, Value As String, Type As CommVar.Types) Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA CN.RWVariableManager.WriteVar(Address, Value) Case NCTypes.NUM_FLEXIUM Select Case Type Case CommVar.Types.PLC Num_Flexium.WritePlcVariables(Address, Value) Case CommVar.Types.CN Num_Flexium.WriteNCVariables(Address, Value) End Select End Select End Sub Public Shared Function InitVar(nIndex As Integer) As CommVar Dim NewVar As CommVar = GetPrivateProfileVariable(S_VARIABLES, nIndex, Map.refMainWindowVM.MainWindowM.sMachIniFile) Select Case Map.refMainWindowVM.SelNCType Case NCTypes.TPA Return RWVariableManager.InitVar(NewVar) Case NCTypes.NUM_FLEXIUM Return NUMFlexiumComm.InitVar(NewVar) End Select End Function Private Shared Function GetPrivateProfileVariable(IpAppName As String, IpKeyName As String, IpFileName As String) As CommVar Dim sVariable As String = "" EgtUILib.GetPrivateProfileString(IpAppName, IpKeyName, "", sVariable, IpFileName) Dim sVariableValues() As String = sVariable.Split(","c) If Not sVariableValues.Count >= 4 Then Return Nothing For Index = 0 To sVariableValues.Count - 1 sVariableValues(Index) = sVariableValues(Index).Trim() Next Dim ReadType As CommVar.ReadTypes Select Case sVariableValues(2).ToLower() Case "o" ReadType = CommVar.ReadTypes.ONETIME Case "c" ReadType = CommVar.ReadTypes.CONTINUOUS Case Else ReadType = CommVar.ReadTypes.NULL End Select Dim Type As CommVar.Types Select Case sVariableValues(3).ToLower() Case "plc" Type = CommVar.Types.PLC Case "cn" Type = CommVar.Types.CN Case Else Type = CommVar.ReadTypes.NULL End Select Return New CommVar(sVariableValues(0), sVariableValues(1), ReadType, Type) End Function End Class