lain/widgets/net.lua

106 lines
3.2 KiB
Lua
Raw Normal View History

2013-09-07 12:06:42 +02:00
--[[
Licensed under GNU General Public License v2
* (c) 2013, Luke Bonham
* (c) 2010-2012, Peter Hofmann
--]]
local helpers = require("lain.helpers")
2013-09-10 23:02:11 +02:00
local notify_fg = require("beautiful").fg_focus
local naughty = require("naughty")
2013-09-07 12:06:42 +02:00
local wibox = require("wibox")
2014-08-08 14:25:21 +02:00
local io = { popen = io.popen }
2013-09-07 12:06:42 +02:00
local tostring = tostring
2013-09-10 23:02:11 +02:00
local string = { format = string.format,
gsub = string.gsub }
2013-09-07 12:06:42 +02:00
local setmetatable = setmetatable
-- Network infos
-- lain.widgets.net
local net = {
2013-09-10 23:02:11 +02:00
last_t = 0,
last_r = 0
2013-09-07 12:06:42 +02:00
}
function net.get_device()
f = io.popen("ip link show | cut -d' ' -f2,9")
2014-08-05 12:59:19 +02:00
ws = f:read("*a")
2013-09-07 12:06:42 +02:00
f:close()
2014-10-20 15:56:44 +02:00
ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN")
2013-09-07 12:06:42 +02:00
if ws ~= nil then
2014-10-20 15:56:44 +02:00
return ws:match("(%w+):")
2013-09-07 12:06:42 +02:00
else
2014-02-07 12:39:18 +01:00
return "network off"
2013-09-07 12:06:42 +02:00
end
end
2013-09-11 19:39:14 +02:00
local function worker(args)
2013-09-07 12:06:42 +02:00
local args = args or {}
2013-09-11 00:10:53 +02:00
local timeout = args.timeout or 2
2013-09-07 12:06:42 +02:00
local iface = args.iface or net.get_device()
2013-09-10 23:02:11 +02:00
local units = args.units or 1024 --kb
2014-04-13 12:19:02 +02:00
local notify = args.notify or "on"
local screen = args.screen or 1
2013-09-10 23:02:11 +02:00
local settings = args.settings or function() end
2013-09-07 12:06:42 +02:00
2013-09-11 19:39:14 +02:00
net.widget = wibox.widget.textbox('')
2013-09-07 12:06:42 +02:00
2013-09-10 23:02:11 +02:00
helpers.set_map(iface, true)
2013-09-07 12:06:42 +02:00
2013-09-13 18:15:25 +02:00
function update()
2013-09-13 00:07:44 +02:00
net_now = {}
2013-09-10 23:02:11 +02:00
if iface == "" then iface = net.get_device() end
2013-09-07 12:06:42 +02:00
2013-09-13 00:07:44 +02:00
net_now.carrier = helpers.first_line('/sys/class/net/' .. iface ..
2013-09-10 23:02:11 +02:00
'/carrier') or "0"
2013-09-13 00:07:44 +02:00
net_now.state = helpers.first_line('/sys/class/net/' .. iface ..
2013-09-10 23:02:11 +02:00
'/operstate') or "down"
2013-09-07 12:06:42 +02:00
local now_t = helpers.first_line('/sys/class/net/' .. iface ..
2013-09-10 23:02:11 +02:00
'/statistics/tx_bytes') or 0
2013-09-07 12:06:42 +02:00
local now_r = helpers.first_line('/sys/class/net/' .. iface ..
2013-09-10 23:02:11 +02:00
'/statistics/rx_bytes') or 0
2013-09-13 00:07:44 +02:00
net_now.sent = tostring((now_t - net.last_t) / timeout / units)
net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ",", ".")
2013-09-10 23:02:11 +02:00
2013-09-13 00:07:44 +02:00
net_now.received = tostring((now_r - net.last_r) / timeout / units)
net_now.received = string.gsub(string.format('%.1f', net_now.received), ",", ".")
2013-09-10 23:02:11 +02:00
2013-09-11 19:39:14 +02:00
widget = net.widget
2013-09-10 23:02:11 +02:00
settings()
net.last_t = now_t
net.last_r = now_r
2013-09-07 12:06:42 +02:00
2014-04-13 12:19:02 +02:00
if net_now.carrier ~= "1" and notify == "on"
2013-09-07 12:06:42 +02:00
then
if helpers.get_map(iface)
then
2013-09-10 23:02:11 +02:00
naughty.notify({
2014-02-07 12:39:18 +01:00
title = iface,
2013-09-10 23:02:11 +02:00
text = "no carrier",
timeout = 7,
position = "top_left",
icon = helpers.icons_dir .. "no_net.png",
fg = notify_fg or "#FFFFFF",
screen = screen
2013-09-10 23:02:11 +02:00
})
2013-09-07 12:06:42 +02:00
helpers.set_map(iface, false)
end
else
helpers.set_map(iface, true)
end
end
2013-09-13 00:07:44 +02:00
helpers.newtimer(iface, timeout, update)
2013-09-11 19:39:14 +02:00
return net.widget
2013-09-07 12:06:42 +02:00
end
return setmetatable(net, { __call = function(_, ...) return worker(...) end })