Announcement

Collapse
No announcement yet.

How to program a trigger ???

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

  • How to program a trigger ???

    Hi,

    I try to implement a trigger into a plugin I am developing, based on the example plugin files, but I don't know how to get it working.
    See my "Trigger' region code in the hspi.vb I have now.
    I can create the trigger in an event as it, so this part is working well. But how does my code 'know' this trigger exist and check if the trigger does occur ???

    Many thanks in advance.
    Code:
    #Region "Triggers"
        Public ReadOnly Property HasConditions(ByVal TriggerNumber As Integer) As Boolean Implements HomeSeerAPI.IPlugInAPI.HasConditions
            Get
                Select Case TriggerNumber
                    Case 1
                        Return False
                    Case 2
                        Return True
                    Case Else
                        Return False
                End Select
            End Get
        End Property
    
        Sub SetTriggers()
            Dim o As Object = Nothing
            If triggers.Count = 0 Then
                triggers.Add(o, "Partition")
                triggers.Add(o, "Zone")
            End If
        End Sub
    
        Public Function TriggerTrue(ByVal TrigInfo As HomeSeerAPI.IPlugInAPI.strTrigActInfo) As Boolean Implements HomeSeerAPI.IPlugInAPI.TriggerTrue
            Return True
        End Function
    
        Public ReadOnly Property HasTriggers() As Boolean Implements HomeSeerAPI.IPlugInAPI.HasTriggers
            Get
                SetTriggers()
                Return IIf(triggers.Count > 0, True, False)
            End Get
        End Property
    
        Public ReadOnly Property SubTriggerCount(ByVal TriggerNumber As Integer) As Integer Implements HomeSeerAPI.IPlugInAPI.SubTriggerCount
            Get
                Dim trigger As trigger
                If ValidTrig(TriggerNumber) Then
                    trigger = triggers(TriggerNumber - 1)
                    If Not (trigger Is Nothing) Then
                        Return trigger.Count
                    Else
                        Return 0
                    End If
                Else
                    Return 0
                End If
            End Get
        End Property
    
        Public ReadOnly Property SubTriggerName(ByVal TriggerNumber As Integer, ByVal SubTriggerNumber As Integer) As String Implements HomeSeerAPI.IPlugInAPI.SubTriggerName
            Get
                Dim trigger As trigger
                If ValidSubTrig(TriggerNumber, SubTriggerNumber) Then
                    trigger = triggers(TriggerNumber)
                    Return IFACE_NAME & ": " & trigger.Keys(SubTriggerNumber)
                Else
                    Return ""
                End If
            End Get
        End Property
    
        Public Function TriggerBuildUI(ByVal sUnique As String, ByVal TrigInfo As HomeSeerAPI.IPlugInAPI.strTrigActInfo) As String Implements HomeSeerAPI.IPlugInAPI.TriggerBuildUI
            Dim stb As New StringBuilder
            Dim CaddxPartition As String = ""
            Dim CaddxEvent As String = ""
            Dim dd As New clsJQuery.jqDropList("PartitionT1" & sUnique, PageName, True)
            Dim dd1 As New clsJQuery.jqDropList("EventT1" & sUnique, PageName, True)
            Dim sKey As String
            Dim i
    
            dd.autoPostBack = True
            dd.AddItem("--Please Select--", "", False)
            dd1.autoPostBack = True
            dd1.AddItem("--Please Select--", "", False)
    
            If Not (TrigInfo.DataIn Is Nothing) Then
                DeSerializeObject(TrigInfo.DataIn, trigger)
            Else 'new event, so clean out the trigger object
                trigger = New trigger
            End If
    
            For Each sKey In trigger.Keys
                Select Case True
                    Case InStr(sKey, "PartitionT1") > 0
                        CaddxPartition = trigger(sKey)
                    Case InStr(sKey, "EventT1") > 0
                        CaddxEvent = trigger(sKey)
                End Select
            Next
    
            For i = 1 To 8
                dd.AddItem(i.ToString, i.ToString, (i.ToString = CaddxPartition))
            Next
    
            stb.Append("Select Partition:")
            stb.Append(dd.Build)
    
            'For Each item In Commands.Keys
            dd1.AddItem("Armed Stay", "Armed Stay", "Armed Stay" = CaddxEvent)
            dd1.AddItem("Armed Away", "Armed Away", "Armed Away" = CaddxEvent)
            dd1.AddItem("Disarmed", "Disarmed", "Disarmed" = CaddxEvent)
            dd1.AddItem("Siren On", "Siren On", "Siren On" = CaddxEvent)
            dd1.AddItem("Fire", "Fire", "Fire" = CaddxEvent)
            dd1.AddItem("Entry Timer 1 Started", "Entry Timer 1 Started", "Entry Timer 1 Started" = CaddxEvent)
            dd1.AddItem("Entry Timer 1 Expired", "Entry Timer 1 Expired", "Entry Timer 1 Expired" = CaddxEvent)
            dd1.AddItem("Entry Timer 2 Started", "Entry Timer 2 Started", "Entry Timer 2 Started" = CaddxEvent)
            dd1.AddItem("Entry Timer 2 Expired", "Entry Timer 2 Expired", "Entry Timer 2 Expired" = CaddxEvent)
            dd1.AddItem("Exit Timers Started", "Exit Timers Started", "Exit Timers Started" = CaddxEvent)
            dd1.AddItem("Exit Timer 1 Expired", "Exit Timer 1 Expired", "Exit Timer 1 Expired" = CaddxEvent)
            dd1.AddItem("Exit Timer 2 Expired", "Exit Timer 2 Expired", "Exit Timer 2 Expired" = CaddxEvent)
            dd1.AddItem("No Connection", "No Connection", "No Connection" = CaddxEvent)
            dd1.AddItem("Troubled", "Troubled", "Troubled" = CaddxEvent)
            dd1.AddItem("Ready", "Ready", "Ready" = CaddxEvent)
            dd1.AddItem("Faulted", "Faulted", "Faulted" = CaddxEvent)
            dd1.AddItem("Any Fault", "Any Fault", "Any Fault" = CaddxEvent)
    
            stb.Append("Select Event:")
            stb.Append(dd1.Build)
    
            Return stb.ToString
        End Function
    
        Public ReadOnly Property TriggerConfigured(ByVal TrigInfo As HomeSeerAPI.IPlugInAPI.strTrigActInfo) As Boolean Implements HomeSeerAPI.IPlugInAPI.TriggerConfigured
            Get
                Dim Configured As Boolean = False
                Dim sKey As String
                Dim itemsConfigured As Integer = 0
                Dim itemsToConfigure As Integer = 2
    
                If Not (TrigInfo.DataIn Is Nothing) Then
                    DeSerializeObject(TrigInfo.DataIn, trigger)
                    For Each sKey In trigger.Keys
                        Select Case True
                            Case InStr(sKey, "PartitionT1") > 0 AndAlso trigger(sKey) <> ""
                                itemsConfigured += 1
                            Case InStr(sKey, "EventT1") > 0 AndAlso trigger(sKey) <> ""
                                itemsConfigured += 1
                        End Select
                    Next
                    If itemsConfigured = itemsToConfigure Then Configured = True
                End If
                Return Configured
            End Get
        End Property
    
        Public Function TriggerReferencesDevice(ByVal TrigInfo As HomeSeerAPI.IPlugInAPI.strTrigActInfo, ByVal dvRef As Integer) As Boolean Implements HomeSeerAPI.IPlugInAPI.TriggerReferencesDevice
            Return False
        End Function
    
        Public Function TriggerFormatUI(ByVal TrigInfo As HomeSeerAPI.IPlugInAPI.strTrigActInfo) As String Implements HomeSeerAPI.IPlugInAPI.TriggerFormatUI
            Dim stb As New StringBuilder
            Dim sKey As String
            Dim CaddxPartition As String = ""
            Dim CaddxEvent As String = ""
    
            If Not (TrigInfo.DataIn Is Nothing) Then
                DeSerializeObject(TrigInfo.DataIn, trigger)
            End If
    
            For Each sKey In trigger.Keys
                Select Case True
                    Case InStr(sKey, "PartitionT1") > 0
                        CaddxPartition = trigger(sKey)
                    Case InStr(sKey, "EventT1") > 0
                        CaddxEvent = trigger(sKey)
    
                End Select
            Next
    
            stb.Append(" the system detected the " & CaddxEvent & " event ")
            stb.Append("for Partition " & CaddxPartition)
    
            Return stb.ToString
        End Function
    
        Public ReadOnly Property TriggerName(ByVal TriggerNumber As Integer) As String Implements HomeSeerAPI.IPlugInAPI.TriggerName
            Get
                If Not ValidTrig(TriggerNumber) Then
                    Return ""
                Else
                    Return IFACE_NAME & ": " & Instance & ": " & triggers.Keys(TriggerNumber - 1)
                End If
            End Get
        End Property
    
        Public Function TriggerProcessPostUI(ByVal PostData As System.Collections.Specialized.NameValueCollection, _
                                             ByVal TrigInfoIn As HomeSeerAPI.IPlugInAPI.strTrigActInfo) As HomeSeerAPI.IPlugInAPI.strMultiReturn Implements HomeSeerAPI.IPlugInAPI.TriggerProcessPostUI
            Dim Ret As New HomeSeerAPI.IPlugInAPI.strMultiReturn
    
            Ret.sResult = ""
            ' We cannot be passed info ByRef from HomeSeer, so turn right around and return this same value so that if we want, 
            '   we can exit here by returning 'Ret', all ready to go.  If in this procedure we need to change DataOut or TrigInfo,
            '   we can still do that.
            Ret.DataOut = TrigInfoIn.DataIn
            Ret.TrigActInfo = TrigInfoIn
    
            If PostData Is Nothing Then Return Ret
            If PostData.Count < 1 Then Return Ret
    
            If Not (TrigInfoIn.DataIn Is Nothing) Then
                DeSerializeObject(TrigInfoIn.DataIn, trigger)
            End If
    
            Dim parts As Collections.Specialized.NameValueCollection
    
            Dim sKey As String
    
            parts = PostData
            Try
                For Each sKey In parts.Keys
                    If sKey Is Nothing Then Continue For
                    If String.IsNullOrEmpty(sKey.Trim) Then Continue For
                    Select Case True
                        Case InStr(sKey, "PartitionT1") > 0, InStr(sKey, "EventT1") > 0
                            trigger.Add(CObj(parts(sKey)), sKey)
                    End Select
                Next
                If Not SerializeObject(trigger, Ret.DataOut) Then
                    Ret.sResult = IFACE_NAME & " Error, Serialization failed. Signal Action not added."
                    Return Ret
                End If
            Catch ex As Exception
                Ret.sResult = "ERROR, Exception in Action UI of " & IFACE_NAME & ": " & ex.Message
                Return Ret
            End Try
    
            ' All OK
            Ret.sResult = ""
            Return Ret
        End Function
    
        Public ReadOnly Property TriggerCount As Integer Implements HomeSeerAPI.IPlugInAPI.TriggerCount
            Get
                SetTriggers()
                Return triggers.Count
            End Get
        End Property
    
    #End Region
    Regards,

    Rien du Pre
    The Netherlands
    Using:
    Homeseer PRO 3.0.0.435 on MacOS - Running a Mac Mini
    Plugin's:
    RFXCOM, Caddx, BLShutdown, BLLogMonitor, BLRadar, BLBackup, Log2Text, BLLAN, Z-Wave

  • #2
    Whenever an event happens in your plugin (say the temperature rises) and you think a trigger could exist on the basis of that event (if a temperature rise trigger existed for example) you call callback.TriggerMatches passing in the correct parameters. This will return an array of strTrigActInfo depending on how many triggers it finds - careful to check if it actually returns any.

    You then need to go through the array and deserialise the object contained in TrigInfo.DataIn if you are storing data here. Then find out whether or not this data matches what your event initially was raised for, if so you then call callback.TriggerFire passing in the trigger info. Then your event should run.
    My Plugins:

    Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
    Yeelight 3P | Nanoleaf 3P

    Comment


    • #3
      Originally posted by mrhappy View Post
      Whenever an event happens in your plugin (say the temperature rises) and you think a trigger could exist on the basis of that event (if a temperature rise trigger existed for example) you call callback.TriggerMatches passing in the correct parameters. This will return an array of strTrigActInfo depending on how many triggers it finds - careful to check if it actually returns any.

      You then need to go through the array and deserialise the object contained in TrigInfo.DataIn if you are storing data here. Then find out whether or not this data matches what your event initially was raised for, if so you then call callback.TriggerFire passing in the trigger info. Then your event should run.
      Can anyone share some detailed code. The whole actions and trigger functionality is the most complex part if plugin building and I really don't know why they made it so difficult.
      Regards,

      Rien du Pre
      The Netherlands
      Using:
      Homeseer PRO 3.0.0.435 on MacOS - Running a Mac Mini
      Plugin's:
      RFXCOM, Caddx, BLShutdown, BLLogMonitor, BLRadar, BLBackup, Log2Text, BLLAN, Z-Wave

      Comment


      • #4
        Originally posted by Rien du Pre View Post
        Can anyone share some detailed code. The whole actions and trigger functionality is the most complex part if plugin building and I really don't know why they made it so difficult.
        I am not getting into the politics but I have the same opinion. I don't know if this will honestly help or not but this is the sort of stuff I am doing with triggers (I may not be necessarily doing them very well).

        Code:
        Log("CheckSimpleValue: " & DVRef, LogLevel.Debug)
        
                    Dim TrigsToCheck() As IAllRemoteAPI.strTrigActInfo
                    Dim ToFire As Boolean = False
        
                    Try
                        TrigsToCheck = callback.TriggerMatches(IFACE_NAME, 1, -1) 'check both trig numbers but why is it returning triggers with trigger 1 when they are 2? 
                        'Log("TrigsToCheck Count: " & TrigsToCheck.Length, LogLevel.Debug)
                    Catch ex As Exception : Log("TrigsToCheck Exception: " & ex.Message, LogLevel.Debug)
                    End Try
        
                    If TrigsToCheck IsNot Nothing Then 'warning is OK, it tests whether or not it is nothing
        
                        For Each TrigInfo In TrigsToCheck
        
                            Dim UID As String = TrigInfo.UID.ToString
                            Dim trigger As New trigger
        
                            DeSerializeObject(TrigInfo.DataIn, trigger)
        
                            For Each TrigValue As String In trigger.Values
                                Log("trigger.Values: " & TrigValue, LogLevel.Debug)
                            Next
        
                            If Convert.ToInt32(trigger.Values(0)) = DVRef Or Convert.ToInt32(trigger.Values(2)) = DVRef Then 'this should look for the fact either device has changed
        
                                Dim dv1 As Double = 0
                                Dim dv2 As Double = 0
        
                                If Convert.ToInt32(trigger.Values(0)) = DVRef Then
                                    dv1 = Convert.ToDouble(hs.DeviceValueEx(trigger.Values(0)))
                                    dv2 = Convert.ToDouble(hs.DeviceValueEx(trigger.Values(2)))
                                Else
                                    dv1 = Convert.ToDouble(hs.DeviceValueEx(trigger.Values(2)))
                                    dv2 = Convert.ToDouble(hs.DeviceValueEx(trigger.Values(0)))
                                End If
        
                                Log("Simple Comparison", LogLevel.Debug)
        
                                Select Case trigger.Values(1).ToString.ToLower
                                    Case "greater than" : If dv1 > dv2 Then ToFire = True
                                    Case "less than" : If dv1 < dv2 Then ToFire = True
                                    Case "equal to" : If dv1 = dv2 Then ToFire = True
                                    Case "greater than or equal to" : If dv1 >= dv2 Then ToFire = True
                                    Case "less than or equal to" : If dv1 <= dv2 Then ToFire = True
                                End Select
        
                                If ToFire = True Then
                                    Log("Event Found - We Are Firing", LogLevel.Debug)
                                    callback.TriggerFire(IFACE_NAME, TrigInfo)
                                Else : Log("Event Not Firing", LogLevel.Debug)
                                End If
        
                            End If
                        Next
                    End If
        My Plugins:

        Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
        Yeelight 3P | Nanoleaf 3P

        Comment


        • #5
          Thanks !!!

          I have a working setup now....

          Thanks to your help I figured this out much faster...
          Regards,

          Rien du Pre
          The Netherlands
          Using:
          Homeseer PRO 3.0.0.435 on MacOS - Running a Mac Mini
          Plugin's:
          RFXCOM, Caddx, BLShutdown, BLLogMonitor, BLRadar, BLBackup, Log2Text, BLLAN, Z-Wave

          Comment

          Working...
          X