Advertisement
Hiranus

Fluid Monitor

Jun 22nd, 2015
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1. local tanks={}
  2. local data ={}
  3. function Tanks ()
  4.     print ("Loading tanks")
  5.     tankID = 1
  6.    
  7.     per_list = peripheral.getNames()
  8.     for i = 1, #per_list do
  9.         methods = peripheral.getMethods(per_list[i])
  10.         for j = 1, #methods do
  11.             if methods[j] == "getTankInfo" then
  12.                 tanks[tankID] = per_list[i]
  13.                 tankID = tankID+1
  14.             end
  15.         end
  16.     end
  17.     print ("Tanks loaded")
  18.     return tanks
  19. end
  20. function Tank_Data_Dump (__tank_ident)
  21.     tank = peripheral.wrap(__tank_ident)
  22.     if tank == nil then
  23.         return
  24.     end
  25.     curr_tank_data = tank.getTankInfo()
  26.     curr_tank_data["peripheralName"] = __tank_ident
  27.     for i=1,#curr_tank_data do
  28.         if curr_tank_data[i]["contents"]==nil then
  29.             curr_tank_data[i]["contents"] = {}
  30.             curr_tank_data[i]["contents"]["rawName"]=""
  31.         else
  32.             curr_tank_data[i]["contents"]["amount"]=nil
  33.             curr_tank_data[i]["contents"]["name"]=nil
  34.             curr_tank_data[i]["contents"]["id"]=nil
  35.         end
  36.         curr_tank_data[i]["capacity"] = nil
  37.         curr_tank_data[i]["signalWhen"] = "above"
  38.         curr_tank_data[i]["percentage"] = 50
  39.         curr_tank_data[i]["redstone"] = {}
  40.         curr_tank_data[i]["redstone"]["side"] = "top"
  41.         --curr_tank_data[i]["redstone"]["strength"] = 15
  42.         curr_tank_data[i]["redstone"]["bundled"] = 0
  43.     end
  44.     return curr_tank_data
  45. end
  46. function Data_Dump ()
  47.     print("Preparing data dump")
  48.     data["interval"] = 5
  49.     data["tanks"] = {}
  50.     for i=1,#tanks do
  51.         data["tanks"][i] = Tank_Data_Dump(tanks[i])
  52.     end
  53.    
  54.     local h = fs.open("config.cfg", "w")
  55.     h.write(textutils.serialize( data ) )
  56.     h.close()
  57.     print ("Data dumped, Edit 'config.cfg' to your liking and start program again")
  58. end
  59. function Data_Load ()
  60.     print("Loading config")
  61.     local h = fs.open("config.cfg", "r")
  62.     data = textutils.unserialize( h.readAll() )  
  63.     h.close()
  64.     print("Loaded config")
  65. end
  66. function Reset_Redstone ()
  67.     sides = rs.getSides()
  68.     for i=1,#sides do
  69.         rs.setOutput(sides[i],false)
  70.         rs.setBundledOutput(sides[i],0)
  71.     end
  72. end
  73. function Set_Redstone(__state,__redstone)
  74.     if __redstone["bundled"]==0 then
  75.         rs.setOutput(__redstone["side"],__state)
  76.     else
  77.         signal = rs.testBundledInput(__redstone["side"],__redstone["bundled"])
  78.         if signal ~= __state then
  79.             if signal ==true then
  80.  
  81.                 rs.setBundledOutput(__redstone["side"],rs.getBundledInput(__redstone["side"]) - __redstone["bundled"])
  82.             else
  83.  
  84.                 rs.setBundledOutput(__redstone["side"],rs.getBundledInput(__redstone["side"]) + __redstone["bundled"])
  85.             end
  86.         end
  87.     end
  88. end
  89. function Output (__tank_info, __tank_config)
  90.     perc=0
  91.     if  __tank_info["contents"]~= nil then
  92.         perc = __tank_info["contents"]["amount"]/__tank_info["capacity"]*100
  93.     end
  94.     if __tank_config["signalWhen"] == "above" then
  95.         if perc>__tank_config["percentage"] then
  96.             Set_Redstone(true,__tank_config["redstone"])
  97.         else
  98.             Set_Redstone(false,__tank_config["redstone"])
  99.         end
  100.     else
  101.         if __tank_config["signalWhen"] == "below" then
  102.             if perc<__tank_config["percentage"] then
  103.                 Set_Redstone(true,__tank_config["redstone"])
  104.             else
  105.                 Set_Redstone(false,__tank_config["redstone"])
  106.             end
  107.         end
  108.     end
  109. end
  110.  
  111.  
  112. if not fs.exists("config.cfg") then
  113.    
  114.     Tanks()
  115.     Data_Dump()
  116. else
  117.     Reset_Redstone()
  118.     Data_Load()
  119.     print ("Looping")
  120.     while (true) do
  121.         for i=1,#data["tanks"] do
  122.             tank_info = peripheral.call(data["tanks"][i]["peripheralName"],"getTankInfo")
  123.             if tank_info == nil then
  124.                 data["tanks"][i] = nil
  125.             else
  126.                 for j=1, #data["tanks"][i] do
  127.                     if data["tanks"][i][j]["contents"]["rawName"] == "" or tank_info[j]["contents"]==nil then
  128.                         Output(tank_info[j],data["tanks"][i][j])
  129.                     else
  130.                         if (data["tanks"][i][j]["contents"]["rawName"] == tank_info[j]["contents"]["rawName"]) then
  131.                             Output(tank_info[j],data["tanks"][i][j])
  132.                         end
  133.                     end
  134.                    
  135.                 end
  136.             end
  137.         end
  138.         sleep(data["interval"])
  139.     end
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement