Advertisement
Syndran

tank monitor

Sep 26th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. local tArgs = {...}
  2. local dir = tonumber(tArgs[1]) --valve Direction
  3. local warning = tonumber(tArgs[2]) -- Warning level in %
  4.  
  5. -- Valve and output
  6. local tank = peripheral.wrap(dir)
  7.  
  8. local cap                -- Tank capacity
  9. local amount             -- Amount liquid in tank
  10. local percentfull        -- Percent liquid in tank
  11. local lastpercent = 100  -- Percent last loop
  12. local sleeptime          -- How long to sleep
  13.  
  14. --redstone.setOutput("left", false)
  15.  
  16. function getTank(tankPeriph)
  17.     local tableInfo = tankPeriph.getTankInfo(dir) -- Local to the getTank function.
  18.  
  19.     for k,v in pairs(tableInfo) do
  20.         fluidRaw = v.rawName -- local to this for loop
  21.         fluidName = v.name -- local to this for loop
  22.         fluidAmount = v.amount -- local to this for loop
  23.         fluidCapacity = v.capacity -- local to this for loop
  24.     end
  25.  
  26.     return fluidRaw, fluidName, fluidAmount, fluidCapacity -- Returning the values of global variables (which are nil).
  27. end
  28.  
  29. -- Main prog loop, never stop
  30.  
  31. while true do  
  32.  
  33.   -- Fill table with data from tank valve
  34.   local fluidRaw, fluidName, fluidAmount, fluidCapacity = getTank(tank)
  35.  
  36.   if fluidName then
  37.     -- Get values for tank capacity and amount
  38.  
  39.     fluidName = string.gsub(fluidName, "^%l", string.upper)
  40.  
  41.     cap = fluidCapacity / 1000
  42.  
  43.     amount = fluidAmount
  44.    
  45.     -- If tank is empty, to avoid math issues with 0
  46.  
  47.     if amount == nil then
  48.       amount = 0
  49.       percentfull = 0
  50.     else
  51.  
  52.       -- Use math.floor to convert to integers
  53.       amount = math.floor(amount / 1000)
  54.       percentfull = math.floor(100 * amount / cap)
  55.  
  56.                
  57.     end
  58.     term.clear()
  59.     print(fluidName)
  60.     print(amount)  
  61.    print(percentful)
  62.    sleeptime = 10
  63.  
  64.    end -- ends if fluidname
  65. sleep(sleeptime)    
  66.  
  67.  
  68. end  --ends while true loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement