1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| -- Installation de luasec pour le https
-- sudo apt-get install libssl1.0.0 libssl-dev
-- sudo luarocks install luasec OPENSSL_LIBDIR=/usr/lib/x86_64-linux-gnu (Ubuntu)
-- sudo luarocks install luasec OPENSSL_LIBDIR=/usr/lib/arm-linux-gnueabihf (Raspberry Pi)
local https = require("ssl.https")
local identifiant = 'login Myfox'
local motDePasse = 'Mot de passe Myfox'
local clientID = 'Client ID généré sur le site Myfox'
local clientSecret = 'Client secret généré sur le site Myfox'
local siteId = 'num. du site'
local deviceID = 'num. du device'
local access_token = nil
local refresh_token = nil
local echeance = nil
-- Request access token
function acquerirJeton()
local url = 'https://api.myfox.me/oauth2/token'
local post = string.format('grant_type=password&client_id=%s&client_secret=%s&username=%s&password=%s', clientID, clientSecret, identifiant, motDePasse)
local body, code = https.request(url, post)
assert(code == 200)
access_token, refresh_token = string.match(body, '"access_token":"([^"]*)".*"refresh_token":"([^"]*)"')
end
-- Refreshing an expired access_token
function renouvelerJeton()
local url = 'https://api.myfox.me/oauth2/token'
local post = string.format('grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s', refresh_token, clientID, clientSecret)
local body, code = https.request(url, post)
assert(code == 200)
access_token, refresh_token = string.match(body, '"access_token":"([^"]*)".*"refresh_token":"([^"]*)"')
end
-- Statut de l'alarme : 'disarmed', 'partial' ou 'armed'
function statut()
local url = string.format('https://api.myfox.me:443/v2/site/%s/device/%s/heater/off?access_token=%s', tostring(siteId), tostring(deviceID), access_token)
local body, code = https.request(url)
assert(code == 200)
local statut = string.match(body, '"statusLabel":"([^"]*)"')
return statut
end |
Partager