GARDENA Smart System, der er udviklet af Husqvarna Group, leverer innovative løsninger til styring og kontrol af dine have- og udendørsenheder. For at integrere og interagere med GARDENA Smart System kan udviklere udnytte Husqvarna Groups åbne API, som tilbyder en bekvem måde at få adgang til og styre forskellige smarte enheder ved hjælp af RESTful-webtjenester og JSON-dataformat.
I denne vejledning vil vi vise dig, hvordan du opretter forbindelse til GARDENA smart system ved hjælp af ASP.NET, et populært framework til opbygning af webapplikationer. Ved at udnytte kraften i RESTful API'er og JSON kan vi problemfrit kommunikere med GARDENA-enheder, hente oplysninger og udføre handlinger på afstand.
Implementering af godkendelsesflowet for at få et adgangstoken.
Hente en liste over tilgængelige enheder og deres egenskaber.
Kontrollere enhedstilstande, såsom at tænde/slukke eller justere indstillinger.
Overvåge enhedens status og hente data i realtid.
Parsning af JSON-svar fra GARDENA API.
Serialisering af JSON-data for at sende anmodninger og opdatere enhedsindstillinger.
Oprettelse af tidsplaner for automatiserede enhedsoperationer.
Håndtering af enhedsgrupper og zoner for effektiv kontrol.
Håndtering af hændelser og notifikationer fra GARDENA-systemet.
Bemærk: Før du starter implementeringen, skal du sørge for at registrere dig som udvikler og få API-legitimation.
Private Sub doAuth2()
Dim clientId As String = "xxxxxx-xxx-xxxx-xxxx-xx"
Dim clientSecret As String = "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
Dim accessToken As String = GetAccessToken(clientId, clientSecret)
Dim LocationID As String = "xxxxxxx-xxxx-xxxx-xxx-xxxxxxxx"
Response.Write(accessToken)
Response.Write(GetLanMoverList(accessToken, clientId))
Response.Write(GetLocations(accessToken, LocationID, clientId))
'End If
End Sub
Private Function GetAccessToken(clientId As String, clientSecret As String) As String
Dim tokenEndpoint As String = "https://api.authentication.husqvarnagroup.dev/v1/oauth2/token"
Dim redirectUri As String = "https://localhost:44306/"
Dim request As HttpWebRequest = CType(WebRequest.Create(tokenEndpoint), HttpWebRequest)
request.Method = "POST"
Dim postData As String = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&redirect_uri={2}", clientId, clientSecret, redirectUri)
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Dim serializer As New JavaScriptSerializer()
Dim tokenData As Dictionary(Of String, Object) = serializer.Deserialize(Of Dictionary(Of String, Object))(responseFromServer)
If tokenData.ContainsKey("access_token") Then
Return tokenData("access_token").ToString()
Else
Return ""
End If
End Function
Private Function GetLanMoverList(Token As String, clientId As String) As String
Dim tokenEndpoint As String = "https://api.amc.husqvarna.dev/v1/mowers"
Dim X_Api_Key As String = clientId
Dim Token_var As String = "Bearer " + Token
Dim Authorization_Provider As String = "husqvarna"
Dim request As HttpWebRequest = CType(WebRequest.Create(tokenEndpoint), HttpWebRequest)
request.Method = "GET"
request.Headers.Add("Authorization", Token_var)
request.Headers.Add("X-Api-Key", X_Api_Key)
request.Headers.Add("Authorization-Provider", Authorization_Provider)
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
Return responseFromServer
End Function
Private Function GetLocations(Token As String, LocationID As String, clientid As String) As String
Dim tokenEndpoint As String = "https://api.smart.gardena.dev/v1/locations" + "/" + LocationID
Dim X_Api_Key As String = clientid
Dim Token_var As String = "Bearer " + Token
Dim Authorization_Provider As String = "husqvarna"
Dim request As HttpWebRequest = CType(WebRequest.Create(tokenEndpoint), HttpWebRequest)
request.Method = "GET"
request.Headers.Add("Authorization", Token_var)
request.Headers.Add("X-Api-Key", X_Api_Key)
request.Headers.Add("Authorization-Provider", Authorization_Provider)
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
' Parse the JSON string into a JsonDocument
Dim jsonDocumentVar As JsonDocument = JsonDocument.Parse(responseFromServer)
Dim locationIdVar As String = jsonDocumentVar.RootElement.GetProperty("data").GetProperty("id").GetString()
Dim locationName As String = jsonDocumentVar.RootElement.GetProperty("data").GetProperty("attributes").GetProperty("name").GetString()
Return responseFromServer
End Function