Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local N = shell.getRunningProgram()
- local Debug = false
- local warning = 10
- local channel = tonumber(tArgs[2])
- local Usage = function()
- if #tArgs < 2 or #tArgs > 2 then
- print("Usage:")
- print(" "..N.." host <channel>")
- print(" "..N.." serv <channel>")
- exit()
- end
- end
- local findColor = function(NAME)
- if string.lower(NAME) == "water" then
- return colors.blue
- elseif string.lower(NAME) == "ethanol" then
- return colors.orange
- elseif string.lower(NAME) == "lava" then
- return colors.red
- elseif string.lower(NAME) == "milk" then
- return colors.white
- elseif string.lower(NAME) == "biomass" then
- return colors.lime
- elseif string.lower(NAME) == "creosote oil" then
- return colors.brown
- elseif string.lower(NAME) == "essence" then
- return colors.green
- elseif string.lower(NAME) == "fuel" then
- return colors.yellow
- elseif string.lower(NAME) == "biofuel" then
- return colors.purple
- elseif string.lower(NAME) == "oil" then
- return colors.gray
- end
- return colors.cyan
- end
- Usage()
- if string.lower(tArgs[1]) == "host" then
- local MON, MOD
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "monitor" then
- MON = peripheral.wrap(v)
- if Debug then print("Monitor "..v) end
- elseif peripheral.getType(v) == "modem" then
- MOD = peripheral.wrap(v)
- if not MOD.isWireless() then
- MOD = nil
- else
- if Debug then print("Modem "..v) end
- end
- end
- end
- if not MOD then error("Wireless Modem Required") end
- if not MON then error("Monitor Required") end
- local background = colors.lightGray
- local tanks = {}
- local X,Y = MON.getSize()
- local isA = MON.isColor and MON.isColor()
- local barDraw = function(TANK,START,WIDTH,COLS)
- local height = math.ceil(Y*TANK["percent"])
- local Name = TANK["name"]
- local detail = tostring(TANK["amount"]).."/"..tostring(TANK["capacity"])
- local color = findColor(Name)
- for i=1,height do
- MON.setCursorPos(START,(Y+1)-i)
- for I=1,WIDTH do
- if isA then MON.setBackgroundColor(color) end
- MON.write(" ")
- if isA then MON.setBackgroundColor(background) end
- end
- end
- MON.setCursorPos(((X/COLS)+START-(WIDTH/2))-(#Name/2),(Y-height/2))
- if isA then MON.setBackgroundColor(color) end
- MON.write(Name)
- if isA then MON.setBackgroundColor(background) end
- MON.setCursorPos(((X/COLS)+START-(WIDTH/2))-(#detail/2),(Y-height/2)+1)
- if isA then MON.setBackgroundColor(color) end
- MON.write(detail)
- if isA then MON.setBackgroundColor(background) end
- end
- local drawAllTanks = function()
- local cols = 0
- for tank, data in pairs(tanks) do
- cols = cols + 1
- end
- local colWidth = math.floor(X/cols)
- local nextStart = 1
- if Debug then print("ColWidth:"..colWidth) end
- for tank, data in pairs(tanks) do
- barDraw(data,nextStart,colWidth,cols)
- nextStart = nextStart + colWidth
- end
- end
- MOD.open(channel)
- if isA then
- MON.setTextColor(colors.black)
- MON.setBackgroundColor(colors.lightGray)
- end
- MON.clear()
- if isA then MON.setBackgroundColor(background) end
- if Debug then print("X:"..X.." Y:"..Y) end
- while true do
- local ev = {os.pullEvent()}
- if ev[1] == "monitor_resize" then
- X,Y = MON.getSize()
- if Debug then print("resized- X:"..X.." Y:"..Y) end
- elseif ev[1] == "modem_message" then
- if Debug then print(ev[5]) end
- local rawData = textutils.unserialize(ev[5])
- if type(rawData) == "table" then
- local id = rawData[1]["cid"]
- local PRCT = (rawData[1]["amount"] / rawData[1]["capacity"])
- tanks[id] = {percent=PRCT, amount=math.floor(rawData[1]["amount"]/1000), capacity=math.floor(rawData[1]["capacity"]/1000), name=rawData[1]["name"]}
- end
- end
- MON.clear()
- drawAllTanks()
- if Debug then print(textutils.serialize(tanks)) end
- end
- elseif string.lower(tArgs[1]) == "serv" then
- local MOD, T
- for k,v in pairs(rs.getSides()) do
- if peripheral.getType(v) == "iron_tank_valve" then
- T = peripheral.wrap(v)
- if Debug then print("Valve "..v) end
- elseif peripheral.getType(v) == "modem" then
- MOD = peripheral.wrap(v)
- if not MOD.isWireless() then
- MOD = nil
- else
- if Debug then print("Modem "..v) end
- end
- end
- end
- if not MOD then error("Wireless Modem Required") end
- if not T then error("Computer Must be Next to Tank Valve") end
- MOD.open(channel)
- local oldD = nil
- while true do
- local data = T.getTanks("")
- data[1]["cid"] = os.getComputerID()
- if data[1]["amount"] == nil then
- data[1]["amount"] = 0
- data[1]["name"] = tostring(data[1]["cid"]).." EMPTY!"
- end
- if oldD == data[1]["amount"] then
- if Debug then print("skipped") end
- sleep(1)
- else
- MOD.transmit(channel,channel,textutils.serialize(data))
- oldD = data[1]["amount"]
- print("Sent Update on: "..os.time())
- end
- if Debug then print(textutils.serialize(data)) end
- sleep(3)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement