Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local term = require("term")
- local component = require("component")
- local event = require("event")
- local thread = require("thread")
- local text = require("text")
- local utils = require("psiutils")
- local gpu = component.gpu
- local DroneList, DroneDetail, Terminal
- local threads = {}
- local running = true
- local registeredDrones = {}
- local unregisteredDrones = {}
- -- Shared Functions
- local function log(msg, level)
- DroneRegistry.log.log("[GUI] " .. msg, level)
- end
- local function SetupViews()
- gpu.setResolution(80, 25)
- term.clear()
- local right, bottom = gpu.getResolution()
- local left, top = 1, 1
- local centre = (top - 1) + math.ceil((bottom - (top - 1))/2)
- local centreTop = (left - 1) + math.floor((right - (left - 1))/2)
- DroneList = utils.createWindow(left, top, (centreTop - 1) - left, (centre - 1) - top) --{["x"] = left + 1, ["y"] = top + 1, ["w"] = (centreTop - 1) - left, ["h"] = (centre - 1) - top}
- DroneDetail = utils.createWindow(centreTop, top, (right - 1) - centreTop, (centre - 1) - top) --{["x"] = centreTop + 1, ["y"] = top + 1, ["w"] = (right - 1) - centreTop, ["h"] = (centre - 1) - top}
- Terminal = {}
- Terminal.console = utils.createWindow(left, centre, (right - 1) - left, (bottom - 1) - centre - 1) --{["x"] = left + 1, ["y"] = centre + 1, ["w"] = (right - 1) - left , ["h"] = (bottom - 1) - centre}
- Terminal.input = utils.createWindow(left, bottom - 2, (right - 1) - left, 1)
- gpu.setBackground(0x222222)
- gpu.fill(left, top, right, bottom, " ")
- gpu.setBackground(0x000000)
- DroneList:as_window(term.clear)
- DroneDetail:as_window(term.clear)
- Terminal.console:as_window(term.clear)
- Terminal.input:as_window(term.clear)
- end
- local function DrawLine(Window, line, ...)
- local args = {...}
- local n = #args
- local coloumnW = math.floor((Window.width/n) + 0.5)
- local coloumns = {}
- for i = 1, n - 1 do
- coloumns[i] = coloumnW
- end
- coloumns[n] = Window.width - (coloumnW * (n - 1))
- local i = 1
- while i <= n do
- local l = coloumns[i]
- ln = 1
- while args[i + ln] == "@" do
- l = l + coloumns[i + ln]
- ln = ln + 1
- end
- Window:as_window(term.setCursor, ((i - 1) * (coloumnW)) + 2, line)
- Window:as_window(term.write, string.sub(args[i], 1, l - 2))
- i = i + ln
- end
- end
- local function Shutdown()
- running = false
- gpu.setResolution(gpu.maxResolution())
- term.clear()
- end
- -- Window Functions
- local function RunDroneList()
- local function UpdateLists()
- local function update(list)
- local drones = {}
- for _, addr in pairs(list) do
- local drone = DroneRegistry.getDroneInfo(addr)
- drone.address = addr
- table.insert(drones, drone)
- end
- return drones
- end
- local registeredList = DroneRegistry.getRegisteredDrones()
- local unregisteredList = DroneRegistry.getUnregisteredDrones()
- registeredDrones = update(registeredList)
- unregisteredDrones = update(unregisteredList)
- Terminal.hints = utils.concatTables(registeredList, unregisteredList, Terminal.cmdHints)
- end
- local function Draw()
- DroneList:as_window(term.clear)
- local line = 1
- local function DrawList(list)
- for _, v in ipairs(list) do
- if line == DroneList.highlighted then
- gpu.setBackground(0x666666)
- end
- DrawLine(DroneList, line, v.name or "unknown", v.address, "@")
- if line == DroneList.highlighted then
- gpu.setBackground(0x000000)
- end
- line = line + 1
- end
- end
- DrawList(registeredDrones)
- DroneList:as_window(term.setCursor, 2, line)
- DroneList:as_window(term.write, string.rep("-", DroneList.width - 2))
- line = line + 1
- DrawList(unregisteredDrones)
- end
- local function Run()
- while running do
- UpdateLists()
- Draw()
- os.sleep(5)
- end
- end
- local sucess, result = pcall(Run)
- if running then Shutdown() end
- if not sucess then error(result) end
- end
- local function RunDroneDetail()
- local function Draw()
- DroneDetail:as_window(term.clear)
- local d = registeredDrones[DroneList.highlighted]
- if d then
- local function Bar(value, max)
- local p = (value/max) * 10
- return string.rep("|", p) .. string.rep("-", 10 - p)
- end
- DrawLine(DroneDetail, 2, d.name or "unknown", d.address, "@")
- DrawLine(DroneDetail, 4, "Energy:", tostring(d.energy) .. "/" .. tostring(d.maxEnergy), "@", Bar(d.energy, d.maxEnergy))
- DrawLine(DroneDetail, 6, "Memory:", tostring(d.maxMemory - d.usedMemory) .. "/" .. tostring(d.maxMemory), "@", Bar(d.maxMemory - d.usedMemory, d.maxMemory))
- if d.pos then
- DrawLine(DroneDetail, 8, "Current Pos:", "@", tostring(d.pos.x), tostring(d.pos.y), tostring(d.pos.z))
- end
- end
- end
- local function Run()
- while running do
- local _, _, x, y, b = event.pull(1, "touch")
- if x and y and b then
- log(utils.strCombine(x, y, b), utils.debug)
- if x >= DroneList.dx + 1 and x <= DroneList.dx + DroneList.width and y >= DroneList.dy + 1 and y <= DroneList.dy + DroneList.height then
- if b == 0 then
- DroneList.highlighted = y - (DroneList.dx + 1)
- Draw()
- end
- end
- end
- end
- end
- local sucess, result = pcall(Run)
- if running then Shutdown() end
- if not sucess then error(result) end
- end
- local function RunTerminal()
- Terminal.console:as_window(term.clear)
- local function WriteToTerminal(tx)
- return Terminal.console:as_window(term.write, tx)
- end
- local Terminal.Id = DroneRegistry.log.addHandler(WriteToTerminal, utils.info)
- local function RunRegistryCmd(cmd, ...)
- local sucess, result = pcall(DroneRegistry[cmd], ...)
- if not sucess then
- WriteToTerminal(result .. "\n")
- end
- end
- local commands = {}
- commands.exit = function() running = false end
- commands.register = function(...) RunRegistryCmd("registerDrone", ...) end
- commands.unregister = function(...) RunRegistryCmd("unregisterDrone", ...) end
- Terminal.cmdHints = {}
- for k, v in pairs(commands) do table.insert(Terminal.cmdHints, k) end
- log(utils.strCombine(table.unpack(Terminal.cmdHints)), utils.debug)
- local history = {}
- history.hint = utils.createHintFunc(Terminal.hints)
- history.dobreak = false
- local function Run()
- while running do
- log(utils.strCombine(table.unpack(Terminal.hints)), utils.debug)
- Terminal.input:as_window(term.clear)
- Terminal.input:as_window(term.write, "> ")
- local input = text.trim(Terminal.input:as_window(term.read, history))
- local cmd = {}
- for w in string.gmatch(input, "%g+") do
- table.insert(cmd, w)
- end
- if commands[cmd[1]] then
- WriteToTerminal(utils.strCombine(table.unpack(cmd), "\n"))
- commands[cmd[1]](table.unpack(cmd, 2))
- else
- WriteToTerminal("Invalid command\n")
- end
- end
- end
- local sucess, result = pcall(Run)
- DroneRegistry.log.removeHandler(terminalId)
- if running then Shutdown() end
- if not sucess then error(result) end
- end
- -- Start Running code
- if not DroneRegistry then
- error("Drone Registry service must be running")
- end
- SetupViews()
- threads[1] = thread.create(RunDroneList)
- threads[2] = thread.create(RunDroneDetail)
- threads[3] = thread.create(RunTerminal)
- thread.waitForAll(threads)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement