crameep

Tank Monitor

Apr 4th, 2013
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2. os.loadAPI("ocs/apis/graph")
  3. local usedamount = 0
  4. local prox = sensor.wrap("left")
  5. local i = 0
  6. local usage = 0
  7. --print(textutils.serialize(prox.getTargets()))
  8.  
  9. local targets = prox.getTargets()
  10.  
  11. for name,basicDetails in pairs(targets) do
  12.   print("Found Tank: "..name)
  13.   tankDetails = prox.getTargetDetails(name)
  14.   if (tankDetails ~= nil) then
  15.     print("That Works")
  16.     tank = name
  17.     break
  18.   end
  19. end
  20.  
  21. local init = tankDetails.Tanks[1].Amount
  22. local capacity = tankDetails.Tanks[1].Capacity
  23. local type = tankDetails.Tanks[1].Name
  24.  
  25. if type == "item.fuel" then
  26.  type = "Fuel"
  27. end
  28.  
  29. function comma_value(amount)
  30.   local formatted = amount
  31.   while true do
  32.     formatted, k = string.gsub(formatted,"^(-?%d+) (%d%d%d)", '%1,%2')
  33.      if (k==0) then
  34.        break
  35.      end
  36.     end
  37.     return formatted
  38. end
  39.  
  40. function round(val, decimal)
  41.   if (decimal) then
  42.     return math.floor( (val * 10^decimal) + 0.5 / (10^decimal) )
  43.   else
  44.     return math.floor(val+0.5)
  45.   end
  46. end
  47.  
  48. function format_num(amount, decimal,prefix, neg_prefix)
  49.   local str_amount, formatted, famount, remain
  50.  
  51.   decimal = decimal or 2
  52.   neg_prefix = neg_prefix or "-"
  53.  
  54.   famount = math.abs(round(amount,decimal))
  55.   famount = math.floor(famount)
  56.  
  57.   remain = round(math.abs(amount) - famount, decimal)
  58.  
  59.   formatted = comma_value(famount)
  60.  
  61.   if (decimal > 0 ) then
  62.     remain = string.sub(tostring(remain),3)
  63.     formatted = formatted .. "." .. remain ..
  64.     string.rep("0", decimal - string.len(remain))
  65.   end
  66.  
  67.   if (amount < 0 ) then
  68.     if (neg_prefix=="()") then
  69.       formatted = "("..formatted ..")"
  70.     else
  71.       formatted = neg_prefix .. formatted
  72.     end
  73.   end
  74.   return formatted
  75. end
  76.  
  77. function update()
  78.   i = i + 1
  79.   local amt = sensor.call("left", "getTargetDetails",tank)
  80.   if amt ~= nil then
  81.   usage = init - amt.Tanks[1].Amount
  82.   fuelcons = ( usage / i * 12 * 60 )
  83.   timeleft = amt.Tanks[1].Amount / fuelcons
  84.   fuelecons = fuelcons * -1
  85.  
  86. --  fuelcons = format_num(fuelcons,2)
  87.  term.clear()
  88.  term.setCursorPos(1,1)
  89.   print("Consuming " ..fuelcons .." Fuel Per Hour")
  90.   return amt.Tanks[1].Amount
  91.   else
  92.   print("amt was null")
  93.   end
  94. end
  95.  
  96. function newLine()
  97.   local_,cY= mon.getCursorPos()
  98.   mon.setCursorPos(1,cY+1)
  99. end
  100.  
  101. mon = peripheral.wrap("top")
  102. graphInstance = graph.new(mon, update, type, nil, 0 , capacity)
  103. mon.setTextScale(5)
  104. while true do
  105.  
  106. graphInstance:draw()
  107. newLine()
  108. mon.write(" Consuming: ")
  109. mon.setTextColor(colors.lightBlue)
  110. mon.write(fuelcons)
  111. mon.setTextColor(colors.white)
  112. mon.write(" /h")
  113. newLine()
  114. mon.write(" Time Remaining: ")
  115. mon.setTextColor(colors.yellow)
  116. mon.write(timeleft)
  117. mon.setTextColor(colors.white)
  118. mon.write(" Hours")
  119. sleep(5)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment