dadragon84

Minor Monitor

Nov 13th, 2013 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | None | 0 0
  1.  
  2. --[[
  3. Name: MinerMonitor
  4. Version: 1.4
  5. Description: Receives information from mining turtles and displays it on an attached monitor.
  6.  
  7. Date: 02/28/2025
  8. Author: MrJohnDowe
  9.  
  10. ]]--
  11.  
  12.  
  13. computerID = os.getComputerID()
  14. -- Active Turtles
  15. activeMiners = {}
  16. --[[Section: Helpers]]--
  17. modemLocation = nil
  18. consoleName = nil
  19. consoleID = 0
  20. monitorID = 0
  21. dSide = nil
  22.  
  23. function getDeviceSide(device)
  24.     local dSide = nil
  25.    
  26.     term.clear()
  27.     term.setCursorPos(1,1)
  28.    
  29.     local dName = device
  30.     if dName == "monitor" then
  31.       dName = "monitor"
  32.     elseif dName == "modem" then
  33.       dname = "modem"
  34.     else
  35.       error("No Device Named: " .. device)
  36.     end
  37.  
  38.     -- Determine where device is.
  39.    
  40.  
  41.     print("Looking for device,")
  42.     if peripheral.isPresent("left") and peripheral.getType("left")==dName then
  43.         dSide = "left"
  44.     elseif peripheral.isPresent("right") and peripheral.getType("right")==dName then
  45.         dSide = "right"
  46.     elseif peripheral.isPresent("top") and peripheral.getType("top")==dName then
  47.         dSide = "top"
  48.     elseif peripheral.isPresent("back") and peripheral.getType("back")==dName then
  49.         dSide = "back"
  50.     elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")==dName then
  51.         dSide = "bottom"
  52.     else
  53.         print("No " .. device )
  54.         -- return dSide
  55.          
  56.     end
  57.    
  58.     term.write(dName .. " found on ".. dSide)
  59.  
  60.     if dName == "modem" then
  61.         -- term.clear()
  62.         -- term.setCursorPos(1,1)
  63.         rednet.open(dSide)
  64.         -- print(dName .. " found on "..dSide)
  65.     elseif dName == "monitor" then
  66.         if dSide ~= nil then
  67.             dSide = peripheral.wrap(dSide)
  68.             dSide.setTextScale(.7)
  69.         else
  70.             dSide = nil
  71.         end
  72.        
  73.         -- print(dName .. " found on ".. dSide)
  74.     end
  75.    
  76.     -- print(dName .. " found on ".. dSide)
  77.     return dSide
  78.        
  79. end
  80.  
  81. if not rednet.isOpen(getDeviceSide("modem")) then rednet.open(getDeviceSide("modem")) end
  82.  
  83. function printLeft(dev, msg, height)
  84.   dev.setCursorPos(1, height)
  85.   dev.clearLine()
  86.   dev.write(msg)
  87. end
  88.  
  89. function printIndented(dev, msg, indent, height)
  90.   dev.setCursorPos(indent, height)
  91.   dev.clearLine()
  92.   dev.write(msg)
  93. end
  94.  
  95. function printCentered(dev, msg, height)
  96.   w,h = dev.getSize()
  97.   dev.setCursorPos(w/2 - #msg/2, tonumber(height))
  98.   dev.clearLine()
  99.   dev.write(msg)
  100. end
  101.  
  102. function printRight(dev, msg, height)
  103.   w,h = dev.getSize()
  104.   dev.setCursorPos(w-#msg)
  105.   dev.clearLine()
  106.   dev.write(msg)
  107. end
  108.  
  109. --- COLORED SECTION
  110.  
  111. function printLeftColored(dev, msg, height, bgColor, txtColor)
  112.     dev.setBackgroundColor(bgColor)
  113.     dev.setTextColor(txtColor)
  114.     dev.setCursorPos(1, height)
  115.     dev.clearLine()
  116.     dev.write(msg)
  117.     dev.setBackgroundColor(colors.black)
  118.     dev.setTextColor(colors.white)
  119. end
  120.  
  121. function printIndentedColored(dev, msg, indent, height, bgColor, txtColor)
  122.     dev.setBackgroundColor(bgColor)
  123.     dev.setTextColor(txtColor)
  124.     dev.setCursorPos(indent, height)
  125.     dev.clearLine()
  126.     dev.write(msg)
  127.     dev.setBackgroundColor(colors.black)
  128.     dev.setTextColor(colors.white) 
  129. end
  130.  
  131. function printCenteredColored(dev, msg, height, bgColor, txtColor)
  132.     w,h = dev.getSize()
  133.     dev.setBackgroundColor(bgColor)
  134.     dev.setTextColor(txtColor)
  135.     dev.setCursorPos(w/2 - #msg/2, tonumber(height))
  136.     dev.clearLine()
  137.     dev.write(msg)
  138.     dev.setBackgroundColor(colors.black)
  139.     dev.setTextColor(colors.white)
  140. end
  141.  
  142. function printRightColored(dev, msg, height, bgColor, txtColor)
  143.     w,h = dev.getSize()
  144.     dev.setBackgroundColor(bgColor)
  145.     dev.setTextColor(txtColor)
  146.     dev.setCursorPos(w-#msg)
  147.     dev.clearLine()
  148.     dev.write(msg)
  149.     dev.setBackgroundColor(colors.black)
  150.     dev.setTextColor(colors.white)
  151. end
  152.  
  153. function wprintOffCenter(dev, msg, height, width, offset)
  154.     local inc = 0
  155.     local ops = 1
  156.     while #msg - ops > width do
  157.         local nextspace = 0
  158.         while string.find(msg, " ", ops + nextspace) and
  159.                 string.find(msg, " ", ops + nextspace) - ops < width do
  160.             nextspace = string.find(msg, " ", nextspace + ops) + 1 - ops
  161.         end
  162.         dev.setCursorPos(width/2 - (nextspace)/2 + offset, height + inc)
  163.         inc = inc + 1
  164.         dev.write(string.sub(msg, ops, nextspace + ops - 1))
  165.         ops = ops + nextspace
  166.     end
  167.     dev.write(string.sub(msg, ops))
  168.    
  169.     return inc + 1
  170. end
  171.  
  172. --[[Section: Menu State Functions]]--
  173.  
  174. function printHeader()
  175.   term.clear()
  176.   w,h = term.getSize()
  177.     printCentered(term, "Miner Monitor System  Computer ID:"..computerID, 1)
  178.     printCentered(term, string.rep("-", w), 2)
  179.   row = 3
  180.   for key,value in pairs( activeMiners ) do
  181.     printIndented(term, value, 1, row)
  182.     row = row + 1
  183.   end
  184.     printCentered(term, string.rep("-", w), h-2)
  185.     printIndented(term, "Last update: "..updateTime, 3, h-1)
  186.   if getDeviceSide("monitor") ~= nil then
  187.     printMonitor()
  188.   end
  189. end
  190.  
  191. function printMonitor()
  192.   getDeviceSide("monitor").clear()
  193.   w,h = getDeviceSide("monitor").getSize()
  194.  
  195.   w,h = term.getSize()
  196.     printCenteredColored(getDeviceSide("monitor"), "ID: "..computerID.."  Miner Monitor System", 1, colors.lime, colors.gray)
  197.     printCenteredColored(getDeviceSide("monitor"), string.rep("-", w), 2, colors.lime, colors.gray)
  198.    
  199.   row = 3
  200.   for key,value in pairs( activeMiners ) do
  201.     printIndented(getDeviceSide("monitor"), value, 1, row)
  202.     row = row + 1
  203.   end
  204.     printCenteredColored(getDeviceSide("monitor"), string.rep("-", w), h-2, colors.lime, colors.gray)
  205.     printIndentedColored(getDeviceSide("monitor"), "Last update: "..updateTime, 3, h-1, colors.lime, colors.gray)
  206.    
  207. end
  208.  
  209. function getCurrentMessages()
  210.   updateTime = textutils.formatTime(os.time())
  211.   printHeader()
  212.     while true do
  213.         local id, msg = rednet.receive()
  214.        
  215.     if id ~= nil then
  216.       if string.sub(msg,1,3) == "MM:" then
  217.         local minerIdx = string.find(msg,":")
  218.         if minerIdx ~= nil then
  219.           updateTime = textutils.formatTime(os.time())
  220.           local minerName = string.sub(msg,4,minerIdx-1)
  221.           local minerMsg = string.sub(msg,minerIdx+1)
  222.           activeMiners[id] = minerMsg
  223.         end
  224.         printHeader(term)
  225.        
  226.       end
  227.     end
  228.    
  229.     return true
  230.    
  231.     end
  232. end
  233.  
  234.  
  235.  
  236.  
  237. while true do
  238.     getCurrentMessages()
  239.    
  240. end
Advertisement
Add Comment
Please, Sign In to add comment