2018-08-05 11:22:07 -04:00
|
|
|
local M = {}
|
|
|
|
|
2017-07-19 00:36:15 -04:00
|
|
|
local Text = require 'Text'
|
|
|
|
local Line = require 'Line'
|
|
|
|
local ScalePlot = require 'ScalePlot'
|
2017-07-19 00:43:44 -04:00
|
|
|
local Util = require 'Util'
|
2017-07-19 00:36:15 -04:00
|
|
|
|
|
|
|
local __string_gmatch = string.gmatch
|
|
|
|
|
|
|
|
local _PLOT_SEC_BREAK_ = 20
|
|
|
|
local _PLOT_HEIGHT_ = 56
|
|
|
|
|
|
|
|
local network_label_function = function(bytes)
|
2017-07-19 00:43:44 -04:00
|
|
|
local new_unit = Util.get_unit(bytes)
|
2017-07-19 00:36:15 -04:00
|
|
|
|
2017-07-19 00:43:44 -04:00
|
|
|
local converted = Util.convert_bytes(bytes, 'B', new_unit)
|
2017-07-19 00:36:15 -04:00
|
|
|
local precision = 0
|
|
|
|
if converted < 10 then precision = 1 end
|
|
|
|
|
2017-07-19 00:43:44 -04:00
|
|
|
return Util.round_to_string(converted, precision)..' '..new_unit..'/s'
|
2017-07-19 00:36:15 -04:00
|
|
|
end
|
|
|
|
|
2017-07-20 00:49:24 -04:00
|
|
|
local header = _G_Widget_.Header{
|
2017-07-19 00:36:15 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
|
|
|
y = _G_INIT_DATA_.TOP_Y,
|
|
|
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
|
|
|
header = 'NETWORK'
|
|
|
|
}
|
|
|
|
|
|
|
|
local _RIGHT_X_ = _G_INIT_DATA_.CENTER_RIGHT_X + _G_INIT_DATA_.SECTION_WIDTH
|
|
|
|
|
|
|
|
local dnload = {
|
2017-07-20 00:49:24 -04:00
|
|
|
label = _G_Widget_.Text{
|
2017-07-19 00:36:15 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
|
|
|
y = header.bottom_y,
|
|
|
|
text = 'Download',
|
|
|
|
},
|
2017-07-20 00:49:24 -04:00
|
|
|
speed = _G_Widget_.Text{
|
2017-07-19 00:36:15 -04:00
|
|
|
x = _RIGHT_X_,
|
|
|
|
y = header.bottom_y,
|
|
|
|
x_align = 'right',
|
2020-01-21 23:58:31 -05:00
|
|
|
text_color = _G_Patterns_.PURPLE
|
2017-07-19 00:36:15 -04:00
|
|
|
},
|
2017-07-20 00:49:24 -04:00
|
|
|
plot = _G_Widget_.ScalePlot{
|
2017-07-19 00:36:15 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
|
|
|
y = header.bottom_y + _PLOT_SEC_BREAK_,
|
|
|
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
|
|
|
height = _PLOT_HEIGHT_,
|
|
|
|
y_label_func = network_label_function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local _UPLOAD_Y_ = header.bottom_y + _PLOT_HEIGHT_ + _PLOT_SEC_BREAK_ * 2
|
|
|
|
|
|
|
|
local upload = {
|
2017-07-20 00:49:24 -04:00
|
|
|
label = _G_Widget_.Text{
|
2017-07-19 00:36:15 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
|
|
|
y = _UPLOAD_Y_,
|
|
|
|
text = 'Upload',
|
|
|
|
},
|
2017-07-20 00:49:24 -04:00
|
|
|
speed = _G_Widget_.Text{
|
2017-07-19 00:36:15 -04:00
|
|
|
x = _RIGHT_X_,
|
|
|
|
y = _UPLOAD_Y_,
|
|
|
|
x_align = 'right',
|
2020-01-21 23:58:31 -05:00
|
|
|
text_color = _G_Patterns_.PURPLE
|
2017-07-19 00:36:15 -04:00
|
|
|
},
|
2017-07-20 00:49:24 -04:00
|
|
|
plot = _G_Widget_.ScalePlot{
|
2017-07-19 00:36:15 -04:00
|
|
|
x = _G_INIT_DATA_.CENTER_RIGHT_X,
|
|
|
|
y = _UPLOAD_Y_ + _PLOT_SEC_BREAK_,
|
|
|
|
width = _G_INIT_DATA_.SECTION_WIDTH,
|
|
|
|
height = _PLOT_HEIGHT_,
|
|
|
|
y_label_func = network_label_function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local interface_counters_tbl = {}
|
|
|
|
|
|
|
|
local update = function(cr, update_frequency)
|
|
|
|
local dspeed, uspeed = 0, 0
|
|
|
|
|
|
|
|
local rx_delta, tx_delta
|
|
|
|
|
|
|
|
-- iterate through the route file and filter on interfaces that are gateways (flag = 0003)
|
2017-07-19 00:43:44 -04:00
|
|
|
local iterator = __string_gmatch(Util.read_file('/proc/net/route'),
|
2017-07-19 00:36:15 -04:00
|
|
|
'(%w+)%s+%w+%s+%w+%s+0003%s+')
|
|
|
|
|
|
|
|
for interface in iterator do
|
|
|
|
local interface_counters = interface_counters_tbl[interface]
|
|
|
|
|
|
|
|
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,
|
2017-07-19 00:43:44 -04:00
|
|
|
prev_rx_byte_cnt = Util.read_file(rx_path, nil, '*n'),
|
|
|
|
prev_tx_byte_cnt = Util.read_file(tx_path, nil, '*n'),
|
2017-07-19 00:36:15 -04:00
|
|
|
}
|
|
|
|
interface_counters_tbl[interface] = interface_counters
|
|
|
|
end
|
|
|
|
|
2017-07-19 00:43:44 -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')
|
2017-07-19 00:36:15 -04:00
|
|
|
|
|
|
|
rx_delta = rx_byte_cnt - interface_counters.prev_rx_byte_cnt
|
|
|
|
tx_delta = tx_byte_cnt - interface_counters.prev_tx_byte_cnt
|
|
|
|
|
|
|
|
interface_counters.prev_rx_byte_cnt = rx_byte_cnt
|
|
|
|
interface_counters.prev_tx_byte_cnt = tx_byte_cnt
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
end
|
|
|
|
|
2017-07-19 00:43:44 -04:00
|
|
|
local dspeed_unit = Util.get_unit(dspeed)
|
|
|
|
local uspeed_unit = Util.get_unit(uspeed)
|
2017-07-19 00:36:15 -04:00
|
|
|
|
|
|
|
dnload.speed.append_end = ' '..dspeed_unit..'/s'
|
|
|
|
upload.speed.append_end = ' '..uspeed_unit..'/s'
|
|
|
|
|
2017-07-19 00:43:44 -04:00
|
|
|
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))
|
2017-07-19 00:36:15 -04:00
|
|
|
|
|
|
|
ScalePlot.update(dnload.plot, cr, dspeed)
|
|
|
|
ScalePlot.update(upload.plot, cr, uspeed)
|
|
|
|
end
|
|
|
|
|
|
|
|
_PLOT_SEC_BREAK_ = nil
|
|
|
|
_PLOT_HEIGHT_ = nil
|
|
|
|
_RIGHT_X_ = nil
|
|
|
|
_UPLOAD_Y_ = nil
|
|
|
|
|
2018-08-05 11:22:07 -04:00
|
|
|
local draw_static = function(cr)
|
2018-08-05 11:35:00 -04:00
|
|
|
Text.draw(header.text, cr)
|
|
|
|
Line.draw(header.underline, cr)
|
2018-08-05 11:22:07 -04:00
|
|
|
|
2018-08-05 11:35:00 -04:00
|
|
|
Text.draw(dnload.label, cr)
|
|
|
|
Text.draw(upload.label, cr)
|
2018-08-05 11:22:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
local draw_dynamic = function(cr, update_frequency)
|
2018-08-05 11:08:37 -04:00
|
|
|
update(cr, update_frequency)
|
|
|
|
|
|
|
|
Text.draw(dnload.speed, cr)
|
2018-08-05 14:29:52 -04:00
|
|
|
ScalePlot.draw_dynamic(dnload.plot, cr)
|
2018-08-05 11:08:37 -04:00
|
|
|
|
|
|
|
Text.draw(upload.speed, cr)
|
2018-08-05 14:29:52 -04:00
|
|
|
ScalePlot.draw_dynamic(upload.plot, cr)
|
2017-07-19 00:36:15 -04:00
|
|
|
end
|
|
|
|
|
2018-08-05 11:22:07 -04:00
|
|
|
M.draw_static = draw_static
|
|
|
|
M.draw_dynamic = draw_dynamic
|
|
|
|
|
|
|
|
return M
|