Imports EgtBEAMWALL.Core.ConstMachComm

Class MachineCommThread

   ' variabile che ferma il ciclo
   Private Shared m_Stop As Boolean = False
   Private Shared m_StartTick As Boolean = False

   Friend Shared Sub StopThread()
      m_Stop = True
   End Sub
   Friend Shared Sub StartTick()
      m_StartTick = True
   End Sub

   ' variabile in cui ci sono stati inizio e fine pezzo
   Private Shared sP1VarName As String = "0.FUNM.E81295"
   Private Shared sP2VarName As String = "0.FUNM.E81296"
   Private Shared sP3VarName As String = "0.FUNM.E81297"
   Private Shared sP4VarName As String = "0.FUNM.E81298"
   Private Shared sResetVarName As String = "0.FUNM.E80048"

   Public Shared Sub MachManagingThreadFunction(ResultCallbackDlg As ResultCallbackDlg, CloseCallbackDlg As CloseCallbackDlg, UpdateCallbackDlg As UpdateCallbackDlg,
                                                TPAAlarmCallbackDlg As TPAAlarmCallbackDlg, NUMAlarmCallbackDlg As NUMAlarmCallbackDlg, AxisCoordinatesCallbackDlg As AxisCoordinatesCallbackDlg,
                                                OpStateCallbackDlg As OpStateCallbackDlg, OpModeCallbackDlg As OpModeCallbackDlg, ChannelCallbackDlg As ChannelCallbackDlg,
                                                ReadVarCallbackDlg As ReadVarCallbackDlg)
      ' inizializzo callback
      m_ResultCallbackDlg = ResultCallbackDlg
      m_CloseCallbackDlg = CloseCallbackDlg
      m_UpdateCallbackDlg = UpdateCallbackDlg
      m_TPAAlarmCallbackDlg = TPAAlarmCallbackDlg
      m_NUMAlarmCallbackDlg = NUMAlarmCallbackDlg
      m_AxisCoordinatesCallbackDlg = AxisCoordinatesCallbackDlg
      m_OpStateCallbackDlg = OpStateCallbackDlg
      m_OpModeCallbackDlg = OpModeCallbackDlg
      m_ChannelCallbackDlg = ChannelCallbackDlg
      m_ReadVarCallbackDlg = ReadVarCallbackDlg

      ' creo classe di comunicazione
      Dim m_MachManaging As MachManaging = New MachManaging()

      While Not m_Stop
         ' eseguo ciclo principale che tiene vivo il thread
         If m_StartTick Then
            m_MachManaging.Timer_Tick()
         End If
         Threading.Thread.Sleep(TimeSpan.FromMilliseconds(1000))
      End While

   End Sub


End Class

Public Class ThreadCommand

   Public Enum ProcessingStatuses
      WAITING = 1
      PROCESSING = 2
      DONE = 3
   End Enum

   Private m_CommandType As CommandTypes
   Public ReadOnly Property CommandType As CommandTypes
      Get
         Return m_CommandType
      End Get
   End Property

   Private m_nVariables(5) As Integer
   Public ReadOnly Property nVariable As Integer
      Get
         Return m_nVariables(0)
      End Get
   End Property
   Public ReadOnly Property nVariables As Integer()
      Get
         Return m_nVariables
      End Get
   End Property
   Private m_dVariables(5) As Double
   Public ReadOnly Property dVariable As Double
      Get
         Return m_dVariables(0)
      End Get
   End Property
   Public ReadOnly Property dVariables As Double()
      Get
         Return m_dVariables
      End Get
   End Property
   Private m_sVariables(5) As String
   Public ReadOnly Property sVariable As String
      Get
         Return m_sVariables(0)
      End Get
   End Property
   Public ReadOnly Property sVariables As String()
      Get
         Return m_sVariables
      End Get
   End Property

   Private m_ProcessingStatus As ProcessingStatuses = ProcessingStatuses.WAITING
   Public ReadOnly Property ProcessingStatus As ProcessingStatuses
      Get
         Return m_ProcessingStatus
      End Get
   End Property
   Friend Sub SetProcessingStatus(value As ProcessingStatuses)
      m_ProcessingStatus = value
   End Sub

   Protected Sub New()
   End Sub

   Public Shared Function CreateCommand(CommandType As CommandTypes) As ThreadCommand
      Dim NewCommand As ThreadCommand = New ThreadCommand
      NewCommand.m_CommandType = CommandType
      Return NewCommand
   End Function
   Public Shared Function CreateCommand(CommandType As CommandTypes, Variable As Integer) As ThreadCommand
      Dim NewCommand As ThreadCommand = New ThreadCommand
      NewCommand.m_CommandType = CommandType
      NewCommand.m_nVariables(0) = Variable
      Return NewCommand
   End Function
   Public Shared Function CreateCommand(CommandType As CommandTypes, Variable As Double) As ThreadCommand
      Dim NewCommand As ThreadCommand = New ThreadCommand
      NewCommand.m_CommandType = CommandType
      NewCommand.m_dVariables(0) = Variable
      Return NewCommand
   End Function
   Public Shared Function CreateCommand(CommandType As CommandTypes, Variable As String) As ThreadCommand
      Dim NewCommand As ThreadCommand = New ThreadCommand
      NewCommand.m_CommandType = CommandType
      NewCommand.m_sVariables(0) = Variable
      Return NewCommand
   End Function
   Public Shared Function CreateCommand(CommandType As CommandTypes, nVariables() As Integer, dVariables() As Double, sVariables() As String) As ThreadCommand
      Dim NewCommand As ThreadCommand = New ThreadCommand
      NewCommand.m_CommandType = CommandType
      NewCommand.m_nVariables = nVariables
      NewCommand.m_dVariables = dVariables
      NewCommand.m_sVariables = sVariables
      Return NewCommand
   End Function
   Public Shared Function CreateReadCommand(sVariable As String) As ThreadCommand
      Dim NewCommand As ThreadCommand = New ThreadCommand
      NewCommand.m_CommandType = CommandTypes.READ_TPA
      NewCommand.m_sVariables(0) = sVariable
      Return NewCommand
   End Function

End Class
