Page 3 of 5

Re: Afvalwijzer script

Posted: Thu Sep 17, 2015 4:10 pm
by stefxx
Zoveel geld heb je niet :D :twisted:

Re: Afvalwijzer script

Posted: Thu Sep 17, 2015 4:11 pm
by stefxx
Maar even eerlijk, het script is gewoon vrijwel klaar. Al het moeilijke werk is al gedaan. Er is vast wel iemand die de laatste puntjes op de I kan zetten, inclusief de meeste uit deze thread met een beetje goede wil en doorzettingsvermogen! :roll:

Re: Afvalwijzer script

Posted: Sun Oct 04, 2015 12:10 pm
by RdP
Hoi,

Ik heb eindelijk weer tijd om wat te gaan knutselen :D

Eerst maar eens het afvalwijzer script aangepakt en werkt nu voor HS3, nog toegevoegd dat het script ook het juiste icon laat zien (wel even zelf naar je HS directory kopiƫren, of direct ophalen via de mijnafvalijzer site (maar ik heb ze zelf liever lokaal))
Maak zelf een status device in HS, onthoud de device reference want dat heb je nodig in het scripts. Zorg dat je 4 device value graphics toevoegt
Device value 1 = restafval, 2 = gft, 3 = papier, 4=plastic
2015-10-04_1202.png
2015-10-04_1202.png (7.95 KiB) Viewed 15107 times
2015-10-04_1217.png
2015-10-04_1217.png (110.67 KiB) Viewed 15107 times
Succes.

Code: Select all

Public Sub Main()
Dim Postcode, Huisnummer, Toevoeging, DevRef, Debug
Dim objHttp, Text, i, Ophaaldagen
Dim Datum, d

' Vul hier je eigen gegevens in
Postcode = "1234ab"
Huisnummer = "88"
Toevoeging = ""
DevRef="999"
Debug="0"  '0=off, 1=on


' Ander test adres
'Postcode = "8266bh"
'Huisnummer = "1"
'Toevoeging = ""

Set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
objHttp.Open "GET", "http://json.mijnafvalwijzer.nl/?method=postcodecheck&postcode=" & Postcode & "&street=&huisnummer=" & Huisnummer & "&toevoeging=" & Toevoeging & _
    "&platform=phone&langs=nl&mobiletype=android&platform=phone&version=33%20HTTP/1.1", False
objHttp.Send
Text = objHttp.ResponseText
Set objHttp = Nothing

If Len(Text) > 10 Then

    Set oJSON = New aspJSON
    oJSON.loadJSON(Text)

    'Get single value
    if Debug = "1" then hs.writelog "afval", "Response: " & oJSON.data("response")
    if Debug = "1" then hs.writelog "afval", "Gemeente: " & oJSON.data("data")("gemeente")
    Datum = Year(now()) & "-" & Right("0" & Month(Now()),2) & "-" & Right("0" & Day(Now()),2)
    if Debug = "1" then hs.writelog "afval", "Datum: " & chr(13) & Datum
    Ophaaldagen = ""
    For Each i In oJSON.data("data")("ophaaldagen")("data")
        Set this = oJSON.data("data")("ophaaldagen")("data").item(i)
        Ophaaldagen = Ophaaldagen & this.item("type") & ": " & this.item("date") & this.item("type") & ": " & this.item("date") & chr(13)
        d = this.item("date")
        if Debug = "1" then hs.writelog "afval", "d: " & chr(13) & d
        If d >= Datum Then
          Hs.setdevicestring DevRef, this.item("type") & ": " & this.item("date"), true
	  if this.item("type") = "restafval" then Hs.setdevicevaluebyref DevRef, 1, true
	  if this.item("type") = "gft" then Hs.setdevicevaluebyref DevRef, 2, true
	  if this.item("type") = "papier" then Hs.setdevicevaluebyref DevRef, 3, true
	  if this.item("type") = "plastic" then Hs.setdevicevaluebyref DevRef, 4, true
          Exit For
        End If
    Next
    If Debug = "1" then hs.writelog "afval", "Ophaaldagen: " & chr(13) & Ophaaldagen

Else   

    if Debug = "1" then hs.writelog "afval", "No data received. Location probably not supported by mijnafvalwijzer.nl."
   
End If

End Sub

'Februari 2014 - Version 1.17 by Gerrit van Kuipers
Class aspJSON
   Public data
   Private p_JSONstring
   private aj_in_string, aj_in_escape, aj_i_tmp, aj_char_tmp, aj_s_tmp, aj_line_tmp, aj_line, aj_lines, aj_currentlevel, aj_currentkey, aj_currentvalue, aj_newlabel, aj_XmlHttp, aj_RegExp, aj_colonfound

   Private Sub Class_Initialize()
      Set data = Collection()

       Set aj_RegExp = new regexp
       aj_RegExp.Pattern = "\s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}"
       aj_RegExp.Global = False
       aj_RegExp.IgnoreCase = True
       aj_RegExp.Multiline = True
   End Sub

   Private Sub Class_Terminate()
      Set data = Nothing
       Set aj_RegExp = Nothing
   End Sub

   Public Sub loadJSON(inputsource)
      inputsource = aj_MultilineTrim(inputsource)
      If Len(inputsource) = 0 Then Err.Raise 1, "loadJSON Error", "No data to load."
      
      select case Left(inputsource, 1)
         case "{", "["
         case else
            Set aj_XmlHttp = CreateObject("Msxml2.ServerXMLHTTP")
            aj_XmlHttp.open "GET", inputsource, False
            aj_XmlHttp.setRequestHeader "Content-Type", "text/json"
            aj_XmlHttp.setRequestHeader "CharSet", "UTF-8"
            aj_XmlHttp.Send
            inputsource = aj_XmlHttp.responseText
            set aj_XmlHttp = Nothing
      end select

      p_JSONstring = CleanUpJSONstring(inputsource)
      aj_lines = Split(p_JSONstring, Chr(13) & Chr(10))

      Dim level(99)
      aj_currentlevel = 1
      Set level(aj_currentlevel) = data
      For Each aj_line In aj_lines
         aj_currentkey = ""
         aj_currentvalue = ""
         If Instr(aj_line, ":") > 0 Then
            aj_in_string = False
            aj_in_escape = False
            aj_colonfound = False
            For aj_i_tmp = 1 To Len(aj_line)
               If aj_in_escape Then
                  aj_in_escape = False
               Else
                  Select Case Mid(aj_line, aj_i_tmp, 1)
                     Case """"
                        aj_in_string = Not aj_in_string
                     Case ":"
                        If Not aj_in_escape And Not aj_in_string Then
                           aj_currentkey = Left(aj_line, aj_i_tmp - 1)
                           aj_currentvalue = Mid(aj_line, aj_i_tmp + 1)
                           aj_colonfound = True
                           Exit For
                        End If
                     Case "\"
                        aj_in_escape = True
                  End Select
               End If
            Next
            if aj_colonfound then
               aj_currentkey = aj_Strip(aj_JSONDecode(aj_currentkey), """")
               If Not level(aj_currentlevel).exists(aj_currentkey) Then level(aj_currentlevel).Add aj_currentkey, ""
            end if
         End If
         If right(aj_line,1) = "{" Or right(aj_line,1) = "[" Then
            If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
            Set level(aj_currentlevel).Item(aj_currentkey) = Collection()
            Set level(aj_currentlevel + 1) = level(aj_currentlevel).Item(aj_currentkey)
            aj_currentlevel = aj_currentlevel + 1
            aj_currentkey = ""
         ElseIf right(aj_line,1) = "}" Or right(aj_line,1) = "]" or right(aj_line,2) = "}," Or right(aj_line,2) = "]," Then
            aj_currentlevel = aj_currentlevel - 1
         ElseIf Len(Trim(aj_line)) > 0 Then
            if Len(aj_currentvalue) = 0 Then aj_currentvalue = aj_line
            aj_currentvalue = getJSONValue(aj_currentvalue)

            If Len(aj_currentkey) = 0 Then aj_currentkey = level(aj_currentlevel).Count
            level(aj_currentlevel).Item(aj_currentkey) = aj_currentvalue
         End If
      Next
   End Sub

   Public Function Collection()
      set Collection = CreateObject("Scripting.Dictionary")
   End Function

   Public Function AddToCollection(dictobj)
      if TypeName(dictobj) <> "Dictionary" then Err.Raise 1, "AddToCollection Error", "Not a collection."
      aj_newlabel = dictobj.Count
      dictobj.Add aj_newlabel, Collection()
      set AddToCollection = dictobj.item(aj_newlabel)
   end function

   Private Function CleanUpJSONstring(aj_originalstring)
      aj_originalstring = Replace(aj_originalstring, Chr(13) & Chr(10), "")
      aj_originalstring = Mid(aj_originalstring, 2, Len(aj_originalstring) - 2)
      aj_in_string = False : aj_in_escape = False : aj_s_tmp = ""
      For aj_i_tmp = 1 To Len(aj_originalstring)
         aj_char_tmp = Mid(aj_originalstring, aj_i_tmp, 1)
         If aj_in_escape Then
            aj_in_escape = False
            aj_s_tmp = aj_s_tmp & aj_char_tmp
         Else
            Select Case aj_char_tmp
               Case "\" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_escape = True
               Case """" : aj_s_tmp = aj_s_tmp & aj_char_tmp : aj_in_string = Not aj_in_string
               Case "{", "["
                  aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
               Case "}", "]"
                  aj_s_tmp = aj_s_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10)) & aj_char_tmp
               Case "," : aj_s_tmp = aj_s_tmp & aj_char_tmp & aj_InlineIf(aj_in_string, "", Chr(13) & Chr(10))
               Case Else : aj_s_tmp = aj_s_tmp & aj_char_tmp
            End Select
         End If
      Next

      CleanUpJSONstring = ""
      aj_s_tmp = split(aj_s_tmp, Chr(13) & Chr(10))
      For Each aj_line_tmp In aj_s_tmp
         aj_line_tmp = replace(replace(aj_line_tmp, chr(10), ""), chr(13), "")
         CleanUpJSONstring = CleanUpJSONstring & aj_Trim(aj_line_tmp) & Chr(13) & Chr(10)
      Next
   End Function

   Private Function getJSONValue(ByVal val)
      val = Trim(val)
      If Left(val,1) = ":"  Then val = Mid(val, 2)
      If Right(val,1) = "," Then val = Left(val, Len(val) - 1)
      val = Trim(val)

      Select Case val
         Case "true"  : getJSONValue = True
         Case "false" : getJSONValue = False
         Case "null" : getJSONValue = Null
         Case Else
            If (Instr(val, """") = 0) Then
               If IsNumeric(val) Then
                  getJSONValue = CDbl(val)
               Else
                  getJSONValue = val
               End If
            Else
               If Left(val,1) = """" Then val = Mid(val, 2)
               If Right(val,1) = """" Then val = Left(val, Len(val) - 1)
               getJSONValue = aj_JSONDecode(Trim(val))
            End If
      End Select
   End Function

   Private JSONoutput_level
   Public Function JSONoutput()
      dim wrap_dicttype, aj_label
      JSONoutput_level = 1
      wrap_dicttype = "[]"
      For Each aj_label In data
          If Not aj_IsInt(aj_label) Then wrap_dicttype = "{}"
      Next
      JSONoutput = Left(wrap_dicttype, 1) & Chr(13) & Chr(10) & GetDict(data) & Right(wrap_dicttype, 1)
   End Function

   Private Function GetDict(objDict)
      dim aj_item, aj_keyvals, aj_label, aj_dicttype
      For Each aj_item In objDict
         Select Case TypeName(objDict.Item(aj_item))
            Case "Dictionary"
               GetDict = GetDict & Space(JSONoutput_level * 4)
               
               aj_dicttype = "[]"
               For Each aj_label In objDict.Item(aj_item).Keys
                   If Not aj_IsInt(aj_label) Then aj_dicttype = "{}"
               Next
               If aj_IsInt(aj_item) Then
                  GetDict = GetDict & (Left(aj_dicttype,1) & Chr(13) & Chr(10))
               Else
                  GetDict = GetDict & ("""" & aj_JSONEncode(aj_item) & """" & ": " & Left(aj_dicttype,1) & Chr(13) & Chr(10))
               End If
               JSONoutput_level = JSONoutput_level + 1
               
               aj_keyvals = objDict.Keys
               GetDict = GetDict & (GetSubDict(objDict.Item(aj_item)) & Space(JSONoutput_level * 4) & Right(aj_dicttype,1) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1),"" , ",") & Chr(13) & Chr(10))
            Case Else
               aj_keyvals =  objDict.Keys
               GetDict = GetDict & (Space(JSONoutput_level * 4) & aj_InlineIf(aj_IsInt(aj_item), "", """" & aj_JSONEncode(aj_item) & """: ") & WriteValue(objDict.Item(aj_item)) & aj_InlineIf(aj_item = aj_keyvals(objDict.Count - 1),"" , ",") & Chr(13) & Chr(10))
         End Select
      Next
   End Function

   Private Function aj_IsInt(val)
      aj_IsInt = (TypeName(val) = "Integer" Or TypeName(val) = "Long")
   End Function

   Private Function GetSubDict(objSubDict)
      GetSubDict = GetDict(objSubDict)
      JSONoutput_level= JSONoutput_level -1
   End Function

   Private Function WriteValue(ByVal val)
      Select Case TypeName(val)
         Case "Double", "Integer", "Long": WriteValue = val
         Case "Null"                  : WriteValue = "null"
         Case "Boolean"               : WriteValue = aj_InlineIf(val, "true", "false")
         Case Else                  : WriteValue = """" & aj_JSONEncode(val) & """"
      End Select
   End Function

   Private Function aj_JSONEncode(ByVal val)
      val = Replace(val, "\", "\\")
      val = Replace(val, """", "\""")
      'val = Replace(val, "/", "\/")
      val = Replace(val, Chr(8), "\b")
      val = Replace(val, Chr(12), "\f")
      val = Replace(val, Chr(10), "\n")
      val = Replace(val, Chr(13), "\r")
      val = Replace(val, Chr(9), "\t")
      aj_JSONEncode = Trim(val)
   End Function

   Private Function aj_JSONDecode(ByVal val)
      val = Replace(val, "\""", """")
      val = Replace(val, "\\", "\")
      val = Replace(val, "\/", "/")
      val = Replace(val, "\b", Chr(8))
      val = Replace(val, "\f", Chr(12))
      val = Replace(val, "\n", Chr(10))
      val = Replace(val, "\r", Chr(13))
      val = Replace(val, "\t", Chr(9))
      aj_JSONDecode = Trim(val)
   End Function

   Private Function aj_InlineIf(condition, returntrue, returnfalse)
      If condition Then aj_InlineIf = returntrue Else aj_InlineIf = returnfalse
   End Function

   Private Function aj_Strip(ByVal val, stripper)
      If Left(val, 1) = stripper Then val = Mid(val, 2)
      If Right(val, 1) = stripper Then val = Left(val, Len(val) - 1)
      aj_Strip = val
   End Function

   Private Function aj_MultilineTrim(TextData)
      aj_MultilineTrim = aj_RegExp.Replace(TextData, "$1")
   End Function

   private function aj_Trim(val)
      aj_Trim = Trim(val)
      Do While Left(aj_Trim, 1) = Chr(9) : aj_Trim = Mid(aj_Trim, 2) : Loop
      Do While Right(aj_Trim, 1) = Chr(9) : aj_Trim = Left(aj_Trim, Len(aj_Trim) - 1) : Loop
      aj_Trim = Trim(aj_Trim)
   end function
End Class

Re: Afvalwijzer script

Posted: Sun Oct 04, 2015 9:36 pm
by esschenk
Hallo Rien,

Dank je voor het aanpassen van het script
Ik krijg echter een foutmelding bij het runnen van het script.
Heb je enig idee ,Ik heb hem weggeschreven als een .vb

Compiling script C:\HomeSeer3\scripts\afvalscript.vb: Functie GetDict retourneert niet voor alle codepaden een waarde. Als het resultaat wordt gebruikt, kan een uitzondering voor een null-verwijzing optreden tijdens runtime.
okt-04 21:26:22 Error Compiling script C:\HomeSeer3\scripts\afvalscript.vb: Variabele GetDict wordt gebruikt voordat hieraan een waarde is toegewezen. Hierdoor kan een uitzondering voor een null-verwijzing optreden tijdens runtime.
okt-04 21:26:22 Error Compiling script C:


Gr Ed

Re: Afvalwijzer script

Posted: Mon Oct 05, 2015 7:35 am
by RdP
Hi Ed,

het moet weggeschreven worden als VBS....

Re: Afvalwijzer script

Posted: Mon Oct 05, 2015 8:07 am
by esschenk
Hallo Rien,


Thanks dat was het.


Ed

Re: Afvalwijzer script

Posted: Mon Oct 05, 2015 11:46 am
by mrqnapper
Prachtig het werkt nu wel. Super bedankt :D


Groet,
W.

Re: Afvalwijzer script

Posted: Tue Oct 06, 2015 7:32 pm
by geert-jan
Dank!

Script werkt prima, de extensie .vbs is essentieel :-)
Ik heb ook de kerstbomenophaaldag (goed scrabblewoord) toegevoegd:

Code: Select all

 if this.item("type") = "kerstbomen" then Hs.setdevicevaluebyref DevRef, 5, true
(Vergeet dan niet het icon te downloaden van mijnafvalwijzer)

Geert-Jan

Re: Afvalwijzer script

Posted: Tue Oct 06, 2015 9:57 pm
by JoopK
Mooi dat het nu ook in HS3 werkt. Ikzelf ben nog steeds niet begonnen met de migratie.
Zelf run ik nog een scriptje om 21:00 iedere avond welke verteld welke bak aan de straat moet (Gewoon de description laten uitspreken), en een hue lamp in een passende kleur laat knipperen.
Met datediff kan je bepalen of er morgenochten een bak aan de straat moet.

Re: Afvalwijzer script

Posted: Wed Oct 07, 2015 7:09 am
by raymonvdm
Bij mij zegt hij vandaag textiel met het plaatje van plastic :-) Dus ik moet even kijken of die er ook tussen staat in het script

Nu wel:

Code: Select all

     if this.item("type") = "textiel" then Hs.setdevicevaluebyref DevRef, 5, true
Het nadeel is wel dat HSTouch de devices niet snapt. Ik moet daar handmatig een icoon aan een value koppelen. Dubbel werk maar dat zal een HS3/HSTouch dingetje zijn denk ik want dat probleem zie ik vaker. Verder echt een top script. The wife is happy :D

Re: Afvalwijzer script

Posted: Wed Oct 07, 2015 8:10 am
by RdP
Hoi,

Bij ons komen ze niet voor zoveel zaken langs, dus ik had 'alleen' de vier klikko's er in gezet. Maar is natuurlijk makkelijk door iedereen aan te vullen.

Re: Afvalwijzer script

Posted: Wed Oct 07, 2015 3:51 pm
by Karell
mooi script , alleen mijn locatie word niet ondersteund :(

Re: Afvalwijzer script

Posted: Thu Oct 08, 2015 7:41 am
by raymonvdm
Het viel me op dat de datum in de amerikaanse notatie staa. Hoe kan ik dat nog aanpassen ?

Re: Afvalwijzer script

Posted: Mon Oct 12, 2015 12:01 pm
by DrDoo
Bedankt voor het script!
Heeft iemand een idee hoe ik een event moet maken wat de avond van te voren een melding geeft? (Mail/pushover of wat dan ook).
Ik laat dit script elke 12u lopen maar zou dus graag de avond van te voren nog een mail reminder krijgen.

Iemand een idee?

Re: Afvalwijzer script

Posted: Mon Oct 12, 2015 1:38 pm
by raymonvdm
DrDoo wrote:Bedankt voor het script!
Heeft iemand een idee hoe ik een event moet maken wat de avond van te voren een melding geeft? (Mail/pushover of wat dan ook).
Ik laat dit script elke 12u lopen maar zou dus graag de avond van te voren nog een mail reminder krijgen.

Iemand een idee?
Gewoon een los event maken die s`avond om 19:00 de status van het device naar je toe stuurt ?

http://board.homeseer.com/showthread.php?t=165676