Imports UTS_Core.DebugLog
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
        Private stringLen_Limit As String = ""
        Private Char_VerfiyMode As String = ""
        Private ReturnString_Tobe_Processed As String = ""

        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))

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

            If TestCommand.Parameter.Count > 6 Then '判断参数数量，以便新版本软件与旧版本testplan兼容
                stringLen_Limit = Trim(TestCommand.Parameter(6))
                Char_VerfiyMode = Trim(TestCommand.Parameter(7))
            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



            ''接收正常,处理数据是否在期望范围内
            '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

            ''接收正常,处理数据是否在期望范围内
            ''Momo 2022-11-14 对于UTS_WR_Text_String 命令，判断标准改为包含关系，即：返回字符串包含目标字符串即算pass
            ''接收正常,处理数据是否在期望范围内
            'CommandReturn.RecordValue = receiveData.ParamListToString()
            'CommandReturn.ExecuteResult = CompareFunction.StringContain_Momo(CommandReturn.RecordValue, CommandReturn.LowerLimit, CommandReturn.UpperLimit)

            'If CommandReturn.ExecuteResult Then
            '    CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围内"
            'Else
            '    CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围外"
            'End If
            '‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’’
            'Momo 2023-03-07 增加格式判断
            ReturnString_Tobe_Processed = receiveData.ParamListToString()
            '最长只取255个字节
            If ReturnString_Tobe_Processed.Length >= 255 Then ReturnString_Tobe_Processed = Left(ReturnString_Tobe_Processed, 255)

            '上下限包含判断
            If CompareFunction.StringContain_Momo(ReturnString_Tobe_Processed, CommandReturn.LowerLimit, CommandReturn.UpperLimit) = True Then
                Dim tmpLenLowerlimit As String = ""
                Dim tmpLenUpperlimit As String = ""
                Dim tmpLimt() As String = Split(stringLen_Limit, ",")
                If tmpLimt.Count >= 2 Then
                    tmpLenLowerlimit = Trim(tmpLimt(0))
                    tmpLenUpperlimit = Trim(tmpLimt(1))
                End If
                '’If String.IsNullOrEmpty(tmpLenLowerlimit) AndAlso String.IsNullOrEmpty(tmpLenUpperlimit) Then '上下限都是合法数据
                '对比字符串长度（长度符合上下限范围返回 TRUE，长度上下限为空白返回 TRUE）
                If CompareFunction.StringLengthCheck(ReturnString_Tobe_Processed, tmpLenLowerlimit, tmpLenUpperlimit) = True Then
                    '逐个字符格式比对
                    If CompareFunction.StringEachCharCheck(ReturnString_Tobe_Processed, Char_VerfiyMode) = True Then
                        CommandReturn.ExecuteResult = True
                        CommandReturn.RecordValue = ReturnString_Tobe_Processed
                        CommandReturn.ExecuteResultTipString = "输入框显示成功"
                    Else
                        CommandReturn.ExecuteResult = False
                        CommandReturn.RecordValue = ReturnString_Tobe_Processed
                        CommandReturn.ExecuteResultTipString = $"返回值 {ReturnString_Tobe_Processed},长度：{ReturnString_Tobe_Processed.Length}, 逐字符格式匹配失败,匹配模式： {Char_VerfiyMode} "
                    End If
                Else
                    CommandReturn.ExecuteResult = False
                    CommandReturn.RecordValue = ReturnString_Tobe_Processed
                    CommandReturn.ExecuteResultTipString = $"返回值 {ReturnString_Tobe_Processed},长度：{ReturnString_Tobe_Processed.Length}, 不在 {tmpLenLowerlimit} ~ {tmpLenUpperlimit} 之间"
                End If
                'End If
            Else
                CommandReturn.ExecuteResult = False
                CommandReturn.RecordValue = ReturnString_Tobe_Processed
                CommandReturn.ExecuteResultTipString = $"执行串口通讯收发正常，返回值 {ReturnString_Tobe_Processed},长度：{ReturnString_Tobe_Processed.Length }, 未包含： {CommandReturn.LowerLimit} 或 { CommandReturn.UpperLimit} "
            End If




            Return CommandReturn
        End Function

    End Class
End Namespace