Bladeren bron

ffho-site-generate: improve toolbox
update ffho-site-auto-select

Karsten Böddeker 8 jaren geleden
bovenliggende
commit
9b6a8ba46c

+ 5 - 15
ffho/ffho-site-auto-select/files/lib/gluon/upgrade/003-site-auto-select

@@ -5,7 +5,7 @@ local json = require 'luci.json'
 local tools = require 'gluon.site_generate'
 local shape = require 'gluon.pointwithinshape'
 
-local geo_default_site = "ffho_error"
+local geo_default_site = "ffho_yho"
 
 function get_config(file)
   local f = io.open(file)
@@ -17,18 +17,8 @@ function get_config(file)
   return nil
 end
 
-function is_site_valid(site_code)
-  local sites = get_config('/lib/gluon/site-select/sites.json')
-  for _, site in pairs(sites) do
-    if site.site_code == site_code then
-      return true
-    end
-  end
-  return false
-end
-
 function set_currentsite(site_code)
-  if site_code and is_site_valid(site_code) then
+  if site_code and tools.validate_site(site_code) then
     uci:set('currentsite', 'current', 'name', site_code)
     uci:save('currentsite')
     uci:commit('currentsite')
@@ -62,9 +52,9 @@ function get_site_by_geo(latitude, longitude)
 end
 
 local currentsite = uci:get('currentsite', 'current', 'name')
-local configured = is_site_valid(currentsite)
+local configured = tools.validate_site(currentsite)
 
-if configured == false then
+if not configured then
   local latitude = uci:get_first('gluon-node-info', 'location', 'latitude')
   local longitude = uci:get_first('gluon-node-info', 'location', 'longitude')
   if latitude and longitude then
@@ -73,7 +63,7 @@ if configured == false then
   end
 end
 
-if configured == false then
+if not configured then
   local minute = math.random(0, 59)
   local f = io.open('/usr/lib/micron.d/ffho-site-auto-select', 'w')
   f:write(string.format('%i * * * * /usr/sbin/ffho-site-auto-select\n', minute))

+ 11 - 6
ffho/ffho-site-generate/files/usr/lib/lua/gluon/site_generate.lua

@@ -17,14 +17,19 @@ function get_config(file)
   return nil
 end
 
-function validate_site(site_code)
+function get_list()
+  local list = {}
   local sites = get_config(sites_json)
-  for _, site in pairs(sites) do
-    if site.site_code == site_code then
-      return true
-    end
+  for index, site in pairs(sites) do
+    list[site.site_code]=index
   end
-  return false
+  return list
+end
+
+local site_list=get_list()
+
+function validate_site(site_code)
+  return site_list[site_code]
 end
 
 function force_site_code(site_code)