lain icon indicating copy to clipboard operation
lain copied to clipboard

[Net widget] devices not passed to net_now

Open zvolin opened this issue 6 years ago • 3 comments

Hi, I was trying to hack net widget a bit, to refetch network ssid every time new connection is acquired and I've noticed that net_now variable doesn't contain devices array. I've added simple logging to check what's going on, but I couldn't find why does it happen. I've added some simple logging which shows that indeed net_now is not updated in the end. Here is a diff of my changes:

diff --git a/util/separators.lua b/util/separators.lua
index 465132d..ccf85fd 100644
--- a/util/separators.lua
+++ b/util/separators.lua
@@ -11,7 +11,7 @@ local gears = require("gears")
 
 -- Lain Cairo separators util submodule
 -- lain.util.separators
-local separators = { height = 0, width = 9 }
+local separators = { height = 0, width = 15 }
 
 -- [[ Arrow
 
diff --git a/widget/net.lua b/widget/net.lua
index 805b577..720992a 100644
--- a/widget/net.lua
+++ b/widget/net.lua
@@ -37,7 +37,25 @@ local function factory(args)
 
     if #net.iface == 0 then net.get_device() end
 
+    file = io.open('lain.log', 'a')
+    io.output(file)
+    io.write('Initializing lain.widget.net:\n'..
+            '\ttimeout: '..timeout..'\n'..
+            '\tunits: '..units..'\n'..
+            '\tnotify: '..notify..'\n'..
+            '\twifi_state: '..wifi_state..'\n'..
+            '\teth_state: '..eth_state..'\n'..
+            '\tscreen: '..screen..'\n'..
+            '\tiface: { ')
+    for i, s in ipairs(net.iface) do
+        io.write(i..', '..s..'; ')
+    end
+    io.write('}\n')
+    io.close(file)
+
     function net.update()
+        file = io.open('lain.log', 'a')
+        io.output(file)
         -- These are the totals over all specified interfaces
         net_now = {
             devices  = {},
@@ -46,6 +64,16 @@ local function factory(args)
             received = 0
         }
 
+        function net_now:dump()
+            ret = 'Net_now:\n\tdevices: '
+            for n, dev in ipairs(self.devices) do
+                ret = ret..'{'..self.n..', '..self.dev..'}, '
+            end
+            ret = ret..'\n\tsent: '..self.sent
+            ret = ret..'\n\treceived: '..self.sent..'\n'
+            return ret
+        end
+
         for _, dev in ipairs(net.iface) do
             local dev_now    = {}
             local dev_before = net.devices[dev] or { last_t = 0, last_r = 0 }
@@ -66,14 +94,27 @@ local function factory(args)
 
             dev_now.last_t   = now_t
             dev_now.last_r   = now_r
+            io.write('Update:\n'..
+                    '\tdev: '..dev..'\n'..
+                    '\tdev_now.carrier: '..dev_now.carrier..'\n'..
+                    '\tdev_now.state: '..dev_now.state..'\n'..
+                    '\tdev_now.sent: '..dev_now.sent..'\n'..
+                    '\tdev_now.received: '..dev_now.received..'\n'..
+                    '\tdev_now.last_t: '..dev_now.last_t..'\n'..
+                    '\tdev_now.last_r: '..dev_now.last_r..'\n')
 
             if wifi_state == "on" and helpers.first_line(string.format("/sys/class/net/%s/uevent", dev)) == "DEVTYPE=wlan" and string.match(dev_now.carrier, "1") then
                 dev_now.wifi   = true
                 dev_now.signal = tonumber(string.match(helpers.lines_from("/proc/net/wireless")[3], "(%-%d+%.)")) or nil
+                io.write(
+                    '\tdev_now.wifi: '..tostring(dev_now.wifi)..'\n'..
+                    '\tdev_now.signal: '..dev_now.signal..'\n')
             end
 
             if eth_state == "on" and helpers.first_line(string.format("/sys/class/net/%s/uevent", dev)) ~= "DEVTYPE=wlan" and string.match(dev_now.carrier, "1") then
                 dev_now.ethernet = true
+                io.write(
+                    '\tdev_now.ethernet: '..dev_now.ethernet..'\n')
             end
 
             net.devices[dev] = dev_now
@@ -102,6 +143,8 @@ local function factory(args)
         net_now.received = string.format("%.1f", net_now.received)
 
         widget = net.widget
+        io.write(net_now:dump())
+        io.close(file)
         settings()
     end

And here is a sample of output:

Initializing lain.widget.net:
	timeout: 20
	units: 1024
	notify: on
	wifi_state: on
	eth_state: off
	screen: 1
	iface: { 1, wlan0; }
Update:
	dev: wlan0
	dev_now.carrier: 1
	dev_now.state: up
	dev_now.sent: 578.2
	dev_now.received: 799.8
	dev_now.last_t: 11842390
	dev_now.last_r: 16379913
	dev_now.wifi: true
	dev_now.signal: -54.0
Net_now:
	devices: 
	sent: 578.2
	received: 578.2
Update:
	dev: wlan0
	dev_now.carrier: 1
	dev_now.state: up
	dev_now.sent: 0.5
	dev_now.received: 0.4
	dev_now.last_t: 11853508
	dev_now.last_r: 16389063
	dev_now.wifi: true
	dev_now.signal: -56.0
Net_now:
	devices: 
	sent: 0.5
	received: 0.5
Update:
	dev: wlan0
	dev_now.carrier: 1
	dev_now.state: up
	dev_now.sent: 1.8
	dev_now.received: 2.9
	dev_now.last_t: 11890441
	dev_now.last_r: 16448641
	dev_now.wifi: true
	dev_now.signal: -54.0
Net_now:
	devices: 
	sent: 1.8
	received: 1.8
❯ awesome -v
awesome v4.3 (Too long)
 • Compiled against Lua 5.3.5 (running with Lua 5.3)
 • D-Bus support: ✔
 • execinfo support: ✔
 • xcb-randr version: 1.6
 • LGI version: 0.9.2
❯ lua -v
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio

zvolin avatar Apr 28 '20 20:04 zvolin

Keep it simple: to get the current network SSID, use a watcher. You can find examples here.

To update the iface table, pull the latest commit and use the function net.get_devices().

The reason why you get an empty devices array in net_now:dump() is that self.devices is always null. It should be just devices, but I am not sure (I prefer to use the dot notation).

You do not need this logging, just check if net_now.devices is correctly populated within the settings() function, that is, in your widget customisation.

lcpz avatar Apr 29 '20 17:04 lcpz

Thanks for the response.

The reason why you get an empty devices array in net_now:dump() is that self.devices is always null.

To be sure, I just pasted function body to the place I called it, replaced all self with net_now, and the output is the same it was. I'm newcommer to lua but according to the resources I've found it should work with : :) Anyway, according to this:

You do not need this logging, just check if net_now.devices is correctly populated within the settings() function, that is, in your widget customisation.

The reason I wrote this logger is because it isn't populated in settings, but I wanted to be sure about it. Thanks for the watcher sollution but I'd like to stay with my own, mostly for educational purposes, and also I want it to be fired only when needed. I tried regularly polling that information, but nmcli sometimes is really slow and it resulted in freezes of whole awesome.

zvolin avatar Apr 29 '20 18:04 zvolin

The reason I wrote this logger is because it isn't populated in settings, but I wanted to be sure about it.

settings() belongs to the widget scope, so it is enough to check just there.

I will try to replicate your issue.

Thanks for the watcher sollution but I'd like to stay with my own, mostly for educational purposes, and also I want it to be fired only when needed. I tried regularly polling that information, but nmcli sometimes is really slow and it resulted in freezes of whole awesome.

I still suggest you to use awful.widget.watch: it is asynchronous and won't freeze Awesome as io.popen does. Also, you could use the less memory demanding iw instead of Network Manager:

$ iw dev wlan0 scan

lcpz avatar Apr 29 '20 20:04 lcpz