

Imports Telephone.PhoneBurstification

Public Class PhoneBurstification

    Enum PhoneBuffType
        '拨打电话
        CallPhone = 1
        '发送短信
        SendSMS = 2
        '打电话并播放预定内容 = 3
        CallPhoneAndPlay = 3

    End Enum

    Enum comdType
        '拨打电话
        comdCallPhone = 1
        '发送短信
        comdSendSMS = 2
        '打电话并播放预定内容 = 3
        comdCallPhoneAndPlay = 3
        'CDTAM 设置TTS模式 1远程0本地
        comdCDTAM
        '
        comdCMGF
        comdCMGS
        comdmsg
        comd1A
    End Enum

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="PhoneBuffType"> 操作类型 </param>
    ''' <param name="PhoneNum"> 电话号码 </param>
    ''' <param name="PhoneBuffContent"> 内容 </param>
    Public Shared Function PhoneBurstification(ByVal PhoneBuffType As PhoneBuffType, callnode As CallInfoNode) As List(Of SendBuffnode)
        If callnode Is Nothing Then Return Nothing
        Dim PhoneBuffList As New List(Of SendBuffnode)
        Select Case PhoneBuffType
            Case PhoneBuffType.CallPhone
                PhoneBuffList = CallPhone(callnode)
            Case PhoneBuffType.SendSMS
                PhoneBuffList = SendSMS(callnode)
            Case PhoneBuffType.CallPhoneAndPlay
                PhoneBuffList = CallPhoneAndPlay(callnode)
        End Select
        Return PhoneBuffList
    End Function
    ''' <summary>
    ''' 拨打电话
    ''' </summary>
    ''' <param name="callnode"></param>
    ''' <returns></returns>
    Public Shared Function CallPhone(callnode As CallInfoNode) As List(Of SendBuffnode)
        Dim PhoneBuffList As New List(Of SendBuffnode)
        Dim buff As String = $"{callnode.PhoneNumber};{vbCrLf}"
        Dim SendBuffNode As New SendBuffnode(comdType.comdCallPhone, buff, 1000, 1, 100, 30000, True)
        PhoneBuffList.Add(SendBuffNode)
        Return PhoneBuffList
    End Function
    ''' <summary>
    ''' 发送短信
    ''' </summary>
    ''' <param name="callnode"></param>
    ''' <returns></returns>
    Public Shared Function SendSMS(callnode As CallInfoNode) As List(Of SendBuffnode)
        Dim PhoneBuffList As New List(Of SendBuffnode)
        '打电话
        Dim content As String
        Dim buff As String
        Dim SendBuffNode As SendBuffnode
        buff = $"AT+CMGF = 0{vbCrLf}"
        SendBuffNode = New SendBuffnode(comdType.comdCMGF, buff, 1000, 1, 100, 2000, False)
        PhoneBuffList.Add(SendBuffnode)

        content = GetSendSMSContent(callnode)
        Dim len As Integer = (content.Length \ 2) - 1

        buff = $"AT+CMGS = {len.ToString()}{vbCrLf}"
        SendBuffNode = New SendBuffnode(comdType.comdCMGS, buff, 1000, 1, 100, 2000, False)
        PhoneBuffList.Add(SendBuffnode)

        buff = $"{content}"
        SendBuffNode = New SendBuffnode(comdType.comdmsg, buff, 1000, 1, 100, 2000, False)
        PhoneBuffList.Add(SendBuffnode)

        buff = $"{Chr(26)}{vbCrLf}"
        SendBuffNode = New SendBuffnode(comdType.comd1A, buff, 1000, 1, 100, 2000, False)
        PhoneBuffList.Add(SendBuffNode)
        Return PhoneBuffList
    End Function

    Public Shared Function CallPhoneAndPlay(callnode As CallInfoNode) As List(Of SendBuffnode)
        Dim PhoneBuffList As New List(Of SendBuffnode)
        '打电话
        Dim content As String
        Dim buff As String = $"ATD{callnode.PhoneNumber};{vbCrLf}"
        Dim SendBuffNode As New SendBuffnode(comdType.comdCallPhone, buff, 1000, 1, 100, 60000, True)
        PhoneBuffList.Add(SendBuffNode)
        'CDTAM 设置TTS模式 1远程0本地
        buff = $"AT+CDTAM=1{vbCrLf}"
        SendBuffNode = New SendBuffnode(comdType.comdCDTAM, buff, 1000, 1, 100, 1000, False)
        PhoneBuffList.Add(SendBuffNode)
        '播放内容
        buff = $"AT+CTTS=2,{callnode.Content}{vbCrLf}"
        SendBuffNode = New SendBuffnode(comdType.comdCallPhoneAndPlay, buff, 1000, 1, 100, 60000, False)
        PhoneBuffList.Add(SendBuffNode)

        '发送短信


        'buff = $"AT+CMGF = 0{vbCrLf}"
        'SendBuffNode = New SendBuffnode(comdType.comdCMGF, buff, 1000, 1, 100, 1000, False)
        'PhoneBuffList.Add(SendBuffNode)

        'content = GetSendSMSContent(callnode)
        'Dim len As Integer = (content.Length \ 2) - 1

        'buff = $"AT+CMGS = {len.ToString()}{vbCrLf}"
        'SendBuffNode = New SendBuffnode(comdType.comdCMGS, buff, 1000, 1, 100, 1000, False)
        'PhoneBuffList.Add(SendBuffNode)

        'buff = $"{content}"
        'SendBuffNode = New SendBuffnode(comdType.comdmsg, buff, 1000, 1, 100, 1000, False)
        'PhoneBuffList.Add(SendBuffNode)

        'buff = $"{Chr(26)}{vbCrLf}"
        'SendBuffNode = New SendBuffnode(comdType.comd1A, buff, 1000, 1, 100, 1000, False)
        'PhoneBuffList.Add(SendBuffNode)

        Return PhoneBuffList
    End Function




    '获取短信发送内容
    Public Shared Function GetSendSMSContent(callnode As CallInfoNode) As String
        Dim result As String = ""
        Dim munberstr As String = publicMode.SwapAndPad(callnode.PhoneNumber)
        Dim content As String = publicMode.ConvertToUnicode(callnode.Content)
        Dim len As Integer = content.Length \ 2
        '将len转换成16进制字符串
        Dim lenstr As String = len.ToString("X2")
        result = $"0011000D9168{munberstr}0008C4{lenstr}{content}"
        Return result
    End Function



End Class

Public Class SendBuffnode
    '命令类型
    Public CommandType As Integer

    '发送数据
    Public SendData As String
    '发送时间
    Public SendTime As DateTime
    '发送次数
    Public SendCount As Integer
    '发送状态
    Public SendStatus As Boolean
    '发送超时
    Public SendTimeout As Integer
    '当前发送次数
    Public SendCurrentCount As Integer
    '发送间隔
    Public SendInterval As Integer
    '是否需要回复
    Public NeedReply As Boolean




    '接收数据
    Public ReceiveData As String
    '接收结果
    Public ReceiveResult As Boolean
    '接收时间
    Public ReceiveTime As DateTime
    '接收次数
    Public ReceiveCount As Integer
    '接收状态
    Public ReceiveStatus As Boolean
    '接收超时
    Public ReceiveTimeout As Integer

    Enum SendStatustype
        '待发送
        WaitSend = 0
        '已经发送等待回复
        Sending
        '重发数据
        Resend
        '发送超时
        SendTimeout
        '接收成功
        ReceiveSuccess
        '接收超时
        ReceiveTimeout
    End Enum


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Data"></param>
    ''' <param name="Timeout"></param>
    ''' <param name="SendCount"></param>
    ''' <param name="nSendInterval"></param>
    Sub New(condtype As Integer, Data As String, Optional Timeout As Integer = 300, Optional nSendCount As Integer = 1, Optional nSendInterval As Integer = 100, Optional nReceiveTimeout As Integer = 300, Optional nNeedReply As Boolean = True)
        CommandType = condtype
        SendData = Data
        'SendTime = Now
        SendStatus = 0
        SendCount = nSendCount
        SendTimeout = Timeout
        SendCurrentCount = 0
        SendInterval = nSendInterval
        ReceiveStatus = 0
        ReceiveTimeout = nReceiveTimeout
    End Sub
    Public Function ReceiveStatusStateMachine(bode As SendBuffnode, voice As Byte()) As Boolean
        '判断发送状态是否处于
        If bode.SendStatus = 0 Then Return False
        Dim readstr As String = publicMode.ByteToString(voice)
        Select Case bode.CommandType
            Case comdType.comdCallPhone
                If readstr.Contains("VOICE CALL: END") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = False
                    Return True
                End If
                If readstr.Contains("VOICE CALL: BEGIN") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = True
                    Return True
                End If

            Case comdType.comdCDTAM
                If readstr.Contains("OK") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = True
                    Return True
                End If

            Case comdType.comdCallPhoneAndPlay
                If readstr.Contains("VOICE CALL: END") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = False
                    bode.SendStatus = 0
                    Return True
                End If

                If readstr.Contains("OK") OrElse readstr.Contains("ERROR") OrElse readstr.Contains("AT+CTTS") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = True
                    Return True
                End If


            Case comdType.comdCMGF, comdType.comd1A
                If readstr.Contains("OK") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = True
                    Return True
                End If
            Case comdType.comdCMGS
                If readstr.Contains("AT+CMGS =") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = True
                    Return True
                End If
            Case comdType.comdmsg
                If bode.SendData.Contains(readstr) Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = True
                    Return True
                End If
            Case comdType.comdmsg

            Case Else
                If readstr.Contains("OK") Then
                    bode.ReceiveStatus = 1
                    bode.ReceiveData = readstr
                    bode.ReceiveResult = True
                    Return True
                End If

        End Select



        Return False
    End Function

    '判断接收是否超时
    Public Function IsNodeReceiveTimeout(bode As SendBuffnode) As Boolean
        '判断接收是否超时
        If bode.SendTime.AddMilliseconds(ReceiveTimeout) > Now Then
            '判断接收是否成功
            'If bode.ReceiveStatus = 1 Then
            '    Return False
            'Else

            Return True
            'End If
        Else
            If bode.ReceiveStatus = True Then
                Return True
            End If
            Return False
        End If

    End Function
    Public Function IsNodeReceiveTimeout2(bode As SendBuffnode) As Boolean
        '判断接收是否超时
        If bode.SendTime.AddMilliseconds(ReceiveTimeout) > Now Then
            '判断接收是否成功
            'If bode.ReceiveStatus = 1 Then
            '    Return False
            'Else

            Return True
            'End If
        Else
            Return False
        End If

    End Function


    '设置发送状态
    Public Shared Function SetSendStatus(bode As SendBuffnode, Status As SendStatustype) As SendBuffnode
        Select Case Status
            Case SendStatustype.WaitSend
                bode.SendStatus = 0
            Case SendStatustype.Sending
                bode.SendStatus = 1
                bode.SendTime = Now
                bode.SendCurrentCount = bode.SendCurrentCount + 1
            Case SendStatustype.Resend
                bode.SendStatus = 2
                bode.SendTime = Now
                bode.SendCurrentCount = bode.SendCurrentCount + 1
                bode.ReceiveResult = 0
                bode.ReceiveData = ""
                bode.ReceiveResult = False
            Case SendStatustype.ReceiveSuccess
                bode.SendStatus = 4


        End Select

    End Function

End Class