en probleem gevonden. Update script naar 2.4 indien je multi-device optie gebruikt en/of Last Update geupdate hebben wilt ondanks of de device gewijzigd is of niet.
Code: Select all
' RegenMonitor.vb script
' Author: Alexander
' Version: 2.4
' Last Update: 25-07-2010 12:38
' 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: - Script rewrite for other url
' 2.1: - Added DeviceStatusString
' 2.2: - Multi Devices support
' 2.3: - Bugfix about the double quotes which didn't were added in setdevicestring commands
' - Added images from Rien and included those in the script for display
' - Added DeviceLocation and DeviceType option when created idea by Rien
' 2.4: - Bugfix when multi-device selected. The on/off commands were transmitted only to the first device
' - When the string would be updated, the update time/date would only change when there really is an different string. Did set this to always update.
Sub Main(ByVal Params As String)
' Customize this value if you want to change the new created device(s) if not already existing
Dim strDeviceLocation As String = "Virtual - Monitoring"
Dim strDeviceType As String = "Verwachte Regen Monitoring"
Dim strGeoAreaId As String = hs.StringItem(Params, 1, ";")
Dim strDevice As String = hs.StringItem(Params, 2, ";")
Dim blRainInText As Boolean = hs.StringItem(Params, 3, ";") = "yes"
Dim blMultiDevice As Boolean = hs.StringItem(Params, 4, ";") = "yes"
Dim strData As String = hs.GetURL("www.weeronline.nl", "/Go/FlashCharts/RainImmediate?geoAreaId=" & strGeoAreaId, false, 80)
Dim i As Integer
If blMultiDevice Then
For i = 0 To 23
If hs.DeviceExistsRef(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = -1 Then
Dim dv As Object
dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen" & "-" & i+1))
dv.hc = GetChar(strDevice,1)
dv.dc = Val(GetChar(strDevice,2))+i
dv.misc = "&h10"
dv.location = strDeviceLocation
dv.dev_type_string = strDeviceType
End If
Next
Else
If hs.DeviceExistsRef(GetChar(strDevice,1) & GetChar(strDevice,2)) = -1 Then
Dim dv As Object
dv = hs.GetDeviceByRef(hs.NewDeviceRef("Regen"))
dv.hc = GetChar(strDevice,1)
dv.dc = GetChar(strDevice,2)
dv.misc = "&h10"
dv.location = strDeviceLocation
dv.dev_type_string = strDeviceType
End If
End If
If (Not InStr(strData, "?xml") > 0) Then
hs.SetDeviceString(strDevice, "Website weeronline.nl down", True)
hs.SetDeviceStatus(strDevice, 17)
hs.SetDeviceLastChange(strDevice, Now)
Else
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 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 strEvent As String = strDate(i) & "_" & strTime(i)
If hs.EventExists(strEvent) Then hs.DeleteEvent(strEvent)
Dim lngValueCommand As Long = Math.Round((Left(strValue(i), InStr(strValue(i), " mm") - 1) * 100), 0)
Dim strValueCommand As String
If blMultiDevice Then
strValueCommand = "&hs.SetDeviceValue(" & GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i & ", " & lngValueCommand.toString() & ")"
Else
strValueCommand = "&hs.SetDeviceValue(" & strDevice & ", " & lngValueCommand.toString() & ")"
End If
If (DateTime.Compare(strTime(i), FormatDateTime(Now, 4)) <= 0) Then
If InStr(strValue(i), "0 mm/uur") > 0 Then
If blMultiDevice Then
If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off")
hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0)
hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i), True)
Else
If Not hs.DeviceStatus(strDevice) = 3 Then hs.Transmit(strDevice, "off")
hs.SetDeviceValue(strDevice, 0)
hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText), True)
End If
Else
If blMultiDevice Then
If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "on")
hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString())
hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i), True)
Else
If Not hs.DeviceStatus(strDevice) = 2 Then hs.Transmit(strDevice, "on")
hs.SetDeviceValue(strDevice, lngValueCommand.toString())
hs.SetDeviceString(strDevice, WeerType(lngValueCommand, blRainInText), True)
End If
End If
Else If Not blMultiDevice Then
If (InStr(strValue(i), "0 mm/uur") > 0) And ((Not InStr(strValue(i-1), "0 mm/uur") > 0) or (i = 0)) Then
hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":off", 1, "", "")
hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand)
hs.AddAction(hs.GetEventRefByName(strEvent), 5, Chr(34) & "&hs.SetDeviceString(" & strDevice & ", " & Chr(34) & WeerType(lngValueCommand, blRainInText) & Chr(34) & "), True")
hs.EnableEvent(strEvent)
Else If (Not InStr(strValue(i), "0 mm/uur") > 0) Then
hs.NewTimeEvent(strEvent, strTime(i), "", 1, 1, 1, 1, 1, 1, 1, strDevice & ":on", 1, "", "")
hs.AddAction(hs.GetEventRefByName(strEvent), 5, strValueCommand)
hs.AddAction(hs.GetEventRefByName(strEvent), 5, Chr(34) & "&hs.SetDeviceString(" & strDevice & ", " & Chr(34) & WeerType(lngValueCommand, blRainInText) & Chr(34) & ", True)")
hs.EnableEvent(strEvent)
End If
Else
If InStr(strValue(i), "0 mm/uur") > 0 Then
If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 3 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "off")
hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, 0)
hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i), True)
Else
If Not hs.DeviceStatus(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i) = 2 Then hs.Transmit(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, "on")
hs.SetDeviceValue(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, lngValueCommand.toString())
hs.SetDeviceString(GetChar(strDevice,1) & Val(GetChar(strDevice,2))+i, WeerType(lngValueCommand, blRainInText) & " om " & strTime(i), True)
End If
End If
'hs.writelog("RegenMonitor", strDate(i) & " - " & strTime(i) & " - " & strValue(i))
Next
End If
End Sub
Function WeerType(Value As Integer, blRainInText As Boolean) As String
If blRainInText Then
Select Case Value
Case 0
Return "<img align='absmiddle' src='images/RegenMonitor/no_rain.png'> Geen regen"
Case 1 To 100
Return "<img align='absmiddle' src='images/RegenMonitor/light_rain.png'> Lichte regen"
Case 101 To 500
Return "<img align='absmiddle' src='images/RegenMonitor/medium_rain.png'> Matige regen"
Case 501 To 2500
Return "<img align='absmiddle' src='images/RegenMonitor/heavy_rain.png'> Zware regen"
Case Is > 2500
Return "<img align='absmiddle' src='images/RegenMonitor/heavy_rain.png'> Wolkbreuk"
End Select
Else
Dim strTmp As String
Select Case Value
Case 0
strTmp = "<img align='absmiddle' src='images/RegenMonitor/no_rain.png'>"
Case 1 To 100
strTmp = "<img align='absmiddle' src='images/RegenMonitor/light_rain.png'>"
Case 101 To 500
strTmp = "<img align='absmiddle' src='images/RegenMonitor/medium_rain.png'>"
Case 501 To 2500
strTmp = "<img align='absmiddle' src='images/RegenMonitor/heavy_rain.png'>"
Case Is > 2500
strTmp = "<img src='images/RegenMonitor/heavy_rain.png'>"
End Select
Return strTmp & " " & Math.Round(Value/100, 2).toString() & " mm/uur"
End If
End Function