Imports System.ComponentModel
Imports System.Text
Imports EgtUILib

Public Class CN_Siemens

   Inherits CN_generico

#Region "ENUM States and Local Constants"

   Enum eCnMode As Short
      AutoM = 0
      SingleM = 1
      MDI = 2
      Manual = 7
   End Enum

   Const RETURN_OK As Short = 0
   Const RETURN_BAD As Short = 1

#End Region

   '---------------------------------------------------------------------------------------------

   Sub New()

      sz_NC_error_messages = New List(Of String)

   End Sub

   Public Overrides Function Init() As Boolean

      Call M_MMFiles.init()

      Return True

   End Function

   Public Overrides Function Stop_Connection() As Boolean

      Return M_MMFiles.Stop_Connection

   End Function

   Public Overrides Function ActivateProgram(n_part_program As Integer) As Short

      Return M_MMFiles.ActivateProgram(n_part_program.ToString())

   End Function

   Public Overrides Function Delete_NC_prog(ByVal sz_filename As String) As Short

      Return M_MMFiles.Delete_NC_prog(sz_filename)

   End Function

   Public Overrides Function DGeneralFunctions_CycleStart() As Short

      Return M_MMFiles.CycleStart()

   End Function

   Public Overrides Function DGeneralFunctions_FeedHold() As Short

      Return M_MMFiles.FeedHold()

   End Function

   Public Overrides Function DGeneralFunctions_Reset() As Short

      Return M_MMFiles.Reset()

   End Function

   Public Overrides Function DGeneralFunctions_WriteCncMode(ByVal nmode As Short) As Short
      If m_IsSiemensOne Then
         Dim sDBVarPath As String = m_DbVarPath
         Select Case nmode
            Case 0   ' Modalità auto
               sDBVarPath &= "AUTO"
            Case 1   ' Modalita single block
               sDBVarPath &= "SINGLE_BLOCK"
            Case 2   ' Modalita MDI
               sDBVarPath &= "MDI"
            Case 7   ' Modalità manuale
               sDBVarPath &= "JOG"
            Case 8   ' Modalita Ref Point
               sDBVarPath &= "REF_POINT"
         End Select
         DVariables_WriteVariables3(sDBVarPath, 1, 1, 0, 0, "")
         Return 0
      Else
         Return M_MMFiles.WriteCncMode(nmode)
      End If
   End Function

   Public Overrides Function Download_NC_prog(ByVal sz_Pc_file_name As String, ByVal sz_NC_filen As String) As Short

      Return M_MMFiles.Download_NC_prog(sz_Pc_file_name, sz_NC_filen)

   End Function

   Public Overrides Function SetProgramMainOrSub(bMain As Boolean) As Boolean

      Dim szProgExt As String = If(bMain, "mpf", "spf")
      Return M_MMFiles.SetProgramExt(szProgExt)

   End Function

   Public Overrides Function DPOsition_ReadOnce() As Short

      Return RETURN_OK

   End Function

   Public Overrides Function MDI_command() As Short
      Return M_MMFiles.MDI_command(sz_ManualDataInput)
   End Function

   Public Overrides Function New_MDI_command(nChannel As Integer) As Short
      ' aggiungo M30 alla fine per non farlo andare in stato stop
      sz_ManualDataInput &= vbCrLf & " M30" & vbCrLf
      Return M_MMFiles.NEWMDI_command(sz_ManualDataInput, nChannel)
   End Function

   Public Overrides Sub set_OP_OM(ByVal n As Short)

      Call M_MMFiles.set_OP_OM(n)

   End Sub

   Public Overrides Function read_active_mode() As Short
      'EgtOutLog("Modo attivo: " & M_MMFiles.SiemensRet.n_param2)
      If m_IsSiemensOne Or m_IsActiveModeSubscription Then
         Select Case n_data(3)
            Case 1
               Return 0
            Case 2
               Return 2
            Case 8
               Return 7
            Case Else
               Return 0
         End Select
      Else
         ' per verificare che la comunicazione funzioni correttamente
         EgtOutLog("Modo -read_active_mode-: " & M_MMFiles.SiemensRet.n_param2.ToString)
         Return M_MMFiles.read_active_mode()
         'Return CShort(M_MMFiles.SiemensRet.n_param2)
      End If
   End Function

   Public Overrides Function Is_G24_active() As Boolean

      Return M_MMFiles.SiemensRet.b_interf_G24

   End Function

   Public Overrides Function Is_G71_active() As Boolean

      Return M_MMFiles.SiemensRet.b_interf_G71

   End Function

   Public Overrides Sub ReadEls_Add_Parameter(ByVal sz_var As String, ByVal n_par As Short)

      Dim n_var As Short
      If Not Short.TryParse(sz_var, n_var) Then Return

      Dim n_ret As Short = M_MMFiles.Read_Nc_var(n_var)
      If n_ret = RETURN_OK Then
         n_DReadELS_handle = n_par
      End If

   End Sub

   Public Overrides Sub Read_Variable(VarAddr As String, ParType As Integer, ByRef nVarValue As Integer, ByRef lVarValue As Long, ByRef dVarValue As Double, ByRef sVarValue As String)

      Dim n_ret As Short = M_MMFiles.Read_var(VarAddr, ParType)
      If n_ret = RETURN_OK Then
         Select Case ParType
            Case VarType.TInteger
               nVarValue = n_param1
            Case VarType.TLong
               lVarValue = l_param1
            Case VarType.TDouble
               dVarValue = d_param1
            Case VarType.TString
               sVarValue = sz_string
         End Select
      End If

   End Sub

   Public Overrides Sub DVariables_WriteVariables(ByVal sz_parameters As String, ByVal sz_values As String)
   End Sub

   Public Overrides Sub DVariables_WriteVariables2(ByVal sz_parameters As String, ByVal sz_values As String)

      Dim n_var As Short
      If Not Short.TryParse(sz_parameters, n_var) Then Return
      Dim n_value As Short
      If Not Short.TryParse(sz_values, n_value) Then Return

      Dim n_ret As Short = Write_Nc_var(n_var, n_value)

   End Sub

   Public Enum VarType As Short
      TBoolean = 0
      TInteger = 1
      TLong = 2
      TDouble = 3
      TString = 4
   End Enum

   Public Overrides Sub DVariables_WriteVariables3(VarAddr As String, ParType As Short, nVarValue As Integer, lVarValue As Long, dVarValue As Double, sVarValue As String)
      Dim n_ret As Short = Write_var(VarAddr, ParType, nVarValue, lVarValue, dVarValue, sVarValue)
      'EgtOutLog("CN SIEMENS Dopo scrittura variabile")

   End Sub

   Public Overrides Sub DVariables_Read_Subscription(VarAddr As String, ParType As Short, Index As Integer)
      Dim n_ret As Short = Read_subscription(VarAddr, ParType, Index)
      'EgtOutLog("CN SIEMENS Dopo lettura abbonamento")
   End Sub

   '
   '---------------------------------------------------------------------------------------------
   ' non necessarie....adesso .....
   '
   Public Overrides Function Write_var_1(ByVal sz_par As String, ByVal lvalue As Long) As Short
      Return RETURN_OK
   End Function

   Public Overrides Function DTool_set_read_range(ByVal n_start As Short, ByVal n_stop As Short) As Short
      ' non necessaria
      Return RETURN_OK
   End Function

   Public Overrides Function DTool_write_tool(ByVal n_tool_number As Short, ByVal Length_X As Double, ByVal Radius As Double, ByVal TipRadius_Z As Double, ByVal DLength_X As Double, ByVal DTipRadius_Z As Double, ByVal nDirection As Short, ByVal nType As Short, ByVal nH As Integer) As Short
      ' non necessaria
      Return RETURN_OK
   End Function

   Public Overrides Function Upload_NC_prog(ByVal sz_PC_filename As String, ByVal sz_NC_filename As String) As Short
      ' non necessaria
      Return 0
   End Function

   Public Overrides Function Is_G26_active() As Boolean
      ' non necessaria
      Return False
   End Function

   Public Overrides Function get_free_memory(ByVal n_zone As Int16) As Long
      ' non necessaria
      Return RETURN_OK
   End Function

   Public Overrides Function get_program_status() As Int16
      Return RETURN_OK
   End Function

   Public Overrides Function get_plc_messages() As Int16
      Return RETURN_OK
   End Function

   Public Overrides Function VerifyAxis(ByRef nAx As Integer) As Boolean
      ' Identificativo deve essere nei limiti previsti
      If nAx < 0 Or nAx > M_MMFiles.MAX_AXIS_ID Then Return False
      ' Numero assi non deve superare il massimo
      If M_MMFiles.m_Axes_Id.Count() > MAX_AXES Then Return False
      ' Assegno i dati
      M_MMFiles.m_Axes_Id.Add(nAx)
      nAx = M_MMFiles.m_Axes_Id.Count() - 1
      Return True
   End Function

End Class
