|
@@ -577,57 +577,77 @@ local function main()
|
|
|
end)
|
|
end)
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
- -- Start monitors first, then run the event loop
|
|
|
|
|
- start_monitors()
|
|
|
|
|
-
|
|
|
|
|
- -- luv.run() - start the event loop (no arguments)
|
|
|
|
|
- local ok, err = pcall(function() luv.run() end)
|
|
|
|
|
- if not ok then
|
|
|
|
|
- log("err", "luv.run error: " .. tostring(err))
|
|
|
|
|
- end
|
|
|
|
|
-
|
|
|
|
|
- -- WebSocket connection and keepalive
|
|
|
|
|
- while true do
|
|
|
|
|
- log("info", "Connecting to server...")
|
|
|
|
|
-
|
|
|
|
|
- if ws_client then
|
|
|
|
|
- sock = ws.connect(cfg.url)
|
|
|
|
|
- end
|
|
|
|
|
-
|
|
|
|
|
- if sock then
|
|
|
|
|
- log("info", "Connected!")
|
|
|
|
|
- retries = 0
|
|
|
|
|
-
|
|
|
|
|
- -- Flush buffer
|
|
|
|
|
- buffer.flush(function(data)
|
|
|
|
|
- return ws.send(sock, data)
|
|
|
|
|
- end)
|
|
|
|
|
-
|
|
|
|
|
- -- Keep alive loop
|
|
|
|
|
- local loop_count = 0
|
|
|
|
|
- while ws.connected(sock) and loop_count < (cfg.ping_interval / 5) do
|
|
|
|
|
- luv.sleep(5000)
|
|
|
|
|
- loop_count = loop_count + 1
|
|
|
|
|
-
|
|
|
|
|
- -- Periodic flush
|
|
|
|
|
|
|
+ -- Start monitors first, then run the event loop with WebSocket
|
|
|
|
|
+ local function run_event_loop()
|
|
|
|
|
+ -- Register all monitors
|
|
|
|
|
+ monitor_wifi_events()
|
|
|
|
|
+ monitor_dhcp_leases()
|
|
|
|
|
+ monitor_wan()
|
|
|
|
|
+ poll_network_status()
|
|
|
|
|
+
|
|
|
|
|
+ -- Keep the event loop running with idle
|
|
|
|
|
+ local idle = luv.new_idle()
|
|
|
|
|
+ luv.idle_start(idle, function()
|
|
|
|
|
+ -- Idle work - check WebSocket periodically
|
|
|
|
|
+ end)
|
|
|
|
|
+
|
|
|
|
|
+ -- WebSocket connection - try to connect and stay connected
|
|
|
|
|
+ local function ws_loop()
|
|
|
|
|
+ log("info", "Connecting to server...")
|
|
|
|
|
+
|
|
|
|
|
+ if ws_client then
|
|
|
|
|
+ sock = ws.connect(cfg.url)
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ if sock then
|
|
|
|
|
+ log("info", "Connected!")
|
|
|
|
|
+ retries = 0
|
|
|
|
|
+
|
|
|
|
|
+ -- Flush buffer
|
|
|
buffer.flush(function(data)
|
|
buffer.flush(function(data)
|
|
|
return ws.send(sock, data)
|
|
return ws.send(sock, data)
|
|
|
end)
|
|
end)
|
|
|
|
|
+
|
|
|
|
|
+ -- Stay connected - reconnect timer will handle reconnection
|
|
|
|
|
+ local ws_timer = luv.new_timer()
|
|
|
|
|
+ luv.timer_start(ws_timer, 5000, 5000, function()
|
|
|
|
|
+ if ws.connected(sock) then
|
|
|
|
|
+ -- Periodic flush
|
|
|
|
|
+ buffer.flush(function(data)
|
|
|
|
|
+ return ws.send(sock, data)
|
|
|
|
|
+ end)
|
|
|
|
|
+ else
|
|
|
|
|
+ -- Disconnected, reconnect
|
|
|
|
|
+ ws.close(sock)
|
|
|
|
|
+ sock = nil
|
|
|
|
|
+ buffer.save()
|
|
|
|
|
+ ws_loop() -- Reconnect
|
|
|
|
|
+ end
|
|
|
|
|
+ end)
|
|
|
|
|
+ else
|
|
|
|
|
+ log("err", "Connection failed")
|
|
|
|
|
+ retries = retries + 1
|
|
|
|
|
+
|
|
|
|
|
+ -- Retry after delay
|
|
|
|
|
+ local retry_timer = luv.new_timer()
|
|
|
|
|
+ luv.timer_start(retry_timer, cfg.reconnect_delay * 1000, 0, function()
|
|
|
|
|
+ ws_loop()
|
|
|
|
|
+ end)
|
|
|
end
|
|
end
|
|
|
- else
|
|
|
|
|
- log("err", "Connection failed")
|
|
|
|
|
- retries = retries + 1
|
|
|
|
|
end
|
|
end
|
|
|
|
|
+
|
|
|
|
|
+ -- Start WebSocket loop
|
|
|
|
|
+ ws_loop()
|
|
|
|
|
+ end
|
|
|
|
|
|
|
|
- -- Cleanup and reconnect
|
|
|
|
|
- ws.close(sock)
|
|
|
|
|
- sock = nil
|
|
|
|
|
-
|
|
|
|
|
- -- Save buffer on disconnect
|
|
|
|
|
- buffer.save()
|
|
|
|
|
-
|
|
|
|
|
- -- Delay before reconnect
|
|
|
|
|
- luv.sleep(cfg.reconnect_delay * 1000)
|
|
|
|
|
|
|
+ -- Run the event loop
|
|
|
|
|
+ local ok, err = pcall(function() luv.run(run_event_loop) end)
|
|
|
|
|
+ if not ok then
|
|
|
|
|
+ log("err", "luv.run error: " .. tostring(err))
|
|
|
|
|
+ -- Fallback: sequential mode
|
|
|
|
|
+ while true do
|
|
|
|
|
+ os.execute("sleep 60")
|
|
|
|
|
+ end
|
|
|
end
|
|
end
|
|
|
else
|
|
else
|
|
|
-- Fallback: sequential mode
|
|
-- Fallback: sequential mode
|