Imports System.Text

Namespace UTSModule.Test
    Public Class UtsComPortData
        Private Shared _separator As Char = ":"c

        ''' <summary>
        ''' 发送方地址
        ''' </summary>
        ''' <returns></returns>
        Property SenderAddress() As String

        ''' <summary>
        ''' 接收方地址
        ''' </summary>
        ''' <returns></returns>
        Property ReceiverAddress() As String

        ''' <summary>
        ''' UTS通讯命令
        ''' </summary>
        ''' <returns></returns>
        Property Command() As String

        ''' <summary>
        ''' UTS命令参数
        ''' </summary>
        ''' <returns></returns>
        Property Params() As List(Of String)


        Sub New()
            Params = New List(Of String)
        End Sub

        ''' <summary>
        ''' 测试命令常用初始化方式
        ''' </summary>
        ''' <param name="utsAddress"></param>
        ''' <param name="utsCmd"></param>
        ''' <param name="utsParamString"></param>
        Sub New(utsAddress As String, utsCmd As String, utsParamString As String)
            SenderAddress = DeviceAddress.LocalAddress()
            ReceiverAddress = utsAddress
            Command = utsCmd
            Params = StringToParamList(utsParamString)
        End Sub


        Sub New(utsAddress As String, utsCmd As String)
            SenderAddress = DeviceAddress.LocalAddress()
            ReceiverAddress = utsAddress
            Command = utsCmd
            Params = New List(Of String)
        End Sub


        ''' <summary>
        ''' 发送后台通讯命令常用初始化方式
        ''' </summary>
        ''' <param name="utsAddress"></param>
        ''' <param name="utsCmd"></param>
        ''' <param name="utsParam"></param>
        Sub New(utsAddress As String, utsCmd As String, utsParam As List(Of String))
            SenderAddress = DeviceAddress.LocalAddress()
            ReceiverAddress = utsAddress
            Command = utsCmd
            Params = utsParam
        End Sub


        ''' <summary>
        ''' 重置当前对象的信息
        ''' </summary>
        Public Sub Reset()
            SenderAddress = String.Empty
            ReceiverAddress = String.Empty
            Command = String.Empty
            Params.Clear()
        End Sub

        ''' <summary>
        ''' 深度拷贝数据, 创建当前对象的克隆
        ''' </summary>
        ''' <returns></returns>
        Public Function Clone() As UtsComPortData
            Dim uc As New UtsComPortData
            uc.Command = Command
            uc.SenderAddress = SenderAddress
            uc.ReceiverAddress = ReceiverAddress
            uc.Params.AddRange(Params.ToArray())
            Return uc
        End Function

        ''' <summary>
        ''' 将参数字符串处理成参数列表
        ''' </summary>
        ''' <returns></returns>
        Public Shared Function StringToParamList(paramString As String) As List(Of String)
            Dim strings() As String = paramString.Split(New Char() {_separator}, StringSplitOptions.RemoveEmptyEntries)
            Return New List(Of String)(strings)
        End Function


        Public Function ParamListToString() As String
            If Params.Count = 0 Then Return String.Empty
            Dim str As New StringBuilder
            str.Append(Params.Item(0))
            For i As Integer = 1 To Params.Count - 1
                str.Append(_separator)
                str.Append(Params.Item(i))
            Next

            Return str.ToString()
        End Function


        ''' <summary>
        ''' 将必须通讯参数转换为uts通讯字符串
        ''' </summary>
        ''' <param name="fromAddress">发送方地址</param>
        ''' <param name="toAddress">接收发地址</param>
        ''' <param name="command">命令字</param>
        ''' <param name="params">命令参数</param>
        ''' <param name="separator">组合命令分隔符</param>
        ''' <returns></returns>
        Public Shared Function ConvertToString(fromAddress As String, toAddress As String, command As String, params As List(Of String), Optional separator As Char = ":"c) As String
            Dim strTemp As New StringBuilder
            strTemp.Append(fromAddress)
            strTemp.Append(separator)
            strTemp.Append(toAddress)
            strTemp.Append(separator)
            strTemp.Append(command)

            For Each param As String In params
                strTemp.Append(separator)
                strTemp.Append(param)
            Next

            Return strTemp.ToString()
        End Function


        ''' <summary>
        ''' 将字符串按格式转换为通讯格式数据对象
        ''' </summary>
        ''' <param name="dataParamString">uts通讯字符串</param>
        ''' <param name="separator">组合命令分隔符</param>
        ''' <returns></returns>
        Public Shared Function ConvertFromString(dataParamString As String, Optional separator As Char = ":"c) As UtsComPortData
            Dim strings() As String
            strings = dataParamString.ToUpper().Split(separator)
            If strings.Length < 3 Then Throw New Exception($"FromString：非法字符串,{dataParamString}")

            Dim dataParam As New UtsComPortData
            dataParam.SenderAddress = strings(0)
            dataParam.ReceiverAddress = strings(1)
            dataParam.Command = strings(2)

            For idx As Integer = 3 To strings.Length - 1
                dataParam.Params.Add(strings(idx))
            Next

            Return dataParam
        End Function

        ''' <summary>
        ''' 解析uts通讯字符串，初始化内部数据
        ''' </summary>
        ''' <param name="dataParamString"></param>
        Public Sub FromString(dataParamString As String)
            Dim strings() As String
            strings = dataParamString.ToUpper().Split(_separator)
            If strings.Length < 3 Then Throw New Exception($"FromString：非法字符串,{dataParamString}")

            SenderAddress = strings(0)
            ReceiverAddress = strings(1)
            Command = strings(2)

            Params.Clear()
            For idx As Integer = 3 To strings.Length - 1
                Params.Add(strings(idx))
            Next
        End Sub

        ''' <summary>
        ''' 将本地数据内容，转换为uts通讯字符串
        ''' </summary>
        ''' <returns></returns>
        Public Overrides Function ToString() As String
            Return ConvertToString(SenderAddress, ReceiverAddress, Command, Params, _separator)
        End Function
    End Class

End Namespace