opus/sys/apps/Network.lua

147 lines
3.4 KiB
Lua
Raw Normal View History

requireInjector(getfenv(1))
local Event = require('event')
2016-12-11 19:24:52 +00:00
local Socket = require('socket')
local UI = require('ui')
local Util = require('util')
2016-12-11 19:24:52 +00:00
multishell.setTitle(multishell.getCurrent(), 'Network')
UI:configure('Network', ...)
local gridColumns = {
2016-12-27 21:01:06 +00:00
{ heading = 'Label', key = 'label' },
{ heading = 'Dist', key = 'distance' },
{ heading = 'Status', key = 'status' },
2016-12-11 19:24:52 +00:00
}
if UI.term.width >= 30 then
2017-10-01 00:35:36 +00:00
table.insert(gridColumns, { heading = 'Fuel', key = 'fuel', width = 5 })
2016-12-11 19:24:52 +00:00
table.insert(gridColumns, { heading = 'Uptime', key = 'uptime' })
end
2016-12-27 21:01:06 +00:00
local page = UI.Page {
menuBar = UI.MenuBar {
2016-12-11 19:24:52 +00:00
buttons = {
{ text = 'Telnet', event = 'telnet' },
{ text = 'VNC', event = 'vnc' },
2017-05-14 01:05:05 +00:00
{ text = 'Trust', event = 'trust' },
2016-12-11 19:24:52 +00:00
{ text = 'Reboot', event = 'reboot' },
},
2016-12-27 21:01:06 +00:00
},
grid = UI.ScrollingGrid {
2016-12-11 19:24:52 +00:00
y = 2,
values = network,
columns = gridColumns,
sortColumn = 'label',
autospace = true,
2016-12-27 21:01:06 +00:00
},
notification = UI.Notification { },
2016-12-11 19:24:52 +00:00
accelerators = {
q = 'quit',
c = 'clear',
},
2016-12-27 21:01:06 +00:00
}
2016-12-11 19:24:52 +00:00
2017-05-14 01:05:05 +00:00
local function sendCommand(host, command)
2016-12-11 19:24:52 +00:00
if not device.wireless_modem then
page.notification:error('Wireless modem not present')
return
end
page.notification:info('Connecting')
page:sync()
local socket = Socket.connect(host, 161)
if socket then
socket:write({ type = command })
socket:close()
page.notification:success('Command sent')
else
page.notification:error('Failed to connect')
end
end
function page:eventHandler(event)
2016-12-27 21:01:06 +00:00
local t = self.grid:getSelected()
2016-12-11 19:24:52 +00:00
if t then
if event.type == 'telnet' or event.type == 'grid_select' then
multishell.openTab({
2017-05-20 23:05:00 +00:00
path = 'sys/apps/telnet.lua',
2016-12-11 19:24:52 +00:00
focused = true,
args = { t.id },
title = t.label,
})
elseif event.type == 'vnc' then
multishell.openTab({
2017-05-20 23:05:00 +00:00
path = 'sys/apps/vnc.lua',
2016-12-11 19:24:52 +00:00
focused = true,
args = { t.id },
title = t.label,
})
2017-05-14 01:05:05 +00:00
elseif event.type == 'trust' then
2017-05-19 23:00:23 +00:00
shell.openForegroundTab('trust ' .. t.id)
2016-12-11 19:24:52 +00:00
elseif event.type == 'reboot' then
sendCommand(t.id, 'reboot')
elseif event.type == 'shutdown' then
sendCommand(t.id, 'shutdown')
end
end
if event.type == 'quit' then
Event.exitPullEvents()
end
UI.Page.eventHandler(self, event)
end
function page.grid:getRowTextColor(row, selected)
if not row.active then
return colors.orange
end
return UI.Grid.getRowTextColor(self, row, selected)
end
function page.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
if row.uptime then
if row.uptime < 60 then
row.uptime = string.format("%ds", math.floor(row.uptime))
else
row.uptime = string.format("%sm", math.floor(row.uptime/6)/10)
end
end
if row.fuel then
row.fuel = Util.toBytes(row.fuel)
end
if row.distance then
row.distance = Util.round(row.distance, 1)
end
return row
end
2017-07-24 02:37:07 +00:00
Event.onInterval(1, function()
page.grid:update()
page.grid:draw()
page:sync()
2016-12-27 03:26:43 +00:00
end)
2016-12-11 19:24:52 +00:00
2017-07-24 02:37:07 +00:00
Event.on('device_attach', function(h, deviceName)
2016-12-11 19:24:52 +00:00
if deviceName == 'wireless_modem' then
page.notification:success('Modem connected')
page:sync()
end
end)
2017-07-24 02:37:07 +00:00
Event.on('device_detach', function(h, deviceName)
2016-12-11 19:24:52 +00:00
if deviceName == 'wireless_modem' then
page.notification:error('Wireless modem not attached')
page:sync()
end
end)
if not device.wireless_modem then
page.notification:error('Wireless modem not attached')
end
UI:setPage(page)
2017-07-28 23:01:59 +00:00
UI:pullEvents()