No announcement yet.

How To Access HS3 from IIS web page/server

  • 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, 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:
    <%@ 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 = ""
        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")
        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
                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 = Nothing
                End If
                Return Nothing
            End 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
            If client IsNot Nothing Then
                client = Nothing
            End If
        End Sub
        Private Sub client_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs) Handles client.Disconnected
        End Sub

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

    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")
                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.

    Attached Files


    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 25, 2015, 10:11 PM.
    - Pete

    Auto mator
    Homeseer 3 Pro - (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU 16Gb
    Homeseer Zee2 (Lite) - (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro
    HS4 Lite - Ubuntu 20.04 / VB W7e Jetway JBC420U591
    Fanless IntelĀ® Celeron N3160 SoC 8Gb
    HS4 Pro - V4.1.18.1 - Ubuntu 20.04/VB W7e 64 bit Intel Kaby Lake CPU - 32Gb
    HSTouch on Intel tabletop tablets

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation-Tasmota-Espurna. OmniPro 2, Russound zoned audio, Smartthings hub, Hubitat Hub, and Home Assistant