Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Name: MinerMonitor
- Version: 1.4
- Description: Receives information from mining turtles and displays it on an attached monitor.
- Date: 02/28/2025
- Author: MrJohnDowe
- ]]--
- computerID = os.getComputerID()
- -- Active Turtles
- activeMiners = {}
- --[[Section: Helpers]]--
- modemLocation = nil
- consoleName = nil
- consoleID = 0
- monitorID = 0
- dSide = nil
- function getDeviceSide(device)
- local dSide = nil
- term.clear()
- term.setCursorPos(1,1)
- local dName = device
- if dName == "monitor" then
- dName = "monitor"
- elseif dName == "modem" then
- dname = "modem"
- else
- error("No Device Named: " .. device)
- end
- -- Determine where device is.
- print("Looking for device,")
- if peripheral.isPresent("left") and peripheral.getType("left")==dName then
- dSide = "left"
- elseif peripheral.isPresent("right") and peripheral.getType("right")==dName then
- dSide = "right"
- elseif peripheral.isPresent("top") and peripheral.getType("top")==dName then
- dSide = "top"
- elseif peripheral.isPresent("back") and peripheral.getType("back")==dName then
- dSide = "back"
- elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")==dName then
- dSide = "bottom"
- else
- print("No " .. device )
- -- return dSide
- end
- term.write(dName .. " found on ".. dSide)
- if dName == "modem" then
- -- term.clear()
- -- term.setCursorPos(1,1)
- rednet.open(dSide)
- -- print(dName .. " found on "..dSide)
- elseif dName == "monitor" then
- if dSide ~= nil then
- dSide = peripheral.wrap(dSide)
- dSide.setTextScale(.7)
- else
- dSide = nil
- end
- -- print(dName .. " found on ".. dSide)
- end
- -- print(dName .. " found on ".. dSide)
- return dSide
- end
- if not rednet.isOpen(getDeviceSide("modem")) then rednet.open(getDeviceSide("modem")) 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
- --- COLORED SECTION
- function printLeftColored(dev, msg, height, bgColor, txtColor)
- dev.setBackgroundColor(bgColor)
- dev.setTextColor(txtColor)
- dev.setCursorPos(1, height)
- dev.clearLine()
- dev.write(msg)
- dev.setBackgroundColor(colors.black)
- dev.setTextColor(colors.white)
- end
- function printIndentedColored(dev, msg, indent, height, bgColor, txtColor)
- dev.setBackgroundColor(bgColor)
- dev.setTextColor(txtColor)
- dev.setCursorPos(indent, height)
- dev.clearLine()
- dev.write(msg)
- dev.setBackgroundColor(colors.black)
- dev.setTextColor(colors.white)
- end
- function printCenteredColored(dev, msg, height, bgColor, txtColor)
- w,h = dev.getSize()
- dev.setBackgroundColor(bgColor)
- dev.setTextColor(txtColor)
- dev.setCursorPos(w/2 - #msg/2, tonumber(height))
- dev.clearLine()
- dev.write(msg)
- dev.setBackgroundColor(colors.black)
- dev.setTextColor(colors.white)
- end
- function printRightColored(dev, msg, height, bgColor, txtColor)
- w,h = dev.getSize()
- dev.setBackgroundColor(bgColor)
- dev.setTextColor(txtColor)
- dev.setCursorPos(w-#msg)
- dev.clearLine()
- dev.write(msg)
- dev.setBackgroundColor(colors.black)
- dev.setTextColor(colors.white)
- 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 getDeviceSide("monitor") ~= nil then
- printMonitor()
- end
- end
- function printMonitor()
- getDeviceSide("monitor").clear()
- w,h = getDeviceSide("monitor").getSize()
- w,h = term.getSize()
- printCenteredColored(getDeviceSide("monitor"), "ID: "..computerID.." Miner Monitor System", 1, colors.lime, colors.gray)
- printCenteredColored(getDeviceSide("monitor"), string.rep("-", w), 2, colors.lime, colors.gray)
- row = 3
- for key,value in pairs( activeMiners ) do
- printIndented(getDeviceSide("monitor"), value, 1, row)
- row = row + 1
- end
- printCenteredColored(getDeviceSide("monitor"), string.rep("-", w), h-2, colors.lime, colors.gray)
- printIndentedColored(getDeviceSide("monitor"), "Last update: "..updateTime, 3, h-1, colors.lime, colors.gray)
- 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