lain/widgets/net.lua

105 lines
3.1 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")
local io = io
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")
ws = f:read("*all")
f:close()
ws = ws:match("%w+: UP")
if ws ~= nil then
return ws:gsub(": UP", "")
else
return ""
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
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
2013-09-13 00:07:44 +02:00
if net_now.carrier ~= "1"
2013-09-07 12:06:42 +02:00
then
if helpers.get_map(iface)
then
n_title = iface
2013-09-10 23:02:11 +02:00
if n_title == "" then n_title = "network" end
naughty.notify({
title = n_title,
text = "no carrier",
timeout = 7,
position = "top_left",
icon = helpers.icons_dir .. "no_net.png",
fg = notify_fg or "#FFFFFF"
})
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 })