Advertisement
Atomik

[GOLLARK] Status Board

Nov 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. -- monday - a thing for running and monitoring services - servery bit
  2.  
  3. local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/e961b964508c0272eed6ae3aecd537f50803e201/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close()
  4.  
  5. local skynet = require "/skynet"
  6.  
  7. local chan = settings.get "monday.channel" or "monday"
  8. local m = peripheral.find "monitor"
  9.  
  10. local mX, mY = m.getSize()
  11.  
  12. local things = {}
  13. local by_ID = {}
  14.  
  15. local function send_command(computer, command)
  16.     skynet.send(chan, {
  17.         type = "command",
  18.         computer = computer,
  19.         command = command
  20.     })
  21. end
  22.  
  23. local function displayer()
  24.     m.setTextScale(0.5)
  25.     m.setTextColor(colors.black)
  26.     m.setBackgroundColor(colors.black)
  27.     m.clear()
  28.    
  29.     m.setCursorPos(1, 1)
  30.     m.setBackgroundColor(colors.gray)
  31.     m.write((" "):rep(mX))
  32.  
  33.     m.setTextColor(colors.white)
  34.     m.setCursorPos(1, 1)
  35.     m.write("Computer")
  36.    
  37.     m.setCursorPos(mX / 2, 1)
  38.     m.write("Status")
  39.    
  40.     m.setBackgroundColor(colors.black)
  41.     m.setTextColor(colors.black)
  42.    
  43.     while true do
  44.         local _, msg = skynet.receive(chan)
  45.         if type(msg) == "table" and msg.type == "update" then
  46.             things[msg.computer] = {
  47.                 state = msg.state,
  48.                 message = msg.message
  49.             }
  50.         end
  51.  
  52.         local i = 2
  53.         for k, v in pairs(things) do
  54.             m.setCursorPos(1, i)
  55.             if v.state == "running" then
  56.                 m.setBackgroundColor(colors.green)
  57.             elseif v.state == "restarting" then
  58.                 m.setBackgroundColor(colors.yellow)
  59.             else
  60.                 m.setBackgroundColor(colors.red)
  61.             end
  62.             m.clearLine()
  63.             m.write(("%s"):format(k))
  64.             m.setCursorPos(mX / 2, i)
  65.             m.write(("%s"):format(v.message or "Online"))
  66.             by_ID[i] = k
  67.             i = i + 1
  68.         end
  69.     end
  70. end
  71.  
  72. local function touch_handler()
  73.     local event, peripheral_name, x, y = os.pullEvent "monitor_touch"
  74.     local name = by_ID[y]
  75.     if name then
  76.         send_command(name, "restart")
  77.     end
  78. end
  79.  
  80. local function split_at_spaces(s)
  81.     local t = {}
  82.     for i in string.gmatch(s, "%S+") do
  83.        table.insert(t, i)
  84.     end
  85.     return t
  86. end
  87.  
  88. local function command_reader()
  89.     local hist = {}
  90.     while true do
  91.         write "|> "
  92.         local text = read(nil, hist)
  93.         table.insert(hist, text)
  94.         local command = split_at_spaces(text)
  95.         send_command(command[2], command[1])
  96.     end
  97. end
  98.  
  99. parallel.waitForAll(displayer, touch_handler, command_reader)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement