lain/widgets/net.lua

105 lines
3.3 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")
2013-09-10 23:02:11 +02:00
local string = { format = string.format,
2014-11-15 13:38:07 +01:00
gsub = string.gsub,
match = string.match }
2013-09-07 12:06:42 +02:00
local setmetatable = setmetatable
-- Network infos
-- lain.widgets.net
2013-09-11 19:39:14 +02:00
local function worker(args)
2015-11-11 18:25:51 +01:00
local net = { last_t = 0, last_r = 0 }
function net.get_device()
local ws = helpers.read_pipe("ip link show | cut -d' ' -f2,9")
ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN")
if ws ~= nil then
return ws:match("(%w+):")
else
return "network off"
end
end
2015-11-11 18:52:40 +01:00
local args = args or {}
local timeout = args.timeout or 2
local units = args.units or 1024 --kb
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
2015-11-11 18:52:40 +01:00
local iface = args.iface or net.get_device()
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()
2015-10-20 15:17:44 +02:00
net_now = {}
2013-09-13 00:07:44 +02:00
2014-11-15 13:38:07 +01:00
if iface == "" or string.match(iface, "network off")
then
iface = net.get_device()
end
2013-09-07 12:06:42 +02:00
2015-10-20 15:17:44 +02:00
net_now.carrier = helpers.first_line('/sys/class/net/' .. iface ..
'/carrier') or "0"
net_now.state = helpers.first_line('/sys/class/net/' .. iface ..
'/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
2015-10-20 15:17:44 +02:00
net_now.sent = (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
2015-10-20 15:17:44 +02:00
net_now.received = (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
2015-10-20 15:17:44 +02:00
widget = net.widget
settings()
2013-09-10 23:02:11 +02:00
2015-10-20 15:17:44 +02:00
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
2015-10-20 15:17:44 +02:00
helpers.newtimer(iface, timeout, update)
2015-11-11 18:25:51 +01:00
return setmetatable(net, { __index = net.widget })
2013-09-07 12:06:42 +02:00
end
2015-11-11 18:25:51 +01:00
return setmetatable({}, { __call = function(_, ...) return worker(...) end })