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

        Private ReadOnly _portList As List(Of ComPort)

        Private ReadOnly _portAlias As String
        Private ReadOnly _portParam As String
        Private ReadOnly _sendText As String
        Private ReadOnly _sendType As ComPort.SendTypes
        Private ReadOnly _receiveTimeout As Integer
        Private ReadOnly _dealType As DealTypes
        Private ReadOnly _expression As String

        Sub New(command As TestCommand, portList As List(Of ComPort))
            MyBase.New(command)

            _portList = portList

            _portAlias = command.Parameter(0)

            _portParam = command.Parameter(1)

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

            _receiveTimeout = CInt(command.Parameter(4))

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

            _expression = TestCommand.Parameter(6)

            CommandReturn.LowerLimit = TestCommand.Parameter(7)
            CommandReturn.UpperLimit = TestCommand.Parameter(7)
        End Sub

        Public Overrides Function Execute() As TestCommandReturn
            '从当前设备上串口中查询
            Dim names As String() = IO.Ports.SerialPort.GetPortNames()
            Dim _comPort As New ComPort
            _comPort.PortAlias = _portAlias
            For Each name As String In names
                If _comPort.IsOpen Then _comPort.Close()

                '串口初始化
                _comPort.PortName = name
                If _comPort.InitializePort(_portParam) = False Then Continue For

                '串口打开
                If _comPort.Open() = False Then Continue For

                '串口发送
                _comPort.ClearRecvCache()
                If _comPort.Write(_sendText, _sendType) = False Then Continue For

                '串口接收
                Dim buf() As Byte = _comPort.ReadBytes(_receiveTimeout)
                If buf.Length = 0 Then Continue For

                '范围判断
                Select Case _dealType
                    Case DealTypes.String
                        If CompareFunction.StringCompare(Encoding.UTF8.GetString(buf), CommandReturn.LowerLimit, CommandReturn.UpperLimit) = False Then Continue For
                    Case DealTypes.StringAndCrlf
                        If CompareFunction.StringCompare(Encoding.UTF8.GetString(buf).Replace(vbCrLf, ""), CommandReturn.LowerLimit, CommandReturn.UpperLimit) = False Then Continue For
                    Case DealTypes.StringContain
                        If CompareFunction.StringContain(Encoding.UTF8.GetString(buf), CommandReturn.LowerLimit, CommandReturn.UpperLimit) = False Then Continue For
                    Case DealTypes.IntegerString
                        If CompareFunction.IntegerCompare(Encoding.UTF8.GetString(buf), CommandReturn.LowerLimit, CommandReturn.UpperLimit) = False Then Continue For
                    Case DealTypes.DoubleString
                        If CompareFunction.DoubleCompare(Encoding.UTF8.GetString(buf), CommandReturn.LowerLimit, CommandReturn.UpperLimit) = False Then Continue For
                    Case DealTypes.ByteString
                        '检测有无需要计算的表达式
                        If String.IsNullOrWhiteSpace(_expression) = False Then
                            '检测表达式的有效性
                            buf = ComPort.HexStringToBytes(CommandReturn.RecordValue)
                            Dim str As String = Expression.StringExpression.ReplaceBytes(buf, _expression)
                            If Expression.StringExpression.CheckExpressionString(str) = False Then Continue For

                            Try
                                If CompareFunction.DoubleCompare(Expression.StringExpression.GetDoubleExpressionResult(str).ToString(), CommandReturn.LowerLimit, CommandReturn.UpperLimit) = False Then Continue For
                            Catch ex As Exception
                                Continue For
                            End Try
                        End If
                    Case DealTypes.Byte
                        '检测有无需要计算的表达式
                        If String.IsNullOrWhiteSpace(_expression) = False Then
                            '检测表达式的有效性
                            Dim str As String = Expression.StringExpression.ReplaceBytes(buf, _expression)
                            If Expression.StringExpression.CheckExpressionString(str) = False Then Continue For

                            Try
                                If CompareFunction.DoubleCompare(Expression.StringExpression.GetDoubleExpressionResult(str).ToString(), CommandReturn.LowerLimit, CommandReturn.UpperLimit) = False Then Continue For
                            Catch ex As Exception
                                Continue For
                            End Try
                        End If
                End Select

                '查询到目标串口
                _comPort.Close()
                _portList.Add(_comPort)

                CommandReturn.RecordValue = name
                CommandReturn.ExecuteResultTipString = $"设备上已查询到可用串口:{name}"
                CommandReturn.ExecuteResult = True
                Return CommandReturn
            Next

            '未查询到目标串口
            If _comPort.IsOpen Then _comPort.Close()

            CommandReturn.RecordValue = ""
            CommandReturn.ExecuteResultTipString = $"设备上未查询到可用串口"
            CommandReturn.ExecuteResult = False
            Return CommandReturn
        End Function
    End Class
End Namespace
