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 json = require ("luci.jsonc")
local iw = luci.sys.wifi.getiwinfo(luci.http.formvalue("device"))
require("iwinfo")
local LOC="gluon-node-info"
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)
end
@ -46,32 +46,52 @@ local function table_contains(tbl, prefix)
return false
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
local function Get_geolocation_info()
-- Get list of BSSID there should ignored
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))
end
-- Get list of BSSID without blacklisted and redundancy entrys.
local scaned_bssid = ""
local uniq = { }
for _, net in ipairs(iw.scanlist 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
for _, ifname in ipairs(getWifiifnames()) do
local api = iwinfo.type(ifname)
if api then
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
if #scaned_bssid < 12 then
io.stdout:write("No surrounded BSSIDs found.\n")
return { }
end
scaned_bssid = scaned_bssid:gsub("^,(.-),*", "%1")
-- 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
io.stdout:write("connection failed.\n")
return { }
@ -79,7 +99,7 @@ local function Get_geolocation_info()
local jreq, _, err = json.parse(req:read("*a"), 1, nil)
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")
return { }
end
@ -89,7 +109,7 @@ end -- end Get_geolocation_info()
-- Check if interval over or not exist
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)
end
end
@ -98,7 +118,7 @@ local pos = Get_geolocation_info()
if not next(pos) then
exit(1)
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'), 'longitude', pos.lon)
uci:save(LOC)