Flitser en File Script HS3
Posted: Thu Jun 04, 2015 11:41 am
Hallo,
Zoals beloofd de 2 scripts voor het ophalen van files en flitsers, met dank aan Dhr van zoelen (van het HS2 script)
Ik heb dit HS2 script als uitgangspunt gebruikt voor beide scripts. Installatie instructies staan in het commentaar, tevens moet ik de code nog eens goed gaan opruimen.
het script maakt zelf de devices aan. Tevens attached, de images, deze dienen in "C:\Program Files (x86)\Homeseer HS3\html\images\FileFlitserMonitor" map geplaatst te worden
maar voor de time being, hier zijn ze dan;
(Filemonitor.vb)
FlitserMonitor.vb
Zoals beloofd de 2 scripts voor het ophalen van files en flitsers, met dank aan Dhr van zoelen (van het HS2 script)
Ik heb dit HS2 script als uitgangspunt gebruikt voor beide scripts. Installatie instructies staan in het commentaar, tevens moet ik de code nog eens goed gaan opruimen.
het script maakt zelf de devices aan. Tevens attached, de images, deze dienen in "C:\Program Files (x86)\Homeseer HS3\html\images\FileFlitserMonitor" map geplaatst te worden
maar voor de time being, hier zijn ze dan;
(Filemonitor.vb)
Code: Select all
'FileMonitor.vb
'by BB Bakels derived from script from A.A. van Zoelen
' Version
' 1.0 - BETA'
'USAGE with HomeSeer 3
'Make an event and select script.
'For the parameter field use 1;A16 N11 A12 A13;0
'
'where: <details> ; <roads> ; <debug>
' '
' <Details> vb. 1 - 1(yes) or 0(no)
' When N only the file count on the road ID is shown
' When Y also the detailed info will be shown.
' <Road ID(s)> vb. A28 A12 N11
' A list with road id's that you want information from.
' Each road ID is separated with a space from the other
' Leaving this empty will give you every file
'' <Debug> vb. 1 - 1(yes) or 0(no)
' for debugging only
'REMARKS
'I may be necessary to throw away old devices when you
'update your road id list.
'
'This script is optimized for http://www.verkeerplaza.nl/filelijst
'And most likely won't work for an other source.
sub Main(params As Object)
'*********************************************
' These variable must be set first
'*********************************************
'The full URL to the page where the data is
'USE FORWARD SLASHES AS IN //
Dim strURL as String
strURL = "http://www.verkeerplaza.nl/filelijst"
'*********************************************
' The next few variables might be alter if needed
'The tag for the Road list block
Dim strRoadListTag as String
strRoadListTag = "list-road"
Dim strRoadListEndTag as String
strRoadListEndTag = "</div>"
' Use these values to create new device(s)
Dim dv As Scheduler.Classes.DeviceClass = Nothing
Dim Graphic
Dim DevExists
Dim GPair = New VGPair
Dim Ref
Dim strDeviceRoom As String = "Status"
Dim strDeviceType As String = "FileMonitor"
Dim strDeviceFloor As String = "FileMonitor"
'*********************************************
'Below this there is no real need for altering anything
Dim strSite as String
Dim strPath as String
Dim strWebPage as String
Dim intTemp as Integer
Dim strTemp as String
Dim strOutput as String
Dim intNumberOfRoads as Integer
Dim i as Integer
Dim count as Integer
Dim CalcLenght as Integer
Dim strRoads ' String array
Dim Total as Integer
Dim TotalString as String
Dim parameters(3) as String
parameters = params.Split(";")
Dim blDetailedInfo As Boolean = parameters(0)
Dim strRoadList as String = parameters(1)
Dim Debug as boolean = parameters(2)
If Debug = true Then
hs.writelog("FileMonitor","blDetailedInfo :" & blDetailedInfo.ToString & ":" )
hs.writelog("FileMonitor","strRoadList :" & strRoadList & ":" )
hs.writelog("FileMonitor","Debug :" & Debug & ":" )
End If
'********************************
' If there are roads then split
' them into an array
'********************************
strRoads = Split(strRoadList," ")
intNumberOfRoads = UBound(strRoads)
If Debug = true Then hs.writelog("FileMonitor","Number of roads :" & intNumberOfRoads + 1)
'********************************
'Get Address and path of the URL
'********************************
strTemp = Replace(UCase(strURL), "HTTP://", "")
intTemp = Instr(strTemp, "/")
strSite = "http://" & Left(strTemp, intTemp - 1)
strPath = Right(strTemp, Len(strTemp) - intTemp + 1)
If Debug = true then
hs.writelog("FileMonitor","Site found :" & strSite )
hs.writelog("FileMonitor","Path found :" & strPath )
End If
'********************************
'Try to get the website data
'********************************
Try
strWebPage = hs.GetURL(strSite, strPath, false, 80)
If Len(strWebPage) < 100 Then
hs.writelog ("FileMonitor Error","No data found at " & strURL):exit sub
End If
Catch ex As Exception
hs.writelog("FileMonitor Error","Connection to site failed due:" & ex.Message )
Exit Sub
End Try
If Debug = true Then hs.writelog("FileMonitor","Byte(s) found :" & len(strWebPage) )
'********************************
'Now we have the complete webpage
'loaded into a string we can
'search for the road list block
'and drop all data infront
'and after the block.
'********************************
'Occassionally there is a negative string lenght which
'obviously isn't possible to caught this calculate the
'lenght first and make sure that it's => 0
CalcLenght = Len(strWebPage) - InStr(strWebPage, strRoadListTag) + 1
If CalcLenght < 0 Then CalcLenght = 0
strWebPage = "<ul class=" & Right(strWebPage, CalcLenght )
CalcLenght = InStr(strWebPage, strRoadListEndTag ) - 1
If CalcLenght < 0 Then CalcLenght = 0
strWebPage = Left(strWebPage, CalcLenght)
'********************************
'We have the data block.
'If the user wants this be split
'to seperate roads then do it
'here otherwise place all file
'information into one device.
'********************************
i = 0
Do
strOutput = "error"
If intNumberOfRoads > 0 Then
strTemp = FindRoadNumberBlock(strWebPage, strRoads(i), Debug)
Else
strTemp = strWebPage
End If
count = CountOccurrences(strTemp, "locatie" , false, Debug)
If strRoads(i) <> "" Then
strOutput = strRoads(i)
Else
strOutput = "Geheel NL"
End If
strOutput = strOutput & " - Totaal " & count & " files" & vbCrLf
' Device Creation if not exists
DevExists = hs.DeviceExistsAddress("FileMonitor" & strRoads(i), False)
If DevExists = -1 Then
dv = hs.NewDeviceEx(strRoads(i))
dv.Location(hs) = strDeviceFloor
dv.Location2(hs) = strDeviceRoom
dv.Address(hs) = "FileMonitor" & strRoads(i)
dv.Device_Type_String(hs) = strDeviceType
dv.Image(hs) = "/images/FileFlitserMonitor/file.png"
Ref = hs.GetDeviceRef("FileMonitor" & strRoads(i))
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 0
GPair.RangeEnd = 0
GPair.Graphic = "/images/FileFlitserMonitor/OK.png"
hs.DeviceVGP_AddPair(ref, GPair)
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 1
GPair.RangeEnd = 100000000
GPair.Graphic = "/images/FileFlitserMonitor/Pasop.png"
hs.DeviceVGP_AddPair(ref, GPair)
hs.WriteLog("FileMonitor device creation", strRoads(i))
End If
If blDetailedInfo then strOutput = strOutput & strTemp
If Debug = true Then hs.writelog("FileMonitor","Number of traffic jams(s) on " & strRoads(i) & ":" & count )
Ref = hs.GetDeviceRef("FileMonitor" & strRoads(i))
hs.SetDeviceString(Ref, strOutput , True)
hs.SetDeviceValueByRef(Ref, count , True)
if count = 0 then strOutput = ""
TotalString = TotalString & " " & strOutput
Total = Total + count
i = i + 1
Loop until i > intNumberOfRoads
If Total = 0 then TotalString = "Geen Files"
'remove HTML CRAP
TotalString = TotalString.Replace("</ul>", "")
TotalString = TotalString.Replace("<ul>", "")
TotalString = TotalString.Replace("<br/>", ".")
TotalString = TotalString.Replace("</li>", "")
TotalString = TotalString.Replace("<ul class=""melding"">", "")
TotalString = TotalString.Replace("<li class=""list-detail"">", "")
TotalString = TotalString.Replace("<ul class=""list-road"">", "")
TotalString = TotalString.Replace("<li class=""locatie"">", "")
TotalString = TotalString.Replace("<li class=""situatie"">", "")
TotalString = TotalString.Replace("<li class=""vertraging"">", "")
TotalString = TotalString.Replace(">", "")
TotalString = TotalString.Replace("<", "")
' Device Creation if not exists
DevExists = hs.DeviceExistsAddress("FileMonitor Totaal", False)
If DevExists = -1 Then
dv = hs.NewDeviceEx("FileMonitor Totaal")
dv.Location(hs) = strDeviceFloor
dv.Location2(hs) = strDeviceRoom
dv.Address(hs) = "FileMonitor Totaal"
dv.Device_Type_String(hs) = strDeviceType
dv.Image(hs) = "/images/FileFlitserMonitor/file.png"
Ref = hs.GetDeviceRef("FileMonitor Totaal")
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 0
GPair.RangeEnd = 0
GPair.Graphic = "/images/FileFlitserMonitor/OK.png"
hs.DeviceVGP_AddPair(ref, GPair)
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 1
GPair.RangeEnd = 100000000
GPair.Graphic = "/images/FileFlitserMonitor/Pasop.png"
hs.DeviceVGP_AddPair(ref, GPair)
hs.WriteLog("FileMonitor device creation", "FileMonitorTotaal")
End If
If Debug = true Then hs.writelog("FileMonitor","Totaal Files " & Total)
Ref = hs.GetDeviceRef("FileMonitor Totaal")
hs.SetDeviceString(Ref, TotalString , True)
hs.SetDeviceValueByRef(Ref, Total , True)
End sub
'===========================================
'== Functions and SUBS ==
'===========================================
Function FindRoadNumberBlock(strWebPage, Road, Debug)
Dim strWegData as String
Dim count as Integer
Dim CalcLenght as Integer
' If Debug = true Then hs.writelog("FileMonitor","FindRoadNumberBlock:Start function=" & strWebPage )
'Find start op road information
count = Instr(strWebPage, Road & "</li>")
If Debug = true Then hs.writelog("FileMonitor","FindRoadNumberBlock:Search for = " & Road )
If Debug = true Then hs.writelog("FileMonitor","FindRoadNumberBlock:Found at " & count )
'The requested road is found
if count > 0 then
'Drop the first part incl. the </li>
CalcLenght = Len(strWebPage) - (count + len(Road) + Len("</li>") )
If CalcLenght < 0 Then CalcLenght = 0
strWegData = "<ul><ul>" & Right(strWebPage, CalcLenght)
If Debug = true Then hs.writelog("FileMonitor","FindRoadNumberBlock:Right string=" & strWegData )
'Now find the end of this block
count = Instr(strWegData, "wegnummer")
'Drop the last part
count = count - Len("<li class=") - 2
If count < 0 Then count = 0
strWegData = Left(strWegData, count )
If Debug = true Then hs.writelog("FileMonitor","FindRoadNumberBlock:After left string=" & strWegData )
'Close a few tags to prevent unwanted
'generation of html code
strWegData = strWegData & "</ul></ul>"
If Debug = true Then hs.writelog("FileMonitor","FindRoadNumberBlock:ReturnString=" & strWegData )
End If
return strWegData
End Function
'------------------------------------------------
'function takes three parameters.
'The first is the string to be searched through.
'The second is the string to search for.
'The third is a Boolean that determines if the search
' should be case sensitive.
'The function returns the count of occurrences
Function CountOccurrences(p_strStringToCheck, p_strSubString, p_boolCaseSensitive, Debug)
Dim arrstrTemp
Dim strBase, strToFind
If Debug = true Then hs.writelog("FileMonitor","Start Count Occurences" )
If p_boolCaseSensitive Then
strBase = p_strStringToCheck
strToFind = p_strSubString
Else
strBase = LCase(p_strStringToCheck)
strToFind = LCase(p_strSubString)
End If
arrstrTemp = Split(strBase, strToFind)
CountOccurrences = UBound(arrstrTemp)
If Debug = true Then hs.writelog("FileMonitor","End Count Occurences : Count = " & CountOccurrences )
End Function
FlitserMonitor.vb
Code: Select all
'FlitserMonitor.vb
'by BB Bakels derived from script from A.A. van Zoelen
' Version
' 1.0 - BETA'
'USAGE with HomeSeer 3
'Make an event and select script.
'For the parameter field use 1;A16 N11 A12 A13;0
'
'where: <details> ; <roads> ; <debug>
' '
' <Details> vb. 1 - 1(yes) or 0(no)
' When N only the file count on the road ID is shown
' When Y also the detailed info will be shown.
' <Road ID(s)> vb. A28 A12 N11
' A list with road id's that you want information from.
' Each road ID is separated with a space from the other
' Leaving this empty will give you every file
'' <Debug> vb. 1 - 1(yes) or 0(no)
' for debugging only
'
'REMARKS
'I may be nessecary to throw away old devices when you
'update your road id list.
'
'This script is optimized for http://www.verkeerplaza.nl/filelijst
'And most likely won't work for an other source.
sub Main(params As Object)
'*********************************************
' These variable must be set first
'*********************************************
'The full URL to the page where the data is
'USE FORWARD SLASHES AS IN //
Dim strURL as String
strURL = "http://www.verkeerplaza.nl/flitsers"
'*********************************************
' The next few variables might be alter if needed
'The tag for the Road list block
Dim strRoadListTag as String
strRoadListTag = "list-road"
Dim strRoadListEndTag as String
strRoadListEndTag = "</div>"
' Use these values to create new device(s)
Dim dv As Scheduler.Classes.DeviceClass = Nothing
Dim Graphic
Dim DevExists
Dim GPair = New VGPair
Dim Ref
Dim strDeviceRoom As String = "Status"
Dim strDeviceType As String = "FlitserMonitor"
Dim strDeviceFloor As String = "FlitserMonitor"
'*********************************************
'Below this there is no real need for altering anything
Dim strSite as String
Dim strPath as String
Dim strWebPage as String
Dim intTemp as Integer
Dim strTemp as String
Dim strOutput as String
Dim intNumberOfRoads as Integer
Dim i as Integer
Dim count as Integer
Dim CalcLenght as Integer
Dim strRoads ' String array
Dim Total as Integer
Dim TotalString as String
Dim parameters(3) as String
parameters = params.Split(";")
Dim blDetailedInfo As Boolean = parameters(0)
Dim strRoadList as String = parameters(1)
Dim Debug as boolean = parameters(2)
If Debug = true Then
hs.writelog("FlitserMonitor","blDetailedInfo :" & blDetailedInfo.ToString & ":" )
hs.writelog("FlitserMonitor","strRoadList :" & strRoadList & ":" )
hs.writelog("FlitserMonitor","Debug :" & Debug & ":" )
End If
'********************************
' If there are roads then split
' them into an array
'********************************
strRoads = Split(strRoadList," ")
intNumberOfRoads = UBound(strRoads)
If Debug = true Then hs.writelog("FlitserMonitor","Number of roads :" & intNumberOfRoads + 1)
'********************************
'Get Address and path of the URL
'********************************
strTemp = Replace(UCase(strURL), "HTTP://", "")
intTemp = Instr(strTemp, "/")
strSite = "http://" & Left(strTemp, intTemp - 1)
strPath = Right(strTemp, Len(strTemp) - intTemp + 1)
If Debug = true then
hs.writelog("FlitserMonitor","Site found :" & strSite )
hs.writelog("FlitserMonitor","Path found :" & strPath )
End If
'********************************
'Try to get the website data
'********************************
Try
strWebPage = hs.GetURL(strSite, strPath, false, 80)
If Len(strWebPage) < 100 Then
hs.writelog ("FlitserMonitor Error","No data found at " & strURL):exit sub
End If
Catch ex As Exception
hs.writelog("FlitserMonitor Error","Connection to site failed due:" & ex.Message )
Exit Sub
End Try
If Debug = true Then hs.writelog("FlitserMonitor","Byte(s) found :" & len(strWebPage) )
'********************************
'Now we have the complete webpage
'loaded into a string we can
'search for the road list block
'and drop all data infront
'and after the block.
'********************************
'Occassionally there is a negative string lenght which
'obviously isn't possible to caught this calculate the
'lenght first and make sure that it's => 0
CalcLenght = Len(strWebPage) - InStr(strWebPage, strRoadListTag) + 1
If CalcLenght < 0 Then CalcLenght = 0
strWebPage = "<ul class=" & Right(strWebPage, CalcLenght )
CalcLenght = InStr(strWebPage, strRoadListEndTag ) - 1
If CalcLenght < 0 Then CalcLenght = 0
strWebPage = Left(strWebPage, CalcLenght)
'********************************
'We have the data block.
'If the user wants this be split
'to seperate roads then do it
'here otherwise place all flitser
'information into one device.
'********************************
i = 0
Do
strOutput = "error"
If intNumberOfRoads > 0 Then
strTemp = FindRoadNumberBlock(strWebPage, strRoads(i), Debug)
Else
strTemp = strWebPage
End If
count = CountOccurrences(strTemp, "locatie" , false, Debug)
If strRoads(i) <> "" Then
strOutput = strRoads(i)
Else
strOutput = "Geheel NL"
End If
strOutput = strOutput & " - Totaal " & count & " flitsers" & vbCrLf
' Device Creation if not exists
DevExists = hs.DeviceExistsAddress("FlitserMonitor" & strRoads(i), False)
If DevExists = -1 Then
dv = hs.NewDeviceEx(strRoads(i))
dv.Location(hs) = strDeviceFloor
dv.Location2(hs) = strDeviceRoom
dv.Address(hs) = "FlitserMonitor" & strRoads(i)
dv.Device_Type_String(hs) = strDeviceType
dv.Image(hs) = "/images/FileFlitserMonitor/flitser.png"
Ref = hs.GetDeviceRef("FlitserMonitor" & strRoads(i))
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 0
GPair.RangeEnd = 0
GPair.Graphic = "/images/FileFlitserMonitor/OK.png"
hs.DeviceVGP_AddPair(ref, GPair)
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 1
GPair.RangeEnd = 100000000
GPair.Graphic = "/images/FileFlitserMonitor/Pasop.png"
hs.DeviceVGP_AddPair(ref, GPair)
hs.WriteLog("FlitserMonitor device creation", strRoads(i))
End If
If blDetailedInfo then strOutput = strOutput & strTemp
If Debug = true Then hs.writelog("FlitserMonitor","Number of traffic jams(s) on " & strRoads(i) & ":" & count )
Ref = hs.GetDeviceRef("FlitserMonitor" & strRoads(i))
If Debug = true Then hs.writelog("FlitserMonitor","deviceref " & Ref )
hs.SetDeviceString(Ref, strOutput , True)
hs.SetDeviceValueByRef(Ref, count , True)
if count = 0 then strOutput = ""
TotalString = TotalString & " " & strOutput
Total = Total + count
i = i + 1
Loop until i > intNumberOfRoads
If Total = 0 then TotalString = "Geen Flitsers"
'remove HTML CRAP
TotalString = TotalString.Replace("</ul>", "")
TotalString = TotalString.Replace("<ul>", "")
TotalString = TotalString.Replace("<br/>", ".")
TotalString = TotalString.Replace("</li>", "")
TotalString = TotalString.Replace("<ul class=""melding"">", "")
TotalString = TotalString.Replace("<li class=""list-detail"">", "")
TotalString = TotalString.Replace("<ul class=""list-road"">", "")
TotalString = TotalString.Replace("<li class=""locatie"">", "")
TotalString = TotalString.Replace("<li class=""situatie"">", "")
TotalString = TotalString.Replace("<li class=""vertraging"">", "")
TotalString = TotalString.Replace(">", "")
TotalString = TotalString.Replace("<", "")
' Device Creation if not exists
DevExists = hs.DeviceExistsAddress("FlitserMonitor Totaal", False)
If DevExists = -1 Then
dv = hs.NewDeviceEx("FlitserMonitor Totaal")
dv.Location(hs) = strDeviceFloor
dv.Location2(hs) = strDeviceRoom
dv.Address(hs) = "FlitserMonitor Totaal"
dv.Device_Type_String(hs) = strDeviceType
dv.Image(hs) = "/images/FileFlitserMonitor/flitser.png"
Ref = hs.GetDeviceRef("FlitserMonitor Totaal")
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 0
GPair.RangeEnd = 0
GPair.Graphic = "/images/FileFlitserMonitor/OK.png"
hs.DeviceVGP_AddPair(ref, GPair)
GPair = New VGPair
GPair.PairType = VSVGPairType.Range
GPair.RangeStart = 1
GPair.RangeEnd = 100000000
GPair.Graphic = "/images/FileFlitserMonitor/Pasop.png"
hs.DeviceVGP_AddPair(ref, GPair)
hs.WriteLog("FlitserMonitor device creation", "FlitserMonitor Totaal")
End If
If Debug = true Then hs.writelog("FlitserMonitor","Totaal Flitsers " & Total)
Ref = hs.GetDeviceRef("FlitserMonitor Totaal")
hs.SetDeviceString(Ref, TotalString , True)
hs.SetDeviceValueByRef(Ref, Total , True)
End sub
'===========================================
'== Functions and SUBS ==
'===========================================
Function FindRoadNumberBlock(strWebPage, Road, Debug)
Dim strWegData as String
Dim count as Integer
Dim CalcLenght as Integer
' If Debug = true Then hs.writelog("FlitserMonitor","FindRoadNumberBlock:Start function=" & strWebPage )
'Find start op road information
count = Instr(strWebPage, Road & "</li>")
If Debug = true Then hs.writelog("FlitserMonitor","FindRoadNumberBlock:Search for = " & Road )
If Debug = true Then hs.writelog("FlitserMonitor","FindRoadNumberBlock:Found at " & count )
'The requested road is found
if count > 0 then
'Drop the first part incl. the </li>
CalcLenght = Len(strWebPage) - (count + len(Road) + Len("</li>") )
If CalcLenght < 0 Then CalcLenght = 0
strWegData = "<ul><ul>" & Right(strWebPage, CalcLenght)
If Debug = true Then hs.writelog("FlitserMonitor","FindRoadNumberBlock:Right string=" & strWegData )
'Now find the end of this block
count = Instr(strWegData, "wegnummer")
'Drop the last part
count = count - Len("<li class=") - 2
If count < 0 Then count = 0
strWegData = Left(strWegData, count )
If Debug = true Then hs.writelog("FlitserMonitor","FindRoadNumberBlock:After left string=" & strWegData )
'Close a few tags to prevent unwanted
'generation of html code
strWegData = strWegData & "</ul></ul>"
If Debug = true Then hs.writelog("FlitserMonitor","FindRoadNumberBlock:ReturnString=" & strWegData )
End If
return strWegData
End Function
'------------------------------------------------
'function takes three parameters.
'The first is the string to be searched through.
'The second is the string to search for.
'The third is a Boolean that determines if the search
' should be case sensitive.
'The function returns the count of occurrences
Function CountOccurrences(p_strStringToCheck, p_strSubString, p_boolCaseSensitive, Debug)
Dim arrstrTemp
Dim strBase, strToFind
If Debug = true Then hs.writelog("FlitserMonitor","Start Count Occurences" )
If p_boolCaseSensitive Then
strBase = p_strStringToCheck
strToFind = p_strSubString
Else
strBase = LCase(p_strStringToCheck)
strToFind = LCase(p_strSubString)
End If
arrstrTemp = Split(strBase, strToFind)
CountOccurrences = UBound(arrstrTemp)
If Debug = true Then hs.writelog("FlitserMonitor","End Count Occurences : Count = " & CountOccurrences )
End Function