www.homeseer.com  
 

Go Back   HomeSeer Message Board > Developer Support > Programming with HomeSeer

Programming with HomeSeer Discussions related to custom programming using the HomeSeer ActiveX interface. This includes Visual Basic, ASP, and C++.

Reply
 
Thread Tools Display Modes
  #1  
Old June 4th, 2011, 01:30 AM
manxam manxam is offline
Seer Deluxe
 
Join Date: Oct 2009
Location: Canada
Posts: 272
Default 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
Reply With Quote
  #2  
Old June 4th, 2011, 02:49 AM
mrhappy mrhappy is offline
OverSeer
 
Join Date: Nov 2007
Location: UK
Posts: 4,314
Default

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?
Reply With Quote
  #3  
Old June 4th, 2011, 01:03 PM
heatvent's Avatar
heatvent heatvent is offline
Seer Master
 
Join Date: Nov 2007
Location: Chicago
Posts: 1,112
Default

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.
__________________
PC Hardware: Combined HS and SageTV Server on a ESXi 5.1 desktop, AMD FX-6300 3.5ghz Six-Core, 12GB RAM
PC OS: ESXi 5.1 VM Server, WHS 2011 64-bit for server/SageTV, WinXP for HS2
HS Version: HS2 Pro 2.latest
HA Hardware: Insteon Hub, Aeon Labs Z-Stick II, W800RF, RF8315, HAI RC-122 Thermostat, Insteon, X10, Z-Wave, Way2Call, SqueezeBox Boom (2) and Radio, Schlage Locks (2)
Reply With Quote
  #4  
Old June 4th, 2011, 01:22 PM
jasv's Avatar
jasv jasv is offline
Super Seer
 
Join Date: Feb 2001
Location: Southern Cal, USA
Posts: 2,310
Default

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
__________________
James

Running HS 2.4 on winXP WMWare guest .
KillProcess, mcsDeviceGrid, UPB, BLBackup, DooMenuBar, AC-RF2 Processor ,RFXcom, ZWave Thermostats, Script Connector, HSTouch Server, jvCaddx Plug-In, mcsSprinklers, mcsSprinklersB, mcsTemperature, ADIOcelot, UltraMon, UltraLog

Last edited by jasv; June 4th, 2011 at 01:42 PM.
Reply With Quote
  #5  
Old June 4th, 2011, 08:35 PM
manxam manxam is offline
Seer Deluxe
 
Join Date: Oct 2009
Location: Canada
Posts: 272
Default

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
Reply With Quote
  #6  
Old June 5th, 2011, 04:08 PM
mrhappy mrhappy is offline
OverSeer
 
Join Date: Nov 2007
Location: UK
Posts: 4,314
Default

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...
Reply With Quote
  #7  
Old June 5th, 2011, 04:22 PM
manxam manxam is offline
Seer Deluxe
 
Join Date: Oct 2009
Location: Canada
Posts: 272
Default

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!
Reply With Quote
  #8  
Old June 5th, 2011, 05:11 PM
mrhappy mrhappy is offline
OverSeer
 
Join Date: Nov 2007
Location: UK
Posts: 4,314
Default

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...
Reply With Quote
  #9  
Old June 7th, 2011, 12:43 AM
manxam manxam is offline
Seer Deluxe
 
Join Date: Oct 2009
Location: Canada
Posts: 272
Default

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
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 10:59 AM.

Untitled Document
NEW!
New How-To's
New Products

HomeSeer Z-Troller"
MyHomeSeer-Connect Service

Copyright HomeSeer Technologies, LLC