Perhaps someone else has some use for it aswell.
The Function:
Code: Select all
function UnifiController(Username)
unifi_username = "admin" -- Username for example admin
unifi_password = "admin123" -- Password for example 1234546
unifi_controller_ip = "192.168.0.2" -- Controller IP for example 192.168.0.2
unifi_controller_port = "8443" -- Controller port, default is 8443
cookie = "/tmp/unifi_cookie_lua" -- Temp cookie file
tempfilename = "/tmp/UnifiController.tmp" -- Temp JSON output file
local f = io.popen("stat -c %Y " .. tempfilename)
local last_modified = f:read()
if (os.difftime (os.time(), last_modified) > 30)
then
-- URL for logging in, query and logging out
url_login = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -H \'Content-Type: application/json\' -X POST -d \'{"password":"' .. unifi_password .. '","username":"' .. unifi_username .. '"}\' https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/login'
url_open = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure -s -o '..tempfilename..' --data "json={}" https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/api/s/default/stat/sta'
url_logout = 'curl --cookie ' .. cookie .. ' --cookie-jar ' .. cookie .. ' --insecure https://' .. unifi_controller_ip .. ':' .. unifi_controller_port .. '/logout'
-- Execute url
-- Execute url
read_login = os.execute(url_login)
read_open = os.execute(url_open)
read_logout = os.execute(url_logout)
end
file = io.open(tempfilename, "r")
while true do
line = file:read("*line")
if not line then break end
if string.find(line, Username)
then
UnifiResult = "Home"
else
UnifiResult = "NotHome"
end
end
file:close()
return UnifiResult
end
Code: Select all
--Example
TestUser = "Domoticz"
print(TestUser .. ": " .. UnifiController(TestUser))