Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PocketOS tracker hub.
- -- Shows a live table of every PocketOS device reporting its location.
- -- The computer whose ID matches 'tracker' in /pocketos/config.lua boots
- -- into this automatically. Needs a wireless modem attached.
- local devices = {}
- local hasModem = false
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- if pcall(rednet.open, side) then hasModem = true end
- end
- end
- if hasModem then
- rednet.host("pocketos.track", "tracker" .. os.getComputerID())
- end
- -- admin safety: only these IDs may pull the fleet location list
- local admins = { 11, 18 }
- do
- local ok, cfg = pcall(dofile, "/pocketos/config.lua")
- if ok and type(cfg) == "table" and type(cfg.admins) == "table" then
- admins = cfg.admins
- end
- end
- local function isAdmin(id)
- for _, a in ipairs(admins) do
- if tonumber(a) == tonumber(id) then return true end
- end
- return false
- end
- local color = term.isColor and term.isColor()
- local function tint(c)
- if color then term.setTextColor(c) end
- end
- local function draw()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- local w, h = term.getSize()
- term.setCursorPos(1, 1)
- tint(colors.yellow)
- write("PocketOS Tracker - computer #" .. os.getComputerID())
- if not hasModem then
- term.setCursorPos(1, 2)
- tint(colors.red)
- write("No modem attached!")
- end
- term.setCursorPos(1, 3)
- tint(colors.lightGray)
- write(("%-4s %-12s %6s %4s %6s %s"):format("ID", "Name", "X", "Y", "Z", "Seen"))
- local ids = {}
- for id in pairs(devices) do ids[#ids + 1] = id end
- table.sort(ids)
- tint(colors.white)
- local y = 4
- for _, id in ipairs(ids) do
- if y > h - 1 then break end
- local d = devices[id]
- local age = math.floor(os.clock() - d.seen)
- term.setCursorPos(1, y)
- write(("%-4d %-12s %6d %4d %6d %ds"):format(
- id, (d.label or "?"):sub(1, 12), d.x, d.y, d.z, age))
- y = y + 1
- end
- if #ids == 0 then
- term.setCursorPos(1, 5)
- tint(colors.gray)
- write("Waiting for reports...")
- term.setCursorPos(1, 7)
- write("On each pocket, set tracker = " .. os.getComputerID())
- term.setCursorPos(1, 8)
- write("in /pocketos/config.lua")
- end
- term.setCursorPos(1, h)
- tint(colors.gray)
- write("Hold Ctrl+T to exit")
- end
- local timer = os.startTimer(1)
- draw()
- while true do
- local e, a, b, c = os.pullEvent()
- if e == "rednet_message" then
- local id, msg, proto = a, b, c
- if proto == "pocketos.track" and type(msg) == "table" then
- if msg.type == "query" then
- -- a developer's Fleet screen asking for all positions
- if isAdmin(id) then
- local list = {}
- for did, d in pairs(devices) do
- list[#list + 1] = {
- id = did, label = d.label, x = d.x, y = d.y, z = d.z,
- age = math.floor(os.clock() - d.seen),
- }
- end
- rednet.send(id, { ok = true, devices = list }, "pocketos.track")
- else
- rednet.send(id, { ok = false, err = "Not an admin" }, "pocketos.track")
- end
- elseif type(msg.x) == "number" and type(msg.y) == "number"
- and type(msg.z) == "number" then
- devices[id] = {
- label = type(msg.label) == "string" and msg.label or nil,
- x = math.floor(msg.x),
- y = math.floor(msg.y),
- z = math.floor(msg.z),
- seen = os.clock(),
- }
- draw()
- end
- end
- elseif e == "timer" and a == timer then
- draw()
- timer = os.startTimer(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment