15.06.2023, 18:02
(This post was last modified: 15.06.2023, 18:06 by pioneersteffen.)
Hi Admin,
many thanks for your help and feedback.
I tried to implement your hints:
Unfortunately I getting the following failure:
I think the body might be in the wrong format?!
Any ideas how to improve?
Many thanks for your help!
Best Regards
Steffen
many thanks for your help and feedback.
I tried to implement your hints:
Code:
local https = require('ssl.https')
local json = require('json')
-- Husqvarna Authentication API
local authEndpoint = 'https://api.authentication.husqvarnagroup.dev/v1/oauth2/token'
local clientId = 'xxx'
local clientSecret = 'xxx'
-- Husqvarna Automower Connect API
local connectEndpoint = 'https://api.amc.husqvarna.dev/v1/mowers/ba15739b-0449-411a-9eb9-9ad866362075/actions'
local accessToken
-- Funktion zum Erhalten eines Zugriffstokens
function getAccessToken()
local requestBody = 'grant_type=client_credentials'
.. '&client_id=' .. clientId
.. '&client_secret=' .. clientSecret
local response_body = {}
local _, code, _, _ = https.request{
url = authEndpoint,
method = 'POST',
headers = {
['Content-Type'] = 'application/x-www-form-urlencoded',
['Content-Length'] = #requestBody
},
source = ltn12.source.string(requestBody),
sink = ltn12.sink.table(response_body)
}
if code == 200 then
local response = table.concat(response_body)
local jsonResponse = json.decode(response)
accessToken = jsonResponse.access_token
else
-- Fehlerbehandlung
log('Fehler beim Abrufen des Zugriffstokens:', code)
end
end
-- Funktion zum Senden des Startbefehls
function sendStartCommand()
local requestBody = '{"type: 'Start', attributes: { duration: 24 // minutes}'
local response_body = {}
local _, code, _, _ = https.request{
url = connectEndpoint,
method = 'POST',
headers = {
['Authorization'] = 'Bearer ' .. accessToken,
['X-Api-Key'] = clientId,
['Authorization-Provider'] = 'husqvarna',
['Content-Type'] = 'application/vnd.api+json',
['Content-Length'] = #requestBody
},
source = ltn12.source.string(requestBody),
sink = ltn12.sink.table(response_body)
}
log (code, status, headers, response_body)
if code == 200 then
-- Erfolgreich gestartet
log('Automower wurde gestartet.')
else
-- Fehlerbehandlung
log('Fehler beim Starten des Automowers:', code)
end
end
-- Funktion zum Senden des Stopbefehls
function sendStopCommand()
local requestBody = '{"name": "stop"}'
local response_body = {}
local _, code, _, _ = https.request{
url = connectEndpoint,
method = 'POST',
headers = {
['Authorization'] = 'Bearer ' .. accessToken,
['X-Api-Key'] = clientId,
['Authorization-Provider'] = 'husqvarna',
['Content-Type'] = 'application/json',
['Content-Length'] = #requestBody
},
source = ltn12.source.string(requestBody),
sink = ltn12.sink.table(response_body)
}
log (code, status, headers, response_body)
if code == 200 then
-- Erfolgreich gestoppt
log('Automower wurde gestoppt.')
else
-- Fehlerbehandlung
log('Fehler beim Stoppen des Automowers:', code)
end
end
-- Beispielaufrufe
getAccessToken()
sendStartCommand()
Unfortunately I getting the following failure:
Code:
string: {"errors":[{"id":"e9f44309-1dfb-48bf-a0b3-dcab8111951d","status":"500","code":"internal.error","title":"Internal error","detail":""}]}
I think the body might be in the wrong format?!
Code:
local requestBody = '{"type: 'Start', attributes: { duration: 24 // minutes}'
Any ideas how to improve?
Many thanks for your help!
Best Regards
Steffen