Announcement

Collapse
No announcement yet.

Script Error 1 Exception has been thrown by the target of an invocation

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

    Script Error 1 Exception has been thrown by the target of an invocation

    I have a script which downloads RSS data from BBC Weather, creates / updates devices (including status graphics) so that that my HSTouch screens update.

    The script works just fine but after it has run I get the following error. ANy ideas where to start looking ? Thank you.

    Running script /usr/share/HomeSeer/scripts/GetRss_and_MakeDevices.vb :Exception has been thrown by the target of an invocation.An exception was thrown by the type initializer for System.Windows.Forms.WindowsFormsSynchronizationContext

    #2
    It is going to depend on what is in the script I am afraid (if you want to share it), it is unusual to get forms errors because the majority of scripting commands don't use forms but you might have put something in there that uses it.

    Comment


      #3
      I don't mind sharing. It isn't very pretty code as I knocked it up quickly, I should go back and add error traps etc. Any way here it is.

      Public Sub Main(ByVal Parms As Object)

      Dim RssString = ParseRssFile() 'get the RSS details as string
      Dim SMarker = InStr(RssString, "day=0'>") + 7 'find the start fo today's name
      Dim EMarker = InStr(SMarker, RssString, ":") 'finf the end of it
      Dim Length = EMarker - SMarker ' find the legth of the text
      Dim strDay0 = Trim(Mid(RssString, SMarker, Length)) ' make that strDay0 e.g. Suunday
      MakeDevice("Day0", strDay0, strDay0, False)

      SMarker = EMarker + 1 'move the start of search to the begining of the strForecast for that day
      EMarker = InStr(EMarker, RssString, ",") ' and the end marker to the next comma
      Length = EMarker - SMarker ' get the length of the text
      Dim strForecast0 = Trim(Mid(RssString, SMarker, Length)) ' make that the strForecast0 e.g. Light Cloud
      MakeDevice("Forecast0", strForecast0, strForecast0, True)

      'Find Maxuimum Temperture - if it exists (once past heat of day RSS feed misses it out on todays weather.
      Dim ShortRSS = Left(RssString, 200)
      Dim intMaxTemp0
      Dim strMaxTemp0 As String
      If InStr(SMarker, ShortRSS, "Maximum Temperature:") = 0 Then
      intMaxTemp0 = "-"
      strMaxTemp0 = "-"
      Else
      SMarker = InStr(EMarker, RssString, "Maximum Temperature:") + 20
      EMarker = InStr(SMarker, RssString, "C") - 1 '-1 becuase don't want degrees symbol
      Length = EMarker - SMarker
      intMaxTemp0 = Trim(Mid(RssString, SMarker, Length))
      strMaxTemp0 = Trim(Mid(RssString, SMarker, Length + 2))
      End If
      MakeDevice("MaxTemp0", intMaxTemp0, strMaxTemp0, False)

      SMarker = InStr(EMarker, RssString, "Minimum Temperature:") + 20
      EMarker = InStr(SMarker, RssString, "C") - 1
      Length = EMarker - SMarker
      Dim intMinTemp0 = Trim(Mid(RssString, SMarker, Length))
      Dim strMinTemp0 As String = Trim(Mid(RssString, SMarker, Length + 2))
      MakeDevice("MinTemp0", intMinTemp0, strMinTemp0, False)

      SMarker = InStr(EMarker, RssString, "Wind Speed:") + 11
      EMarker = InStr(SMarker, RssString, "mph")
      Length = EMarker - SMarker
      Dim intWindSpeed0 = Trim(Mid(RssString, SMarker, Length))
      Dim strWindSpeed0 As String = Trim(Mid(RssString, SMarker, Length + 3))
      MakeDevice("WindSpeed0", intWindSpeed0, strWindSpeed0, False)

      'DAY1
      SMarker = InStr(EMarker, RssString, "day=1'>") + 7
      EMarker = InStr(SMarker, RssString, ":") 'finf the end of it
      Length = EMarker - SMarker ' find the legth of the text
      Dim strDay1 = Trim(Mid(RssString, SMarker, Length))
      MakeDevice("Day1", strDay1, strDay1, False)

      SMarker = EMarker + 1
      EMarker = InStr(EMarker, RssString, ",")
      Length = EMarker - SMarker
      Dim strForecast1 = Trim(Mid(RssString, SMarker, Length))
      MakeDevice("Forecast1", strForecast1, strForecast1, True)

      SMarker = InStr(EMarker, RssString, "Maximum Temperature:") + 20
      EMarker = InStr(SMarker, RssString, "C") - 1
      Length = EMarker - SMarker
      Dim intMaxTemp1 = Trim(Mid(RssString, SMarker, Length))
      Dim strMaxTemp1 = Trim(Mid(RssString, SMarker, Length + 2))
      MakeDevice("MaxTemp1", intMaxTemp1, strMaxTemp1, False)

      SMarker = InStr(EMarker, RssString, "Minimum Temperature:") + 20
      EMarker = InStr(SMarker, RssString, "C") - 1
      Length = EMarker - SMarker
      Dim intMinTemp1 = Trim(Mid(RssString, SMarker, Length))
      Dim strMinTemp1 As String = Trim(Mid(RssString, SMarker, Length + 2))
      MakeDevice("MinTemp1", intMinTemp1, strMinTemp1, False)

      SMarker = InStr(EMarker, RssString, "Wind Speed:") + 11
      EMarker = InStr(SMarker, RssString, "mph")
      Length = EMarker - SMarker
      Dim intWindSpeed1 = Trim(Mid(RssString, SMarker, Length))
      Dim strWindSpeed1 = Trim(Mid(RssString, SMarker, Length + 3))
      MakeDevice("WindSpeed1", intWindSpeed1, strWindSpeed1, False)

      'strDay 2
      SMarker = InStr(EMarker, RssString, "day=2'>") + 7
      EMarker = InStr(SMarker, RssString, ":") 'finf the end of it
      Length = EMarker - SMarker ' find the legth of the text
      Dim strDay2 = Trim(Mid(RssString, SMarker, Length))
      MakeDevice("Day2", strDay2, strDay2, False)

      SMarker = EMarker + 1
      EMarker = InStr(EMarker, RssString, ",")
      Length = EMarker - SMarker
      Dim strForecast2 = Trim(Mid(RssString, SMarker, Length))
      MakeDevice("Forecast2", strForecast2, strForecast2, True)

      SMarker = InStr(EMarker, RssString, "Maximum Temperature:") + 20
      EMarker = InStr(SMarker, RssString, "C") - 1
      Length = EMarker - SMarker
      Dim intMaxTemp2 = Trim(Mid(RssString, SMarker, Length))
      Dim strMaxTemp2 As String = Trim(Mid(RssString, SMarker, Length + 2))
      MakeDevice("MaxTemp2", intMaxTemp2, strMaxTemp2, False)

      SMarker = InStr(EMarker, RssString, "Minimum Temperature:") + 20
      EMarker = InStr(SMarker, RssString, "C") - 1
      Length = EMarker - SMarker
      Dim intMinTemp2 = Trim(Mid(RssString, SMarker, Length))
      Dim strMinTemp2 = Trim(Mid(RssString, SMarker, Length + 2))
      MakeDevice("MinTemp2", intMinTemp2, strMinTemp2, False)

      SMarker = InStr(EMarker, RssString, "Wind Speed:") + 11
      EMarker = InStr(SMarker, RssString, "mph")
      Length = EMarker - SMarker
      Dim intWindSpeed2 = Trim(Mid(RssString, SMarker, Length))
      Dim strWindSpeed2 As String = Trim(Mid(RssString, SMarker, Length + 3))
      MakeDevice("WindSpeed2", intWindSpeed2, strWindSpeed2, False)

      hs.WriteLog("RSS feed", RssString)
      MsgBox(RssString)

      End Sub

      Private Sub MakeDevice(ByVal strDeviceName As String, strStatus As String, strString As String, withIcon As Boolean)
      Dim objDeviceRef As Integer
      Dim objDevice As Scheduler.Classes.DeviceClass
      Dim objStatusText As HomeSeerAPI.VSVGPairs.VSPair
      Dim objStatusGraphic = New HomeSeerAPI.VSVGPairs.VGPair


      'does it exist ?
      objDeviceRef = hs.GetDeviceRefByName(strDeviceName)
      If objDeviceRef <= 0 Then 'it doesn't exist so make it
      objDeviceRef = hs.NewDeviceRef(strDeviceName)
      objDevice = hs.GetDeviceByRef(objDeviceRef)
      objDevice.Location(hs) = "Utility"
      objDevice.Location2(hs) = "BBC Weather"

      objDevice.Status_Support(hs) = True
      End If

      'then update it with status
      hs.SetDeviceString(objDeviceRef, strString, True)
      objStatusText = New HomeSeerAPI.VSVGPairs.VSPair(HomeSeerAPI.ePairStatusControl. Both)
      objStatusText.PairType = HomeSeerAPI.VSVGPairs.VSVGPairType.SingleValue
      objStatusText.Value = 0
      objStatusText.Status = strStatus
      hs.DeviceVSP_AddPair(objDeviceRef, objStatusText)

      'if has a graphic update that
      If withIcon = True Then
      objStatusGraphic.PairType = HomeSeerAPI.VSVGPairs.VSVGPairType.SingleValue
      objStatusGraphic.Set_Value = 0
      If strStatus = "Sunny" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0001_sunny.png"
      ElseIf strStatus = "Light Cloud" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0003_white_cloud.png"
      ElseIf strStatus = "Sunny Intervals" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0002_sunny_intervals.png"
      ElseIf strStatus = "Light Rain Shower" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0009_light_rain_showers.png"
      ElseIf strStatus = "Heavy Rain Shower" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0010_heavy_rain_showers.png"
      ElseIf strStatus = "Thundery Shower" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0016_thundery_showers.png"
      ElseIf strStatus = "Clear Sky" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0008_clear_sky_night.png"
      ElseIf strStatus = "Light Rain" Then
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0017_cloudy_with_light_rain.png"

      Else
      objStatusGraphic.Graphic = "/images/AshTree Weather Icons/wsymbol_0999_unknown.png"

      End If



      hs.DeviceVGP_AddPair(objDeviceRef, objStatusGraphic)
      End If







      End Sub





      Private Function ParseRssFile() As String
      Dim rssXmlDoc As New Xml.XmlDocument()

      ' Load the RSS file from the RSS URL
      rssXmlDoc.Load("http://open.live.bbc.co.uk/weather/feeds/en/2636047/3dayforecast.rss")

      ' Parse the Items in the RSS file
      Dim rssNodes As Xml.XmlNodeList = rssXmlDoc.SelectNodes("rss/channel/item")

      Dim rssContent As New System.Text.StringBuilder()

      ' Iterate through the items in the RSS file
      For Each rssNode As Xml.XmlNode In rssNodes
      Dim rssSubNode As Xml.XmlNode = rssNode.SelectSingleNode("title")
      Dim title As String = If(rssSubNode IsNot Nothing, rssSubNode.InnerText, "")

      rssSubNode = rssNode.SelectSingleNode("link")
      Dim link As String = If(rssSubNode IsNot Nothing, rssSubNode.InnerText, "")

      rssSubNode = rssNode.SelectSingleNode("description")
      Dim description As String = If(rssSubNode IsNot Nothing, rssSubNode.InnerText, "")

      rssContent.Append("<a href='" & link & "'>" & title & "</a><br>" & description)
      Next

      ' Return the string that contain the RSS items
      Return rssContent.ToString()
      End Function

      Comment


        #4
        Just having a quick visual scan through the only thing that looks unusual is your call to MsgBox(RssString), that to me is the only thing that looks vaguely like it could cause an error in the forms namespace - from your path to the script do you run on Linux? I'm not sure how Linux plays with forms, if you need the message box are you able to call Messagebox.Show instead?

        Comment


          #5
          Well spotted.

          I didn't need MsgBox at all, it was part of my debugging process. Deleted the line.... no error message.

          Thank you very much
          Ian

          Comment

          Working...
          X