2016-08-11 23:25:56 -04:00
|
|
|
local Widget = require 'Widget'
|
|
|
|
local Text = require 'Text'
|
|
|
|
local Line = require 'Line'
|
|
|
|
local ScalePlot = require 'ScalePlot'
|
|
|
|
local util = require 'util'
|
|
|
|
local schema = require 'default_patterns'
|
|
|
|
|
2017-07-17 00:21:31 -04:00
|
|
|
local __string_gmatch = string.gmatch
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 00:21:31 -04:00
|
|
|
local _PLOT_SEC_BREAK_ = 20
|
|
|
|
local _PLOT_HEIGHT_ = 56
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 00:21:31 -04:00
|
|
|
local network_label_function = function(bytes)
|
2016-08-11 23:25:56 -04:00
|
|
|
local new_unit = util.get_unit(bytes)
|
|
|
|
|
|
|
|
local converted = util.convert_bytes(bytes, 'B', new_unit)
|
|
|
|
local precision = 0
|
|
|
|
if converted < 10 then precision = 1 end
|
|
|
|
|
|
|
|
return util.round_to_string(converted, precision)..' '..new_unit..'/s'
|
|
|
|
end
|
|
|
|
|
|
|
|
local header = Widget.Header{
|
2017-07-16 14:50:33 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
|
|
|
y = _G_INIT_DATA_.TOP_Y,
|
|
|
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
2017-07-17 00:21:31 -04:00
|
|
|
header = 'NETWORK'
|
2016-08-11 23:25:56 -04:00
|
|
|
}
|
|
|
|
|
2017-07-17 00:21:31 -04:00
|
|
|
local _RIGHT_X_ = _G_INIT_DATA_.CENTER_RIGHT_X + _G_INIT_DATA_.SECTION_WIDTH
|
2016-08-11 23:25:56 -04:00
|
|
|
|
|
|
|
local dnload = {
|
|
|
|
label = Widget.Text{
|
2017-07-16 14:50:33 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
2016-08-11 23:25:56 -04:00
|
|
|
y = header.bottom_y,
|
|
|
|
text = 'Download',
|
|
|
|
},
|
|
|
|
speed = Widget.Text{
|
2017-07-17 00:21:31 -04:00
|
|
|
x = _RIGHT_X_,
|
2016-08-11 23:25:56 -04:00
|
|
|
y = header.bottom_y,
|
|
|
|
x_align = 'right',
|
|
|
|
text_color = schema.blue
|
|
|
|
},
|
|
|
|
plot = Widget.ScalePlot{
|
2017-07-16 14:50:33 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
2017-07-17 00:21:31 -04:00
|
|
|
y = header.bottom_y + _PLOT_SEC_BREAK_,
|
2017-07-16 14:50:33 -04:00
|
|
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
2017-07-17 00:21:31 -04:00
|
|
|
height = _PLOT_HEIGHT_,
|
|
|
|
y_label_func = network_label_function
|
2016-08-11 23:25:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 00:21:31 -04:00
|
|
|
local _UPLOAD_Y_ = header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2
|
2016-08-11 23:25:56 -04:00
|
|
|
|
|
|
|
local upload = {
|
|
|
|
label = Widget.Text{
|
2017-07-16 14:50:33 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
2017-07-17 00:21:31 -04:00
|
|
|
y = _UPLOAD_Y_,
|
2016-08-11 23:25:56 -04:00
|
|
|
text = 'Upload',
|
|
|
|
},
|
|
|
|
speed = Widget.Text{
|
2017-07-17 00:21:31 -04:00
|
|
|
x = _RIGHT_X_,
|
|
|
|
y = _UPLOAD_Y_,
|
2016-08-11 23:25:56 -04:00
|
|
|
x_align = 'right',
|
|
|
|
text_color = schema.blue
|
|
|
|
},
|
|
|
|
plot = Widget.ScalePlot{
|
2017-07-16 14:50:33 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
2017-07-17 00:21:31 -04:00
|
|
|
y = _UPLOAD_Y_ + _PLOT_SEC_BREAK_,
|
2017-07-16 14:50:33 -04:00
|
|
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
2017-07-17 00:21:31 -04:00
|
|
|
height = _PLOT_HEIGHT_,
|
|
|
|
y_label_func = network_label_function
|
2016-08-11 23:25:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
local interface_counters_tbl = {}
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 00:21:31 -04:00
|
|
|
local update = function(cr, update_frequency)
|
2016-08-11 23:25:56 -04:00
|
|
|
local dspeed, uspeed = 0, 0
|
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
local rx_delta, tx_delta
|
|
|
|
|
|
|
|
-- iterate through the route file and filter on interfaces that are gateways (flag = 0003)
|
|
|
|
local iterator = __string_gmatch(util.read_file('/proc/net/route'),
|
|
|
|
'(%w+)%s+%w+%s+%w+%s+0003%s+')
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
for interface in iterator do
|
|
|
|
local interface_counters = interface_counters_tbl[interface]
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
if not interface_counters then
|
|
|
|
local rx_path = '/sys/class/net/'..interface..'/statistics/rx_bytes'
|
|
|
|
local tx_path = '/sys/class/net/'..interface..'/statistics/tx_bytes'
|
|
|
|
|
|
|
|
interface_counters = {
|
|
|
|
rx_path = rx_path,
|
|
|
|
tx_path = tx_path,
|
|
|
|
prev_rx_byte_cnt = util.read_file(rx_path, nil, '*n'),
|
|
|
|
prev_tx_byte_cnt = util.read_file(tx_path, nil, '*n'),
|
|
|
|
}
|
|
|
|
interface_counters_tbl[interface] = interface_counters
|
2016-08-11 23:25:56 -04:00
|
|
|
end
|
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
local rx_byte_cnt = util.read_file(interface_counters.rx_path, nil, '*n')
|
|
|
|
local tx_byte_cnt = util.read_file(interface_counters.tx_path, nil, '*n')
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
rx_delta = rx_byte_cnt - interface_counters.prev_rx_byte_cnt
|
|
|
|
tx_delta = tx_byte_cnt - interface_counters.prev_tx_byte_cnt
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
interface_counters.prev_rx_byte_cnt = rx_byte_cnt
|
|
|
|
interface_counters.prev_tx_byte_cnt = tx_byte_cnt
|
2016-08-11 23:25:56 -04:00
|
|
|
|
2017-07-17 01:34:03 -04:00
|
|
|
-- mask overflow
|
|
|
|
if rx_delta > 0 then dspeed = dspeed + rx_delta * update_frequency end
|
|
|
|
if tx_delta > 0 then uspeed = uspeed + tx_delta * update_frequency end
|
2016-08-11 23:25:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
local dspeed_unit = util.get_unit(dspeed)
|
|
|
|
local uspeed_unit = util.get_unit(uspeed)
|
|
|
|
|
|
|
|
dnload.speed.append_end = ' '..dspeed_unit..'/s'
|
|
|
|
upload.speed.append_end = ' '..uspeed_unit..'/s'
|
|
|
|
|
|
|
|
Text.set(dnload.speed, cr, util.precision_convert_bytes(dspeed, 'B', dspeed_unit, 3))
|
|
|
|
Text.set(upload.speed, cr, util.precision_convert_bytes(uspeed, 'B', uspeed_unit, 3))
|
|
|
|
|
|
|
|
ScalePlot.update(dnload.plot, cr, dspeed)
|
|
|
|
ScalePlot.update(upload.plot, cr, uspeed)
|
|
|
|
end
|
|
|
|
|
|
|
|
Widget = nil
|
|
|
|
schema = nil
|
2017-07-17 00:21:31 -04:00
|
|
|
_PLOT_SEC_BREAK_ = nil
|
|
|
|
_PLOT_HEIGHT_ = nil
|
|
|
|
_RIGHT_X_ = nil
|
|
|
|
_UPLOAD_Y_ = nil
|
2016-08-11 23:25:56 -04:00
|
|
|
|
|
|
|
local draw = function(cr, current_interface, update_frequency)
|
2017-07-17 00:21:31 -04:00
|
|
|
update(cr, update_frequency)
|
2016-08-11 23:25:56 -04:00
|
|
|
|
|
|
|
if current_interface == 0 then
|
|
|
|
Text.draw(header.text, cr)
|
|
|
|
Line.draw(header.underline, cr)
|
|
|
|
|
|
|
|
Text.draw(dnload.label, cr)
|
|
|
|
Text.draw(dnload.speed, cr)
|
|
|
|
ScalePlot.draw(dnload.plot, cr)
|
|
|
|
|
|
|
|
Text.draw(upload.label, cr)
|
|
|
|
Text.draw(upload.speed, cr)
|
|
|
|
ScalePlot.draw(upload.plot, cr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return draw
|