Browse Source

Fix function order: move DHCP helpers before monitor functions

Gogs 2 months ago
parent
commit
d799283ad2
1 changed files with 35 additions and 31 deletions
  1. 35 31
      usr/sbin/client2server-luv.lua

+ 35 - 31
usr/sbin/client2server-luv.lua

@@ -342,39 +342,9 @@ local function monitor_wifi_events()
 end
 
 -- ============================================================================
--- DHCP LEASES (file watching with luv)
+-- DHCP LEASES HELPERS (must be before monitor functions)
 -- ============================================================================
 
-local function monitor_dhcp_leases()
-    if not luv then
-        -- Fallback: sequential file polling
-        monitor_dhcp_sequential()
-        return
-    end
-    
-    -- Use timer-based polling instead of fs_event (more compatible)
-    local timer = luv.new_timer()
-    local old_leases = {}
-    
-    luv.timer_start(timer, cfg.dhcp_poll_interval * 1000, cfg.dhcp_poll_interval * 1000, function()
-        -- File changed, process leases
-        process_leases(old_leases, function(mac, ip, hostname, action)
-            local ev = build_event("dhcp_lease", {
-                mac = mac,
-                ip = ip,
-                hostname = hostname,
-                action = action
-            })
-            log("info", "DHCP: " .. action .. " - " .. mac .. " -> " .. ip)
-            buffer.add(ev)
-        end)
-        
-        old_leases = read_leases()
-    end)
-    
-    log("info", "DHCP lease monitor started")
-end
-
 -- Helper: read current leases
 local function read_leases()
     local leases = {}
@@ -410,6 +380,40 @@ local function process_leases(old_leases, callback)
     end
 end
 
+-- ============================================================================
+-- DHCP LEASES MONITOR
+-- ============================================================================
+
+local function monitor_dhcp_leases()
+    if not luv then
+        -- Fallback: sequential file polling
+        monitor_dhcp_sequential()
+        return
+    end
+    
+    -- Use timer-based polling instead of fs_event (more compatible)
+    local timer = luv.new_timer()
+    local old_leases = {}
+    
+    luv.timer_start(timer, cfg.dhcp_poll_interval * 1000, cfg.dhcp_poll_interval * 1000, function()
+        -- File changed, process leases
+        process_leases(old_leases, function(mac, ip, hostname, action)
+            local ev = build_event("dhcp_lease", {
+                mac = mac,
+                ip = ip,
+                hostname = hostname,
+                action = action
+            })
+            log("info", "DHCP: " .. action .. " - " .. mac .. " -> " .. ip)
+            buffer.add(ev)
+        end)
+        
+        old_leases = read_leases()
+    end)
+    
+    log("info", "DHCP lease monitor started")
+end
+
 -- Fallback: sequential DHCP monitoring
 local function monitor_dhcp_sequential()
     local old_leases = {}