Advertisement
Guest User

DroneRegistryManagerWin.lua

a guest
Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.14 KB | None | 0 0
  1. local term = require("term")
  2. local component = require("component")
  3. local event = require("event")
  4. local thread = require("thread")
  5. local text = require("text")
  6. local utils = require("psiutils")
  7.  
  8. local gpu = component.gpu
  9.  
  10. local DroneList, DroneDetail, Terminal
  11. local threads = {}
  12. local running = true
  13.  
  14. local registeredDrones = {}
  15. local unregisteredDrones = {}
  16.  
  17. -- Shared Functions
  18.  
  19. local function log(msg, level)
  20.   DroneRegistry.log.log("[GUI] " .. msg, level)
  21. end
  22.  
  23. local function SetupViews()
  24.   gpu.setResolution(80, 25)
  25.   term.clear()
  26.   local right, bottom = gpu.getResolution()
  27.   local left, top  = 1, 1
  28.   local centre = (top - 1) +  math.ceil((bottom - (top - 1))/2)
  29.   local centreTop = (left - 1) + math.floor((right - (left - 1))/2)
  30.   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}
  31.   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}
  32.   Terminal = {}
  33.   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}
  34.   Terminal.input = utils.createWindow(left, bottom - 2, (right - 1) - left, 1)
  35.  
  36.   gpu.setBackground(0x222222)
  37.   gpu.fill(left, top, right, bottom, " ")
  38.   gpu.setBackground(0x000000)
  39.  
  40.   DroneList:as_window(term.clear)
  41.   DroneDetail:as_window(term.clear)
  42.   Terminal.console:as_window(term.clear)
  43.   Terminal.input:as_window(term.clear)
  44. end
  45.  
  46. local function DrawLine(Window, line, ...)
  47.   local args = {...}
  48.   local n = #args
  49.   local coloumnW = math.floor((Window.width/n) + 0.5)
  50.   local coloumns = {}
  51.   for i = 1, n - 1 do
  52.     coloumns[i] = coloumnW
  53.   end
  54.   coloumns[n] = Window.width - (coloumnW * (n - 1))
  55.   local i = 1
  56.   while  i <= n do
  57.     local l = coloumns[i]
  58.     ln = 1
  59.     while args[i + ln] == "@" do
  60.       l = l + coloumns[i + ln]
  61.       ln = ln + 1
  62.     end
  63.     Window:as_window(term.setCursor, ((i - 1) * (coloumnW)) + 2, line)
  64.     Window:as_window(term.write, string.sub(args[i], 1, l - 2))
  65.     i = i + ln
  66.   end
  67. end
  68.  
  69. local function Shutdown()
  70.   running = false
  71.   gpu.setResolution(gpu.maxResolution())
  72.   term.clear()
  73. end
  74.  
  75. -- Window Functions
  76.  
  77. local function RunDroneList()
  78.   local function UpdateLists()
  79.     local function update(list)
  80.       local drones = {}
  81.       for _, addr in pairs(list) do
  82.         local drone = DroneRegistry.getDroneInfo(addr)
  83.         drone.address = addr
  84.         table.insert(drones, drone)
  85.       end
  86.       return drones
  87.     end
  88.     local registeredList = DroneRegistry.getRegisteredDrones()
  89.     local unregisteredList = DroneRegistry.getUnregisteredDrones()
  90.     registeredDrones = update(registeredList)
  91.     unregisteredDrones = update(unregisteredList)
  92.     Terminal.hints = utils.concatTables(registeredList, unregisteredList, Terminal.cmdHints)
  93.   end
  94.  
  95.   local function Draw()
  96.     DroneList:as_window(term.clear)
  97.     local line = 1
  98.    
  99.     local function DrawList(list)
  100.       for _, v in ipairs(list) do
  101.         if line == DroneList.highlighted then
  102.           gpu.setBackground(0x666666)
  103.         end
  104.         DrawLine(DroneList, line, v.name or "unknown", v.address, "@")
  105.         if line == DroneList.highlighted then
  106.           gpu.setBackground(0x000000)
  107.         end
  108.         line = line + 1
  109.       end
  110.     end
  111.  
  112.     DrawList(registeredDrones)
  113.     DroneList:as_window(term.setCursor, 2, line)
  114.     DroneList:as_window(term.write, string.rep("-", DroneList.width - 2))
  115.     line = line + 1
  116.     DrawList(unregisteredDrones)
  117.   end
  118.  
  119.   local function Run()
  120.     while running do
  121.       UpdateLists()
  122.       Draw()
  123.       os.sleep(5)
  124.     end
  125.   end
  126.  
  127.   local sucess, result = pcall(Run)
  128.   if running then Shutdown() end
  129.   if not sucess then error(result) end
  130. end
  131.  
  132. local function RunDroneDetail()
  133.   local function Draw()
  134.     DroneDetail:as_window(term.clear)
  135.     local d = registeredDrones[DroneList.highlighted]
  136.     if d then
  137.       local function Bar(value, max)
  138.         local p = (value/max) * 10
  139.         return string.rep("|", p) .. string.rep("-", 10 - p)
  140.       end
  141.       DrawLine(DroneDetail, 2, d.name or "unknown", d.address, "@")
  142.       DrawLine(DroneDetail, 4, "Energy:", tostring(d.energy) .. "/" .. tostring(d.maxEnergy), "@", Bar(d.energy, d.maxEnergy))
  143.       DrawLine(DroneDetail, 6, "Memory:", tostring(d.maxMemory - d.usedMemory) .. "/" .. tostring(d.maxMemory), "@", Bar(d.maxMemory - d.usedMemory, d.maxMemory))
  144.       if d.pos then
  145.         DrawLine(DroneDetail, 8, "Current Pos:", "@",  tostring(d.pos.x), tostring(d.pos.y), tostring(d.pos.z))
  146.       end
  147.     end
  148.   end
  149.  
  150.   local function Run()
  151.     while running do
  152.       local _, _, x, y, b = event.pull(1, "touch")
  153.       if x and y and b then
  154.         log(utils.strCombine(x, y, b), utils.debug)
  155.         if x >= DroneList.dx + 1 and x <= DroneList.dx + DroneList.width and y >= DroneList.dy + 1 and y <= DroneList.dy + DroneList.height then
  156.           if b == 0 then
  157.             DroneList.highlighted = y - (DroneList.dx + 1)
  158.             Draw()
  159.           end
  160.         end
  161.       end
  162.     end
  163.   end
  164.  
  165.   local sucess, result = pcall(Run)
  166.   if running then Shutdown() end
  167.   if not sucess then error(result) end
  168. end
  169.  
  170. local function RunTerminal()
  171.   Terminal.console:as_window(term.clear)
  172.   local function WriteToTerminal(tx)
  173.     return Terminal.console:as_window(term.write, tx)
  174.   end
  175.   local Terminal.Id = DroneRegistry.log.addHandler(WriteToTerminal, utils.info)
  176.   local function RunRegistryCmd(cmd, ...)
  177.     local sucess, result = pcall(DroneRegistry[cmd], ...)
  178.     if not sucess then
  179.       WriteToTerminal(result .. "\n")
  180.     end
  181.   end
  182.   local commands = {}
  183.   commands.exit = function() running = false end
  184.   commands.register = function(...) RunRegistryCmd("registerDrone", ...) end
  185.   commands.unregister = function(...) RunRegistryCmd("unregisterDrone", ...) end
  186.   Terminal.cmdHints = {}
  187.   for k, v in pairs(commands) do table.insert(Terminal.cmdHints, k) end
  188.   log(utils.strCombine(table.unpack(Terminal.cmdHints)), utils.debug)
  189.   local history = {}
  190.   history.hint = utils.createHintFunc(Terminal.hints)
  191.   history.dobreak = false
  192.  
  193.   local function Run()
  194.     while running do
  195.       log(utils.strCombine(table.unpack(Terminal.hints)), utils.debug)
  196.       Terminal.input:as_window(term.clear)
  197.       Terminal.input:as_window(term.write, "> ")
  198.       local input = text.trim(Terminal.input:as_window(term.read, history))
  199.       local cmd = {}
  200.       for w in string.gmatch(input, "%g+") do
  201.         table.insert(cmd, w)
  202.       end
  203.       if commands[cmd[1]] then
  204.         WriteToTerminal(utils.strCombine(table.unpack(cmd), "\n"))
  205.         commands[cmd[1]](table.unpack(cmd, 2))
  206.       else
  207.         WriteToTerminal("Invalid command\n")
  208.       end
  209.     end
  210.   end
  211.  
  212.   local sucess, result = pcall(Run)
  213.   DroneRegistry.log.removeHandler(terminalId)
  214.   if running then Shutdown() end
  215.   if not sucess then error(result) end
  216. end
  217.  
  218. -- Start Running code
  219.  
  220. if not DroneRegistry then
  221.   error("Drone Registry service must be running")
  222. end
  223.  
  224. SetupViews()
  225.  
  226. threads[1] = thread.create(RunDroneList)
  227. threads[2] = thread.create(RunDroneDetail)
  228. threads[3] = thread.create(RunTerminal)
  229.  
  230. thread.waitForAll(threads)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement