Imports UTS_Core.UTSModule.Test.StatusMonitor

Namespace UTSModule.Test.Command.UtsComPortCommand
    Public Class UtsWrTextStringExecutor
        Inherits TestCommandExecutor

        Private ReadOnly _receiverAddress As String
        Private ReadOnly _utsCommand As String
        Private ReadOnly _utsParamString As String
        Private ReadOnly _timeout As Integer

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

            _receiverAddress = TestCommand.Parameter(0)
            _utsCommand = TestCommand.Parameter(1)
            _utsParamString = TestCommand.Parameter(2)
            _timeout = CInt(TestCommand.Parameter(3))

            CommandReturn.LowerLimit = TestCommand.Parameter(4)
            CommandReturn.UpperLimit = TestCommand.Parameter(5)
            CommandReturn.RecordValue = String.Empty
            CommandReturn.ExecuteResultTipString = String.Empty
            CommandReturn.ExecuteResult = True
        End Sub

        ''' <summary>
        ''' 执行串口通讯命令，接收返回值，并比较返回数据是否在期望上下范围中
        ''' </summary>
        ''' <returns></returns>
        Public Overrides Function Execute() As TestCommandReturn
            '组包
            Dim sendData As New UtsComPortData(_receiverAddress, _utsCommand, _utsParamString)


            '调用测试串口发送接口
            If UtsComportTask.SendTestCommand(sendData) = TestCommandStatusMonitor.TestCommandStatusEnum.SendingFailure Then
                '发送数据失败
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = "执行串口通讯发送异常"
                Return CommandReturn
            End If


            '调用测试串口等待接收完成接口
            Dim receiveData As UtsComPortData = Nothing
            If UtsComportTask.ReceivedTestCommandReturn(_timeout, receiveData) =
               TestCommandStatusMonitor.TestCommandStatusEnum.ReceiveTimeout Then  '接收数据超时

                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = "执行串口通讯接收异常"

                Return CommandReturn
            End If

          

            '接收正常,处理数据是否在期望范围内
            CommandReturn.RecordValue = receiveData.ParamListToString()
            CommandReturn.ExecuteResult = CompareFunction.ParamStringListCompare(receiveData.Params, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
            If CommandReturn.ExecuteResult Then
                CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围内"
            Else
                CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围外"
            End If

            Return CommandReturn
        End Function
    End Class
End Namespace