Announcement

Collapse
No announcement yet.

How To Access HS3 from IIS web page/server

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

  • How To Access HS3 from IIS web page/server

    For those who are maintaining there own web pages on their own or hosted web servers using IIS asp.net, and would like to add HS3 automation to their web pages, here is what is needed.

    Your IIS Web Application or Web Site must contain an Global.Asax file with event routines that run: (1) when your application is first loaded and run to connect to HS3 and create the .hs object (2) for each time a page is loaded, retrieve the .hs object from first step, (3) when your application is unloaded by server, disconnect and destroy all used resources.

    Here is example using VS 2015 Community version:
    HTML Code:
    Global.Asax
    
    <%@ Application Language="VB" %>
    <%@ Import Namespace="Scheduler" %>
    <%@ Import Namespace="HomeSeerAPI" %>
    <%@ Import Namespace="HSCF.Communication.Scs.Communication.EndPoints.Tcp" %>
    <%@ Import Namespace="HSCF.Communication.ScsServices.Client" %>
    <%@ Import Namespace="HSCF.Communication.ScsServices.Service" %>
    
    <script runat="server">
        Shared ghs As HomeSeerAPI.IHSApplication
        Public WithEvents client As HSCF.Communication.ScsServices.Client.IScsServiceClient(Of IHSApplication)
        Dim Hs3IpAddress As String = "192.168.1.71"
    
        Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
            '' Fires when the application is started
            '
            '' Get HS objects as this is the development environment
            ghs = OpenHsConnection(Hs3IpAddress)
        End Sub
    
        Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
            '' Fires when the application is ending
            '
            '' Disconnect and release resources
            ghs.WriteLog("WebAp Sample", "WebAp Sample disconnecting from HS3")
            CloseHsConnection()
        End Sub
    
        Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
            '' Fires at the beginning of each request
            '
            '' Add HS object to Context for the web pages
            Context.Items.Add("HsObject", ghs)
        End Sub
    
        Private Function OpenHsConnection(ipAddress As String) As HomeSeerAPI.IHSApplication
            Dim host As HomeSeerAPI.IHSApplication
            Dim hs As HomeSeerAPI.IHSApplication
            Dim Attempts As Integer
    
            client = ScsServiceClientBuilder.CreateClient(Of IHSApplication)(New ScsTcpEndPoint(ipAddress, 10400))
            Attempts = 1
    TryAgain:
            Try
                client.Connect()
                host = client.ServiceProxy
                Dim APIVersion As Double = host.APIVersion  ' will cause an error if not really connected
            Catch ex As Exception
                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
                Return Nothing
            End Try
            Try
                ' create the user object, accessed from each Web Page
                hs = host
                hs.WriteLog("WebAp Sample", "WebAp Sample Connection to HS3 Successful")
            Catch ex As Exception
                Return Nothing
            End Try
            Return hs
        End Function
    
        Private Sub CloseHsConnection()
            ' disconnect from server for good here
            client.Disconnect()
            If client IsNot Nothing Then
                client.Dispose()
                client = Nothing
            End If
        End Sub
    
        Private Sub client_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs) Handles client.Disconnected
            CloseHsConnection()
        End Sub
    </script>

    Here is a very simple Default.aspx page code behind that just retrieves and displays the HomeSeer version info from the HS3 server:

    Code:
    Default.aspx.vb
    
    Partial Class _Default
        Inherits System.Web.UI.Page
        Friend hs As HomeSeerAPI.IHSApplication
        Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            '' Get HS object
            hs = Context.Items("HsObject")
            '' Retrieve and display HS Server Version
            If hs Is Nothing Then
                Response.Write("Could not connect to HS3")
            Else
                Response.Write("HomeSeer Version = " & hs.Version)
            End If
        End Sub
    End Class
    This should be enough to point you in the correct direction.

    I've attached the complete set of web site files if needed.

    tenholde
    Attached Files
    tenholde

  • #2
    @tenholde,

    Unrelated to IIS here....

    Goofing around a bit here yesterday configured my Homeseer 3 Buntu 14.04 64 bit box with Apache2 (V2.4.7) to include dot net stuff. Baby steps here as I am also doing this and that. I have other stuff running on the Apache 2 side concurrent with Homeseer 3 running its own stuff.

    I have not yet tried to run anything and just did the configuration and install stuff relating to mono at this time.
    Last edited by Pete; August 25th, 2015, 10:11 PM.
    - Pete

    Auto mator
    Homeseer 3 Pro - 3.0.0.534 (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU - Mono 6.4X
    Homeseer Zee2 (Lite) - 3.0.0.534 (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro - Mono 6.4X

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation.

    Comment

    Working...
    X