
Namespace UTSModule.Test.Command.ComPortCommand
    Public Class ComPortCommandManager
        Private Shared ReadOnly PortList As New List(Of ComPort)

        Private Shared _delayedSend As Boolean

        Private Shared _delaySendParam As DelaySendParam

        Public Shared Function CreateExecutor(command As TestCommand) As TestCommandExecutor
            Dim executor As TestCommandExecutor
            Select Case command.Name
                Case "Config_Comm_Alias"
                    executor = New ConfigComAliasExecutor(command, PortList)
                Case "Open_CommPort"
                    executor = New OpenComPortExecutor(command, PortList)
                Case "Close_CommPort"
                    executor = New CloseComPortExecutor(command)
                Case "Close_All_CommPort"
                    executor = New CloseAllComportExecutor(command, PortList)
                Case "Comm_Write"
                    executor = New ComWriteExecutor(command)
                Case "Comm_Read"
                    If _delayedSend Then
                        executor = New ComReadExecutor(command, _delaySendParam)
                        _delayedSend = False
                    Else
                        executor = New ComReadExecutor(command, Nothing)
                    End If
                Case "Comm_Write_Read"
                    executor = New ComWriteReadExecutor(command)
                Case "Get_CommPort"
                    executor = New GetComExecutor(command, PortList)
                    'Case "Comm_Write_Text"
                    '    executor = New ComWriteTextExecutor(command)
                    'Case "Comm_Read_Text"
                    '    executor = New ComReadTextExecutor(command)
                    'Case "Comm_WR_Text"
                    '    executor = New ComWrTextExecutor(command)
                    'Case "Comm_Write_Bytes"
                    '    executor = New ComWriteBytesExecutor(command)
                    'Case "Comm_Read_Bytes"
                    '    executor = New ComReadBytesExecutor(command)
                    'Case "Comm_WR_Bytes"
                    '    executor = New ComWrBytesExecutor(command)
                Case Else
                    Throw New Exception($"ComPort集,未知命令 ：{command.Name}")
            End Select
            Return executor
        End Function


        ''' <summary>
        ''' 根据串口名，查询对应串口
        ''' </summary>
        ''' <param name="portName"></param>
        ''' <param name="port"></param>
        ''' <returns></returns>
        Friend Shared Function FindComPort(portName As String, ByRef port As ComPort) As Boolean
            For Each comPort As ComPort In PortList
                If comPort.NameEqual(portName) OrElse comPort.AliasEqual(portName) Then
                    port = comPort
                    Return True
                End If
            Next
            Return False
        End Function


        Friend Shared Sub FillDelaySendParam(port As ComPort, sendText As String, sendType As ComPort.SendTypes)
            _delaySendParam = New DelaySendParam(port, sendText, sendType)
            _delayedSend = True
        End Sub


        Public Class DelaySendParam
            Sub New(port As ComPort, text As String, type As ComPort.SendTypes)
                ComPort = port
                SendText = text
                SendType = type
            End Sub

            Public ComPort As ComPort

            Public SendText As String

            Public SendType As ComPort.SendTypes

        End Class

    End Class
End Namespace