Announcement

Collapse
No announcement yet.

VB TCP/IP read / listen help

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

    VB TCP/IP read / listen help

    I'm no programmer but I've managed to write a few simple scripts and even managed to cobble together some code to read and write to a com port but the whole tcp/ip thing seems WAY over my head.

    I've scoured HS' forum but only find partial examples and the majority are for sending data; few for receiving.

    What I'm trying to do is connect to a raw socket and listen to all data. Once a given value is found then do X. I can do the later but can't figure out the whole connect, listen, read using tcp/ip and what few examples I can find online are crude in that they don't have a try/reconnect if the socket isn't available.

    If someone could help shed some light on this for me and give me a brief walkthrough I'd really appreciate it!

    Thanks, M

    #2
    Although i've only ever sent commands by the System.Net.Sockets class (google some vb.net tutorials to give some examples) i've never had to receive them, I have to wonder whether its possible by a script as i'm not sure it has a way the class has to call back into a script (not 100% on this though), a plugin yes - this might explain the lack of examples for HS scripts. What sort of commands are you sending, is there anything that could be done with a webpage?

    Comment


      #3
      I know there is a script callback for com ports but not sure for tcp/ip. May need to do a plugin instead.

      I do have some code for tcp/ip that I haven,t used that I will try to post later.

      Comment


        #4
        Here is an example I have in a script to check the health of HS. If you need any explanation let me know.

        Code:
         
                Dim client As New System.Net.Sockets.TcpClient
                Try
                    Dim myMessage As String = 'all_ok_msg"
                    Dim Buffer() As Byte = System.Text.Encoding.Default.GetBytes(myMessage.ToCharArray)
                    Try
                        client.ReceiveTimeout = 10000
                        client.SendTimeout = 10000
                        client.Connect(HS_RemoteWatchdog_IP_address, 8112)
                    Catch ex As Exception
                        hs.WriteLog("jv_hs_heartbeat", "Error - No Remote watchdog server found")
                        hs.WriteLog("jv_hs_heartbeat", "Error - Tried Connecting to " & HS_RemoteWatchdog_IP_address & ", 8112")
                        client.Close()
                        Exit Sub
                    End Try
                    client.SendBufferSize = 6
                    client.GetStream.Write(Buffer, 0, Buffer.Length)
                    hs.WriteLog("jv_hs_heartbeat", "Remote Watchdog Server was notified that HS is alive")
                    hs.WaitSecs(1)
                    If client.GetStream.CanRead Then
                        Dim bytes(client.ReceiveBufferSize) As Byte
                        Dim dataLength As Int32
                        dataLength = client.GetStream.Read(bytes, 0, client.ReceiveBufferSize)
                        Dim myData As String = String.Empty
                        myData = Encoding.ASCII.GetString(bytes, 0, dataLength)
                        hs.ExecX10ByName("ScriptFlags Remote Watchdog", "on")
                        hs.SetDeviceLastChange("f64", Now)
                        ' myData = myData.Substring(0, dataLength)
                        hs.SetDeviceStringByName("ScriptFlags Remote Watchdog", myData)
                        hs.WriteLog("jv_hs_heartbeat", "The remote server sent = " & myData.Trim)
                        If myData.Trim.ToLower = "restarted hs" Then
                            hs.SendEmail("jasvi@renefe.com", "homeseer@renefe.com", _
                                        "Remote restart", "RemoteWatchdog restarted Homeseer", )
                        End If
                    End If
                    client.Close()
                Catch ex As Exception
                    hs.WriteLog("jv_hs_heartbeat", "Remote jvError = " & ex.Message)
                    hs.ExecX10ByName("ScriptFlags Remote Watchdog", "off")
                End Try
            End Sub
        Last edited by jasv; June 4, 2011, 12:42 PM.
        James

        Running HS 3 on Win10 .

        Comment


          #5
          Thanks guys and especially Jasv as that's the best example that I've seen thus far. It appears though that you're connecting to a socket, getting a small amount of data, and then disconnecting?

          I'll let you guys know what I'm TRYING to do: XBMC sends its play/pause/stop status via JSON-RPC on a raw socket and I'd like HS to monitor this and tell me if I'm watching a movie (lights down), pause (lights up slightly), or stopped (lights on full). While I can also connect via http and request this info periodically, I can also just connect to the port via telnet and watch the status change immediately. I'm basically trying to have HS do the later. I understand that this will be an example of a 'blocking port' using the system.net.sockets.tcpclient but I only expect to have one client connected.

          Keep the information coming!

          Thanks again,
          M

          Comment


            #6
            I think its plugin time, or having a very small extra application that you could write that ran in the system tray. I don't think there is anyway of letting this run in the background with a script (unless you ran it every 30secs or so and could check the socket by sending commands? You would end with a delay if you paused/stopped the move though) ust doing a quick search reveals there is no plugin at the moment for XBMC unfortunately...

            Comment


              #7
              MyHappy, thanks for the response. It seems that I keep coming up with the same answer Could someone more versed in VB than me tell me WHY I can keep a com port open in the background (open on HS start, close on HS stop) and continually read data from it and generate events based upon the data but I can't do it for tcp/ip? I had thought that tcp/ip would be a more robust protocol.

              I can monitor the socket using Eventghost and have EG generate HS events through python (calling HS.) or via HTTP but had hoped I didn't need to have a 3rd party intervene.

              Thanks for all of the help!

              Comment


                #8
                HomeSeer has its own in built COM port control and 'behind the scenes' shall we say in HS is dealing with the COM port callbacks and when you open the comport you specify a script and procedure that HS should call when it gets serial data. Thats a function that HS have implemented, the same does not exist for a TCP/IP port.

                If you look at the .NET System.IO.Ports class then you could not use this in a script as you open the port and then there is a seperate event handler to handle the serial data. On the other side of the coin you cannot use hs.opencomport in a plugin as there is no script/procedure to call into, you then have to use the Ports class.

                My understanding is that when you run a script it will run from Sub Main to End Sub and then 'dispose' of it so if you opened the TCP/IP port the script would end up finishing and any subroutines would never be called, you might be able to implement some form of wait command but i'm not sure if this would work well.

                Don't take my advice as gospel though, its just my uneducated and amateur wannabe programmer idea on the matter...

                Comment


                  #9
                  Thanks MrHappy. What've you said has made a lot of sense. I guess I'll continue to use eventghost along with homeseer for this particular case.

                  Cheers,
                  M

                  Comment

                  Working...
                  X