Initial commit
This commit is contained in:
39
sys/services/device.lua
Normal file
39
sys/services/device.lua
Normal file
@@ -0,0 +1,39 @@
|
||||
require = requireInjector(getfenv(1))
|
||||
local Event = require('event')
|
||||
local Peripheral = require('peripheral')
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Devices')
|
||||
|
||||
local attachColor = colors.green
|
||||
local detachColor = colors.red
|
||||
|
||||
if not term.isColor() then
|
||||
attachColor = colors.white
|
||||
detachColor = colors.lightGray
|
||||
end
|
||||
|
||||
Event.addHandler('peripheral', function(event, side)
|
||||
if side then
|
||||
local dev = Peripheral.addDevice(side)
|
||||
if dev then
|
||||
term.setTextColor(attachColor)
|
||||
Util.print('[%s] %s attached', dev.side, dev.name)
|
||||
os.queueEvent('device_attach', dev.name)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
Event.addHandler('peripheral_detach', function(event, side)
|
||||
if side then
|
||||
local dev = Util.find(device, 'side', side)
|
||||
if dev then
|
||||
term.setTextColor(detachColor)
|
||||
Util.print('[%s] %s detached', dev.side, dev.name)
|
||||
os.queueEvent('device_detach', dev.name)
|
||||
device[dev.name] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
print('waiting for peripheral changes')
|
||||
Event.pullEvents()
|
||||
43
sys/services/gpshost.lua
Normal file
43
sys/services/gpshost.lua
Normal file
@@ -0,0 +1,43 @@
|
||||
if device.wireless_modem then
|
||||
|
||||
require = requireInjector(getfenv(1))
|
||||
local Config = require('config')
|
||||
local config = {
|
||||
host = false,
|
||||
auto = false,
|
||||
x = 0,
|
||||
y = 0,
|
||||
z = 0,
|
||||
}
|
||||
|
||||
Config.load('gps', config)
|
||||
|
||||
if config.host then
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'GPS Daemon')
|
||||
|
||||
if config.auto then
|
||||
local GPS = require('gps')
|
||||
local pt
|
||||
|
||||
for i = 1, 3 do
|
||||
pt = GPS.getPoint(10, true)
|
||||
if pt then
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not pt then
|
||||
error('Unable to get GPS coordinates')
|
||||
end
|
||||
|
||||
config.x = pt.x
|
||||
config.y = pt.y
|
||||
config.z = pt.z
|
||||
end
|
||||
|
||||
os.run(getfenv(1), '/rom/programs/gps', 'host', config.x, config.y, config.z)
|
||||
|
||||
print('GPS daemon stopped')
|
||||
end
|
||||
end
|
||||
38
sys/services/log.lua
Normal file
38
sys/services/log.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
require = requireInjector(getfenv(1))
|
||||
local Terminal = require('terminal')
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Debug')
|
||||
|
||||
term.redirect(Terminal.scrollable(term.current(), 50))
|
||||
|
||||
local tabId = multishell.getCurrent()
|
||||
local tab = multishell.getTab(tabId)
|
||||
local terminal = term.current()
|
||||
local previousId
|
||||
|
||||
_G.debug = function(pattern, ...)
|
||||
local oldTerm = term.current()
|
||||
term.redirect(terminal)
|
||||
Util.print(pattern, ...)
|
||||
term.redirect(oldTerm)
|
||||
end
|
||||
|
||||
print('Debug started')
|
||||
print('Press ^d to activate debug window')
|
||||
|
||||
multishell.addHotkey(32, function()
|
||||
local currentId = multishell.getFocus()
|
||||
if currentId ~= tabId then
|
||||
previousId = currentId
|
||||
multishell.setFocus(tabId)
|
||||
elseif previousId then
|
||||
multishell.setFocus(previousId)
|
||||
end
|
||||
end)
|
||||
|
||||
os.pullEventRaw('terminate')
|
||||
|
||||
print('Debug stopped')
|
||||
|
||||
_G.debug = function() end
|
||||
multishell.removeHotkey(32)
|
||||
66
sys/services/network.lua
Normal file
66
sys/services/network.lua
Normal file
@@ -0,0 +1,66 @@
|
||||
require = requireInjector(getfenv(1))
|
||||
local Util = require('util')
|
||||
|
||||
multishell.setTitle(multishell.getCurrent(), 'Net Daemon')
|
||||
|
||||
_G.network = { }
|
||||
|
||||
local function netUp()
|
||||
local process = require('process')
|
||||
|
||||
local files = fs.list('/sys/network')
|
||||
|
||||
for _,file in pairs(files) do
|
||||
local fn, msg = loadfile('/sys/network/' .. file, getfenv())
|
||||
if fn then
|
||||
fn()
|
||||
else
|
||||
printError(msg)
|
||||
end
|
||||
end
|
||||
|
||||
while true do
|
||||
local e = process:pullEvent('device_detach')
|
||||
if not device.wireless_modem or e == 'terminate' then
|
||||
for _,c in pairs(network) do
|
||||
c.active = false
|
||||
os.queueEvent('network_detach', c)
|
||||
end
|
||||
os.queueEvent('network_down')
|
||||
process:pullEvent('network_down')
|
||||
process:threadEvent('terminate')
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
Util.clear(_G.network)
|
||||
end
|
||||
|
||||
print('Net daemon started')
|
||||
|
||||
local function startNetwork()
|
||||
print('Starting network services')
|
||||
|
||||
local success, msg = Util.runFunction(
|
||||
Util.shallowCopy(getfenv(1)), netUp)
|
||||
|
||||
if not success and msg then
|
||||
printError(msg)
|
||||
end
|
||||
print('Network services stopped')
|
||||
end
|
||||
|
||||
if device.wireless_modem then
|
||||
startNetwork()
|
||||
else
|
||||
print('No modem detected')
|
||||
end
|
||||
|
||||
while true do
|
||||
local e, deviceName = os.pullEvent('device_attach')
|
||||
if deviceName == 'wireless_modem' then
|
||||
startNetwork()
|
||||
end
|
||||
end
|
||||
|
||||
print('Net daemon stopped')
|
||||
Reference in New Issue
Block a user