Namespace UTSModule.Test.Command.ComPortCommand
    Public Class ComWriteExecutor
        Inherits TestCommandExecutor

        Private _comPort As ComPort

        Private ReadOnly _portName As String
        Private ReadOnly _sendText As String
        Private ReadOnly _sendType As ComPort.SendTypes
        Private ReadOnly _delayedSend As Boolean


        Sub New(command As TestCommand)
            MyBase.New(command)

            _portName = command.Parameter(0)
            _sendText = command.Parameter(1)
            If [Enum].TryParse(command.Parameter(2), _sendType) = False Then
                _sendType = ComPort.SendTypes.StringAndCrlf
            End If

            _delayedSend = String.Compare(command.Parameter(3), "1") = 0
        End Sub

        Public Overrides Function Execute() As TestCommandReturn
            '从历史使用串口中查询
            If ComPortCommandManager.FindComPort(_portName, _comPort) = False Then
                CommandReturn.ExecuteResultTipString = $"无效的串口名"
                CommandReturn.ExecuteResult = False
                Return CommandReturn
            End If

            If _delayedSend Then
                ComPortCommandManager.FillDelaySendParam(_comPort, _sendText, _sendType)
                CommandReturn.ExecuteResult = True
                CommandReturn.ExecuteResultTipString = "执行串口延时发送填充成功"
                Return CommandReturn
            End If

            If _comPort.Write(_sendText, _sendType) = False Then
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = "执行串口通讯发送异常"
            Else
                CommandReturn.ExecuteResult = True
                CommandReturn.ExecuteResultTipString = "执行串口通讯发送成功"
            End If

            Return CommandReturn
        End Function
    End Class
End Namespace