Gå til innhold
  • Bli medlem
Støtt hjemmeautomasjon.no!

Anbefalte innlegg

Skrevet (endret)

Hei.

 

Jeg har laget et script for å starte og parkere Gardena Smart Sileno gressklippere. Testet og fungerer på min Gardena Smart Sileno R100Li.

Legg inn e-postadressen, passordet, og navnet på klipperen på linje 12->14 i skriptet.

Sett logJson = True for å se alt i json-responsen fra Gardena. Det ligger mye nyttig informasjon i responsen fra get_device_id.

 

Kall scriptet med parameter start eller stop.

Disse parametrene blir så brukt lenger nede i scriptet til:

 

    'Available start commands:
    'Start according to schedule: "{""name"":""start_resume_schedule"", ""parameters"":{}}"
    'Start overriding schedule. Run for 1440 minutes: "{""name"":""start_override_timer"", ""parameters"":{""duration"": 1440}}"
    
    'Available stop commands:
    'Park and pause all schedules: "{""name"":""park_until_further_notice""}"
    'Park and start again at next schedule: "{""name"":""park_until_next_timer""}"

Imports System.IO
Imports System.Net
  
'*** Choose if json-responces should get written to the log ***
Const logJson As Boolean = False
'**************************************************************

Public Sub Main(ByVal command As String)

    '*** Put in e-mailadress and password and lawnmover-name  ***
    '*** They must be exactly as entered into the Gardena app ***
    Dim email As String = "[email protected]"  
    Dim password As String = "Mypassword"         
    Dim mower_name As String = "Name_of_lawnmower_in_gardena_app"
    '*************************************************
    '***     NO NEED TO EDIT BEHIND THIS POINT!    ***
    '*************************************************
    
    'Get token and username
    Dim jsonGetToken As String = "{""sessions"":{""email"":""" & email & """,""password"":""" & password & """}}"	
    Dim tokenAndUser() As String = getToken(jsonGetToken)
    
    'Get location
    Dim location As String = getLocation(tokenAndUser)
    
    'Get device-id for lawnmover set in mower_name
    Dim locationAndToken(3) as String
    locationAndToken(0) = tokenAndUser(0)
    locationAndToken(1) = location
    locationAndToken(2) = mower_name
    Dim deviceId As String = getdeviceId(locationAndToken)
    
    'Send start or stop command to mower
    Dim data(4) as String
    data(0) = tokenAndUser(0)
    data(1) = location
    data(2) = deviceId
    data(3) = command
    sendCommand(data)
End Sub

Public Function getToken(json As string) As String()
    'Get token and user ID based on e-mail and password
    Dim strURL As String = "https://sg-api.dss.husqvarnagroup.net/sg-1/sessions"
    Dim myWebReq As HttpWebRequest
    Dim myWebResp As HttpWebResponse
    Dim encoding As New System.Text.UTF8Encoding
    Dim sr As StreamReader
    Dim result(2) As String
    
    Try
    
        Dim data As Byte() = encoding.GetBytes(json)
        myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
        myWebReq.ContentType = "application/json"
        myWebReq.ContentLength = data.Length
        myWebReq.Method = "POST"

        Dim myStream As Stream = myWebReq.GetRequestStream()

        If data.Length > 0 Then
            myStream.Write(data, 0, data.Length)
            myStream.Close()
        End If

        myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
        sr = New StreamReader(myWebResp.GetResponseStream())
        Dim responseText As String = sr.ReadToEnd()
        
        if logJson Then
            hs.WriteLog("Gardena", "Response getToken: " & responseText)
        End If
        
        'Search json for token
        Dim sSource As String = responseText 'String that is being searched
        Dim sDelimStart As String = """token"":""" 'First delimiting word
        Dim sDelimEnd As String = """,""user_id" 'Second delimiting word
        Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
        Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

        If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
            Dim token As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
            hs.WriteLog("Gardena", "Token: " & token)

            'We got the token, continue to get the user id
            sDelimStart = "user_id"":""" 'First delimiting word
            sDelimEnd = """,""refresh_token" 'Second delimiting word
            nIndexStart = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
            nIndexEnd = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

            If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
                
                Dim user As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
                hs.WriteLog("Gardena", "User: " & user)
                result(0) = token
                result(1) = user
                return result
                
            Else
            
                hs.WriteLog("Gardena", "Error: Did not receive user ID")
            
            End If
            
        Else
            
            hs.WriteLog("Gardena", "Error: Did not receive token")
        
        End If

    Catch ex As Exception : hs.writelog("Gardena", "Error:  " & ex.Message.ToString)
    
    End Try

End Function 

Public Function getLocation(tokenAndUser() As String) As String
    'Get location based on token and user id
    Dim token As String = tokenAndUser(0)
    Dim user As String = tokenAndUser(1)
    Dim strURL As String = "https://sg-api.dss.husqvarnagroup.net/sg-1/locations/?user_id=" & user
    Dim myWebReq As HttpWebRequest
    Dim myWebResp As HttpWebResponse
    Dim sr As StreamReader
    
    Try
        myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
        myWebReq.ContentType = "application/json"
        myWebReq.Method = "GET"
        myWebReq.Headers.Add("X-session", token)

        myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
        sr = New StreamReader(myWebResp.GetResponseStream())
        Dim responseText As String = sr.ReadToEnd()
        
        if logJson Then
            hs.WriteLog("Gardena", "Response getLocation: " & responseText)
        End if
        
        'Search json for location
        Dim sSource As String = responseText 'String that is being searched
        Dim sDelimStart As String = """id"":""" 'First delimiting word
        Dim sDelimEnd As String = """,""name" 'Second delimiting word
        Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
        Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

        If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
            Dim location As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
            hs.WriteLog("Gardena", "Location: " & location)
            return location

        Else
            
            hs.WriteLog("Gardena", "Error: Did not receive location")
        
        End If

    Catch ex As Exception : hs.writelog("Gardena", "Error:  " & ex.Message.ToString)
    
    End Try

End Function

Public Function getdeviceId(tokenAndLocation() As String) As String
    'Get device id for lawnmover based on location and mower_name
    Dim token As String = tokenAndLocation(0)
    Dim location As String = tokenAndLocation(1)
    Dim mower_name As String = tokenAndLocation(2)
    Dim strURL As String = "https://sg-api.dss.husqvarnagroup.net/sg-1/devices?locationId=" & location
    Dim myWebReq As HttpWebRequest
    Dim myWebResp As HttpWebResponse
    Dim sr As StreamReader
    
    Try
        myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
        myWebReq.ContentType = "application/json"
        myWebReq.Method = "GET"
        myWebReq.Headers.Add("X-session", token)

        myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
        sr = New StreamReader(myWebResp.GetResponseStream())
        Dim responseText As String = sr.ReadToEnd()
        
        if logJson Then
            hs.WriteLog("Gardena", "Response getLocation: " & responseText)
        End If
        
        'Finn Device ID
        Dim sSource As String = responseText 'String that is being searched
        Dim sDelimEnd As String = """,""name"":""" & mower_name & """,""category"":""mower""" 'Second delimiting word
        Dim nIndexStart As Integer = sSource.IndexOf(sDelimEnd) - 36
        Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

        If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
            
            Dim deviceId As String = Strings.Mid(sSource, nIndexStart + 1, nIndexEnd - nIndexStart) 'Crop the text between
            hs.WriteLog("Gardena", "Device ID: " & deviceId)
            return deviceId

        Else
            
            hs.WriteLog("Gardena", "Error: Did not receive device ID")
        
        End If

    Catch ex As Exception : hs.writelog("Gardena", "Error:  " & ex.Message.ToString)
    
    End Try

End Function

Public Function sendCommand(data() As String)
    'Send start or stop to the mower.
    Dim token As String = data(0)
    Dim location As String = data(1)
    Dim device As String = data(2)
    Dim command As String = data(3)
    Dim json As String
    
    '*** Here you can change what start and stop does.***
    'Available start commands:
    'Start according to schedule: "{""name"":""start_resume_schedule"", ""parameters"":{}}"
    'Start overriding schedule. Run for 1440 minutes: "{""name"":""start_override_timer"", ""parameters"":{""duration"": 1440}}"
    
    'Available stop commands:
    'Park and pause all schedules: "{""name"":""park_until_further_notice""}"
    'Park and start again at next schedule: "{""name"":""park_until_next_timer""}"
      
    if command = "start" Then
        json = "{""name"":""start_resume_schedule"", ""parameters"":{}}"
    else if command = "stop" Then
        json = "{""name"":""park_until_further_notice""}"
    else
        hs.writelog("Gardena", "Error: Angi start eller stop som parameter")
    End If	

	Dim strURL As String = "https://sg-api.dss.husqvarnagroup.net/sg-1/devices/" & device & "/abilities/mower/command?locationId=" & location
	Dim myWebReq As HttpWebRequest
	Dim myWebResp As HttpWebResponse
	Dim encoding As New System.Text.UTF8Encoding
	Dim sr As StreamReader
	
	Try
		Dim data As Byte() = encoding.GetBytes(json)
		myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
		myWebReq.ContentType = "application/json"
		myWebReq.ContentLength = data.Length
        myWebReq.Headers.Add("X-session", token)
		myWebReq.Method = "POST"
        
		Dim myStream As Stream = myWebReq.GetRequestStream()
        
		If data.Length > 0 Then
			myStream.Write(data, 0, data.Length)
			myStream.Close()
		End If
		
		myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
		sr = New StreamReader(myWebResp.GetResponseStream())
		Dim responseText As String = sr.ReadToEnd()
        hs.WriteLog("Gardena", "Sent command: " & command)

    Catch ex As Exception : hs.writelog("Gardena", "Error: " & ex.Message.ToString)
	End Try
End Function

 

Endret av Jonnyhar
Lagt til tilgjengelige kommandoer i skriptet
  • Like 1
  • Thanks 2
  • 3 år senere...

Bli med i samtalen

Du kan publisere innhold nå og registrere deg senere. Hvis du har en konto, logg inn nå for å poste med kontoen din.

Gjest
Skriv svar til emnet...

×   Du har limt inn tekst med formatering.   Lim inn uten formatering i stedet

  Du kan kun bruke opp til 75 smilefjes.

×   Lenken din har blitt bygget inn på siden automatisk.   Vis som en ordinær lenke i stedet

×   Tidligere tekst har blitt gjenopprettet.   Tøm tekstverktøy

×   Du kan ikke lime inn bilder direkte. Last opp eller legg inn bilder fra URL.

×
×
  • Opprett ny...

Viktig informasjon

Vi har plassert informasjonskapsler/cookies på din enhet for å gjøre denne siden bedre. Du kan justere dine innstillinger for informasjonskapsler, ellers vil vi anta at dette er ok for deg.