Zo weer een stap verder, maar schrik niet. Het script maakt voor iedere record in die pagina 2 events aan, tenzij de tijd daarin vermeld staat, in het verleden valt. Ik moet nog even een manier vinden om te achterhalen of er 0 mm/uur meldingen achter elkaar zijn, zodat daar geen events voor aangemaakt hoeft te worden, tenzij het eerste is na regen. Dat wordt even bedenken.
Script logt ook wat voor informatie verkregen is en het device zal nu ook een andere string meekrijgen.
Code: Select all
' RegenMonitor.vb script
' Author: Alexander
' Version: 2.0
' Last Update: 20-07-2010 21:20
' ChangeLog:
' 1.0: Initial
' 1.1: - Added the ability to stop the script if the website doesn't return a known text, if that is the case the device will get the status unknown.
' - If the website would update the rain prediction to a lower time, the event that is already created will get the earlier condition.
' - If the time of the rain prediction is suddenly earlier that the current time, the device will be set instantly and earlier created event is deleted.
' - Parameters have to be added. Example ("Main","4057886;v7"). First is the geoAreaId and second is the device.
' - First code optimization
' - Code fault in handling if rain is predicted was not defined
' 2.0: - Complete script rewrite for other url
' - 48 temporary events will be created if the time in the page is all in the future.
Sub Main(ByVal Params As String)
Dim strGeoAreaId As String = hs.StringItem(Params, 1, ";")
Dim strDevice As String = hs.StringItem(Params, 2, ";")
Dim strData As String = hs.GetURL("www.weeronline.nl", "/Go/FlashCharts/RainImmediate?geoAreaId=" & strGeoAreaId, false, 80)
If (Not InStr(strData, "?xml") > 0) Then
hs.WriteLog("RegenMonitor", "Website weeronline.nl down")
hs.SetDeviceStatus(strDevice, 17)
hs.SetDeviceLastChange(strDevice, Now)
Else
Dim strDeviceString As String = strData.SubString(InStr(strData, "<label>")+6, InStr(strData, "</label>")-InStr(strData, "<label>")-1)
hs.writelog("RegenMonitor", strDeviceString)
hs.SetDeviceString(strDevice, strDeviceString)
Dim strArr() As String
Dim strDate(24) As String
Dim strTime(24) As String
Dim strValue(24) As String
strArr = strdata.split(chr(13))
Dim i,j As Integer
For i = 12 To strArr.Length - 1
If InStr(strArr(i), "<date>") > 0 Then
Dim a As Integer = InStr(strArr(i), "<date>")
Dim b As Integer = InStr(strArr(i), "</date>")
Dim c As Integer = InStr(strArr(i), "<date>")
Dim strTmp = strArr(i).SubString(a+5, b-c-6)
strDate(j) = Left(strTmp, InStr(strTmp, "T")-1)
strTime(j) = Mid(strTmp, InStr(strTmp, "T")+1)
Else If InStr(strArr(i), "<item") > 0 Then
Dim a As Integer = InStr(strArr(i), "value=")
Dim b As Integer = InStr(strArr(i), " />")
Dim c As Integer = InStr(strArr(i), "value=")
strValue(j) = strArr(i).SubString(a+6, b-c-8)
j +=1
End If
Next
For i = 0 To j - 1
Dim strOffDevice As String = strDevice & "_" & strDate(i) & strTime(i) & "_OFF"
Dim strOnDevice As String = strDevice & "_" & strDate(i) & strTime(i) & "_ON"
If hs.EventExists(strOffDevice & "-1") Then
hs.DeleteEvent(strOffDevice & "-1")
hs.DeleteEvent(strOffDevice & "-2")
End If
If hs.EventExists(strOnDevice & "-1") Then
hs.DeleteEvent(strOnDevice & "-1")
hs.DeleteEvent(strOnDevice & "-2")
End If
Dim lngValueCommand As Long = Math.Round((Left(strValue(i), InStr(strValue(i), " mm") - 1) * 100), 0)
Dim strValueCommand As String = "&hs.SetDeviceValue(" & strDevice & ", " & lngValueCommand.toString() & ")"
If (DateTime.Compare(strTime(i), FormatDateTime(Now, 4)) <= 0) Then
If InStr(strValue(i), "0 mm/uur") > 0 Then
If Not hs.DeviceStatus(strDevice) = 3 Then hs.Transmit(strDevice, "off")
hs.SetDeviceValue(strDevice, 0)
Else
If Not hs.DeviceStatus(strDevice) = 2 Then hs.Transmit(strDevice, "on")
hs.SetDeviceValue(strDevice, lngValueCommand.toString())
End If
Else
If InStr(strValue(i), "0 mm/uur") > 0 Then
hs.NewTimeEvent(strOffDevice & "-1", strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":off", 1, "", "")
hs.NewTimeEvent(strOffDevice & "-2", strTime(i), "", 1, 1, 1, 1, 1, 1, 1, "", 1, "&hs.SetDeviceValue(" & strDevice & ", 0)", "")
hs.EnableEvent(strOffDevice & "-1")
hs.EnableEvent(strOffDevice & "-2")
Else
hs.NewTimeEvent(strOnDevice & "-1", strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":on", 1, "", "")
hs.NewTimeEvent(strOnDevice & "-2", strTime(i), "", 1, 1, 1, 1, 1, 1, 1, "", 1, strValueCommand, "")
hs.EnableEvent(strOnDevice & "-1")
hs.EnableEvent(strOnDevice & "-2")
End If
End If
hs.writelog("RegenMonitor", strDate(i) & " - " & strTime(i) & " - " & strValue(i))
Next
End If
End Sub