Incognition

dataCollectorTankOnly

Apr 12th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.70 KB | None | 0 0
  1. local mTank         = peripheral.wrap("back")   -- Tank Modem
  2. local pGlasses      = peripheral.wrap("top")    -- Bridge
  3. local updateFreq    = 1                         -- How often do we update?
  4.  
  5. -- Glasses
  6. local anchorMain        = { ["x"] = 10, ["y"] = 50 }
  7. local glassesBarSize    = { ["x"] = 60, ["y"] = 10 }
  8. local g_bars            = {}
  9. local g_texts           = {}
  10. local bar_y             = 0
  11.  
  12. -- Tanks
  13. local tanksPredefData   = { [2] = { ["name"] = "Lava", ["color"] = 0xFF0000 } }
  14. local tanksData         = {}
  15. local tanksEmpty        = 0
  16.  
  17. function registerRemotePeripherals( modem )
  18.     local remote = modem.getNamesRemote()
  19.     local peripherals = {}
  20.      
  21.     for k,v in pairs( remote ) do
  22.         peripherals[ k ] = peripheral.wrap( tostring( v ) )
  23.     end
  24.    
  25.     return peripherals
  26. end
  27.  
  28. local pTanks            = registerRemotePeripherals( mTank )
  29.  
  30. function getTank( id )
  31.   return pTanks[ id ].getTankInfo( "unknown" )[1]
  32. end
  33.  
  34. function performUpdate()
  35.     -- Update tank values
  36.     tanksEmpty = 0
  37.    
  38.     for k = 1, #pTanks do
  39.         local tank = getTank( k )
  40.  
  41.         if( tank.rawName ~= nil ) then
  42.             tanksData[ tank.id ] = { ["amount"] = tank.amount, ["capacity"] = tank.capacity }
  43.         else
  44.           tanksEmpty = tanksEmpty + 1    
  45.         end
  46.     end
  47. end
  48.  
  49. function getRoundedPercent( num )
  50.     return math.floor( num * 100 )
  51. end
  52.  
  53. function barExist( id )
  54.     if( g_bars[ id ] ~= nil ) then
  55.         return true
  56.     end
  57.     return false
  58. end
  59.  
  60. function changeBar( id, x, y, percent, color )
  61.     if( barExist( id ) == false ) then
  62.         return
  63.     end
  64.    
  65.     local d = g_bars[ id ]
  66.    
  67.     if( color == nil ) then
  68.         color = 0x99FF00
  69.     end
  70.    
  71.     if( x == nil ) then
  72.         x = d[ "border" ].getX()
  73.     end
  74.    
  75.     if( y == nil ) then
  76.         y = d[ "border" ].getY()
  77.     end
  78.    
  79.     d[ "border" ].setX( x )
  80.     d[ "border" ].setY( y )
  81.     d[ "borderbg" ].setX( x + 1 )
  82.     d[ "borderbg" ].setY( y + 1 )
  83.     d[ "bar" ].setX( x + 3 )
  84.     d[ "bar" ].setY( y + 3 )
  85.    
  86.     local w = d[ "border" ].getWidth()
  87.     local dx = math.ceil( ( w - 6 ) * ( percent / 100 ) )
  88.    
  89.     d["bar"].setWidth( dx )
  90.    
  91.     return d
  92. end
  93.  
  94. function createBarAt( id, x, y, percent, color )
  95.     if( color == nil ) then
  96.         color = 0x99FF00
  97.     end
  98.    
  99.     local d = {}
  100.     local w = glassesBarSize["x"]
  101.     local h = glassesBarSize["y"]
  102.     local dx = math.ceil( ( w - 6 ) * ( percent / 100 ) )
  103.    
  104.     d["border"]     = pGlasses.addBox( x, y, w, h, 0xFFFFFF, 1 )
  105.     d["borderbg"]   = pGlasses.addBox( x + 1, y + 1, w - 2, h - 2, 0x000000, 1 )
  106.     d["bar"]        = pGlasses.addGradientBox( x + 3, y + 3, dx, h - 6, color, 0.05, 0xFFFFFF, 1, 2 )
  107.    
  108.     d["border"].setZ( 1 )
  109.     d["borderbg"].setZ( 2 )
  110.     d["bar"].setZ( 3 )
  111.    
  112.     g_bars[ id ] = d
  113.    
  114.     return g_bars[ id ]
  115. end
  116.  
  117. function getBar( id, x, y, percent, color )
  118.     if( barExist( id ) ) then
  119.         return changeBar( id, x, y, percent, color )
  120.     end
  121.    
  122.     return createBarAt( id, x, y, percent, color )
  123. end
  124.  
  125. function textExist( id )
  126.     if( g_texts[ id ] ~= nil ) then
  127.         return true
  128.     end
  129.     return false
  130. end
  131.  
  132. function changeText( id, x, y, text, color )
  133.     if( textExist( id ) == false ) then
  134.         return nil
  135.     end
  136.    
  137.     if( color == nil ) then
  138.         color = 0xFFFFFF
  139.     end
  140.    
  141.     local d = g_texts[ id ]
  142.     d.setX( x )
  143.     d.setY( y )
  144.     d.setText( text )
  145.     d.setColor( color )
  146.    
  147.     return d
  148. end
  149.  
  150. function createTextAt( id, x, y, text, color )
  151.     if( textExist( id ) ) then
  152.         return nil
  153.     end
  154.    
  155.     if( color == nil ) then
  156.         color = 0xFFFFFF
  157.     end
  158.    
  159.     local d = pGlasses.addText( x, y, text, color )
  160.    
  161.     g_texts[ id ] = d
  162.    
  163.     return g_texts[ id ]
  164. end
  165.  
  166. function getText( id, x, y, text, color )
  167.     if( textExist( id ) ) then
  168.         return changeText( id, x, y, text, color )
  169.     end
  170.    
  171.     return createTextAt( id, x, y, text, color )
  172. end
  173.  
  174. function handleRow( tid, part, total, color, text )
  175.     local percent = getRoundedPercent( part / total )
  176.     local c = 0xFFFFFF
  177.    
  178.     -- Bytes
  179.     b = getBar( tid, anchorMain["x"] + 50, bar_y, percent, color )
  180.    
  181.     if( percent < 11 ) then
  182.         c = 0xFF0000
  183.     end
  184.    
  185.     x = b[ "border" ].getX() + b[ "border" ].getWidth() + 5
  186.    
  187.     getText( tid .. "_pre", anchorMain["x"], bar_y + 1, text, 0xFFFFFF )
  188.     getText( tid .. "_post", x, bar_y + 1, percent .. "%", c )
  189.    
  190.     print( text .. " - " .. percent .. "%" .. "[ " .. part .. " / " .. total .. " ]" )
  191.    
  192.     bar_y = bar_y + glassesBarSize["y"] + 5
  193. end
  194.  
  195. function updateGlassesDisplay()
  196.     bar_y = anchorMain["y"]
  197.    
  198.     term.clear()
  199.     term.setCursorPos( 1, 1 )
  200.    
  201.     -- Tanks
  202.     for k, v in pairs( tanksData ) do
  203.         if tanksPredefData[ k ] == nil then
  204.             print( "missing fluid data for [ " .. k .. " ]" )
  205.         else
  206.             handleRow( "tank_" .. k, v["amount"], v["capacity"], tanksPredefData[ k ][ "color" ], tanksPredefData[ k ][ "name" ] )
  207.         end
  208.     end
  209. end
  210.  
  211. pGlasses.clear()
  212.  
  213. while ( true ) do
  214.     performUpdate()
  215.     updateGlassesDisplay()
  216.     sleep( updateFreq )
  217. end
Advertisement
Add Comment
Please, Sign In to add comment