1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-05 08:26:16 +00:00
opus/apps/Network.lua

146 lines
3.3 KiB
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
require = requireInjector(getfenv(1))
local Event = require('event')
local UI = require('ui')
local Socket = require('socket')
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
2016-12-27 21:01:06 +00:00
table.insert(gridColumns, { heading = 'Fuel', key = 'fuel' })
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' },
{ 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
function sendCommand(host, command)
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({
path = '/apps/telnet.lua',
focused = true,
args = { t.id },
title = t.label,
})
elseif event.type == 'vnc' then
multishell.openTab({
path = '/apps/vnc.lua',
focused = true,
args = { t.id },
title = t.label,
})
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
2016-12-27 03:26:43 +00:00
Event.addThread(function()
2016-12-11 19:24:52 +00:00
while true do
page.grid:update()
page.grid:draw()
page:sync()
os.sleep(1)
end
2016-12-27 03:26:43 +00:00
end)
2016-12-11 19:24:52 +00:00
Event.addHandler('device_attach', function(h, deviceName)
if deviceName == 'wireless_modem' then
page.notification:success('Modem connected')
page:sync()
end
end)
Event.addHandler('device_detach', function(h, deviceName)
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)
2016-12-27 03:26:43 +00:00
Event.pullEvents()
2016-12-11 19:24:52 +00:00
UI.term:reset()