livehouse-automation / unifi-sensor

Vera Plugin for Presence Detection using a Unifi Controller
GNU General Public License v3.0
1 stars 1 forks source link

Update from .sh to Lua ? #1

Open nodecentral opened 3 years ago

nodecentral commented 3 years ago

Hi

Hope all is well, while I was a big user of your plug-in in the past, I moved to use my own UniFi controller lua code, which I run ad-hoc to retrieve the json file etc.

I hope you don’t mind the question, but I was just wondering if it would be better if this plug-in utilised Lua to do the requests, rather than a .sh file ?

Also have you looked at anymore of the UniFi API to add more functionality ?

nodecentral commented 3 years ago

If it helps, (as your read-me asked for a Lua version) here’s what I use..

-- http://www.domoticz.com/forum/viewtopic.php?t=18508

function UnifiController()
    unifi_username = "vera" 
    unifi_password = "vera" 
    unifi_controller_ip = "192.168.102.207" 
    unifi_controller_port = "8443"  
    cookie = "/tmp/unifi_cookie_lua" -- Temp cookie file
    tempfilename = "/tmp/UnifiController.tmp" -- Temp JSON output file

    local deviceb = 'api/s/default/stat/device-basic'
    local device = 'api/s/default/stat/device'
    local health = 'api/s/default/stat/health'
    local stat = 'api/s/default/stat/sta'
    local user = 'api/s/default/stat/user'
    local mac = 'api/s/default/stat/device/74:bc:b9:d7:a3:75'
    local site = 'api/s/default/stat/report/daily'
    local event = 'api/s/default/stat/event'
    local netconf = 'api/s/default/list/networkconf'
    local wlanconf = 'api/s/default/list/wlanconf'
    local site = 'api/s/default/stat/site' -- not working
    local status = 'status'
    local alarm = 'api/s/default/list/alarm'
    local client = 'api/s/default/list/client' -- not working

    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 .. '/' ..stat
        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

    local line
    local file = io.open(tempfilename, "r")
        if file then
            line = file:read("*a")
            file:close()
        end
    return line
end

print(UnifiController())