Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- MonitorOS v1.0 by Masserio
- __ __ _ _ ____ _____
- | \/ | (_) | / __ \ / ____|
- | \ / | ___ _ __ _| |_ ___ _ __| | | | (___
- | |\/| |/ _ \| '_ \| | __/ _ \| '__| | | |\___ \
- | | | | (_) | | | | | || (_) | | | |__| |____) |
- |_| |_|\___/|_| |_|_|\__\___/|_| \____/|_____/
- MonitorOS takes in all information gathered by BatteryHost and
- EngineOS (see list below), and prints it to the screen. My setup
- consists of a max size screen of advanced monitors (8 wide and 6 tall),
- and the programme is designed thereafter.
- This is one part in a series of programs, and this will not work as
- intended on it's own. If you're planning on using this program please
- check out the following programs as well, as they all communicate:
- -BatteryOS v1.1 Pastebin: 3xja4QFK (Stable)
- Monitors Redstone Energy Cells and sends status messages
- -BatteryHost v1.1 Pastebin: m2HkbMiJ (Stable)
- The program collecting statuses from BatteryOS and processes it.
- -EngineOS v1.0 Pastebin: PYRX7G2t (Stable)
- Simple engine control which also sends operational status
- Feel free to copy parts or this whole code, and change it as you like!
- --]]
- -- Set up some variables to be used throughout the programme --
- batteryHostID = nil -- ID of computer running BatteryHost
- batteryStatus = {} -- Stores statuses for all cells
- statusUpdated = false -- For color coding if updated or not
- batteryOutput = 0 -- Maximum power output
- outputUpdated = false -- For color coding if updated or not
- batteryStored = 0 -- Estimate of how much power is stored
- storedUpdated = false -- For color coding if updated or not
- engineID = nil -- ID of computer running EngineOS
- enginesRunning = 0 -- How many engines are currently running
- enginesUpdated = false -- For color coding if updated or not
- monitor = nil -- Monitor where the magic happens, wrapped in a peripheral
- monitorWidth = 0 -- Width of the monitor
- monitorHeight = 0 -- Height of the monitor
- modem = nil -- Side of the modem
- updateTimer = 0 -- To synch with the BatteryHost update time (every ~3 minutes)
- -- Function to produce the time to next update bar --
- function updateBar()
- bar = "["
- timer = math.floor(updateTimer / 2)
- for i=1, timer do
- bar = bar.."I"
- end
- for i=1, 5 - timer do
- bar = bar.." "
- end
- bar = bar.."]"
- return bar
- end
- -- Update everything to the screen --
- function updateScreen()
- monitor.clear()
- monitor.setCursorPos((monitorWidth / 3) - 20, 13)
- monitor.write("Next update "..updateBar())
- monitor.setCursorPos(2, 2)
- monitor.write("Max Output: "..batteryOutput.."MJ/t")
- monitor.setCursorPos(2, 3)
- monitor.write("Stored Energy: "..batteryStored.."M MJ")
- monitor.setCursorPos(2, 4)
- monitor.write("Active engines: "..enginesRunning.." at "..(enginesRunning * 6).."MJ/t")
- for i=1, #batteryStatus do
- oldx, oldy = monitor.getCursorPos()
- if i == 1 or i == 3 or i == 5 or i == 7 or
- i == 9 or i == 11 or i == 13 or i == 15 then
- monitor.setCursorPos(2, oldy + 1)
- else
- monitor.setCursorPos((monitorWidth / 3) - 12, oldy)
- end
- monitor.write("Cell"..string.format("%02d", i)..":"..string.format("%3d", batteryStatus[i]).."%")
- end
- end
- -- Get one of the statuses --
- function getOneCellStatus(ID, msg)
- status = nil
- done = false
- if ID == batteryHostID then
- status = msg
- rednet.send(batteryHostID, "Thanks")
- ID, msg = rednet.receive(1)
- if ID == batteryHostID and msg == "Next" then
- done = true
- print("Done, go to next")
- end
- else
- print("Error in getting a cell status, abort")
- end
- return status, done
- end
- -- Get the cell statuses --
- function getStatuses()
- print("Getting cell statuses")
- done = false
- rednet.send(batteryHostID, "Status")
- gettingStatuses = true
- newBatteryStatus = {}
- while gettingStatuses do
- ID, msg = rednet.receive(1)
- if ID == batteryHostID then
- if msg ~= "Done" and msg ~= "Calling" then
- status, isDone = getOneCellStatus(ID, msg)
- if isDone then
- table.insert(newBatteryStatus, status)
- print("Status added")
- else
- gettingStatuses = false
- end
- elseif msg == "Done" then
- done = true
- gettingStatuses = false
- batteryStatus = newBatteryStatus
- print("Got all statuses")
- else
- -- Timed it with BatteryHost trying to locate new BatteryOS, try again
- print("Timed it with BatteryHost trying to locate new BatteryOS, try again")
- sleep(3)
- getStatuses()
- end
- else
- print("Wrong or no answer, abort")
- gettingStatuses = false
- end
- end
- end
- -- Get the maximum power output --
- function getOutput(times)
- print("Getting maximum power output")
- done = false
- rednet.send(batteryHostID, "Output")
- ID, msg = rednet.receive(1)
- if ID == batteryHostID and msg ~= "Calling" then
- batteryOutput = tonumber(msg) * 0.75
- done = true
- rednet.send(batteryHostID, "Thanks")
- print("Got the output: "..msg)
- elseif times == 4 then
- print("Answer timeout, abort")
- else
- print("Wrong or no answer, try again")
- sleep(1)
- getOutput(times + 1)
- end
- return done
- end
- -- Get the estimate of how much power is stored --
- function getStored(times)
- print("Getting stored energy estimate")
- done = false
- rednet.send(batteryHostID, "Power")
- ID, msg = rednet.receive(1)
- if ID == batteryHostID and msg ~= "Calling" then
- batteryStored = msg
- done = true
- rednet.send(batteryHostID, "Thanks")
- print("Got the estimate: "..msg)
- elseif times == 4 then
- print("Answer timeout, abort")
- else
- print("Wrong or no answer, try again")
- sleep(1)
- getStored(times + 1)
- end
- return done
- end
- -- Get the amount of engines running --
- function getEnginesRunning(times)
- print("Getting amount of engines running")
- done = false
- rednet.send(engineID, "Running")
- ID, msg = rednet.receive(1)
- if ID == engineID then
- enginesRunning = msg
- done = true
- rednet.send(engineID, "Thanks")
- print("Got amount: "..msg)
- elseif times == 4 then
- print("Answer timeout, abort")
- else
- print("Wrong or no answer, try again")
- getEnginesRunning(times + 1)
- end
- return done
- end
- -- Find the computer running BatteryHost --
- function findBatteryHost(times)
- print("Find the computer running BatteryHost")
- done = false
- rednet.broadcast("Host")
- ID, msg = rednet.receive(1)
- if ID ~= nil and msg == "Here" then
- rednet.send(ID, "Monitor")
- ID, msg = rednet.receive(1)
- if ID ~= nil and msg == "Thanks" then
- batteryHostID = ID
- done = true
- print("Host computer found and connected with ID: "..ID)
- else
- print("No confirmation, try again")
- findBatteryHost(times + 1)
- end
- elseif times == 10 then
- print("Tried too many times with no luck, is a host set up?")
- else
- print("Host busy, try again in a sec")
- sleep(2)
- findBatteryHost(times + 1)
- end
- return done
- end
- -- Find the computer running EngineOS --
- function findEngineOS(times)
- print("Find the computer running EngineOS")
- done = false
- rednet.broadcast("Engine")
- ID, msg = rednet.receive(1)
- if ID ~= nil and msg == "Here" then
- rednet.send(ID, "Monitor")
- ID, msg = rednet.receive(1)
- if ID ~= nil and msg == "Thanks" then
- engineID = ID
- done = true
- print("EngineOS computer found and connected with ID: "..ID)
- else
- print("No confirmation, try again")
- findEngineOS(times + 1)
- end
- elseif times == 10 then
- print("Tried too many times with no luck, is EngineOS set up?")
- else
- print("EngineOS busy, try again in a sec")
- sleep(2)
- findEngineOS(times + 1)
- end
- return done
- end
- -- Function to detect peripherals --
- function detectSides()
- print("Detecting Modem and Monitor")
- for i, side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) then
- if peripheral.getType(side) == "modem" then
- print("Modem set to "..side.." side")
- modem = side
- elseif peripheral.getType(side) == "monitor" then
- print("Monitor set to "..side.." side")
- monitor = peripheral.wrap(side)
- end
- end
- end
- end
- -- If it couldn't find BatteryHost or EngineOS pc, give option to input ID manually --
- function getInput(missing)
- print("Could not find "..missing..". Input manually or write 'terminate' to exit program")
- input = io.read()
- if input == "terminate" then
- print("Please configure "..missing.." correctly and try again")
- error()
- elseif tonumber(input) ~= nil then
- if missing == "BatteryHost" then
- batteryHostID = math.floor(input)
- print("BatteryHost computer set to ID: "..batteryHostID)
- elseif missing == "EngineOS" then
- engineID = math.floor(input)
- print("EngineOS computer set to ID: "..engineID)
- else
- print("Something went wrong! Number input, but not correct missing statement")
- error()
- end
- else
- print("Input not a number, try again")
- getInput(missing)
- end
- end
- -- Set up the monitor pc --
- function setup()
- term.clear()
- term.setCursorPos(1, 1)
- print("Running MonitorOS v1.0")
- detectSides()
- if modem == nil then
- print("No modem detected, please attach a modem and try again")
- error()
- end
- if monitor == nil then
- print("No monitor detected, please attach a monitor and try again")
- error()
- end
- width, height = monitor.getSize()
- monitorWidth = width
- monitorHeight = height
- rednet.open(modem)
- print("Modem open and ready")
- -- Wait 45 seconds for BatteryHost and EngineOS to update
- monitor.clear()
- monitor.setTextScale(3)
- monitor.setCursorPos(3, 7)
- monitor.write("Setting up, please wait")
- monitor.setCursorPos(4, 8)
- monitor.write("Takes up to 1 minute")
- sleep(45)
- if not findBatteryHost(1) then
- getInput("BatteryHost")
- end
- sleep(1)
- if not findEngineOS(1) then
- getInput("EngineOS")
- end
- sleep(1)
- updateTimer = 10 -- Run an update via the main loop
- print("Setup complete")
- end
- -- Run the setup, and then start main loop --
- setup()
- while true do
- if updateTimer == 10 then
- -- Get all the info, then update screen
- statusUpdated = getStatuses()
- sleep(1)
- storedUpdated = getStored(1)
- sleep(1)
- outputUpdated = getOutput(1)
- enginesUpdated = getEnginesRunning(1)
- updateScreen()
- updateTimer = 0
- else
- updateTimer = updateTimer + 1
- updateScreen()
- ID, msg = rednet.receive(10)
- if msg == "Reboot" then
- rednet.close(modem)
- os.reboot()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement