Ver código fonte

Add debug for sent events and buffer flush

Luis Rosales 2 meses atrás
pai
commit
4549eb2360
1 arquivos alterados com 14 adições e 6 exclusões
  1. 14 6
      package/src/client2server-unified.lua

+ 14 - 6
package/src/client2server-unified.lua

@@ -506,6 +506,7 @@ local function co_dhcp()
                             event_type = "dhcp_lease_new",
                             payload = { mac = mac, ip = ip, hostname = name }
                         })
+                        if cfg.debug then log("debug", "SENDING: " .. event_type) end
                         ws.send(ev)
                         log_info("DHCP: " .. mac .. " -> " .. ip)
                     end
@@ -520,7 +521,8 @@ local function co_dhcp()
                         event_type = "dhcp_lease_expire",
                         payload = { mac = mac, old_ip = leases[mac].ip }
                     })
-                    ws.send(ev)
+                    if cfg.debug then log("debug", "SENDING: " .. event_type) end
+                        ws.send(ev)
                     log_info("DHCP expire: " .. mac)
                 end
             end
@@ -550,7 +552,8 @@ local function co_wan()
                 event_type = link_now and "wan_link_up" or "wan_link_down",
                 payload = { device = cfg.wan_device }
             })
-            ws.send(ev)
+            if cfg.debug then log("debug", "SENDING: " .. event_type) end
+                        ws.send(ev)
             log_info("WAN link: " .. (link_now and "up" or "down"))
         end
         
@@ -569,7 +572,8 @@ local function co_wan()
                 event_type = ip_last and "wan_dhcp_changed" or "wan_dhcp_new",
                 payload = { old_ip = ip_last, new_ip = ip_now }
             })
-            ws.send(ev)
+            if cfg.debug then log("debug", "SENDING: " .. event_type) end
+                        ws.send(ev)
             log_info("WAN IP: " .. (ip_last or "none") .. " -> " .. ip_now)
             ip_last = ip_now
         end
@@ -641,6 +645,7 @@ local function co_connect()
                 ws.connected = true
                 retry_delay = 30  -- Reset on success
                 log_info("Connected!")
+                if cfg.debug then log("debug", "FLUSH buffer") end
                 buffer.flush(function(d) return ws.send(d) end)
             else
                 log_err("Connection failed, retry in " .. retry_delay .. "s")
@@ -649,7 +654,8 @@ local function co_connect()
             end
         else
             -- Even when connected, periodically flush to clear buffer buildup
-            buffer.flush(function(d) return ws.send(d) end)
+            if cfg.debug then log("debug", "FLUSH buffer") end
+                buffer.flush(function(d) return ws.send(d) end)
             coroutine.yield(30)
         end
     end
@@ -679,7 +685,8 @@ local function scheduler()
         
         -- Flush buffer when connected
         if ws.connected then
-            buffer.flush(function(d) return ws.send(d) end)
+            if cfg.debug then log("debug", "FLUSH buffer") end
+                buffer.flush(function(d) return ws.send(d) end)
         end
         
         os.execute("sleep 1")
@@ -707,7 +714,8 @@ local function main()
     if sock then
         ws.connected = true
         log_info("Connected!")
-        buffer.flush(function(d) return ws.send(d) end)
+        if cfg.debug then log("debug", "FLUSH buffer") end
+                buffer.flush(function(d) return ws.send(d) end)
     else
         log_err("Connection failed")
     end