Hiranus icon

Fluid Monitor

Hiranus | PRO | 06/22/15 06:44:52 PM UTC | 0 ⭐ | 1350 👁️ | Never ⏰ | []
Lua |

3.74 KB

|

None

|

0 👍

/

0 👎

local tanks={}
local data ={}
function Tanks ()
    print ("Loading tanks")
    tankID = 1
    
    per_list = peripheral.getNames()
    for i = 1, #per_list do
        methods = peripheral.getMethods(per_list[i]) 
        for j = 1, #methods do
            if methods[j] == "getTankInfo" then
                tanks[tankID] = per_list[i]
                tankID = tankID+1
            end
        end
    end
    print ("Tanks loaded")
    return tanks
end
function Tank_Data_Dump (__tank_ident)
    tank = peripheral.wrap(__tank_ident)
    if tank == nil then 
        return
    end
    curr_tank_data = tank.getTankInfo()
    curr_tank_data["peripheralName"] = __tank_ident
    for i=1,#curr_tank_data do
        if curr_tank_data[i]["contents"]==nil then
            curr_tank_data[i]["contents"] = {}
            curr_tank_data[i]["contents"]["rawName"]=""
        else
            curr_tank_data[i]["contents"]["amount"]=nil
            curr_tank_data[i]["contents"]["name"]=nil
            curr_tank_data[i]["contents"]["id"]=nil
        end
        curr_tank_data[i]["capacity"] = nil
        curr_tank_data[i]["signalWhen"] = "above"
        curr_tank_data[i]["percentage"] = 50
        curr_tank_data[i]["redstone"] = {}
        curr_tank_data[i]["redstone"]["side"] = "top"
        --curr_tank_data[i]["redstone"]["strength"] = 15
        curr_tank_data[i]["redstone"]["bundled"] = 0
    end
    return curr_tank_data
end
function Data_Dump ()
    print("Preparing data dump")
    data["interval"] = 5
    data["tanks"] = {}
    for i=1,#tanks do
        data["tanks"][i] = Tank_Data_Dump(tanks[i])
    end
    
    local h = fs.open("config.cfg", "w")
    h.write(textutils.serialize( data ) ) 
    h.close()
    print ("Data dumped, Edit 'config.cfg' to your liking and start program again")
end
function Data_Load ()
    print("Loading config")
    local h = fs.open("config.cfg", "r")
    data = textutils.unserialize( h.readAll() )  
    h.close()
    print("Loaded config")
end
function Reset_Redstone ()
    sides = rs.getSides()
    for i=1,#sides do
        rs.setOutput(sides[i],false)
        rs.setBundledOutput(sides[i],0)
    end
end
function Set_Redstone(__state,__redstone)
    if __redstone["bundled"]==0 then
        rs.setOutput(__redstone["side"],__state)
    else
        signal = rs.testBundledInput(__redstone["side"],__redstone["bundled"])
        if signal ~= __state then 
            if signal ==true then
 
                rs.setBundledOutput(__redstone["side"],rs.getBundledInput(__redstone["side"]) - __redstone["bundled"])
            else
 
                rs.setBundledOutput(__redstone["side"],rs.getBundledInput(__redstone["side"]) + __redstone["bundled"])
            end
        end
    end
end
function Output (__tank_info, __tank_config)
    perc=0
    if  __tank_info["contents"]~= nil then
        perc = __tank_info["contents"]["amount"]/__tank_info["capacity"]*100
    end
    if __tank_config["signalWhen"] == "above" then
        if perc>__tank_config["percentage"] then
            Set_Redstone(true,__tank_config["redstone"])
        else
            Set_Redstone(false,__tank_config["redstone"])
        end
    else 
        if __tank_config["signalWhen"] == "below" then
            if perc<__tank_config["percentage"] then
                Set_Redstone(true,__tank_config["redstone"])
            else
                Set_Redstone(false,__tank_config["redstone"])
            end
        end
    end
end
 
 
if not fs.exists("config.cfg") then
    
    Tanks()
    Data_Dump()
else
    Reset_Redstone()
    Data_Load()
    print ("Looping")
    while (true) do
        for i=1,#data["tanks"] do
            tank_info = peripheral.call(data["tanks"][i]["peripheralName"],"getTankInfo")
            if tank_info == nil then
                data["tanks"][i] = nil
            else 
                for j=1, #data["tanks"][i] do
                    if data["tanks"][i][j]["contents"]["rawName"] == "" or tank_info[j]["contents"]==nil then
                        Output(tank_info[j],data["tanks"][i][j])
                    else 
                        if (data["tanks"][i][j]["contents"]["rawName"] == tank_info[j]["contents"]["rawName"]) then
                            Output(tank_info[j],data["tanks"][i][j])
                        end
                    end
                    
                end
            end
        end 
        sleep(data["interval"])
    end
end

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎