Imports System.Text
Imports UTS_Core.UTSModule.Test.Command.TestCommandManger
Namespace UTSModule.Test.Command.ComPortCommand
    Public Class ComReadExecutor
        Inherits TestCommandExecutor

        Private _comPort As ComPort

        Private ReadOnly _portName As String
        Private ReadOnly _receiveTimeout As Integer
        Private ReadOnly _dealType As DealTypes
        Private ReadOnly _expression As String
        Private ReadOnly _delaySendParam As ComPortCommandManager.DelaySendParam

        Sub New(command As TestCommand, delaySendParam As ComPortCommandManager.DelaySendParam)
            MyBase.New(command)

            _portName = command.Parameter(0)
            _receiveTimeout = CInt(command.Parameter(1))

            If [Enum].TryParse(TestCommand.Parameter(2), _dealType) = False Then
                _dealType = DealTypes.StringAndCrlf
            End If

            _expression = TestCommand.Parameter(3)

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

            _delaySendParam = delaySendParam
        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 _delaySendParam IsNot Nothing Then
                If _delaySendParam.ComPort.IsOpen() = False Then
                    CommandReturn.RecordValue = String.Empty
                    CommandReturn.ExecuteResult = False
                    CommandReturn.ExecuteResultTipString = "延时发送串口未打开"
                End If

                If _delaySendParam.ComPort.Write(_delaySendParam.SendText, _delaySendParam.SendType) = False Then
                    CommandReturn.RecordValue = String.Empty
                    CommandReturn.ExecuteResult = False
                    CommandReturn.ExecuteResultTipString = "执行串口延时发送失败"
                    Return CommandReturn
                End If
            End If

            '检测串口打开
            If _comPort.IsOpen() = False Then
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = "接受串口未打开"
            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

            '范围判断
            Select Case _dealType
                Case DealTypes.String
                    CommandReturn.RecordValue = Encoding.UTF8.GetString(buf)
                    CommandReturn.ExecuteResult = CompareFunction.StringCompare(CommandReturn.RecordValue, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
                Case DealTypes.StringAndCrlf
                    CommandReturn.RecordValue = Encoding.UTF8.GetString(buf).Replace(vbCrLf, "")
                    CommandReturn.ExecuteResult = CompareFunction.StringCompare(CommandReturn.RecordValue, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
                Case DealTypes.StringContain
                    CommandReturn.RecordValue = Encoding.UTF8.GetString(buf)
                    CommandReturn.ExecuteResult = CompareFunction.StringContain(CommandReturn.RecordValue, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
                Case DealTypes.IntegerString
                    CommandReturn.RecordValue = Encoding.UTF8.GetString(buf)
                    CommandReturn.ExecuteResult = CompareFunction.IntegerCompare(CommandReturn.RecordValue, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
                Case DealTypes.DoubleString
                    CommandReturn.RecordValue = Encoding.UTF8.GetString(buf)
                    CommandReturn.ExecuteResult = CompareFunction.DoubleCompare(CommandReturn.RecordValue, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
                Case DealTypes.ByteString
                    '检测有无需要计算的表达式
                    If String.IsNullOrWhiteSpace(_expression) Then
                        CommandReturn.RecordValue = String.Empty
                        CommandReturn.ExecuteResultTipString = $"无表达式默认通过"
                        CommandReturn.ExecuteResult = True
                        Return CommandReturn
                    End If

                    '检测表达式的有效性
                    buf = ComPort.HexStringToBytes(CommandReturn.RecordValue)
                    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
                Case DealTypes.Byte
                    '检测有无需要计算的表达式
                    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
            End Select

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

            Return CommandReturn
        End Function
    End Class
End Namespace