12.08.2019, 10:23
Here's a working example, it will log all devices from your account.
Code:
email = 'test@test.com'
password = '123456'
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')
function encodepost(t)
local res = {}
local esc = require('socket.url').escape
for k, v in pairs(t) do
res[ #res + 1 ] = esc(k) .. '=' .. esc(v)
end
return table.concat(res, '&')
end
function login(email, password)
local url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Login/ClientLogin'
local data = encodepost({
AppVersion = '1.9.3.0',
Language = '7',
CaptchaChallenge = '',
CaptchaResponse = '',
Persist = 'true',
Email = email,
Password = password,
})
local res, code = https.request(url, data)
if res then
resp = json.pdecode(res)
if type(resp) ~= 'table' then
return nil, 'failed to decode reply data'
elseif resp.ErrorId ~= json.null then
return nil, 'cloud login failed'
else
return resp.LoginData.ContextKey
end
else
return nil, 'login request failed'
end
end
function getdevices(key)
local url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices'
local tbl = {}
local res, code = https.request({
url = url,
headers = {
['X-MitsContextKey'] = key
},
sink = ltn12.sink.table(tbl),
})
if res then
local data = table.concat(tbl)
return json.pdecode(data)
else
return nil, 'get devices request failed'
end
end
key, err = login(email, password)
if key then
items, err = getdevices(key)
if items then
for _, item in ipairs(items) do
local devs = item.Structure.Devices
for _, dev in ipairs(devs) do
log(dev)
end
end
else
log(err)
end
else
log(err)
end