54 lines
1.4 KiB
Lua
Executable File
54 lines
1.4 KiB
Lua
Executable File
#!/usr/bin/lua
|
|
|
|
local function read_scan()
|
|
local h = io.popen("ubus call iwinfo scan '{\"device\":\"radio0\"}' 2>/dev/null")
|
|
local raw = h:read("*a")
|
|
h:close()
|
|
return raw
|
|
end
|
|
|
|
local function jsondec(s)
|
|
local json = require("luci.jsonc")
|
|
return json.parse(s)
|
|
end
|
|
|
|
local function jsonenc(t)
|
|
local json = require("luci.jsonc")
|
|
return json.stringify(t)
|
|
end
|
|
|
|
io.write("Content-Type: application/json\r\n\r\n")
|
|
|
|
local raw = read_scan()
|
|
local data = raw and jsondec(raw)
|
|
|
|
if not data or not data.results then
|
|
io.write(jsonenc({ ok = false, error = "scan failed or no results" }))
|
|
os.exit(0)
|
|
end
|
|
|
|
local best = {}
|
|
for _, n in ipairs(data.results) do
|
|
local ssid = n.ssid
|
|
if ssid and ssid ~= "" then
|
|
local prev = best[ssid]
|
|
if not prev or (n.signal or -999) > prev.signal then
|
|
best[ssid] = {
|
|
ssid = ssid,
|
|
signal = n.signal or -999,
|
|
quality = n.quality or 0,
|
|
quality_max = n.quality_max or 70,
|
|
band = n.band,
|
|
channel = n.channel,
|
|
secured = n.encryption and n.encryption.enabled or false,
|
|
}
|
|
end
|
|
end
|
|
end
|
|
|
|
local list = {}
|
|
for _, v in pairs(best) do list[#list+1] = v end
|
|
table.sort(list, function(a, b) return a.signal > b.signal end)
|
|
|
|
io.write(jsonenc({ ok = true, networks = list }))
|