No announcement yet.

Telnet Callback Engine similar to hs.OpenComPort

  • Filter
  • Time
  • Show
Clear All
new posts

  • Telnet Callback Engine similar to hs.OpenComPort


    I would like to use telnet to communicate much in the same way as I already do on a serial port. Commands are sent depending on feedback received from a server. A dynamic dialogue.

    The code below (almost) works, but if there is a cleaner approach similar to hs.OpenComPort, I would like it.

    PROBLEM: I would like to call the TelnetSend function from other scripts or events when the engine is running, but cannot.
    I have also tried to move it to a separate script.

    I feel unsure how to declare Public functions (classes/modules?). Is this the problem?
    I can't reference "TelnetClient" from another script.

    Running script C:\Program Files (x86)\HomeSeer HS3\scripts\Telnet.vb :Exception has been thrown by the target of an invocation.->Does entry point TelnetSend exist in script? at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeIn ternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Scheduler.clsRunVBNetScript.ExecuteScript()
    ' Acknowledgement to Bart for original DJ Telnet script
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    Public TelnetClient As New TcpClient()    'System.Net.Sockets.TcpClient
    Sub Main(ByVal Parms As Object)
        Const LogLabel as String = "Telnet"
        Const RemoteIP As String = "192.168.1.xx"
        Const PortNumber As Integer = yy
        Dim Reply As String
        Dim Buffernum As Integer
        ' --- Connect (user/password turned off for development)
        TelnetClient.Connect(IPAddress.Parse(RemoteIP), PortNumber)
        hs.SaveVar("Telnet", TelnetClient.Connected) 'A Boolean variable to enable/stop the callback engine
        ' --- Get first device response after connecting
        Reply = ReadString(TelnetClient)
        hs.WriteLog(LogLabel , "Connected:" & Reply)
        ' --- Send some test commands
    '    TelnetSend("*Z6Power" & vbCrLf)    
        TelnetSend("*VER" & vbCrLf)    
        ' --- The Callback engine
        Do While hs.GetVar("Telnet") = True
            BufferNum = BufferNum + 1 ' For debugging purposes. (The loop does not run wild.)
            Reply = ReadString(TelnetClient)
            If Reply.Length > 2 Then
                hs.WriteLog(LogLabel , BufferNum & ": " & Reply)
    '            hs.RunScript(”TelnetCallback.vb”, Reply, True, True)
            End if
            Reply = ""
        ' --- Close connection
        hs.WriteLog(LogLabel , "Disconected")
    End Sub
    ' --- Function to process received data
    Private Function ReadString(ByVal Client As System.Net.Sockets.TcpClient) As String
        Dim Buffersize As Integer = TelnetClient.ReceiveBufferSize
        Dim Data(Buffersize -1) As Byte
        Dim TelnetStream As NetworkStream = TelnetClient.GetStream
        If TelnetStream.CanTimeout Then
            TelnetStream.ReadTimeout = 1000 * 15
        End If
        Dim BytesRead As Integer = TelnetStream.Read(Data, 0, Buffersize)
        Return Encoding.ASCII.GetString(Data, 0, BytesRead)
    End Function
    ' --- Function to process send data
    Public Function TelnetSend(ByVal Data As String) As Boolean
        hs.WriteLog("LogLabel ", Data)
        Dim Bytes As [Byte]() = Encoding.ASCII.GetBytes(Data)
        Dim TelnetStream As NetworkStream = TelnetClient.GetStream
        If TelnetStream.CanTimeout Then
            TelnetStream.WriteTimeout = 1000 * 5
        End If
        TelnetStream.Write(Bytes, 0, Bytes.Length)
        Return True
    End Function