diff --git a/package/gluon-hoodselector/luasrc/usr/lib/lua/hoodselector/util.lua b/package/gluon-hoodselector/luasrc/usr/lib/lua/hoodselector/util.lua index f1351ee3..05e8cb8c 100644 --- a/package/gluon-hoodselector/luasrc/usr/lib/lua/hoodselector/util.lua +++ b/package/gluon-hoodselector/luasrc/usr/lib/lua/hoodselector/util.lua @@ -3,18 +3,9 @@ local util = require 'gluon.util' local json = require 'jsonc' local uci = require('simple-uci').cursor() local site = require 'gluon.site' -local vpn_util = require('gluon.mesh-vpn') local logger = require('posix.syslog') local M = {} -function M.split(s, delimiter) - local result = {} - for match in (s..delimiter):gmatch("(.-)"..delimiter) do - table.insert(result, match) - end - return result -end - function M.log(msg) io.stdout:write(msg..'\n') logger.openlog(msg, logger.LOG_PID) @@ -31,31 +22,23 @@ function M.get_domains() return list end --- Return the default hood in the hood list. +-- Return the default domain from the domaon list. -- This method can return the following data: --- * default hood --- * nil if no default hood has been defined -function M.get_default_hood(jhood) - for _, h in pairs(jhood) do - if h.domain_code == site.default_domain() then - return h +-- * default domain +-- * nil if no default domain has been defined +function M.get_default_domain(jdomains) + for _, domain in pairs(jdomains) do + if domain.domain_code == site.default_domain() then + return domain end end return nil end --- Returns true if the node has an established running VPN connection. -function M.direct_vpn() - for outgoing_if in io.popen(string.format("batctl o"), 'r'):lines() do - -- escape special chars "[]-" - if outgoing_if:match(string.gsub("%[ " .. vpn_util.get_mesh_vpn_interface() .. "%]","%-", "%%-")) then - return true - end - end - return false -end - --- Get Geoposition. Return nil for no position +-- Get Geoposition. +-- This method can return the following data: +-- * table {lat, lon} +-- * nil for no position function M.get_geolocation() return { lat = tonumber(uci:get('gluon-node-info', uci:get_first('gluon-node-info', 'location'), 'latitude')), @@ -115,21 +98,21 @@ function M.point_in_polygon(poly, point) return t end --- Return hood from the hood file based on geo position or nil if no real hood could be determined --- First check if an area has > 2 points and is hence a polygon. Else assume it is a rectangular --- box defined by two points (south-west and north-east) -function M.get_hood_by_geo(jhood,geo) - for _, hood in pairs(jhood) do - if hood.domain_code ~= site.default_domain() then - for _, area in pairs(hood.domain.hoodselector.shapes) do +-- Return domain from the domain list based on geo position or nil if no geo base domain could be +-- determined. First check if an area has > 2 points and is hence a polygon. Else assume it is a +-- rectangular box defined by two points (south-west and north-east) +function M.get_domain_by_geo(jdomains,geo) + for _, domain in pairs(jdomains) do + if domain.domain_code ~= site.default_domain() then + for _, area in pairs(domain.domain.hoodselector.shapes) do if #area > 2 then if (M.point_in_polygon(area,geo) == 1) then - return hood + return domain end else if ( geo.lat >= area[1].lat and geo.lat < area[2].lat and geo.lon >= area[1].lon and geo.lon < area[2].lon ) then - return hood + return domain end end end @@ -138,12 +121,12 @@ function M.get_hood_by_geo(jhood,geo) return nil end -function M.set_hoodconfig(geo_hood) - if uci:get('gluon', 'core', 'domain') ~= geo_hood.domain_code then - uci:set('gluon', 'core', 'domain', geo_hood.domain_code) +function M.set_domain_config(domain) + if uci:get('gluon', 'core', 'domain') ~= domain.domain_code then + uci:set('gluon', 'core', 'domain', domain.domain_code) uci:commit('gluon') os.execute('gluon-reconfigure') - M.log('Set domain "'..geo_hood.domain.domain_names[geo_hood.domain_code]..'"') + M.log('Set domain "'..domain.domain.domain_names[domain.domain_code]..'"') return true end return false diff --git a/package/gluon-hoodselector/luasrc/usr/sbin/hoodselector b/package/gluon-hoodselector/luasrc/usr/sbin/hoodselector index a02406f4..b6562473 100755 --- a/package/gluon-hoodselector/luasrc/usr/sbin/hoodselector +++ b/package/gluon-hoodselector/luasrc/usr/sbin/hoodselector @@ -29,38 +29,29 @@ if not ok then os.exit(1) end --- VPN MODE --- If we have a VPN connection we will try to get the node's location and --- select the hood coresponding to this location. --- If no hood for the location has been defined, we will select --- the default hood. --- If we can't determine the node's location, we will continue to the next mode. -if hoodutil.direct_vpn() then - io.stdout:write('VPN connection found.\n') - local geo = hoodutil.get_geolocation() - if geo.lat ~= nil and geo.lon ~= nil then - io.stdout:write('Position found.\n') - local jhood = hoodutil.get_domains() - local geo_hood = hoodutil.get_hood_by_geo(jhood, geo) - if geo_hood ~= nil then - if hoodutil.set_hoodconfig(geo_hood) then - hoodutil.restart_services() - hoodutil.log('Domain set by VPN mode.\n') - end - return - end - io.stdout:write('No domain has been defined for the current position.\n') - if hoodutil.set_hoodconfig(hoodutil.get_default_hood(jhood)) then +-- geolocation mode +-- If we have a location we will try to select the domain coresponding to this location. +-- If no domain for the location has been defined or if we can't determine the node's location, +-- we will select the default domain as last fallback instanc. +local geo = hoodutil.get_geolocation() +if geo.lat ~= nil and geo.lon ~= nil then + io.stdout:write('Position found. Enter "geolocation mode" ...\n') + local jdomains = hoodutil.get_domains() + local geo_base_domain = hoodutil.get_domain_by_geo(jdomains, geo) + if geo_base_domain ~= nil then + if hoodutil.set_domain_config(geo_base_domain) then hoodutil.restart_services() - hoodutil.log('Domain set by VPN mode.\n') + hoodutil.log('Domain set by geolocation mode.\n') end return - else - -- The hoodselector should continue with the next states because there can be other - -- VPN nodes in the local mesh network which provide a position and therefore - -- have set a geo base hood. - io.stdout:write('No position found\n') end + io.stdout:write('No domain has been defined for the current position. Continure with default domain mode\n') else - io.stdout:write('No VPN connection found\n') + io.stdout:write('No position found. Continure with default domain mode\n') +end + +-- default domain mode +if hoodutil.set_domain_config(hoodutil.get_default_domain(hoodutil.get_domains())) then + hoodutil.restart_services() + hoodutil.log('Set default domain.\n') end