Gå til innhold
  • Bli medlem

Jonnyhar

Medlemmer
  • Innholdsteller

    5
  • Ble med

  • Besøkte siden sist

Nettsamfunnsomdømme

5 Neutral

Om Jonnyhar

  • Rang
    Nybegynner

Hjemmeautomasjon

  • System
    HomeSeer

Nylige profilbesøk

Blokken for nylige besøkende er slått av og vises ikke for andre medlemmer.

  1. Hei igjen. Jeg har laget en oppdatert versjon av scriptet, der man bare trenger å angi a-postadresse, passord, og navn på gressklipper. Jeg la det her:
  2. 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 = "myemail@gmail.com" 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
  3. Takk Moskus! Da har jeg fått snekret sammen et .vb skript som fungerer på min Gardena R100Li Smart Sileno. Ganske unyttig, men du verden så tilfredstillende å be Alexa om å klippe plenen. 😄 Skriptet mitt krever at man vet token, location, og device-id. Jeg har brukt samme token siden i går og det fungerer fortsatt, men jeg mistenker at man må ha et nytt etterhvert.. Forresten: Jeg har scramblet min token, location, og device i kode-eksemplet.. De står der bare for eksemplets skyld, Imports System.IO Imports System.Net Sub Main(ByVal par As String) 'Kall skriptet med parameter start eller stop 'Følg steg 1 til 3 beskrevet her: http://www.roboter-forum.com/showthread.php?16777-Gardena-Smart-System-Analyse 'Og legg inn token, location, og device (for gressklipperen) 'Jeg bruker postman for å kjøre og teste slike kall Dim token As String = "9ba34642-0543-4545-86456-5692e5753d" Dim location As String = "fb45407e-96ea-4651-921d7-0278f7650d21" Dim device As String = "0bf7654cf-4659-455c-be56-1763d9945d6" Dim json As String if par = "start" Then json = "{""name"":""start_override_timer"", ""parameters"":{""duration"":1440}}" else if par = "stop" Then json = "{""name"":""park_until_further_notice""}" 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("Gressklipper", "Response: " & responseText) Catch ex As Exception : hs.writelog("Gressklipper", "Error: " & ex.Message.ToString) End Try End Sub
  4. Hei. Jeg har kjøpt samme klipper, og vil også gjerne be Alexa om å klippe plenen! Kommandoene i denne tyske forum-posten http://www.roboter-forum.com/showthread.php?16777-Gardena-Smart-System-Analyse fungerer aldeles utmerket når jeg tester det, men problemet er at jeg ikke får hs.URLAction til å sende med to headere. Man må angi både Content-Type og X-session som header. Jeg er nybegynner på homeseer, og vb.net. Er det noen andre her som har en homeseer SEL boks, og som har fått til å kjøre HTTP POST kall på en annen måte enn via hs.URLAction?
  5. BUMP. Er det noen som har lyktes å få til dette med flere headere siden sist? Jeg prøver fånyttes å integrere Gardena Sileno-klipperen min i en event. (Ved hjelp av disse kommandoene som fungerer helt fint når jeg tester i postman http://www.roboter-forum.com/showthread.php?16777-Gardena-Smart-System-Analyse ) Er det noen andre måter å kjøre et POST kall fra en homeseer SEL?
×
×
  • Opprett ny...