Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local global_run = true
- local modem = peripheral.find("modem")
- local monitor = peripheral.find("monitor")
- if not monitor then
- print("No monitor found!")
- end
- term.redirect(monitor)
- local branches = {}
- local function find(id)
- if tonumber(id) then
- for _, v in pairs(branches) do
- if v.id == id then
- return v
- end
- end
- else
- for _, v in pairs(branches) do
- if v.name == id then
- return v
- end
- end
- end
- -- not found
- return nil
- end
- local function checkSide(side)
- return side == "top" or side == "bottom" or side == "left" or side == "right" or side == "front" or side == "back"
- end
- local wid, hig = term.getSize()
- local evqueue = {}
- local function pullCommand()
- term.redirect(term.native())
- local buf = ":"
- term.scroll(1)
- term.setCursorPos(1, hig)
- term.write(buf)
- local running = true
- while running do
- local ev = {os.pullEvent()}
- if ev[1] == "char" then
- buf = buf .. ev[2]
- elseif ev[1] == "key" then
- if ev[2] == keys.enter then
- running = false
- elseif ev[2] == keys.backspace then
- buf = buf:sub(1, #buf - 1)
- if #buf == 0 then
- running = false
- end
- end
- else
- table.insert(evqueue, ev)
- end
- term.setCursorPos(1, hig)
- term.clearLine(hig)
- term.write(buf)
- end
- local words = {}
- for str in buf:gmatch("%S+") do
- table.insert(words, str)
- end
- if #words < 1 then
- return
- end
- words[1] = words[1]:sub(2, #words[1])
- print()
- if words[1] == "test" then
- print("aawagga")
- elseif words[1] == "q" then
- print("Exiting. Goodbye!")
- global_run = false
- elseif words[1] == "redstone" then
- if (words[2] == "on" or words[2] == "off") and words[3] and words[4] and checkSide(words[3]) then
- local receiver = find(words[4])
- if receiver then
- modem.transmit(15, 43, receiver.id .. " command redstone " .. words[3] .. " " .. words[2])
- else
- print("Computer name/id `" .. words[3] .. "` not found!")
- end
- else
- print("Usage: redstone <on/off> <side> <receiver name/id>")
- end
- elseif words[1] == "ping" then
- if words[2] and ((not words[3]) or tonumber(words[3])) then
- local receiver = find(words[2])
- if receiver then
- modem.transmit(15, 43, receiver.id .. " ping")
- local id = os.startTimer(words[3] and tonumber(words[3]) or 3)
- local ev
- local replied = false
- local running_2 = true
- while running_2 do
- ev = {os.pullEvent()}
- if ev[1] == "timer" and ev[2] == id then
- running_2 = false --timed out
- elseif ev[1] == "modem_message" then
- if ev[5] == "ping_reply " .. receiver.id then
- replied = true
- running_2 = false
- os.cancelTimer(id)
- end
- end
- end
- if replied then
- print("Received reply from " .. words[2])
- else
- print("Did not receive reply from " .. words[2])
- end
- else
- print("Computer name/id `" .. words[2] .. "` not found!")
- end
- else
- print("Usage: ping <receiver name/id> [timeout]")
- end
- elseif words[1] == "enable" then
- if words[2] then
- local receiver = find(words[2])
- if receiver then
- modem.transmit(15, 43, receiver.id .. " enable")
- else
- print("Computer name/id `" .. words[2] .. "` not found!")
- end
- else
- print("Usage: enable <receiver name/id>")
- end
- elseif words[1] == "disable" then
- if words[2] then
- local receiver = find(words[2])
- if receiver then
- modem.transmit(15, 43, receiver.id .. " disable")
- else
- print("Computer name/id `" .. words[2] .. "` not found!")
- end
- else
- print("Usage: disable <receiver name/id>")
- end
- elseif words[1] == "scan" then
- modem.transmit(15, 43, "hub_scan")
- elseif words[1] == "help" then
- if not words[2] then
- print("hubOS commands:")
- print("redstone, ping, <...no more yet>")
- print("Run :help <command_name> for more info on any command.")
- elseif words[2] == "redstone" then
- print("hubOS help: redstone")
- print("Usage: redstone <on/off> <side> <receiver name/id>")
- print("Enables or disables the redstone output on a given side for a computer.")
- elseif words[2] == "ping" then
- print("hubOS help: ping")
- print("ping <receiver name/id> [timeout]")
- print("Pings a given computer to check whether it is online. Times out after 3 seconds, or after the passed timeout in seconds.")
- elseif words[2] == "enable" or words[2] == "disable" then
- print("hubOS help: enable/disable")
- print("enable <receiver name/id>")
- print("disable <receiver name/id>")
- print("Enables or disables the redstone output as configured in the computer's branch_config.cfg file.")
- end
- else
- print("Unknown command: " .. words[1])
- end
- term.redirect(monitor)
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("Starting hubOS...")
- --Set up the modem for transmit
- if not modem then
- term.setTextColor(colors.red)
- print("No modem attached! Halting.")
- do return end
- end
- modem.open(43) --listen on 43
- os.startTimer(0.05)
- --Send a seeker message
- modem.transmit(15, 43, "hub_boot")
- while global_run do
- local event, ev1, ev2, ev3, ev4, ev5 = os.pullEvent()
- if event == "timer" then
- os.startTimer(0.05)
- elseif event == "modem_message" then
- print("message " .. ev4)
- local words = {}
- for word in ev4:gmatch("%S+") do
- table.insert(words, word)
- end
- if words[1] == "feeder" then
- if not find(words[3]) then
- print("Registering branch " .. words[2] .. " as " .. words[3])
- branches[tonumber(words[2])] = {id = tonumber(words[2]), name = words[3]}
- else
- local holder = find(words[3])
- print("Already using ID " .. words[3] .. " for computer " .. holder.id)
- end
- end
- elseif event == "char" then
- if ev1 == ":" then
- pullCommand()
- end
- else
- --print(event)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment