Announcement

Collapse
No announcement yet.

Basic connection to run a remote script

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

    Basic connection to run a remote script

    I am trying to establish a connection to HS3 from a remote system so that I can run basic script commands from the remote machine. I have been successful in creating a "Mock Plugin" to establish a connection, however 95% of the code involved is completely unnecessary for what I am trying to do.

    All I need to do is:

    1) Establish connection to HS3 from the remote system
    2) Run a basic script command, like: hs.TriggerEvent("EventName")

    What would be the easiest way to establish a connection without having to initialize a plugin every time?

    #2
    You will need an application of some sort, a console app being the easiest. The easiest way I have found is to open HSPI_SAMPLE_BASIC and remove everything except for main.vb. This vb file will handle all of the communications with the new HS connection interface.

    You will end up with a couple of errors but get something like this, I have removed any ability to specify command line arguments but you may want to do that if you want to pass the event name in by command line.

    Code:
    Imports Scheduler
    Imports HomeSeerAPI
    Imports HSCF.Communication.Scs.Communication.EndPoints.Tcp
    Imports HSCF.Communication.ScsServices.Client
    Imports HSCF.Communication.ScsServices.Service
    
    Module Main
        Public WithEvents client As HSCF.Communication.ScsServices.Client.IScsServiceClient(Of IHSApplication)
        Dim WithEvents clientCallback As HSCF.Communication.ScsServices.Client.IScsServiceClient(Of IAppCallbackAPI)
    
        Private host As HomeSeerAPI.IHSApplication
        Public callback As HomeSeerAPI.IAppCallbackAPI
        Public hs As HomeSeerAPI.IHSApplication
    
    
        Sub Main()
            Dim sIp As String = "127.0.0.1"
    
            Console.WriteLine("Connecting to server at " & sIp & "...")
            client = ScsServiceClientBuilder.CreateClient(Of IHSApplication)(New ScsTcpEndPoint(sIp, 10400))
            clientCallback = ScsServiceClientBuilder.CreateClient(Of IAppCallbackAPI)(New ScsTcpEndPoint(sIp, 10400))
    
            Dim Attempts As Integer = 1
    
    TryAgain:
            Try
                client.Connect()
                clientCallback.Connect()
    
                host = client.ServiceProxy
                Dim APIVersion As Double = host.APIVersion  ' will cause an error if not really connected
    
                callback = clientCallback.ServiceProxy
                APIVersion = callback.APIVersion  ' will cause an error if not really connected
            Catch ex As Exception
                Console.WriteLine("Cannot connect attempt " & Attempts.ToString & ": " & ex.Message)
                If ex.Message.ToLower.Contains("timeout occurred.") Then
                    Attempts += 1
                    If Attempts < 6 Then GoTo TryAgain
                End If
    
                If client IsNot Nothing Then
                    client.Dispose()
                    client = Nothing
                End If
                If clientCallback IsNot Nothing Then
                    clientCallback.Dispose()
                    clientCallback = Nothing
                End If
                wait(4)
                Return
            End Try
    
            Try
                ' create the user object that is the real plugin, accessed from the pluginAPI wrapper
                callback = callback
                hs = host
                ' connect to HS so it can register a callback to us
                'host.Connect(IFACE_NAME, "")
                Console.WriteLine("Connected, waiting to be initialized...")
    
                hs.WriteLog("TestRemote", "This is only a test connection...")
    
                Threading.Thread.Sleep(500)
    
                hs.WriteLog("TestRemote", "I'm off sorry...")
    
                'Do
                'Threading.Thread.Sleep(30)
                'Loop While client.CommunicationState = HSCF.Communication.Scs.Communication.CommunicationStates.Connected
    
                ' disconnect from server for good here
                client.Disconnect()
                clientCallback.Disconnect()
                wait(2)
                End
            Catch ex As Exception
                Console.WriteLine("Cannot connect(2): " & ex.Message)
                wait(2)
                End
                Return
            End Try
    
        End Sub
    
        Private Sub client_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs) Handles client.Disconnected
            Console.WriteLine("Disconnected from server - client")
        End Sub
    
        Private Sub wait(ByVal secs As Integer)
            Threading.Thread.Sleep(secs * 1000)
        End Sub
    End Module

    Comment


      #3
      That totally worked. Thanks for your help mrhappy!

      I'm not really a programmer so I was having a heck of a time reverse engineering HSPI_SAMPLE_BASIC to not add all the plugin stuff. This can probably be streamlined still but it's plenty to get me going right now.

      Comment


        #4
        For anybody else who might want to do this, I streamlined the code a bit.

        Just add the following in a file named HSConnection.vb in your favorite development environment:
        Code:
        Imports Scheduler
        Imports HomeSeerAPI
        Imports HSCF.Communication.Scs.Communication.EndPoints.Tcp
        Imports HSCF.Communication.ScsServices.Client
        Imports HSCF.Communication.ScsServices.Service
        
        Module HSConnection
            Public WithEvents client As HSCF.Communication.ScsServices.Client.IScsServiceClient(Of IHSApplication)
            Dim WithEvents clientCallback As HSCF.Communication.ScsServices.Client.IScsServiceClient(Of IAppCallbackAPI)
        
            Private host As HomeSeerAPI.IHSApplication
            Public callback As HomeSeerAPI.IAppCallbackAPI
            Public hs As HomeSeerAPI.IHSApplication
        
        
            Sub Open(ipAddress As String, Optional displayStatus As Boolean = True)
                Dim sIp As String = ipAddress
        
                If displayStatus Then Console.WriteLine("Connecting to server at " & sIp & "...")
                client = ScsServiceClientBuilder.CreateClient(Of IHSApplication)(New ScsTcpEndPoint(sIp, 10400))
                clientCallback = ScsServiceClientBuilder.CreateClient(Of IAppCallbackAPI)(New ScsTcpEndPoint(sIp, 10400))
        
                Dim Attempts As Integer = 1
        
        TryAgain:
                Try
                    client.Connect()
                    clientCallback.Connect()
        
                    host = client.ServiceProxy
                    Dim APIVersion As Double = host.APIVersion  ' will cause an error if not really connected
        
                    callback = clientCallback.ServiceProxy
                    APIVersion = callback.APIVersion  ' will cause an error if not really connected
                Catch ex As Exception
                    If displayStatus Then Console.WriteLine("Cannot connect attempt " & Attempts.ToString & ": " & ex.Message)
                    If ex.Message.ToLower.Contains("timeout occurred.") Then
                        Attempts += 1
                        If Attempts < 6 Then GoTo TryAgain
                    End If
        
                    If client IsNot Nothing Then
                        client.Dispose()
                        client = Nothing
                    End If
                    If clientCallback IsNot Nothing Then
                        clientCallback.Dispose()
                        clientCallback = Nothing
                    End If
                    If displayStatus Then Threading.Thread.Sleep(4000)
                    Return
                End Try
        
                Try
                    ' create the user object, accessed from the pluginAPI wrapper
                    callback = callback
                    hs = host
                    ' connect to HS so it can register a callback to us
                    If displayStatus Then Console.WriteLine("Connection successful.")
                    hs.WriteLog("RemoteApp", "Connection Successful")
        
                Catch ex As Exception
                    If displayStatus Then
                        Console.WriteLine("Cannot connect(2): " & ex.Message)
                        Threading.Thread.Sleep(2000)
                    End If
                    End
                    Return
                End Try
            End Sub
            Public Sub Close(Optional DisplayStatus As Boolean = True)
                ' disconnect from server for good here
                hs.WriteLog("RemoteApp", "Disconnected")
                client.Disconnect()
                clientCallback.Disconnect()
                If DisplayStatus Then
                    Console.WriteLine("HomeSeer Connection Closed")
                    Threading.Thread.Sleep(2000)
                End If
        
            End Sub
            Private Sub client_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs) Handles client.Disconnected
                'Console.WriteLine("Disconnected from server - client")
            End Sub
        End Module
        Be sure to have HomeSeerAPI.dll, HSCF.dll, and Scheduler.dll on the remote machine and add those as references to your Visual Studio (or other IDE) project.

        Then, you can use these two commands in your script to open/close the connection to HS:

        HSConnection.Open("your HomeSeer IP address")
        HSConnection.Close()

        By default the status of the connection will display in a console window. If you prefer your script to run silently, then add "False" as an optional parameter like so:

        HSConnection.Open("your HomeSeer IP address", False)
        HSConnection.Close(False)

        Here is a very simple example of how a Main() sub might look:

        Code:
        Module Main
            Sub Main()
                'Open Connection to HomeSeer server
                'Use HSConnection.Open("ipaddress", False) to prevent console window messages
                HSConnection.Open("192.168.0.100")
        
                'Your script here
                hs.WriteLog("RemoteApp", "Your Script is now running.")
        
                'Close HS Connection
                'Use HSConnection.Close(False) to prevent console window messages
                HSConnection.Close()
            End Sub
        End Module
        Disclaimer: I am more of a hack than a programmer so I can't really tell you everything that is going on here. Thanks to mrhappy for making this possible.

        Comment


          #5
          Nice work, one thing I am wondering whether you might want to convert HSConnection.Open into a function rather than a sub. You could return a boolean value depending on whether the HS connection attempt was successful or unsuccessful, the only reason I think behind that is if you connect and it fails and then try and use the hs. object I am wondering whether it will crash or at the very least error?

          Comment


            #6
            Hi!

            I was controlling all my devices on HS2 remotely by script before (from the same PC Homeseer was installed on) and it worked great. Now with HS3 everything seems to have changed. Im trying to call HS3 using the vb.net code (from a VBScript) mrhappy and galoobus provided, but I can't simply connect to HS3. I've copied HomeSeerAPI.dll, HSCF.dll, and Scheduler.dll into the same folder where the remote script resides. I get error messages on row 8 (baloobus) "end of instruction are expected". Maybe the dlls aren't loaded ok. If so how to do that?

            Please I need help!

            /Majstang

            Comment


              #7
              I have a basic remote script connector for HS3 on my site if you are interested.
              Jon

              Comment


                #8
                Jon00, you are a lifesaver That works very well. Now I can begin the big re-write of the scripts.

                Comment


                  #9
                  Old but useful thread! Thanks.

                  mr. happy / galoobus - how do we extend this framework to get events from hs3 working.

                  i.e. I wish to capture:
                  1. device events
                  2. device added/deleted events
                  3. event events
                  4. event added/deleted events


                  Must be a way! Thanks.

                  Comment


                    #10
                    Originally posted by skavan View Post
                    Old but useful thread! Thanks.

                    mr. happy / galoobus - how do we extend this framework to get events from hs3 working.

                    i.e. I wish to capture:
                    1. device events
                    2. device added/deleted events
                    3. event events
                    4. event added/deleted events


                    Must be a way! Thanks.
                    Can you explain a bit more of what you are after? Do you mean be able to from an external program determine whether or not there have been new devices/events added/remove from HS?

                    Comment


                      #11
                      Originally posted by mrhappy View Post
                      Can you explain a bit more of what you are after? Do you mean be able to from an external program determine whether or not there have been new devices/events added/remove from HS?
                      So - Using the above scaffolding, I have built a vb.net app, that connects to Homeseer, Requests Devices, Transforms said devices into easier to manage objects and lets you edit data en-masse and post it back.

                      But my app's (which may end up as a plugin) real purpose is to act as a bridge between a number of IP controllable devices (that Homeseer doesn't have plugins for)...such as Smart AC's and so on.

                      Once my app (through a tcp client/server) receives messages, it parses them and will send out actions or event requests to HS3.

                      But I need to tie into Homeseer Events so I can detect devices being updated or added and see other events that I might want to act on.

                      Like I said, eventually, I will probably roll this into a plugin, but for now, I just want to get as much event info triggered by HS3 as possible.

                      s.

                      Comment


                        #12
                        Just to add to my own post!
                        To be explicit, I am trying to implement:
                        Code:
                        RegisterEventCB(Enums.HSEvent.LOG, ????, ???)
                        In a standalone exe.

                        I can connect to HS using a derivative of the code above....but I don;t know how (and if its possible) to subscribe to events without being a plugin.

                        If it is possible --- how!?!?!?

                        Comment


                          #13
                          Originally posted by skavan View Post
                          Just to add to my own post!
                          To be explicit, I am trying to implement:
                          Code:
                          RegisterEventCB(Enums.HSEvent.LOG, ????, ???)
                          In a standalone exe.

                          I can connect to HS using a derivative of the code above....but I don;t know how (and if its possible) to subscribe to events without being a plugin.

                          If it is possible --- how!?!?!?
                          I'm not sure you could, when you register the callback it will call back into the HSEvent routine in a plugin. You could try and implement a similar sub (Public Sub HSEvent(ByVal EventType As Enums.HSEvent, ByVal parms() As Object) Implements HomeSeerAPI.IPlugInAPI.HSEvent) however I don't know if there is something that it is expecting to send it to a particular plugin name that your solution will not have (or any other sort of API type bits).

                          The way of connecting in this thread really isn't advertised as something supported or advised by HS (they would probably advise where possible to talk to HS through the JSON interface) hence the method is likely viable only for certain functions outside of a plugin.

                          Comment


                            #14
                            Dear Mr Happy,

                            Having spent some time looking at information on how to connect to HS3, this was just what I was looking for, 5 years after you posted it(!)

                            The intention is to develop C# code in a 3rd party dev tool such as MonoDevelop, even if it ends up being cut and pasted back in to the event (minus the hs object instantiation which is automatically injected).

                            Having to write and test code from within HS3 would kill me. So far only managed to connect to HS and write to the log. Will provide a C# version of your code if it is useful to anyone.

                            Many thanks

                            Dom

                            Comment


                              #15
                              Originally posted by Duomi8 View Post
                              Dear Mr Happy,

                              Having spent some time looking at information on how to connect to HS3, this was just what I was looking for, 5 years after you posted it(!)

                              The intention is to develop C# code in a 3rd party dev tool such as MonoDevelop, even if it ends up being cut and pasted back in to the event (minus the hs object instantiation which is automatically injected).

                              Having to write and test code from within HS3 would kill me. So far only managed to connect to HS and write to the log. Will provide a C# version of your code if it is useful to anyone.

                              Many thanks

                              Dom

                              Providing that C# example wouldn't hurt. I know I am personally much more comfortable with C# over VB.

                              Comment

                              Working...
                              X