Announcement

Collapse
No announcement yet.

Question about keeping telnet connection open

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Question about keeping telnet connection open

    I have a script that connects to an Asterisk PBX and checks to see if there are any voicemails waiting. Currently I open the connection, check, and then close the connection. I was wondering how I can do the same and leave the connection open to continually check for activity. With a COM port I can use the hs.opencomport command and then specify a script file to deal with the activity that comes over the COM port. Is there a similar way to do this with an IP connection? my code is below if it helps:

    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    
    ' Userid: admin
    ' Password: amp111
    
    REM ACTION: LOGIN
    REM USERNAME: username
    REM SECRET: password
    REM EVENTS: ON 
    
    Sub Main(parms As Object)
        Telnet
    end sub
     
    Sub Telnet()
        dim Reply as string
        dim messages as string
        dim strpos
        dim old_messages, new_messages as string
        dim old_messages_num, new_messages_num
        dim length, strpos1, strpos2
        
        Const RemoteIP As String = "192.168.1.170"
        Const PortNumber As Integer = 5038 
    
        ' Connect to the Asterisk server
        Dim Client As New TcpClient()
        Client.Connect(IPAddress.Parse(RemoteIP), PortNumber)
        Dim NetStream As NetworkStream = Client.GetStream()
        Reply = ReadString(Client)
        'hs.WriteLog("debug", Reply)
        hs.WaitSecs(2)
        ' Send login command
        SendString(NetStream, "ACTION: LOGIN" & vbCrLf)
        SendString(NetStream, "USERNAME: admin" & vbCrLf)
        SendString(NetStream, "SECRET: amp111" & vbCrLf)
        SendString(NetStream, "EVENTS: ON" & vbCrLf & vbCrLf)
    
        Reply = ReadString(Client)
        'hs.WriteLog("debug", Reply)
        Reply = ReadString(Client)
        'hs.WriteLog("debug", Reply)
    
    ' Checking messages for 301
        SendString(NetStream, "ACTION: MailboxCount" & vbCrLf)
        SendString(NetStream, "MAILBOX: 301@default" & vbCrLf & vbCrLf)
        'Response: SuccessMessage: Mailbox Message CountMailbox: 301@defaultNewMessages: 4OldMessages: 0
        messages = ReadString(Client)
        'hs.WriteLog("debug", "301 : " & messages)
        
        'Reply = ReadString(Client)
        'hs.WriteLog("debug", Reply)
        REM Reply = ReadString(Client)
        REM hs.WriteLog("debug", Reply)
    
    
    ' Get old messages first:
        old_messages = right(messages, 2)
        old_messages_num = val(old_messages)
        hs.SetDeviceString("s1", old_messages_num, True)
    
    'Get new messages second:
        strpos1 = Instr(1, messages, "s:", 1)    
        strpos2 = Instr(1, messages, "Old", 1)    
        strpos1 = strpos1 + 2
        strpos2 = strpos2 - 1
        new_messages = mid(messages, strpos1, strpos2)
        'hs.writelog("Debug", "Messages : |" & messages & "| and strpos1 : " & strpos1 & " and strpos2 : " & strpos2 & " and new_messages : " & new_messages)
        new_messages_num = val(new_messages)
        hs.SetDeviceString("s2", new_messages_num, True)
    
    ' Checking messages for 302
        SendString(NetStream, "ACTION: MailboxCount" & vbCrLf)
        SendString(NetStream, "MAILBOX: 302@default" & vbCrLf & vbCrLf)
        'Response: SuccessMessage: Mailbox Message CountMailbox: 302@defaultNewMessages: 4OldMessages: 0
        messages = ReadString(Client)
        'hs.WriteLog("debug", "302 : " & messages)
      
        'Reply = ReadString(Client)
        'hs.WriteLog("debug", Reply)
        REM Reply = ReadString(Client)
        REM hs.WriteLog("debug", Reply)
    
    
    ' Get old messages first:
        strpos1 = Instr(1, messages, "OldMessages:", 1)
        strpos2 = len(messages)
        strpos1 = strpos1 + 13
        'old_messages = right(messages, 2)
        old_messages = mid(messages, strpos1, strpos2)
        'hs.writelog("Debug", "old messages so far : " & old_messages)
        old_messages_num = val(old_messages)
        hs.SetDeviceString("s3", old_messages_num, True)
        'hs.writelog("Debug", "old messages : " & old_messages_num)
        
    'Get new messages second:
        strpos1 = Instr(1, messages, "s:", 1)    
        strpos2 = Instr(1, messages, "Old", 1)    
        strpos1 = strpos1 + 2
        strpos2 = strpos2 - 1
        new_messages = mid(messages, strpos1, strpos2)
        ' hs.writelog("Debug", "Messages : |" & messages & "| and strpos1 : " & strpos1 & " and strpos2 : " & strpos2 & " and new_messages : " & new_messages)
        new_messages_num = val(new_messages)
        'hs.writelog("Debug", "new messages : " & new_messages_num)
        hs.SetDeviceString("s4", new_messages_num, True)
    
    ' Logout & Disconnect
        SendString(NetStream, "ACTION: logoff" & vbCrLf & vbCrLf)
        SendString(NetStream, vbCrLf)
        
    ' Close connection to Asterisk server
        Client.Close()
    
    End Sub
    
    Private Function ReadString(ByVal Client As System.Net.Sockets.TcpClient) As String
        Dim Stream As NetworkStream = Client.GetStream
        Dim Buffersize As Integer = Client.ReceiveBufferSize
        Dim Data(Buffersize -1) As Byte
        If Stream.CanTimeout Then
            Stream.ReadTimeout = 1000 * 15
        End If
        Dim BytesRead As Integer = Stream.Read(Data, 0, Buffersize)
        Return Encoding.ASCII.GetString(Data, 0, BytesRead)
    End Function
     
    Private Function SendString(ByVal Stream As NetworkStream, ByVal Data As String) As Boolean
        Dim Bytes As [Byte]() = Encoding.ASCII.GetBytes(Data)
        If Stream.CanTimeout Then
            Stream.WriteTimeout = 1000 * 5
        End If
        Stream.Write(Bytes, 0, Bytes.Length)
        Return True
    End Function

    #2
    Telnet Port listening

    Simon,

    Did you ever found a way for this. I'm facing the same issue. I can send command to a Denon amp and request status on regular basis but I'd like to have a more dynamic approach, as you described.

    Thanks

    Michel

    Comment


      #3
      Sorry, I didn't. Do you connect to the Denon amp through a telnet connection or through a traditional COM connection? I gave up on the Asterisk connection - the API is extremely limited.

      Comment

      Working...
      X