1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 13:14:49 +00:00
opus/sys/init/5.network.lua

70 lines
1.4 KiB
Lua
Raw Normal View History

local Config = require('opus.config')
2018-11-11 19:45:04 +00:00
local device = _G.device
local kernel = _G.kernel
local os = _G.os
2018-01-14 04:40:53 +00:00
do
local config = Config.load('os')
_G.network = setmetatable({ }, { __index = {
getGroup = function() return config.group end,
setGroup = function(name)
config.group = name
end
}})
end
2018-01-14 21:44:43 +00:00
local function startNetwork()
kernel.run({
title = 'Net daemon',
path = 'sys/apps/netdaemon.lua',
hidden = true,
})
end
2018-11-11 19:45:04 +00:00
local function setModem(dev)
if not device.wireless_modem and dev.isWireless() then
2018-12-23 03:11:16 +00:00
local config = Config.load('os')
if not config.wirelessModem or
config.wirelessModem == 'auto' or
dev.name == config.wirelessModem then
2018-11-11 19:45:04 +00:00
device.wireless_modem = dev
os.queueEvent('device_attach', 'wireless_modem')
return dev
end
end
end
2019-10-31 04:49:30 +00:00
-- create a psuedo-device named 'wireless_modem'
2018-01-14 04:40:53 +00:00
kernel.hook('device_attach', function(_, eventData)
2018-11-11 19:45:04 +00:00
local dev = device[eventData[1]]
2018-11-12 20:22:29 +00:00
if dev and dev.type == 'modem' then
2018-11-11 19:45:04 +00:00
if setModem(dev) then
startNetwork()
end
end
end)
kernel.hook('device_detach', function(_, eventData)
if device.wireless_modem and eventData[1] == device.wireless_modem.name then
device['wireless_modem'] = nil
os.queueEvent('device_detach', 'wireless_modem')
2018-01-14 04:40:53 +00:00
end
end)
2018-01-14 21:44:43 +00:00
2018-11-11 19:45:04 +00:00
for _,dev in pairs(device) do
if dev.type == 'modem' then
if setModem(dev) then
break
end
end
end
if device.wireless_modem then
2018-01-21 22:22:59 +00:00
print('waiting for network...')
2018-01-14 21:44:43 +00:00
startNetwork()
2018-01-21 11:09:25 +00:00
os.pullEvent('network_up')
2018-01-14 21:44:43 +00:00
end