Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- BatteryOS v1.1 by Masserio
- ____ _ _ ____ _____
- | _ \ | | | | / __ \ / ____|
- | |_) | __ _| |_| |_ ___ _ __ _ _| | | | (___
- | _ < / _` | __| __/ _ \ '__| | | | | | |\___ \
- | |_) | (_| | |_| || __/ | | |_| | |__| |____) |
- |____/ \__,_|\__|\__\___|_| \__, |\____/|_____/
- __/ |
- |___/
- This program can be started without requireing input by using
- 'shell.run("BatteryOS", <Cell>, "<Input side>", "<Output side>")'
- from any program (most commonly via 'startup').
- Input side is where power goes in to the cells, and output where
- it goes out from the cells.
- This is just a simple program I made for a setup in Minecraft.
- It's made to monitor 'Cells' of Redstone Energy Cells that powers
- my machinery. My setup consists of three Redstone Cells per 'Cell'.
- Two Gate Readers is hooked up to the input cell and output cell, with
- a computer in the middle to read info from both readers.
- If I can be bothered I might update this with a link to screenshots
- of my setup for further explanations.
- 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:
- -BatteryHost v1.1 Pastebin: m2HkbMiJ (Stable)
- The program collecting statuses from BatteryOS and processes it.
- -MonitorOS v1.0 Pastebin: HpFunt8W (Stable)
- Updating statuses on a big screen
- -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 --
- runArgs = {...} -- If started by another program with cell and gate info
- cell = nil -- Info about what Cell the computer is monitoring
- inputGate = nil -- Gate Reader on the input side of the cell
- outputGate = nil -- Gate Reader on the output side of the cell
- modem = nil -- Side of the Modem
- hostID = 0 -- ID of the host computer where statuses are sent
- connected = false -- True if connected to the host
- status = "Empty" -- Status, default is Empty
- change = true -- Just something to keep monitor clear during main loop
- -- Get user input: Cell --
- local function getCell()
- print("Input Cell: number between 1 and 16")
- local input = io.read()
- -- Floor to get rid of decimals and check if it is within range and that it is a number
- if tonumber(input) ~= nil and math.floor(input) <= 16 and math.floor(input) >= 1 then
- cell = math.floor(input)
- print("Cell set to: "..cell)
- else
- print(input.." is an invalid Cell number, try again")
- getCell()
- end
- end
- -- Get user input: Side of the Input Gate Reader --
- local function getInputGate()
- print("Input side for Gate Reader in input cell")
- local input = io.read()
- if (input ~= nil or input ~= outputGate) and (input == "front" or
- input == "back" or input == "left" or input == "right" or
- input == "top" or input == "bottom") then
- inputGate = peripheral.wrap(input)
- print("Input Gate Reader set to: "..input)
- else
- print(input.." is an invalid position for the Input Gate")
- getInputGate()
- end
- end
- -- Get user input: Side of the Output Gate Reader --
- local function getOutputGate()
- print("Input side for Gate Reader in output cell")
- local input = io.read()
- if (input ~= nil or input ~= inputGate) and (input == "front" or
- input == "back" or input == "left" or input == "right" or
- input == "top" or input == "bottom") then
- outputGate = peripheral.wrap(input)
- print("Output Gate Reader set to: "..input)
- else
- print(input.." is an invalid position for the Output Gate")
- getOutputGate()
- end
- end
- -- Main input function to handle all inputs --
- local function getInput()
- -- If info is not sent as arguments when starting the program, get input
- if runArgs == nil then
- -- One function for each to easily handle wrong inputs
- print("Getting inputs")
- getCell()
- getInputGate()
- getOutputGate()
- print("Inputs completed")
- else
- -- Inputs were sent via arguments, no input required
- print("Info gotten trough arguments, no input required")
- if tonumber(runArgs[1]) ~= nil and math.floor(runArgs[1]) <= 16 and math.floor(runArgs[1]) >= 1 then
- cell = math.floor(runArgs[1])
- print("Cell set to: "..cell)
- else
- -- Invalid Cell number, has to be input manually
- print(runArgs[1].." is an invalid Cell number, input manually")
- getCell()
- end
- if runArgs[2] == "front" or runArgs[2] == "back" or runArgs[2] == "left" or
- runArgs[2] == "right" or runArgs[2] == "top" or runArgs[2] == "bottom" then
- inputGate = peripheral.wrap(runArgs[2])
- print("InputGate set to "..runArgs[2])
- else
- -- Invalid position, has to be input manually
- print("Invalid position, input manually")
- getInputGate()
- end
- if runArgs[3] == "front" or runArgs[3] == "back" or runArgs[3] == "left" or
- runArgs[3] == "right" or runArgs[3] == "top" or runArgs[3] == "bottom" then
- outputGate = peripheral.wrap(runArgs[3])
- print("OutputGate set to "..runArgs[3])
- else
- -- Invalid position, has to be input manually
- print("Invalid position, input manually")
- getOutputGate()
- end
- end
- end
- -- Function to detect peripherals --
- function detectModem()
- print("Detecting Modem")
- 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
- end
- end
- end
- end
- -- Send Cell info and wait for confirmation --
- function sendCell(times)
- done = false
- ID, msg = rednet.receive(1)
- if ID == hostID and msg == "Cell" then
- done = true
- print("Request for Cell received")
- rednet.send(hostID, cell)
- print("Cell info sent")
- elseif times == 4 then
- print("Timeout, abort")
- else
- print("No answer or wrong sender, try again")
- sendCell(times + 1)
- end
- return done
- end
- -- Confirm the host is trying to connect with this computer --
- function confirmConnect()
- confirmation = false
- ID, msg = rednet.receive(3)
- if ID == hostID then
- if msg == "Standby" then
- print("Connection is a go, begin connection")
- confirmation = true
- end
- elseif ID == nil then
- -- No answer, another computer got the confirmation
- print("No answer, aborting")
- else
- -- Someone else sent the message, try again
- confirmConnect()
- end
- return confirmation
- end
- -- Function to send info to the host, and confirm it has been set up properly --
- function connect(ID)
- hostID = ID
- rednet.send(hostID, "Here")
- print("Replied to call, waiting for instructions")
- if confirmConnect() then
- if sendCell(1) then
- connected = true
- end
- end
- if connected then
- print("Connection completed")
- else
- print("Connection interrupted, waiting for new call")
- end
- end
- -- Set up the monitoring computer --
- function setup()
- -- Get input from the user
- getInput()
- -- Detect peripherals
- detectModem()
- if modem == nil then
- print("No modem detected, please attach a modem and try again")
- error()
- end
- -- Open the modem --
- rednet.open(modem)
- print("Modem open and ready")
- print("Setup complete")
- end
- -- Get status from first Gate Reader --
- function getInputGateStatus()
- returnValue = ""
- data = inputGate.get()
- if data["Full Energy"] then
- returnValue = "Full"
- elseif data["Energy Stored"] then
- returnValue = "Stored"
- else
- returnValue = "Empty"
- end
- print("InputGate status: "..returnValue)
- return returnValue
- end
- -- Get status from second Gate Reader --
- function getOutputGateStatus()
- returnValue = ""
- data = outputGate.get()
- if data["Full Energy"] then
- returnValue = "Full"
- elseif data["Energy Stored"] then
- returnValue = "Stored"
- else
- returnValue = "Empty"
- end
- print("OutputGate status: "..returnValue)
- return returnValue
- end
- -- Function to update status --
- function updateStatus()
- print("Calculating status")
- -- Get status from both Gate Readers and update status
- outStatus = getOutputGateStatus()
- inStatus = getInputGateStatus()
- if outStatus == "Full" and inStatus == "Full" then
- status = 100
- elseif outStatus == "Full" and inStatus == "Stored" then
- status = 80
- elseif outStatus == "Full" and inStatus == "Empty" then
- status = 60
- elseif outStatus == "Stored" and inStatus == "Full" then
- status = 60
- elseif outStatus == "Stored" and inStatus == "Stored" then
- status = 40
- elseif outStatus == "Stored" and inStatus == "Empty" then
- status = 20
- elseif outStatus == "Empty" and inStatus == "Full" then
- status = nil
- elseif outStatus == "Empty" and inStatus == "Stored" then
- status = 10
- else
- status = 0
- end
- print("Status: "..status)
- end
- --Function to send status and receive confirmation --
- function sendStatus(times)
- print("Sending status")
- updateStatus()
- rednet.send(hostID, status)
- ID, msg = rednet.receive(1)
- if ID == hostID and msg == "Thanks" then
- print("Status sent and received")
- elseif times == 4 then
- print("Timeout, abort")
- else
- print("No answer or wrong sender, try again")
- sendStatus(times + 1)
- end
- end
- -- Clear terminal and run setup before entering the main loop --
- term.clear()
- term.setCursorPos(1,1)
- print("Running BatteryOS v1.1")
- setup()
- -- Main loop to handle incoming unscheduled messages --
- while true do
- if change then
- print("Main loop entered, waiting for message")
- change = false
- end
- ID, msg = rednet.receive()
- if msg == "Calling" then
- if not connected then
- print("Call received, initiate connection")
- connect(ID)
- change = true
- end
- elseif ID == hostID and msg == "Status" then
- print("Request for status received")
- sendStatus(1)
- change = true
- elseif msg == "Reboot" then
- print("Reboot order received, rebooting")
- rednet.close(modem)
- os.reboot()
- else
- print("Other message received: "..msg)
- change = true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement