PlowmanPlow

ComputerCraft - TE Tank Monitor

Feb 25th, 2016
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local statusChannelID = 5
  2.  
  3. -- Find Modem
  4. local modem = peripheral.find("modem", function(n, o) return o.isWireless() end)
  5.  
  6. -- Find TE Tank
  7. local tank = peripheral.find("thermalexpansion_tank")
  8. if tank == nil then
  9.    error("Could not bind to TE Tank. Is one attached?")
  10. end
  11.  
  12. term.clear()
  13. term.setCursorPos(1,1)
  14. print("TE Tank Monitor...")
  15. print()
  16. print("Tank Contents: ")
  17. print("Fill Percentage: ")
  18. print()
  19. if ( modem == nil ) then
  20.    print("Modem not present. No status reports.")
  21. else
  22.    print("Modem present. Status reports sent on channel " .. statusChannelID)
  23. end
  24. print()
  25. print("Press 'q' to quit")
  26.  
  27. local status = {}
  28. local pollTimer = os.startTimer(0)
  29. while true do
  30.    local event, p1, p2, p3, p4 = os.pullEvent()
  31.    if event == "key" then
  32.       if p1 == keys.q then
  33.          term.setCursorPos(1,11)
  34.          return
  35.       end
  36.    elseif event == "timer" and p1 == pollTimer then
  37.       local info = tank.getTankInfo()
  38.       local maxCapacity = info[1].capacity
  39.       local curCapacity = info[1].contents.amount
  40.       local fillP = math.floor((curCapacity/maxCapacity) * 100)
  41.       local tankLabel = "Fluid " .. info[1].contents.rawName
  42.       status[tankLabel] = fillP .. "%"
  43.       if modem ~= nil then modem.transmit(statusChannelID, 0, textutils.serialize(status)) end
  44.       term.setCursorPos(16, 3)
  45.       term.write(info[1].contents.rawName .. "                          ")
  46.       term.setCursorPos(18, 4)
  47.       term.write(fillP .. "%        ")
  48.       pollTimer = os.startTimer(5)
  49.    end
  50. end
RAW Paste Data