Advertisement
cozzimoto

Railcraft Tank Monitor all-in-one B:1

Oct 8th, 2013
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 KB | None | 0 0
  1. local tArgs = {...}
  2. local N = shell.getRunningProgram()
  3. local Debug = false
  4. local warning = 10
  5. local channel = tonumber(tArgs[2])
  6.  
  7. local Usage = function()
  8.   if #tArgs < 2 or #tArgs > 2 then
  9.     print("Usage:")
  10.     print("   "..N.." host <channel>")
  11.     print("   "..N.." serv <channel>")
  12.     exit()
  13.   end
  14. end
  15.  
  16. local findColor = function(NAME)
  17.   if string.lower(NAME) == "water" then
  18.     return colors.blue
  19.   elseif string.lower(NAME) == "ethanol" then
  20.     return colors.orange
  21.   elseif string.lower(NAME) == "lava" then
  22.     return colors.red
  23.   elseif string.lower(NAME) == "milk" then
  24.     return colors.white
  25.   elseif string.lower(NAME) == "biomass" then
  26.     return colors.lime
  27.   elseif string.lower(NAME) == "creosote oil" then
  28.     return colors.brown
  29.   elseif string.lower(NAME) == "essence" then
  30.     return colors.green
  31.   elseif string.lower(NAME) == "fuel" then
  32.     return colors.yellow
  33.   elseif string.lower(NAME) == "biofuel" then
  34.     return colors.purple
  35.   elseif string.lower(NAME) == "oil" then
  36.     return colors.gray
  37.   end
  38.   return colors.cyan
  39. end
  40.  
  41. Usage()
  42. if string.lower(tArgs[1]) == "host" then
  43.   local MON, MOD
  44.   for k,v in pairs(rs.getSides()) do
  45.     if peripheral.getType(v) == "monitor" then
  46.       MON = peripheral.wrap(v)
  47.       if Debug then print("Monitor "..v) end
  48.     elseif peripheral.getType(v) == "modem" then
  49.       MOD = peripheral.wrap(v)
  50.       if not MOD.isWireless() then
  51.         MOD = nil
  52.       else
  53.         if Debug then print("Modem "..v) end
  54.       end
  55.     end
  56.   end
  57.   if not MOD then error("Wireless Modem Required") end
  58.   if not MON then error("Monitor Required") end
  59.  
  60.   local background = colors.lightGray
  61.   local tanks = {}
  62.   local X,Y = MON.getSize()
  63.   local isA = MON.isColor and MON.isColor()
  64.  
  65.   local barDraw = function(TANK,START,WIDTH,COLS)
  66.     local height = math.ceil(Y*TANK["percent"])
  67.     local Name = TANK["name"]
  68.     local detail = tostring(TANK["amount"]).."/"..tostring(TANK["capacity"])
  69.     local color = findColor(Name)
  70.    
  71.     for i=1,height do
  72.       MON.setCursorPos(START,(Y+1)-i)
  73.       for I=1,WIDTH do
  74.         if isA then MON.setBackgroundColor(color) end
  75.         MON.write(" ")
  76.         if isA then MON.setBackgroundColor(background) end
  77.       end    
  78.     end
  79.     MON.setCursorPos(((X/COLS)+START-(WIDTH/2))-(#Name/2),(Y-height/2))
  80.     if isA then MON.setBackgroundColor(color) end
  81.     MON.write(Name)
  82.     if isA then MON.setBackgroundColor(background) end
  83.     MON.setCursorPos(((X/COLS)+START-(WIDTH/2))-(#detail/2),(Y-height/2)+1)
  84.     if isA then MON.setBackgroundColor(color) end
  85.     MON.write(detail)
  86.     if isA then MON.setBackgroundColor(background) end
  87.   end
  88.  
  89.   local drawAllTanks = function()
  90.     local cols = 0
  91.     for tank, data in pairs(tanks) do
  92.       cols = cols + 1
  93.     end
  94.     local colWidth = math.floor(X/cols)
  95.     local nextStart = 1
  96.     if Debug then print("ColWidth:"..colWidth) end
  97.     for tank, data in pairs(tanks) do
  98.       barDraw(data,nextStart,colWidth,cols)
  99.       nextStart = nextStart + colWidth
  100.     end
  101.   end
  102.  
  103.   MOD.open(channel)
  104.   if isA then
  105.     MON.setTextColor(colors.black)
  106.     MON.setBackgroundColor(colors.lightGray)
  107.   end
  108.   MON.clear()
  109.   if isA then MON.setBackgroundColor(background) end
  110.   if Debug then print("X:"..X.." Y:"..Y) end
  111.  
  112.   while true do
  113.     local ev = {os.pullEvent()}
  114.     if ev[1] == "monitor_resize" then
  115.       X,Y = MON.getSize()
  116.       if Debug then print("resized- X:"..X.." Y:"..Y) end
  117.  
  118.     elseif ev[1] == "modem_message" then
  119.       if Debug then print(ev[5]) end
  120.       local rawData = textutils.unserialize(ev[5])
  121.      
  122.       if type(rawData) == "table" then
  123.         local id = rawData[1]["cid"]
  124.         local PRCT = (rawData[1]["amount"] / rawData[1]["capacity"])
  125.        
  126.         tanks[id] = {percent=PRCT, amount=math.floor(rawData[1]["amount"]/1000), capacity=math.floor(rawData[1]["capacity"]/1000), name=rawData[1]["name"]}
  127.       end
  128.     end
  129.  
  130.     MON.clear()
  131.     drawAllTanks()
  132.     if Debug then print(textutils.serialize(tanks)) end
  133.   end
  134.  
  135. elseif string.lower(tArgs[1]) == "serv" then
  136.   local MOD, T
  137.   for k,v in pairs(rs.getSides()) do
  138.     if peripheral.getType(v) == "iron_tank_valve" then
  139.       T = peripheral.wrap(v)
  140.       if Debug then print("Valve "..v) end
  141.      
  142.     elseif peripheral.getType(v) == "modem" then
  143.       MOD = peripheral.wrap(v)
  144.       if not MOD.isWireless() then
  145.         MOD = nil
  146.       else
  147.         if Debug then print("Modem "..v) end
  148.       end
  149.     end
  150.   end
  151.   if not MOD then error("Wireless Modem Required") end
  152.   if not T then error("Computer Must be Next to Tank Valve") end
  153.  
  154.   MOD.open(channel)
  155.   local oldD = nil
  156.   while true do
  157.     local data = T.getTanks("")
  158.    
  159.     data[1]["cid"] = os.getComputerID()
  160.      if data[1]["amount"] == nil then
  161.       data[1]["amount"] = 0
  162.       data[1]["name"] = tostring(data[1]["cid"]).." EMPTY!"
  163.     end
  164.    
  165.     if oldD == data[1]["amount"] then
  166.       if Debug then print("skipped") end
  167.       sleep(1)
  168.     else
  169.       MOD.transmit(channel,channel,textutils.serialize(data))
  170.       oldD = data[1]["amount"]
  171.       print("Sent Update on: "..os.time())
  172.     end
  173.    
  174.     if Debug then print(textutils.serialize(data)) end
  175.  
  176.   sleep(3)
  177.   end
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement