This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Husqvarna lawn robot mover API
#3
Hi Admin,

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
Reply


Messages In This Thread
RE: Husqvarna lawn robot mover API - by admin - 12.06.2023, 12:27
RE: Husqvarna lawn robot mover API - by pioneersteffen - 15.06.2023, 18:02
RE: Husqvarna lawn robot mover API - by admin - 16.06.2023, 07:33
RE: Husqvarna lawn robot mover API - by Joep - 31.07.2023, 14:40
RE: Husqvarna lawn robot mover API - by admin - 19.06.2023, 08:00
RE: Husqvarna lawn robot mover API - by Joep - 06.09.2023, 15:38

Forum Jump: