gluon-geolocator: geolocator replace spaces with taps
Signed-off-by: Jan-Tarek Butt <tarek@ring0.de>
This commit is contained in:
parent
20661a2356
commit
791dfe0a2f
@ -11,7 +11,7 @@ local GLC="geolocator"
|
|||||||
local TIME_STAMP="/tmp/geolocator_timestamp"
|
local TIME_STAMP="/tmp/geolocator_timestamp"
|
||||||
|
|
||||||
if not uci:get_bool(GLC, "settings", "auto_location") then
|
if not uci:get_bool(GLC, "settings", "auto_location") then
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- PID file to ensure the geolocator isn't running parallel
|
-- PID file to ensure the geolocator isn't running parallel
|
||||||
@ -19,98 +19,98 @@ local lockfile = '/var/lock/geolocator.lock'
|
|||||||
local lockfd = nixio.open(lockfile, 'w', 'rw-------')
|
local lockfd = nixio.open(lockfile, 'w', 'rw-------')
|
||||||
|
|
||||||
if not lockfd:lock('tlock') then
|
if not lockfd:lock('tlock') then
|
||||||
io.stderr:write(string.format(
|
io.stderr:write(string.format(
|
||||||
"Unable to lock file %s. Make sure there is no other instance of the geolocator running.\n",
|
"Unable to lock file %s. Make sure there is no other instance of the geolocator running.\n",
|
||||||
lockfile
|
lockfile
|
||||||
))
|
))
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Iterates over all active WLAN interfaces
|
-- Iterates over all active WLAN interfaces
|
||||||
-- Returning true from the callback function will skip all remaining
|
-- Returning true from the callback function will skip all remaining
|
||||||
-- interfaces of the same radio
|
-- interfaces of the same radio
|
||||||
local function foreach_radio(f)
|
local function foreach_radio(f)
|
||||||
local uconn = assert(ubus.connect(), 'failed to connect to ubus')
|
local uconn = assert(ubus.connect(), 'failed to connect to ubus')
|
||||||
local status = uconn:call('network.wireless', 'status', {})
|
local status = uconn:call('network.wireless', 'status', {})
|
||||||
ubus.close(uconn)
|
ubus.close(uconn)
|
||||||
|
|
||||||
for _, radio in pairs(status) do
|
for _, radio in pairs(status) do
|
||||||
for _, iface in ipairs(radio.interfaces) do
|
for _, iface in ipairs(radio.interfaces) do
|
||||||
if f(iface.ifname) then
|
if f(iface.ifname) then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function receive_json(request)
|
local function receive_json(request)
|
||||||
local f = assert(io.popen(string.format("exec wget -T 15 -q -O- '%s'", request)), 'failed to run wget')
|
local f = assert(io.popen(string.format("exec wget -T 15 -q -O- '%s'", request)), 'failed to run wget')
|
||||||
local data = f:read('*a')
|
local data = f:read('*a')
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
return json.parse(data)
|
return json.parse(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get position
|
-- Get position
|
||||||
local function locate()
|
local function locate()
|
||||||
local done_bssids = {}
|
local done_bssids = {}
|
||||||
local found_bssids = {}
|
local found_bssids = {}
|
||||||
foreach_radio(function(ifname)
|
foreach_radio(function(ifname)
|
||||||
local iw = iwinfo[iwinfo.type(ifname)]
|
local iw = iwinfo[iwinfo.type(ifname)]
|
||||||
if not iw then
|
if not iw then
|
||||||
-- Skip other ifaces of this radio, as they
|
-- Skip other ifaces of this radio, as they
|
||||||
-- will have the same type
|
-- will have the same type
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local scanlist = iw.scanlist(ifname)
|
local scanlist = iw.scanlist(ifname)
|
||||||
if not scanlist then
|
if not scanlist then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, entry in ipairs(scanlist) do
|
for _, entry in ipairs(scanlist) do
|
||||||
if entry.mode:match("Master") then
|
if entry.mode:match("Master") then
|
||||||
local bssid = string.upper(entry.bssid:gsub(":", ""))
|
local bssid = string.upper(entry.bssid:gsub(":", ""))
|
||||||
if not done_bssids[bssid] then
|
if not done_bssids[bssid] then
|
||||||
table.insert(found_bssids, bssid)
|
table.insert(found_bssids, bssid)
|
||||||
done_bssids[bssid] = true
|
done_bssids[bssid] = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end)
|
end)
|
||||||
|
|
||||||
assert(#found_bssids >= 12, 'insufficient BSSIDs found')
|
assert(#found_bssids >= 12, 'insufficient BSSIDs found')
|
||||||
|
|
||||||
local data = receive_json('http://openwifi.su/api/v1/bssids/' .. table.concat(found_bssids, ','))
|
local data = receive_json('http://openwifi.su/api/v1/bssids/' .. table.concat(found_bssids, ','))
|
||||||
assert(type(data) == 'table' and data.lon and data.lat, 'location not available')
|
assert(type(data) == 'table' and data.lon and data.lat, 'location not available')
|
||||||
|
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if interval over or not exist
|
-- Check if interval over or not exist
|
||||||
if io.open(TIME_STAMP) ~= nil then
|
if io.open(TIME_STAMP) ~= nil then
|
||||||
if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "settings", "refresh_interval") * 60 then
|
if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "settings", "refresh_interval") * 60 then
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local pos = locate()
|
local pos = locate()
|
||||||
if not next(pos) then
|
if not next(pos) then
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
end
|
end
|
||||||
if not uci:get_bool(GLC, "settings", "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)
|
||||||
uci:commit(LOC)
|
uci:commit(LOC)
|
||||||
end
|
end
|
||||||
local timestap = io.open(TIME_STAMP, "w")
|
local timestap = io.open(TIME_STAMP, "w")
|
||||||
if timestap ~= nil then
|
if timestap ~= nil then
|
||||||
timestap:write(os.time())
|
timestap:write(os.time())
|
||||||
timestap:close()
|
timestap:close()
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
end
|
end
|
||||||
io.stdout:write("Can`t create file on " .. TIME_STAMP .. "\n")
|
io.stdout:write("Can`t create file on " .. TIME_STAMP .. "\n")
|
||||||
os.exit(1)
|
os.exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user