Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Display
- ]]--
- os.loadAPI('StatusController')
- os.loadAPI('Peripheral')
- os.loadAPI('Modem')
- _G.Channels = {
- StatusRequest = 4290,
- StatusResponse = 4291,
- MachineRequest = 4292,
- MachineResponse = 4293,
- MachineDiscover = 4294,
- MachineDiscoverResponse = 4295,
- CommandRequest = 4296,
- CommandResponse = 4297,
- MachineOnToggle = 4298,
- Reboot = 4299
- }
- Types = {
- ['Energy Cell'] = {
- Type = 'ProgressBar',
- Max = 'Maximum',
- Current = 'Current',
- ProgressBarColour = colours.red
- },
- ['Energy Infuser'] = {
- Type = 'ProgressBar',
- Max = 'Maximum',
- Current = 'Current',
- ProgressBarColour = colours.red
- },
- ['Furnace'] = {
- Type = 'ProgressBar',
- Max = 'Maximum',
- Current = 'Current',
- ProgressBarColour = colours.red
- },
- ['Pulveriser'] = {
- Type = 'ProgressBar',
- Max = 'Maximum',
- Current = 'Current',
- ProgressBarColour = colours.red
- },
- }
- Current = {}
- Machines = {}
- Sections = {}
- function GetMachine(name)
- if Machines[name] then
- return Machines[name]
- end
- end
- function _G.GetName(id)
- for k, v in pairs(Machines) do
- if v == id then
- return k
- end
- end
- end
- function GetState(machineName, name)
- return Current.StatusController:GetMachineState(GetMachine(machineName), name)
- end
- function log(message)
- if message then
- print('['..textutils.formatTime(os.time())..'] '..tostring(message))
- end
- end
- function DrawProgressBar(i, x, y, current, max, colour)
- Drawing.DrawBlankArea(x + 2, 3, cellWidth - 5, 3, colours.grey)
- if current and max and colour then
- if current ~= 0 and max ~= 0 then
- Drawing.DrawBlankArea(x + 2, 3, (cellWidth - 5)*(current/max), 3, colour)
- end
- Drawing.DrawCharacters(x + 1 + (cellWidth - 5)/2, 4, tostring(math.ceil((current/max)*100))..'%', colours.white, colours.transparent)
- end
- end
- function DrawSection(i, x, y)
- if Sections[i] then
- log('S: '..i..' X: '..x..' Y: '..y)
- local name = Sections[i].Machine
- local bgCol = colours.red
- if GetState(name, 'On') then
- bgCol = colours.green
- end
- Drawing.DrawBlankArea(x, y, cellWidth - 1, 1, bgCol)
- Drawing.DrawCharacters(x, y, Sections[i].Machine, colours.white, bgCol)
- if not GetMachine(Sections[i].Machine) then
- Drawing.DrawCharacters(x + (cellWidth - #"Not Responding")/2, y + (cellHeight/2) - 2, "Not Responding", colours.red, colours.white)
- elseif Sections[i].Type == 'ProgressBar' then
- DrawProgressBar(i, x, y, GetState(name, Sections[i].Current), GetState(name, Sections[i].Max), Sections[i].ProgressBarColour)
- else
- Drawing.DrawCharacters(x + (cellWidth - #"Unknown Type")/2, y + (cellHeight/2) - 2, "Unknown Type", colours.red, colours.white)
- Drawing.DrawCharacters(x + (cellWidth - #Sections[i].Type)/2, y + (cellHeight/2) - 1, Sections[i].Type, colours.red, colours.white)
- end
- end
- end
- function Draw()
- Drawing.Clear(colours.white)
- for i = 1, rows - 1 do
- Drawing.DrawArea(1, (Drawing.Screen.Height / rows)*i, Drawing.Screen.Width, 1, ' ', colours.lightGrey, colours.lightGrey)
- end
- for i = 1, (Drawing.Screen.Width / cols) - 1 do
- Drawing.DrawArea(i * (Drawing.Screen.Width / cols), 1, 1, Drawing.Screen.Height, ' ', colours.lightGrey, colours.lightGrey)
- end
- for x = 1, cols do
- for y = 1, rows do
- local section = x + (rows * (y - 1))
- if Sections[section] then
- DrawSection(section, ((x-1) * cellWidth) + 1, ((y-1) * cellHeight) + 1)
- end
- end
- end
- Drawing.DrawBuffer()
- end
- local closedSplash = false
- function Splash()
- Monitor.setTextScale(4)
- Monitor.setTextColour(colours.white)
- Monitor.setBackgroundColour(colours.grey)
- Monitor.clear()
- local w, h = Monitor.getSize()
- Monitor.setCursorPos((w-#'Loading')/2, math.ceil(h/2))
- Monitor.write('Loading')
- end
- local lastTouch = 0
- function Initialise()
- log('Starting up...')
- log('Initialising Status Controller...')
- Current.StatusController = StatusController:Initialise()
- Current.StatusController.log = log
- Modem.Open(Channels.StatusRequest)
- Modem.Open(Channels.StatusResponse)
- Modem.Open(Channels.Reboot)
- Monitor = Peripheral.WrapType('monitor')
- _G.Monitor = Monitor
- --Monitor.setTextScale(0.5)
- os.loadAPI('Drawing')
- rows = 4
- cols = 4
- cellWidth = math.ceil(Drawing.Screen.Width / cols)
- cellHeight = math.ceil(Drawing.Screen.Height / rows)
- os.startTimer(2)
- Splash()
- Current.StatusController:RefreshStates()
- log('System online!')
- log('Waiting for modem message...')
- while true do
- local event, side, channel, reply, message, distance = os.pullEvent()
- if event == 'modem_message' and channel == Channels.StatusResponse then
- if not closedSplash then
- closedSplash = true
- Monitor.setTextScale(1)
- end
- Current.StatusController.States = message.States
- Machines = message.Machines
- Sections = {}
- for id, _type in pairs(message.Types) do
- local t = {Type = _type}
- if Types[_type] then
- for k, v in pairs(Types[_type]) do
- t[k] = v
- end
- end
- t.Machine = GetName(id)
- table.insert(Sections, t)
- end
- Current.StatusController.LastCheck = os.clock()
- log('Refresh complete!')
- Draw()
- elseif event == 'modem_message' and channel == Channels.Reboot then
- os.reboot()
- elseif event == 'timer' then
- os.startTimer(5)
- Current.StatusController:RefreshStates()
- elseif event == 'monitor_touch' and lastTouch + 1 < os.clock() then
- local x, y = channel, reply
- local section = math.ceil(cols * (x/Drawing.Screen.Width) + (cols * (math.ceil(rows * (y/Drawing.Screen.Height)) - 1)))
- if Sections[section] then
- log('Toggling section '..section)
- log()
- Modem.Send(Channels.MachineOnToggle, Channels.MachineOnToggle, {GetMachine(Sections[section].Machine), not GetState(Sections[section].Machine, 'On')})
- lastTouch = os.clock()
- Current.StatusController:RefreshStates()
- else
- Draw()
- end
- else
- Draw()
- end
- end
- end
- Initialise()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement