gluon-geolocator: set uci getbool fix scan for surrounded wifis

This commit is contained in:
Jan-Tarek Butt 2017-08-18 15:44:59 +02:00
parent 6c806d77dc
commit eb6f071730

View File

@ -2,12 +2,12 @@
local uci = require('simple-uci').cursor() local uci = require('simple-uci').cursor()
local json = require ("luci.jsonc") local json = require ("luci.jsonc")
local iw = luci.sys.wifi.getiwinfo(luci.http.formvalue("device")) require("iwinfo")
local LOC="gluon-node-info" local LOC="gluon-node-info"
local GLC="geolocator" local GLC="geolocator"
if not uci:get(GLC, "stettings", "auto_location") then if not uci:get_bool(GLC, "settings", "auto_location") then
os.exit(0) os.exit(0)
end end
@ -46,32 +46,52 @@ local function table_contains(tbl, prefix)
return false return false
end end
local function getWifiifnames()
local ifnames = {}
uci:foreach('wireless', 'wifi-iface',
function(s)
local ifname = uci:get('wireless', s['.name'], 'ifname')
if (ifname ~= nil) then
table.insert(ifnames, ifname)
end
end
)
return ifnames
end
-- Get position -- Get position
local function Get_geolocation_info() local function Get_geolocation_info()
-- Get list of BSSID there should ignored -- Get list of BSSID there should ignored
local blacklist_bssid = { } local blacklist_bssid = { }
for _, bl_bssid in ipairs(uci:get(GLC, "stettings", "blacklist")) do for _, bl_bssid in ipairs(uci:get(GLC, "settings", "blacklist")) do
table.insert(blacklist_bssid,string.upper(bl_bssid)) table.insert(blacklist_bssid,string.upper(bl_bssid))
end end
-- Get list of BSSID without blacklisted and redundancy entrys.
local scaned_bssid = "" local scaned_bssid = ""
local uniq = { } local uniq = { }
for _, net in ipairs(iw.scanlist or { }) do for _, ifname in ipairs(getWifiifnames()) do
if not uniq[net.bssid] and not table_contains(blacklist_bssid, net.bssid) then local api = iwinfo.type(ifname)
scaned_bssid = scaned_bssid .. "," .. net.bssid if api then
uniq[net.bssid] = true local iw = iwinfo[api]
-- Get list of BSSID without blacklisted and redundancy entrys.
-- TODO: use scanlist dump insead of wasting airtime
for _, net in ipairs(iw.scanlist(ifname) or { }) do
if not uniq[net.bssid] and not table_contains(blacklist_bssid, net.bssid) then
scaned_bssid = scaned_bssid .. "," .. net.bssid
uniq[net.bssid] = true
end
end
if #scaned_bssid < 12 then
io.stdout:write("No surrounded BSSIDs found.\n")
return { }
end
scaned_bssid = scaned_bssid:gsub("^,(.-),*", "%1")
end end
end end
if #scaned_bssid < 12 then
io.stdout:write("No surrounded BSSIDs found.\n")
return { }
end
scaned_bssid = scaned_bssid:gsub("^,(.-),*", "%1")
-- Request position -- Request position
local req = io.popen("curl --connect-timeout 15 -s http://openwifi.su/api/v1/bssids/" .. scaned_bssid) local req = io.popen("wget -T 15 -q -O - http://openwifi.su/api/v1/bssids/" .. scaned_bssid)
if not req then if not req then
io.stdout:write("connection failed.\n") io.stdout:write("connection failed.\n")
return { } return { }
@ -79,7 +99,7 @@ local function Get_geolocation_info()
local jreq, _, err = json.parse(req:read("*a"), 1, nil) local jreq, _, err = json.parse(req:read("*a"), 1, nil)
req:close() req:close()
if err or jreq.lon == nil or jreq.lat == nil then if err or jreq == nil or jreq.lon == nil or jreq.lat == nil then
io.stdout:write("openwifi.su doesn't gif a location.\n") io.stdout:write("openwifi.su doesn't gif a location.\n")
return { } return { }
end end
@ -89,7 +109,7 @@ end -- end Get_geolocation_info()
-- Check if interval over or not exist -- Check if interval over or not exist
if file_exsist(TIME_STAMP) then if file_exsist(TIME_STAMP) then
if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "stettings", "refresh_interval") * 60 then if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "settings", "refresh_interval") * 60 then
exit(0) exit(0)
end end
end end
@ -98,7 +118,7 @@ local pos = Get_geolocation_info()
if not next(pos) then if not next(pos) then
exit(1) exit(1)
end end
if not uci:get(GLC, "stettings", "static_location") then if not uci:get_bool(GLC, "settings", "static_location") then
uci:set(LOC, uci:get_first(LOC, 'location'), 'latitude', pos.lat) uci:set(LOC, uci:get_first(LOC, 'location'), 'latitude', pos.lat)
uci:set(LOC, uci:get_first(LOC, 'location'), 'longitude', pos.lon) uci:set(LOC, uci:get_first(LOC, 'location'), 'longitude', pos.lon)
uci:save(LOC) uci:save(LOC)