Imports UTS_Core.DebugLog
Imports UTS_Core.Expression
Imports UTS_Core.UTSModule.Test.StatusMonitor

Namespace UTSModule.Test.Command.UtsComPortCommand
    Public Class UtsWrTextDoubleExecutor
        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 = UtsComPortCommandManager.ReplaceWriteHexString(_utsCommand, TestCommand.Parameter(2))
            _timeout = CInt(TestCommand.Parameter(3))

            'Momo 2022-11-10 上下限支持表达式
            Dim tmpLowerLmt As String = TestCommand.Parameter(4)
            Dim tmpUpperLmt As String = TestCommand.Parameter(5)

            Dim tmpStrVal_LowerLmt As String = ""
            Dim tmpStrVal_UpperLmt As String = ""

            'Standard_1''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            If StringExpression.ExecuteLimit(tmpLowerLmt, tmpStrVal_LowerLmt) = True AndAlso
               StringExpression.ExecuteLimit(tmpUpperLmt, tmpStrVal_UpperLmt) = True Then
                CommandReturn.LowerLimit = tmpStrVal_LowerLmt
                CommandReturn.UpperLimit = tmpStrVal_UpperLmt
            Else
                CommandReturn.LowerLimit = TestCommand.Parameter(4)
                CommandReturn.UpperLimit = TestCommand.Parameter(5)
            End If
            ''''''''''''''''''''''''''''''''''''''''''''
            'Standard_2 如果参数个数大于7个'''''''''''''''''''''''''''''''''''''''''''''''''''''''
            If TestCommand.Parameter.Count >= 8 Then
                Dim tmpLowerLmt_2 As String = TestCommand.Parameter(6)
                Dim tmpUpperLmt_2 As String = TestCommand.Parameter(7)
                If StringExpression.ExecuteLimit(tmpLowerLmt_2, tmpStrVal_LowerLmt) = True AndAlso
                   StringExpression.ExecuteLimit(tmpUpperLmt_2, tmpStrVal_UpperLmt) = True Then
                    CommandReturn.LowerLimit_2 = tmpStrVal_LowerLmt
                    CommandReturn.UpperLimit_2 = tmpStrVal_UpperLmt
                Else
                    CommandReturn.LowerLimit_2 = TestCommand.Parameter(6)
                    CommandReturn.UpperLimit_2 = TestCommand.Parameter(7)
                End If
            End If
            ''''''''''''''''''''''''''''''''''''''''''''
            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
            Dim cmdStatus As TestCommandStatusMonitor.TestCommandStatusEnum = UtsComportTask.ReceivedTestCommandReturn(_timeout, receiveData)
            If cmdStatus = TestCommandStatusMonitor.TestCommandStatusEnum.ReceiveTimeout Then
                '接收数据失败
                CommandReturn.RecordValue = String.Empty
                CommandReturn.ExecuteResult = False
                CommandReturn.ExecuteResultTipString = "执行串口通讯接收异常"

                Return CommandReturn
            End If

            If receiveData Is Nothing Then
                ApplicationLog.WriteFatalLog($"测试器异常情况，串口返回状态：{cmdStatus}")
                CommandReturn.ExecuteResultTipString = "测试器异常情况，串口返回状态：{cmdStatus}"
                CommandReturn.RecordValue = ""
                CommandReturn.ExecuteResult = False
                Return CommandReturn
            End If

            '接收正常,处理数据是否在期望范围内
            '标准判定 ' Momo 2023-12-15 增加标准2的判定
            CommandReturn.RecordValue = receiveData.ParamListToString()
            CommandReturn.ExecuteResult = CompareFunction.ParamDoubleListCompare(receiveData.Params,
                                                                                 CommandReturn.LowerLimit,
                                                                                 CommandReturn.UpperLimit,
                                                                                 CommandReturn.LowerLimit_2,
                                                                                 CommandReturn.UpperLimit_2)


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

            Return CommandReturn
        End Function

    End Class
End Namespace