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

        Private _comPort As ComPort
        Private ReadOnly _portName As String
        Private ReadOnly _sendText As String
        Private ReadOnly _expression As String
        Private ReadOnly _receiveTimeout As Integer

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

            _portName = command.Parameter(0)
            _sendText = command.Parameter(1)

            _expression = command.Parameter(2)
            _receiveTimeout = CInt(command.Parameter(3))

            CommandReturn.LowerLimit = TestCommand.Parameter(4)
            CommandReturn.UpperLimit = TestCommand.Parameter(5)
        End Sub

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

            '串口发送数据
            _comPort.ClearRecvCache()
            If _comPort.WriteBytes(_sendText) = False Then
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = "执行串口通讯发送异常"
                Return CommandReturn
            End If

            '串口接收
            Dim buf() As Byte = _comPort.ReadBytes(_receiveTimeout)
            If buf.Length = 0 Then
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = "执行串口通讯接收异常"
                Return CommandReturn
            End If

            '检测有无需要计算的表达式
            If String.IsNullOrWhiteSpace(_expression) Then
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResultTipString = $"无表达式默认通过"
                CommandReturn.ExecuteResult = True
                Return CommandReturn
            End If

            '检测表达式的有效性
            Dim str As String = Expression.StringExpression.ReplaceBytes(buf, _expression)
            If Expression.StringExpression.CheckExpressionString(str) = False Then
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResultTipString = $"无效的表达式[{str}]"
                CommandReturn.ExecuteResult = False
                Return CommandReturn
            End If

            '串口接收正常，判断数据是否在有效范围内
            Try
                CommandReturn.RecordValue = Expression.StringExpression.GetDoubleExpressionResult(str).ToString()
                CommandReturn.ExecuteResult = CompareFunction.DoubleCompare(CommandReturn.RecordValue, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
            Catch ex As Exception
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = $"表达式[{str}]计算失败"
                Return CommandReturn
            End Try

            If CommandReturn.ExecuteResult Then
                CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围内"
            Else
                CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围外"
            End If

            Return CommandReturn
        End Function
    End Class
End Namespace