Oeed

Server

Apr 14th, 2014
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. Display
  3. ]]--
  4.  
  5. os.loadAPI('StatusController')
  6. os.loadAPI('Peripheral')
  7. os.loadAPI('Modem')
  8.  
  9. _G.Channels = {
  10.     StatusRequest = 4290,
  11.     StatusResponse = 4291,
  12.     MachineRequest = 4292,
  13.     MachineResponse = 4293,
  14.     MachineDiscover = 4294,
  15.     MachineDiscoverResponse = 4295,
  16.     CommandRequest = 4296,
  17.     CommandResponse = 4297,
  18.     MachineOnToggle = 4298,
  19.     Reboot = 4299
  20. }
  21.  
  22. Types = {
  23.     ['Energy Cell'] = {
  24.         Type = 'ProgressBar',
  25.         Max = 'Maximum',
  26.         Current = 'Current',
  27.         ProgressBarColour = colours.red
  28.     },
  29.     ['Energy Infuser'] = { 
  30.         Type = 'ProgressBar',
  31.         Max = 'Maximum',
  32.         Current = 'Current',
  33.         ProgressBarColour = colours.red
  34.     },
  35.     ['Furnace'] = {
  36.         Type = 'ProgressBar',
  37.         Max = 'Maximum',
  38.         Current = 'Current',
  39.         ProgressBarColour = colours.red
  40.     },
  41.     ['Pulveriser'] = { 
  42.         Type = 'ProgressBar',
  43.         Max = 'Maximum',
  44.         Current = 'Current',
  45.         ProgressBarColour = colours.red
  46.     },
  47. }
  48.  
  49. Current = {}
  50. Machines = {}
  51. Sections = {}
  52.  
  53. function GetMachine(name)
  54.     if Machines[name] then
  55.         return Machines[name]
  56.     end
  57. end
  58.  
  59. function _G.GetName(id)
  60.     for k, v in pairs(Machines) do
  61.         if v == id then
  62.             return k
  63.         end
  64.     end
  65. end
  66.  
  67. function GetState(machineName, name)
  68.     return Current.StatusController:GetMachineState(GetMachine(machineName), name)
  69. end
  70.  
  71. function log(message)
  72.     if message then
  73.         print('['..textutils.formatTime(os.time())..'] '..tostring(message))
  74.     end
  75. end
  76.  
  77. function DrawProgressBar(i, x, y, current, max, colour)
  78.     Drawing.DrawBlankArea(x + 2, 3, cellWidth - 5, 3, colours.grey)
  79.     if current and max and colour then
  80.         if current ~= 0 and max ~= 0 then
  81.             Drawing.DrawBlankArea(x + 2, 3, (cellWidth - 5)*(current/max), 3, colour)
  82.         end
  83.         Drawing.DrawCharacters(x + 1 + (cellWidth - 5)/2, 4, tostring(math.ceil((current/max)*100))..'%', colours.white, colours.transparent)
  84.     end
  85. end
  86.  
  87. function DrawSection(i, x, y)
  88.     if Sections[i] then
  89.         log('S: '..i..' X: '..x..' Y: '..y)
  90.         local name = Sections[i].Machine
  91.         local bgCol = colours.red
  92.         if GetState(name, 'On') then
  93.             bgCol = colours.green
  94.         end
  95.         Drawing.DrawBlankArea(x, y, cellWidth - 1, 1, bgCol)
  96.         Drawing.DrawCharacters(x, y, Sections[i].Machine, colours.white, bgCol)
  97.  
  98.         if not GetMachine(Sections[i].Machine) then
  99.             Drawing.DrawCharacters(x + (cellWidth - #"Not Responding")/2, y + (cellHeight/2) - 2, "Not Responding", colours.red, colours.white)
  100.         elseif Sections[i].Type == 'ProgressBar' then
  101.             DrawProgressBar(i, x, y, GetState(name, Sections[i].Current), GetState(name, Sections[i].Max), Sections[i].ProgressBarColour)
  102.         else
  103.             Drawing.DrawCharacters(x + (cellWidth - #"Unknown Type")/2, y + (cellHeight/2) - 2, "Unknown Type", colours.red, colours.white)
  104.             Drawing.DrawCharacters(x + (cellWidth - #Sections[i].Type)/2, y + (cellHeight/2) - 1, Sections[i].Type, colours.red, colours.white)
  105.         end
  106.     end
  107. end
  108.  
  109. function Draw()
  110.     Drawing.Clear(colours.white)
  111.     for i = 1, rows - 1 do
  112.         Drawing.DrawArea(1, (Drawing.Screen.Height / rows)*i, Drawing.Screen.Width, 1, ' ', colours.lightGrey, colours.lightGrey)
  113.     end
  114.     for i = 1, (Drawing.Screen.Width / cols) - 1 do
  115.         Drawing.DrawArea(i * (Drawing.Screen.Width / cols), 1, 1, Drawing.Screen.Height, ' ', colours.lightGrey, colours.lightGrey)
  116.     end
  117.  
  118.     for x = 1, cols do
  119.         for y = 1, rows do
  120.             local section = x + (rows * (y - 1))
  121.             if Sections[section] then
  122.                 DrawSection(section, ((x-1) * cellWidth) + 1, ((y-1) * cellHeight) + 1)
  123.             end
  124.         end
  125.     end
  126.  
  127.     Drawing.DrawBuffer()
  128. end
  129.  
  130. local closedSplash = false
  131. function Splash()
  132.     Monitor.setTextScale(4)
  133.     Monitor.setTextColour(colours.white)
  134.     Monitor.setBackgroundColour(colours.grey)
  135.     Monitor.clear()
  136.     local w, h = Monitor.getSize()
  137.     Monitor.setCursorPos((w-#'Loading')/2, math.ceil(h/2))
  138.     Monitor.write('Loading')
  139. end
  140.  
  141. local lastTouch = 0
  142. function Initialise()
  143.     log('Starting up...')
  144.     log('Initialising Status Controller...')
  145.     Current.StatusController = StatusController:Initialise()
  146.     Current.StatusController.log = log
  147.     Modem.Open(Channels.StatusRequest)
  148.     Modem.Open(Channels.StatusResponse)
  149.     Modem.Open(Channels.Reboot)
  150.     Monitor = Peripheral.WrapType('monitor')
  151.     _G.Monitor = Monitor
  152.     --Monitor.setTextScale(0.5)
  153.     os.loadAPI('Drawing')
  154.     rows = 4
  155.     cols = 4
  156.     cellWidth = math.ceil(Drawing.Screen.Width / cols)
  157.     cellHeight = math.ceil(Drawing.Screen.Height / rows)
  158.     os.startTimer(2)
  159.     Splash()
  160.     Current.StatusController:RefreshStates()
  161.  
  162.     log('System online!')
  163.     log('Waiting for modem message...')
  164.     while true do
  165.         local event, side, channel, reply, message, distance = os.pullEvent()
  166.         if event == 'modem_message' and channel == Channels.StatusResponse then
  167.             if not closedSplash then
  168.                 closedSplash = true
  169.                 Monitor.setTextScale(1)
  170.             end
  171.             Current.StatusController.States = message.States
  172.             Machines = message.Machines
  173.             Sections = {}
  174.             for id, _type in pairs(message.Types) do
  175.                 local t = {Type = _type}
  176.                 if Types[_type] then
  177.                     for k, v in pairs(Types[_type]) do
  178.                         t[k] = v
  179.                     end
  180.                 end
  181.                 t.Machine = GetName(id)
  182.                 table.insert(Sections, t)
  183.             end
  184.             Current.StatusController.LastCheck = os.clock()
  185.             log('Refresh complete!')
  186.             Draw()
  187.         elseif event == 'modem_message' and channel == Channels.Reboot then
  188.             os.reboot()
  189.         elseif event == 'timer' then
  190.             os.startTimer(5)
  191.             Current.StatusController:RefreshStates()
  192.         elseif event == 'monitor_touch' and lastTouch + 1 < os.clock() then
  193.             local x, y = channel, reply
  194.             local section = math.ceil(cols * (x/Drawing.Screen.Width) + (cols * (math.ceil(rows * (y/Drawing.Screen.Height)) - 1)))
  195.             if Sections[section] then
  196.                 log('Toggling section '..section)
  197.                 log()
  198.                 Modem.Send(Channels.MachineOnToggle, Channels.MachineOnToggle, {GetMachine(Sections[section].Machine), not GetState(Sections[section].Machine, 'On')})
  199.                 lastTouch = os.clock()
  200.                 Current.StatusController:RefreshStates()
  201.             else
  202.                 Draw()
  203.             end
  204.         else
  205.             Draw()
  206.         end
  207.     end
  208. end
  209.  
  210. Initialise()
RAW Paste Data