Imports EgtUILib

Public Class MachineCNPageUC

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Friend m_CN As CN_generico
   Private m_bFirst As Boolean = True
   Private m_ButtonPower As New List(Of MachineButton)

   Private Sub TestingPage_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      StartBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 1)
      StopBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 2)
      ResetBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 3)
      AutoBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 4)
      SingleBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 5)
      MDIBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 6)
      ManualBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 7)
      HomeBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 8)
      DownloadBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 9)
      UploadBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 10)
      ActivateBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 11)
      DeleteBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 12)
      MDIConfirmBtn.Content = EgtMsg(MSG_MACHINECNPAGEUC + 13)
      CNModeGpBx.Header = EgtMsg(MSG_MACHINECNPAGEUC + 14)
      CommandsGpBx.Header = EgtMsg(MSG_MACHINECNPAGEUC + 15)
      MDICommandGpBx.Header = EgtMsg(MSG_MACHINECNPAGEUC + 16)
      PartProgTransferGpBx.Header = EgtMsg(MSG_MACHINECNPAGEUC + 17)
      ErrorLstGpBx.Header = EgtMsg(MSG_ALARMSPAGEUC + 12)
      ' Attivo gruppo PartProgTransferGpBx leggendo la variabile nel file ini
      If GetPrivateProfileInt(S_GENERAL, K_TESTINGPAGE, 0, m_MainWindow.GetIniFile()) = 1 Then
         PartProgTransferGpBx.Visibility = Windows.Visibility.Visible
      Else
         PartProgTransferGpBx.Visibility = Windows.Visibility.Hidden
      End If

      ' nuovi pulsanti
      If GetPrivateProfileInt(S_NCDATA, K_NEWCONSOLE, 0, m_MainWindow.GetMachIniFile()) = 1 Then
         PowerONBtn.Content = "ON"
         PowerOFFBtn.Content = "OFF"
         PowerGpBx.Header = "Power"
      Else
         PowerGpBx.Visibility = Visibility.Collapsed
      End If
   End Sub

   Private Sub TestingPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      If m_MainWindow.m_CNCommunication.m_nNCType = 3 AndAlso m_MainWindow.m_CNCommunication.m_CN.m_IsSiemensOne Then
         HomeBtn.Visibility = Visibility.Collapsed
         SingleBtn.Visibility = Visibility.Collapsed
         PartProgTransferGpBx.Visibility = Windows.Visibility.Hidden
      End If

      '------ NUOVI BOTTONI ----------
      ' Lettura configurazione bottoni (per impostare l'uso del Joystick) da Ini di macchina
      m_ButtonPower.Clear()
      Dim m_nCount As Integer = 1
      Dim bFoundBtn As Boolean = True
      Dim sTestVal As String = String.Empty
      ' se non trovo il primo pulsante della lista allora nascondo l'elenco dei pulasnti per accensione
      If GetPrivateProfileString("MachineButtons", K_BUTTON & "1", "", sTestVal, m_MainWindow.GetMachIniFile()) = 0 Then
         PowerGpBx.Visibility = Visibility.Collapsed
         Return
      End If
      While bFoundBtn
         Dim sNameBtn As String = K_BUTTON & (m_nCount).ToString()
         Dim sMachineButtonType As String = String.Empty
         Dim sTImageName As String = String.Empty
         Dim sTLuaScriptName As String = String.Empty
         Dim sFImageName As String = String.Empty
         Dim sFLuaScriptName As String = String.Empty
         Dim sStateFlag As String = String.Empty
         bFoundBtn = m_MainWindow.m_DirectCutPageUC.m_ControlsMachineButton.GetPrivateProfileMachineButtons("MachineButtons", sNameBtn, sMachineButtonType, sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag, m_MainWindow.GetMachIniFile())
         If bFoundBtn Then
            m_nCount += 1
            Dim ButtonToAdd As MachineButton = Nothing
            ' la selezione dei comandi si trova nella pagina m_ControlsMachineButton.MachineButtonType (tipo Enum)
            Select Case sMachineButtonType
               Case 1
                  ButtonToAdd = New NoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 2
                  ButtonToAdd = New TwoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 3
                  ButtonToAdd = New ThreeStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 4
                  ButtonToAdd = New DoubleCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 5
                  ButtonToAdd = New PressedCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
            End Select
            If Not IsNothing(ButtonToAdd) Then
               m_ButtonPower.Add(ButtonToAdd)
            End If
         End If
      End While
      ' lo stato di questi comandi vieni carico dalla lettura del PLC (refresh)

      '-----------NUOVI BOTTONI------------

   End Sub

   Private Sub StartBtn_Click(sender As Object, e As RoutedEventArgs) Handles StartBtn.Click
      If m_MainWindow.m_CNCommunication.m_nNCType = 3 AndAlso m_MainWindow.m_CNCommunication.m_CN.m_IsSiemensOne Then
         Dim sDBVarPath As String = ""
         GetPrivateProfileString(S_NCSIEMENS, K_DBVARPATH, "", sDBVarPath, m_MainWindow.GetMachIniFile)
         sDBVarPath &= "Start_Cycle"
         m_CN.DVariables_WriteVariables3(sDBVarPath, 1, 1, 0, 0, "")     ' Modalità manuale
      Else
         m_CN.DGeneralFunctions_CycleStart()
      End If
   End Sub

   Private Sub StopBtn_Click(sender As Object, e As RoutedEventArgs) Handles StopBtn.Click
      If m_MainWindow.m_CNCommunication.m_nNCType = 3 AndAlso m_MainWindow.m_CNCommunication.m_CN.m_IsSiemensOne Then
         Dim sDBVarPath As String = ""
         GetPrivateProfileString(S_NCSIEMENS, K_DBVARPATH, "", sDBVarPath, m_MainWindow.GetMachIniFile)
         sDBVarPath &= "Stop_Cycle"
         m_CN.DVariables_WriteVariables3(sDBVarPath, 1, 0, 0, 0, "")     ' Modalità manuale
      Else
         m_CN.DGeneralFunctions_FeedHold()
      End If
   End Sub

   Private Sub ResetBtn_Click(sender As Object, ghe As RoutedEventArgs) Handles ResetBtn.Click
      If m_MainWindow.m_CNCommunication.m_nNCType = 3 AndAlso m_MainWindow.m_CNCommunication.m_CN.m_IsSiemensOne Then
         Dim sDBVarPath As String = ""
         GetPrivateProfileString(S_NCSIEMENS, K_DBVARPATH, "", sDBVarPath, m_MainWindow.GetMachIniFile)
         sDBVarPath &= "Reset_Cycle"
         m_CN.DVariables_WriteVariables3(sDBVarPath, 1, 1, 0, 0, "")     ' Modalità manuale
      Else
         Call m_CN.DGeneralFunctions_Reset()
      End If
   End Sub

   Private Sub AutoBtn_Click(sender As Object, e As RoutedEventArgs) Handles AutoBtn.Click
      m_CN.DGeneralFunctions_WriteCncMode(0)
   End Sub

   Private Sub SingleBtn_Click(sender As Object, e As RoutedEventArgs) Handles SingleBtn.Click
      m_CN.DGeneralFunctions_WriteCncMode(1)
   End Sub

   Private Sub MDIBtn_Click(sender As Object, e As RoutedEventArgs) Handles MDIBtn.Click
      m_CN.DGeneralFunctions_WriteCncMode(2)
   End Sub

   Private Sub ManualBtn_Click(sender As Object, e As RoutedEventArgs) Handles ManualBtn.Click
      m_CN.DGeneralFunctions_WriteCncMode(7)
   End Sub

   Private Sub HomeBtn_Click(sender As Object, e As RoutedEventArgs) Handles HomeBtn.Click
      m_CN.DGeneralFunctions_WriteCncMode(8)
   End Sub

   Private Sub MDITxBx_KeyDown(sender As Object, e As KeyEventArgs) Handles MDITxBx.KeyDown
      If e.Key = Key.Return Then
         ' leggo se stato reset
         If Not m_CN.bResetState Then Return
         m_CN.sz_ManualDataInput = MDITxBx.Text
         ' se attivi nuovi comandi MDI su siemens
         If m_MainWindow.m_CNCommunication.m_nNCType = 3 AndAlso GetPrivateProfileInt(S_NCSIEMENS, K_USENEWMDI, 0, m_MainWindow.GetMachIniFile()) > 0 Then
            EgtOutLog("MDI command: " & m_CN.sz_ManualDataInput)
            Dim x = m_CN.New_MDI_command(1)
            EgtOutLog("MDI command res: " & x)
         Else
            m_CN.MDI_command()
         End If
         MDITxBx.Background = Brushes.LightGray
         'Keyboard.ClearFocus()
      End If
   End Sub

   Private Sub MDITxBx_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs) Handles MDITxBx.PreviewMouseDown
      MDITxBx.Background = Brushes.White
   End Sub

   Friend Sub DownloadBtn_Click(sender As Object, e As RoutedEventArgs) Handles DownloadBtn.Click
      If m_MainWindow.m_CNCommunication.m_nNCType = 1 Then
         m_CN.Delete_NC_prog(DULoadTxBx.Text)
      End If
      Dim DownloadName As String = String.Empty
      If m_MainWindow.m_CNCommunication.m_nNCType = 1 Or m_MainWindow.m_CNCommunication.m_nNCType = 2 Then
         DownloadName = "%" & DULoadTxBx.Text
      Else
         DownloadName = DULoadTxBx.Text
      End If
      Dim sPcFile As String = m_MainWindow.GetCncDir() & "\" & DULoadTxBx.Text & m_MainWindow.m_CurrentMachine.sIsoFileExt
      m_CN.Download_NC_prog(sPcFile, DownloadName)
   End Sub

   Private Sub UploadBtn_Click(sender As Object, e As RoutedEventArgs) Handles UploadBtn.Click
      Dim DownloadName As String = String.Empty
      If m_MainWindow.m_CNCommunication.m_nNCType = 1 Or m_MainWindow.m_CNCommunication.m_nNCType = 2 Then
         DownloadName = "%" & DULoadTxBx.Text
      Else
         DownloadName = DULoadTxBx.Text
      End If
      Dim sPcFile As String = m_MainWindow.GetCncDir() & "\" & DULoadTxBx.Text & m_MainWindow.m_CurrentMachine.sIsoFileExt
      m_CN.Upload_NC_prog(sPcFile, DownloadName)
   End Sub

   Private Sub ActivateBtn_Click(sender As Object, e As RoutedEventArgs) Handles ActivateBtn.Click
      If m_MainWindow.m_CNCommunication.m_nNCType = 1 Or m_MainWindow.m_CNCommunication.m_nNCType = 2 Then
         Dim nName As Integer = 0
         StringToInt(DULoadTxBx.Text, nName)
         m_CN.ActivateProgram(nName)             ' integer version
      Else
         m_CN.ActivateProgram(DULoadTxBx.Text)   ' string version
      End If
   End Sub

   Private Sub DeleteBtn_Click(sender As Object, e As RoutedEventArgs) Handles DeleteBtn.Click
      Dim DeleteName As String = String.Empty
      If m_MainWindow.m_CNCommunication.m_nNCType = 1 Or m_MainWindow.m_CNCommunication.m_nNCType = 2 Then
         DeleteName = "%" & DULoadTxBx.Text
      Else
         DeleteName = DULoadTxBx.Text
      End If
      m_CN.Delete_NC_prog(DeleteName)
   End Sub

   Private Sub MDIConfirmBtn_Click(sender As Object, e As RoutedEventArgs) Handles MDIConfirmBtn.Click
      m_CN.sz_ManualDataInput = MDITxBx.Text
      ' se attivi nuovi comandi MDI su siemens
      If m_MainWindow.m_CNCommunication.m_nNCType = 3 AndAlso GetPrivateProfileInt(S_NCSIEMENS, K_USENEWMDI, 0, m_MainWindow.GetMachIniFile()) > 0 Then
         ' leggo se stato reset
         If Not m_CN.bResetState Then Return
         EgtOutLog("MDI command: " & m_CN.sz_ManualDataInput)
         Dim x = m_CN.New_MDI_command(1)
         EgtOutLog("MDI command res: " & x)
      Else
         m_CN.MDI_command()
      End If

      MDITxBx.Background = Brushes.LightGray
   End Sub

   Private Sub PowerON_Click(sender As Object, e As RoutedEventArgs) Handles PowerONBtn.Click
      Dim PowerOnButton As MachineButton = m_ButtonPower(0)
      ' eseguo lo script lua associato
      PowerOnButton.ExecuteMDICommand(PowerOnButton.TLuaScriptName)
      ' che comunica al PLC di cambiare lo stato, la modifica dello stato è intercettata dalla funzione Refresh che avvia il metodo Changed
   End Sub

   Private Sub PowerOFF_Click(sender As Object, e As RoutedEventArgs) Handles PowerOFFBtn.Click
      Dim PowerOffButton As MachineButton = m_ButtonPower(1)
      ' eseguo lo script lua associato
      PowerOffButton.ExecuteMDICommand(PowerOffButton.TLuaScriptName)
      ' che comunica al PLC di cambiare lo stato, la modifica dello stato è intercettata dalla funzione Refresh che avvia il metodo Changed
   End Sub

   Friend Sub PowerONChanged(bPowerON As Boolean)
      ' devo decodificare il tipo di pulsante, quindi eseguire la conversione...
      Dim PowerONButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonPower
         If MachineButton.StateFlag.Trim = K_POWERON Then
            PowerONButton = MachineButton
            Exit For
         End If
      Next
      If Not IsNothing(PowerONButton) Then
         PowerONButton.SetIsChecked(bPowerON)
         PowerONBtn.IsChecked = bPowerON
      End If
   End Sub

   Friend Sub PowerOFFChanged(bPowerOFF As Boolean)
      ' leggo solo una varibile -> quindi quando modifico lo stato di PowerON devo modificare lo stato di PowerOFF
      Dim PowerOFFButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonPower
         If MachineButton.StateFlag.Trim = K_POWEROFF Then
            PowerOFFButton = MachineButton
            Exit For
         End If
      Next
      If Not IsNothing(PowerOFFButton) Then
         PowerOFFButton.SetIsChecked(bPowerOFF)
         PowerOFFBtn.IsChecked = bPowerOFF
      End If
   End Sub

End Class
