1
0
mirror of https://github.com/kepler155c/opus synced 2024-06-16 10:19:59 +00:00

support preferred wireless modems

This commit is contained in:
kepler155c@gmail.com 2018-11-11 14:45:04 -05:00
parent 4721b6aec1
commit 17d55e75ce
2 changed files with 43 additions and 13 deletions

View File

@ -28,9 +28,9 @@ function Peripheral.addDevice(deviceList, side)
end end
if ptype == 'modem' then if ptype == 'modem' then
if Peripheral.call(name, 'isWireless') then if not Peripheral.call(name, 'isWireless') then
ptype = 'wireless_modem' -- ptype = 'wireless_modem'
else -- else
ptype = 'wired_modem' ptype = 'wired_modem'
end end
end end
@ -57,10 +57,6 @@ function Peripheral.addDevice(deviceList, side)
-- this can randomly fail -- this can randomly fail
pcall(function() pcall(function()
deviceList[name] = Peripheral.wrap(side) deviceList[name] = Peripheral.wrap(side)
if ptype == 'wireless_modem' and not deviceList.wireless_modem then
-- TODO: fix this
deviceList.wireless_modem = deviceList[name]
end
end) end)
if deviceList[name] then if deviceList[name] then

View File

@ -1,3 +1,8 @@
_G.requireInjector(_ENV)
local Config = require('config')
local device = _G.device
local kernel = _G.kernel local kernel = _G.kernel
local os = _G.os local os = _G.os
@ -11,15 +16,44 @@ local function startNetwork()
}) })
end end
local function setModem(dev)
if not device.wireless_modem and dev.isWireless() then
local config = Config.load('os', { })
if not config.wirelessModem or dev.name == config.wirelessModem then
device.wireless_modem = dev
os.queueEvent('device_attach', 'wireless_modem')
return dev
end
end
end
-- create a psuedo-device named 'wireleess_modem'
kernel.hook('device_attach', function(_, eventData) kernel.hook('device_attach', function(_, eventData)
if eventData[1] == 'wireless_modem' then local dev = device[eventData[1]]
if dev.type == 'modem' then
if setModem(dev) then
startNetwork() startNetwork()
end end
end
end) end)
if _G.device.wireless_modem then 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')
end
end)
for _,dev in pairs(device) do
if dev.type == 'modem' then
if setModem(dev) then
break
end
end
end
if device.wireless_modem then
print('waiting for network...') print('waiting for network...')
startNetwork() startNetwork()
os.pullEvent('network_up') os.pullEvent('network_up')
end end