Announcement

Collapse
No announcement yet.

System.Core error with in HS but not when testing in tenScripting

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

  • sparkman
    replied
    Great, thanks!

    Leave a comment:


  • DavidLiles
    replied
    Here it is sparkman

    Code:
    Imports System.Text
    Imports System.Collections.Generic
    
        Public Sub Main(ByVal Parms As Object)
    
            Dim dte As Date = CDate(System.DateTime.Today.ToString("MM/dd/yyyy"))
            Dim dayNumber As Integer
            Dim girlsHome As String = "No message displayed."
            Dim dfi = Globalization.DateTimeFormatInfo.CurrentInfo
            Dim calendar = dfi.Calendar
    
            dayNumber = dte.DayOfWeek
    
            ' Determine the week of the year
            Dim weekOfyear = calendar.GetWeekOfYear(dte, dfi.CalendarWeekRule, dayNumber)
            ' Check if the week of the year is odd or even.
            If weekOfyear Mod 2 = 1 Then
                ' Week number is odd so the girls are not here.
                girlsHome = "The girls are not here this week."
            Else
                ' Week number is even so the girls are here.
                hs.WriteLog("LightSchedule Script", "LightSchedule: The day of the week is: " & dte.ToString("dddd"))
                ' Check if the week the girls are here is during the summer. If Not, then it is a school week and continue.
                girlsHome = "It is summer."
                If Not IsSummer(weekOfyear) Then
                    ' Check if it is a week day
                    girlsHome = "It is not a school day for the girls."
                    If IsWeekDay(dayNumber) Then
                        ' Check if it is a school holiday. If True, then continue.
                        If IsSchoolDay(dte) Then
                            girlsHome = "It is a school day for the girls."
                            ' If it is a week day (M-F) then turn on the device.
                            hs.CAPIControlHandler(hs.CAPIGetSingleControl(hs.GetDeviceRefByName("BDRM 3 Light"), True, "On", False, False))
                            hs.CAPIControlHandler(hs.CAPIGetSingleControl(hs.GetDeviceRefByName("BDRM 2 Light"), True, "On", False, False))
                        End If
                    End If
                End If
            End If
            hs.WriteLog("LightSchedule Script", "LightSchedule: " & girlsHome)
        End Sub
    
        Public Function IsWeekDay(dayNumber As Integer) As Boolean
            Dim weekDay As Boolean
            weekDay = False
    
            Select Case dayNumber
                Case 1                  ' Monday
                    weekDay = True
                Case 2                  ' Tuesday
                    weekDay = True
                Case 3                  ' Wednesday
                    weekDay = True
                Case 4                  ' Thursday
                    weekDay = True
                Case 5                  ' Friday
                    weekDay = True
            End Select
    
            Return weekDay
        End Function
    
        Public Function IsSummer(weekNumber As Integer) As Boolean
            Dim summerWeek As Boolean
            summerWeek = False
            If weekNumber >= 24 And weekNumber <= 34 Then
                summerWeek = True
            End If
    
            Return summerWeek
        End Function
    
        Public Function IsSchoolDay(todayDate As Date) As Boolean
            Dim currentYear As Integer
            currentYear = todayDate.Year
            Dim schoolDay As Boolean
            schoolDay = True
    
            ' First check to make sure the getHolidayList(currentYear) List is not empty.
            If getHolidayList(currentYear) IsNot Nothing AndAlso getHolidayList(currentYear).Count > 0 Then
                ' Check if date is a school holiday.
                If getHolidayList(currentYear).Contains(todayDate) Then
                    schoolDay = False
                End If
            End If
    
            Return schoolDay
        End Function
    
        Public Function getHolidayList(ByVal vYear As Integer) As List(Of Date)
    
            Dim FirstWeek As Integer = 1
            Dim SecondWeek As Integer = 2
            Dim ThirdWeek As Integer = 3
            Dim FourthWeek As Integer = 4
            Dim LastWeek As Integer = 5
    
            Dim HolidayList As New List(Of Date)()
    
            '   http://www.usa.gov/citizens/holidays.shtml      
    
            ' New Year's Day            Jan 1
            HolidayList.Add(DateSerial(vYear, 1, 1))
    
            ' Martin Luther King, Jr. third Mon in Jan
            HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 1, 1), DayOfWeek.Monday, ThirdWeek))
    
            ' Washington's Birthday third Mon in Feb
            HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 2, 1), DayOfWeek.Monday, ThirdWeek))
    
            ' Memorial Day          last Mon in May
            HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 5, 1), DayOfWeek.Monday, LastWeek))
    
            ' Independence Day      July 4
            HolidayList.Add(DateSerial(vYear, 7, 4))
    
            ' Labor Day             first Mon in Sept
            HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 9, 1), DayOfWeek.Monday, FirstWeek))
    
            ' Columbus Day          second Mon in Oct
            HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 10, 1), DayOfWeek.Monday, SecondWeek))
    
            ' Veterans Day          Nov 11
            HolidayList.Add(DateSerial(vYear, 11, 11))
    
            ' Thanksgiving Day      fourth Thur in Nov
            HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 11, 1), DayOfWeek.Thursday, FourthWeek))
    
            ' Christmas Day         Dec 25
            HolidayList.Add(DateSerial(vYear, 12, 25))
    
            ' Add school specific holidays. Need to update each school year. Current school year: 2018-2019
            ' Christmas Break
            HolidayList.Add(DateSerial(vYear, 12, 24))
            HolidayList.Add(DateSerial(vYear, 12, 26))
            HolidayList.Add(DateSerial(vYear, 1, 7))
            ' Bad Weather Makeup Day
            HolidayList.Add(DateSerial(vYear, 4, 19))
    
            'saturday holidays are moved to Fri; Sun to Mon
            For i As Integer = 0 To HolidayList.Count - 1
                Dim dt As Date = HolidayList(i)
    
                If dt.DayOfWeek = DayOfWeek.Saturday Then
                    HolidayList(i) = dt.AddDays(-1)
                End If
                If dt.DayOfWeek = DayOfWeek.Sunday Then
                    HolidayList(i) = dt.AddDays(1)
                End If
            Next
    
            'return
            Return HolidayList
    
        End Function
    
        Private Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
            'specify which day of which week of a month and this function will get the date
            'this function uses the month and year of the date provided
    
            'get first day of the given date
            Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)
    
            'get first DayOfWeek of the month
            Dim dtRet As Date = dtFirst.AddDays(6 - dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek)
    
            'get which week
            dtRet = dtRet.AddDays((WhichWeek - 1) * 7)
    
            'if day is past end of month then adjust backwards a week
            If dtRet >= dtFirst.AddMonths(1) Then
                dtRet = dtRet.AddDays(-7)
            End If
    
            'return
            Return dtRet
    
        End Function
    Last edited by DavidLiles; December 16, 2018, 10:24 AM. Reason: Refactored function name to be more readable, corrected a logic error when determining if week is odd or even, and removed a variable that wasn't being used.

    Leave a comment:


  • sparkman
    replied
    Glad to hear you got it working! If you could post the final script, that would be great. I haven’t used lists in my scripts so would be great to see a working example.

    Cheers
    Al

    Leave a comment:


  • DavidLiles
    replied
    sparkman - thanks to your insight regarding the List syntax being ASP. I made the following change:

    Original syntax: Dim HolidayList As List(Of Date) = New List(Of Date)
    Changed to: Dim HolidayList As New List(Of Date)()

    Then on a whim I added the following to the top of the file:

    Imports System.Collections.Generic

    And when I exported and ran the script in my HS environment it worked without any issues. I had previously removed all imports because during my earlier dev/testing I was getting an error when running the script in HS stating the imports weren't necessary.

    Thanks again sparkman for identifying the ASP syntax. I can still post the entire exported script if you would like.

    Leave a comment:


  • sparkman
    replied
    DavidLiles Ah ok, I saw those @DONOTEXPORT tags, but since they were in the script it seems like they did not get stripped. Can you post the export to HS? I have tenscripting installed, but haven't used it in years. Also, if you go into the Advanced Editor for the forum posts, there are tags that can be wrapped around scripts that make them a bit more readable on the forum. If there's any indentation to make loops and if then else statements more visible, the indentation is then preserved as well. Here's an example of that:

    Code:
    private Function GetColor(ByVal color As Double) as String
        Select Case color
            Case 0
                GetColor = "Off"
            Case 1
                GetColor = "Red"
            Case 2
                GetColor = "Green"
            Case 3
                GetColor = "Blue"
            Case 4
                GetColor = "Magenta"
            Case 5
                GetColor = "Yellow"
            Case 6
                GetColor = "Cyan"
            Case 7
                GetColor = "White"
        End Select
    End Function

    Leave a comment:


  • DavidLiles
    replied
    sparkman - Thanks for the feedback. I wasn't aware that the example I found was ASP so it appears there is probably a compatibility issue I need to figure out.

    As for the Class wrapper, the code I posted is from the tenScripting tool I'm using to create and test scripts. The tool removes it when exporting to my HS environment. The msgbox also gets stripped out because the of @DONOTEXPORT at the end of the statement. Anything with that tag at the end of the line will get removed when exporting the script. Kind of a nice feature because when testing within the tool lines with that tag will still get processed but not get included when exporting to HS.

    Leave a comment:


  • sparkman
    replied
    A few things jump out at me:

    - The Dim HolidayList As List(Of Date) = New List(Of Date) statement and example appears to be ASP based which in my understanding is VBScript based, not VB.NET based. The languages are very similar but there are differences. I believe the New statement is not used in VB.NET.
    - You have a Class wrapper around the whole script. That is not needed unless you are doing something else with this script that I don't know about.
    - You have a msgbox statement in the script. As these scripts are run in the background, I don't believe those are visible and are not needed.

    Leave a comment:


  • DavidLiles
    replied
    Here is what I'm working on. It is a script that will turn on the lights in my daughters rooms in the morning under the following conditions:
    - It is a week when the girls are with me.
    - It is not during the summer.
    - Is it a no school day.
    - Is it a weekday (m - f).

    I'm no Visual Basic programmer so be gentle with the critiquing.


    Public Class LightSchedule

    Public Sub Main(ByVal Parms As Object)

    Dim dte As Date = CDate(System.DateTime.Today.ToString("MM/dd/yyyy")) 'DateTime.Today
    Dim weekNumber As Integer
    Dim dayNumber As Integer
    Dim girlsHome As String = "No message displayed."
    Dim weekDay As String = ""

    Dim dfi = Globalization.DateTimeFormatInfo.CurrentInfo
    Dim calendar = dfi.Calendar

    dayNumber = dte.DayOfWeek

    ' Determine the week of the year
    Dim weekOfyear = calendar.GetWeekOfYear(dte, dfi.CalendarWeekRule, dayNumber)

    ' Check if the week of the year is odd or even
    If weekNumber Mod 2 = 1 Then
    ' Week number is even so the girls are not here.
    ' Use this section for testing code.
    Else '@DONOTEXPORT
    ' Week number is odd so the girls are here.
    hs.WriteLog("LightSchedule Script", "LightSchedule: The day of the week is: " & dte.ToString("dddd"))
    ' Check if the week the girls are here is during the summer. If Not, then it is a school week and continue.
    If Not IsSummer(weekNumber) Then
    ' Check if it is a school holiday. If Not, then continue.
    If Not NotSchoolDay(dte) Then
    ' Check if it is a week day
    If IsWeekDay(dayNumber) Then
    girlsHome = "It is a school day for the girls."
    ' If it is a week day (M-F) then turn on the device.
    hs.CAPIControlHandler(hs.CAPIGetSingleControl(hs.GetDeviceRe fByName("BDRM 3 Light"), True, "On", False, False))
    hs.CAPIControlHandler(hs.CAPIGetSingleControl(hs.GetDeviceRe fByName("BDRM 2 Light"), True, "On", False, False))
    End If
    End If
    End If
    End If
    hs.WriteLog("LightSchedule Script", "LightSchedule: " & girlsHome)
    MsgBox("LightSchedule: LightSchedule", , "tenScripting") '@DONOTEXPORT
    End Sub

    Public Function IsWeekDay(dayNumber As Integer) As Boolean
    Dim weekDay As Boolean
    weekDay = False

    If dayNumber = 1 Then ' Monday
    weekDay = True
    ElseIf dayNumber = 2 Then ' Tuesday
    weekDay = True
    ElseIf dayNumber = 3 Then ' Wednesday
    weekDay = True
    ElseIf dayNumber = 4 Then ' Thursday
    weekDay = True
    ElseIf dayNumber = 5 Then ' Friday
    weekDay = True
    End If

    Return weekDay
    End Function

    Public Function IsSummer(weekNumber As Integer) As Boolean
    Dim summerWeek As Boolean
    summerWeek = False
    If weekNumber >= 24 And weekNumber <= 34 Then
    summerWeek = True
    End If

    Return summerWeek
    End Function

    Public Function NotSchoolDay(todayDate As Date) As Boolean
    Dim currentYear As Integer
    currentYear = todayDate.Year

    Dim noSchool As Boolean
    noSchool = False
    ' First check to make sure the NotSchoolDay list is not empty.
    If getHolidayList(currentYear) IsNot Nothing AndAlso getHolidayList(currentYear).Count > 0 Then
    ' Check if date is a school holiday.
    If getHolidayList(currentYear).Contains(todayDate) Then
    noSchool = True
    End If
    End If

    Return noSchool
    End Function

    ' Taken from an online example.
    Public Function getHolidayList(ByVal vYear As Integer) As List(Of Date)

    Dim FirstWeek As Integer = 1
    Dim SecondWeek As Integer = 2
    Dim ThirdWeek As Integer = 3
    Dim FourthWeek As Integer = 4
    Dim LastWeek As Integer = 5

    Dim HolidayList As List(Of Date) = New List(Of Date)

    ' http://www.usa.gov/citizens/holidays.shtml
    ' http://archive.opm.gov/operating_sta...edhol/2013.asp

    ' New Year's Day Jan 1
    HolidayList.Add(DateSerial(vYear, 1, 1))

    ' Martin Luther King, Jr. third Mon in Jan
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 1, 1), DayOfWeek.Monday, ThirdWeek))

    ' Washington's Birthday third Mon in Feb
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 2, 1), DayOfWeek.Monday, ThirdWeek))

    ' Memorial Day last Mon in May
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 5, 1), DayOfWeek.Monday, LastWeek))

    ' Independence Day July 4
    HolidayList.Add(DateSerial(vYear, 7, 4))

    ' Labor Day first Mon in Sept
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 9, 1), DayOfWeek.Monday, FirstWeek))

    ' Columbus Day second Mon in Oct
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 10, 1), DayOfWeek.Monday, SecondWeek))

    ' Veterans Day Nov 11
    HolidayList.Add(DateSerial(vYear, 11, 11))

    ' Thanksgiving Day fourth Thur in Nov
    HolidayList.Add(GetNthDayOfNthWeek(DateSerial(vYear, 11, 1), DayOfWeek.Thursday, FourthWeek))

    ' Christmas Day Dec 25
    HolidayList.Add(DateSerial(vYear, 12, 25))

    ' Add school specific holidays. Need to update each school year. Current school year: 2018-2019
    ' Christmas Break
    HolidayList.Add(DateSerial(vYear, 12, 24))
    HolidayList.Add(DateSerial(vYear, 12, 26))
    HolidayList.Add(DateSerial(vYear, 1, 7))
    ' Bad Weather Makeup Day
    HolidayList.Add(DateSerial(vYear, 4, 19))

    'saturday holidays are moved to Fri; Sun to Mon
    For i As Integer = 0 To HolidayList.Count - 1
    Dim dt As Date = HolidayList(i)
    If dt.DayOfWeek = DayOfWeek.Saturday Then
    HolidayList(i) = dt.AddDays(-1)
    End If
    If dt.DayOfWeek = DayOfWeek.Sunday Then
    HolidayList(i) = dt.AddDays(1)
    End If
    Next

    'return
    Return HolidayList

    End Function

    ' Taken from an online example.
    Private Function GetNthDayOfNthWeek(ByVal dt As Date, ByVal DayofWeek As Integer, ByVal WhichWeek As Integer) As Date
    'specify which day of which week of a month and this function will get the date
    'this function uses the month and year of the date provided

    'get first day of the given date
    Dim dtFirst As Date = DateSerial(dt.Year, dt.Month, 1)

    'get first DayOfWeek of the month
    Dim dtRet As Date = dtFirst.AddDays(6 - dtFirst.AddDays(-(DayofWeek + 1)).DayOfWeek)

    'get which week
    dtRet = dtRet.AddDays((WhichWeek - 1) * 7)

    'if day is past end of month then adjust backwards a week
    If dtRet >= dtFirst.AddMonths(1) Then
    dtRet = dtRet.AddDays(-7)
    End If

    'return
    Return dtRet

    End Function


    End Class

    Leave a comment:


  • sparkman
    replied
    Originally posted by Krumpy View Post
    It might be helpful for you to post the script. But I am guessing that you're not importing System.Core …..
    HS automatically imports System.Core and typically throws that error message if there is something else wrong in a script. It appears to be an issue related to the declaration of “List”.

    Leave a comment:


  • Krumpy
    replied
    It might be helpful for you to post the script. But I am guessing that you're not importing System.Core …..

    Leave a comment:


  • System.Core error with in HS but not when testing in tenScripting

    I've created a script in tenScripting that executes as expected and doesn't generate any errors and I can see. When I export the script to HS and run it I get the following errors in the log and don't understand why.
    Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\LightSchedule.vb: Type 'List' is not defined.
    Dec-11 7:33:45 PM Error Compiling script C:\Program Files (x86)\HomeSeer HS3\scripts\LightSchedule.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
Working...
X