Advertisement
Clorith

LiquidController

Apr 13th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. -- Load dependencies
  2. os.loadAPI( "ProgressBar" )
  3.  
  4. -- Configurations
  5. local rednetLocation = "right"
  6. local monitorFontSize = 1.0
  7. local liquidPerScreen = 3
  8. -- Configurations End
  9.  
  10. local monitor = {
  11.     peripheral.find( "monitor" )
  12. }
  13. local liquidType = {}
  14. local monitorCount = {}
  15.  
  16.  
  17. -- Helper functions
  18. function stringSplit( str )
  19.     local t = {}
  20.  
  21.     local function helper( word )
  22.         table.insert( t, word )
  23.         return ""
  24.     end
  25.  
  26.     if not str:gsub( "%w+", helper ):find"%S" then
  27.         return t
  28.     end
  29. end
  30.  
  31. function getAvailableMonitor()
  32.     for monitorNum, monitorFunc in pairs( monitor ) do
  33.         if table.getn( monitorCount[ monitorNum ] ) < liquidPerScreen then
  34.             return monitorNum
  35.         end
  36.     end
  37.  
  38.     return nil
  39. end
  40.  
  41. function getLatestMonitorLocation( monitor )
  42.  
  43. end
  44.  
  45. function writeToMonitor( liquid, msg )
  46.     local liquid = liquidType[ liquid ]
  47.     local m = monitor[ liquid["monitor"] ]
  48.  
  49. end
  50.  
  51. function printN( repetitions, msg )
  52.     while repetitions >= 0 do
  53.         print( msg )
  54.         repetitions = repetitions - 1
  55.     end
  56. end
  57.  
  58. function parseMsg( id, msg )
  59.     local incomingLine = stringSplit( msg )
  60.  
  61.     if "liquidData" == incomingLine[1] then
  62.         local liquidID      = incomingLine[2]
  63.         local liquidCurrent = incomingLine[3]
  64.         local liquidMax     = incomingLine[4]
  65.         local liquidLabel   = incomingLine[5]
  66.         local liquidPercent = ( liquidCurrent / liquidMax ) * 100
  67.  
  68.         if nil == liquidType[ liquidID ] then
  69.             local newMonitor = getAvailableMonitor()
  70.             if nil == newMonitor then
  71.                 printN( 20, "No more monitor space, expansion needed for " .. liquidLabel .. "!" )
  72.             else
  73.                 local latestMonitorLocation = getLatestMonitorLocation( newMonitor )
  74.                 if nil == latestMonitorLocation then
  75.                     local progressBarStart = 1
  76.                 else
  77.                     local progressBarStart = latestMonitorLocation + 1
  78.                 end
  79.  
  80.                 local newProgressBar = ProgressBar.SetPeripheral( monitor[ newMonitor ] )
  81.  
  82.                 monitorCount[ newMonitor ][ liquidID ] = true
  83.                 liquidType[ liquidID ] = {}
  84.                 liquidType[ liquidID ]["label"] = liquidLabel
  85.                 liquidType[ liquidID ]["monitor"] = monitor[ newMonitor ]
  86.                 liquidType[ liquidID ]["writeLine"] = latestMonitorLocation
  87.                 liquidType[ liquidID ]["progress"] = newProgressBar
  88.  
  89.                 newProgressBar.SetTable( liquidID, 100, 0, 5, barWidth, progressBarStart )
  90.             end
  91.         end
  92.  
  93.         if not nil == liquidType[ liquidID ] then
  94.             local writeTo = liquidType[ liquidID ]["writeLine"]
  95.             local m = liquidType[ liquidID ]["monitor"]
  96.             local p = liquidType[ liquidID ]["progress"]
  97.  
  98.             m.setCursorPos( 5, writeTo )
  99.             m.clearLine()
  100.             m.write( liquidType[ liquidID ]["label"] .. " (" .. liquidCurrent .. " / " .. liquidMax .. "" )
  101.             p.SetCurValue( liquidID, liquidPercent )
  102.  
  103.         end
  104.     end
  105. end
  106.  
  107.  
  108. -- Start by resetting all active monitors
  109. for monitorNum, monitorFunc in pairs( monitor ) do
  110.     monitorFunc.clear()
  111.     monitorCount[monitorNum] = {}
  112. end
  113.  
  114. rednet.open( rednetLocation )
  115.  
  116. while true do
  117.     id, msg, time = rednet.receive()
  118.     print( msg )
  119.  
  120.     parseMsg( id, msg )
  121. end
  122.  
  123. rednet.close( rednetLocation )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement