Imports System.Text
Imports UTS_Core.UTSModule.Test.StatusMonitor

Namespace UTSModule.Test.Command.UtsComPortCommand
    Public Class UtsWrTextBytesExecutor
        Inherits TestCommandExecutor

        Private ReadOnly _receiverAddress As String
        Private ReadOnly _utsCommand As String
        Private ReadOnly _utsExpression 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 = TestCommand.Parameter(2)
            _timeout = CInt(TestCommand.Parameter(3))

            _utsExpression = TestCommand.Parameter(4)

            CommandReturn.LowerLimit = TestCommand.Parameter(5)
            CommandReturn.UpperLimit = TestCommand.Parameter(6)
            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
            If UtsComportTask.ReceivedTestCommandReturn(_timeout, receiveData) = TestCommandStatusMonitor.TestCommandStatusEnum.ReceiveTimeout Then

                If String.IsNullOrEmpty(_utsExpression) Then
                    CommandReturn.RecordValue = String.Empty
                    CommandReturn.ExecuteResult = True
                    CommandReturn.ExecuteResultTipString = "执行串口通讯发送正常,接受超时，无表达式默认通过"
                Else
                    CommandReturn.RecordValue = String.Empty
                    CommandReturn.ExecuteResult = False
                    CommandReturn.ExecuteResultTipString = "执行串口通讯接收异常"
                End If

                Return CommandReturn
            End If

            If String.IsNullOrEmpty(_utsExpression) Then
                CommandReturn.RecordValue = receiveData.ParamListToString
                CommandReturn.ExecuteResult = True
                CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常,无表达式默认通过"

                Return CommandReturn
            End If

            '考虑多个表达式的结果用冒号分隔,填充到结果列表中
            Dim strExpression As New List(Of String)
            strExpression.AddRange(_utsExpression.Split(":"c))

            Dim valueExpression As New List(Of String)

            '转换接收，string转换为byte数组,byte通讯默认只会返回一个数组
            Dim tmp As String() = receiveData.Params(0).Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
            Dim bytes(tmp.Length - 1) As Byte
            For j As Integer = 0 To tmp.Length - 1
                bytes(j) = CByte($"&H{tmp(j)}")
            Next

            Dim recordBuild As New StringBuilder
            For i As Integer = 0 To strExpression.Count - 1
                Dim str As String = strExpression.Item(i)

                If String.IsNullOrWhiteSpace(str) Then
                    valueExpression.Add(String.Empty)
                    Continue For
                End If

                str = str.Replace(" ", String.Empty)

                str = Expression.StringExpression.ReplaceBytes(bytes, str)

                '表达式为空或无效则默认不需要比较
                If String.IsNullOrWhiteSpace(str) Then
                    valueExpression.Add(String.Empty)
                    Continue For
                End If

                If Expression.StringExpression.CheckExpressionString(str) = False Then
                    valueExpression.Add(String.Empty)
                    Continue For
                End If

                Dim result As String

                Try
                    result = Expression.StringExpression.GetDoubleExpressionResult(str).ToString() '保留2位小数
                Catch ex As Exception
                    result = String.Empty
                End Try

                valueExpression.Add(result)

                recordBuild.Append(result)
                If i < strExpression.Count - 1 Then recordBuild.Append(":"c)
            Next

            '接收正常,处理表达式结果数据是否在期望范围内
            CommandReturn.RecordValue = recordBuild.ToString()
            CommandReturn.ExecuteResult = CompareFunction.ParamDoubleListCompare(valueExpression, CommandReturn.LowerLimit, CommandReturn.UpperLimit)
            If CommandReturn.ExecuteResult Then
                CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围内"
            Else
                CommandReturn.ExecuteResultTipString = "执行串口通讯收发正常，数据在有效范围外"
            End If

            Return CommandReturn
        End Function
    End Class
End Namespace