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 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")
|
2016-03-03 12:40:49 +01:00
|
|
|
if ws then return ws:match("(%w+):")
|
|
|
|
else return "network off" end
|
2015-11-11 18:25:51 +01:00
|
|
|
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()
|
2014-11-15 13:21:22 +01: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()
|
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
|
|
|
|
2016-03-03 12:40:49 +01:00
|
|
|
net_now.carrier = helpers.first_line(string.format('/sys/class/net/%s/carrier', iface)) or '0'
|
|
|
|
net_now.state = helpers.first_line(string.format('/sys/class/net/%s/operstate', iface)) or 'down'
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2016-03-03 12:40:49 +01:00
|
|
|
local now_t = helpers.first_line(string.format('/sys/class/net/%s/statistics/tx_bytes', iface)) or 0
|
|
|
|
local now_r = helpers.first_line(string.format('/sys/class/net/%s/statistics/rx_bytes', iface)) or 0
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2016-04-04 14:09:54 +02:00
|
|
|
if now_t ~= net.last_t or now_r ~= net.last_r then
|
2016-03-03 12:40:49 +01:00
|
|
|
net_now.sent = (now_t - net.last_t) / timeout / units
|
|
|
|
net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ',', '.')
|
|
|
|
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
|
|
|
|
2016-03-03 12:40:49 +01:00
|
|
|
widget = net.widget
|
|
|
|
settings()
|
2013-09-10 23:02:11 +02:00
|
|
|
|
2016-03-03 12:40:49 +01:00
|
|
|
net.last_t = now_t
|
|
|
|
net.last_r = now_r
|
|
|
|
end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2016-03-03 12:40:49 +01:00
|
|
|
if not string.match(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",
|
|
|
|
icon = helpers.icons_dir .. "no_net.png",
|
2014-10-11 14:45:14 +02:00
|
|
|
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 })
|