Imports System.Text

Namespace UTSModule.Test.Command.UtsComPortCommand
    Public Class UtsComPortCommandManager
        Public Shared Function CreateExecutor(command As TestCommand) As TestCommandExecutor
            Dim executor As TestCommandExecutor
            Select Case command.Name
                Case "UTS_Write_Text"
                    executor = New UtsWriteTextExecutor(command)
               ' Case "UTS_Read_Text"

                Case "UTS_WR_Text_String"
                    executor = New UtsWrTextStringExecutor(command)
                Case "UTS_WR_Text_Integer"
                    executor = New UtsWrTextIntegerExecutor(command)
                Case "UTS_WR_Text_Double"
                    executor = New UtsWrTextDoubleExecutor(command)
                Case "UTS_WR_Text_Byte"
                    executor = New UtsWrTextBytesExecutor(command)
                Case Else
                    Throw New Exception($"UtsComPort集,未知命令 ：{command.Name}")
            End Select
            Return executor
        End Function

        Public Shared Function ReplaceWriteHexString(cmd As String, param As String) As String
            cmd = cmd.ToLower
            If cmd.EndsWith("hex") = False Then Return param

            Dim sb As New StringBuilder
            param = param.Replace(" ", "")
            If param.Length Mod 2 <> 0 Then param = "0" & param

            sb.Append(param.Chars(0))
            For i As Integer = 1 To param.Length - 1
                If i Mod 2 = 0 Then
                    sb.Append(" ")
                End If
                sb.Append(param.Chars(i))
            Next

            Return sb.ToString
        End Function
    End Class
End Namespace