Announcement

Collapse
No announcement yet.

[VB.NET] updateEvent healper functions

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

  • [VB.NET] updateEvent healper functions

    these are functions that I created to help me code easier. Just things so I would not have to keep remembering the order of variables in time math functions, or trying to remember all the caveats to setting and getting variables in HS.

    Generally they test something and return a true/false.

    It seems (for me) to make reading the code better.
    • if(hs.isoff("A2")) then

    is easy to read and was my inspiration.
    • if (datediff("s",hs.DeviceLastChange("A2"), NOW) > someTimeInput) then

    is the same as:
    • if(lastChangeGreaterThanSeconds("A2|10")

    But I can tell that the second one is looking for the last change of the device, in seconds, and it is trying to see if the time it has been in the state it is in, is greater than the time I am testing against.


    Copy this code into the updateevent.vb file...or any file you want, then use an include to the source you are working on.

    Included are functions for:
    • testing if an event is enabled/disabled
    • testing to see if a time is greater than now
    • getting/setting variables
    • is the time after/before now
    • is dark/is light (so I would not have to remember the "backwards" nature of the X10 motion sensors)
    • is closed / open (DS10A ON/OFF to human words)
    • last change for a device...that is how long has it been on/off
      • last change greater/less than minutes/seconds
    • my "is Double tap" function

    I will be adding in the is triple tap, however have not finished testing it out, as this runs my house, I'm pretty good about making sure the code is 100% before putting it into "production" use.


    Code:
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''' Support Functions '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Private Function StringFind(ByVal strSearchIn As String, ByVal strSearchFor As String) As Boolean
        If InStr(1, strSearchIn, strSearchFor, vbTextCompare) > 0 Then
            StringFind = True
        End If
    End Function
    
    Function isNowAfter(ByVal par as object)
    
       if (datediff("n",timevalue(NOW), par) < 0) then
          isNowAfter = TRUE
       else
          isNowAfter = FALSE
       end if
    
    End Function
    
    Function isNowBefore(ByVal par as object)
    
       if (datediff("n",timevalue(NOW), par) > 0) then
          isNowBefore = TRUE
       else
          isNowBefore = FALSE
       end if
    
    End Function
    
    Function isClosed(ByVal par as object)
    
       if (hs.isOff(par)) then
          isClosed = TRUE
       else
          isClosed = FALSE
       end if
    
    End Function
    
    Function isOpened(ByVal par as object)
    
       if (hs.isOn(par)) then
          isOpened = TRUE
       else
          isOpened = FALSE
       end if
    
    End Function
    
    Function isSitting(ByVal par as object)
    
       if (hs.isOff(par)) then
          isSitting = TRUE
       else
          isSitting = FALSE
       end if
    
    End Function
    
    Function isDark(ByVal par as object)
    
       if hs.isOn(par) then
          isDark = TRUE
       else
          isDark = FALSE
       end if
    
    End Function
    
    Function isLight(ByVal par as object)
    
       if hs.isOff(par) then
          isLight = TRUE
       else
          isLight = FALSE
       end if
    
    End Function
    
    Function isDectivated(ByVal par as object)
    
       if hs.devicevalue(par) = 2 then
          isDectivated = TRUE
       else
          isDectivated = FALSE
       end if
    
    End Function
    
    Function isActivated(ByVal par as object)
    
       if hs.devicevalue(par) = 1 then
          isActivated = TRUE
       else
          isActivated = FALSE
       end if
    
    End Function
    
    Function lastChangeGreaterThanSeconds(ByVal par as object)
    ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
    
       if (datediff("s",hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
          lastChangeGreaterThanSeconds = TRUE
       else
          lastChangeGreaterThanSeconds = FALSE
       end if
    
    End Function
    
    Function lastChangeLessThanSeconds(ByVal par as object)
    ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
    
       if (datediff("s", hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
          lastChangeLessThanSeconds = FALSE
       else
          lastChangeLessThanSeconds = TRUE
       end if
    
    End Function
    
    Function lastChangeGreaterThanMinutes(ByVal par as object)
    ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
    
       if (datediff("n",hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
          lastChangeGreaterThanMinutes = TRUE
       else
          lastChangeGreaterThanMinutes = FALSE
       end if
    
    End Function
    
    Function lastChangeLessThanMinutes(ByVal par as object)
    ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
    
       if (datediff("n", hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
          lastChangeLessThanMinutes = FALSE
       else
          lastChangeLessThanMinutes = TRUE
       end if
    
    End Function
    
    ' "Name not found" can not get as not created yet
    ' "" is created AOK
    ' "Name Exists" can not create as already exists
    ' hs.CreateVar("testVar")
    ' functions SaveVar and GetVar the DeleteVar
    
    Function getMyVar(ByVal par as object)
       
       if(hs.GetVar(par) = "Name not found") then
          getMyVar = "NotSet"
       else
          getMyVar = hs.GetVar(par)
       end if
       
    End Function
    
    Function setMyVar(ByVal par as object)
    
       hs.CreateVar(hs.stringitem(par, 1, "|"))
       setMyVar = hs.SaveVar(hs.stringitem(par, 1, "|"),  hs.stringitem(par, 2, "|"))
       
    End Function
    
    Function timeDiffGreaterThanSeconds(ByVal par as object)
    
       if (datediff("s",hs.stringitem(par, 1, "|"), NOW) > hs.stringitem(par, 2, "|")) then
          timeDiffGreaterThanSeconds = TRUE
       else
          timeDiffGreaterThanSeconds = FALSE
       end if
    
    End Function
    
    Function timeDiffLessThanSeconds(ByVal par as object)
    
       if (datediff("s",hs.stringitem(par, 1, "|"), NOW) > hs.stringitem(par, 2, "|")) then
          timeDiffLessThanSeconds = FALSE
       else
          timeDiffLessThanSeconds = TRUE
       end if
    
    End Function
    
    Function isDoubleTap(ByVal par as object)
       Dim lastTime
       
       lastTime = getMyVar(par & "DoubleTap")
       if(lastTime = "NotSet") then
          setMyVar(par & "DoubleTap|" & Now)
          isDoubleTap = FALSE
       elseif timeDiffGreaterThanSeconds(lastTime & "|5")
          setMyVar(par & "DoubleTap|" & Now)
          isDoubleTap = FALSE
       else
          isDoubleTap = TRUE
       end if
    
    End Function
    
    Function isEvtEnabled(ByVal par as object)
       Dim eventsEnabled
       Dim ev
       
       '' if the result of the AND is = 32 then it is disabled
       '' if the result of the AND is = 0, then it is enabled
       ev = hs.GetEventByRef(hs.GetEventRefByName(par))
       eventsEnabled = ev.Misc AND &H20 
       if(eventsEnabled = 0) then
          isEvtEnabled = TRUE
       else
          isEvtEnabled = FALSE
       end if
    
    End Function
    
    Function isEvtDisabled(ByVal par as object)
       Dim eventsEnabled
       Dim ev
       
       '' if the result of the AND is = 32 then it is disabled
       '' if the result of the AND is = 0, then it is enabled
       ev = hs.GetEventByRef(hs.GetEventRefByName(par))
       eventsEnabled = ev.Misc AND &H20 
       if(eventsEnabled = 32) then
          isEvtDisabled = TRUE
       else
          isEvtDisabled = FALSE
       end if
    
    End Function
    Tasker, to a person who does Homeautomation...is like walking up to a Crack Treatment facility with a truck full of 3lb bags of crack. Then for each person that walks in and out smack them in the face with an open bag.

  • #2
    (BTW, healper was supposed to be helper...so just pronounce it with a southern twang and it'll be OK)
    discussion thread:
    http://forums.homeseer.com/showthread.php?p=985621

    And yes, I even mispelled that so that searching might be easier
    Tasker, to a person who does Homeautomation...is like walking up to a Crack Treatment facility with a truck full of 3lb bags of crack. Then for each person that walks in and out smack them in the face with an open bag.

    Comment


    • #3
      Updated functions per Moskus:
      http://forums.homeseer.com/showpost....53&postcount=3
      Changed "old" return method to newer .Net
      "return TRUE" instead of
      "{functioname} = TRUE"

      Code:
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      '''''''''''' Support Functions '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
      Private Function StringFind(ByVal strSearchIn As String, ByVal strSearchFor As String) As Boolean
          If InStr(1, strSearchIn, strSearchFor, vbTextCompare) > 0 Then
              return True
          End If
      End Function
      Function isNowAfter(ByVal par as object)
         if (datediff("n",timevalue(NOW), par) < 0) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isNowBefore(ByVal par as object)
         if (datediff("n",timevalue(NOW), par) > 0) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isClosed(ByVal par as object)
         if (hs.isOff(par)) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isOpened(ByVal par as object)
         if (hs.isOn(par)) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isSitting(ByVal par as object)
         if (hs.isOff(par)) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isDark(ByVal par as object)
         if hs.isOn(par) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isLight(ByVal par as object)
         if hs.isOff(par) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isDectivated(ByVal par as object)
         if hs.devicevalue(par) = 2 then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isActivated(ByVal par as object)
         if hs.devicevalue(par) = 1 then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function lastChangeGreaterThanSeconds(ByVal par as object)
      ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
         if (datediff("s",hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function lastChangeLessThanSeconds(ByVal par as object)
      ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
         if (datediff("s", hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
            return FALSE
         else
            return TRUE
         end if
      End Function
      Function lastChangeGreaterThanMinutes(ByVal par as object)
      ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
         if (datediff("n",hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function lastChangeLessThanMinutes(ByVal par as object)
      ' adding 2 seconds: hs.DeviceLastChange("^9") + (2/24/3600))
         if (datediff("n", hs.DeviceLastChange(hs.stringitem(par, 1, "|")), NOW) > hs.stringitem(par, 2, "|")) then
            return FALSE
         else
            return TRUE
         end if
      End Function
      ' "Name not found" can not get as not created yet
      ' "" is created AOK
      ' "Name Exists" can not create as already exists
      ' hs.CreateVar("testVar")
      ' functions SaveVar and GetVar the DeleteVar
      Function getMyVar(ByVal par as object)
         
         if(hs.GetVar(par) = "Name not found") then
            return "NotSet"
         else
            return hs.GetVar(par)
         end if
         
      End Function
      Function setMyVar(ByVal par as object)
         hs.CreateVar(hs.stringitem(par, 1, "|"))
         return hs.SaveVar(hs.stringitem(par, 1, "|"),  hs.stringitem(par, 2, "|"))
         
      End Function
      Function timeDiffGreaterThanSeconds(ByVal par as object)
         if (datediff("s",hs.stringitem(par, 1, "|"), NOW) > hs.stringitem(par, 2, "|")) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function timeDiffLessThanSeconds(ByVal par as object)
         if (datediff("s",hs.stringitem(par, 1, "|"), NOW) > hs.stringitem(par, 2, "|")) then
            return FALSE
         else
            return TRUE
         end if
      End Function
      Function isDoubleTap(ByVal par as object)
         Dim lastTime
         
         lastTime = getMyVar(par & "DoubleTap")
         if(lastTime = "NotSet") then
            setMyVar(par & "DoubleTap|" & Now)
            return FALSE
         elseif timeDiffGreaterThanSeconds(lastTime & "|5")
            setMyVar(par & "DoubleTap|" & Now)
            return FALSE
         else
            return TRUE
         end if
      End Function
      Function isTripleTap(ByVal par as object)
         Dim lastTime
         
         lastTime = getMyVar(par & "TripleTap")
         if(isDoubleTap(par) & getMyVar(par & "wasDoubleTapped") <> "TRUE") then
            setMyVar(par & "wasDoubleTapped|" & "TRUE")
            setMyVar(par & "TripleTap|" & Now)
            return FALSE
         elseif(getMyVar(par & "wasDoubleTapped") = "TRUE" & not(timeDiffGreaterThanSeconds(lastTime & "|5"))) then
            setMyVar(par & "wasDoubleTapped|" & "FALSE")
            return TRUE
         else
            setMyVar(par & "wasDoubleTapped|" & "FALSE")
            return FALSE
         end if
      End Function  
      Function isEvtEnabled(ByVal par as object)
         Dim eventsEnabled
         Dim ev
         
         '' if the result of the AND is = 32 then it is disabled
         '' if the result of the AND is = 0, then it is enabled
         ev = hs.GetEventByRef(hs.GetEventRefByName(par))
         eventsEnabled = ev.Misc AND &H20 
         if(eventsEnabled = 0) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Function isEvtDisabled(ByVal par as object)
         Dim eventsEnabled
         Dim ev
         
         '' if the result of the AND is = 32 then it is disabled
         '' if the result of the AND is = 0, then it is enabled
         ev = hs.GetEventByRef(hs.GetEventRefByName(par))
         eventsEnabled = ev.Misc AND &H20 
         if(eventsEnabled = 32) then
            return TRUE
         else
            return FALSE
         end if
      End Function
      Tasker, to a person who does Homeautomation...is like walking up to a Crack Treatment facility with a truck full of 3lb bags of crack. Then for each person that walks in and out smack them in the face with an open bag.

      Comment

      Working...
      X