Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Name: MinerMonitor
- Version: 1.3
- Description: Receives information from mining turtles and displays it on an attached monitor.
- Date: 04/27/2023
- Author: MrJohnDowe
- ]]--
- computerID = os.getComputerID()
- -- Active Turtles
- activeMiners = {}
- --[[Section: Helpers]]--
- modemLocation = nil
- consoleName = nil
- consoleID = 0
- monitorID = 0
- -- Determine where the modem is.
- monitorSide = nil
- print("Looking for modem,")
- if peripheral.isPresent("left") and peripheral.getType("left")=="modem" then
- modemLocation = "left"
- elseif peripheral.isPresent("right") and peripheral.getType("right")=="modem" then
- modemLocation = "right"
- elseif peripheral.isPresent("top") and peripheral.getType("top")=="modem" then
- modemLocation = "top"
- elseif peripheral.isPresent("back") and peripheral.getType("back")=="modem" then
- modemLocation = "back"
- elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")=="modem" then
- modemLocation = "bottom"
- else
- print("No modem")
- return
- end
- print("Modem found on "..modemLocation)
- -- Determine where monitor is.
- monitorSide = nil
- if peripheral.isPresent("left") and peripheral.getType("left")=="monitor" then
- monitorSide = "left"
- elseif peripheral.isPresent("right") and peripheral.getType("right")=="monitor" then
- monitorSide = "right"
- elseif peripheral.isPresent("top") and peripheral.getType("top")=="monitor" then
- monitorSide = "top"
- elseif peripheral.isPresent("back") and peripheral.getType("back")=="monitor" then
- monitorSide = "back"
- elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")=="monitor" then
- monitorSide = "bottom"
- end
- if monitorSide ~= nil then
- monitor = peripheral.wrap(monitorSide)
- print("Monitor found on "..monitorSide)
- monitor.setTextScale(.7)
- else
- monitor = nil
- end
- function printLeft(dev, msg, height)
- dev.setCursorPos(1, height)
- dev.clearLine()
- dev.write(msg)
- end
- function printIndented(dev, msg, indent, height)
- dev.setCursorPos(indent, height)
- dev.clearLine()
- dev.write(msg)
- end
- function printCentered(dev, msg, height)
- w,h = dev.getSize()
- dev.setCursorPos(w/2 - #msg/2, tonumber(height))
- dev.clearLine()
- dev.write(msg)
- end
- function printRight(dev, msg, height)
- w,h = dev.getSize()
- dev.setCursorPos(w-#msg)
- dev.clearLine()
- dev.write(msg)
- end
- function wprintOffCenter(dev, msg, height, width, offset)
- local inc = 0
- local ops = 1
- while #msg - ops > width do
- local nextspace = 0
- while string.find(msg, " ", ops + nextspace) and
- string.find(msg, " ", ops + nextspace) - ops < width do
- nextspace = string.find(msg, " ", nextspace + ops) + 1 - ops
- end
- dev.setCursorPos(width/2 - (nextspace)/2 + offset, height + inc)
- inc = inc + 1
- dev.write(string.sub(msg, ops, nextspace + ops - 1))
- ops = ops + nextspace
- end
- dev.write(string.sub(msg, ops))
- return inc + 1
- end
- --[[Section: Menu State Functions]]--
- function printHeader()
- term.clear()
- w,h = term.getSize()
- printCentered(term, "Miner Monitor System Computer ID:"..computerID, 1)
- printCentered(term, string.rep("-", w), 2)
- row = 3
- for key,value in pairs( activeMiners ) do
- printIndented(term, value, 1, row)
- row = row + 1
- end
- printCentered(term, string.rep("-", w), h-2)
- printIndented(term, "Last update: "..updateTime, 3, h-1)
- if monitor ~= nil then
- printMonitor()
- end
- end
- function printMonitor()
- monitor.clear()
- w,h = monitor.getSize()
- w,h = term.getSize()
- printCentered(monitor, "ID: "..computerID.." Miner Monitor System", 1)
- printCentered(monitor, string.rep("-", w), 2)
- row = 3
- for key,value in pairs( activeMiners ) do
- printIndented(monitor, value, 1, row)
- row = row + 1
- end
- printCentered(monitor, string.rep("-", w), h-2)
- printIndented(monitor, "Last update: "..updateTime, 3, h-1)
- end
- function getCurrentMessages()
- updateTime = textutils.formatTime(os.time())
- printHeader()
- while true do
- local id, msg = rednet.receive()
- if id ~= nil then
- if string.sub(msg,1,3) == "MM:" then
- local minerIdx = string.find(msg,":")
- if minerIdx ~= nil then
- updateTime = textutils.formatTime(os.time())
- local minerName = string.sub(msg,4,minerIdx-1)
- local minerMsg = string.sub(msg,minerIdx+1)
- activeMiners[id] = minerMsg
- end
- printHeader(term)
- end
- end
- return true
- end
- end
- while true do
- getCurrentMessages()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement